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 "dl_align.h"
21#include "dl_component.h"
22
23namespace dl::UI
24{
25
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 //克隆使用
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<ButtonBase&>(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 IsOpen() { return _bOpen; }
88
89 void SetOpen(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 bool IsPickup() = 0;
113 virtual void SetOrder(float order) = 0;
114 virtual Position GetSize() = 0;
115 virtual Position GetAnchor() = 0;
116
118 {
119 _cptElemData._size = GetSize();
120 _cptElemData._anchor = GetAnchor();
121 _cptElemData._hv = hv;
122 }
123
124 Components* GetCpt() { return &_cpt; }
125
126 Mode GetMode() { return _mode; }
127
128 virtual ~ButtonBase()
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 if (state == State::DOWN)
155 return 4;
156 }
157 else
158 {
159 return (std::size_t)state;
160 }
161 }
162
163
164private:
165 //--------------------------组件------------------------
166 Components _cpt;
167 Cpt::DeleteThis _cptDeleteThis;
168 Cpt::Update _cptUpdate;
169 Cpt::Render _cptRender;
170 Cpt::IsPickup _cptIsPickup;
171 Cpt::SetOrder _cptSetOrder;
172 Cpt::GetCoor _cptGetCoor;
173 Cpt::IsClick _cptIsClick;
174 Cpt::ElementData _cptElemData;
175
176 void _delete_this() { delete this; }
177
178 void _init_cpt()
179 {
180 _cptDeleteThis = std::bind(&ButtonBase::_delete_this, this);
181 _cptUpdate = std::bind(&ButtonBase::Update, this);
182 _cptRender = std::bind(&ButtonBase::Render, this);
183 _cptIsPickup = std::bind(&ButtonBase::IsPickup, this);
184 _cptSetOrder = std::bind(&ButtonBase::SetOrder, this, std::placeholders::_1);
185 _cptGetCoor = std::bind(&ButtonBase::GetCoor, this);
186 _cptIsClick = std::bind(&ButtonBase::IsClick, this);
187
188 _cpt.PushBack(&_cptDeleteThis);
189 _cpt.PushBack(&_cptUpdate);
190 _cpt.PushBack(&_cptRender);
191 _cpt.PushBack(&_cptIsPickup);
192 _cpt.PushBack(&_cptSetOrder);
193 _cpt.PushBack(&_cptGetCoor);
194 _cpt.PushBack(&_cptIsClick);
195
196 _cpt.PushBack(&_cptElemData);
197 }
198
199
200 static ButtonBase* _clickControl;//防止在按钮外面按下左键移动到按钮内也能触发Click
201
205 virtual void UpdateChild(State state_pre, State state) = 0;
206 virtual void RenderChild(State state_pre, State state) = 0;
207
208 void _update_button();
209 void _update_switch();
210 void _update_radio();
211};
212
213}
组件容器还需表示对象自己(比如UI::Text)
Definition: dl_component.h:191
void PushBack(AnyType any)
添加一个任意对象
Definition: dl_component.h:216
2d坐标系
Definition: dl_coor.h:27
static Coor * Create(Coor *parent=nullptr)
元素通用属性(默认左中对齐,具体看类型)
Definition: dl_component.h:273
Align::Direction2 _hv
Definition: dl_component.h:288
按钮(Button、Switch、Radio)
void SetOpen(bool open)
void SetMode(Mode mode)
void SetSound(std::string_view click, std::string_view over)
设置按钮音效
bool IsClick()
控件被点击
virtual Position GetAnchor()=0
Components * GetCpt()
void SetDisable(bool disable)
void SetElementData(const Align::Direction2 &hv)
@ SWITCH
开关,按下后不会自动弹起,再次点击后弹起
@ RADIO
收音机按钮,按下后就不能弹起,只能通过代码弹起
ButtonBase(const ButtonBase &b)
virtual bool IsPickup()=0
virtual void SetOrder(float order)=0
std::size_t GetIndex(State state)
返回索引
bool IsThrough()
经过按钮,直到离开控件再次进入才会返回真
virtual Position GetSize()=0
方向和对齐定义
组件系统
2d坐标系
键盘与手柄等按键操作
std::array< Direction, 2 > Direction2
Definition: dl_align.h:31
Position2 Position
Definition: dl_type_def.h:43