dl
dl_ui_msg_box.h
浏览该文件的文档.
1
12#pragma once
13
15#include "dl_system.h"
16
17namespace dl::UI
18{
20constexpr size_t NUM_MSG_BOX_BTN = 10;
21
22class MsgBox
23{
24public:
36 static MsgBox* Create(
37 Background* bg,
38 gcText txt_title,
39 gcText txt,
41 UI::ButtonSprite* btn_close,
42 Float dx_btn = 0.0)
43 {
44 MsgBox* ret = new MsgBox;
45 Coor* coor = Coor::Create();
47 bg->SetUI(true);
48
49 ret->_coor = coor;
50 ret->_bg = bg;
51 ret->_txtTitle = txt_title;
52 ret->_txt = txt;
53 ret->_btn = btn;
54 ret->_btnClose = btn_close;
55 ret->_dxBtn = dx_btn;
56
57 ret->_refresh_coor();
59 return ret;
60 }
61
63 {
64 MsgBox* ret = new MsgBox(*this);
65 ret->_coor = _coor->Clone();
66 ret->_bg = _bg->Clone();
67 ret->_btn = _btn->Clone();
68 ret->_btnClose = _btnClose->Clone();
69
70 for (size_t i = 0; i < _allBtn.size(); ++i)
71 {
72 ret->_allBtn[i] = _allBtn[i]->Clone();
73 }
74 ret->_refresh_coor();
75 return ret;
76 }
77
85 void Set(std::string_view title, std::string_view info, std::vector<std::string_view> all_btn)
86 {
87 _txtTitle->SetString(title);
88 if(_bAddTab)
89 _txt->SetString(std::string{ "\t" } + std::string{ info });
90 else
91 _txt->SetString(info);
92
93 //少了就新建按钮
94 _numBtn = all_btn.size();
95 if (_numBtn > _allBtn.size())
96 {
97 _allBtn.resize(_numBtn);
98 for (UI::ButtonSprite*& btn : _allBtn)
99 {
100 if (!btn)
101 {
102 btn = _btn->Clone();
103 }
104 }
105 }
106
107 for (std::size_t i = 0; i < _numBtn; ++i)
108 {
109 _allBtn[i]->SetString(all_btn[i]);
110 }
111
112 //--------------------------------更新布局--------------------------------
113 Float w_btn = _btn->GetSize()[0]; //按钮宽度
114 Float h_btn = _btn->GetSize()[1];
115 //整体宽度为 按钮宽度 + 间隔(多一个)
116 Float w = w_btn * _numBtn + _dxBtn * (1 + _numBtn);
117
118 _txt->SetMaxW(w);//设置文本宽度
119
120 Float h_txt = _txt->GetSize()[1]; //文本高度
121 Float h_title = _txtTitle->GetSize()[1];//标题高度
122 //整体高度 标题 + 文本 + 按钮
123 Float h = h_title + h_txt * 1 + h_btn * 1;
124 _txt->SetCenter();
125 Position size{ w, h };
126 Position size_half{ size * 0.5f };
127 _bg->SetSize(Math::CastU(toPoint(size) + _bg->GetExtra()));
128
129 //设置位置
130 _bg->GetCoor()->SetPosition(toPosition(_bg->GetOffset()) - size_half);
131 _txtTitle->SetPositionAlign(-size_half, Align::LC);
132 _btnClose->GetCoor()->SetPosition({ size_half[0], -size_half[1] });
133
134 if (_numBtn > 0)
135 {
136 //偏移为 负的 按钮总宽度的一半(间隔少一个) + 半个按钮
137 Float sx_btn = (w_btn * _numBtn + _dxBtn * (_numBtn - 1)) * -0.5 + w_btn * 0.5;
138 for (std::size_t i = 0; i < _numBtn; ++i)
139 {
140 _allBtn[i]->GetCoor()->SetPosition({ sx_btn + i * (w_btn + _dxBtn), size_half[1] });
141 }
142 }
143
144 }
149 void Set(std::string_view str)
150 {
151 std::string_view str_title = lang("msg_box", std::string{ str } + "_title");
152 std::string_view str_info = lang("msg_box", std::string{ str } + "_info");
153 std::vector<std::string_view> all_btn;
154 for (std::size_t i = 0; i < NUM_MSG_BOX_BTN; ++i)
155 {
156 std::string str_name = format("{}_{}", str, i);
157 auto str = g_lang->GetCheck("msg_box", str_name);
158 if (!str.has_value())
159 {
160 break;
161 }
162 all_btn.push_back(str.value());
163 }
164
165 Set(str_title, str_info, all_btn);
166 }
167
168 void Update()
169 {
170 _btnClose->Update();
171 for (size_t i = 0; i < _numBtn; ++i)
172 {
173 _allBtn[i]->Update();
174 }
175
176 if (_posPick)
177 {
179 _posPick.reset();
180 }
181 else
182 {
184 {
185 _posPick = _coor->WorldToThis(toPosition(g_input->GetCursorPosition()));
186 }
187 }
188
189 //左键拖动
190 if (_posPick)
191 {
193 _coor->SetPosition(toPosition(pos) - _posPick.value());
194 }
195 }
196
197 void Render()
198 {
199 _bg->Render();
200 _txtTitle->Render();
201 _txt->Render();
202 }
206 size_t GetClick()
207 {
208 for (size_t i = 0; i < _numBtn; ++i)
209 {
210 if (_allBtn[i]->IsClick())
211 return i;
212 }
213 return -1;
214 }
219 {
220 return _btnClose->IsClick();
221 }
226 {
227 if (_bg->IsPickup())
228 {//按钮会屏蔽bg的IsPickup
230 if (pos[1] < _txtTitle->GetSize()[1])
231 return true;
232 }
233 return false;
234 }
235
236 void SetOrder(float order)
237 {
238 _txt->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
239 _txtTitle->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
240 _btnClose->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
241 _btn->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
242 for (auto& iter : _allBtn)
243 {
244 iter->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
245 }
246 _bg->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
247 }
248
250 {
251 delete _coor;
252 delete _bg;
253 delete _btn;
254 delete _btnClose;
255 for (auto& iter : _allBtn)
256 delete iter;
257 }
258private:
259 MsgBox():
260 _coor{nullptr},
261 _bg{nullptr},
262 _btn{nullptr},
263 _btnClose{nullptr},
264 _numBtn{0},
265 _bAddTab{true}
266 {}
267 Coor* _coor;
268
269 Background* _bg;
270 gcText _txtTitle;
271 gcText _txt;
272 UI::ButtonSprite* _btn;
273 UI::ButtonSprite* _btnClose;
274
275 //动态改变长度,多余的不会自动删除,所以需要标记使用数量
276 std::vector<UI::ButtonSprite*> _allBtn;
277 std::size_t _numBtn;
278
279 Float _dxBtn;
280
281 std::optional<Position> _posPick;//被拾取位置(差值,相加即可)(直到再抬起)
282 bool _bAddTab;//添加段首缩进
283
284 void _refresh_coor()
285 {
286 _bg->GetCoor()->SetParent(_coor);
287 _txtTitle->GetCoor()->SetParent(_coor);
288 _txt->GetCoor()->SetParent(_coor);
289 _btn->GetCoor()->SetParent(_coor);
290 _btnClose->GetCoor()->SetParent(_coor);
291
292 for (size_t i = 0; i < _allBtn.size(); ++i)
293 {
294 _allBtn[i]->GetCoor()->SetParent(_coor);
295 }
296 }
297};
298}
绘图背景
void SetUI(bool ui)
Background * Clone()
void SetOrder(float order)
void SetSize(const Size &size)
2d坐标系
Definition: dl_coor.h:27
Coor * Clone()
void SetParent(Coor *coor)
Position WorldToThis(Position pos) const
将点 从 世界坐标系 转到 自身坐标系
static Coor * Create(Coor *parent=nullptr)
void SetPosition(const Position &pos)
bool KeyUp(KeyCode key_code)
检测一个按键按下并释放,只1帧有效。
Definition: dl_input.h:242
bool KeyDown(KeyCode key_code)
检测一个按键按下了,松开后再按下才会再次触发,只1帧有效。
Definition: dl_input.h:249
Point GetCursorPosition()
返回鼠标位置。限制到窗口范围
std::optional< std::string_view > GetCheck(std::string_view tag, std::string_view name)
失败返回false
Definition: dl_language.h:46
Size GetWindowSize()
返回窗口大小
void SetMaxW(unsigned w)
设置自动换行宽度,0为不自动换行
Definition: dl_text.h:151
void Render()
绘制
Size GetSize()
返回区域大小
Definition: dl_text.h:253
void SetPositionAlign(const Position &pos={}, const Align::Direction2 &dir={ Align::Direction::BACK, Align::Direction::BACK })
设置Coor位置,来整体对齐
Definition: dl_text.h:129
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:238
void SetOrder(float order)
设置绘制顺序
void SetCenter(const Position &pos={})
设置Coor位置,并根据整体大小居中
void SetString(std::string_view str)
设置内容
bool IsClick()
控件被点击
精灵按钮(注意精灵没有Coor)
virtual Position GetSize() override
virtual void SetOrder(float order) override
ButtonSprite * Clone()
void Set(std::string_view title, std::string_view info, std::vector< std::string_view > all_btn)
设置内容
Definition: dl_ui_msg_box.h:85
void SetOrder(float order)
void Set(std::string_view str)
设置内容,以语言文件配置的名称
bool IsClickClose()
是否点击了关闭按钮
size_t GetClick()
返回点击按钮索引,返回-1代表没有点击
static MsgBox * Create(Background *bg, gcText txt_title, gcText txt, UI::ButtonSprite *btn, UI::ButtonSprite *btn_close, Float dx_btn=0.0)
构造
Definition: dl_ui_msg_box.h:36
MsgBox * Clone()
Definition: dl_ui_msg_box.h:62
bool IsPickupTitle()
是否选中标题栏
引擎主系统
constexpr Direction2 LC
Definition: dl_align.h:33
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
Definition: dl_math.h:525
std::array< unsigned, N > CastU(const std::array< T, N > &a, bool show_log=true)
有符号转无符号,并且判断负数
Definition: dl_math.h:89
constexpr size_t NUM_MSG_BOX_BTN
消息框最大按钮数量
Definition: dl_ui_msg_box.h:20
Input * g_input
float Float
Definition: dl_type_def.h:37
constexpr float Z_DEFAULT_UI
Definition: dl_sprite.h:26
std::string_view lang(std::string_view tag, std::string_view name)
返回一个语言配置的字符串,传入 menu、start
Definition: dl_language.h:119
Language * g_lang
constexpr std::array< Point::value_type, N > toPoint(const std::array< T, N > &a)
System * g_system
Point2 Point
Definition: dl_type_def.h:23
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)
Position2 Position
Definition: dl_type_def.h:43