Skip to content

Commit

Permalink
Add support for window close button calback (#1697)
Browse files Browse the repository at this point in the history
  • Loading branch information
lich426 authored Feb 18, 2024
1 parent 6477a8f commit 8e2e577
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/platform/GLViewImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ class GLFWEventHandler
}
}

static void onGLFWWindowCloseCallback(GLFWwindow* window)
{
if (_view)
{
_view->onGLFWWindowCloseCallback(window);
}
}

private:
static GLViewImpl* _view;
};
Expand All @@ -182,6 +190,7 @@ const std::string GLViewImpl::EVENT_WINDOW_POSITIONED = "glview_window_positione
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";
const std::string GLViewImpl::EVENT_WINDOW_CLOSE = "glview_window_close";

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

Expand Down Expand Up @@ -575,6 +584,7 @@ bool GLViewImpl::initWithRect(std::string_view viewName, const ax::Rect& rect, f
glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeCallback);
glfwSetWindowIconifyCallback(_mainWindow, GLFWEventHandler::onGLFWWindowIconifyCallback);
glfwSetWindowFocusCallback(_mainWindow, GLFWEventHandler::onGLFWWindowFocusCallback);
glfwSetWindowCloseCallback(_mainWindow, GLFWEventHandler::onGLFWWindowCloseCallback);

#if (AX_TARGET_PLATFORM != AX_PLATFORM_MAC)
loadGL();
Expand Down Expand Up @@ -1282,6 +1292,16 @@ void GLViewImpl::onGLFWWindowFocusCallback(GLFWwindow* /*window*/, int focused)
}
}

void GLViewImpl::onGLFWWindowCloseCallback(GLFWwindow* window)
{
bool isClose = true;
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GLViewImpl::EVENT_WINDOW_CLOSE, &isClose);
if (isClose == false)
{
glfwSetWindowShouldClose(window, 0);
}
}

#if (AX_TARGET_PLATFORM != AX_PLATFORM_MAC)
static bool loadFboExtensions()
{
Expand Down
3 changes: 3 additions & 0 deletions core/platform/GLViewImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
https://axmolengine.github.io/
Expand Down Expand Up @@ -171,6 +172,7 @@ class AX_DLL GLViewImpl : public GLView
void onGLFWWindowSizeCallback(GLFWwindow* window, int width, int height);
void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified);
void onGLFWWindowFocusCallback(GLFWwindow* window, int focused);
void onGLFWWindowCloseCallback(GLFWwindow* window);

bool _isTouchDevice = false;
bool _captured;
Expand Down Expand Up @@ -200,6 +202,7 @@ class AX_DLL GLViewImpl : public GLView
static const std::string EVENT_WINDOW_RESIZED;
static const std::string EVENT_WINDOW_FOCUSED;
static const std::string EVENT_WINDOW_UNFOCUSED;
static const std::string EVENT_WINDOW_CLOSE;

private:
AX_DISALLOW_COPY_AND_ASSIGN(GLViewImpl);
Expand Down
58 changes: 58 additions & 0 deletions tests/cpp-tests/Source/WindowTest/WindowTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ WindowTests::WindowTests()
ADD_TEST_CASE(WindowTestFullscreen2);
ADD_TEST_CASE(WindowTestFullscreen3);
ADD_TEST_CASE(WindowTestResizedAndPositioned);
ADD_TEST_CASE(WindowTestClose);
}

std::string WindowTest::title() const
Expand Down Expand Up @@ -165,4 +166,61 @@ void WindowTestResizedAndPositioned::onWindowResized(EventCustom* e)
label2->setString(StringUtils::format("size : %d, %d", (int)size->width, (int)size->height));
}

void WindowTestClose::onEnter()
{
WindowTest::onEnter();

label = nullptr;

_director->getEventDispatcher()->addCustomEventListener(GLViewImpl::EVENT_WINDOW_CLOSE,
AX_CALLBACK_1(WindowTestClose::onWindowClose, this));
}

void WindowTestClose::onExit()
{
WindowTest::onExit();
_director->getEventDispatcher()->removeCustomEventListeners(GLViewImpl::EVENT_WINDOW_CLOSE);
}

std::string WindowTestClose::subtitle() const
{
return "Window close callback test";
}

void WindowTestClose::onWindowClose(EventCustom* e)
{
auto isClose = static_cast<bool*>(e->getUserData());
if (isClose == nullptr)
return;

// false prevents the window from closing
*isClose = false;

this->stopActionByTag(1);
if (label != nullptr)
{
label->removeFromParent();
label = nullptr;
}

auto s = _director->getWinSize();
label = Label::createWithTTF(StringUtils::format("Window close button callback!"), "fonts/Marker Felt.ttf", 16.0f);
label->setPosition(s.width / 2, s.height / 2);
addChild(label);

auto delay = DelayTime::create(3.0);
auto callFunc = CallFunc::create(
[this]()
{
if (label != nullptr)
{
label->removeFromParent();
label = nullptr;
}
});
auto sequence = Sequence::create(delay, callFunc, nullptr);
sequence->setTag(1);
this->runAction(sequence);
}

#endif
14 changes: 14 additions & 0 deletions tests/cpp-tests/Source/WindowTest/WindowTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ class WindowTestResizedAndPositioned : public WindowTest
ax::Label* label2;
};

class WindowTestClose : public WindowTest
{
public:
CREATE_FUNC(WindowTestClose);
virtual void onEnter() override;
virtual void onExit() override;
virtual std::string subtitle() const override;

void onWindowClose(ax::EventCustom* e);

private:
ax::Label* label;
};

#endif /* __WINDOWTEST_H__ */

#endif

0 comments on commit 8e2e577

Please sign in to comment.