2014-06-20 01:30:09 +00:00
|
|
|
#include "guis/GuiMsgBox.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ButtonComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "components/MenuComponent.h"
|
2014-03-15 17:18:50 +00:00
|
|
|
|
2014-03-29 01:58:45 +00:00
|
|
|
#define HORIZONTAL_PADDING_PX 20
|
|
|
|
|
2014-03-15 17:18:50 +00:00
|
|
|
GuiMsgBox::GuiMsgBox(Window* window, const std::string& text,
|
|
|
|
const std::string& name1, const std::function<void()>& func1,
|
|
|
|
const std::string& name2, const std::function<void()>& func2,
|
|
|
|
const std::string& name3, const std::function<void()>& func3) : GuiComponent(window),
|
2017-10-28 20:24:35 +00:00
|
|
|
mBackground(window, ":/frame.png"), mGrid(window, Vector2i(1, 2))
|
2014-03-15 17:18:50 +00:00
|
|
|
{
|
|
|
|
float width = Renderer::getScreenWidth() * 0.6f; // max width
|
|
|
|
float minWidth = Renderer::getScreenWidth() * 0.3f; // minimum width
|
|
|
|
|
2014-05-01 19:47:33 +00:00
|
|
|
mMsg = std::make_shared<TextComponent>(mWindow, text, Font::get(FONT_SIZE_MEDIUM), 0x777777FF, ALIGN_CENTER);
|
2017-10-28 20:24:35 +00:00
|
|
|
mGrid.setEntry(mMsg, Vector2i(0, 0), false, false);
|
2014-03-29 01:58:45 +00:00
|
|
|
|
2014-03-15 17:18:50 +00:00
|
|
|
// create the buttons
|
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(mWindow, name1, name1, std::bind(&GuiMsgBox::deleteMeAndCall, this, func1)));
|
|
|
|
if(!name2.empty())
|
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(mWindow, name2, name3, std::bind(&GuiMsgBox::deleteMeAndCall, this, func2)));
|
|
|
|
if(!name3.empty())
|
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(mWindow, name3, name3, std::bind(&GuiMsgBox::deleteMeAndCall, this, func3)));
|
|
|
|
|
2014-03-15 22:06:16 +00:00
|
|
|
// set accelerator automatically (button to press when "b" is pressed)
|
|
|
|
if(mButtons.size() == 1)
|
|
|
|
{
|
|
|
|
mAcceleratorFunc = mButtons.front()->getPressedFunc();
|
|
|
|
}else{
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mButtons.cbegin(); it != mButtons.cend(); it++)
|
2014-03-15 22:06:16 +00:00
|
|
|
{
|
|
|
|
if(strToUpper((*it)->getText()) == "OK" || strToUpper((*it)->getText()) == "NO")
|
|
|
|
{
|
|
|
|
mAcceleratorFunc = (*it)->getPressedFunc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:18:50 +00:00
|
|
|
// put the buttons into a ComponentGrid
|
|
|
|
mButtonGrid = makeButtonGrid(mWindow, mButtons);
|
2017-10-28 20:24:35 +00:00
|
|
|
mGrid.setEntry(mButtonGrid, Vector2i(0, 1), true, false, Vector2i(1, 1), GridFlags::BORDER_TOP);
|
2014-03-15 17:18:50 +00:00
|
|
|
|
2014-03-29 01:58:45 +00:00
|
|
|
// decide final width
|
|
|
|
if(mMsg->getSize().x() < width && mButtonGrid->getSize().x() < width)
|
2014-03-15 17:18:50 +00:00
|
|
|
{
|
2014-03-29 01:58:45 +00:00
|
|
|
// mMsg and buttons are narrower than width
|
2017-11-13 22:16:38 +00:00
|
|
|
width = Math::max(mButtonGrid->getSize().x(), mMsg->getSize().x());
|
|
|
|
width = Math::max(width, minWidth);
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 01:58:45 +00:00
|
|
|
// now that we know width, we can find height
|
|
|
|
mMsg->setSize(width, 0); // mMsg->getSize.y() now returns the proper length
|
2017-11-13 22:16:38 +00:00
|
|
|
const float msgHeight = Math::max(Font::get(FONT_SIZE_LARGE)->getHeight(), mMsg->getSize().y()*1.225f);
|
2014-03-29 01:58:45 +00:00
|
|
|
setSize(width + HORIZONTAL_PADDING_PX*2, msgHeight + mButtonGrid->getSize().y());
|
2014-03-15 17:18:50 +00:00
|
|
|
|
|
|
|
// center for good measure
|
|
|
|
setPosition((Renderer::getScreenWidth() - mSize.x()) / 2.0f, (Renderer::getScreenHeight() - mSize.y()) / 2.0f);
|
|
|
|
|
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&mGrid);
|
|
|
|
}
|
|
|
|
|
2014-03-15 22:06:16 +00:00
|
|
|
bool GuiMsgBox::input(InputConfig* config, Input input)
|
|
|
|
{
|
2014-05-31 23:00:42 +00:00
|
|
|
// special case for when GuiMsgBox comes up to report errors before anything has been configured
|
|
|
|
if(config->getDeviceId() == DEVICE_KEYBOARD && !config->isConfigured() && input.value &&
|
|
|
|
(input.id == SDLK_RETURN || input.id == SDLK_ESCAPE || input.id == SDLK_SPACE))
|
|
|
|
{
|
|
|
|
mAcceleratorFunc();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-15 22:06:16 +00:00
|
|
|
if(mAcceleratorFunc && config->isMappedTo("b", input) && input.value != 0)
|
|
|
|
{
|
|
|
|
mAcceleratorFunc();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:18:50 +00:00
|
|
|
void GuiMsgBox::onSizeChanged()
|
|
|
|
{
|
|
|
|
mGrid.setSize(mSize);
|
2014-03-21 16:54:48 +00:00
|
|
|
mGrid.setRowHeightPerc(1, mButtonGrid->getSize().y() / mSize.y());
|
2014-03-29 01:58:45 +00:00
|
|
|
|
|
|
|
// update messagebox size
|
|
|
|
mMsg->setSize(mSize.x() - HORIZONTAL_PADDING_PX*2, mGrid.getRowHeight(0));
|
|
|
|
mGrid.onSizeChanged();
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
mBackground.fitTo(mSize, Vector3f::Zero(), Vector2f(-32, -32));
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiMsgBox::deleteMeAndCall(const std::function<void()>& func)
|
|
|
|
{
|
2014-05-16 22:19:25 +00:00
|
|
|
auto funcCopy = func;
|
2014-03-15 17:18:50 +00:00
|
|
|
delete this;
|
2014-05-16 22:19:25 +00:00
|
|
|
|
|
|
|
if(funcCopy)
|
|
|
|
funcCopy();
|
|
|
|
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
2014-03-24 01:33:27 +00:00
|
|
|
|
|
|
|
std::vector<HelpPrompt> GuiMsgBox::getHelpPrompts()
|
|
|
|
{
|
|
|
|
return mGrid.getHelpPrompts();
|
|
|
|
}
|