Plutonium framework API 0.3.0
UI framework libraries for libnx
Loading...
Searching...
No Matches
elm_Button.hpp
Go to the documentation of this file.
1
2/*
3
4 Plutonium library
5
6 @file Button.hpp
7 @brief A Button is an Element for option selecting.
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 <functional>
16#include <pu/ui/elm/elm_Element.hpp>
17
18namespace pu::ui::elm {
19
20 class Button : public Element {
21 public:
22 using OnClickCallback = std::function<void()>;
23
24 static constexpr u8 DefaultDarkerColorFactor = 70;
25
26 static constexpr u8 DefaultHoverAlphaIncrementSteps = 48;
27
29
30 private:
31 i32 x;
32 i32 y;
33 i32 w;
34 i32 h;
35 std::string fnt_name;
36 Color bg_clr;
37 Color cnt_clr;
38 std::string cnt;
39 sdl2::Texture cnt_tex;
40 OnClickCallback on_click_cb;
41 bool hover;
42 i32 hover_alpha;
43 SigmoidIncrementer<i32> hover_alpha_incr;
44 u8 darker_color_factor;
45 u8 hover_alpha_incr_steps;
46
47 inline Color MakeHoverBackgroundColor(const i32 alpha) {
48 i32 base_r = this->bg_clr.r - this->darker_color_factor;
49 if(base_r < 0) {
50 base_r = 0;
51 }
52 i32 base_g = this->bg_clr.g - this->darker_color_factor;
53 if(base_g < 0) {
54 base_g = 0;
55 }
56 i32 base_b = this->bg_clr.b - this->darker_color_factor;
57 if(base_b < 0) {
58 base_b = 0;
59 }
60
61 auto base_a = this->bg_clr.a;
62 if(alpha >= 0) {
63 base_a = static_cast<u8>(alpha);
64 }
65
66 return { static_cast<u8>(base_r), static_cast<u8>(base_g), static_cast<u8>(base_b), base_a };
67 }
68
69 public:
70 Button(const i32 x, const i32 y, const i32 width, const i32 height, const std::string &content, const Color content_clr, const Color bg_clr);
73
74 inline i32 GetX() override {
75 return this->x;
76 }
77
78 inline void SetX(const i32 x) {
79 this->x = x;
80 }
81
82 inline i32 GetY() override {
83 return this->y;
84 }
85
86 inline void SetY(const i32 y) {
87 this->y = y;
88 }
89
90 inline i32 GetWidth() override {
91 return this->w;
92 }
93
94 inline void SetWidth(const i32 width) {
95 this->w = width;
96 }
97
98 inline i32 GetHeight() override {
99 return this->h;
100 }
101
102 inline void SetHeight(const i32 height) {
103 this->h = height;
104 }
105
106 inline std::string GetContent() {
107 return this->cnt;
108 }
109
110 void SetContent(const std::string &content);
111
112 PU_CLASS_POD_GET(ContentColor, cnt_clr, Color)
113
114 void SetContentColor(const Color content_clr);
115
116 PU_CLASS_POD_GETSET(BackgroundColor, bg_clr, Color)
117
118 void SetContentFont(const std::string &font_name);
119
120 inline void SetOnClick(OnClickCallback on_click_cb) {
121 this->on_click_cb = on_click_cb;
122 }
123
124 void OnRender(render::Renderer::Ref &drawer, const i32 x, const i32 y) override;
125 void OnInput(const u64 keys_down, const u64 keys_up, const u64 keys_held, const TouchPoint touch_pos) override;
126 };
127
128}
Definition elm_Button.hpp:20
void SetOnClick(OnClickCallback on_click_cb)
Definition elm_Button.hpp:120
void SetHeight(const i32 height)
Definition elm_Button.hpp:102
static constexpr u8 DefaultHoverAlphaIncrementSteps
Definition elm_Button.hpp:26
void SetContent(const std::string &content)
void SetY(const i32 y)
Definition elm_Button.hpp:86
i32 GetY() override
Definition elm_Button.hpp:82
static constexpr u8 DefaultDarkerColorFactor
Definition elm_Button.hpp:24
void OnRender(render::Renderer::Ref &drawer, const i32 x, const i32 y) override
std::string GetContent()
Definition elm_Button.hpp:106
Button(const i32 x, const i32 y, const i32 width, const i32 height, const std::string &content, const Color content_clr, const Color bg_clr)
void OnInput(const u64 keys_down, const u64 keys_up, const u64 keys_held, const TouchPoint touch_pos) override
void SetX(const i32 x)
Definition elm_Button.hpp:78
void SetContentColor(const Color content_clr)
static constexpr DefaultFontSize DefaultContentFontSize
Definition elm_Button.hpp:28
void SetContentFont(const std::string &font_name)
void SetWidth(const i32 width)
Definition elm_Button.hpp:94
i32 GetX() override
Definition elm_Button.hpp:74
i32 GetHeight() override
Definition elm_Button.hpp:98
i32 GetWidth() override
Definition elm_Button.hpp:90
Definition elm_Element.hpp:37
Definition render_Renderer.hpp:127
Definition sdl2_Types.hpp:10
Definition render_Renderer.hpp:20
Definition elm_Button.hpp:18
DefaultFontSize
Definition ui_Types.hpp:21
#define PU_SMART_CTOR(type)
Definition pu_Include.hpp:20
#define PU_CLASS_POD_GETSET(fn_name, var_name, type)
Definition pu_Include.hpp:37
#define PU_CLASS_POD_GET(fn_name, var_name, type)
Definition pu_Include.hpp:27
Definition ui_Types.hpp:44
Definition ui_Types.hpp:67