Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
application.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019-2021 natinusala
3 Copyright 2019 p-sam
4 Copyright 2021 XITRIX
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17*/
18
19#pragma once
20
21#include <deque>
22#include <functional>
23#include <memory>
24#include <string>
25#include <unordered_map>
26#include <utility>
27#include <vector>
29#include <aurore/core/audio.hpp>
30#include <aurore/core/font.hpp>
36#include <aurore/core/style.hpp>
37#include <aurore/core/theme.hpp>
38#include <aurore/core/view.hpp>
40
41#ifdef __WINRT__
42#ifdef main
43#undef main
44#endif
45#define main SDL_main
46#endif
47
48namespace aurore
49{
50
52enum class InputType
53{
54 GAMEPAD,
55 TOUCH,
56};
57
58class DebugLayer;
59class EditTextDialog;
60struct ThemeImage;
61
63typedef std::function<View*(void)> XMLViewCreator;
64
68{
69 public:
70 friend class EditTextDialog;
71
72 static inline uint32_t ORIGINAL_WINDOW_WIDTH = 1280;
73 static inline uint32_t ORIGINAL_WINDOW_HEIGHT = 720;
74 inline static std::string appId = "";
75
80 static bool init();
81
89 static void setAppId(std::string appId) { Application::appId = std::move(appId); }
91 static const std::string& getAppId() { return Application::appId; }
92
98 static void setCadran(bool on) { Application::cadran = on; }
100 static bool isCadran() { return Application::cadran; }
101
106 static void createWindow(std::string title);
107
112 static bool mainLoop();
113
118
123 static YGConfig* getYogaConfig();
124
126 static NVGcontext* getNVGContext();
128 inline static float contentWidth, contentHeight;
129
131 inline static unsigned windowWidth, windowHeight;
132
134 inline static int windowXPos, windowYPos;
135
139 static void onWindowResized(int width, int height);
140
145 static void setWindowSize(int width, int height);
146
150 static void onWindowReposition(int xPos, int yPos);
151
156 static void setWindowPosition(int xPos, int yPos);
157
159 static std::vector<Activity*> getActivitiesStack();
160
172
181
188 static bool popActivity(
189 TransitionAnimation animation = TransitionAnimation::FADE, std::function<void(void)> cb = [] {}, bool free = true);
190
195 static void giveFocus(View* view);
196
201 static void forgetView(View* view);
202
209 static void setHighlightIdleFade(bool fade);
210
217 static void setFocusAnnouncer(std::function<void(const std::string&)> announcer);
218
225 static void announce(const std::string& text);
226 static const std::string& getLastAnnouncement() { return Application::lastAnnouncement; }
227
239 static void setReduceMotion(bool reduce);
240 static bool getReduceMotion();
242 static bool isReduceMotionSet() { return Application::reduceMotion; }
243
248 static Time getLastInputTime() { return Application::lastInputTime; }
249
256 static void setTextScale(float scale);
257 static float getTextScale();
258
265 static void setDensity(float density) { aurore::setDensity(density); }
266 static float getDensity() { return aurore::getDensity(); }
267
273 static void registerModel(const std::string& name, DataModel* model);
274 static void unregisterModel(const std::string& name);
275 static DataModel* getModel(const std::string& name);
276
281 static View* getHoveredView() { return Application::hoveredView; }
282
284 inline static Style getStyle()
285 {
286 return aurore::getStyle();
287 }
288
290 static Theme getTheme();
293
302
307 static bool hasThemeBackground();
308
316 static void drawThemeBackground(NVGcontext* vg, float x, float y, float width, float height);
317
323 static void drawThemeBackground(NVGcontext* vg, float x, float y, float width, float height, float radius);
324
327
334 static bool loadFontFromFile(std::string fontName, std::string filePath);
335
340 static bool loadFontFromMemory(std::string fontName, void* data, size_t size, bool freeData);
341
345 static bool loadFontFromMemory(std::string fontName, void* data, size_t size, bool freeData, int faceIndex);
346
351 static int getFont(std::string fontName);
352
357 static bool addFontFallback(const std::string& font, const std::string& fallback);
358
360 static int getDefaultFont();
361
364 static void notify(const std::string& text, View* leading = nullptr);
365
368 static void onControllerButtonPressed(enum ControllerButton button, bool repeating);
369
371 static void onKeyboardPressed(AuroreKeyCombination key, bool repeating);
372
381 static void crash(std::string text);
382
384 static void quit();
386 static void clear();
387
391 static void blockInputs(bool muteSounds = false);
392
397 static void unblockInputs();
398
400 static bool isInputBlocks();
401
404
406 static void setCommonFooter(std::string footer);
407 static std::string* getCommonFooter();
408
410 inline static float windowScale;
411 inline static float maximumUIScale = 0.0f;
412
417 static void setMaximumUIScale(float scale);
418 static float getMaximumUIScale();
419
423 static void setGlobalQuit(bool enabled);
424
426 static void setFPSStatus(bool enabled);
427 static bool getFPSStatus();
429 static size_t getFPS();
430
435 static void setLimitedFPS(size_t fps);
436
471 static void setSwapInterval(int interval);
472
479 static void setAutomaticDeactivation(bool value);
484 static bool hasActiveEvent();
486 static void setActiveEvent(bool value);
488 static void setDeactivatedTime(int millisecond);
490 static void setDeactivatedFPS(int value);
491 static int getDeactivatedFPS();
493 static double getDeactivatedFrameTime();
494
508 static void setRedrawOnDemand(bool enabled);
510 static bool isRedrawOnDemand();
511
515 static void requestRedraw();
516
521 static uint64_t getDrawnFrameCount();
522
532 static void setRedrawDebug(bool enabled);
534 static bool isRedrawDebug();
535
541 static uint64_t getMissedRedrawCount();
543
566
569
571 static std::string getTitle();
572
582 static void registerXMLView(std::string name, XMLViewCreator creator);
583
585 static bool XMLViewsRegisterContains(std::string name);
586
588 static std::vector<std::string> getXMLViewNames();
590 static XMLViewCreator getXMLViewCreator(std::string name);
591
595 static std::string getLocale();
596
599 static void addToFreeQueue(View* view);
600
604 inline static InputType getInputType()
605 {
606 return inputType;
607 }
608
618 static void setInspectorEnabled(bool enabled);
620 static bool isInspectorEnabled();
621
633 static void setHotReload(bool enabled, const std::string& resourcesDir = "");
635 static bool isHotReload();
636
643 static bool rebuildActivity();
644
646 inline static void enableDebuggingView(bool enable)
647 {
648 debuggingViewEnabled = enable;
649 }
650
651 inline static bool isDebuggingViewEnabled()
652 {
653 return debuggingViewEnabled;
654 }
655
657 static void setSwapInputKeys(bool swap);
658
668 static void clearKeyMap();
669 static const std::vector<std::pair<AuroreKeyCombination, ControllerButton>>& getKeyMap();
671
672 inline static bool isSwapInputKeys()
673 {
674 return swapInputKeys;
675 }
676
678 inline static void setDrawCoursor(bool draw)
679 {
680 drawCoursor = draw;
681 }
682
683 inline static bool isDrawCursor()
684 {
685 return drawCoursor;
686 }
687
689 inline static bool isHintsLiteMode()
690 {
691 return hintsLiteMode;
692 }
693
695 inline void static setHintsLiteMode(const bool value)
696 {
697 hintsLiteMode = value;
698 }
699
701 static void tryDeinitFirstResponder(View* view);
702
705
708
709 private:
710 static void updateHover(Point position, bool inside);
711 static void announceFocus(View* oldFocus, View* newFocus);
712
713 inline static std::function<void(const std::string&)> focusAnnouncer;
714 inline static std::string lastAnnouncement;
715 inline static bool reduceMotion = false;
716 inline static std::unordered_map<std::string, DataModel*> models;
717 inline static View* hoveredView = nullptr;
718
719 inline static bool inited = false;
720 inline static bool cadran = true;
721 inline static bool quitRequested = false;
722 inline static bool debuggingViewEnabled = false;
723 inline static bool swapInputKeys = false;
724 inline static bool drawCoursor = false;
725
726 inline static Platform* platform = nullptr;
727
728 inline static std::string title;
729
730 inline static FontStash fontStash;
731
732 inline static std::vector<Activity*> activitiesStack;
733 inline static std::vector<View*> focusStack;
734 inline static std::deque<View*> deletionPool;
735
736 inline static View* currentFocus = nullptr;
737 inline static std::vector<TouchState> currentTouchState;
738 inline static MouseState currentMouseState;
739 inline static NotificationManager* notificationManager;
740
742 static bool setInputType(InputType type);
743
744 inline static InputType inputType = InputType::GAMEPAD;
745 inline static std::vector<AuroreKeyState> watchedKeys;
746 inline static std::vector<AuroreKeyState> oldWatchedKeys;
747 inline static std::unordered_map<int, int> watchedKeysMap;
748 static std::vector<std::pair<AuroreKeyCombination, ControllerButton>> keyMap;
749 static void applyKeyMap(InputManager* inputManager, ControllerState* state);
750 inline static ControllerState controllerState = {};
751
752 inline static void processInput();
753 inline static bool internalMainLoop();
754
755 inline static void updateFPS();
756
757 inline static unsigned blockInputsTokens = 0;
758 inline static bool muteSounds = false;
759
760 inline static std::string commonFooter;
761
762 inline static bool globalQuitEnabled = false;
763 inline static ActionIdentifier gloablQuitIdentifier = ACTION_NONE;
764 inline static bool globalFPSToggleEnabled = false;
765 inline static size_t globalFPS = 60;
766 inline static Time limitedFrameTime = 0;
767 inline static Time frameStartTime = 0;
768 inline static bool hintsLiteMode = false;
769
770 inline static bool deactivatedBehavior = false;
771 inline static bool activeEvent = false;
772 inline static Time lastActiveTime = 0;
773 inline static Time lastInputTime = 0;
774 inline static bool highlightIdleFade = false;
775 inline static bool redrawOnDemand = false;
776 inline static int redrawFrames = 2;
777 inline static Time lastDrawTime = 0;
778 inline static uint64_t drawnFrames = 0;
779 inline static bool redrawDebug = false;
780 inline static uint64_t missedRedraws = 0;
781 static Rect missedRedrawArea;
782 inline static std::vector<unsigned char> presentedPixels;
783 inline static int presentedWidth = 0, presentedHeight = 0;
784 inline static int deactivatedFPS = 5;
785 inline static int deactivatedTime = 5000000;
786
787 inline static View* repetitionOldFocus = nullptr;
788
789 inline static GenericEvent globalFocusChangeEvent;
790 inline static VoidEvent globalHintsUpdateEvent;
791 inline static Event<InputType> globalInputTypeChangeEvent;
792 inline static VoidEvent runLoopEvent;
793 inline static VoidEvent postFrameEvent;
794 inline static VoidEvent exitEvent;
795 inline static VoidEvent exitDoneEvent;
796 inline static VoidEvent windowSizeChangedEvent;
797 inline static VoidEvent windowCreationDoneEvent;
798 inline static VoidEvent windowShouldCloseEvent;
799 inline static Event<bool> windowFocusChangedEvent;
800 inline static Event<ThemeVariant> themeChangedEvent;
801
803 inline static uint64_t themeGeneration = 0;
804 inline static ThemeVariant themeVariant = ThemeVariant::DARK;
805 static void checkThemeChanged();
806
809 inline static ThemeBackground themeBackgroundSpec;
810 inline static std::shared_ptr<ThemeImage> themeBackground;
811 inline static std::shared_ptr<ThemeImage> themeBackgroundLoading;
812 static void loadThemeBackground();
813 static void onThemeImageLoaded(const ThemeImage* image);
814 static void drawThemeBackgroundImage(NVGcontext* vg, float x, float y, float width, float height, float radius = 0.0f);
815
816 inline static std::unordered_map<std::string, XMLViewCreator> xmlViewsRegister;
817
818 static void navigate(FocusDirection direction, bool repeating);
819
821 static void frame(bool present = true);
822 static void checkSkippedFrame();
823 static void exit();
824
830 static bool handleAction(ActionType type, int button, bool repeating);
831
832 static void registerBuiltInXMLViews();
833
834 inline static DebugLayer* debugLayer = nullptr;
835};
836
837} // namespace aurore
#define ACTION_NONE
Definition actions.hpp:102
Definition activity.hpp:45
Definition application.hpp:68
static std::string appId
See setAppId().
Definition application.hpp:74
static bool popActivity(TransitionAnimation animation=TransitionAnimation::FADE, std::function< void(void)> cb=[] {}, bool free=true)
static void setRedrawDebug(bool enabled)
static bool hasActiveEvent()
static float getMaximumUIScale()
static bool isInputBlocks()
Whether blockInputs() is in force.
static bool mainLoop()
static float getTextScale()
static int getFont(std::string fontName)
static void setLimitedFPS(size_t fps)
static std::vector< std::string > getXMLViewNames()
The XML tags registered (registerXMLView).
static void addToFreeQueue(View *view)
static void onKeyboardPressed(AuroreKeyCombination key, bool repeating)
Handles a key as the main loop does (the actions registered on keys).
static void unmapKey(AuroreKeyCombination key)
static XMLViewCreator getXMLViewCreator(std::string name)
The function registered for an XML tag.
static void pushActivity(Activity *view, TransitionAnimation animation=TransitionAnimation::FADE)
static bool loadFontFromMemory(std::string fontName, void *data, size_t size, bool freeData)
static void forgetView(View *view)
static Platform * getPlatform()
The platform the app runs on (window, inputs, video, audio, fonts), made by init().
static void drawThemeBackground(NVGcontext *vg, float x, float y, float width, float height, float radius)
static VoidEvent * getRunLoopEvent()
Fired once per main loop iteration, after the frame and the sync tasks.
static void registerXMLView(std::string name, XMLViewCreator creator)
static void notify(const std::string &text, View *leading=nullptr)
static void setDensity(float density)
Definition application.hpp:265
static void setSwapInputKeys(bool swap)
Swaps A and B (and X and Y) for the app's actions, as on a Japanese console.
static void setWindowPosition(int xPos, int yPos)
static void quit()
Asks the main loop to stop: the next mainLoop() returns false, after the exit events.
static bool isHintsLiteMode()
See setHintsLiteMode().
Definition application.hpp:689
static bool getFPSStatus()
static void setFPSStatus(bool enabled)
Whether getFPS() is counted (60 when off).
static std::string getLocale()
static void drawThemeBackground(NVGcontext *vg, float x, float y, float width, float height)
static size_t getFPS()
The frames per second over the last second.
static std::vector< Activity * > getActivitiesStack()
A copy of the stack of activities, the first pushed first.
static void onWindowReposition(int xPos, int yPos)
static std::string getTitle()
The window's title, as given to createWindow().
static void requestRedraw()
static void setHotReload(bool enabled, const std::string &resourcesDir="")
static void setCommonFooter(std::string footer)
A text the app keeps for its footers (the library does not draw it).
static ImeManager * getImeManager()
The platform's text input (the keyboard shown for an EditTextDialog).
static void enableDebuggingView(bool enable)
Draws the debug layer (DebugLayer) over the app.
Definition application.hpp:646
static bool loadFontFromMemory(std::string fontName, void *data, size_t size, bool freeData, int faceIndex)
static void setAppId(std::string appId)
Definition application.hpp:89
static bool getAutomaticDeactivation()
See setAutomaticDeactivation().
static void setInspectorEnabled(bool enabled)
static void unregisterModel(const std::string &name)
static AudioPlayer * getAudioPlayer()
The platform's sound player.
static void removeWatchedKeys(const AuroreKeyCombination key)
See addToWatchedKeys().
static void clear()
Deletes every activity on the stack and clears the focus.
static void setDeactivatedTime(int millisecond)
After this long without an input, the app is drawn at the deactivated FPS.
static bool isReduceMotionSet()
The app's own setting alone, without Cadran's.
Definition application.hpp:242
static void onWindowResized(int width, int height)
static ThemeVariant getThemeVariant()
The variant in use, as the platform reports it.
static bool isSwapInputKeys()
Definition application.hpp:672
static VoidEvent * getWindowShouldCloseEvent()
Fired when the window is closed or quit() was asked, before the exit.
static Time getLastInputTime()
Definition application.hpp:248
static void setFocusAnnouncer(std::function< void(const std::string &)> announcer)
static void clearKeyMap()
static void setWindowSize(int width, int height)
static bool isDebuggingViewEnabled()
Definition application.hpp:651
static void setGlobalQuit(bool enabled)
static VoidEvent * getWindowSizeChangedEvent()
Fired when the window's size changed.
static void setSwapInterval(int interval)
static void mapKey(AuroreKeyCombination key, ControllerButton button)
static bool loadFontFromFile(std::string fontName, std::string filePath)
static void tryDeinitFirstResponder(View *view)
Stops the touch gestures view is tracking, before it is removed.
static void announce(const std::string &text)
static Theme getTheme()
The colours of the variant in use (light or dark), with the theme file's over them.
static bool isRedrawOnDemand()
See setRedrawOnDemand().
static double getDeactivatedFrameTime()
A frame's time once deactivated, in seconds.
static void setDeactivatedFPS(int value)
The frame rate once deactivated (5 by default).
static void blockInputs(bool muteSounds=false)
static uint64_t getDrawnFrameCount()
static void setHighlightIdleFade(bool fade)
static bool isDrawCursor()
Definition application.hpp:683
static VoidEvent * getWindowCreationDoneEvent()
Fired at the end of createWindow().
static uint32_t ORIGINAL_WINDOW_HEIGHT
The design height.
Definition application.hpp:73
static int windowYPos
Definition application.hpp:134
static void setActiveEvent(bool value)
Marks the app active now (and asks for a redraw), or no longer.
static void setTextScale(float scale)
static bool init()
static int getDefaultFont()
The font a new Label draws with: the regular font (the Chinese one on the Switch).
static void setAutomaticDeactivation(bool value)
static NVGcontext * getNVGContext()
The nanovg context every view draws with.
static bool isRedrawDebug()
See setRedrawDebug().
static void setRedrawOnDemand(bool enabled)
static const std::string & getLastAnnouncement()
Definition application.hpp:226
static bool XMLViewsRegisterContains(std::string name)
Whether an XML tag is registered (registerXMLView()).
static bool isHotReload()
See setHotReload().
static void setCadran(bool on)
Definition application.hpp:98
static void onControllerButtonPressed(enum ControllerButton button, bool repeating)
static float contentHeight
Definition application.hpp:128
static void replaceActivity(Activity *activity, TransitionAnimation animation=TransitionAnimation::FADE)
static InputType getInputType()
Definition application.hpp:604
static GenericEvent * getGlobalFocusChangeEvent()
Fired with the new focus each time the focus moves.
static std::string * getCommonFooter()
static bool addFontFallback(const std::string &font, const std::string &fallback)
static VoidEvent * getExitDoneEvent()
Fired once the activities and the views left are deleted.
static void createWindow(std::string title)
static Event< bool > * getWindowFocusChangedEvent()
Fired with true or false when the window gains or loses the system's focus.
static Rect getLastMissedRedrawArea()
static bool hasThemeBackground()
static float maximumUIScale
See setMaximumUIScale().
Definition application.hpp:411
static bool getReduceMotion()
static bool isInspectorEnabled()
See setInspectorEnabled().
static bool rebuildActivity()
static int getDeactivatedFPS()
static DataModel * getModel(const std::string &name)
static uint64_t getMissedRedrawCount()
static float getDensity()
Definition application.hpp:266
static float contentWidth
The size of the UI in layout units: ORIGINAL_WINDOW_WIDTH wide, as high as the window's ratio gives.
Definition application.hpp:128
static unsigned windowHeight
Definition application.hpp:131
static void addToWatchedKeys(const AuroreKeyCombination key)
Reads key each frame from now on (counted: one removeWatchedKeys() per add).
static const std::string & getAppId()
The application ID given to setAppId(), "" by default.
Definition application.hpp:91
static View * getHoveredView()
Definition application.hpp:281
static YGConfig * getYogaConfig()
static View * getCurrentFocus()
The view that has the focus, or nullptr.
static float windowScale
Pixels per layout unit (windowWidth / ORIGINAL_WINDOW_WIDTH, capped by setMaximumUIScale()).
Definition application.hpp:410
static VoidEvent * getGlobalHintsUpdateEvent()
Fired when the footer's hints must be read again (the focus moved, keys swapped).
static int windowXPos
The window's position on the screen (desktop).
Definition application.hpp:134
static void registerModel(const std::string &name, DataModel *model)
static void setDrawCoursor(bool draw)
Draws the mouse pointer with nanovg (for platforms with no system pointer).
Definition application.hpp:678
static Event< ThemeVariant > * getThemeChangedEvent()
static VoidEvent * getPostFrameEvent()
Fired after each frame presented.
static Style getStyle()
The metrics in use (aurore::getStyle()).
Definition application.hpp:284
static void setReduceMotion(bool reduce)
static VoidEvent * getExitEvent()
Fired when the app starts to exit, before the activities are deleted.
static unsigned windowWidth
The window's size in pixels.
Definition application.hpp:131
static void crash(std::string text)
static void setMaximumUIScale(float scale)
static const std::vector< std::pair< AuroreKeyCombination, ControllerButton > > & getKeyMap()
static const ControllerState & getControllerState()
The controller's buttons and sticks as read this frame.
static void setHintsLiteMode(const bool value)
Set hints lite mode, which will only show hints for A button and B button.
Definition application.hpp:695
static bool isCadran()
Whether the app is drawn in Cadran's look (see setCadran()).
Definition application.hpp:100
static void giveFocus(View *view)
static void unblockInputs()
static Event< InputType > * getGlobalInputTypeChangeEvent()
Fired when the input type changes (a touch after the buttons, or the reverse).
static uint32_t ORIGINAL_WINDOW_WIDTH
The design width, in layout units.
Definition application.hpp:72
Definition audio.hpp:45
Definition observable.hpp:222
Definition debug_layer.hpp:26
Definition edit_text_dialog.hpp:9
Definition event.hpp:41
Definition ime.hpp:41
Definition input.hpp:334
Definition notification_manager.hpp:44
Definition platform.hpp:37
Simple wrapper around StyleValues for the array operator.
Definition style.hpp:115
Definition theme.hpp:298
Definition view.hpp:232
Definition actions.hpp:25
TransitionAnimation
Definition view.hpp:153
@ FADE
the old activity fades away and the new one fades in
void setDensity(float density)
ThemeVariant
The two sets of colours every theme has: light and dark.
Definition theme.hpp:37
std::unordered_map< std::string, int > FontStash
Definition font.hpp:38
std::function< View *(void)> XMLViewCreator
Makes a new view for an XML tag (Application::registerXMLView).
Definition application.hpp:63
int ActionIdentifier
Definition actions.hpp:31
ControllerButton
Definition input.hpp:179
FocusDirection
Focus direction when navigating.
Definition view.hpp:96
InputType
Input types for entire app.
Definition application.hpp:53
@ GAMEPAD
Gamepad or keyboard.
@ TOUCH
Touch screen.
ActionType
Definition actions.hpp:105
Style getStyle()
The app's metrics: what XML reads with @style/….
retro_time_t Time
Definition time.hpp:31
float getDensity()
The density in use (DENSITY_COMFORTABLE by default).
Definition cadran.hpp:31
Definition text.hpp:13
Definition actions.hpp:36
Definition input.hpp:251
Definition input.hpp:321
Definition geometry.hpp:26
Definition geometry.hpp:70
A variant's background, as a theme file sets it (docs/themes.md)
Definition theme.hpp:81
Definition theme_image.hpp:49