ScreenManager v1.1.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 <stdint.h>
6#include "ScreenRouter.h"
7#include "ScreenEntry.h"
8#include "ScreenTransition.h"
9
10
11using ScreenId = uint8_t;
12
18
19public:
20
22
27 void begin();
28
37 void update(uint32_t nowMs);
38
47 bool registerScreen(ScreenId id, IManagedScreen* screen ) {
48 if ( id == 0 || !screen ) return false;
49 if ( haveScreen(id)) return false;
50 auto* entry = new ScreenEntryExternal(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 displayRefreshMs = (displayRefreshMs < 4) ? 4 : displayRefreshMs; //Ensure getFps doesn't overflow
85 }
86 }
87
93 uint8_t getFps() const {
94 if ( displayRefreshMs == 0 ) return 0;
95 return (uint8_t)(1000/displayRefreshMs);
96 }
97
105 bool isCurrent(const ScreenId id) const;
106
114 bool isPrevious(const ScreenId id) const;
115
121 IManagedScreen* getCurrent() const;
122
123
129 ScreenId getPreviousId() const;
130
138 bool haveScreen(const ScreenId id) const;
139
147 void requestScreen(const ScreenId nextScreen);
148
156 void requestScreen(const TransitionIntent& intent);
157
163 void addRouter(ScreenRouter* router);
164
174 bool setInitialScreen(const ScreenId id);
175
176
177 private:
184 ScreenEntry* getScreenEntry(const ScreenId id) const;
185
186
187
188 private:
189
196 void addScreenEntry(ScreenEntry* entry);
197
198
199
205 void resolveTransition(const TransitionIntent& intent);
206
207
208 ScreenEntry* current = nullptr;
209 ScreenEntry* previous = nullptr;
210 //ScreenEntry* pausedScreen = nullptr;
211 TransitionIntent pendingIntent = {};
212 ScreenId initialScreenId = 0;
213 bool begun = false;
214
215
216 uint16_t displayRefreshMs = 100;
217 uint32_t lastDisplayRefresh = 0;
218
219
220
221 // List management
222 ScreenEntry* firstScreenEntry = nullptr;
223 ScreenEntry* lastScreenEntry = nullptr;
224
225 ScreenRouter* firstRouter = nullptr;
226 ScreenRouter* lastRouter = nullptr;
227
228
229};
230
231
232#endif
Interface for a screen that can be managed by ScreenManager. A 'Screen' is a combination of the 'View...
Definition: IManagedScreen.h:13
virtual void begin()=0
Called after a screen is created but before start()
A registration entry for an External IManagedScreen.
Definition: ScreenEntry.h:49
A registration entry for a Managed IManagedScreen.
Definition: ScreenEntry.h:81
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:17
bool registerScreen(ScreenId id, IManagedScreen *screen)
Register a screen created externally with a unique ID greater than 0.
Definition: ScreenManager.h:47
bool isPrevious(const ScreenId id) const
Return true if passed id is that of the previous screen.
Definition: ScreenManager.cpp:44
uint8_t getFps() const
Get the currently set Fps.
Definition: ScreenManager.h:93
bool registerScreen(ScreenId id)
Register a managed screen with a unique ID greater than 0 (ScreenManager will create and delete the s...
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:29
bool haveScreen(const ScreenId id) const
Check if a screen has been added and exists.
Definition: ScreenManager.cpp:65
ScreenId getPreviousId() const
Get the previous screen ID.
Definition: ScreenManager.cpp:58
void addRouter(ScreenRouter *router)
Add a new router for screen transitions. Routers are called in the order they are added....
Definition: ScreenManager.cpp:82
bool setInitialScreen(const ScreenId id)
Set the Initial Screen.
Definition: ScreenManager.cpp:93
IManagedScreen * getCurrent() const
Get the current screen or nullptr. ScreenManager::update()` must be called before the initial screen ...
Definition: ScreenManager.cpp:49
void setFps(uint8_t fps)
Set the FPS (Frames Per Second) for draw() to be called (default is 10)
Definition: ScreenManager.h:79
bool isCurrent(const ScreenId id) const
Return true if passed id is that of the current screen.
Definition: ScreenManager.cpp:39
void requestScreen(const ScreenId nextScreen)
Request a transition to a named screen.
Definition: ScreenManager.cpp:74
void begin()
Called once during setup(). Will call the IManagedScreen::begin() method of all external screens....
Definition: ScreenManager.cpp:19
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 ScreenId.
Definition: ScreenTransition.h:25