Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Chesto::RootDisplay Class Reference
Inheritance diagram for Chesto::RootDisplay:
Inheritance graph
[legend]
Collaboration diagram for Chesto::RootDisplay:
Collaboration graph
[legend]

Public Member Functions

bool process (InputEvents *event)
 process any input that is received for this element More...
 
void render (Element *parent)
 display the current state of the display More...
 
void update ()
 
int mainLoop ()
 
void initMusic ()
 
void startMusic ()
 
void setScreenResolution (int width, int height)
 
void requestQuit ()
 
- Public Member Functions inherited from Chesto::Element
virtual bool process (InputEvents *event)
 process any input that is received for this element More...
 
virtual void render (Element *parent)
 display the current state of the display More...
 
bool onTouchDown (InputEvents *event)
 
bool onTouchDrag (InputEvents *event)
 
bool onTouchUp (InputEvents *event)
 
void hide ()
 
void unhide ()
 
void renderBackground (bool fill=true)
 
void addNode (std::unique_ptr< Element > node)
 
void remove (Element *element)
 
void removeAll ()
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
float getEffectiveScale () const
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementsetAbsolute (bool isAbs)
 
template<typename T , typename... Args>
T * createNode (Args &&... args)
 
Elementconstrain (int flags, int padding=0)
 
ElementconstrainToTarget (Element *target, int flags, int padding=0)
 
Elementanimate (int durationIn, std::function< void(float)> onStep, std::function< void()> onFinish)
 
ElementmoveToFront ()
 
ElementsetTouchable (bool touchable)
 
void screenshot (std::string path)
 Take a screenshot of this element and its children, and save it to the given path. More...
 

Static Public Member Functions

static void pushScreen (std::unique_ptr< Screen > screen)
 
static void popScreen ()
 
static ScreentopScreen ()
 
static void clearScreens ()
 
static bool hasScreens ()
 
static void deferAction (std::function< void()> action)
 
static void processDeferredActions ()
 

Public Attributes

bool canUseSelectToExit = false
 
int lastFrameTime = 99
 
SDL_Event needsRender
 
std::unique_ptr< InputEventsevents
 
std::function< void()> windowResizeCallback = NULL
 
Mix_Music * music = NULL
 
- Public Attributes inherited from Chesto::Element
std::function< void()> action = NULL
 the action to call (from binded callback) on touch or button selection https://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples More...
 
std::function< void(InputEvents *event)> actionWithEvents = NULL
 
std::vector< std::unique_ptr< Element, std::function< void(Element *)> > > elements
 visible GUI child elements of this element More...
 
float scale = 1.0f
 
bool touchable = false
 whether or not this element can be touched (highlights bounds) More...
 
bool dragging = false
 whether or not this element is currently being dragged More...
 
bool needsRedraw = false
 whether or not this element needs the screen redrawn next time it's processed More...
 
int futureRedrawCounter = 0
 whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws) More...
 
int lastMouseY = 0
 the last Y, X coordinate of the mouse (from a drag probably) More...
 
int lastMouseX = 0
 
bool hasBackground = false
 
rgb backgroundColor = {0, 0, 0}
 
int backgroundOpacity = 0xff
 
bool isAbsolute = false
 
Elementparent = nullptr
 the parent element (reference only, not owned) More...
 
bool hidden = false
 whether this element should skip rendering or not More...
 
bool isProtected = false
 
int elasticCounter = 0
 how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll, and counts down every frame More...
 
int width = 0
 width and height of this element (must be manually set, isn't usually calculated (but is in some cases, like text or images)) More...
 
int height = 0
 
int x = 0
 
int y = 0
 
int xAbs = 0
 
int yAbs = 0
 
double angle = 0
 rotation angle in degrees More...
 
int cornerRadius = 0
 
int tag = 0
 
std::vector< std::unique_ptr< Constraint > > constraints
 
std::vector< std::unique_ptr< Animation > > animations
 
bool useColorMask = false
 whether or not to overlay a color mask on top of this element More...
 
CST_Color maskColor = {0,0,0,0}
 The color to overlay on top. More...
 

Static Public Attributes

static CST_Renderer * renderer = NULL
 
static CST_Window * window = NULL
 
static RootDisplaymainDisplay = NULL
 
static std::vector< std::unique_ptr< Screen > > screenStack
 
static std::vector< std::function< void()> > deferredActions
 
static bool isProcessingEvents = false
 
static int screenWidth = 1280
 
static int screenHeight = 720
 
static float dpiScale = 1.0f
 
static float globalScale = 1.0f
 
static bool idleCursorPulsing = false
 
static bool isDebug = false
 

Additional Inherited Members

- Public Types inherited from Chesto::Element
typedef Element super
 
- Protected Member Functions inherited from Chesto::Element
void addStackMember (Element *element)
 

Detailed Description

Definition at line 24 of file RootDisplay.hpp.

Constructor & Destructor Documentation

◆ RootDisplay()

Chesto::RootDisplay::RootDisplay ( )

Definition at line 47 of file RootDisplay.cpp.

48{
49 // initialize the romfs for switch/wiiu
50#if defined(USE_RAMFS)
51 ramfsInit();
52#endif
53 // initialize internal drawing library
54 CST_DrawInit(this);
55
56 this->x = 0;
57 this->y = 0;
58 this->width = SCREEN_WIDTH;
59 this->height = SCREEN_HEIGHT;
60
61 this->hasBackground = true;
62
63#ifdef __APPLE__
64 chdirForPlatform();
65#endif
66
67 // set the display scale on high resolution displays
68 RootDisplay::dpiScale = CST_GetDpiScale();
69
70 // default background color is dark-gray, can be overridden by the implementing library
71 this->backgroundColor = fromRGB(30, 30, 30);
72
73 // set starting resolution based on SDL version
74#if defined(WII) || defined(WII_MOCK)
75 // setScreenResolution(640, 480);
76 setScreenResolution(854, 480);
77#elif defined(_3DS) || defined(_3DS_MOCK)
78 setScreenResolution(400, 480); // 3ds has a special resolution!
79#else
80 // setScreenResolution(640, 480);
81 setScreenResolution(1280, 720);
82#endif
83 // the main input handler
84 this->events = std::make_unique<InputEvents>();
85
86 // TODO: initialize this in a way that doesn't block the main thread
87 // always load english first, to initialize defaults
88 TextElement::loadI18nCache("en-us");
89
90 // TODO: detect language and system, and store preference
91 // TextElement::loadI18nCache("zh-cn");
92
93 // Initialize download queue early so it's available during screen construction
94 DownloadQueue::init();
95}
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

◆ ~RootDisplay()

Chesto::RootDisplay::~RootDisplay ( )
virtual

Definition at line 210 of file RootDisplay.cpp.

211{
212 // Clean up screens and root element children before destroying download queue
213 // This ensures NetImageElements can cancel downloads properly
214 screenStack.clear();
215 elements.clear();
216
217 // Now safe to destroy download queue
218 DownloadQueue::quit();
219
220 CST_DrawExit();
221
222#if defined(USE_RAMFS)
223 ramfsExit();
224#endif
225}
std::vector< std::unique_ptr< Element, std::function< void(Element *)> > > elements
visible GUI child elements of this element
Definition: Element.hpp:59

Member Function Documentation

◆ clearScreens()

void Chesto::RootDisplay::clearScreens ( )
static

Definition at line 167 of file RootDisplay.cpp.

168{
169 if (isProcessingEvents) {
170 // Defer operation until after event processing
171 deferAction([]() {
172 screenStack.clear();
173 if (mainDisplay) mainDisplay->needsRedraw = true;
174 });
175 } else {
176 screenStack.clear();
177 if (mainDisplay) mainDisplay->needsRedraw = true;
178 }
179}
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
Definition: Element.hpp:95

◆ deferAction()

void Chesto::RootDisplay::deferAction ( std::function< void()>  action)
static

Definition at line 181 of file RootDisplay.cpp.

182{
183 // Queue the action to be executed after event processing
184 deferredActions.push_back(std::move(action));
185}
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
Definition: Element.hpp:55

◆ hasScreens()

bool Chesto::RootDisplay::hasScreens ( )
static

Definition at line 205 of file RootDisplay.cpp.

206{
207 return !screenStack.empty();
208}

◆ initMusic()

void Chesto::RootDisplay::initMusic ( )

Definition at line 97 of file RootDisplay.cpp.

98{
99#ifdef SWITCH
100 // no music if we're in applet mode
101 // they use up too much memory, and a lot of people only use applet mode
102 AppletType at = appletGetAppletType();
103 if (at != AppletType_Application && at != AppletType_SystemApplication) {
104 return;
105 }
106#endif
107
108 // Initialize CST_mixer
109 CST_MixerInit(this);
110}

◆ mainLoop()

int Chesto::RootDisplay::mainLoop ( )

Definition at line 298 of file RootDisplay.cpp.

299{
300#ifdef __WIIU__
301 // setup procui callback for resuming application to force a chesto render
302 // https://stackoverflow.com/a/56145528 and http://bannalia.blogspot.com/2016/07/passing-capturing-c-lambda-functions-as.html
303 auto updateDisplay = +[](void* display) -> unsigned int {
304 ((RootDisplay*)display)->futureRedrawCounter = 10;
305 return 0;
306 };
307 ProcUIRegisterCallback(PROCUI_CALLBACK_ACQUIRE, updateDisplay, this, 100);
308
309 // also, register a callback for when we need to quit, to break out the main loop
310 // (other platforms will do this directly, but wiiu needs procui to do stuff first)
311 auto actuallyQuit = +[](void* display) -> unsigned int {
312 ((RootDisplay*)display)->isAppRunning = false;
313 return 0;
314 };
315 ProcUIRegisterCallback(PROCUI_CALLBACK_EXIT, actuallyQuit, this, 100);
316#endif
317
318 while (isAppRunning)
319 {
320 bool atLeastOneNewEvent = false;
321 bool viewChanged = false;
322
323 int frameStart = CST_GetTicks();
324
325 // update download queue
326 DownloadQueue::downloadQueue->process();
327
328 // get any new input events
329 while (events->update())
330 {
331 isProcessingEvents = true;
332
333 // process the inputs of the supplied event
334 viewChanged |= this->process(events.get());
335 atLeastOneNewEvent = true;
336
337 isProcessingEvents = false;
338
339 // if we see a minus, exit immediately!
340 if (this->canUseSelectToExit && events->pressed(SELECT_BUTTON)) {
341 requestQuit();
342 }
343 } // one more event update if nothing changed or there were no previous events seen
344 // needed to non-input related processing that might update the screen to take place
345 if ((!atLeastOneNewEvent && !viewChanged))
346 {
347 isProcessingEvents = true;
348 events->update();
349 viewChanged |= this->process(events.get());
350 isProcessingEvents = false;
351 }
352
353 // Event processing done, so run any deferred actions
354 processDeferredActions();
355
356 // draw the display if we processed an event or the view
357 if (viewChanged)
358 this->render(NULL);
359 else
360 {
361 // delay for the remainder of the frame to keep up to 60fps
362 // (we only do this if we didn't draw to not waste energy
363 // if we did draw, then proceed immediately without waiting for smoother progress bars / scrolling)
364 int delayTime = (CST_GetTicks() - frameStart);
365 if (delayTime < 0)
366 delayTime = 0;
367 if (delayTime < 16)
368 CST_Delay(16 - delayTime);
369 }
370 }
371
372 // unique_ptr will automatically clean up events
373
374 return 0;
375}
bool process(InputEvents *event)
process any input that is received for this element
void render(Element *parent)
display the current state of the display

◆ popScreen()

void Chesto::RootDisplay::popScreen ( )
static

Definition at line 148 of file RootDisplay.cpp.

149{
150 if (isProcessingEvents) {
151 // Defer POP until after event processing
152 deferAction([]() {
153 if (!screenStack.empty()) {
154 screenStack.pop_back();
155 if (mainDisplay) mainDisplay->needsRedraw = true;
156 }
157 });
158 } else {
159 // Safe to execute immediately
160 if (!screenStack.empty()) {
161 screenStack.pop_back();
162 if (mainDisplay) mainDisplay->needsRedraw = true;
163 }
164 }
165}

◆ process()

bool Chesto::RootDisplay::process ( InputEvents event)
virtual

process any input that is received for this element

Reimplemented from Chesto::Element.

Definition at line 227 of file RootDisplay.cpp.

228{
229 // process either the subscreen or the children elements, always return true if "dragging"
230 // (may be a mouse cursor or wiimote pointing and moving on the screen)
231
232 bool result = false;
233
234 if (!screenStack.empty()) {
235 result = screenStack.back()->process(event) || event->isTouchDrag();
236 } else {
237 // keep processing child elements
238 result = super::process(event) || event->isTouchDrag();
239 }
240
241 return result;
242}
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:29

References Chesto::Element::process().

◆ processDeferredActions()

void Chesto::RootDisplay::processDeferredActions ( )
static

Definition at line 187 of file RootDisplay.cpp.

188{
189 // Execute all deferred actions, on a copy
190 auto actionsToRun = std::move(deferredActions);
191 deferredActions.clear();
192
193 for (auto& action : actionsToRun) {
194 if (action) {
195 action();
196 }
197 }
198}

◆ pushScreen()

void Chesto::RootDisplay::pushScreen ( std::unique_ptr< Screen screen)
static

Definition at line 139 of file RootDisplay.cpp.

140{
141 if (!screen) return;
142
143 // PUSHes are not deferred! They go right onto the stack and render immediately
144 screenStack.push_back(std::move(screen));
145 if (mainDisplay) mainDisplay->needsRedraw = true;
146}

◆ render()

void Chesto::RootDisplay::render ( Element parent)
virtual

display the current state of the display

Reimplemented from Chesto::Element.

Definition at line 244 of file RootDisplay.cpp.

245{
246 // if we have a screen stack, render each screen as layers
247 if (!screenStack.empty())
248 {
250
251 for (const auto& screen : screenStack) {
252 screen->render(this);
253 }
254
255 this->update();
256 return;
257 }
258
259 // render the rest of the subelements
261
262 // commit everything to the screen
263 this->update();
264}
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:89
Element * parent
the parent element (reference only, not owned)
Definition: Element.hpp:116

References Chesto::Element::parent, and Chesto::Element::render().

◆ requestQuit()

void Chesto::RootDisplay::requestQuit ( )

Definition at line 282 of file RootDisplay.cpp.

283{
284 // if we've already requested quit, don't proceed
285 if (hasRequestedQuit) {
286 return;
287 }
288 hasRequestedQuit = true;
289
290 // depending on the platform, either break our loop or (wiiu) switch to the home menu
291#ifdef __WIIU__
292 SYSLaunchMenu();
293#else
294 this->isAppRunning = false;
295#endif
296}

◆ setScreenResolution()

void Chesto::RootDisplay::setScreenResolution ( int  width,
int  height 
)

Definition at line 117 of file RootDisplay.cpp.

118{
119 // set the screen resolution
120 SCREEN_WIDTH = width;
121 SCREEN_HEIGHT = height;
122
123 // update the root element
124 this->width = SCREEN_WIDTH;
125 this->height = SCREEN_HEIGHT;
126
127 // update the renderer, but respect the DPI scaling
128 CST_SetWindowSize(window, SCREEN_WIDTH / RootDisplay::dpiScale, SCREEN_HEIGHT / RootDisplay::dpiScale);
129
130 RootDisplay::deferAction([]() {
131 // inform all screens of the resolution change
132 for (auto& screen : screenStack) {
133 screen->rebuildUI();
134 }
135 });
136}

◆ startMusic()

void Chesto::RootDisplay::startMusic ( )

Definition at line 112 of file RootDisplay.cpp.

113{
114 CST_FadeInMusic(this);
115}

◆ topScreen()

Screen * Chesto::RootDisplay::topScreen ( )
static

Definition at line 200 of file RootDisplay.cpp.

201{
202 return screenStack.empty() ? nullptr : screenStack.back().get();
203}

◆ update()

void Chesto::RootDisplay::update ( )

Definition at line 266 of file RootDisplay.cpp.

267{
268 // never exceed 60fps because there's no point
269 // commented out, as if render isn't called manually,
270 // the CST_Delay in the input processing loop should handle this
271
272 // int now = CST_GetTicks();
273 // int diff = now - this->lastFrameTime;
274
275 // if (diff < 16)
276 // return;
277
278 CST_RenderPresent(this->renderer);
279 // this->lastFrameTime = now;
280}

Member Data Documentation

◆ canUseSelectToExit

bool Chesto::RootDisplay::canUseSelectToExit = false

Definition at line 82 of file RootDisplay.hpp.

◆ deferredActions

std::vector< std::function< void()> > Chesto::RootDisplay::deferredActions
static

Definition at line 62 of file RootDisplay.hpp.

◆ dpiScale

float Chesto::RootDisplay::dpiScale = 1.0f
static

Definition at line 71 of file RootDisplay.hpp.

◆ events

std::unique_ptr<InputEvents> Chesto::RootDisplay::events

Definition at line 88 of file RootDisplay.hpp.

◆ globalScale

float Chesto::RootDisplay::globalScale = 1.0f
static

Definition at line 74 of file RootDisplay.hpp.

◆ idleCursorPulsing

bool Chesto::RootDisplay::idleCursorPulsing = false
static

Definition at line 79 of file RootDisplay.hpp.

◆ isDebug

bool Chesto::RootDisplay::isDebug = false
static

Definition at line 81 of file RootDisplay.hpp.

◆ isProcessingEvents

bool Chesto::RootDisplay::isProcessingEvents = false
static

Definition at line 64 of file RootDisplay.hpp.

◆ lastFrameTime

int Chesto::RootDisplay::lastFrameTime = 99

Definition at line 84 of file RootDisplay.hpp.

◆ mainDisplay

RootDisplay * Chesto::RootDisplay::mainDisplay = NULL
static

Definition at line 43 of file RootDisplay.hpp.

◆ music

Mix_Music* Chesto::RootDisplay::music = NULL

Definition at line 95 of file RootDisplay.hpp.

◆ needsRender

SDL_Event Chesto::RootDisplay::needsRender

Definition at line 85 of file RootDisplay.hpp.

◆ renderer

CST_Renderer * Chesto::RootDisplay::renderer = NULL
static

Definition at line 41 of file RootDisplay.hpp.

◆ screenHeight

int Chesto::RootDisplay::screenHeight = 720
static

Definition at line 68 of file RootDisplay.hpp.

◆ screenStack

std::vector< std::unique_ptr< Screen > > Chesto::RootDisplay::screenStack
static

Definition at line 59 of file RootDisplay.hpp.

◆ screenWidth

int Chesto::RootDisplay::screenWidth = 1280
static

Definition at line 67 of file RootDisplay.hpp.

◆ window

CST_Window * Chesto::RootDisplay::window = NULL
static

Definition at line 42 of file RootDisplay.hpp.

◆ windowResizeCallback

std::function<void()> Chesto::RootDisplay::windowResizeCallback = NULL

Definition at line 90 of file RootDisplay.hpp.


The documentation for this class was generated from the following files: