dl
dl_ui_button_base.h
浏览该文件的文档.
1
15#pragma once
16
17#ifndef DL_DISABLE_RENDER
18#include "math/dl_coor.h"
19#include "dl_type.h"
20#include "io/dl_input.h"
21#include "math/dl_align.h"
22#include "misc/dl_component.h"
23
24namespace dl::UI
25{
26
28class Button
29{
30public:
31 enum class State
32 {
33 NORMAL,
34 OVER,
35 DOWN,
37 };
38
39 enum class Mode
40 {
41 BUTTON,
42 SWITCH,
43 RADIO,
44 };
45
47 _state(State::NORMAL),
48 _statePre(State::NORMAL),
49 _mode(Mode::BUTTON),
50 _bOpen{ false },
51 _bDisable(false),
52 _cpt{this}
53 {
55 _init_cpt();
56 }
57
58 //克隆使用
59 Button(const Button& b) :
60 _state(b._state),
61 _statePre(b._statePre),
62 _mode(b._mode),
63 _bOpen{ b._bOpen },
64 _bDisable(b._bDisable),
65 _cpt{ this }
66 {
67 _coor = Coor::Create(const_cast<Button&>(b).GetCoor()->GetParent());
68 _init_cpt();
69 }
70
71
72 void Update();
73 void Render();
74
81 bool IsClick();
82
84 bool IsThrough();
85
86 bool IsRadio() { return _mode == Mode::RADIO; }
87
88 bool IsSelect() { return _bOpen; }
89
90 void SetSelect(bool open);
91
92 void SetMode(Mode mode);
93
94 void SetDisable(bool disable) { _bDisable = disable; }
95
103 void SetSound(std::string_view click, std::string_view over)
104 {
105 _strSndClick = std::string{ click };
106 _strSndOver = std::string{ over };
107 }
108
109 State GetState() { return _state; }
110
111 Coor* GetCoor() { return _coor; }
112
113 virtual Button* Clone() = 0;
114 virtual bool IsPickup() = 0;
115 virtual void SetOrder(float order) = 0;
116 virtual Position GetSize() = 0;
117 virtual Position GetAnchor() = 0;
118
120 {
121 _cptElemData._hv = hv;
123 }
124
125 Components* GetCpt() { return &_cpt; }
126
127 Mode GetMode() { return _mode; }
128
129 virtual ~Button()
130 {
131 delete _coor;
132 }
133private:
134 State _state;
135 State _statePre;
136 bool _bDisable;
137
138 Mode _mode;
139 bool _bOpen;
140 bool _isSwitchClick;
141
143 std::string _strSndClick;
144 std::string _strSndOver;
145protected:
147
149 std::size_t GetIndex(State state)
150 {
151 if (_bDisable)
152 {
153 if (state == State::NORMAL || state == State::OVER)
154 return 3;
155 else
156 return 4;
157 }
158 else
159 {
160 return (std::size_t)state;
161 }
162 }
163
164 //
166 {
167 _cptElemData._size = GetSize();
168 _cptElemData._anchor = GetAnchor();
169 }
170
171private:
172 //--------------------------组件------------------------
173 Components _cpt;
174 Cpt::DeleteThis _cptDeleteThis;
175 Cpt::Update _cptUpdate;
176 Cpt::Render _cptRender;
177 Cpt::IsPickup _cptIsPickup;
178 Cpt::SetOrder _cptSetOrder;
179 Cpt::GetCoor _cptGetCoor;
180 Cpt::IsClick _cptIsClick;
181 Cpt::IsSelect _cptIsOpen;
182 Cpt::SetSelect _cptSetOpen;
183 Cpt::ElementData _cptElemData;
184 Cpt::Clone _cptClone;
185
186 void _delete_this() { delete this; }
187
188 Components* _clone()
189 {
190 return Clone()->GetCpt();
191 }
192
193 void _init_cpt()
194 {
195 _cptDeleteThis = std::bind(&Button::_delete_this, this);
196 _cptUpdate = std::bind(&Button::Update, this);
197 _cptRender = std::bind(&Button::Render, this);
198 _cptIsPickup = std::bind(&Button::IsPickup, this);
199 _cptSetOrder = std::bind(&Button::SetOrder, this, std::placeholders::_1);
200 _cptGetCoor = std::bind(&Button::GetCoor, this);
201 _cptIsClick = std::bind(&Button::IsClick, this);
202 _cptIsOpen = std::bind(&Button::IsSelect, this);
203 _cptSetOpen = std::bind(&Button::SetSelect, this, std::placeholders::_1);
204 _cptClone = std::bind(&Button::_clone, this);
205
206 _cpt.PushBack(&_cptDeleteThis);
207 _cpt.PushBack(&_cptUpdate);
208 _cpt.PushBack(&_cptRender);
209 _cpt.PushBack(&_cptIsPickup);
210 _cpt.PushBack(&_cptSetOrder);
211 _cpt.PushBack(&_cptGetCoor);
212 _cpt.PushBack(&_cptIsClick);
213 _cpt.PushBack(&_cptIsOpen);
214 _cpt.PushBack(&_cptSetOpen);
215 _cpt.PushBack(&_cptClone);
216
217 _cpt.PushBack(&_cptElemData);
218 }
219
220
221 static Button* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
222
226 virtual void UpdateChild(State state_pre, State state) = 0;
227 virtual void RenderChild(State state_pre, State state) = 0;
228
229 void _update_button();
230 void _update_switch();
231 void _update_radio();
232};
233
236
237}
238
239#endif
void PushBack(AnyType any)
添加一个任意对象
Definition: dl_component.h:246
2d坐标系
Definition: dl_coor.h:27
static Coor * Create(Coor *parent=nullptr)
元素通用属性(默认左上对齐,便于理解)
Definition: dl_component.h:362
Align::Direction2 _hv
Definition: dl_component.h:394
按钮(Button、Switch、Radio)
void SetMode(Mode mode)
void SetDisable(bool disable)
virtual Button * Clone()=0
bool IsClick()
控件被点击
virtual Position GetSize()=0
Components * GetCpt()
virtual bool IsPickup()=0
void SetSound(std::string_view click, std::string_view over)
设置按钮音效
@ OVER
鼠标经过
@ DOWN
持续按下
Button(const Button &b)
virtual Position GetAnchor()=0
void SetElementData(const Align::Direction2 &hv)
@ SWITCH
开关,按下后不会自动弹起,再次点击后弹起
@ RADIO
收音机按钮,按下后就不能弹起,只能通过代码弹起
void _refresh_element_data()
std::size_t GetIndex(State state)
返回索引
bool IsThrough()
经过按钮,直到离开控件再次进入才会返回真
virtual void SetOrder(float order)=0
void SetSelect(bool open)
方向和对齐定义
组件系统
2d坐标系
键盘与手柄等按键操作
通用类型
std::array< Direction, 2 > Direction2
Definition: dl_align.h:31
Position2 Position
Definition: dl_type_def.h:43