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"
23
24namespace dl::UI
25{
26
28class Button
29{
30public:
41
42 enum class Mode
43 {
47 };
48
50 _state{ State::NORMAL },
51 _statePre{ State::NORMAL },
52 _mode{ Mode::BUTTON },
53 _bOpen{ false },
54 _isClick{ false },
55 _bDisable{false},
56 _cpt{ this }
57 {
59 _init_cpt();
60 }
61
62 // 克隆使用,init_cpt不赋值值类的组件拷贝,需要手动处理
63 Button(const Button& b) :
64 _state{ b._state },
65 _statePre{ b._statePre },
66 _mode{ b._mode },
67 _bOpen{ b._bOpen },
68 _isClick{ b._isClick },
69 _bDisable{ b._bDisable },
70 _cptElemData{ b._cptElemData },
71 _cpt{ this }
72 {
73 _coor = Coor::Create(const_cast<Button&>(b).GetCoor()->GetParent());
74 _init_cpt();
75 }
76
77
78 void Update();
79 void Render();
80
87 bool IsClick();
92 bool IsDown()
93 {
94 return _state == State::DOWN;
95 }
96
98 bool IsThrough();
99
100 bool IsRadio() { return _mode == Mode::RADIO; }
105 bool IsSelect() { return _bOpen; }
106
107 void SetSelect(bool open);
108
109 void SetMode(Mode mode);
110
111 void SetDisable(bool disable) { _bDisable = disable; }
112
120 void SetSound(std::string_view click, std::string_view over)
121 {
122 _strSndClick = std::string{ click };
123 _strSndOver = std::string{ over };
124 }
125
126 State GetState() { return _state; }
127
128 Coor* GetCoor() { return _coor; }
129
130 virtual Button* Clone() = 0;
131 virtual bool IsPickup() = 0;
132 virtual void SetOrder(float order) = 0;
133 virtual Position GetSize() = 0;
134 virtual Position GetAnchor() = 0;
135 virtual void RenderDebug() = 0;
136
137 void SetElementData(const Align::Direction2& hv, const Position& pos = {})
138 {
139 _cptElemData._hv = hv;
140 _cptElemData._pos = pos;
142 }
143 void SetElementDataRunAble(bool able)
144 {
145 _cptElemData._bRunAble = able;
146 }
147
148 Components* GetCpt() { return &_cpt; }
149
150 Mode GetMode() { return _mode; }
151
152 // not use
154 {
155 return _clickControl;
156 }
157
158 virtual ~Button()
159 {
160 if (_clickControl == this)
161 _clickControl = nullptr;
162 delete _coor;
163 }
164private:
165 State _state;
166 State _statePre;
167 bool _bDisable;
168
169 Mode _mode;
170 bool _bOpen;
171 bool _isClick;
172
174 std::string _strSndClick;
175 std::string _strSndOver;
176protected:
178
180 std::size_t GetIndex(State state)
181 {
182 if (_bDisable)
183 {
184 if (state == State::NORMAL || state == State::OVER)
185 return 3;
186 else
187 return 4;
188 }
189 else
190 {
191 return (std::size_t)state;
192 }
193 }
194
195 //
197 {
198 _cptElemData._size = GetSize();
199 _cptElemData._anchor = GetAnchor();
200 }
201
202 constexpr bool _use_over()
203 {
204#if DL_PLATFORM == DL_PLATFORM_ANDROID
205 return false;
206#else
207 return true;
208#endif
209 }
210private:
211 //--------------------------组件------------------------
212 Components _cpt;
213 Cpt::DeleteThis _cptDeleteThis;
214 Cpt::Update _cptUpdate;
215 Cpt::Render _cptRender;
216 Cpt::IsPickup _cptIsPickup;
217 Cpt::SetOrder _cptSetOrder;
218 Cpt::GetCoor _cptGetCoor;
219 Cpt::IsClick _cptIsClick;
220 Cpt::IsDown _cptIsDown;
221 Cpt::IsSelect _cptIsOpen;
222 Cpt::SetSelect _cptSetOpen;
223 Cpt::RenderDebug _cptRenderDebug;
224 Cpt::ElementData _cptElemData;
225 Cpt::Clone _cptClone;
226
227 void _delete_this() { delete this; }
228
229 Components* _clone()
230 {
231 return Clone()->GetCpt();
232 }
233
234 void _init_cpt()
235 {
236 _cptDeleteThis = std::bind(&Button::_delete_this, this);
237 _cptUpdate = std::bind(&Button::Update, this);
238 _cptRender = std::bind(&Button::Render, this);
239 _cptIsPickup = std::bind(&Button::IsPickup, this);
240 _cptSetOrder = std::bind(&Button::SetOrder, this, std::placeholders::_1);
241 _cptGetCoor = std::bind(&Button::GetCoor, this);
242 _cptIsClick = std::bind(&Button::IsClick, this);
243 _cptIsDown = std::bind(&Button::IsDown, this);
244 _cptIsOpen = std::bind(&Button::IsSelect, this);
245 _cptSetOpen = std::bind(&Button::SetSelect, this, std::placeholders::_1);
246 _cptRenderDebug = std::bind(&Button::RenderDebug, this);
247 _cptClone = std::bind(&Button::_clone, this);
248
249 _cpt.PushBack(&_cptDeleteThis);
250 _cpt.PushBack(&_cptUpdate);
251 _cpt.PushBack(&_cptRender);
252 _cpt.PushBack(&_cptIsPickup);
253 _cpt.PushBack(&_cptSetOrder);
254 _cpt.PushBack(&_cptGetCoor);
255 _cpt.PushBack(&_cptIsClick);
256 _cpt.PushBack(&_cptIsDown);
257 _cpt.PushBack(&_cptIsOpen);
258 _cpt.PushBack(&_cptSetOpen);
259 _cpt.PushBack(&_cptRenderDebug);
260 _cpt.PushBack(&_cptClone);
261
262 _cpt.PushBack(&_cptElemData);
263 }
264
265
266 static Button* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
267
271 virtual void UpdateChild(State state_pre, State state) = 0;
272 virtual void RenderChild(State state_pre, State state) = 0;
273
274 void _update_button();
275 void _update_switch();
276 void _update_radio();
277};
278
281
282}
283
284#endif
void PushBack(AnyType any)
添加一个任意对象
2d坐标系
static Coor * Create(Coor *parent=nullptr)
元素通用属性(默认左上对齐,便于理解)
void SetMode(Mode mode)
constexpr bool _use_over()
bool IsDown()
控件按下状态
void SetDisable(bool disable)
void SetElementDataRunAble(bool able)
virtual Button * Clone()=0
void SetElementData(const Align::Direction2 &hv, const Position &pos={})
bool IsClick()
控件被点击
virtual Position GetSize()=0
virtual bool IsPickup()=0
void SetSound(std::string_view click, std::string_view over)
设置按钮音效
@ OVER
经过(仅有光标时使用)
@ DOWN_OVER
按下经过(Switch使用)
@ DOWN_DISABLE
按下禁用(Switch、Radio使用)
bool IsSelect()
控件是选中状态
Button(const Button &b)
virtual Position GetAnchor()=0
virtual void RenderDebug()=0
@ SWITCH
开关,按下后不会自动弹起,再次点击后弹起
@ RADIO
收音机按钮,按下后就不能弹起,只能通过代码弹起
static Button * GetClickControl()
std::size_t GetIndex(State state)
返回索引
bool IsThrough()
经过按钮,直到离开控件再次进入才会返回真
virtual void SetOrder(float order)=0
void SetSelect(bool open)
方向和对齐定义
组件实例
2d坐标系
键盘与手柄等按键操作
通用类型
Array< Direction, 2 > Direction2
Function< std::function< bool()>, str_uid("IsSelect")> IsSelect
选择
Function< std::function< void(float)>, str_uid("SetOrder")> SetOrder
设置order
Function< std::function< void()>, str_uid("RenderD")> RenderDebug
调试绘图
Function< std::function< dl::Coor *()>, str_uid("GetCoor")> GetCoor
返回坐标系
Function< std::function< bool()>, str_uid("IsPickup")> IsPickup
---------------------------—具体组件----------------------------------—
Function< std::function< bool()>, str_uid("IsDown")> IsDown
按下状态
Function< std::function< void(bool)>, str_uid("SetSelect")> SetSelect
设置是否选择
Function< std::function< void()>, str_uid("Render")> Render
绘制
Function< std::function< void()>, str_uid("DeleteThis")> DeleteThis
删除自身
Function< std::function< void()>, str_uid("Update")> Update
逻辑
Function< std::function< bool()>, str_uid("IsClick")> IsClick
点击
Function< std::function< Components *()>, str_uid("Clone")> Clone
克隆
Button::State ButtonState
Button::Mode ButtonMode
Position2 Position