mirror of https://github.com/axmolengine/axmol.git
Merge branch 'develop' into develop_nutty_modify_framework_for315
This commit is contained in:
commit
5ee35be0ad
|
@ -33,17 +33,18 @@ NS_CC_BEGIN
|
|||
const int FontAtlas::CacheTextureWidth = 1024;
|
||||
const int FontAtlas::CacheTextureHeight = 1024;
|
||||
|
||||
FontAtlas::FontAtlas(Font &theFont) :
|
||||
_font(&theFont),
|
||||
_currentPageData(nullptr)
|
||||
FontAtlas::FontAtlas(Font &theFont)
|
||||
: _font(&theFont)
|
||||
, _currentPageData(nullptr)
|
||||
, _fontAscender(0)
|
||||
{
|
||||
_font->retain();
|
||||
|
||||
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
|
||||
if (fontTTf)
|
||||
{
|
||||
_currentPageLineHeight = _font->getFontMaxHeight();
|
||||
_commonLineHeight = _currentPageLineHeight * 0.8f;
|
||||
_commonLineHeight = _font->getFontMaxHeight();
|
||||
_fontAscender = fontTTf->getFontAscender();
|
||||
auto texture = new Texture2D;
|
||||
_currentPage = 0;
|
||||
_currentPageOrigX = 0;
|
||||
|
@ -139,13 +140,13 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
|
|||
tempDef.width = tempRect.size.width + _letterPadding;
|
||||
tempDef.height = tempRect.size.height + _letterPadding;
|
||||
tempDef.offsetX = tempRect.origin.x + offsetAdjust;
|
||||
tempDef.offsetY = _commonLineHeight + tempRect.origin.y - offsetAdjust;
|
||||
tempDef.offsetY = _fontAscender + tempRect.origin.y - offsetAdjust;
|
||||
|
||||
if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
|
||||
{
|
||||
_currentPageOrigY += _currentPageLineHeight;
|
||||
_currentPageOrigY += _commonLineHeight;
|
||||
_currentPageOrigX = 0;
|
||||
if(_currentPageOrigY + _currentPageLineHeight >= CacheTextureHeight)
|
||||
if(_currentPageOrigY + _commonLineHeight >= CacheTextureHeight)
|
||||
{
|
||||
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize );
|
||||
_currentPageOrigY = 0;
|
||||
|
|
|
@ -91,9 +91,10 @@ private:
|
|||
int _currentPageDataSize;
|
||||
float _currentPageOrigX;
|
||||
float _currentPageOrigY;
|
||||
float _currentPageLineHeight;
|
||||
float _letterPadding;
|
||||
bool _makeDistanceMap;
|
||||
|
||||
int _fontAscender;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -162,24 +162,26 @@ FontAtlas * FontFreeType::createFontAtlas()
|
|||
|
||||
int * FontFreeType::getHorizontalKerningForTextUTF16(unsigned short *text, int &outNumLetters) const
|
||||
{
|
||||
if (!text)
|
||||
return 0;
|
||||
if (!text || !_fontRef)
|
||||
return nullptr;
|
||||
|
||||
outNumLetters = cc_wcslen(text);
|
||||
|
||||
if (!outNumLetters)
|
||||
return 0;
|
||||
return nullptr;
|
||||
|
||||
int *sizes = new int[outNumLetters];
|
||||
if (!sizes)
|
||||
return 0;
|
||||
return nullptr;
|
||||
memset(sizes,0,outNumLetters * sizeof(int));
|
||||
|
||||
for (int c = 0; c < outNumLetters; ++c)
|
||||
bool hasKerning = FT_HAS_KERNING( _fontRef ) != 0;
|
||||
if (hasKerning)
|
||||
{
|
||||
if (c < (outNumLetters-1))
|
||||
sizes[c] = getHorizontalKerningForChars(text[c], text[c+1]);
|
||||
else
|
||||
sizes[c] = 0;
|
||||
for (int c = 1; c < outNumLetters; ++c)
|
||||
{
|
||||
sizes[c] = getHorizontalKerningForChars(text[c-1], text[c]);
|
||||
}
|
||||
}
|
||||
|
||||
return sizes;
|
||||
|
@ -187,14 +189,6 @@ int * FontFreeType::getHorizontalKerningForTextUTF16(unsigned short *text, int &
|
|||
|
||||
int FontFreeType::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const
|
||||
{
|
||||
if (!_fontRef)
|
||||
return 0;
|
||||
|
||||
bool hasKerning = FT_HAS_KERNING( _fontRef ) != 0;
|
||||
|
||||
if (!hasKerning)
|
||||
return 0;
|
||||
|
||||
// get the ID to the char we need
|
||||
int glyphIndex1 = FT_Get_Char_Index(_fontRef, firstChar);
|
||||
|
||||
|
@ -220,6 +214,11 @@ int FontFreeType::getFontMaxHeight() const
|
|||
return (static_cast<int>(_fontRef->size->metrics.height >> 6));
|
||||
}
|
||||
|
||||
int FontFreeType::getFontAscender() const
|
||||
{
|
||||
return (static_cast<int>(_fontRef->size->metrics.ascender >> 6));
|
||||
}
|
||||
|
||||
unsigned char* FontFreeType::getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight, Rect &outRect,int &xAdvance)
|
||||
{
|
||||
bool invalidChar = true;
|
||||
|
|
|
@ -56,6 +56,7 @@ public:
|
|||
unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight, Rect &outRect,int &xAdvance);
|
||||
|
||||
virtual int getFontMaxHeight() const override;
|
||||
virtual int getFontAscender() const;
|
||||
|
||||
protected:
|
||||
|
||||
|
|
|
@ -87,36 +87,31 @@ void GLViewProtocol::pollInputEvents()
|
|||
{
|
||||
}
|
||||
|
||||
void GLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
|
||||
|
||||
void GLViewProtocol::updateDesignResolutionSize()
|
||||
{
|
||||
CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN, "should set resolutionPolicy");
|
||||
|
||||
if (width == 0.0f || height == 0.0f)
|
||||
if (_screenSize.width > 0 && _screenSize.height > 0
|
||||
&& _designResolutionSize.width > 0 && _designResolutionSize.height > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_designResolutionSize.setSize(width, height);
|
||||
|
||||
_scaleX = (float)_screenSize.width / _designResolutionSize.width;
|
||||
_scaleY = (float)_screenSize.height / _designResolutionSize.height;
|
||||
|
||||
if (resolutionPolicy == ResolutionPolicy::NO_BORDER)
|
||||
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
|
||||
{
|
||||
_scaleX = _scaleY = MAX(_scaleX, _scaleY);
|
||||
}
|
||||
|
||||
else if (resolutionPolicy == ResolutionPolicy::SHOW_ALL)
|
||||
else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
|
||||
{
|
||||
_scaleX = _scaleY = MIN(_scaleX, _scaleY);
|
||||
}
|
||||
|
||||
else if ( resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
|
||||
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
|
||||
_scaleX = _scaleY;
|
||||
_designResolutionSize.width = ceilf(_screenSize.width/_scaleX);
|
||||
}
|
||||
|
||||
else if ( resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
|
||||
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
|
||||
_scaleY = _scaleX;
|
||||
_designResolutionSize.height = ceilf(_screenSize.height/_scaleY);
|
||||
}
|
||||
|
@ -127,15 +122,29 @@ void GLViewProtocol::setDesignResolutionSize(float width, float height, Resoluti
|
|||
|
||||
_viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH);
|
||||
|
||||
_resolutionPolicy = resolutionPolicy;
|
||||
|
||||
// reset director's member variables to fit visible rect
|
||||
auto director = Director::getInstance();
|
||||
director->_winSizeInPoints = getDesignResolutionSize();
|
||||
director->createStatsLabel();
|
||||
director->setGLDefaultValues();
|
||||
}
|
||||
}
|
||||
|
||||
void GLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
|
||||
{
|
||||
CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN, "should set resolutionPolicy");
|
||||
|
||||
if (width == 0.0f || height == 0.0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_designResolutionSize.setSize(width, height);
|
||||
_resolutionPolicy = resolutionPolicy;
|
||||
|
||||
updateDesignResolutionSize();
|
||||
}
|
||||
|
||||
const Size& GLViewProtocol::getDesignResolutionSize() const
|
||||
{
|
||||
return _designResolutionSize;
|
||||
|
|
|
@ -186,6 +186,8 @@ public:
|
|||
ResolutionPolicy getResolutionPolicy() const { return _resolutionPolicy; }
|
||||
|
||||
protected:
|
||||
void updateDesignResolutionSize();
|
||||
|
||||
void handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, int ids[], float xs[], float ys[]);
|
||||
|
||||
// real screen size
|
||||
|
|
|
@ -37,6 +37,78 @@ THE SOFTWARE.
|
|||
|
||||
NS_CC_BEGIN
|
||||
|
||||
// GLFWEventHandler
|
||||
|
||||
class GLFWEventHandler
|
||||
{
|
||||
public:
|
||||
static void onGLFWError(int errorID, const char* errorDesc)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWError(errorID, errorDesc);
|
||||
}
|
||||
|
||||
static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWMouseCallBack(window, button, action, modify);
|
||||
}
|
||||
|
||||
static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWMouseMoveCallBack(window, x, y);
|
||||
}
|
||||
|
||||
static void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWMouseScrollCallback(window, x, y);
|
||||
}
|
||||
|
||||
static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWKeyCallback(window, key, scancode, action, mods);
|
||||
}
|
||||
|
||||
static void onGLFWCharCallback(GLFWwindow* window, unsigned int character)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWCharCallback(window, character);
|
||||
}
|
||||
|
||||
static void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWWindowPosCallback(windows, x, y);
|
||||
}
|
||||
|
||||
static void onGLFWframebuffersize(GLFWwindow* window, int w, int h)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWframebuffersize(window, w, h);
|
||||
}
|
||||
|
||||
static void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
|
||||
{
|
||||
if (_view)
|
||||
_view->onGLFWWindowSizeFunCallback(window, width, height);
|
||||
}
|
||||
|
||||
static void setGLView(GLView* view)
|
||||
{
|
||||
_view = view;
|
||||
}
|
||||
|
||||
private:
|
||||
static GLView* _view;
|
||||
};
|
||||
|
||||
GLView* GLFWEventHandler::_view = nullptr;
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
struct keyCodeItem
|
||||
{
|
||||
int glfwKeyCode;
|
||||
|
@ -175,186 +247,43 @@ static keyCodeItem g_keyCodeStructArray[] = {
|
|||
{ GLFW_KEY_LAST , EventKeyboard::KeyCode::KEY_NONE }
|
||||
};
|
||||
|
||||
|
||||
//begin GLViewEventHandler
|
||||
class GLViewEventHandler
|
||||
{
|
||||
public:
|
||||
static bool s_captured;
|
||||
static float s_mouseX;
|
||||
static float s_mouseY;
|
||||
|
||||
static void onGLFWError(int errorID, const char* errorDesc);
|
||||
static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
|
||||
static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y);
|
||||
static void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y);
|
||||
static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
static void onGLFWCharCallback(GLFWwindow* window, unsigned int character);
|
||||
static void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);
|
||||
static void onGLFWframebuffersize(GLFWwindow* window, int w, int h);
|
||||
static void OnGLFWWindowSizeFunCallback(GLFWwindow *windows, int width, int height);
|
||||
};
|
||||
|
||||
bool GLViewEventHandler::s_captured = false;
|
||||
float GLViewEventHandler::s_mouseX = 0;
|
||||
float GLViewEventHandler::s_mouseY = 0;
|
||||
|
||||
void GLViewEventHandler::onGLFWError(int errorID, const char* errorDesc)
|
||||
{
|
||||
CCLOGERROR("GLFWError #%d Happen, %s\n", errorID, errorDesc);
|
||||
}
|
||||
|
||||
void GLViewEventHandler::onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
|
||||
{
|
||||
GLView* eglView = Director::getInstance()->getOpenGLView();
|
||||
if(nullptr == eglView) return;
|
||||
if(GLFW_MOUSE_BUTTON_LEFT == button)
|
||||
{
|
||||
if(GLFW_PRESS == action)
|
||||
{
|
||||
s_captured = true;
|
||||
if (eglView->getViewPortRect().equals(Rect::ZERO) || eglView->getViewPortRect().containsPoint(Point(s_mouseX,s_mouseY)))
|
||||
{
|
||||
int id = 0;
|
||||
eglView->handleTouchesBegin(1, &id, &s_mouseX, &s_mouseY);
|
||||
}
|
||||
}
|
||||
else if(GLFW_RELEASE == action)
|
||||
{
|
||||
s_captured = false;
|
||||
if (eglView->getViewPortRect().equals(Rect::ZERO) || eglView->getViewPortRect().containsPoint(Point(s_mouseX,s_mouseY)))
|
||||
{
|
||||
int id = 0;
|
||||
eglView->handleTouchesEnd(1, &id, &s_mouseX, &s_mouseY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(GLFW_PRESS == action)
|
||||
{
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
event.setMouseButton(button);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
else if(GLFW_RELEASE == action)
|
||||
{
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
event.setMouseButton(button);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
}
|
||||
|
||||
void GLViewEventHandler::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
|
||||
{
|
||||
GLView* eglView = Director::getInstance()->getOpenGLView();
|
||||
if(nullptr == eglView) return;
|
||||
|
||||
if (eglView->isRetina()) {
|
||||
x *= 2;
|
||||
y *= 2;
|
||||
}
|
||||
|
||||
s_mouseX = (float)x;
|
||||
s_mouseY = (float)y;
|
||||
|
||||
s_mouseX /= eglView->getFrameZoomFactor();
|
||||
s_mouseY /= eglView->getFrameZoomFactor();
|
||||
|
||||
if(s_captured)
|
||||
{
|
||||
if (eglView->getViewPortRect().equals(Rect::ZERO) || eglView->getViewPortRect().containsPoint(Point(s_mouseX,eglView->getFrameSize().height - s_mouseY)))
|
||||
{
|
||||
int id = 0;
|
||||
eglView->handleTouchesMove(1, &id, &s_mouseX, &s_mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
void GLViewEventHandler::onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
|
||||
{
|
||||
GLView* eglView = Director::getInstance()->getOpenGLView();
|
||||
if(nullptr == eglView) return;
|
||||
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setScrollData((float)x, -(float)y);
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
void GLViewEventHandler::onGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
if (GLFW_REPEAT != action)
|
||||
{
|
||||
EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action);
|
||||
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
||||
dispatcher->dispatchEvent(&event);
|
||||
}
|
||||
}
|
||||
|
||||
void GLViewEventHandler::onGLFWCharCallback(GLFWwindow *window, unsigned int character)
|
||||
{
|
||||
IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*) &character, 1);
|
||||
}
|
||||
|
||||
void GLViewEventHandler::onGLFWWindowPosCallback(GLFWwindow *windows, int x, int y)
|
||||
{
|
||||
Director::getInstance()->setViewport();
|
||||
}
|
||||
|
||||
void GLViewEventHandler::onGLFWframebuffersize(GLFWwindow* window, int w, int h)
|
||||
{
|
||||
auto view = Director::getInstance()->getOpenGLView();
|
||||
|
||||
float frameSizeW = view->getFrameSize().width;
|
||||
float frameSizeH = view->getFrameSize().height;
|
||||
float factorX = frameSizeW / w * view->getFrameZoomFactor();
|
||||
float factorY = frameSizeH / h * view->getFrameZoomFactor();;
|
||||
|
||||
if (fabs(factorX - 0.5f) < FLT_EPSILON && fabs(factorY - 0.5f) < FLT_EPSILON )
|
||||
{
|
||||
view->_isRetina = true;
|
||||
view->setFrameZoomFactor(2.0f * view->getFrameZoomFactor());
|
||||
glfwSetWindowSize(window, static_cast<int>(frameSizeW * 0.5f * view->getFrameZoomFactor()) , static_cast<int>(frameSizeH * 0.5f * view->getFrameZoomFactor()));
|
||||
}
|
||||
else if(fabs(factorX - 2.0f) < FLT_EPSILON && fabs(factorY - 2.0f) < FLT_EPSILON)
|
||||
{
|
||||
view->_isRetina = false;
|
||||
view->setFrameZoomFactor(0.5f * view->getFrameZoomFactor());
|
||||
glfwSetWindowSize(window, static_cast<int>(frameSizeW * view->getFrameZoomFactor()), static_cast<int>(frameSizeH * view->getFrameZoomFactor()));
|
||||
}
|
||||
}
|
||||
|
||||
void GLViewEventHandler::OnGLFWWindowSizeFunCallback(GLFWwindow *windows, int width, int height)
|
||||
{
|
||||
auto view = Director::getInstance()->getOpenGLView();
|
||||
if(view && view->getResolutionPolicy() != ResolutionPolicy::UNKNOWN)
|
||||
{
|
||||
Size resSize=view->getDesignResolutionSize();
|
||||
ResolutionPolicy resPolicy=view->getResolutionPolicy();
|
||||
view->setFrameSize(width, height);
|
||||
view->setDesignResolutionSize(resSize.width, resSize.height, resPolicy);
|
||||
Director::getInstance()->setViewport();
|
||||
}
|
||||
}
|
||||
|
||||
//end GLViewEventHandler
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// implement GLView
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
GLView::GLView()
|
||||
: _captured(false)
|
||||
, _supportTouch(false)
|
||||
, _isInRetinaMonitor(false)
|
||||
, _isRetinaEnabled(false)
|
||||
, _retinaFactor(1)
|
||||
, _frameZoomFactor(1.0f)
|
||||
, _mainWindow(nullptr)
|
||||
, _primaryMonitor(nullptr)
|
||||
, _mouseX(0.0f)
|
||||
, _mouseY(0.0f)
|
||||
{
|
||||
_viewName = "cocos2dx";
|
||||
g_keyCodeMap.clear();
|
||||
for (auto& item : g_keyCodeStructArray)
|
||||
{
|
||||
g_keyCodeMap[item.glfwKeyCode] = item.keyCode;
|
||||
}
|
||||
|
||||
GLFWEventHandler::setGLView(this);
|
||||
|
||||
glfwSetErrorCallback(GLFWEventHandler::onGLFWError);
|
||||
glfwInit();
|
||||
}
|
||||
|
||||
GLView::~GLView()
|
||||
{
|
||||
CCLOGINFO("deallocing GLView: %p", this);
|
||||
GLFWEventHandler::setGLView(nullptr);
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
GLView* GLView::create(const std::string& viewName)
|
||||
{
|
||||
auto ret = new GLView;
|
||||
|
@ -388,65 +317,32 @@ GLView* GLView::createWithFullScreen(const std::string& viewName)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
GLView::GLView()
|
||||
: _captured(false)
|
||||
, _frameZoomFactor(1.0f)
|
||||
, _supportTouch(false)
|
||||
, _isRetina(false)
|
||||
, _mainWindow(nullptr)
|
||||
, _primaryMonitor(nullptr)
|
||||
{
|
||||
_viewName = "cocos2dx";
|
||||
g_keyCodeMap.clear();
|
||||
for (auto& item : g_keyCodeStructArray)
|
||||
{
|
||||
g_keyCodeMap[item.glfwKeyCode] = item.keyCode;
|
||||
}
|
||||
glfwSetErrorCallback(GLViewEventHandler::onGLFWError);
|
||||
glfwInit();
|
||||
}
|
||||
|
||||
GLView::~GLView()
|
||||
{
|
||||
CCLOGINFO("deallocing GLView: %p", this);
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
bool GLView::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
|
||||
{
|
||||
setViewName(viewName);
|
||||
setFrameSize(rect.size.width, rect.size.height);
|
||||
setFrameZoomFactor(frameZoomFactor);
|
||||
|
||||
_frameZoomFactor = frameZoomFactor;
|
||||
|
||||
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
|
||||
|
||||
_mainWindow = glfwCreateWindow(_screenSize.width * _frameZoomFactor,
|
||||
_screenSize.height * _frameZoomFactor,
|
||||
_mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor,
|
||||
rect.size.height * _frameZoomFactor,
|
||||
_viewName.c_str(),
|
||||
_primaryMonitor,
|
||||
NULL);
|
||||
nullptr);
|
||||
glfwMakeContextCurrent(_mainWindow);
|
||||
|
||||
int w, h;
|
||||
glfwGetWindowSize(_mainWindow, &w, &h);
|
||||
int frameBufferW, frameBufferH;
|
||||
glfwGetFramebufferSize(_mainWindow, &frameBufferW, &frameBufferH);
|
||||
glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);
|
||||
glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack);
|
||||
glfwSetScrollCallback(_mainWindow, GLFWEventHandler::onGLFWMouseScrollCallback);
|
||||
glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback);
|
||||
glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback);
|
||||
glfwSetWindowPosCallback(_mainWindow, GLFWEventHandler::onGLFWWindowPosCallback);
|
||||
glfwSetFramebufferSizeCallback(_mainWindow, GLFWEventHandler::onGLFWframebuffersize);
|
||||
glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
|
||||
|
||||
if (frameBufferW == 2 * w && frameBufferH == 2 * h)
|
||||
{
|
||||
_isRetina = true;
|
||||
setFrameZoomFactor(frameZoomFactor * 2);
|
||||
glfwSetWindowSize(_mainWindow, rect.size.width/2 * _frameZoomFactor, rect.size.height/2 * _frameZoomFactor);
|
||||
}
|
||||
setFrameSize(rect.size.width, rect.size.height);
|
||||
|
||||
glfwSetMouseButtonCallback(_mainWindow, GLViewEventHandler::onGLFWMouseCallBack);
|
||||
glfwSetCursorPosCallback(_mainWindow, GLViewEventHandler::onGLFWMouseMoveCallBack);
|
||||
glfwSetScrollCallback(_mainWindow, GLViewEventHandler::onGLFWMouseScrollCallback);
|
||||
glfwSetCharCallback(_mainWindow, GLViewEventHandler::onGLFWCharCallback);
|
||||
glfwSetKeyCallback(_mainWindow, GLViewEventHandler::onGLFWKeyCallback);
|
||||
glfwSetWindowPosCallback(_mainWindow, GLViewEventHandler::onGLFWWindowPosCallback);
|
||||
glfwSetFramebufferSizeCallback(_mainWindow, GLViewEventHandler::onGLFWframebuffersize);
|
||||
glfwSetWindowSizeCallback(_mainWindow, GLViewEventHandler::OnGLFWWindowSizeFunCallback);
|
||||
// check OpenGL version at first
|
||||
const GLubyte* glVersion = glGetString(GL_VERSION);
|
||||
|
||||
|
@ -512,6 +408,24 @@ void GLView::pollEvents()
|
|||
glfwPollEvents();
|
||||
}
|
||||
|
||||
|
||||
void GLView::enableRetina(bool enabled)
|
||||
{
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
||||
_isRetinaEnabled = enabled;
|
||||
if (_isRetinaEnabled)
|
||||
{
|
||||
_retinaFactor = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_retinaFactor = 2;
|
||||
}
|
||||
updateFrameSize();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void GLView::setIMEKeyboardState(bool /*bOpen*/)
|
||||
{
|
||||
|
||||
|
@ -519,8 +433,15 @@ void GLView::setIMEKeyboardState(bool /*bOpen*/)
|
|||
|
||||
void GLView::setFrameZoomFactor(float zoomFactor)
|
||||
{
|
||||
CCASSERT(zoomFactor > 0.0f, "zoomFactor must be larger than 0");
|
||||
|
||||
if (fabs(_frameZoomFactor - zoomFactor) < FLT_EPSILON)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_frameZoomFactor = zoomFactor;
|
||||
Director::getInstance()->setProjection(Director::getInstance()->getProjection());
|
||||
updateFrameSize();
|
||||
}
|
||||
|
||||
float GLView::getFrameZoomFactor()
|
||||
|
@ -528,25 +449,209 @@ float GLView::getFrameZoomFactor()
|
|||
return _frameZoomFactor;
|
||||
}
|
||||
|
||||
void GLView::updateFrameSize()
|
||||
{
|
||||
if (_screenSize.width > 0 && _screenSize.height > 0)
|
||||
{
|
||||
int w = 0, h = 0;
|
||||
glfwGetWindowSize(_mainWindow, &w, &h);
|
||||
|
||||
int frameBufferW = 0, frameBufferH = 0;
|
||||
glfwGetFramebufferSize(_mainWindow, &frameBufferW, &frameBufferH);
|
||||
|
||||
if (frameBufferW == 2 * w && frameBufferH == 2 * h)
|
||||
{
|
||||
if (_isRetinaEnabled)
|
||||
{
|
||||
_retinaFactor = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_retinaFactor = 2;
|
||||
}
|
||||
glfwSetWindowSize(_mainWindow, _screenSize.width/2 * _retinaFactor * _frameZoomFactor, _screenSize.height/2 * _retinaFactor * _frameZoomFactor);
|
||||
|
||||
_isInRetinaMonitor = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_isInRetinaMonitor)
|
||||
{
|
||||
_retinaFactor = 1;
|
||||
}
|
||||
glfwSetWindowSize(_mainWindow, _screenSize.width * _retinaFactor * _frameZoomFactor, _screenSize.height *_retinaFactor * _frameZoomFactor);
|
||||
|
||||
_isInRetinaMonitor = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GLView::setFrameSize(float width, float height)
|
||||
{
|
||||
GLViewProtocol::setFrameSize(width, height);
|
||||
updateFrameSize();
|
||||
}
|
||||
|
||||
void GLView::setViewPortInPoints(float x , float y , float w , float h)
|
||||
{
|
||||
glViewport((GLint)(x * _scaleX * _frameZoomFactor + _viewPortRect.origin.x * _frameZoomFactor),
|
||||
(GLint)(y * _scaleY * _frameZoomFactor + _viewPortRect.origin.y * _frameZoomFactor),
|
||||
(GLsizei)(w * _scaleX * _frameZoomFactor),
|
||||
(GLsizei)(h * _scaleY * _frameZoomFactor));
|
||||
glViewport((GLint)(x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor),
|
||||
(GLint)(y * _scaleY * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor),
|
||||
(GLsizei)(w * _scaleX * _retinaFactor * _frameZoomFactor),
|
||||
(GLsizei)(h * _scaleY * _retinaFactor * _frameZoomFactor));
|
||||
}
|
||||
|
||||
void GLView::setScissorInPoints(float x , float y , float w , float h)
|
||||
{
|
||||
glScissor((GLint)(x * _scaleX * _frameZoomFactor + _viewPortRect.origin.x * _frameZoomFactor),
|
||||
(GLint)(y * _scaleY * _frameZoomFactor + _viewPortRect.origin.y * _frameZoomFactor),
|
||||
(GLsizei)(w * _scaleX * _frameZoomFactor),
|
||||
(GLsizei)(h * _scaleY * _frameZoomFactor));
|
||||
glScissor((GLint)(x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor),
|
||||
(GLint)(y * _scaleY * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor),
|
||||
(GLsizei)(w * _scaleX * _retinaFactor * _frameZoomFactor),
|
||||
(GLsizei)(h * _scaleY * _retinaFactor * _frameZoomFactor));
|
||||
}
|
||||
|
||||
void GLView::onGLFWError(int errorID, const char* errorDesc)
|
||||
{
|
||||
CCLOGERROR("GLFWError #%d Happen, %s\n", errorID, errorDesc);
|
||||
}
|
||||
|
||||
void GLView::onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
|
||||
{
|
||||
if(GLFW_MOUSE_BUTTON_LEFT == button)
|
||||
{
|
||||
if(GLFW_PRESS == action)
|
||||
{
|
||||
_captured = true;
|
||||
if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Point(_mouseX,_mouseY)))
|
||||
{
|
||||
int id = 0;
|
||||
this->handleTouchesBegin(1, &id, &_mouseX, &_mouseY);
|
||||
}
|
||||
}
|
||||
else if(GLFW_RELEASE == action)
|
||||
{
|
||||
_captured = false;
|
||||
if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Point(_mouseX,_mouseY)))
|
||||
{
|
||||
int id = 0;
|
||||
this->handleTouchesEnd(1, &id, &_mouseX, &_mouseY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(GLFW_PRESS == action)
|
||||
{
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
|
||||
event.setMouseButton(button);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
else if(GLFW_RELEASE == action)
|
||||
{
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
|
||||
event.setMouseButton(button);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
}
|
||||
|
||||
void GLView::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
|
||||
{
|
||||
_mouseX = (float)x;
|
||||
_mouseY = (float)y;
|
||||
|
||||
_mouseX /= this->getFrameZoomFactor();
|
||||
_mouseY /= this->getFrameZoomFactor();
|
||||
|
||||
if (_isInRetinaMonitor)
|
||||
{
|
||||
if (_retinaFactor == 1)
|
||||
{
|
||||
_mouseX *= 2;
|
||||
_mouseY *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (_captured)
|
||||
{
|
||||
if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Point(_mouseX, _mouseY)))
|
||||
{
|
||||
int id = 0;
|
||||
this->handleTouchesMove(1, &id, &_mouseX, &_mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
void GLView::onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
|
||||
{
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setScrollData((float)x, -(float)y);
|
||||
event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
void GLView::onGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
if (GLFW_REPEAT != action)
|
||||
{
|
||||
EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action);
|
||||
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
||||
dispatcher->dispatchEvent(&event);
|
||||
}
|
||||
}
|
||||
|
||||
void GLView::onGLFWCharCallback(GLFWwindow *window, unsigned int character)
|
||||
{
|
||||
IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*) &character, 1);
|
||||
}
|
||||
|
||||
void GLView::onGLFWWindowPosCallback(GLFWwindow *windows, int x, int y)
|
||||
{
|
||||
Director::getInstance()->setViewport();
|
||||
}
|
||||
|
||||
void GLView::onGLFWframebuffersize(GLFWwindow* window, int w, int h)
|
||||
{
|
||||
float frameSizeW = _screenSize.width;
|
||||
float frameSizeH = _screenSize.height;
|
||||
float factorX = frameSizeW / w * _retinaFactor * _frameZoomFactor;
|
||||
float factorY = frameSizeH / h * _retinaFactor * _frameZoomFactor;
|
||||
|
||||
if (fabs(factorX - 0.5f) < FLT_EPSILON && fabs(factorY - 0.5f) < FLT_EPSILON )
|
||||
{
|
||||
_isInRetinaMonitor = true;
|
||||
if (_isRetinaEnabled)
|
||||
{
|
||||
_retinaFactor = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_retinaFactor = 2;
|
||||
}
|
||||
|
||||
glfwSetWindowSize(window, static_cast<int>(frameSizeW * 0.5f * _retinaFactor * _frameZoomFactor) , static_cast<int>(frameSizeH * 0.5f * _retinaFactor * _frameZoomFactor));
|
||||
}
|
||||
else if(fabs(factorX - 2.0f) < FLT_EPSILON && fabs(factorY - 2.0f) < FLT_EPSILON)
|
||||
{
|
||||
_isInRetinaMonitor = false;
|
||||
_retinaFactor = 1;
|
||||
glfwSetWindowSize(window, static_cast<int>(frameSizeW * _retinaFactor * _frameZoomFactor), static_cast<int>(frameSizeH * _retinaFactor * _frameZoomFactor));
|
||||
}
|
||||
}
|
||||
|
||||
void GLView::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
|
||||
{
|
||||
if (_resolutionPolicy != ResolutionPolicy::UNKNOWN)
|
||||
{
|
||||
updateDesignResolutionSize();
|
||||
Director::getInstance()->setViewport();
|
||||
}
|
||||
}
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
||||
|
|
|
@ -64,6 +64,18 @@ public:
|
|||
virtual void setFrameSize(float width, float height) override;
|
||||
virtual void setIMEKeyboardState(bool bOpen) override;
|
||||
|
||||
/*
|
||||
* Set zoom factor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.
|
||||
*/
|
||||
void setFrameZoomFactor(float zoomFactor);
|
||||
|
||||
/** Retina support is disabled by default
|
||||
* @note This method is only available on Mac.
|
||||
*/
|
||||
void enableRetina(bool enabled);
|
||||
/** Check whether retina display is enabled. */
|
||||
bool isRetinaEnabled() { return _isRetinaEnabled; };
|
||||
|
||||
protected:
|
||||
GLView();
|
||||
virtual ~GLView();
|
||||
|
@ -71,22 +83,36 @@ protected:
|
|||
bool initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor);
|
||||
bool initWithFullScreen(const std::string& viewName);
|
||||
|
||||
/*
|
||||
* Set zoom factor for frame. This method is for debugging big resolution (e.g.new ipad) app on desktop.
|
||||
*/
|
||||
void setFrameZoomFactor(float zoomFactor);
|
||||
bool initGlew();
|
||||
inline bool isRetina() { return _isRetina; };
|
||||
|
||||
void updateFrameSize();
|
||||
|
||||
// GLFW callbacks
|
||||
void onGLFWError(int errorID, const char* errorDesc);
|
||||
void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
|
||||
void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y);
|
||||
void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y);
|
||||
void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
void onGLFWCharCallback(GLFWwindow* window, unsigned int character);
|
||||
void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);
|
||||
void onGLFWframebuffersize(GLFWwindow* window, int w, int h);
|
||||
void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height);
|
||||
|
||||
bool _captured;
|
||||
bool _supportTouch;
|
||||
bool _isRetina;
|
||||
bool _isInRetinaMonitor;
|
||||
bool _isRetinaEnabled;
|
||||
int _retinaFactor; // Should be 1 or 2
|
||||
|
||||
float _frameZoomFactor;
|
||||
|
||||
GLFWwindow* _mainWindow;
|
||||
GLFWmonitor* _primaryMonitor;
|
||||
friend class GLViewEventHandler;
|
||||
|
||||
float _mouseX;
|
||||
float _mouseY;
|
||||
|
||||
friend class GLFWEventHandler;
|
||||
|
||||
private:
|
||||
CC_DISALLOW_COPY_AND_ASSIGN(GLView);
|
||||
|
|
|
@ -27,8 +27,10 @@ THE SOFTWARE.
|
|||
#include "cocos2d.h"
|
||||
#include "CocoStudio.h"
|
||||
|
||||
class cocos2d::EventDispatcher;
|
||||
class cocos2d::EventListener;
|
||||
namespace cocos2d {
|
||||
class EventDispatcher;
|
||||
class EventListener;
|
||||
}
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
|
|
|
@ -29,7 +29,9 @@ THE SOFTWARE.
|
|||
#include "CocoStudio.h"
|
||||
#include <vector>
|
||||
|
||||
class cocos2d::EventListenerCustom;
|
||||
namespace cocos2d {
|
||||
class EventListenerCustom;
|
||||
}
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
|
|
Loading…
Reference in New Issue