2013-07-23 01:58:48 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Cocos2D-X Qt 5 Platform
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Jolla Ltd.
|
|
|
|
* Contact: Thomas Perl <thomas.perl@jollamobile.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
#include "CCEGLView.h"
|
|
|
|
#include "CCGL.h"
|
|
|
|
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
|
|
|
#include "touch_dispatcher/CCTouchDispatcher.h"
|
|
|
|
#include "text_input_node/CCIMEDispatcher.h"
|
|
|
|
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QWindow>
|
|
|
|
|
|
|
|
#include <QOpenGLContext>
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QTouchEvent>
|
|
|
|
|
2013-07-23 21:44:05 +08:00
|
|
|
#include <QInputMethod>
|
|
|
|
|
2013-07-23 01:58:48 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class Cocos2DQt5OpenGLIntegration : public QWindow {
|
|
|
|
public:
|
2013-07-26 16:55:46 +08:00
|
|
|
Cocos2DQt5OpenGLIntegration(EGLView *view, int width, int height);
|
2013-07-23 01:58:48 +08:00
|
|
|
~Cocos2DQt5OpenGLIntegration();
|
|
|
|
|
|
|
|
virtual void touchEvent(QTouchEvent *event);
|
|
|
|
virtual bool event(QEvent *event);
|
|
|
|
|
|
|
|
void swapBuffers();
|
|
|
|
|
|
|
|
private:
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLView *m_egl_view;
|
2013-07-23 01:58:48 +08:00
|
|
|
QOpenGLContext *m_context;
|
|
|
|
};
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
Cocos2DQt5OpenGLIntegration::Cocos2DQt5OpenGLIntegration(EGLView *view, int width, int height)
|
2013-07-23 01:58:48 +08:00
|
|
|
: m_egl_view(view)
|
|
|
|
, m_context(NULL)
|
|
|
|
{
|
|
|
|
setSurfaceType(QSurface::OpenGLSurface);
|
|
|
|
resize(width, height);
|
2013-08-01 15:20:17 +08:00
|
|
|
//showFullScreen();
|
|
|
|
show();
|
2013-07-23 01:58:48 +08:00
|
|
|
|
|
|
|
m_context = new QOpenGLContext(this);
|
|
|
|
m_context->create();
|
|
|
|
m_context->makeCurrent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cocos2DQt5OpenGLIntegration::~Cocos2DQt5OpenGLIntegration()
|
|
|
|
{
|
|
|
|
delete m_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cocos2DQt5OpenGLIntegration::touchEvent(QTouchEvent *event)
|
|
|
|
{
|
|
|
|
foreach (QTouchEvent::TouchPoint point, event->touchPoints()) {
|
|
|
|
int id = point.id();
|
|
|
|
QPointF pos = point.pos();
|
|
|
|
float x = pos.x();
|
|
|
|
float y = pos.y();
|
|
|
|
Qt::TouchPointState state = point.state();
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case Qt::TouchPointPressed:
|
|
|
|
m_egl_view->handleTouchesBegin(1, &id, &x, &y);
|
|
|
|
break;
|
|
|
|
case Qt::TouchPointMoved:
|
|
|
|
m_egl_view->handleTouchesMove(1, &id, &x, &y);
|
|
|
|
break;
|
|
|
|
case Qt::TouchPointStationary:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
case Qt::TouchPointReleased:
|
|
|
|
m_egl_view->handleTouchesEnd(1, &id, &x, &y);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Cocos2DQt5OpenGLIntegration::event(QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::Close) {
|
2013-07-26 16:55:46 +08:00
|
|
|
Director::getInstance()->end();
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return QWindow::event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cocos2DQt5OpenGLIntegration::swapBuffers()
|
|
|
|
{
|
|
|
|
m_context->swapBuffers(this);
|
|
|
|
m_context->makeCurrent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
/* Global EGLView singleton for this module */
|
|
|
|
static EGLView *
|
2013-07-23 01:58:48 +08:00
|
|
|
egl_view = NULL;
|
|
|
|
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
/** @deprecated Use getInstance() instead */
|
|
|
|
EGLView *
|
|
|
|
EGLView::sharedOpenGLView()
|
|
|
|
{
|
|
|
|
return getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLView *
|
|
|
|
EGLView::getInstance()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
if (egl_view == NULL) {
|
2013-07-26 16:55:46 +08:00
|
|
|
egl_view = new EGLView;
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return egl_view;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLView::EGLView()
|
2013-07-23 01:58:48 +08:00
|
|
|
: m_integration(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLView::~EGLView()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
if (m_integration) {
|
|
|
|
delete m_integration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLView::setFrameSize(float width, float height)
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
if (m_integration == NULL) {
|
|
|
|
m_integration = new Cocos2DQt5OpenGLIntegration(this,
|
|
|
|
(int)width, (int)height);
|
|
|
|
} else {
|
|
|
|
m_integration->resize(width, height);
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLViewProtocol::setFrameSize(width, height);
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLView::swapBuffers()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
if (m_integration != NULL) {
|
|
|
|
m_integration->swapBuffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLView::setIMEKeyboardState(bool bOpen)
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
2013-07-23 21:44:05 +08:00
|
|
|
QGuiApplication *app = static_cast<QGuiApplication*>(QGuiApplication::instance());
|
|
|
|
app->inputMethod()->setVisible(bOpen);
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-26 16:55:46 +08:00
|
|
|
EGLView::end()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
QGuiApplication::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|