dl
dl_ui_button_base.h
浏览该文件的文档.
1
15#pragma once
16
17#include "math/dl_coor.h"
18#include "dl_type.h"
19#include "io/dl_input.h"
20#include "math/dl_align.h"
21#include "misc/dl_component.h"
22
23namespace dl::UI
24{
25
27class Button
28{
29public:
30 enum class State
31 {
32 NORMAL,
33 OVER,
34 DOWN,
36 };
37
38 enum class Mode
39 {
40 BUTTON,
41 SWITCH,
42 RADIO,
43 };
44
46 _state(State::NORMAL),
47 _statePre(State::NORMAL),
48 _mode(Mode::BUTTON),
49 _bOpen{ false },
50 _bDisable(false),
51 _cpt{this}
52 {
54 _init_cpt();
55 }
56
57 //克隆使用
58 Button(const Button& b) :
59 _state(b._state),
60 _statePre(b._statePre),
61 _mode(b._mode),
62 _bOpen{ b._bOpen },
63 _bDisable(b._bDisable),
64 _cpt{ this }
65 {
66 _coor = Coor::Create(const_cast<Button&>(b).GetCoor()->GetParent());
67 _init_cpt();
68 }
69
70
71 void Update();
72 void Render();
73
80 bool IsClick();
81
83 bool IsThrough();
84
85 bool IsRadio() { return _mode == Mode::RADIO; }
86
87 bool IsSelect() { return _bOpen; }
88
89 void SetSelect(bool open);
90
91 void SetMode(Mode mode);
92
93 void SetDisable(bool disable) { _bDisable = disable; }
94
102 void SetSound(std::string_view click, std::string_view over)
103 {
104 _strSndClick = std::string{ click };
105 _strSndOver = std::string{ over };
106 }
107
108 State GetState() { return _state; }
109
110 Coor* GetCoor() { return _coor; }
111
112 virtual Button* Clone() = 0;
113 virtual bool IsPickup() = 0;
114 virtual void SetOrder(float order) = 0;
115 virtual Position GetSize() = 0;
116 virtual Position GetAnchor() = 0;
117
119 {
120 _cptElemData._hv = hv;
122 }
123
124 Components* GetCpt() { return &_cpt; }
125
126 Mode GetMode() { return _mode; }
127
128 virtual ~Button()
129 {
130 delete _coor;
131 }
132private:
133 State _state;
134 State _statePre;
135 bool _bDisable;
136
137 Mode _mode;
138 bool _bOpen;
139 bool _isSwitchClick;
140
142 std::string _strSndClick;
143 std::string _strSndOver;
144protected:
146
148 std::size_t GetIndex(State state)
149 {
150 if (_bDisable)
151 {
152 if (state == State::NORMAL || state == State::OVER)
153 return 3;
154 else
155 return 4;
156 }
157 else
158 {
159 return (std::size_t)state;
160 }
161 }
162
163 //
165 {
166 _cptElemData._size = GetSize();
167 _cptElemData._anchor = GetAnchor();
168 }
169
170private:
171 //--------------------------组件------------------------
172 Components _cpt;
173 Cpt::DeleteThis _cptDeleteThis;
174 Cpt::Update _cptUpdate;
175 Cpt::Render _cptRender;
176 Cpt::IsPickup _cptIsPickup;
177 Cpt::SetOrder _cptSetOrder;
178 Cpt::GetCoor _cptGetCoor;
179 Cpt::IsClick _cptIsClick;
180 Cpt::IsSelect _cptIsOpen;
181 Cpt::SetSelect _cptSetOpen;
182 Cpt::ElementData _cptElemData;
183 Cpt::Clone _cptClone;
184
185 void _delete_this() { delete this; }
186
187 Components* _clone()
188 {
189 return Clone()->GetCpt();
190 }
191
192 void _init_cpt()
193 {
194 _cptDeleteThis = std::bind(&Button::_delete_this, this);
195 _cptUpdate = std::bind(&Button::Update, this);
196 _cptRender = std::bind(&Button::Render, this);
197 _cptIsPickup = std::bind(&Button::IsPickup, this);
198 _cptSetOrder = std::bind(&Button::SetOrder, this, std::placeholders::_1);
199 _cptGetCoor = std::bind(&Button::GetCoor, this);
200 _cptIsClick = std::bind(&Button::IsClick, this);
201 _cptIsOpen = std::bind(&Button::IsSelect, this);
202 _cptSetOpen = std::bind(&Button::SetSelect, this, std::placeholders::_1);
203 _cptClone = std::bind(&Button::_clone, this);
204
205 _cpt.PushBack(&_cptDeleteThis);
206 _cpt.PushBack(&_cptUpdate);
207 _cpt.PushBack(&_cptRender);
208 _cpt.PushBack(&_cptIsPickup);
209 _cpt.PushBack(&_cptSetOrder);
210 _cpt.PushBack(&_cptGetCoor);
211 _cpt.PushBack(&_cptIsClick);
212 _cpt.PushBack(&_cptIsOpen);
213 _cpt.PushBack(&_cptSetOpen);
214 _cpt.PushBack(&_cptClone);
215
216 _cpt.PushBack(&_cptElemData);
217 }
218
219
220 static Button* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
221
225 virtual void UpdateChild(State state_pre, State state) = 0;
226 virtual void RenderChild(State state_pre, State state) = 0;
227
228 void _update_button();
229 void _update_switch();
230 void _update_radio();
231};
232
235
236}
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:347
Align::Direction2 _hv
Definition: dl_component.h:379
按钮(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