Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
AlertDialog.cpp
1#include "AlertDialog.hpp"
2#include "Container.hpp"
3#include "Constraint.hpp"
4#include "Button.hpp"
5
6AlertDialog::AlertDialog(const std::string& title, const std::string& message)
7 : title(title), message(message)
8{
17 hidden = true;
18
19 messageText->setText(message);
20 messageText->update(); // to set the size
21
22 // just add the single button for now
23 auto okButton = (new Button("OK", A_BUTTON));
24 okButton->setAction([this]() {
25 if (onConfirm) onConfirm();
26 });
27 okButton->cornerRadius = 10;
28 okButton->updateBounds();
29
30 auto innerVStack = new Container(COL_LAYOUT, 50);
31 innerVStack->add(messageText);
32 innerVStack->add(okButton);
33 innerVStack->width = dialogWidth;
34 // innerVStack->backgroundColor = fromRGB(0xff, 0, 0);
35 // innerVStack->hasBackground = true;
36
37 messageText->constrain(ALIGN_CENTER_HORIZONTAL, 0);
38 okButton->constrain(ALIGN_CENTER_HORIZONTAL, 0);
39
40 // prompt background color
41 vStack->backgroundColor = fromRGB(0xdd, 0xdd, 0xdd);
42 vStack->hasBackground = true;
43 vStack->cornerRadius = 15;
44 vStack->width = dialogWidth;
45 vStack->height = dialogHeight;
46 vStack->add(innerVStack);
47
48 innerVStack->constrain(ALIGN_CENTER_BOTH, 0);
49
50 // overlay and shade bg color
51 overlay->width = RootDisplay::mainDisplay->width;
52 overlay->height = RootDisplay::mainDisplay->height;
53 overlay->backgroundColor = fromRGB(0, 0, 0);
54 overlay->backgroundOpacity = 0x00;
55 overlay->cornerRadius = 1; // forces transparency to render properly (via sdl_gfx)
56 overlay->hasBackground = true;
57
58 overlay->child(vStack);
59
60 vStack->constrain(ALIGN_CENTER_BOTH, 0);
61
62 this->child(overlay);
63}
64
65void AlertDialog::setText(const std::string& newText) {
66 messageText->setText(newText);
67 messageText->update();
68}
69
70void AlertDialog::show() {
71 // we have to go from being 100% transparent and small size to being opaque and full size
72 // TODO: need opacity that affects all children elements
73 hidden = false;
74
75 if (useAnimation) {
76 // start animation
77 animate(250, [this](float progress) {
78 // on step
79 this->vStack->width = (dialogWidth * progress);
80 this->vStack->height = (dialogHeight * progress);
81 this->overlay->backgroundOpacity = (int)(0x80 * progress);
82 }, [this]() {
83 this->vStack->width = dialogWidth;
84 this->vStack->height = dialogHeight;
85 this->overlay->backgroundOpacity = 0x80;
86 });
87 return;
88 }
89
90 // no animation, just do it!
91 // this->setVisible(true);
92 this->width = dialogWidth;
93 this->width = dialogHeight;
94
95}
96
98 // Implementation for rendering the dialog
99 super::render(this);
100}
101
103 // Implementation for processing input events
104 return super::process(event);
105}
virtual bool process(InputEvents *event) override
process any input that is received for this element
virtual void render(Element *parent) override
display the current state of the display
Definition: AlertDialog.cpp:97
AlertDialog(const std::string &title, const std::string &message)
Definition: AlertDialog.cpp:6
Definition: Button.hpp:7
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:17
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:110
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:60
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:123
void update(bool forceUpdate=false)
update TextElement with changes
Definition: TextElement.cpp:90