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