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 ()
 
void requestQuit ()
 
- 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 canUseSelectToExit = false
 
int lastFrameTime = 99
 
SDL_Event needsRender
 
InputEventsevents = NULL
 
std::function< void()> windowResizeCallback = NULL
 
std::vector< Element * > trash
 
Mix_Music * music = NULL
 
- 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
 
int cornerRadius = 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 // default background color is dark-gray, can be overridden by the implementing library
63 this->backgroundColor = fromRGB(30, 30, 30);
64
65 // set starting resolution based on SDL version
66#if defined(WII) || defined(WII_MOCK)
67 setScreenResolution(640, 480);
68#elif defined(_3DS) || defined(_3DS_MOCK)
69 setScreenResolution(400, 480); // 3ds has a special resolution!
70#else
71 // setScreenResolution(640, 480);
72 setScreenResolution(1280, 720);
73#endif
74 // the main input handler
75 this->events = new InputEvents();
76
77 // TODO: initialize this in a way that doesn't block the main thread
78 // always load english first, to initialize defaults
79 TextElement::loadI18nCache("en-us");
80
81 // TODO: detect language and system, and store preference
82 // TextElement::loadI18nCache("zh-cn");
83}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:120

◆ ~RootDisplay()

RootDisplay::~RootDisplay ( )
virtual

Definition at line 119 of file RootDisplay.cpp.

120{
121 CST_DrawExit();
122
123#if defined(USE_RAMFS)
124 ramfsExit();
125#endif
126}

Member Function Documentation

◆ initMusic()

void RootDisplay::initMusic ( )

Definition at line 85 of file RootDisplay.cpp.

86{
87#ifdef SWITCH
88 // no music if we're in applet mode
89 // they use up too much memory, and a lot of people only use applet mode
90 AppletType at = appletGetAppletType();
91 if (at != AppletType_Application && at != AppletType_SystemApplication) {
92 return;
93 }
94#endif
95
96 // Initialize CST_mixer
97 CST_MixerInit(this);
98}

◆ mainLoop()

int RootDisplay::mainLoop ( )

Definition at line 202 of file RootDisplay.cpp.

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

129{
130 if (nextsubscreen != subscreen)
131 {
132 delete subscreen;
133 subscreen = nextsubscreen;
134 return true;
135 }
136
137 // process either the subscreen or the children elements, always return true if "dragging"
138 // (may be a mouse cursor or wiimote pointing and moving on the screen)
139
140 if (RootDisplay::subscreen)
141 return RootDisplay::subscreen->process(event) || event->isTouchDrag();
142
143 // keep processing child elements
144 return super::process(event) || event->isTouchDrag();
145}
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 282 of file RootDisplay.cpp.

283{
284 for (auto e : trash)
285 e->wipeAll(true);
286 trash.clear();
287}

◆ render()

void RootDisplay::render ( Element parent)
virtual

display the current state of the display

Reimplemented from Element.

Definition at line 147 of file RootDisplay.cpp.

148{
149 if (RootDisplay::subscreen)
150 {
151 RootDisplay::subscreen->render(this);
152 this->update();
153 return;
154 }
155
156 // render the rest of the subelements
158
159 // commit everything to the screen
160 this->update();
161}
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:104

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

◆ requestQuit()

void RootDisplay::requestQuit ( )

Definition at line 186 of file RootDisplay.cpp.

187{
188 // if we've already requested quit, don't proceed
189 if (hasRequestedQuit) {
190 return;
191 }
192 hasRequestedQuit = true;
193
194 // depending on the platform, either break our loop or (wiiu) switch to the home menu
195#ifdef __WIIU__
196 SYSLaunchMenu();
197#else
198 this->isAppRunning = false;
199#endif
200}

◆ setScreenResolution()

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

Definition at line 105 of file RootDisplay.cpp.

106{
107 // set the screen resolution
108 SCREEN_WIDTH = width;
109 SCREEN_HEIGHT = height;
110
111 // update the root element
112 this->width = SCREEN_WIDTH;
113 this->height = SCREEN_HEIGHT;
114
115 // update the renderer, but respect the DPI scaling
116 CST_SetWindowSize(window, SCREEN_WIDTH / RootDisplay::dpiScale, SCREEN_HEIGHT / RootDisplay::dpiScale);
117}

◆ startMusic()

void RootDisplay::startMusic ( )

Definition at line 100 of file RootDisplay.cpp.

101{
102 CST_FadeInMusic(this);
103}

◆ switchSubscreen()

void RootDisplay::switchSubscreen ( Element next)
static

Definition at line 179 of file RootDisplay.cpp.

180{
181 if (nextsubscreen != subscreen)
182 delete nextsubscreen;
183 nextsubscreen = next;
184}

◆ update()

void RootDisplay::update ( )

Definition at line 163 of file RootDisplay.cpp.

164{
165 // never exceed 60fps because there's no point
166 // commented out, as if render isn't called manually,
167 // the CST_Delay in the input processing loop should handle this
168
169 // int now = CST_GetTicks();
170 // int diff = now - this->lastFrameTime;
171
172 // if (diff < 16)
173 // return;
174
175 CST_RenderPresent(this->renderer);
176 // this->lastFrameTime = now;
177}

Member Data Documentation

◆ canUseSelectToExit

bool RootDisplay::canUseSelectToExit = false

Definition at line 52 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 58 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.

◆ lastFrameTime

int RootDisplay::lastFrameTime = 99

Definition at line 54 of file RootDisplay.hpp.

◆ mainDisplay

RootDisplay * RootDisplay::mainDisplay = NULL
static

Definition at line 35 of file RootDisplay.hpp.

◆ music

Mix_Music* RootDisplay::music = NULL

Definition at line 68 of file RootDisplay.hpp.

◆ needsRender

SDL_Event RootDisplay::needsRender

Definition at line 55 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 62 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 60 of file RootDisplay.hpp.


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