Plutonium framework API 0.3.0
UI framework libraries for libnx
Loading...
Searching...
No Matches
ui_Application.hpp
Go to the documentation of this file.
1
2/*
3
4 Plutonium library
5
6 @file ui_Application.hpp
7 @brief An Application is the base to use the UI system of this library.
8 @author XorTroll
9
10 @copyright Plutonium project - an easy-to-use UI framework for Nintendo Switch homebrew
11
12*/
13
14#pragma once
15#include <pu/ui/ui_Dialog.hpp>
16#include <pu/ui/ui_Layout.hpp>
17#include <pu/ui/ui_Overlay.hpp>
18#include <chrono>
19
20namespace pu::ui {
21
23 public:
24 using DialogPrepareCallback = std::function<void(Dialog::Ref&)>;
25 using OnInputCallback = std::function<void(const u64, const u64, const u64, const TouchPoint)>;
26 using RenderCallback = std::function<void()>;
27 using RenderOverFunction = std::function<bool(render::Renderer::Ref&)>;
28
29 static constexpr u8 DefaultFadeAlphaIncrementSteps = 20;
30
31 protected:
32 bool loaded;
34 RenderOverFunction render_over_fn;
41 Layout::Ref lyt;
44 std::chrono::steady_clock::time_point ovl_start_time;
46 OnInputCallback on_ipt_cb;
49
50 public:
51 Application(render::Renderer::Ref renderer);
53 virtual ~Application();
54
55 inline void LoadLayout(Layout::Ref lyt) {
56 this->lyt = lyt;
57 }
58
59 template<typename L>
60 inline std::shared_ptr<L> GetLayout() {
61 static_assert(std::is_base_of_v<ui::Layout, L>);
62 return std::static_pointer_cast<L>(this->lyt);
63 }
64
65 void Prepare();
66
67 // Force create a derived Application class which initializes everything here
68 virtual void OnLoad() = 0;
69
70 inline void AddRenderCallback(RenderCallback render_cb) {
71 this->render_cbs.push_back(render_cb);
72 }
73
74 inline void SetOnInput(OnInputCallback on_ipt_cb) {
75 this->on_ipt_cb = on_ipt_cb;
76 }
77
78 inline i32 ShowDialog(Dialog::Ref &dialog) {
79 return dialog->Show(this);
80 }
81
82 i32 CreateShowDialog(const std::string &title, const std::string &content, const std::vector<std::string> &opts, const bool use_last_opt_as_cancel, sdl2::TextureHandle::Ref icon = {}, DialogPrepareCallback prepare_cb = nullptr);
83
84 inline void StartOverlay(Overlay::Ref ovl) {
85 if(this->ovl == nullptr) {
86 this->ovl = ovl;
87 }
88 }
89
90 void StartOverlayWithTimeout(Overlay::Ref ovl, const u64 ms);
91 void EndOverlay();
92 void Show();
93
94 inline void ShowWithFadeIn() {
95 this->FadeIn();
96 this->Show();
97 }
98
99 inline bool IsShown() {
100 return this->is_shown;
101 }
102
103 inline bool CanBeShown() {
104 return this->loaded && (this->lyt != nullptr);
105 }
106
108 bool CallForRenderWithRenderOver(RenderOverFunction render_over_fn);
109
110 inline void LockRender() {
111 rmutexLock(&this->render_lock);
112 }
113
114 inline void UnlockRender() {
115 rmutexUnlock(&this->render_lock);
116 }
117
118 void FadeIn();
119 void FadeOut();
120
121 inline bool IsFadedIn() {
122 return this->fade_alpha > 0;
123 }
124
125 inline void SetFadeAlphaIncrementStepCount(const u8 fade_alpha_increment_steps) {
126 this->fade_alpha_increment_steps = fade_alpha_increment_steps;
127 }
128
131
133 return this->fade_bg_tex;
134 }
135
137 return this->fade_bg_tex != nullptr;
138 }
139
141
142 void OnRender();
143 void Close(const bool do_exit = false);
144
145 inline void CloseWithFadeOut(const bool do_exit = false) {
146 this->FadeOut();
147 this->Close(do_exit);
148 }
149
151 return this->renderer->GetButtonsDown();
152 }
153
154 inline u64 GetButtonsUp() {
155 return this->renderer->GetButtonsUp();
156 }
157
159 return this->renderer->GetButtonsHeld();
160 }
161
163 HidTouchScreenState state = {};
164 hidGetTouchScreenStates(&state, 1);
165 return state;
166 }
167 };
168
169}
Definition sdl2_Types.hpp:18
Definition ui_Application.hpp:22
bool IsShown()
Definition ui_Application.hpp:99
bool is_shown
Definition ui_Application.hpp:35
virtual ~Application()
sdl2::TextureHandle::Ref fade_bg_tex
Definition ui_Application.hpp:39
std::vector< RenderCallback > render_cbs
Definition ui_Application.hpp:45
HidTouchScreenState GetTouchState()
Definition ui_Application.hpp:162
virtual void OnLoad()=0
OnInputCallback on_ipt_cb
Definition ui_Application.hpp:46
Application(render::Renderer::Ref renderer)
static constexpr u8 DefaultFadeAlphaIncrementSteps
Definition ui_Application.hpp:29
SigmoidIncrementer< i32 > fade_alpha_incr
Definition ui_Application.hpp:37
u8 fade_alpha_increment_steps
Definition ui_Application.hpp:36
Layout::Ref lyt
Definition ui_Application.hpp:41
void StartOverlay(Overlay::Ref ovl)
Definition ui_Application.hpp:84
void SetFadeBackgroundColor(const Color clr)
void StartOverlayWithTimeout(Overlay::Ref ovl, const u64 ms)
u64 GetButtonsDown()
Definition ui_Application.hpp:150
void LockRender()
Definition ui_Application.hpp:110
u64 GetButtonsUp()
Definition ui_Application.hpp:154
u64 GetButtonsHeld()
Definition ui_Application.hpp:158
bool CanBeShown()
Definition ui_Application.hpp:103
void AddRenderCallback(RenderCallback render_cb)
Definition ui_Application.hpp:70
void ShowWithFadeIn()
Definition ui_Application.hpp:94
void CloseWithFadeOut(const bool do_exit=false)
Definition ui_Application.hpp:145
void SetFadeBackgroundImage(sdl2::TextureHandle::Ref bg_tex)
bool in_render_over
Definition ui_Application.hpp:33
i32 fade_alpha
Definition ui_Application.hpp:38
bool HasFadeBackgroundImage()
Definition ui_Application.hpp:136
void ResetFadeBackgroundImage()
void SetOnInput(OnInputCallback on_ipt_cb)
Definition ui_Application.hpp:74
RMutex render_lock
Definition ui_Application.hpp:48
bool loaded
Definition ui_Application.hpp:32
void SetFadeAlphaIncrementStepCount(const u8 fade_alpha_increment_steps)
Definition ui_Application.hpp:125
std::shared_ptr< L > GetLayout()
Definition ui_Application.hpp:60
Color fade_bg_clr
Definition ui_Application.hpp:40
void Close(const bool do_exit=false)
bool IsFadedIn()
Definition ui_Application.hpp:121
Overlay::Ref ovl
Definition ui_Application.hpp:42
RenderOverFunction render_over_fn
Definition ui_Application.hpp:34
u64 ovl_timeout_ms
Definition ui_Application.hpp:43
i32 CreateShowDialog(const std::string &title, const std::string &content, const std::vector< std::string > &opts, const bool use_last_opt_as_cancel, sdl2::TextureHandle::Ref icon={}, DialogPrepareCallback prepare_cb=nullptr)
bool CallForRenderWithRenderOver(RenderOverFunction render_over_fn)
render::Renderer::Ref renderer
Definition ui_Application.hpp:47
sdl2::TextureHandle::Ref & GetFadeBackgroundImageTexture()
Definition ui_Application.hpp:132
void LoadLayout(Layout::Ref lyt)
Definition ui_Application.hpp:55
i32 ShowDialog(Dialog::Ref &dialog)
Definition ui_Application.hpp:78
void UnlockRender()
Definition ui_Application.hpp:114
std::chrono::steady_clock::time_point ovl_start_time
Definition ui_Application.hpp:44
Definition ui_Dialog.hpp:22
Definition ui_Layout.hpp:20
Definition ui_Overlay.hpp:19
Definition render_Renderer.hpp:127
Definition sdl2_Types.hpp:10
Definition render_Renderer.hpp:20
#define PU_SMART_CTOR(type)
Definition pu_Include.hpp:20
Definition ui_Types.hpp:44
Definition ui_Types.hpp:67