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#include "render/dl_text.h"
24
25namespace dl::UI
26{
27
29class Button
30{
31public:
32 using CbCustomRender = std::function<void(Button*)>;
43
44 enum class Mode
45 {
49 };
50
51 struct TextData
52 {
54 Align::Direction2 _hv;// 在按钮的位置
57 bool _bSizeWithText; // text大小改变按钮的大小
58 Position _extraSize; // text大小改变按钮的大小(额外值)
59
61 _bSizeWithText{ false },
63 {
64 _hv = Align::CC;
65 for (auto& iter : _col)
66 iter = ColorDef::WHITE;
67 for (auto& iter : _offset)
68 iter = {};
69 }
70 };
71
73 _state{ State::NORMAL },
74 _statePre{ State::NORMAL },
75 _mode{ Mode::BUTTON },
76 _bOpen{ false },
77 _isClick{ false },
78 _bDisable{false},
79 _txtData{ nullptr },
80 _cpt{ this }
81 {
83 _init_cpt();
84 }
85
86 // 克隆使用,init_cpt不赋值值类的组件拷贝,需要手动处理
87 Button(const Button& b);
88
89
90 void Update();
91 void Render();
92
99 bool IsClick();
104 bool IsDown()
105 {
106 return _state == State::DOWN;
107 }
108
110 bool IsThrough();
111
112 bool IsRadio() { return _mode == Mode::RADIO; }
117 bool IsSelect() { return _bOpen; }
118
119 void SetSelect(bool open);
120
121 void SetMode(Mode mode);
122
123 void SetDisable(bool disable) { _bDisable = disable; }
124
134 void SetSound(std::string_view click, std::string_view over)
135 {
136 _strSndClick = std::string{ click };
137 _strSndOver = std::string{ over };
138 }
139
140 State GetState() { return _state; }
141
142 Coor* GetCoor() { return _coor; }
143
147 virtual Button* Clone() = 0;
148 virtual bool IsPickup() = 0;
149 virtual void SetOrder(float order) = 0;
150 virtual float GetOrder() = 0;
151 virtual Position GetSize() = 0;
152 virtual Position GetAnchor() = 0;
153
154 float GetOrder(int offset)
155 {
156 return Math::OffsetOrder(GetOrder(), 0, 0, offset);
157 }
158
159 void SetElementData(const Align::Direction2& hv, const Position& pos = {})
160 {
161 _cptElemData._hv = hv;
162 _cptElemData._pos = pos;
164 }
165 void SetElementDataRunAble(bool able)
166 {
167 _cptElemData._bRunAble = able;
168 }
169 void SetElementDataAutoSize(bool parent_w, bool parent_h)
170 {
171 _cptElemData._bParentW = parent_w;
172 _cptElemData._bParentH = parent_h;
173 }
174
175 std::string GetString()
176 {
177 if (!_txtData)
178 {
179 return {};
180 }
181 return _txtData->_txt->GetString();
182 }
183
190 void AddText(gcText txt,
191 const std::vector<Color>& mul_col,
192 const std::vector<Position>& mul_offset);
194 void AddText(gcText txt);
195 void AddText(gcText txt, const std::vector<Color>& mul_col);
196
197 void SetString(std::string_view str);
199 {
200 if (_txtData)
201 _txtData->_hv = hv;
202 }
203
204 void SetSizeWithText(bool with, const Position& extra_size);
205
206 //
207 void SetName(std::string_view name)
208 {
209 _cptName.Set(std::string{ name });
210 }
211
213 {
214 _cbRender = cb;
215 }
216
217 Components* GetCpt() { return &_cpt; }
218
219 Mode GetMode() { return _mode; }
220
221
222 // not use
224 {
225 return _clickControl;
226 }
227
228 virtual ~Button()
229 {
230 if (_clickControl == this)
231 _clickControl = nullptr;
232 delete _coor;
233 delete _txtData;
234 }
235private:
236 State _state;
237 State _statePre;
238 bool _bDisable;
239
240 Mode _mode;
241 bool _bOpen;
242 bool _isClick;
243 TextData* _txtData;
244
246 std::string _strSndClick;
247 std::string _strSndOver;
248protected:
250
252 std::size_t GetIndex(State state)
253 {
254 if (_bDisable)
255 {
256 if (state == State::NORMAL || state == State::OVER)
257 return 3;
258 else
259 return 4;
260 }
261 else
262 {
263 return (std::size_t)state;
264 }
265 }
266
267 //
269 {
270 _cptElemData._size = GetSize();
271 _cptElemData._anchor = GetAnchor();
272 }
273
274 constexpr bool _use_over()
275 {
276#if DL_PLATFORM == DL_PLATFORM_ANDROID
277 return false;
278#else
279 return true;
280#endif
281 }
282
287 virtual void UpdateChild(State state_pre, State state);
288 virtual void RenderChild(State state_pre, State state);
289 virtual void RenderDebug();
290
294 void UpdateTextOrder(float order)
295 {
296 if (_txtData)
297 _txtData->_txt->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
298 }
299 virtual void OnTextSizeChange(const Position& size) {};
300 TextData* GetTextData() { return _txtData; }
301private:
302 //
303 CbCustomRender _cbRender;
304 //--------------------------组件------------------------
305 Components _cpt;
306 Cpt::DeleteThis _cptDeleteThis;
307 Cpt::Update _cptUpdate;
308 Cpt::Render _cptRender;
309 Cpt::IsPickup _cptIsPickup;
310 Cpt::SetOrder _cptSetOrder;
311 Cpt::GetCoor _cptGetCoor;
312 Cpt::IsClick _cptIsClick;
313 Cpt::IsDown _cptIsDown;
314 Cpt::IsSelect _cptIsOpen;
315 Cpt::SetSelect _cptSetOpen;
316 Cpt::RenderDebug _cptRenderDebug;
317 Cpt::ElementData _cptElemData;
318 Cpt::Clone _cptClone;
319 Cpt::GetSize _cptGetSize;
320 Cpt::CustomName _cptName;
321 Cpt::CustomTag _cptTag;
322
323 void _delete_this() { delete this; }
324
325 Components* _clone()
326 {
327 return Clone()->GetCpt();
328 }
329
330 void _init_cpt()
331 {
332 _cptDeleteThis = std::bind(&Button::_delete_this, this);
333 _cptUpdate = std::bind(&Button::Update, this);
334 _cptRender = std::bind(&Button::Render, this);
335 _cptIsPickup = std::bind(&Button::IsPickup, this);
336 _cptSetOrder = std::bind(&Button::SetOrder, this, std::placeholders::_1);
337 _cptGetCoor = std::bind(&Button::GetCoor, this);
338 _cptIsClick = std::bind(&Button::IsClick, this);
339 _cptIsDown = std::bind(&Button::IsDown, this);
340 _cptIsOpen = std::bind(&Button::IsSelect, this);
341 _cptSetOpen = std::bind(&Button::SetSelect, this, std::placeholders::_1);
342 _cptRenderDebug = std::bind(&Button::RenderDebug, this);
343 _cptClone = std::bind(&Button::_clone, this);
344 _cptGetSize = std::bind(&Button::GetSize, this);
345
346 _cpt.PushBack(&_cptDeleteThis);
347 _cpt.PushBack(&_cptUpdate);
348 _cpt.PushBack(&_cptRender);
349 _cpt.PushBack(&_cptIsPickup);
350 _cpt.PushBack(&_cptSetOrder);
351 _cpt.PushBack(&_cptGetCoor);
352 _cpt.PushBack(&_cptIsClick);
353 _cpt.PushBack(&_cptIsDown);
354 _cpt.PushBack(&_cptIsOpen);
355 _cpt.PushBack(&_cptSetOpen);
356 _cpt.PushBack(&_cptRenderDebug);
357 _cpt.PushBack(&_cptClone);
358 _cpt.PushBack(&_cptGetSize);
359 _cpt.PushBack(&_cptName);
360 _cpt.PushBack(&_cptTag);
361
362 _cpt.PushBack(&_cptElemData);
363 }
364
365
366 static Button* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
367
368
369 void _update_button();
370 void _update_switch();
371 void _update_radio();
372};
373
376
377}
378
379#endif
枚举类数组
void PushBack(AnyType any)
添加一个任意对象
2d坐标系
static Coor * Create(Coor *parent=nullptr)
元素通用属性(默认左上对齐,便于理解)
void SetMode(Mode mode)
void AddText(gcText txt, const std::vector< Color > &mul_col)
constexpr bool _use_over()
bool IsDown()
控件按下状态
void SetDisable(bool disable)
void SetTextAlign(const Align::Direction2 &hv)
void SetElementDataAutoSize(bool parent_w, bool parent_h)
void SetElementDataRunAble(bool able)
virtual void UpdateChild(State state_pre, State state)
重载此函数,制定控件行为
virtual Button * Clone()=0
必须实现的函数
void UpdateTextOrder(float order)
order变化时需要调用
void AddText(gcText txt)
void SetElementData(const Align::Direction2 &hv, const Position &pos={})
bool IsClick()
控件被点击
virtual Position GetSize()=0
void SetSizeWithText(bool with, const Position &extra_size)
设置精灵大小跟随文本
void SetCustomRender(CbCustomRender cb)
float GetOrder(int offset)
virtual bool IsPickup()=0
void SetSound(std::string_view click, std::string_view over)
设置按钮音效
void AddText(gcText txt, const std::vector< Color > &mul_col, const std::vector< Position > &mul_offset)
添加文本属性
@ OVER
经过(仅有光标时使用)
@ DOWN_OVER
按下经过(Switch使用)
@ DOWN_DISABLE
按下禁用(Switch、Radio使用)
std::function< void(Button *)> CbCustomRender
virtual void RenderDebug()
bool IsSelect()
控件是选中状态
virtual float GetOrder()=0
Button(const Button &b)
virtual void RenderChild(State state_pre, State state)
virtual Position GetAnchor()=0
void SetString(std::string_view str)
@ SWITCH
开关,按下后不会自动弹起,再次点击后弹起
@ RADIO
收音机按钮,按下后就不能弹起,只能通过代码弹起
void ClearState()
清理状态为普通
static Button * GetClickControl()
void AddTextDefault()
std::size_t GetIndex(State state)
返回索引
bool IsThrough()
经过按钮,直到离开控件再次进入才会返回真
virtual void SetOrder(float order)=0
void SetSelect(bool open)
void SetName(std::string_view name)
virtual void OnTextSizeChange(const Position &size)
方向和对齐定义
组件实例
2d坐标系
键盘与手柄等按键操作
文本绘制
通用类型
Array< Direction, 2 > Direction2
constexpr Direction2 CC
constexpr Color WHITE
Variable< std::string, str_uid("CustomName")> CustomName
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
---------------------------—具体组件----------------------------------—
Variable< int, str_uid("CustomTag")> CustomTag
Function< std::function< bool()>, str_uid("IsDown")> IsDown
按下状态
Function< std::function< Position(void)>, str_uid("GetSize")> GetSize
获取大小
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
克隆
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Button::State ButtonState
Button::Mode ButtonMode
Position2 Position
GC< Text > gcText
ArrayEnum< Position, Button::State::MAX_NUM > _offset
ArrayEnum< Color, Button::State::MAX_NUM > _col