From dd2ccfa55c2eb1dd39e84f0d865fec9a6a09f4e4 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 12 Jun 2017 13:01:28 +0800 Subject: [PATCH] Add advance MessageBox support --- MiniEngine.cpp | 135 +++++++++++++++++++++++++++++++++++++++++++++++++ MiniEngine.h | 40 +++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 361a7c2..8952468 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -1384,16 +1384,83 @@ namespace MiniEngine } } + WindowMessageBoxButton::WindowMessageBoxButton() + { + _hitoption=0; + text="Button"; + callback=[](){}; + } + + WindowMessageBoxButton::WindowMessageBoxButton(const std::string& ButtonText,const std::function& CallbackFunc) : text(ButtonText) + { + _hitoption=0; + callback=CallbackFunc; + } + + void WindowMessageBoxButton::setHitAsEscape(bool enable) + { + _hitoption=enable?1:0; + } + + void WindowMessageBoxButton::setHitAsReturn(bool enable) + { + _hitoption=enable?2:0; + } + + bool WindowMessageBoxButton::isHitAsEscape() const + { + return _hitoption==1; + } + + bool WindowMessageBoxButton::isHitAsReturn() const + { + return _hitoption==2; + } + + WindowMessageBox::WindowMessageBox() + { + boxtype=MessageBoxType::Information; + } + + WindowMessageBox::WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType,const std::function& DefaultCallback) : title(Title), text(Text) + { + boxtype=BoxType; + defaultcallback=DefaultCallback; + } + + void WindowMessageBox::addButton(const WindowMessageBoxButton& button) + { + _vec.push_back(button); + } + + int WindowMessageBox::getButtonNum() const + { + return _vec.size(); + } + + WindowMessageBoxButton& WindowMessageBox::getButton(int index) + { + return _vec.at(index); + } + + const WindowMessageBoxButton& WindowMessageBox::getButtonConst(int index) const + { + return _vec.at(index); + } + + //private void Window::_set(SDL_Window* p) { _wnd.reset(p,SDL_DestroyWindow); } + //private void Window::_clear() { _wnd.reset(); } + //private SDL_Window* Window::_get() const { return _wnd.get(); @@ -1596,6 +1663,74 @@ namespace MiniEngine return SDL_ShowSimpleMessageBox(flags,Title.c_str(),Message.c_str(),_get()); } + int Window::showMessageBox(const WindowMessageBox& box) const + { + SDL_MessageBoxData mboxdata; + mboxdata.title=box.title.c_str(); + mboxdata.message=box.text.c_str(); + mboxdata.window=_get(); + mboxdata.colorScheme=nullptr; + mboxdata.numbuttons=box.getButtonNum(); + SDL_MessageBoxButtonData* pButtonArr=(SDL_MessageBoxButtonData*)malloc(sizeof(SDL_MessageBoxButtonData)*(mboxdata.numbuttons)); + if(pButtonArr==nullptr) + { + /// Failed to malloc + return -2; + } + for(int i=0;i=1) + { + /// If any button is clicked, call the callback function associated with it. + if(box.getButtonConst(clickret-1).callback) + { + box.getButtonConst(clickret-1).callback(); + } + } + else + { + /// ... else, call the default callback + if(box.defaultcallback) box.defaultcallback(); + } + } + + /// Free allocated memory + free(pButtonArr); + + return ret; + } + bool Window::isScreenKeyboardShown() { return SDL_IsScreenKeyboardShown(_get())==SDL_TRUE; diff --git a/MiniEngine.h b/MiniEngine.h index 12632f4..12968a5 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -348,6 +348,44 @@ namespace MiniEngine Utility, ToolTip, PopUpMenu }; + class WindowMessageBoxButton + { + public: + WindowMessageBoxButton(); + WindowMessageBoxButton(const std::string& ButtonText,const std::function& CallbackFunc=[](){}); + + std::string text; + std::function callback; + + /// Default: no hit option set. + void setHitAsReturn(bool); + void setHitAsEscape(bool); + + bool isHitAsReturn() const; + bool isHitAsEscape() const; + private: + int _hitoption; + }; + + class WindowMessageBox + { + public: + WindowMessageBox(); + WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType=MessageBoxType::Information,const std::function& DefaultCallback=[](){}); + + MessageBoxType boxtype; + std::string title; + std::string text; + std::function defaultcallback; + + void addButton(const WindowMessageBoxButton& button); + int getButtonNum() const; + WindowMessageBoxButton& getButton(int index); + const WindowMessageBoxButton& getButtonConst(int index) const; + private: + std::vector _vec; + }; + class Window { public: @@ -399,6 +437,8 @@ namespace MiniEngine /// Use UTF8 in Title and Message please. int showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const; + int showMessageBox(const WindowMessageBox& box) const; + void show(); void hide(); void raise();