Skip to content

Commit

Permalink
Features: add events for window resize, focus, and unfocus (cocos2d#1…
Browse files Browse the repository at this point in the history
…6578)

* add events for window resize, focus, and unfocus

* fix typo
  • Loading branch information
stevetranby authored and ricardoquesada committed Sep 21, 2016
1 parent f93c6ed commit b22efc9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cocos/platform/desktop/CCGLViewImpl-desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,24 @@ class GLFWEventHandler
}
}

static void onGLFWWindowFocusCallback(GLFWwindow* window, int focused)
{
if (_view)
{
_view->onGLFWWindowFocusCallback(window, focused);
}
}

private:
static GLViewImpl* _view;
};

GLViewImpl* GLFWEventHandler::_view = nullptr;

const std::string GLViewImpl::EVENT_WINDOW_RESIZED = "glview_window_resized";
const std::string GLViewImpl::EVENT_WINDOW_FOCUSED = "glview_window_focused";
const std::string GLViewImpl::EVENT_WINDOW_UNFOCUSED = "glview_window_unfocused";

////////////////////////////////////////////////////

struct keyCodeItem
Expand Down Expand Up @@ -411,6 +423,7 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram
glfwSetFramebufferSizeCallback(_mainWindow, GLFWEventHandler::onGLFWframebuffersize);
glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
glfwSetWindowIconifyCallback(_mainWindow, GLFWEventHandler::onGLFWWindowIconifyCallback);
glfwSetWindowFocusCallback(_mainWindow, GLFWEventHandler::onGLFWWindowFocusCallback);

setFrameSize(rect.size.width, rect.size.height);

Expand Down Expand Up @@ -838,6 +851,7 @@ void GLViewImpl::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int
setFrameSize(frameWidth, frameHeight);
setDesignResolutionSize(baseDesignSize.width, baseDesignSize.height, baseResolutionPolicy);
Director::getInstance()->setViewport();
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_RESIZED, nullptr);
}
}

Expand All @@ -853,6 +867,18 @@ void GLViewImpl::onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified)
}
}

void GLViewImpl::onGLFWWindowFocusCallback(GLFWwindow* window, int focused)
{
if (focused == GL_TRUE)
{
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_FOCUSED, nullptr);
}
else
{
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_UNFOCUSED, nullptr);
}
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
static bool glew_dynamic_binding()
{
Expand Down
7 changes: 7 additions & 0 deletions cocos/platform/desktop/CCGLViewImpl-desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class CC_DLL GLViewImpl : public GLView
void onGLFWframebuffersize(GLFWwindow* window, int w, int h);
void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height);
void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified);
void onGLFWWindowFocusCallback(GLFWwindow* window, int focused);

bool _captured;
bool _supportTouch;
Expand All @@ -153,6 +154,12 @@ class CC_DLL GLViewImpl : public GLView
float _mouseY;

friend class GLFWEventHandler;

public:
// View will trigger an event when window is resized, gains or loses focus
static const std::string EVENT_WINDOW_RESIZED;
static const std::string EVENT_WINDOW_FOCUSED;
static const std::string EVENT_WINDOW_UNFOCUSED;

private:
CC_DISALLOW_COPY_AND_ASSIGN(GLViewImpl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ EventDispatcherTests::EventDispatcherTests()
ADD_TEST_CASE(Issue4160);
ADD_TEST_CASE(DanglingNodePointersTest);
ADD_TEST_CASE(RegisterAndUnregisterWhileEventHanldingTest);
ADD_TEST_CASE(WindowEventsTest);
ADD_TEST_CASE(Issue8194);
ADD_TEST_CASE(Issue9898)
}
Expand Down Expand Up @@ -1371,6 +1372,39 @@ std::string RegisterAndUnregisterWhileEventHanldingTest::subtitle() const
return "Tap the square multiple times - should not crash!";
}

//
WindowEventsTest::WindowEventsTest()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->addCustomEventListener(GLViewImpl::EVENT_WINDOW_RESIZED, [](EventCustom* event) {
// TODO: need to create resizeable window
log("<<< WINDOW RESIZED! >>> ");
});
dispatcher->addCustomEventListener(GLViewImpl::EVENT_WINDOW_FOCUSED, [](EventCustom* event) {
log("<<< WINDOW FOCUSED! >>> ");
});
dispatcher->addCustomEventListener(GLViewImpl::EVENT_WINDOW_UNFOCUSED, [](EventCustom* event) {
log("<<< WINDOW BLURRED! >>> ");
});
#endif
}

std::string WindowEventsTest::title() const
{
return "WindowEventsTest";
}

std::string WindowEventsTest::subtitle() const
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
return "Resize and Switch to another window and back. Read Logs.";
#else
return "Unsupported platform.";
#endif
}


// https://github.com/cocos2d/cocos2d-x/issues/8194
Issue8194::Issue8194()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ class RegisterAndUnregisterWhileEventHanldingTest : public EventDispatcherTestDe
virtual std::string subtitle() const override;
};

class WindowEventsTest : public EventDispatcherTestDemo
{
public:
CREATE_FUNC(WindowEventsTest);
WindowEventsTest();

virtual std::string title() const override;
virtual std::string subtitle() const override;
};

class Issue8194 : public EventDispatcherTestDemo
{
public:
Expand Down

0 comments on commit b22efc9

Please sign in to comment.