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