Add advance MessageBox support

This commit is contained in:
Kirigaya Kazuto 2017-06-12 13:01:28 +08:00
parent cbb780a9da
commit dd2ccfa55c
2 changed files with 175 additions and 0 deletions

View File

@ -1384,16 +1384,83 @@ namespace MiniEngine
}
}
WindowMessageBoxButton::WindowMessageBoxButton()
{
_hitoption=0;
text="Button";
callback=[](){};
}
WindowMessageBoxButton::WindowMessageBoxButton(const std::string& ButtonText,const std::function<void()>& 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<void()>& 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<mboxdata.numbuttons;i++)
{
pButtonArr[i].buttonid=i+1;
pButtonArr[i].text=box.getButtonConst(i).text.c_str();
pButtonArr[i].flags=
(box.getButtonConst(i).isHitAsEscape()) ? SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
:(
(box.getButtonConst(i).isHitAsReturn()) ?SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
:0
);
}
mboxdata.buttons=pButtonArr;
switch(box.boxtype)
{
case MessageBoxType::Error:
mboxdata.flags=SDL_MESSAGEBOX_ERROR;
break;
case MessageBoxType::Information:
mboxdata.flags=SDL_MESSAGEBOX_INFORMATION;
break;
case MessageBoxType::Warning:
mboxdata.flags=SDL_MESSAGEBOX_WARNING;
break;
}
int clickret=-2;
/// Call SDL2 to show MessageBox.
int ret=SDL_ShowMessageBox(&mboxdata,&clickret);
if(ret==0)
{
/// Success.
if(clickret>=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;

View File

@ -348,6 +348,44 @@ namespace MiniEngine
Utility, ToolTip, PopUpMenu
};
class WindowMessageBoxButton
{
public:
WindowMessageBoxButton();
WindowMessageBoxButton(const std::string& ButtonText,const std::function<void()>& CallbackFunc=[](){});
std::string text;
std::function<void()> 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<void()>& DefaultCallback=[](){});
MessageBoxType boxtype;
std::string title;
std::string text;
std::function<void()> defaultcallback;
void addButton(const WindowMessageBoxButton& button);
int getButtonNum() const;
WindowMessageBoxButton& getButton(int index);
const WindowMessageBoxButton& getButtonConst(int index) const;
private:
std::vector<WindowMessageBoxButton> _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();