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
RootDisplay Class Reference
Inheritance diagram for RootDisplay:
Inheritance graph
[legend]
Collaboration diagram for 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 recycle ()
 
- Public Member Functions inherited from 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 append (Element *element)
 
void remove (Element *element)
 
void removeAll (bool moveToTrash=false)
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
void wipeAll (bool delSelf=false)
 
Elementchild (Element *child)
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementcenterHorizontallyIn (Element *parent)
 
ElementcenterVerticallyIn (Element *parent)
 
ElementcenterIn (Element *parent)
 
ElementsetAbsolute (bool isAbs)
 
Elementconstrain (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 switchSubscreen (Element *next)
 

Public Attributes

bool isRunning = true
 
bool exitRequested = false
 
bool canUseSelectToExit = false
 
int lastFrameTime = 99
 
SDL_Event needsRender
 
InputEventsevents = NULL
 
std::function< void()> windowResizeCallback = NULL
 
std::vector< Element * > trash
 
- Public Attributes inherited from 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< 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}
 
bool isAbsolute = false
 
Elementparent = NULL
 the parent element (can sometimes be null if it isn't set) 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 xOff = 0
 
int yOff = 0
 
std::vector< Constraint * > constraints
 
std::vector< 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 Elementsubscreen = NULL
 
static Elementnextsubscreen = NULL
 
static int screenWidth = 1280
 
static int screenHeight = 720
 
static float dpiScale = 1.0f
 
static bool idleCursorPulsing = false
 
static bool isDebug = false
 

Additional Inherited Members

- Public Types inherited from Element
typedef Element super
 

Detailed Description

Definition at line 16 of file RootDisplay.hpp.

Constructor & Destructor Documentation

◆ RootDisplay()

RootDisplay::RootDisplay ( )

Definition at line 39 of file RootDisplay.cpp.

40{
41 // initialize the romfs for switch/wiiu
42#if defined(USE_RAMFS)
43 ramfsInit();
44#endif
45 // initialize internal drawing library
46 CST_DrawInit(this);
47
48 this->x = 0;
49 this->y = 0;
50 this->width = SCREEN_WIDTH;
51 this->height = SCREEN_HEIGHT;
52
53 this->hasBackground = true;
54
55#ifdef __APPLE__
56 chdirForPlatform();
57#endif
58
59 // set the display scale on high resolution displays
60 RootDisplay::dpiScale = CST_GetDpiScale();
61
62 // set platform-specific default background colors, (which can be overridden)
63#if defined(__WIIU__) || defined(WIIU_MOCK)
64 this->backgroundColor = fromRGB(0x20 - 0x10, 154 - 0x10, 199 - 0x10);
65#elif defined(WII) || defined(WII_MOCK)
66 // the system wii gray
67 this->backgroundColor = fromRGB(0x8b, 0x8b, 0x8b);
68#elif defined(_3DS) || defined(_3DS_MOCK)
69 this->backgroundColor = fromRGB(30, 30, 30);
70#elif defined(SWITCH) || defined(SWITCH_MOCK)
71 this->backgroundColor = fromRGB(0xd6, 0x0 + 0x20, 0x12 + 0x20);
72#else
73 this->backgroundColor = fromRGB(0x2d, 0x26, 0x49);
74#endif
75
76 // set starting resolution based on SDL version
77#if defined(WII) || defined(WII_MOCK)
78 setScreenResolution(640, 480);
79#elif defined(_3DS) || defined(_3DS_MOCK)
80 setScreenResolution(400, 480); // 3ds has a special resolution!
81#else
82 // setScreenResolution(640, 480);
83 setScreenResolution(1280, 720);
84#endif
85 // the main input handler
86 this->events = new InputEvents();
87
88 // TODO: initialize this in a way that doesn't block the main thread
89 // always load english first, to initialize defaults
90 TextElement::loadI18nCache("en-us");
91
92 // TODO: detect language and system, and store preference
93 // TextElement::loadI18nCache("zh-cn");
94}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:118

◆ ~RootDisplay()

RootDisplay::~RootDisplay ( )
virtual

Definition at line 130 of file RootDisplay.cpp.

131{
132 CST_DrawExit();
133
134#if defined(USE_RAMFS)
135 ramfsExit();
136#endif
137}

Member Function Documentation

◆ initMusic()

void RootDisplay::initMusic ( )

Definition at line 96 of file RootDisplay.cpp.

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

◆ mainLoop()

int RootDisplay::mainLoop ( )

Definition at line 208 of file RootDisplay.cpp.

209{
210 DownloadQueue::init();
211
212#ifdef __WIIU__
213 // setup procui callback for resuming application to force a chesto render
214 // https://stackoverflow.com/a/56145528 and http://bannalia.blogspot.com/2016/07/passing-capturing-c-lambda-functions-as.html
215 auto updateDisplay = +[](void* display) -> unsigned int {
216 ((RootDisplay*)display)->futureRedrawCounter = 10;
217 return 0;
218 };
219 ProcUIRegisterCallback(PROCUI_CALLBACK_ACQUIRE, updateDisplay, this, 100);
220#endif
221
222 while (isRunning)
223 {
224 bool atLeastOneNewEvent = false;
225 bool viewChanged = false;
226
227 #ifdef __WIIU__
228 processWiiUHomeOverlay();
229 #endif
230
231 int frameStart = CST_GetTicks();
232
233 // update download queue
234 DownloadQueue::downloadQueue->process();
235
236 // get any new input events
237 while (events->update())
238 {
239 // process the inputs of the supplied event
240 viewChanged |= this->process(events);
241 atLeastOneNewEvent = true;
242
243 // if we see a minus, exit immediately!
244 if (events->pressed(SELECT_BUTTON) && this->canUseSelectToExit) {
245 if (events->quitaction != NULL) events->quitaction();
246 else isRunning = false;
247 }
248 }
249
250 // one more event update if nothing changed or there were no previous events seen
251 // needed to non-input related processing that might update the screen to take place
252 if ((!atLeastOneNewEvent && !viewChanged))
253 {
254 events->update();
255 viewChanged |= this->process(events);
256 }
257
258 // draw the display if we processed an event or the view
259 if (viewChanged)
260 this->render(NULL);
261 else
262 {
263 // delay for the remainder of the frame to keep up to 60fps
264 // (we only do this if we didn't draw to not waste energy
265 // if we did draw, then proceed immediately without waiting for smoother progress bars / scrolling)
266 int delayTime = (CST_GetTicks() - frameStart);
267 if (delayTime < 0)
268 delayTime = 0;
269 if (delayTime < 16)
270 CST_Delay(16 - delayTime);
271 }
272
273 // free up any elements that are in the trash
274 this->recycle();
275 }
276
277 delete events;
278
279 if (!isProtected) delete this;
280 DownloadQueue::quit();
281
282 return 0;
283}
int process()
process finished and queued downloads
bool process(InputEvents *event)
process any input that is received for this element
void render(Element *parent)
display the current state of the display

◆ process()

bool RootDisplay::process ( InputEvents event)
virtual

process any input that is received for this element

Reimplemented from Element.

Definition at line 139 of file RootDisplay.cpp.

140{
141 if (nextsubscreen != subscreen)
142 {
143 delete subscreen;
144 subscreen = nextsubscreen;
145 return true;
146 }
147
148 // process either the subscreen or the children elements, always return true if "dragging"
149 // (may be a mouse cursor or wiimote pointing and moving on the screen)
150
151 if (RootDisplay::subscreen)
152 return RootDisplay::subscreen->process(event) || event->isTouchDrag();
153
154 // keep processing child elements
155 return super::process(event) || event->isTouchDrag();
156}
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:17

References Element::process().

◆ recycle()

void RootDisplay::recycle ( )

Definition at line 285 of file RootDisplay.cpp.

286{
287 for (auto e : trash)
288 e->wipeAll(true);
289 trash.clear();
290}

◆ render()

void RootDisplay::render ( Element parent)
virtual

display the current state of the display

Reimplemented from Element.

Definition at line 158 of file RootDisplay.cpp.

159{
160 if (RootDisplay::subscreen)
161 {
162 RootDisplay::subscreen->render(this);
163 this->update();
164 return;
165 }
166
167 // render the rest of the subelements
169
170 // commit everything to the screen
171 this->update();
172}
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:60
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:102

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

◆ setScreenResolution()

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

Definition at line 116 of file RootDisplay.cpp.

117{
118 // set the screen resolution
119 SCREEN_WIDTH = width;
120 SCREEN_HEIGHT = height;
121
122 // update the root element
123 this->width = SCREEN_WIDTH;
124 this->height = SCREEN_HEIGHT;
125
126 // update the renderer, but respect the DPI scaling
127 CST_SetWindowSize(window, SCREEN_WIDTH / RootDisplay::dpiScale, SCREEN_HEIGHT / RootDisplay::dpiScale);
128}

◆ startMusic()

void RootDisplay::startMusic ( )

Definition at line 111 of file RootDisplay.cpp.

112{
113 CST_FadeInMusic(this);
114}

◆ switchSubscreen()

void RootDisplay::switchSubscreen ( Element next)
static

Definition at line 190 of file RootDisplay.cpp.

191{
192 if (nextsubscreen != subscreen)
193 delete nextsubscreen;
194 nextsubscreen = next;
195}

◆ update()

void RootDisplay::update ( )

Definition at line 174 of file RootDisplay.cpp.

175{
176 // never exceed 60fps because there's no point
177 // commented out, as if render isn't called manually,
178 // the CST_Delay in the input processing loop should handle this
179
180 // int now = CST_GetTicks();
181 // int diff = now - this->lastFrameTime;
182
183 // if (diff < 16)
184 // return;
185
186 CST_RenderPresent(this->renderer);
187 // this->lastFrameTime = now;
188}

Member Data Documentation

◆ canUseSelectToExit

bool RootDisplay::canUseSelectToExit = false

Definition at line 54 of file RootDisplay.hpp.

◆ dpiScale

float RootDisplay::dpiScale = 1.0f
static

Definition at line 44 of file RootDisplay.hpp.

◆ events

InputEvents* RootDisplay::events = NULL

Definition at line 60 of file RootDisplay.hpp.

◆ exitRequested

bool RootDisplay::exitRequested = false

Definition at line 53 of file RootDisplay.hpp.

◆ idleCursorPulsing

bool RootDisplay::idleCursorPulsing = false
static

Definition at line 49 of file RootDisplay.hpp.

◆ isDebug

bool RootDisplay::isDebug = false
static

Definition at line 51 of file RootDisplay.hpp.

◆ isRunning

bool RootDisplay::isRunning = true

Definition at line 52 of file RootDisplay.hpp.

◆ lastFrameTime

int RootDisplay::lastFrameTime = 99

Definition at line 56 of file RootDisplay.hpp.

◆ mainDisplay

RootDisplay * RootDisplay::mainDisplay = NULL
static

Definition at line 35 of file RootDisplay.hpp.

◆ needsRender

SDL_Event RootDisplay::needsRender

Definition at line 57 of file RootDisplay.hpp.

◆ nextsubscreen

Element * RootDisplay::nextsubscreen = NULL
static

Definition at line 39 of file RootDisplay.hpp.

◆ renderer

CST_Renderer * RootDisplay::renderer = NULL
static

Definition at line 33 of file RootDisplay.hpp.

◆ screenHeight

int RootDisplay::screenHeight = 720
static

Definition at line 43 of file RootDisplay.hpp.

◆ screenWidth

int RootDisplay::screenWidth = 1280
static

Definition at line 42 of file RootDisplay.hpp.

◆ subscreen

Element * RootDisplay::subscreen = NULL
static

Definition at line 38 of file RootDisplay.hpp.

◆ trash

std::vector<Element*> RootDisplay::trash

Definition at line 68 of file RootDisplay.hpp.

◆ window

CST_Window * RootDisplay::window = NULL
static

Definition at line 34 of file RootDisplay.hpp.

◆ windowResizeCallback

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

Definition at line 62 of file RootDisplay.hpp.


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