Home2L - C/C++ API v1.4-0-g38cc (2024-05-25)
Smart Tools for a Private Home
ui_screen.H
Go to the documentation of this file.
1/*
2 * This file is part of the Home2L project.
3 *
4 * (C) 2015-2021 Gundolf Kiefer
5 *
6 * Home2L is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Home2L is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Home2L. If not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20
21
22#ifndef _SCREEN_
23#define _SCREEN_
24
25
45#include "ui_base.H"
46
47
48/* Some pieces of the code are intentionally robust enough to tolerate 'this == NULL'.
49 * GCC 6 by default emits warnings in this case. We disable them.
50 */
51#if defined(__GNUC__) && !ANDROID
52#pragma GCC diagnostic ignored "-Wnonnull-compare"
53#endif
54
55
56
57
58
59// *************************** General *****************************************
60
61
64
65
66#define UI_BUTTONS_HEIGHT 64
67#define UI_BUTTONS_SPACE 8
68
69#define UI_BUTTONS_RECT Rect (0, UI_RES_Y - UI_BUTTONS_HEIGHT, UI_RES_X, UI_BUTTONS_HEIGHT)
71#define UI_BUTTONS_BACKWIDTH (UI_BUTTONS_HEIGHT + UI_BUTTONS_HEIGHT/2)
73
74#define UI_USER_RECT Rect (0, 0, UI_RES_X, UI_RES_Y - UI_BUTTONS_HEIGHT - UI_BUTTONS_SPACE)
76
77
79
80
81class CScreen;
82
83
84
85
86
87// *************************** CWidget *****************************************
88
89
94class CWidget {
95 public:
96 CWidget ();
97 virtual ~CWidget () { ClearTexture (); }
98
101 void Set (SDL_Surface *_surf, int x0, int y0) {
102 SetArea (Rect (_surf)); RectMove (&area, x0, y0); SetSurface (_surf);
103 }
104
105 void SetArea (SDL_Rect _area) { area = _area; }
106 SDL_Rect *GetArea () { return &area; }
107
108 class CScreen *GetScreen () { return screen; }
109 bool IsOnScreen (class CScreen *_screen) { return screen == _screen; }
110 class CCanvas *GetCanvas () { return canvas; }
112
115 void LocalToScreenCoords (int *x, int *y);
116 void ScreenToLocalCoords (int *x, int *y);
117
118 void GetMouseEventPos (SDL_Event *ev, int *x, int *y);
120
124 void SetSurface (SDL_Surface *_surface) { surface = _surface; ChangedSurface (); }
128 virtual SDL_Surface *GetSurface () { return surface; }
131
134 void GetRenderArea (SDL_Rect *ra) { *ra = area; LocalToScreenCoords (&ra->x, &ra->y); }
136 virtual SDL_Texture *GetTexture ();
142 void SetTextureBlendMode (SDL_BlendMode _sdlBlendMode) { sdlBlendMode = _sdlBlendMode; }
145 virtual void Render (SDL_Renderer *ren);
157
160 virtual bool HandleEvent (SDL_Event *ev) { return false; }
163
164 protected:
165 friend class CScreen;
166 friend class CCanvas;
167
168 CWidget *next;
169 class CScreen *screen;
170 class CCanvas *canvas;
171 int screenLayer; // for use by 'CScreen::AddWidget ()': layer of widget in the current context
172
173 SDL_Rect area;
174 SDL_Surface *surface;
175 SDL_Texture *texture;
176 SDL_BlendMode sdlBlendMode;
177
185 void ChangedSurface () { Changed (); }
186 void Changed ();
188
189 private:
190
191 // Helpers...
192 static void RenderList (CWidget *list, SDL_Renderer *ren);
193 // Render this and all widgets of the linked list behind 'next'; first widgets appear on top
194};
195
196
197
198
199
200// *************************** CCanvas *****************************************
201
202
213class CCanvas: public virtual CWidget {
214 public:
215 CCanvas () { virtArea = Rect (0, 0, 0, 0); firstWidget = NULL; SetDefaults (); }
216 //~ virtual ~CCanvas () {}
217
220 void SetDefaults () { SetColors (); SetScrollbarWidth (); }
221 void SetColors (TColor _backColor = BLACK, TColor _scrollbarColor = ToColor (255, 255, 255, 32)) {
222 backColor = _backColor; scrollbarColor = _scrollbarColor; Changed ();
223 }
224 void SetScrollbarWidth (int width = 8) { scrollbarWidth = width; Changed (); }
225
226 void SetArea (SDL_Rect _area) { CWidget::SetArea (_area); SetVirtArea (_area); }
228
231 void SetVirtArea (SDL_Rect r);
234 SDL_Rect *GetVirtArea () { return &virtArea; }
235 void MoveVirtArea (int toX0, int toY0) { SetVirtArea (Rect (toX0, toY0, virtArea.w, virtArea.h)); }
236 void LimitVirtArea (SDL_Rect *r = NULL);
238 void ScrollTo (SDL_Rect r, int hAlign = 0, int vAlign = -1);
240 void ScrollIn (SDL_Rect r);
243
246 void WidgetToScreenCoords (int *x, int *y);
247 void ScreenToWidgetCoords (int *x, int *y);
249
253 void AddWidget (CWidget *widget) { DoAddWidget (&firstWidget, widget); }
254 void DelWidget (CWidget *widget) { DoDelWidget (&firstWidget, widget); }
255 void DelAllWidgets ();
257
260 bool IsVisible (SDL_Rect *r);
261 bool IsVisible (CWidget *w) { return IsVisible (w->GetArea ()); }
263
266 virtual void Render (SDL_Renderer *ren);
267 virtual bool HandleEvent (SDL_Event *ev);
270
271 protected:
272 SDL_Rect virtArea;
273 CWidget *firstWidget;
274 TColor backColor, scrollbarColor;
275 int scrollbarWidth;
276
277 private:
278
279 // Helpers...
280 void DoAddWidget (CWidget **pFirst, CWidget *widget);
281 void DoDelWidget (CWidget **pFirst, CWidget *widget);
282};
283
284
285
286
287
288// *************************** CScreen *****************************************
289
290
293class CScreen {
294 public:
295 CScreen () { firstWidget = NULL; changed = true; withKeyboard = false; }
296 virtual ~CScreen ();
297
301 static void ClassInit () { /* activeScreen = NULL; */ }
302 static void ClassDone () {}
303
304 static void Refresh () { Changed (); }
306
307 static void EmulateOff (bool off) { emulateOff = off; Changed (); }
309 static void EmulateStandby (bool standby) { emulateStandby = standby; Changed (); }
312
315 bool IsActive () { return this == activeScreen; }
316 static class CScreen *ActiveScreen () { return activeScreen; }
317 virtual void Activate (bool on = true);
318 void Deactivate () { Activate (false); }
320
325 void AddWidget (CWidget *widget, int layer = 0) { DoAddWidget (&firstWidget, widget, layer); }
326 void DelWidget (CWidget *widget) { DoDelWidget (&firstWidget, widget); }
327 void DelAllWidgets ();
329
332 void Run ();
338 void Return () { running = false; }
341
344 void SetKeyboard (bool on);
345 bool HasKeyboard () { return withKeyboard; }
347
348 protected:
349 friend class CWidget;
350 friend void UiIterate (bool noWait);
351
352 static class CScreen *activeScreen;
353 static bool changed;
354 static bool emulateOff, emulateStandby;
355 static bool keyboardOn;
356
357 CWidget *firstWidget;
358 bool running, withKeyboard;
359
360 // Callbacks...
361 virtual bool HandleEvent (SDL_Event *ev);
362
363 // Change management & rendering...
364 static void Changed () { changed = true; }
365 static void RenderUpdate ();
366
367 private:
368
369 // Helpers...
370 void DoAddWidget (CWidget **pFirst, CWidget *widget, int layer);
371 void DoDelWidget (CWidget **pFirst, CWidget *widget);
372};
373
374
375
376
377
378// *************************** Layout helpers **********************************
379
380
383
384
385SDL_Rect *LayoutRow (SDL_Rect container, const int *format, int items = -1, int space = UI_BUTTONS_SPACE);
400SDL_Rect *LayoutRow (SDL_Rect container, int space = UI_BUTTONS_SPACE, ...);
402static inline SDL_Rect *LayoutRowEqually (SDL_Rect container, int items, int space = UI_BUTTONS_SPACE) { return LayoutRow (container, (const int *) NULL, items, space); }
404
405SDL_Rect *LayoutCol (SDL_Rect container, const int *format, int items = -1, int space = UI_BUTTONS_SPACE);
421SDL_Rect *LayoutCol (SDL_Rect container, int space = UI_BUTTONS_SPACE, ...);
423static inline SDL_Rect *LayoutColEqually (SDL_Rect container, int items, int space = UI_BUTTONS_SPACE) { return LayoutRow (container, (const int *) NULL, items, space); }
425
426SDL_Rect *LayoutMatrix (SDL_Rect container, const int *hFormat, const int *vFormat, int hItems = -1, int vItems = -1, int hSpace = UI_BUTTONS_SPACE, int vSpace = UI_BUTTONS_SPACE);
431
432
434
435
436
437
438
439// *************************** Init/Done ***************************************
440
441
444
445
446static inline void ScreenInit () { CScreen::ClassInit (); }
447static inline void ScreenDone () { CScreen::ClassDone (); }
448
449
451
452
453
454
455
461#endif
Canvas widget.
Definition: ui_screen.H:213
void SetVirtArea(SDL_Rect r)
Set the virtual area in screen coordinates; ('virtArea' == 'area' represents no displacement); The ar...
virtual void Render(SDL_Renderer *ren)
Render this widget.
virtual bool HandleEvent(SDL_Event *ev)
Handle wiping/scrolling events, if virtual area is wider (higher) than physical area.
void LimitVirtArea(SDL_Rect *r=NULL)
Move virtual area (or 'r') so that the visible area is completely filled.
void ScrollTo(SDL_Rect r, int hAlign=0, int vAlign=-1)
Scroll such that 'r' is aligned according to '[vh]Align' (-1 = left/up, 0 = center,...
void ScrollIn(SDL_Rect r)
Scroll just enough to get 'r' fully visible.
void SetDefaults()
Set default colors and scrollbar appearance.
Definition: ui_screen.H:220
Screen object.
Definition: ui_screen.H:293
void Run()
Activate the screen, iterate until Return() is called and then return to the previously active screen...
static void EmulateStandby(bool standby)
Emulate screen standby mode.
Definition: ui_screen.H:309
static void EmulateOff(bool off)
Emulate screen off.
Definition: ui_screen.H:307
static void Refresh()
Refresh screen (e.g. after the app has been woken up in Android).
Definition: ui_screen.H:304
friend void UiIterate(bool noWait)
void Return()
Let Run() return at next occasion.
Definition: ui_screen.H:338
Base class for all widgets.
Definition: ui_screen.H:94
virtual bool HandleEvent(SDL_Event *ev)
Handle an event and return 'true' if the event was consumed and is to be ignored by later widgets.
Definition: ui_screen.H:160
void ClearTexture()
Clear the internally cached texture object to save memory.
virtual void Render(SDL_Renderer *ren)
Render this widget.
void SetSurface(SDL_Surface *_surface)
Set the static surface to display. The caller stays owner of surface, must not delete it as long as i...
Definition: ui_screen.H:124
void SetTextureBlendMode(SDL_BlendMode _sdlBlendMode)
Set the blend mode for the texture created by the default implementation of GetTexture()....
Definition: ui_screen.H:142
virtual SDL_Surface * GetSurface()
Get an up-to-date surface or 'NULL' if none is available.
Definition: ui_screen.H:128
void ChangedSurface()
Mark (only) the surface as changed to trigger a redrawing at next occasion.
Definition: ui_screen.H:185
void Changed()
Anything may have changed: Trigger a redrawing at next occasion.
virtual SDL_Texture * GetTexture()
Return an up-to-date texture.
void GetRenderArea(SDL_Rect *ra)
Transpose 'area' to the current screen coordinates to be passed to all SDL rendering functions.
Definition: ui_screen.H:134
static SDL_Rect * LayoutColEqually(SDL_Rect container, int items, int space=UI_BUTTONS_SPACE)
Layout a column of 'items' rectangles with equal width.
Definition: ui_screen.H:423
SDL_Rect * LayoutCol(SDL_Rect container, const int *format, int items=-1, int space=UI_BUTTONS_SPACE)
Layout a column of rectangles.
SDL_Rect * LayoutRow(SDL_Rect container, const int *format, int items=-1, int space=UI_BUTTONS_SPACE)
Layout a row of rectangles.
SDL_Rect * LayoutMatrix(SDL_Rect container, const int *hFormat, const int *vFormat, int hItems=-1, int vItems=-1, int hSpace=UI_BUTTONS_SPACE, int vSpace=UI_BUTTONS_SPACE)
Layout a matrix of variable-size elements.
#define UI_BUTTONS_SPACE
Standard spacing between buttons.
Definition: ui_screen.H:67
static SDL_Rect * LayoutRowEqually(SDL_Rect container, int items, int space=UI_BUTTONS_SPACE)
Layout a row of 'items' rectangles with equal width.
Definition: ui_screen.H:402