forked from duilib/duilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlEx.h
459 lines (413 loc) · 15.8 KB
/
ControlEx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#ifndef __CONTROLEX_H__
#define __CONTROLEX_H__
#include <vector>
#include <math.h>
inline double CalculateDelay(double state) {
return pow(state, 2);
}
// category(0)->game(1)->server(2)->room(3)
class GameListUI : public CListUI
{
public:
enum { SCROLL_TIMERID = 10 };
struct NodeData
{
int _level;
bool _expand;
CDuiString _text;
CListLabelElementUI* _pListElement;
};
class Node
{
typedef std::vector <Node*> Children;
Children _children;
Node* _parent;
NodeData _data;
private:
void set_parent(Node* parent) { _parent = parent; }
public:
Node() : _parent (NULL) {}
explicit Node(NodeData t) : _data (t), _parent (NULL) {}
Node(NodeData t, Node* parent) : _data (t), _parent (parent) {}
~Node()
{
for (int i = 0; i < num_children(); i++)
delete _children[i];
}
NodeData& data() { return _data; }
int num_children() const { return _children.size(); }
Node* child(int i) { return _children[i]; }
Node* parent() { return ( _parent); }
bool has_children() const { return num_children() > 0; }
void add_child(Node* child)
{
child->set_parent(this);
_children.push_back(child);
}
void remove_child(Node* child)
{
Children::iterator iter = _children.begin();
for( ; iter < _children.end(); ++iter )
{
if( *iter == child )
{
_children.erase(iter);
return;
}
}
}
Node* get_last_child()
{
if( has_children() )
{
return child(num_children() - 1)->get_last_child();
}
else return this;
}
};
GameListUI() : _root(NULL), m_dwDelayDeltaY(0), m_dwDelayNum(0), m_dwDelayLeft(0)
{
SetItemShowHtml(true);
_root = new Node;
_root->data()._level = -1;
_root->data()._expand = true;
_root->data()._pListElement = NULL;
}
~GameListUI() { if(_root) delete _root; }
bool Add(CControlUI* pControl)
{
if( !pControl ) return false;
if( _tcscmp(pControl->GetClass(), _T("ListLabelElementUI")) != 0 ) return false;
return CListUI::Add(pControl);
}
bool AddAt(CControlUI* pControl, int iIndex)
{
if( !pControl ) return false;
if( _tcscmp(pControl->GetClass(), _T("ListLabelElementUI")) != 0 ) return false;
return CListUI::AddAt(pControl, iIndex);
}
bool Remove(CControlUI* pControl)
{
if( !pControl ) return false;
if( _tcscmp(pControl->GetClass(), _T("ListLabelElementUI")) != 0 ) return false;
if (reinterpret_cast<Node*>(static_cast<CListLabelElementUI*>(pControl->GetInterface(_T("ListLabelElement")))->GetTag()) == NULL)
return CListUI::Remove(pControl);
else
return RemoveNode(reinterpret_cast<Node*>(static_cast<CListLabelElementUI*>(pControl->GetInterface(_T("ListLabelElement")))->GetTag()));
}
bool RemoveAt(int iIndex)
{
CControlUI* pControl = GetItemAt(iIndex);
if( !pControl ) return false;
if( _tcscmp(pControl->GetClass(), _T("ListLabelElementUI")) != 0 ) return false;
if (reinterpret_cast<Node*>(static_cast<CListLabelElementUI*>(pControl->GetInterface(_T("ListLabelElement")))->GetTag()) == NULL)
return CListUI::RemoveAt(iIndex);
else
return RemoveNode(reinterpret_cast<Node*>(static_cast<CListLabelElementUI*>(pControl->GetInterface(_T("ListLabelElement")))->GetTag()));
}
void RemoveAll()
{
CListUI::RemoveAll();
for (int i = 0; i < _root->num_children(); ++i)
{
Node* child = _root->child(i);
RemoveNode(child);
}
delete _root;
_root = new Node;
_root->data()._level = -1;
_root->data()._expand = true;
_root->data()._pListElement = NULL;
}
void SetVisible(bool bVisible = true)
{
if( m_bVisible == bVisible ) return;
CControlUI::SetVisible(bVisible);
}
void SetInternVisible(bool bVisible = true)
{
CControlUI::SetInternVisible(bVisible);
}
void DoEvent(TEventUI& event)
{
if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
if( m_pParent != NULL ) m_pParent->DoEvent(event);
else CVerticalLayoutUI::DoEvent(event);
return;
}
if( event.Type == UIEVENT_TIMER && event.wParam == SCROLL_TIMERID ) {
if( m_dwDelayLeft > 0 ) {
--m_dwDelayLeft;
SIZE sz = GetScrollPos();
LONG lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
if( (lDeltaY > 0 && sz.cy != 0) || (lDeltaY < 0 && sz.cy != GetScrollRange().cy ) ) {
sz.cy -= lDeltaY;
SetScrollPos(sz);
return;
}
}
m_dwDelayDeltaY = 0;
m_dwDelayNum = 0;
m_dwDelayLeft = 0;
m_pManager->KillTimer(this, SCROLL_TIMERID);
return;
}
if( event.Type == UIEVENT_SCROLLWHEEL ) {
LONG lDeltaY = 0;
if( m_dwDelayNum > 0 ) lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
switch( LOWORD(event.wParam) ) {
case SB_LINEUP:
if( m_dwDelayDeltaY >= 0 ) m_dwDelayDeltaY = lDeltaY + 8;
else m_dwDelayDeltaY = lDeltaY + 12;
break;
case SB_LINEDOWN:
if( m_dwDelayDeltaY <= 0 ) m_dwDelayDeltaY = lDeltaY - 8;
else m_dwDelayDeltaY = lDeltaY - 12;
break;
}
if( m_dwDelayDeltaY > 100 ) m_dwDelayDeltaY = 100;
else if( m_dwDelayDeltaY < -100 ) m_dwDelayDeltaY = -100;
m_dwDelayNum = (DWORD)sqrt((double)abs(m_dwDelayDeltaY)) * 5;
m_dwDelayLeft = m_dwDelayNum;
m_pManager->SetTimer(this, SCROLL_TIMERID, 50U);
return;
}
CListUI::DoEvent(event);
}
Node* GetRoot() { return _root; }
Node* AddNode(LPCTSTR text, Node* parent = NULL)
{
if( !parent ) parent = _root;
CListLabelElementUI* pListElement = new CListLabelElementUI;
Node* node = new Node;
node->data()._level = parent->data()._level + 1;
if( node->data()._level == 0 ) node->data()._expand = true;
else node->data()._expand = false;
node->data()._text = text;
node->data()._pListElement = pListElement;
if( parent != _root ) {
if( !(parent->data()._expand && parent->data()._pListElement->IsVisible()) )
pListElement->SetInternVisible(false);
}
CDuiString html_text;
html_text += _T("<x 6>");
for( int i = 0; i < node->data()._level; ++i ) {
html_text += _T("<x 24>");
}
if( node->data()._level < 3 ) {
if( node->data()._expand ) html_text += _T("<a><i tree_expand.png 2 1></a>");
else html_text += _T("<a><i tree_expand.png 2 0></a>");
}
html_text += node->data()._text;
pListElement->SetText(html_text);
//if( node->data()._level == 0 ) pListElement->SetFixedHeight(28);
//else pListElement->SetFixedHeight(24);
pListElement->SetTag((UINT_PTR)node);
if( node->data()._level == 0 ) {
pListElement->SetBkImage(_T("file='tree_top.png' corner='2,1,2,1' fade='100'"));
}
int index = 0;
if( parent->has_children() ) {
Node* prev = parent->get_last_child();
index = prev->data()._pListElement->GetIndex() + 1;
}
else {
if( parent == _root ) index = 0;
else index = parent->data()._pListElement->GetIndex() + 1;
}
if( !CListUI::AddAt(pListElement, index) ) {
delete pListElement;
delete node;
node = NULL;
}
parent->add_child(node);
return node;
}
bool RemoveNode(Node* node)
{
if( !node || node == _root ) return false;
for( int i = 0; i < node->num_children(); ++i ) {
Node* child = node->child(i);
RemoveNode(child);
}
CListUI::Remove(node->data()._pListElement);
node->parent()->remove_child(node);
delete node;
return true;
}
void ExpandNode(Node* node, bool expand)
{
if( !node || node == _root ) return;
if( node->data()._expand == expand ) return;
node->data()._expand = expand;
CDuiString html_text;
html_text += _T("<x 6>");
for( int i = 0; i < node->data()._level; ++i ) {
html_text += _T("<x 24>");
}
if( node->data()._level < 3 ) {
if( node->data()._expand ) html_text += _T("<a><i tree_expand.png 2 1></a>");
else html_text += _T("<a><i tree_expand.png 2 0></a>");
}
html_text += node->data()._text;
node->data()._pListElement->SetText(html_text);
if( !node->data()._pListElement->IsVisible() ) return;
if( !node->has_children() ) return;
Node* begin = node->child(0);
Node* end = node->get_last_child();
for( int i = begin->data()._pListElement->GetIndex(); i <= end->data()._pListElement->GetIndex(); ++i ) {
CControlUI* control = GetItemAt(i);
if( _tcscmp(control->GetClass(), _T("ListLabelElementUI")) == 0 ) {
Node* local_parent = ((GameListUI::Node*)control->GetTag())->parent();
control->SetInternVisible(local_parent->data()._expand && local_parent->data()._pListElement->IsVisible());
}
}
NeedUpdate();
}
SIZE GetExpanderSizeX(Node* node) const
{
if( !node || node == _root ) return CDuiSize();
if( node->data()._level >= 3 ) return CDuiSize();
SIZE szExpander = {0};
szExpander.cx = 6 + 24 * node->data()._level - 4/*适当放大一点*/;
szExpander.cy = szExpander.cx + 16 + 8/*适当放大一点*/;
return szExpander;
}
private:
Node* _root;
LONG m_dwDelayDeltaY;
DWORD m_dwDelayNum;
DWORD m_dwDelayLeft;
};
class DeskListUI : public CTileLayoutUI
{
public:
enum { SCROLL_TIMERID = 10 };
DeskListUI() : m_uButtonState(0), m_dwDelayDeltaY(0), m_dwDelayNum(0), m_dwDelayLeft(0)
{
SetItemSize(CDuiSize(182, 152));
CDialogBuilder builder;
CContainerUI* pDesk = static_cast<CContainerUI*>(builder.Create(_T("desk.xml"), (UINT)0));
if( pDesk != NULL ) {
for(int i = 0; i < 500; ++i)
{
if( pDesk == NULL ) pDesk = static_cast<CContainerUI*>(builder.Create());
if( pDesk != NULL ) {
this->Add(pDesk);
TCHAR indexBuffer[16];
CDuiString strIndexString = _T("- ");
strIndexString += _itot(i+1, indexBuffer, 10);
strIndexString += _T(" -");
pDesk->GetItemAt(3)->SetText(strIndexString);
pDesk = NULL;
}
else {
this->RemoveAll();
return;
}
}
}
}
void DoEvent(TEventUI& event)
{
if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
if( m_pParent != NULL ) m_pParent->DoEvent(event);
else CTileLayoutUI::DoEvent(event);
return;
}
if( event.Type == UIEVENT_TIMER && event.wParam == SCROLL_TIMERID )
{
if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
POINT pt = m_pManager->GetMousePos();
LONG cy = (pt.y - m_ptLastMouse.y);
m_ptLastMouse = pt;
SIZE sz = GetScrollPos();
sz.cy -= cy;
SetScrollPos(sz);
return;
}
else if( m_dwDelayLeft > 0 ) {
--m_dwDelayLeft;
SIZE sz = GetScrollPos();
LONG lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
if( (lDeltaY > 0 && sz.cy != 0) || (lDeltaY < 0 && sz.cy != GetScrollRange().cy ) ) {
sz.cy -= lDeltaY;
SetScrollPos(sz);
return;
}
}
m_dwDelayDeltaY = 0;
m_dwDelayNum = 0;
m_dwDelayLeft = 0;
m_pManager->KillTimer(this, SCROLL_TIMERID);
return;
}
if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
{
m_uButtonState |= UISTATE_CAPTURED;
m_ptLastMouse = event.ptMouse;
m_dwDelayDeltaY = 0;
m_dwDelayNum = 0;
m_dwDelayLeft = 0;
::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
m_pManager->SetTimer(this, SCROLL_TIMERID, 50U);
return;
}
if( event.Type == UIEVENT_BUTTONUP )
{
if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
m_uButtonState &= ~UISTATE_CAPTURED;
::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
if( m_ptLastMouse.y != event.ptMouse.y ) {
m_dwDelayDeltaY = (event.ptMouse.y - m_ptLastMouse.y);
if( m_dwDelayDeltaY > 120 ) m_dwDelayDeltaY = 120;
else if( m_dwDelayDeltaY < -120 ) m_dwDelayDeltaY = -120;
m_dwDelayNum = (DWORD)sqrt((double)abs(m_dwDelayDeltaY)) * 5;
m_dwDelayLeft = m_dwDelayNum;
}
else
m_pManager->KillTimer(this, SCROLL_TIMERID);
}
return;
}
if( event.Type == UIEVENT_SCROLLWHEEL )
{
LONG lDeltaY = 0;
if( m_dwDelayNum > 0 ) lDeltaY = (LONG)(CalculateDelay((double)m_dwDelayLeft / m_dwDelayNum) * m_dwDelayDeltaY);
switch( LOWORD(event.wParam) ) {
case SB_LINEUP:
if( m_dwDelayDeltaY >= 0 ) m_dwDelayDeltaY = lDeltaY + 8;
else m_dwDelayDeltaY = lDeltaY + 12;
break;
case SB_LINEDOWN:
if( m_dwDelayDeltaY <= 0 ) m_dwDelayDeltaY = lDeltaY - 8;
else m_dwDelayDeltaY = lDeltaY - 12;
break;
}
if( m_dwDelayDeltaY > 100 ) m_dwDelayDeltaY = 100;
else if( m_dwDelayDeltaY < -100 ) m_dwDelayDeltaY = -100;
m_dwDelayNum = (DWORD)sqrt((double)abs(m_dwDelayDeltaY)) * 5;
m_dwDelayLeft = m_dwDelayNum;
m_pManager->SetTimer(this, SCROLL_TIMERID, 50U);
return;
}
CTileLayoutUI::DoEvent(event);
}
private:
UINT m_uButtonState;
POINT m_ptLastMouse;
LONG m_dwDelayDeltaY;
DWORD m_dwDelayNum;
DWORD m_dwDelayLeft;
};
class CDialogBuilderCallbackEx : public IDialogBuilderCallback
{
public:
CControlUI* CreateControl(LPCTSTR pstrClass)
{
if( _tcscmp(pstrClass, _T("GameList")) == 0 ) return new GameListUI;
else if( _tcscmp(pstrClass, _T("DeskList")) == 0 ) return new DeskListUI;
return NULL;
}
};
#endif __CONTROLEX_H__