Plutonium framework API 0.3.0
UI framework libraries for libnx
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>
17
18namespace pu::ui::elm {
19
20 class Button : public Element {
21 public:
22 using OnClickCallback = std::function<void()>;
23
24 static constexpr u8 DarkerColorFactor = 70;
25
26 static constexpr u8 HoverAlphaIncrement = 48;
27
28 private:
29 i32 x;
30 i32 y;
31 i32 w;
32 i32 h;
33 std::string fnt_name;
34 Color bg_clr;
35 Color cnt_clr;
36 std::string cnt;
37 sdl2::Texture cnt_tex;
38 OnClickCallback on_click_cb;
39 bool hover;
40 i32 hover_alpha;
41
42 inline Color MakeHoverBackgroundColor(const i32 alpha) {
43 i32 base_r = this->bg_clr.r - DarkerColorFactor;
44 if(base_r < 0) {
45 base_r = 0;
46 }
47 i32 base_g = this->bg_clr.g - DarkerColorFactor;
48 if(base_g < 0) {
49 base_g = 0;
50 }
51 i32 base_b = this->bg_clr.b - DarkerColorFactor;
52 if(base_b < 0) {
53 base_b = 0;
54 }
55
56 auto base_a = this->bg_clr.a;
57 if(alpha >= 0) {
58 base_a = static_cast<u8>(alpha);
59 }
60
61 return { static_cast<u8>(base_r), static_cast<u8>(base_g), static_cast<u8>(base_b), base_a };
62 }
63
64 public:
65 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);
68
69 inline i32 GetX() override {
70 return this->x;
71 }
72
73 inline void SetX(const i32 x) {
74 this->x = x;
75 }
76
77 inline i32 GetY() override {
78 return this->y;
79 }
80
81 inline void SetY(const i32 y) {
82 this->y = y;
83 }
84
85 inline i32 GetWidth() override {
86 return this->w;
87 }
88
89 inline void SetWidth(const i32 width) {
90 this->w = width;
91 }
92
93 inline i32 GetHeight() override {
94 return this->h;
95 }
96
97 inline void SetHeight(const i32 height) {
98 this->h = height;
99 }
100
101 inline std::string GetContent() {
102 return this->cnt;
103 }
104
105 void SetContent(const std::string &content);
106
108 return this->cnt_clr;
109 }
110
111 void SetContentColor(const Color content_clr);
112
114 return this->bg_clr;
115 }
116
117 inline void SetBackgroundColor(const Color bg_clr) {
118 this->bg_clr = bg_clr;
119 }
120
121 void SetContentFont(const std::string &font_name);
122
123 inline void SetOnClick(OnClickCallback on_click_cb) {
124 this->on_click_cb = on_click_cb;
125 }
126
127 void OnRender(render::Renderer::Ref &drawer, const i32 x, const i32 y) override;
128 void OnInput(const u64 keys_down, const u64 keys_up, const u64 keys_held, const TouchPoint touch_pos) override;
129 };
130
131}
Definition: elm_Button.hpp:20
void SetOnClick(OnClickCallback on_click_cb)
Definition: elm_Button.hpp:123
void SetHeight(const i32 height)
Definition: elm_Button.hpp:97
void SetContent(const std::string &content)
void SetY(const i32 y)
Definition: elm_Button.hpp:81
i32 GetY() override
Definition: elm_Button.hpp:77
void OnRender(render::Renderer::Ref &drawer, const i32 x, const i32 y) override
std::string GetContent()
Definition: elm_Button.hpp:101
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:73
Color GetContentColor()
Definition: elm_Button.hpp:107
void SetContentColor(const Color content_clr)
void SetContentFont(const std::string &font_name)
void SetBackgroundColor(const Color bg_clr)
Definition: elm_Button.hpp:117
std::function< void()> OnClickCallback
Definition: elm_Button.hpp:22
void SetWidth(const i32 width)
Definition: elm_Button.hpp:89
i32 GetX() override
Definition: elm_Button.hpp:69
i32 GetHeight() override
Definition: elm_Button.hpp:93
i32 GetWidth() override
Definition: elm_Button.hpp:85
Color GetBackgroundColor()
Definition: elm_Button.hpp:113
static constexpr u8 HoverAlphaIncrement
Definition: elm_Button.hpp:26
static constexpr u8 DarkerColorFactor
Definition: elm_Button.hpp:24
Definition: elm_Element.hpp:37
SDL_Texture * Texture
Definition: sdl2_Types.hpp:11
Definition: elm_Button.hpp:18
s32 i32
Definition: pu_Include.hpp:28
#define PU_SMART_CTOR(type)
Definition: pu_Include.hpp:19
Definition: ui_Types.hpp:44
u8 g
Definition: ui_Types.hpp:46
u8 a
Definition: ui_Types.hpp:48
u8 r
Definition: ui_Types.hpp:45
u8 b
Definition: ui_Types.hpp:47
Definition: ui_Types.hpp:62