-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathMyGUI_ComboBox.cpp
More file actions
613 lines (504 loc) · 14.6 KB
/
MyGUI_ComboBox.cpp
File metadata and controls
613 lines (504 loc) · 14.6 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_ComboBox.h"
#include "MyGUI_ControllerManager.h"
#include "MyGUI_InputManager.h"
#include "MyGUI_WidgetManager.h"
#include "MyGUI_Gui.h"
#include "MyGUI_ListBox.h"
#include "MyGUI_Button.h"
#include "MyGUI_ResourceSkin.h"
#include "MyGUI_LayerManager.h"
#include "MyGUI_LanguageManager.h"
namespace MyGUI
{
const float COMBO_ALPHA_MAX = ALPHA_MAX;
const float COMBO_ALPHA_MIN = ALPHA_MIN;
const float COMBO_ALPHA_COEF = 4.0f;
ComboBox::ComboBox() :
mButton(nullptr),
mList(nullptr),
mListShow(false),
mAutoHideList(true),
mMaxListLength(-1),
mItemIndex(ITEM_NONE),
mModeDrop(false),
mDropMouse(false),
mShowSmooth(false),
mFlowDirection(FlowDirection::TopToBottom)
{
}
void ComboBox::initialiseOverride()
{
Base::initialiseOverride();
///@wskin_child{ComboBox, Button, Button} Кнопка для выпадающего списка.
assignWidget(mButton, "Button");
if (mButton != nullptr)
{
mButton->eventMouseButtonPressed += newDelegate(this, &ComboBox::notifyButtonPressed);
}
///@wskin_child{ComboBox, ListBox, List} Выпадающий список.
assignWidget(mList, "List");
if (mList == nullptr)
{
std::string list_skin = getUserString("ListSkin");
std::string list_layer = getUserString("ListLayer");
mList = static_cast<ListBox*>(_createSkinWidget(WidgetStyle::Popup, ListBox::getClassTypeName(), list_skin, IntCoord(), Align::Default, list_layer));
}
if (mList != nullptr)
{
mList->setActivateOnClick(true);
mList->setVisible(false);
mList->eventKeyLostFocus += newDelegate(this, &ComboBox::notifyListLostFocus);
mList->eventListSelectAccept += newDelegate(this, &ComboBox::notifyListSelectAccept);
mList->eventListMouseItemActivate += newDelegate(this, &ComboBox::notifyListMouseItemActivate);
mList->eventListChangePosition += newDelegate(this, &ComboBox::notifyListChangePosition);
mList->setNeedToolTip(true);
mList->eventToolTip += newDelegate(this, &ComboBox::notifyToolTip);
}
// подписываем дочерние классы на скролл
if (mScrollViewClient != nullptr)
{
mScrollViewClient->eventMouseWheel += newDelegate(this, &ComboBox::notifyMouseWheel);
mScrollViewClient->eventMouseButtonPressed += newDelegate(this, &ComboBox::notifyMousePressed);
mScrollViewClient->setNeedToolTip(true);
mScrollViewClient->eventToolTip += newDelegate(this, &ComboBox::notifyToolTip);
}
// подписываемся на изменения текста
eventEditTextChange += newDelegate(this, &ComboBox::notifyEditTextChange);
}
void ComboBox::shutdownOverride()
{
mList->shutdownOverride();
mList = nullptr;
mButton = nullptr;
Base::shutdownOverride();
}
void ComboBox::notifyButtonPressed(Widget* _sender, int _left, int _top, MouseButton _id)
{
if (MouseButton::Left != _id)
return;
mDropMouse = true;
if (mListShow)
hideList();
else
showList();
}
void ComboBox::notifyListLostFocus(Widget* _sender, Widget* _new)
{
if (mDropMouse)
{
mDropMouse = false;
Widget* focus = InputManager::getInstance().getMouseFocusWidget();
// кнопка сама уберет список
if (focus == mButton)
return;
// в режиме дропа все окна учавствуют
if (mModeDrop && focus == mScrollViewClient)
return;
}
if (true == mAutoHideList)
hideList();
}
void ComboBox::notifyListSelectAccept(ListBox* _widget, size_t _position)
{
mItemIndex = _position;
Base::setCaption(mItemIndex != ITEM_NONE ? mList->getItemNameAt(mItemIndex) : "");
mDropMouse = false;
InputManager::getInstance().setKeyFocusWidget(this);
if (mModeDrop)
{
_resetContainer(false);
eventComboAccept.m_eventObsolete(this);
eventComboAccept.m_event(this, mItemIndex);
}
}
void ComboBox::notifyListChangePosition(ListBox* _widget, size_t _position)
{
mItemIndex = _position;
_resetContainer(false);
eventComboChangePosition(this, _position);
}
void ComboBox::onKeyButtonPressed(KeyCode _key, Char _char)
{
Base::onKeyButtonPressed(_key, _char);
// при нажатии вниз, показываем лист
if (_key == KeyCode::ArrowDown)
{
// выкидываем список только если мыша свободна
if (!InputManager::getInstance().isCaptureMouse())
{
showList();
}
}
// нажат ввод в окне редиктирования
else if ((_key == KeyCode::Return) || (_key == KeyCode::NumpadEnter))
{
_resetContainer(false);
eventComboAccept.m_eventObsolete(this);
eventComboAccept.m_event(this, mItemIndex);
}
}
void ComboBox::notifyListMouseItemActivate(ListBox* _widget, size_t _position)
{
mItemIndex = _position;
Base::setCaption(mItemIndex != ITEM_NONE ? mList->getItemNameAt(mItemIndex) : "");
InputManager::getInstance().setKeyFocusWidget(this);
if (mModeDrop)
{
_resetContainer(false);
eventComboAccept.m_eventObsolete(this);
eventComboAccept.m_event(this, mItemIndex);
}
}
void ComboBox::notifyMouseWheel(Widget* _sender, int _rel)
{
if (mList->getItemCount() == 0)
return;
if (InputManager::getInstance().getKeyFocusWidget() != this)
return;
if (InputManager::getInstance().isCaptureMouse())
return;
if (_rel > 0)
{
if (mItemIndex != 0)
{
if (mItemIndex == ITEM_NONE)
mItemIndex = 0;
else
mItemIndex --;
Base::setCaption(mList->getItemNameAt(mItemIndex));
mList->setIndexSelected(mItemIndex);
mList->beginToItemAt(mItemIndex);
_resetContainer(false);
eventComboChangePosition(this, mItemIndex);
}
}
else if (_rel < 0)
{
if ((mItemIndex + 1) < mList->getItemCount())
{
if (mItemIndex == ITEM_NONE)
mItemIndex = 0;
else
mItemIndex ++;
Base::setCaption(mList->getItemNameAt(mItemIndex));
mList->setIndexSelected(mItemIndex);
mList->beginToItemAt(mItemIndex);
_resetContainer(false);
eventComboChangePosition(this, mItemIndex);
}
}
}
void ComboBox::notifyMousePressed(Widget* _sender, int _left, int _top, MouseButton _id)
{
// обязательно отдаем отцу, а то мы у него в наглую отняли
Base::notifyMousePressed(_sender, _left, _top, _id);
mDropMouse = true;
// показываем список
if (mModeDrop)
notifyButtonPressed(nullptr, _left, _top, _id);
}
void ComboBox::notifyEditTextChange(EditBox* _sender)
{
// сбрасываем выделенный элемент
if (ITEM_NONE != mItemIndex)
{
mItemIndex = ITEM_NONE;
mList->setIndexSelected(mItemIndex);
mList->beginToItemFirst();
_resetContainer(false);
eventComboChangePosition(this, mItemIndex);
}
}
void ComboBox::showList()
{
// пустой список не показываем
if (mList->getItemCount() == 0)
return;
mListShow = true;
IntCoord coord = calculateListPosition();
mList->setCoord(coord);
if (mShowSmooth)
{
ControllerFadeAlpha* controller = createControllerFadeAlpha(COMBO_ALPHA_MAX, COMBO_ALPHA_COEF, true);
ControllerManager::getInstance().addItem(mList, controller);
}
else
{
mList->setVisible(true);
}
InputManager::getInstance().setKeyFocusWidget(mList);
}
void ComboBox::actionWidgetHide(Widget* _widget, ControllerItem* _controller)
{
_widget->setVisible(false);
_widget->setEnabled(true);
}
void ComboBox::hideList()
{
mListShow = false;
if (mShowSmooth)
{
ControllerFadeAlpha* controller = createControllerFadeAlpha(COMBO_ALPHA_MIN, COMBO_ALPHA_COEF, false);
controller->eventPostAction += newDelegate(this, &ComboBox::actionWidgetHide);
ControllerManager::getInstance().addItem(mList, controller);
}
else
{
mList->setVisible(false);
}
}
void ComboBox::setAutoHideList(bool autoHide)
{
mAutoHideList = autoHide;
}
void ComboBox::setIndexSelected(size_t _index)
{
MYGUI_ASSERT_RANGE_AND_NONE(_index, mList->getItemCount(), "ComboBox::setIndexSelected");
mItemIndex = _index;
mList->setIndexSelected(_index);
if (_index == ITEM_NONE)
{
Base::setCaption("");
return;
}
Base::setCaption(mList->getItemNameAt(_index));
Base::updateView(); // hook for update
}
void ComboBox::setItemNameAt(size_t _index, const UString& _name)
{
mList->setItemNameAt(_index, _name);
mItemIndex = ITEM_NONE;//FIXME
mList->setIndexSelected(mItemIndex);//FIXME
}
void ComboBox::setItemDataAt(size_t _index, Any _data)
{
mList->setItemDataAt(_index, _data);
mItemIndex = ITEM_NONE;//FIXME
mList->setIndexSelected(mItemIndex);//FIXME
}
void ComboBox::insertItemAt(size_t _index, const UString& _item, Any _data)
{
mList->insertItemAt(_index, _item, _data);
mItemIndex = ITEM_NONE;//FIXME
mList->setIndexSelected(mItemIndex);//FIXME
}
void ComboBox::removeItemAt(size_t _index)
{
mList->removeItemAt(_index);
mItemIndex = ITEM_NONE;//FIXME
mList->clearIndexSelected();//FIXME
}
void ComboBox::removeAllItems()
{
mItemIndex = ITEM_NONE;//FIXME
mList->removeAllItems();//FIXME заново созданные строки криво стоят
}
void ComboBox::setComboModeDrop(bool _drop)
{
mModeDrop = _drop;
setEditStatic(mModeDrop);
}
ControllerFadeAlpha* ComboBox::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
{
ControllerItem* item = ControllerManager::getInstance().createItem(ControllerFadeAlpha::getClassTypeName());
ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
controller->setAlpha(_alpha);
controller->setCoef(_coef);
controller->setEnabled(_enable);
return controller;
}
size_t ComboBox::findItemIndexWith(const UString& _name)
{
return mList->findItemIndexWith(_name);
}
void ComboBox::setFlowDirection(FlowDirection _value)
{
mFlowDirection = _value;
}
IntCoord ComboBox::calculateListPosition()
{
int length = 0;
if (mFlowDirection.isVertical())
length = mList->getOptimalHeight();
else
length = mMaxListLength;
if (mMaxListLength > 0 && length > mMaxListLength)
length = mMaxListLength;
// берем глобальные координаты выджета
IntCoord coord = getAbsoluteCoord();
// размер леера
IntSize sizeView = mList->getParentSize();
if (mFlowDirection == FlowDirection::TopToBottom)
{
if ((coord.bottom() + length) <= sizeView.height)
coord.top += coord.height;
else
coord.top -= length;
coord.height = length;
}
else if (mFlowDirection == FlowDirection::BottomToTop)
{
if ((coord.top - length) >= 0)
coord.top -= length;
else
coord.top += coord.height;
coord.height = length;
}
else if (mFlowDirection == FlowDirection::LeftToRight)
{
if ((coord.right() + length) <= sizeView.width)
coord.left += coord.width;
else
coord.left -= length;
coord.width = length;
}
else if (mFlowDirection == FlowDirection::RightToLeft)
{
if ((coord.left - length) >= 0)
coord.left -= length;
else
coord.left += coord.width;
coord.width = length;
}
return coord;
}
void ComboBox::setPropertyOverride(const std::string& _key, const std::string& _value)
{
/// @wproperty{ComboBox, ModeDrop, bool} Режим выпадающего списка, в этом режиме значение в поля поменять нельзя.
if (_key == "ModeDrop")
setComboModeDrop(utility::parseValue<bool>(_value));
/// @wproperty{ComboBox, FlowDirection, FlowDirection} Направление выпадения списка.
else if (_key == "FlowDirection")
setFlowDirection(utility::parseValue<FlowDirection>(_value));
/// @wproperty{ComboBox, MaxListLength, int} Максимальная высота или ширина (зависит от направления) списка в пикселях.
else if (_key == "MaxListLength")
setMaxListLength(utility::parseValue<int>(_value));
/// @wproperty{ComboBox, SmoothShow, bool} Плавное раскрытие списка.
else if (_key == "SmoothShow")
setSmoothShow(utility::parseValue<bool>(_value));
// не коментировать
else if (_key == "AddItem")
addItem(LanguageManager::getInstance().replaceTags(_value));
else
{
Base::setPropertyOverride(_key, _value);
return;
}
eventChangeProperty(this, _key, _value);
}
void ComboBox::setItemHeight(int itemHeight)
{
if (nullptr != mList)
{
mList->setItemHeight(itemHeight);
}
}
ListBox* ComboBox::getList(void) const
{
return mList;
}
size_t ComboBox::getItemCount() const
{
return mList->getItemCount();
}
void ComboBox::addItem(const UString& _name, Any _data)
{
return insertItemAt(ITEM_NONE, _name, _data);
}
size_t ComboBox::getIndexSelected() const
{
return mItemIndex;
}
void ComboBox::clearIndexSelected()
{
setIndexSelected(ITEM_NONE);
}
void ComboBox::clearItemDataAt(size_t _index)
{
setItemDataAt(_index, Any::Null);
}
const UString& ComboBox::getItemNameAt(size_t _index) const
{
return mList->getItemNameAt(_index);
}
void ComboBox::beginToItemAt(size_t _index)
{
mList->beginToItemAt(_index);
}
void ComboBox::beginToItemFirst()
{
if (getItemCount())
beginToItemAt(0);
}
void ComboBox::beginToItemLast()
{
if (getItemCount())
beginToItemAt(getItemCount() - 1);
}
void ComboBox::beginToItemSelected()
{
if (getIndexSelected() != ITEM_NONE)
beginToItemAt(getIndexSelected());
}
bool ComboBox::getComboModeDrop() const
{
return mModeDrop;
}
void ComboBox::setSmoothShow(bool _value)
{
mShowSmooth = _value;
}
bool ComboBox::getSmoothShow() const
{
return mShowSmooth;
}
void ComboBox::setMaxListLength(int _value)
{
mMaxListLength = _value;
}
int ComboBox::getMaxListLength() const
{
return mMaxListLength;
}
FlowDirection ComboBox::getFlowDirection() const
{
return mFlowDirection;
}
void ComboBox::notifyToolTip(Widget* _sender, const ToolTipInfo& _info)
{
if (getNeedToolTip())
eventToolTip(this, _info);
}
size_t ComboBox::_getItemCount() const
{
return getItemCount();
}
void ComboBox::_addItem(const MyGUI::UString& _name)
{
addItem(_name);
}
void ComboBox::_removeItemAt(size_t _index)
{
removeItemAt(_index);
}
void ComboBox::_setItemNameAt(size_t _index, const UString& _name)
{
setItemNameAt(_index, _name);
}
const UString& ComboBox::_getItemNameAt(size_t _index) const
{
return getItemNameAt(_index);
}
void ComboBox::_resetContainer(bool _update)
{
Base::_resetContainer(_update);
if (mList != nullptr)
mList->_resetContainer(_update);
}
} // namespace MyGUI