Plutonium framework API 0.3.0
UI framework libraries for libnx
Loading...
Searching...
No Matches
ui_Types.hpp
Go to the documentation of this file.
1
2/*
3
4 Plutonium library
5
6 @file ui_Types.hpp
7 @brief Several basic types helpful for UI and rendering
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/pu_Include.hpp>
16
17namespace pu::ui {
18
19 // Font sizes Plutonium components use by default
20
21 enum class DefaultFontSize : u32 {
22 Small,
23 Medium,
25 Large,
26
27 Count
28 };
29
30 constexpr u32 DefaultFontSizes[static_cast<u32>(DefaultFontSize::Count)] = { 27, 30, 37, 45 };
31
32 inline std::string MakeDefaultFontName(const u32 font_size) {
33 return "DefaultFont@" + std::to_string(font_size);
34 }
35
36 inline constexpr u32 GetDefaultFontSize(const DefaultFontSize kind) {
37 return DefaultFontSizes[static_cast<u32>(kind)];
38 }
39
40 inline std::string GetDefaultFont(const DefaultFontSize kind) {
41 return MakeDefaultFontName(GetDefaultFontSize(kind));
42 }
43
44 struct Color {
49
50 constexpr Color() : r(0), g(0), b(0), a(0xFF) {}
51 constexpr Color(const u8 r, const u8 g, const u8 b, const u8 a) : r(r), g(g), b(b), a(a) {}
52
53 // Expected format: '#rrggbbaa'
54 static Color FromHex(const std::string &str_clr);
55
56 inline Color WithAlpha(const u8 a) {
57 return { this->r, this->g, this->b, a };
58 }
59 };
60
61 static inline constexpr bool TouchHitsRegion(const i32 touch_x, const i32 touch_y, const i32 region_x, const i32 region_y, const i32 region_w, const i32 region_h) {
62 return (touch_x >= region_x) && (touch_x < (region_x + region_w)) && (touch_y >= region_y) && (touch_y < (region_y + region_h));
63 }
64
65 constexpr u64 TouchPseudoKey = 1 << 29;
66
67 struct TouchPoint {
70
71 constexpr TouchPoint() : x(-1), y(-1) {}
72 constexpr TouchPoint(const u32 x, const u32 y) : x(x), y(y) {}
73
74 inline constexpr bool IsEmpty() const {
75 return (this->x < 0) && (this->y < 0);
76 }
77
78 inline constexpr bool HitsRegion(const i32 region_x, const i32 region_y, const i32 region_w, const i32 region_h) const {
79 if(this->IsEmpty()) {
80 return false;
81 }
82
83 return TouchHitsRegion(this->x, this->y, region_x, region_y, region_w, region_h);
84 }
85 };
86
87 template<typename T>
89 private:
90 double f;
91 double f_incr;
92 T target_initial_val;
93 T target_incr;
94 bool inverted_for_zero;
95
96 inline double ComputeIncrement() {
97 return (double)this->target_incr * (1.0f / (1.0f + exp(-this->f)));
98 }
99
100 public:
101 static constexpr double AllowedError = 0.4f;
102
103 constexpr SigmoidIncrementer() : f(0.0f), f_incr(0.0f), target_initial_val(0), target_incr(0) {}
104
105 void Start(const u32 f_steps, const T target_initial_val, const T target_incr) {
106 this->target_initial_val = target_initial_val;
107 this->target_incr = target_incr;
108 this->inverted_for_zero = false;
109
110 auto target_final_val = target_initial_val + target_incr;
111 if(target_final_val == 0) {
112 // Compute from 0 to X instead of from X to 0
113 this->inverted_for_zero = true;
114 this->target_initial_val = 0;
115 this->target_incr = target_initial_val;
116 target_final_val = target_initial_val;
117 }
118
119 const auto f_limit_abs = log((abs((double)target_final_val) - AllowedError) / AllowedError);
120 this->f_incr = (2.0f * f_limit_abs) / (double)f_steps;
121 this->f = -f_limit_abs;
122 }
123
124 inline void StartFromZero(const u32 f_steps, const T target_final_val) {
125 this->Start(f_steps, 0, target_final_val);
126 }
127
128 inline void StartToZero(const u32 f_steps, const T target_initial_val) {
129 this->Start(f_steps, target_initial_val, -target_initial_val);
130 }
131
132 bool Increment(T &target) {
133 if(this->IsDone()) {
134 return false;
135 }
136
137 const auto target_f = (double)this->target_initial_val + this->ComputeIncrement();
138 if(this->target_incr > 0) {
139 target = (T)((i32)(target_f + 0.5f));
140 }
141 else {
142 target = (T)((i32)(target_f - 0.5f));
143 }
144 this->f += this->f_incr;
145
146 const auto is_done = abs((double)target) >= abs((double)(this->target_initial_val + this->target_incr));
147 if(is_done) {
148 if(this->inverted_for_zero) {
149 target = 0;
150 }
151 else {
152 target = this->target_initial_val + this->target_incr;
153 }
154 this->target_incr = 0;
155 return true;
156 }
157 else if(this->inverted_for_zero) {
158 target = this->target_incr - target;
159 }
160
161 return false;
162 }
163
164 inline bool IsDone() {
165 return this->target_incr == 0;
166 }
167 };
168
169}
Definition ui_Types.hpp:88
bool IsDone()
Definition ui_Types.hpp:164
void StartFromZero(const u32 f_steps, const T target_final_val)
Definition ui_Types.hpp:124
void StartToZero(const u32 f_steps, const T target_initial_val)
Definition ui_Types.hpp:128
bool Increment(T &target)
Definition ui_Types.hpp:132
void Start(const u32 f_steps, const T target_initial_val, const T target_incr)
Definition ui_Types.hpp:105
constexpr SigmoidIncrementer()
Definition ui_Types.hpp:103
static constexpr double AllowedError
Definition ui_Types.hpp:101
constexpr u32 DefaultFontSizes[static_cast< u32 >(DefaultFontSize::Count)]
Definition ui_Types.hpp:30
static constexpr bool TouchHitsRegion(const i32 touch_x, const i32 touch_y, const i32 region_x, const i32 region_y, const i32 region_w, const i32 region_h)
Definition ui_Types.hpp:61
std::string MakeDefaultFontName(const u32 font_size)
Definition ui_Types.hpp:32
constexpr u64 TouchPseudoKey
Definition ui_Types.hpp:65
constexpr u32 GetDefaultFontSize(const DefaultFontSize kind)
Definition ui_Types.hpp:36
std::string GetDefaultFont(const DefaultFontSize kind)
Definition ui_Types.hpp:40
DefaultFontSize
Definition ui_Types.hpp:21
Definition ui_Types.hpp:44
static Color FromHex(const std::string &str_clr)
u8 g
Definition ui_Types.hpp:46
u8 a
Definition ui_Types.hpp:48
u8 r
Definition ui_Types.hpp:45
Color WithAlpha(const u8 a)
Definition ui_Types.hpp:56
constexpr Color()
Definition ui_Types.hpp:50
u8 b
Definition ui_Types.hpp:47
constexpr Color(const u8 r, const u8 g, const u8 b, const u8 a)
Definition ui_Types.hpp:51
Definition ui_Types.hpp:67
i32 y
Definition ui_Types.hpp:69
constexpr TouchPoint(const u32 x, const u32 y)
Definition ui_Types.hpp:72
constexpr TouchPoint()
Definition ui_Types.hpp:71
constexpr bool HitsRegion(const i32 region_x, const i32 region_y, const i32 region_w, const i32 region_h) const
Definition ui_Types.hpp:78
i32 x
Definition ui_Types.hpp:68
constexpr bool IsEmpty() const
Definition ui_Types.hpp:74