Added love.window.showMessageBox.

It has two variants: showMessageBox(type, title, message [, attachtowindow=true]), and showMessageBox(type, title, message, buttons [, attachtowindow=true]). The former shows a simple message box with a single OK button, and the latter displays several buttons. The buttons argument is an array of button names.

The second variant returns the index of the pressed button, or 0 if the message box was closed some other way. showMessageBox will block until the message box is closed (and it can't be called on a separate thread.)
This commit is contained in:
Alex Szpakowski
2014-07-21 21:28:21 -03:00
parent d52bab4221
commit 3ae336dd43
6 changed files with 176 additions and 0 deletions
+59
View File
@@ -678,6 +678,65 @@ const void *Window::getHandle() const
return window;
}
SDL_MessageBoxFlags Window::convertMessageBoxType(MessageBoxType type) const
{
switch (type)
{
case MESSAGEBOX_ERROR:
return SDL_MESSAGEBOX_ERROR;
case MESSAGEBOX_WARNING:
return SDL_MESSAGEBOX_WARNING;
case MESSAGEBOX_INFO:
default:
return SDL_MESSAGEBOX_INFORMATION;
}
}
bool Window::showMessageBox(MessageBoxType type, const std::string &title, const std::string &message, bool attachtowindow)
{
SDL_MessageBoxFlags flags = convertMessageBoxType(type);
SDL_Window *sdlwindow = attachtowindow ? window : nullptr;
return SDL_ShowSimpleMessageBox(flags, title.c_str(), message.c_str(), sdlwindow) >= 0;
}
int Window::showMessageBox(const MessageBoxData &data)
{
SDL_MessageBoxData sdldata = {};
sdldata.flags = convertMessageBoxType(data.type);
sdldata.title = data.title.c_str();
sdldata.message = data.message.c_str();
sdldata.window = data.attachToWindow ? window : nullptr;
sdldata.numbuttons = (int) data.buttons.size();
std::vector<SDL_MessageBoxButtonData> sdlbuttons;
for (size_t i = 0; i < data.buttons.size(); i++)
{
SDL_MessageBoxButtonData sdlbutton = {};
sdlbutton.buttonid = (int) i;
sdlbutton.text = data.buttons[i].c_str();
if ((int) i == data.enterButtonIndex)
sdlbutton.flags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
if ((int) i == data.escapeButtonIndex)
sdlbutton.flags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
sdlbuttons.push_back(sdlbutton);
}
sdldata.buttons = &sdlbuttons[0];
int pressedbutton = -2;
SDL_ShowMessageBox(&sdldata, &pressedbutton);
return pressedbutton;
}
love::window::Window *Window::createSingleton()
{
if (!singleton)