guiutil::alert text wrapping + refactor

This commit is contained in:
MihailRis
2024-01-11 03:21:14 +03:00
parent e1d30732df
commit b2b961552b
7 changed files with 60 additions and 46 deletions
+26 -7
View File
@@ -9,8 +9,6 @@
using namespace gui;
using glm::vec2;
using glm::vec4;
using std::string;
using std::wstring;
Button* guiutil::backButton(PagesControl* menu) {
return (new Button(langs::get(L"Back"), vec4(10.f)))->listenAction([=](GUI* gui) {
@@ -18,18 +16,35 @@ Button* guiutil::backButton(PagesControl* menu) {
});
}
Button* guiutil::gotoButton(wstring text, string page, PagesControl* menu) {
Button* guiutil::gotoButton(
std::wstring text,
const std::string& page,
PagesControl* menu) {
text = langs::get(text, L"menu");
return (new Button(text, vec4(10.f)))->listenAction([=](GUI* gui) {
menu->set(page);
});
}
void guiutil::alert(GUI* gui, wstring text, gui::runnable on_hidden) {
void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden) {
PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f);
panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
panel->add(new Label(text));
// TODO: implement built-in text wrapping
const int wrap_length = 60;
if (text.length() > wrap_length) {
size_t offset = 0;
int extra;
while ((extra = text.length() - offset) > 0) {
extra = std::min(extra, wrap_length);
std::wstring part = text.substr(offset, extra);
panel->add(new Label(part));
offset += extra;
}
} else {
panel->add(new Label(text));
}
panel->add((new Button(langs::get(L"Ok"), vec4(10.f)))->listenAction([=](GUI* gui) {
if (on_hidden)
on_hidden();
@@ -40,8 +55,12 @@ void guiutil::alert(GUI* gui, wstring text, gui::runnable on_hidden) {
menu->set("<alert>");
}
void guiutil::confirm(GUI* gui, wstring text, gui::runnable on_confirm,
wstring yestext, wstring notext) {
void guiutil::confirm(
GUI* gui,
const std::wstring& text,
gui::runnable on_confirm,
std::wstring yestext,
std::wstring notext) {
if (yestext.empty()) yestext = langs::get(L"Yes");
if (notext.empty()) notext = langs::get(L"No");
+3 -3
View File
@@ -10,9 +10,9 @@ namespace gui {
namespace guiutil {
gui::Button* backButton(gui::PagesControl* menu);
gui::Button* gotoButton(std::wstring text, std::string page, gui::PagesControl* menu);
void alert(gui::GUI* gui, std::wstring text, gui::runnable on_hidden=nullptr);
void confirm(gui::GUI* gui, std::wstring text, gui::runnable on_confirm=nullptr,
gui::Button* gotoButton(std::wstring text, const std::string& page, gui::PagesControl* menu);
void alert(gui::GUI* gui, const std::wstring& text, gui::runnable on_hidden=nullptr);
void confirm(gui::GUI* gui, const std::wstring& text, gui::runnable on_confirm=nullptr,
std::wstring yestext=L"", std::wstring notext=L"");
}