dl
dl_ui_msg_box.h
浏览该文件的文档.
1
12#pragma once
13
14#ifndef DL_DISABLE_RENDER
16#include "system/dl_system.h"
17
18namespace dl::UI
19{
21constexpr size_t NUM_MSG_BOX_BTN = 10;
22
24class MsgBox
25{
26public:
38 static MsgBox* Create(
39 Background* bg,
40 gcText txt_title,
41 gcText txt,
43 UI::ButtonSprite* btn_close,
44 Float dx_btn = 0.0)
45 {
46 MsgBox* ret = new MsgBox;
47 Coor* coor = Coor::Create();
48 coor->SetPosition(toPosition(g_system->GetWindowSize() / 2u));
49 bg->SetUI(true);
50
51 ret->_coor = coor;
52 ret->_bg = bg;
53 ret->_txtTitle = txt_title;
54 ret->_txt = txt;
55 ret->_btn = btn;
56 ret->_btnClose = btn_close;
57 ret->_dxBtn = dx_btn;
58
59 ret->_refresh_coor();
61 return ret;
62 }
63
64 MsgBox* Clone()
65 {
66 MsgBox* ret = new MsgBox(*this);
67 ret->_coor = _coor->Clone();
68 ret->_bg = _bg->Clone();
69 ret->_btn = _btn->Clone();
70 ret->_btnClose = _btnClose->Clone();
71
72 for (size_t i = 0; i < _allBtn.size(); ++i)
73 {
74 ret->_allBtn[i] = _allBtn[i]->Clone();
75 }
76 ret->_refresh_coor();
77 return ret;
78 }
79
87 void Set(std::string_view title, std::string_view info, std::vector<std::string_view> all_btn)
88 {
89 _txtTitle->SetString(title);
90 if(_bAddTab)
91 _txt->SetString(std::string{ "\t" } + std::string{ info });
92 else
93 _txt->SetString(info);
94
95 //少了就新建按钮
96 _numBtn = all_btn.size();
97 if (_numBtn > _allBtn.size())
98 {
99 _allBtn.resize(_numBtn);
100 for (UI::ButtonSprite*& btn : _allBtn)
101 {
102 if (!btn)
103 {
104 btn = _btn->Clone();
105 }
106 }
107 }
108
109 for (std::size_t i = 0; i < _numBtn; ++i)
110 {
111 _allBtn[i]->SetString(all_btn[i]);
112 }
113
114 //--------------------------------更新布局--------------------------------
115 Float w_btn = _btn->GetSize()[0]; //按钮宽度
116 Float h_btn = _btn->GetSize()[1];
117 //整体宽度为 按钮宽度 + 间隔(多一个)
118 Float w = w_btn * _numBtn + _dxBtn * (1 + _numBtn);
119
120 _txt->SetMaxW((unsigned)w);//设置文本宽度
121
122 Float h_txt = _txt->GetSize()[1]; //文本高度
123 Float h_title = _txtTitle->GetSize()[1];//标题高度
124 //整体高度 标题 + 文本 + 按钮
125 Float h = h_title + h_txt * 1 + h_btn * 1;
126 _txt->SetCenter();
127 Position size{ w, h };
128 Position size_half{ size * 0.5f };
129 _bg->SetSize(size);
130
131 //设置位置
132 _bg->GetCoor()->SetPosition(toPosition(_bg->GetOffset()) - size_half);
133 _txtTitle->SetPositionAlign(-size_half, Align::LC);
134 _btnClose->GetCoor()->SetPosition({ size_half[0], -size_half[1] });
135
136 if (_numBtn > 0)
137 {
138 //偏移为 负的 按钮总宽度的一半(间隔少一个) + 半个按钮
139 Float sx_btn = (w_btn * _numBtn + _dxBtn * (_numBtn - 1)) * -0.5_f + w_btn * 0.5_f;
140 for (std::size_t i = 0; i < _numBtn; ++i)
141 {
142 _allBtn[i]->GetCoor()->SetPosition({ sx_btn + i * (w_btn + _dxBtn), size_half[1] });
143 }
144 }
145
146 }
147
151 void Set(std::string_view str)
152 {
153 std::string_view str_title = lang("msg_box", std::string{ str } + "_title");
154 std::string_view str_info = lang("msg_box", std::string{ str } + "_info");
155 std::vector<std::string_view> all_btn;
156 for (std::size_t i = 0; i < NUM_MSG_BOX_BTN; ++i)
157 {
158 std::string str_name = fmt::format("{}_{}", str, i);
159 auto str = g_lang->GetCheck("msg_box", str_name);
160 if (!str.has_value())
161 {
162 break;
163 }
164 all_btn.push_back(str.value());
165 }
166
167 Set(str_title, str_info, all_btn);
168 }
169
170 void Update()
171 {
172 _btnClose->Update();
173 for (size_t i = 0; i < _numBtn; ++i)
174 {
175 _allBtn[i]->Update();
176 }
177
178 if (_posPick)
179 {
180 if (g_input->KeyUp(MouseCode::LEFT))
181 _posPick.reset();
182 }
183 else
184 {
185 if (g_input->KeyDown(MouseCode::LEFT) && IsPickupTitle())
186 {
187 _posPick = _coor->WorldToThis(toPosition(g_input->GetCursorPosition()));
188 }
189 }
190
191 //左键拖动
192 if (_posPick)
193 {
194 Point pos = g_input->GetCursorPosition();
195 _coor->SetPosition(toPosition(pos) - _posPick.value());
196 }
197 }
198
199 void Render()
200 {
201 _bg->Render();
202 _txtTitle->Render();
203 _txt->Render();
204 }
205
208 size_t GetClick()
209 {
210 for (size_t i = 0; i < _numBtn; ++i)
211 {
212 if (_allBtn[i]->IsClick())
213 return i;
214 }
215 return -1;
216 }
217
221 {
222 return _btnClose->IsClick();
223 }
224
228 {
229 if (_bg->IsPickup())
230 {//按钮会屏蔽bg的IsPickup
231 Position pos = _txtTitle->GetCoor()->WorldToThis(toPosition(g_input->GetCursorPosition()));
232 if (pos[1] < _txtTitle->GetSize()[1])
233 return true;
234 }
235 return false;
236 }
237
238 void SetOrder(float order)
239 {
240 _txt->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
241 _txtTitle->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
242 _btnClose->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
243 _btn->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
244 for (auto& iter : _allBtn)
245 {
246 iter->SetOrder(Math::OffsetOrder(order, 0, 0, -1));
247 }
248 _bg->SetOrder(Math::OffsetOrder(order, 0, 0, 0));
249 }
250
252 {
253 delete _coor;
254 delete _bg;
255 delete _btn;
256 delete _btnClose;
257 for (auto& iter : _allBtn)
258 delete iter;
259 }
260private:
261 MsgBox():
262 _coor{nullptr},
263 _bg{nullptr},
264 _btn{nullptr},
265 _btnClose{nullptr},
266 _numBtn{0},
267 _bAddTab{true}
268 {}
269 Coor* _coor;
270
271 Background* _bg;
272 gcText _txtTitle;
273 gcText _txt;
274 UI::ButtonSprite* _btn;
275 UI::ButtonSprite* _btnClose;
276
277 //动态改变长度,多余的不会自动删除,所以需要标记使用数量
278 std::vector<UI::ButtonSprite*> _allBtn;
279 std::size_t _numBtn;
280
281 Float _dxBtn;
282
283 std::optional<Position> _posPick;//被拾取位置(差值,相加即可)(直到再抬起)
284 bool _bAddTab;//添加段首缩进
285
286 void _refresh_coor()
287 {
288 _bg->GetCoor()->SetParent(_coor);
289 _txtTitle->GetCoor()->SetParent(_coor);
290 _txt->GetCoor()->SetParent(_coor);
291 _btn->GetCoor()->SetParent(_coor);
292 _btnClose->GetCoor()->SetParent(_coor);
293
294 for (size_t i = 0; i < _allBtn.size(); ++i)
295 {
296 _allBtn[i]->GetCoor()->SetParent(_coor);
297 }
298 }
299};
300}
301
302#endif
2d坐标系
Coor * Clone()
void SetParent(Coor *coor)
static Coor * Create(Coor *parent=nullptr)
void SetPosition(const Position &pos)
不提供其他类型重载,避免{}无法判断类型
Coor * GetCoor()
返回坐标系
Background * Clone()
void SetUI(bool ui)
精灵按钮(注意精灵没有Coor)
virtual ButtonSprite * Clone() override
void Set(std::string_view title, std::string_view info, std::vector< std::string_view > all_btn)
设置内容
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)
构造
bool IsPickupTitle()
是否选中标题栏
窗口主系统
constexpr Direction2 LC
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
constexpr size_t NUM_MSG_BOX_BTN
消息框最大按钮数量
Position2 Position
constexpr Array< Position::value_type, N > toPosition(const Array< T, N > &a)
转换到Position,已知长度
Input * g_input
float Float
constexpr float Z_DEFAULT_UI
std::string_view lang(std::string_view tag, std::string_view name)
返回一个语言配置的字符串,传入 menu、start
Language * g_lang
GC< Text > gcText
System * g_system
Point2 Point