ScreenManager v1.0.0
A lightweight C++ library for managing application screens, navigation, transitions and display timing.
ScreenManager.h
1//#pragma once
2#ifndef SCREEN_MANAGER_H
3#define SCREEN_MANAGER_H
4
5#include "ScreenRouter.h"
6#include "ScreenEntry.h"
7#include "ScreenTransition.h"
8
9
10using ScreenId = uint8_t;
11
17
18public:
19
21
26 void begin();
27
35 void update(uint32_t nowMs);
36
46 template<typename T>
47 bool registerScreen(ScreenId id, T* screen ) {
48 if ( id == 0 ) return false;
49 if ( haveScreen(id)) return false;
50 auto* entry = new ScreenEntryExternal<T>(screen, id);
51 entry->instance = screen;
52 addScreenEntry(entry);
53 if ( begun ) screen->begin();
54 return true;
55 }
56
65 template<typename T>
66 bool registerScreen(ScreenId id) {
67 if ( id == 0 ) return false;
68 if ( haveScreen(id)) return false;
69 auto* entry = new ScreenEntryManaged<T>(id);
70 addScreenEntry(entry);
71 return true;
72 }
73
79 void setFps(uint8_t fps) {
80 if ( fps == 0 ) {
81 displayRefreshMs = 0;
82 } else {
83 displayRefreshMs = (uint16_t)(1000/fps);
84 }
85 }
86
92 uint8_t getFps() {
93 return (uint8_t)(displayRefreshMs/1000);
94 }
95
96
97 // /**
98 // * @brief Get a Screen object (not necessarily the current one)
99 // *
100 // * @param id The id used to register the screen
101 // * @return IManagedScreen* or nullptr if screen doesn't exist
102 // */
103 // IManagedScreen* getScreen(ScreenId id);
104
105
113 bool isCurrent(const ScreenId id);
114
122 bool isPrevious(ScreenId id);
123
130
131
137 ScreenId getPreviousId();
138
146 bool haveScreen(ScreenId id);
147
155 void requestScreen(const ScreenId nextScreen);
156
164 void requestScreen(const TransitionIntent& intent);
165
171 void addRouter(ScreenRouter* router);
172
182 bool setInitialScreen(ScreenId id);
183
184
185 private:
192 ScreenEntry* getScreenEntry(ScreenId id);
193
194
195
196 private:
197
204 void addScreenEntry(ScreenEntry* entry);
205
206
207
213 void resolveTransition(const TransitionIntent& intent);
214
215
216 ScreenEntry* current = nullptr;
217 ScreenEntry* previous = nullptr;
218 //ScreenEntry* pausedScreen = nullptr;
219 TransitionIntent pendingIntent = {};
220 ScreenId initialScreenId = 0;
221 bool begun = false;
222
223
224 uint16_t displayRefreshMs = 100;
225 uint32_t lastDisplayRefresh = 0;
226
227
228
229 // List management
230 ScreenEntry* firstScreenEntry = nullptr;
231 ScreenEntry* lastScreenEntry = nullptr;
232
233 ScreenRouter* firstRouter = nullptr;
234 ScreenRouter* lastRouter = nullptr;
235
236
237};
238
239
240#endif
Interface for a screen that can be managed by ScreenManager. A 'Screen' is a combination of the 'View...
Definition: IManagedScreen.h:13
A registration entry for an External IManagedScreen.
Definition: ScreenEntry.h:51
A registration entry for a Managed IManagedScreen.
Definition: ScreenEntry.h:83
A registered screen. Only ever accessed by ScreenManager.
Definition: ScreenEntry.h:12
The core class that handles registration, transitions and of course, calling the screen's draw() meth...
Definition: ScreenManager.h:16
bool isPrevious(ScreenId id)
Return true if passed id is that of the previous screen.
Definition: ScreenManager.cpp:56
bool isCurrent(const ScreenId id)
Return true if passed id is that of the current screen.
Definition: ScreenManager.cpp:51
uint8_t getFps()
Get the currently set Fps.
Definition: ScreenManager.h:92
bool registerScreen(ScreenId id)
Register a managed screen (ScreenManager will create and delete the screen)
Definition: ScreenManager.h:66
void update(uint32_t nowMs)
Called from your loop(). Checks if a screen transition has been requested and calls current screen's ...
Definition: ScreenManager.cpp:30
bool haveScreen(ScreenId id)
Check if a screen has been added and exists.
Definition: ScreenManager.cpp:77
ScreenId getPreviousId()
Get the previous screen ID.
Definition: ScreenManager.cpp:70
void addRouter(ScreenRouter *router)
Add a new router for screen transitions. Routers are called in the order they are added....
Definition: ScreenManager.cpp:94
bool registerScreen(ScreenId id, T *screen)
Register a screen created externally.
Definition: ScreenManager.h:47
IManagedScreen * getCurrent()
Get the current screen.
Definition: ScreenManager.cpp:61
void setFps(uint8_t fps)
Set the FPS (Frames Per Second) for draw() to be called (default is 10)
Definition: ScreenManager.h:79
void requestScreen(const ScreenId nextScreen)
Request a transition to a named screen.
Definition: ScreenManager.cpp:86
void begin()
Called once during setup(). Will call the IManagedScreen::begin() method of all external screens....
Definition: ScreenManager.cpp:19
bool setInitialScreen(ScreenId id)
Set the Initial Screen.
Definition: ScreenManager.cpp:105
Abstract class that resolves screen transitions (ie moving Next, Back or to a new screen)....
Definition: ScreenRouter.h:11
The type of transition and an optional requested screen name.
Definition: ScreenTransition.h:24