From 9fa2a453baa6951bf5db91f877b2ab3b156fd0c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Mon, 29 Jan 2018 23:30:08 -0300 Subject: [PATCH] Added Window::setCloseRequestCallback to be able to control the window close request from the window manager. --HG-- branch : dev-2.1-float --- include/eepp/ui/tools/textureatlaseditor.hpp | 3 +++ include/eepp/window/window.hpp | 6 +++++ src/eepp/ui/tools/textureatlaseditor.cpp | 11 ++++++++- src/eepp/window/input.cpp | 2 +- src/eepp/window/window.cpp | 12 ++++++++++ src/tools/mapeditor/mapeditor.cpp | 22 ++++++++++++++---- .../textureatlaseditor/textureatlaseditor.cpp | 23 +++++++++++++++++-- 7 files changed, 70 insertions(+), 9 deletions(-) diff --git a/include/eepp/ui/tools/textureatlaseditor.hpp b/include/eepp/ui/tools/textureatlaseditor.hpp index 286386d01..03fc7c901 100644 --- a/include/eepp/ui/tools/textureatlaseditor.hpp +++ b/include/eepp/ui/tools/textureatlaseditor.hpp @@ -29,6 +29,8 @@ class EE_API TextureAtlasEditor { UISpinBox * getSpinOffX() const { return mSpinOffX; } UISpinBox * getSpinOffY() const { return mSpinOffY; } + + bool isEdited() { return mEdited; } protected: class UITGEUpdater : public UINode { @@ -58,6 +60,7 @@ class EE_API TextureAtlasEditor { UIDropDownList * mTextureFilterList; TextureAtlasTextureRegionEditor * mTextureRegionEditor; UITGEUpdater * mTGEU; + bool mEdited; void windowClose( const UIEvent * Event ); diff --git a/include/eepp/window/window.hpp b/include/eepp/window/window.hpp index 5303772bd..1c5695bbb 100644 --- a/include/eepp/window/window.hpp +++ b/include/eepp/window/window.hpp @@ -152,6 +152,7 @@ class DisplayMode { class EE_API Window { public: typedef cb::Callback1 WindowResizeCallback; + typedef cb::Callback1 WindowRequestCloseCallback; Window( WindowSettings Settings, ContextSettings Context, Clipboard * Clipboard, Input * Input, CursorManager * CursorManager ); @@ -424,6 +425,8 @@ class EE_API Window { Vector2i mapCoordsToPixel(const Vector2f & point); Vector2i mapCoordsToPixel(const Vector2f & point, const View & view); + + void setCloseRequestCallback( const WindowRequestCloseCallback& closeRequestCallback ); protected: friend class Engine; friend class Input; @@ -437,6 +440,7 @@ class EE_API Window { const View * mCurrentView; Uint32 mNumCallBacks; std::map mCallbacks; + WindowRequestCloseCallback mCloseRequestCallback; class FrameData { public: @@ -494,6 +498,8 @@ class EE_API Window { void logSuccessfulInit( const std::string& BackendName ); void logFailureInit( const std::string& ClassName, const std::string& BackendName ); + + void onCloseRequest(); }; }} diff --git a/src/eepp/ui/tools/textureatlaseditor.cpp b/src/eepp/ui/tools/textureatlaseditor.cpp index 62b80b6de..8a3fb9b1c 100644 --- a/src/eepp/ui/tools/textureatlaseditor.cpp +++ b/src/eepp/ui/tools/textureatlaseditor.cpp @@ -26,7 +26,8 @@ TextureAtlasEditor::TextureAtlasEditor( UIWindow * AttatchTo, const TGEditorClos mCloseCb( callback ), mTexturePacker( NULL ), mTextureAtlasLoader( NULL ), - mCurTextureRegion( NULL ) + mCurTextureRegion( NULL ), + mEdited( false ) { if ( NULL == UIThemeManager::instance()->getDefaultTheme() ) { eePRINTL( "TextureAtlasEditor needs a default theme assigned to work." ); @@ -169,6 +170,7 @@ void TextureAtlasEditor::onResetDestSize( const UIEvent * Event ) { mSpinDestW->setValue( RealSize.getWidth() ); mSpinDestH->setValue( RealSize.getHeight() ); + mEdited = true; } } @@ -178,6 +180,7 @@ void TextureAtlasEditor::onResetOffset( const UIEvent * Event ) { if ( NULL != mCurTextureRegion && MouseEvent->getFlags() & EE_BUTTON_LMASK ) { mSpinOffX->setValue( 0 ); mSpinOffY->setValue( 0 ); + mEdited = true; } } @@ -189,6 +192,7 @@ void TextureAtlasEditor::onCenterOffset( const UIEvent * Event ) { mSpinOffX->setValue( NSize.x ); mSpinOffY->setValue( NSize.y ); + mEdited = true; } } @@ -200,18 +204,21 @@ void TextureAtlasEditor::onHBOffset( const UIEvent * Event ) { mSpinOffX->setValue( NSize.x ); mSpinOffY->setValue( NSize.y ); + mEdited = true; } } void TextureAtlasEditor::onOffXChange( const UIEvent * Event ) { if ( NULL != mCurTextureRegion ) { mCurTextureRegion->setOffset( Vector2i( (Int32)mSpinOffX->getValue(), mCurTextureRegion->getOffset().y ) ); + mEdited = true; } } void TextureAtlasEditor::onOffYChange( const UIEvent * Event ) { if ( NULL != mCurTextureRegion ) { mCurTextureRegion->setOffset( Vector2i( mCurTextureRegion->getOffset().x, (Int32)mSpinOffY->getValue() ) ); + mEdited = true; } } @@ -219,6 +226,7 @@ void TextureAtlasEditor::onDestWChange( const UIEvent * Event ) { if ( NULL != mCurTextureRegion ) { mCurTextureRegion->setOriDestSize( Sizef( (Int32)mSpinDestW->getValue(), mCurTextureRegion->getDpSize().y ) ); mTextureRegionEditor->getGfx()->setSize( (Int32)mSpinDestW->getValue(), mTextureRegionEditor->getGfx()->getSize().getHeight() ); + mEdited = true; } } @@ -226,6 +234,7 @@ void TextureAtlasEditor::onDestHChange( const UIEvent * Event ) { if ( NULL != mCurTextureRegion ) { mCurTextureRegion->setOriDestSize( Sizef( mCurTextureRegion->getDpSize().x, (Int32)mSpinDestH->getValue() ) ); mTextureRegionEditor->getGfx()->setSize( mTextureRegionEditor->getGfx()->getSize().getWidth(), (Int32)mSpinDestH->getValue() ); + mEdited = true; } } diff --git a/src/eepp/window/input.cpp b/src/eepp/window/input.cpp index fb79f5c76..1fc06b4db 100644 --- a/src/eepp/window/input.cpp +++ b/src/eepp/window/input.cpp @@ -196,7 +196,7 @@ void Input::processEvent( InputEvent * Event ) { } case InputEvent::Quit: { - mWindow->close(); + mWindow->onCloseRequest(); break; } } diff --git a/src/eepp/window/window.cpp b/src/eepp/window/window.cpp index 242b70b67..08913a484 100644 --- a/src/eepp/window/window.cpp +++ b/src/eepp/window/window.cpp @@ -145,6 +145,10 @@ Vector2i Window::mapCoordsToPixel(const Vector2f& point, const View& view) { return pixel; } +void Window::setCloseRequestCallback( const WindowRequestCloseCallback & closeRequestCallback ) { + mCloseRequestCallback = closeRequestCallback; +} + void Window::setViewport( const Int32& x, const Int32& y, const Uint32& Width, const Uint32& Height ) { GLi->viewport( x, getHeight() - ( y + Height ), Width, Height ); } @@ -435,6 +439,14 @@ void Window::logFailureInit( const std::string& ClassName, const std::string& Ba eePRINTL( "Error on %s::Init. Backend %s failed to start.", ClassName.c_str(), BackendName.c_str() ); } +void Window::onCloseRequest() { + if ( mCloseRequestCallback.IsSet() && !mCloseRequestCallback.Call( this ) ) { + return; + } + + close(); +} + std::string Window::getCaption() { return mWindow.WindowConfig.Caption; } diff --git a/src/tools/mapeditor/mapeditor.cpp b/src/tools/mapeditor/mapeditor.cpp index 23b20c983..c050011cf 100644 --- a/src/tools/mapeditor/mapeditor.cpp +++ b/src/tools/mapeditor/mapeditor.cpp @@ -2,17 +2,27 @@ EE::Window::Window * win = NULL; UIMessageBox * MsgBox = NULL; +MapEditor * Editor = NULL; -void mainLoop() { - win->getInput()->update(); - - if ( win->getInput()->isKeyUp( KEY_ESCAPE ) && NULL == MsgBox ) { +bool onCloseRequestCallback( EE::Window::Window * w ) { + if ( NULL != Editor ) { MsgBox = UIMessageBox::New( MSGBOX_OKCANCEL, "Do you really want to close the current map?\nAll changes will be lost." ); MsgBox->addEventListener( UIEvent::MsgBoxConfirmClick, cb::Make1( []( const UIEvent * event ) { win->close(); } ) ); MsgBox->addEventListener( UIEvent::OnClose, cb::Make1( []( const UIEvent * event ) { MsgBox = NULL; } ) ); MsgBox->setTitle( "Close Map?" ); MsgBox->center(); MsgBox->show(); + return false; + } else { + return true; + } +} + +void mainLoop() { + win->getInput()->update(); + + if ( win->getInput()->isKeyUp( KEY_ESCAPE ) && NULL == MsgBox && onCloseRequestCallback( win ) ) { + win->close(); } UIManager::instance()->update(); @@ -39,6 +49,8 @@ EE_MAIN_FUNC int main (int argc, char * argv []) { win = Engine::instance()->createWindow( WindowSettings( width, height, "eepp - Map Editor", WindowStyle::Default, WindowBackend::Default, 32, "assets/icon/ee.png", pixelDensity ), ContextSettings( true, GLv_default, true, 24, 1, 0, false ) ); if ( win->isOpen() ) { + win->setCloseRequestCallback( cb::Make1( onCloseRequestCallback ) ); + UIManager::instance()->init( UI_MANAGER_USE_DRAW_INVALIDATION ); { @@ -55,7 +67,7 @@ EE_MAIN_FUNC int main (int argc, char * argv []) { UIThemeManager::instance()->setDefaultEffectsEnabled( true )->setDefaultTheme( theme )->setDefaultFont( font )->add( theme ); } - MapEditor::New(); + Editor = MapEditor::New(); win->runMainLoop( &mainLoop ); } diff --git a/src/tools/textureatlaseditor/textureatlaseditor.cpp b/src/tools/textureatlaseditor/textureatlaseditor.cpp index d8e975023..169d9fff5 100644 --- a/src/tools/textureatlaseditor/textureatlaseditor.cpp +++ b/src/tools/textureatlaseditor/textureatlaseditor.cpp @@ -1,12 +1,29 @@ #include EE::Window::Window * win = NULL; +UIMessageBox * MsgBox = NULL; +TextureAtlasEditor * Editor = NULL; + +bool onCloseRequestCallback( EE::Window::Window * w ) { + if ( NULL != Editor && Editor->isEdited() ) { + MsgBox = UIMessageBox::New( MSGBOX_OKCANCEL, "Do you really want to close the texture atlas editor?\nAll changes will be lost." ); + MsgBox->addEventListener( UIEvent::MsgBoxConfirmClick, cb::Make1( []( const UIEvent * event ) { win->close(); } ) ); + MsgBox->addEventListener( UIEvent::OnClose, cb::Make1( []( const UIEvent * event ) { MsgBox = NULL; } ) ); + MsgBox->setTitle( "Close Texture Atlas Editor?" ); + MsgBox->center(); + MsgBox->show(); + return false; + } else { + return true; + } +} void mainLoop() { win->getInput()->update(); - if ( win->getInput()->isKeyDown( KEY_ESCAPE ) ) + if ( win->getInput()->isKeyUp( KEY_ESCAPE ) && NULL == MsgBox && onCloseRequestCallback( win ) ) { win->close(); + } UIManager::instance()->update(); @@ -32,6 +49,8 @@ EE_MAIN_FUNC int main (int argc, char * argv []) { win = Engine::instance()->createWindow( WindowSettings( width, height, "eepp - Texture Atlas Editor", WindowStyle::Default, WindowBackend::Default, 32, "assets/icon/ee.png", pixelDensity ), ContextSettings( true, GLv_default, true, 24, 1, 0, false ) ); if ( win->isOpen() ) { + win->setCloseRequestCallback( cb::Make1( onCloseRequestCallback ) ); + UIManager::instance()->init( UI_MANAGER_USE_DRAW_INVALIDATION ); { @@ -48,7 +67,7 @@ EE_MAIN_FUNC int main (int argc, char * argv []) { UIThemeManager::instance()->setDefaultEffectsEnabled( true )->setDefaultTheme( theme )->setDefaultFont( font )->add( theme ); } - TextureAtlasEditor::New(); + Editor = TextureAtlasEditor::New(); win->runMainLoop( &mainLoop ); }