-
-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathguiListBoxCtrl.cpp
More file actions
1637 lines (1385 loc) · 48.5 KB
/
guiListBoxCtrl.cpp
File metadata and controls
1637 lines (1385 loc) · 48.5 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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "gui/controls/guiListBoxCtrl.h"
#include "gfx/gfxDrawUtil.h"
#include "console/engineAPI.h"
IMPLEMENT_CONOBJECT(GuiListBoxCtrl);
ConsoleDocClass( GuiListBoxCtrl,
"@brief A list of text items.\n\n"
"A list of text items where each individual entry can have its own text value, text color and associated SimObject.\n\n"
"@tsexample\n"
"new GuiListBoxCtrl(GuiMusicPlayerMusicList)\n"
"{\n"
" allowMultipleSelections = \"true\";\n"
" fitParentWidth = \"true\";\n"
" mirrorSet = \"AnotherGuiListBoxCtrl\";\n"
" makeNameCallback = \"\";\n"
" colorBullet = \"1\";\n"
" //Properties not specific to this control have been omitted from this example.\n"
"};\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
"@ingroup GuiCore\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, onMouseDragged, void, (),(),
"@brief Called whenever the mouse is dragged across the control.\n\n"
"@tsexample\n"
"// Mouse is dragged across the control, causing the callback to occur.\n"
"GuiListBoxCtrl::onMouseDragged(%this)\n"
" {\n"
" // Code to run whenever the mouse is dragged across the control\n"
" }\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, onClearSelection, void, (),(),
"@brief Called whenever a selected item in the list is cleared.\n\n"
"@tsexample\n"
"// A selected item is cleared, causing the callback to occur.\n"
"GuiListBoxCtrl::onClearSelection(%this)\n"
" {\n"
" // Code to run whenever a selected item is cleared\n"
" }\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, onUnSelect, void, ( S32 index, const char* itemText),( index, itemText ),
"@brief Called whenever a selected item in the list has been unselected.\n\n"
"@param index Index id of the item that was unselected\n"
"@param itemText Text for the list entry at the index id that was unselected\n\n"
"@tsexample\n"
"// A selected item is unselected, causing the callback to occur\n"
"GuiListBoxCtrl::onUnSelect(%this, %indexId, %itemText)\n"
" {\n"
" // Code to run whenever a selected list item is unselected\n"
" }\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, onSelect, void, ( S32 index , const char* itemText ),( index, itemText ),
"@brief Called whenever an item in the list is selected.\n\n"
"@param index Index id for the item in the list that was selected.\n"
"@param itemText Text for the list item at the index that was selected.\n\n"
"@tsexample\n"
"// An item in the list is selected, causing the callback to occur\n"
"GuiListBoxCtrl::onSelect(%this, %index, %itemText)\n"
" {\n"
" // Code to run whenever an item in the list is selected\n"
" }\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, onDoubleClick, void, (),(),
"@brief Called whenever an item in the list has been double clicked.\n\n"
"@tsexample\n"
"// An item in the list is double clicked, causing the callback to occur.\n"
"GuiListBoxCtrl::onDoubleClick(%this)\n"
" {\n"
" // Code to run whenever an item in the control has been double clicked\n"
" }\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, onMouseUp, void, ( S32 itemHit, S32 mouseClickCount ),( itemHit,mouseClickCount ),
"@brief Called whenever the mouse has previously been clicked down (onMouseDown) and has now been raised on the control.\n"
"If an item in the list was hit during the click cycle, then the index id of the clicked object along with how many clicks occured are passed\n"
"into the callback.\n\n"
"Detailed description\n\n"
"@param itemHit Index id for the list item that was hit\n"
"@param mouseClickCount How many mouse clicks occured on this list item\n\n"
"@tsexample\n"
"// Mouse was previously clicked down, and now has been released, causing the callback to occur.\n"
"GuiListBoxCtrl::onMouseUp(%this, %itemHit, %mouseClickCount)\n"
" {\n"
" // Code to call whenever the mouse has been clicked and released on the control\n"
" }\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, onDeleteKey, void, (),(),
"@brief Called whenever the Delete key on the keyboard has been pressed while in this control.\n\n"
"@tsexample\n"
"// The delete key on the keyboard has been pressed while this control is in focus, causing the callback to occur.\n"
"GuiListBoxCtrl::onDeleteKey(%this)\n"
" {\n"
" // Code to call whenever the delete key is pressed\n"
" }\n"
"@endtsexample\n\n"
"@see GuiControl\n\n"
);
IMPLEMENT_CALLBACK( GuiListBoxCtrl, isObjectMirrored, bool, ( const char* indexIdString ),( indexIdString ),
"@brief Checks if a list item at a defined index id is mirrored, and returns the result.\n\n"
"@param indexIdString Index id of the list to check.\n"
"@tsexample\n"
"// Engine has requested of the script level to determine if a list entry is mirrored or not.\n"
"GuiListBoxCtrl::isObjectMirrored(%this, %indexIdString)\n"
" {\n"
" // Perform code required to check and see if the list item at the index id is mirrored or not.\n"
" return %isMirrored;\n"
" }\n"
"@endtsexample\n\n"
"@return A boolean value on if the list item is mirrored or not.\n\n"
"@see GuiControl\n\n"
);
GuiListBoxCtrl::GuiListBoxCtrl()
{
mItems.clear();
mSelectedItems.clear();
mMultipleSelections = true;
mFitParentWidth = true;
mColorBullet = true;
mItemSize = Point2I(10,20);
mLastClickItem = NULL;
mRenderTooltipDelegate.bind( this, &GuiListBoxCtrl::renderTooltip );
}
GuiListBoxCtrl::~GuiListBoxCtrl()
{
clearItems();
}
void GuiListBoxCtrl::initPersistFields()
{
addField( "allowMultipleSelections", TypeBool, Offset( mMultipleSelections, GuiListBoxCtrl), "If true, will allow the selection of multiple items in the listbox.\n");
addField( "fitParentWidth", TypeBool, Offset( mFitParentWidth, GuiListBoxCtrl), "If true, the width of the listbox will match the width of its parent control.\n");
addField( "colorBullet", TypeBool, Offset( mColorBullet, GuiListBoxCtrl), "If true, colored items will render a colored rectangular bullet next to the item text.\n");
addField( "mirrorSet", TypeRealString, Offset( mMirrorSetName, GuiListBoxCtrl ), "If populated with the name of another GuiListBoxCtrl, then this list box will mirror the contents of the mirrorSet listbox.\n");
addField( "makeNameCallback", TypeRealString, Offset( mMakeNameCallback, GuiListBoxCtrl ), "A script snippet to control what is displayed in the list for a SimObject. Within this snippet, $ThisControl is bound to the guiListBoxCtrl and $ThisObject to the contained object in question.\n");
Parent::initPersistFields();
}
bool GuiListBoxCtrl::onWake()
{
if( !Parent::onWake() )
return false;
updateSize();
return true;
}
//-----------------------------------------------------------------------------
// Item Accessors
//-----------------------------------------------------------------------------
DefineEngineMethod( GuiListBoxCtrl, setMultipleSelection, void, (bool allowMultSelections),,
"@brief Enable or disable multiple selections for this GuiListBoxCtrl object.\n\n"
"@param allowMultSelections Boolean variable to set the use of multiple selections or not.\n"
"@tsexample\n"
"// Define the multiple selection use state.\n"
"%allowMultSelections = \"true\";\n\n"
"// Set the allow multiple selection state on the GuiListBoxCtrl object.\n"
"%thisGuiListBoxCtrl.setMultipleSelection(%allowMultSelections);\n"
"@endtsexample\n\n"
"@see GuiControl\n")
{
object->setMultipleSelection( allowMultSelections );
}
DefineEngineMethod( GuiListBoxCtrl, clearItems, void, (),,
"@brief Clears all the items in the listbox.\n\n"
"@tsexample\n"
"// Inform the GuiListBoxCtrl object to clear all items from its list.\n"
"%thisGuiListBoxCtrl.clearItems();\n"
"@endtsexample\n\n"
"@see GuiControl")
{
object->clearItems();
}
void GuiListBoxCtrl::clearItems()
{
// Free item list allocated memory
while( mItems.size() )
deleteItem( 0 );
// Free our vector lists
mItems.clear();
mSelectedItems.clear();
mFilteredItems.clear();
}
DefineEngineMethod( GuiListBoxCtrl, clearSelection, void, (),,
"@brief Sets all currently selected items to unselected.\n\n"
"Detailed description\n\n"
"@tsexample\n"
"// Inform the GuiListBoxCtrl object to set all of its items to unselected./n"
"%thisGuiListBoxCtrl.clearSelection();\n"
"@endtsexample\n\n"
"@see GuiControl")
{
object->clearSelection();
}
void GuiListBoxCtrl::clearSelection()
{
if( !mSelectedItems.size() )
return;
VectorPtr<LBItem*>::iterator i = mSelectedItems.begin();
for( ; i != mSelectedItems.end(); i++ )
(*i)->isSelected = false;
mSelectedItems.clear();
onClearSelection_callback();
}
DefineEngineMethod( GuiListBoxCtrl, setSelected, void, (S32 index, bool setSelected), (true),
"@brief Sets the item at the index specified to selected or not.\n\n"
"Detailed description\n\n"
"@param index Item index to set selected or unselected.\n"
"@param setSelected Boolean selection state to set the requested item index.\n"
"@tsexample\n"
"// Define the index\n"
"%index = \"5\";\n\n"
"// Define the selection state\n"
"%selected = \"true\"\n\n"
"// Inform the GuiListBoxCtrl object of the new selection state for the requested index entry.\n"
"%thisGuiListBoxCtrl.setSelected(%index,%selected);\n"
"@endtsexample\n\n"
"@see GuiControl")
{
if( setSelected == true )
object->addSelection( index );
else
object->removeSelection( index );
}
void GuiListBoxCtrl::removeSelection( S32 index )
{
// Range Check
if( index >= mItems.size() || index < 0 )
{
Con::warnf("GuiListBoxCtrl::removeSelection - index out of range!" );
return;
}
removeSelection( mItems[index], index );
}
void GuiListBoxCtrl::removeSelection( LBItem *item, S32 index )
{
if( !mSelectedItems.size() )
return;
if( !item )
return;
for( S32 i = 0 ; i < mSelectedItems.size(); i++ )
{
if( mSelectedItems[i] == item )
{
mSelectedItems.erase( &mSelectedItems[i] );
item->isSelected = false;
onUnSelect_callback(index, item->itemText);
return;
}
}
}
void GuiListBoxCtrl::addSelection( S32 index )
{
// Range Check
if( index >= mItems.size() || index < 0 )
{
Con::warnf("GuiListBoxCtrl::addSelection- index out of range!" );
return;
}
addSelection( mItems[index], index );
}
void GuiListBoxCtrl::addSelection( LBItem *item, S32 index )
{
if( !mMultipleSelections )
{
if( !mSelectedItems.empty() )
{
LBItem* selItem = mSelectedItems.front();
if( selItem != item )
clearSelection();
else
return;
}
}
else
{
if( !mSelectedItems.empty() )
{
for( S32 i = 0; i < mSelectedItems.size(); i++ )
{
if( mSelectedItems[ i ] == item )
return;
}
}
}
item->isSelected = true;
mSelectedItems.push_front( item );
onSelect_callback(index, item->itemText);
}
S32 GuiListBoxCtrl::getItemIndex( LBItem *item )
{
if( mItems.empty() )
return -1;
// Lookup the index of an item in our list, by the pointer to the item
for( S32 i = 0; i < mItems.size(); i++ )
if( mItems[i] == item )
return i;
return -1;
}
DefineEngineMethod( GuiListBoxCtrl, getItemCount, S32, (),,
"@brief Returns the number of items in the list.\n\n"
"@tsexample\n"
"// Request the number of items in the list of the GuiListBoxCtrl object.\n"
"%listItemCount = %thisGuiListBoxCtrl.getItemCount();\n"
"@endtsexample\n\n"
"@return The number of items in the list.\n\n"
"@see GuiControl")
{
return object->getItemCount();
}
S32 GuiListBoxCtrl::getItemCount()
{
return mItems.size();
}
DefineEngineMethod( GuiListBoxCtrl, getSelCount, S32, (),,
"@brief Returns the number of items currently selected.\n\n"
"@tsexample\n"
"// Request the number of currently selected items\n"
"%selectedItemCount = %thisGuiListBoxCtrl.getSelCount();\n"
"@endtsexample\n\n"
"@return Number of currently selected items.\n\n"
"@see GuiControl")
{
return object->getSelCount();
}
S32 GuiListBoxCtrl::getSelCount()
{
return mSelectedItems.size();
}
DefineEngineMethod( GuiListBoxCtrl, getSelectedItem, S32, (),,
"@brief Returns the selected items index or -1 if none selected. If multiple selections exist it returns the first selected item. \n\n"
"@tsexample\n"
"// Request the index id of the currently selected item\n"
"%selectedItemId = %thisGuiListBoxCtrl.getSelectedItem();\n"
"@endtsexample\n\n"
"@return The selected items index or -1 if none selected.\n\n"
"@see GuiControl")
{
return object->getSelectedItem();
}
S32 GuiListBoxCtrl::getSelectedItem()
{
if( mSelectedItems.empty() || mItems.empty() )
return -1;
for( S32 i = 0 ; i < mItems.size(); i++ )
if( mItems[i]->isSelected )
return i;
return -1;
}
DefineEngineMethod( GuiListBoxCtrl, getSelectedItems, const char*, (),,
"@brief Returns a space delimited list of the selected items indexes in the list.\n\n"
"@tsexample\n"
"// Request a space delimited list of the items in the GuiListBoxCtrl object.\n"
"%selectionList = %thisGuiListBoxCtrl.getSelectedItems();\n"
"@endtsexample\n\n"
"@return Space delimited list of the selected items indexes in the list\n\n"
"@see GuiControl")
{
S32 selCount = object->getSelCount();
if( selCount == -1 || selCount == 0 )
return StringTable->lookup("-1");
else if( selCount == 1 )
return Con::getIntArg(object->getSelectedItem());
Vector<S32> selItems;
object->getSelectedItems( selItems );
if( selItems.empty() )
return StringTable->lookup("-1");
static const U32 bufSize = selItems.size() * 4;
UTF8 *retBuffer = Con::getReturnBuffer( bufSize );
dMemset( retBuffer, 0, bufSize );
Vector<S32>::iterator i = selItems.begin();
for( ; i != selItems.end(); i++ )
{
UTF8 retFormat[12];
dSprintf( retFormat, 12, "%d ", (*i) );
dStrcat( retBuffer, retFormat, 12 );
}
return retBuffer;
}
void GuiListBoxCtrl::getSelectedItems( Vector<S32> &Items )
{
// Clear our return vector
Items.clear();
// If there are no selected items, return an empty vector
if( mSelectedItems.empty() )
return;
for( S32 i = 0; i < mItems.size(); i++ )
if( mItems[i]->isSelected )
Items.push_back( i );
}
DefineEngineMethod( GuiListBoxCtrl, findItemText, S32, (const char* findText, bool bCaseSensitive), (false),
"@brief Returns index of item with matching text or -1 if none found.\n\n"
"@param findText Text in the list to find.\n"
"@param isCaseSensitive If true, the search will be case sensitive.\n"
"@tsexample\n"
"// Define the text we wish to find in the list.\n"
"%findText = \"Hickory Smoked Gideon\"/n/n"
"// Define if this is a case sensitive search or not.\n"
"%isCaseSensitive = \"false\";\n\n"
"// Ask the GuiListBoxCtrl object what item id in the list matches the requested text.\n"
"%matchingId = %thisGuiListBoxCtrl.findItemText(%findText,%isCaseSensitive);\n"
"@endtsexample\n\n"
"@return Index id of item with matching text or -1 if none found.\n\n"
"@see GuiControl")
{
return object->findItemText( findText, bCaseSensitive );
}
S32 GuiListBoxCtrl::findItemText( StringTableEntry text, bool caseSensitive )
{
// Check Proper Arguments
if( !text || !text[0] || text == StringTable->lookup("") )
{
Con::warnf("GuiListBoxCtrl::findItemText - No Text Specified!");
return -1;
}
// Check Items Exist.
if( mItems.empty() )
return -1;
// Lookup the index of an item in our list, by the pointer to the item
for( S32 i = 0; i < mItems.size(); i++ )
{
// Case Sensitive Compare?
if( caseSensitive && ( String::compare( mItems[i]->itemText, text ) == 0 ) )
return i;
else if (!caseSensitive && ( dStricmp( mItems[i]->itemText, text ) == 0 ))
return i;
}
// Not Found!
return -1;
}
DefineEngineMethod( GuiListBoxCtrl, setCurSel, void, (S32 indexId),,
"@brief Sets the currently selected item at the specified index.\n\n"
"@param indexId Index Id to set selected.\n"
"@tsexample\n"
"// Define the index id that we wish to select.\n"
"%selectId = \"4\";\n\n"
"// Inform the GuiListBoxCtrl object to set the requested index as selected.\n"
"%thisGuiListBoxCtrl.setCurSel(%selectId);\n"
"@endtsexample\n\n"
"@see GuiControl")
{
object->setCurSel( indexId );
}
void GuiListBoxCtrl::setCurSel( S32 index )
{
// Range Check
if( index >= mItems.size() )
{
Con::warnf("GuiListBoxCtrl::setCurSel - index out of range!" );
return;
}
// If index -1 is specified, we clear the selection
if( index == -1 )
{
mSelectedItems.clear();
return;
}
// Add the selection
addSelection( mItems[ index ], index );
}
DefineEngineMethod( GuiListBoxCtrl, setCurSelRange, void, (S32 indexStart, S32 indexStop), (999999),
"@brief Sets the current selection range from index start to stop. If no stop is specified it sets from start index to the end of the list\n\n"
"@param indexStart Index Id to start selection.\n"
"@param indexStop Index Id to end selection.\n"
"@tsexample\n"
"// Set start id\n"
"%indexStart = \"3\";\n\n"
"// Set end id\n"
"%indexEnd = \"6\";\n\n"
"// Request the GuiListBoxCtrl object to select the defined range.\n"
"%thisGuiListBoxCtrl.setCurSelRange(%indexStart,%indexEnd);\n"
"@endtsexample\n\n"
"@see GuiControl")
{
object->setCurSelRange( indexStart , indexStop );
}
void GuiListBoxCtrl::setCurSelRange( S32 start, S32 stop )
{
// Verify Selection Range
if( start < 0 )
start = 0;
else if( start > mItems.size() )
start = mItems.size();
if( stop < 0 )
stop = 0;
else if( stop > mItems.size() )
stop = mItems.size();
S32 iterStart = ( start < stop ) ? start : stop;
S32 iterStop = ( start < stop ) ? stop : start;
for( ; iterStart <= iterStop; iterStart++ )
addSelection( mItems[iterStart], iterStart );
}
DefineEngineMethod( GuiListBoxCtrl, addItem, S32, (const char* newItem, const char* color), ( "" ),
"@brief Adds an item to the end of the list with an optional color.\n\n"
"@param newItem New item to add to the list.\n"
"@param color Optional color parameter to add to the new item.\n"
"@tsexample\n"
"// Define the item to add to the list.\n"
"%newItem = \"Gideon's Blue Coat\";\n\n"
"// Define the optional color for the new list item.\n"
"%color = \"0.0 0.0 1.0\";\n\n"
"// Inform the GuiListBoxCtrl object to add the item to the end of the list with the defined color.\n"
"%thisGuiListBoxCtrl.addItem(%newItem,%color);\n"
"@endtsexample\n\n"
"@return If not void, return value and description\n\n"
"@see GuiControl\n"
"@hide")
{
if(dStricmp(color,"") == 0)
{
return object->addItem( newItem );
}
else
{
U32 elementCount = GuiListBoxCtrl::getStringElementCount(color);
if(elementCount == 3)
{
F32 red, green, blue;
red = dAtof(GuiListBoxCtrl::getStringElement( color, 0 ));
green = dAtof(GuiListBoxCtrl::getStringElement( color, 1 ));
blue = dAtof(GuiListBoxCtrl::getStringElement( color, 2 ));
return object->addItemWithColor( newItem, LinearColorF(red, green, blue) );
}
else if(elementCount == 1)
{
U32 objId = dAtoi( color );
return object->addItem( newItem, (void*)(uintptr_t)objId );
}
else
{
Con::warnf("GuiListBoxCtrl::addItem() - Invalid number of parameters for the color!");
return -1;
}
}
}
static ConsoleDocFragment sGuiControlSetExtent1(
"@brief Adds an item to the control with the specific text.\n\n"
"@param text Text item to add to the list.\n"
"GuiListBoxCtrl", // The class to place the method in; use NULL for functions.
"void addItem( const char* text );" );
S32 GuiListBoxCtrl::addItem( StringTableEntry text, void *itemData )
{
// This just calls insert item at the end of the list
return insertItem( mItems.size(), text, itemData );
}
S32 GuiListBoxCtrl::addItemWithColor( StringTableEntry text, LinearColorF color, void *itemData )
{
// This just calls insert item at the end of the list
return insertItemWithColor( mItems.size(), text, color, itemData );
}
DefineEngineMethod( GuiListBoxCtrl, setItemColor, void, (S32 index, LinearColorF color),,
"@brief Sets the color of a single list entry at the specified index id.\n\n"
"@param index Index id to modify the color of in the list.\n"
"@param color Color value to set the list entry to.\n"
"@tsexample\n"
"// Define the index id value\n"
"%index = \"5\";\n\n"
"// Define the color value\n"
"%color = \"1.0 0.0 0.0\";\n\n"
"// Inform the GuiListBoxCtrl object to change the color of the requested index\n"
"%thisGuiListBoxCtrl.setItemColor(%index,%color);\n"
"@endtsexample\n\n"
"@see GuiControl")
{
object->setItemColor( index, color );
}
void GuiListBoxCtrl::setItemColor(S32 index, const LinearColorF& color)
{
if ((index >= mItems.size()) || index < 0)
{
Con::warnf("GuiListBoxCtrl::setItemColor - invalid index");
return;
}
LBItem* item = mItems[index];
item->hasColor = true;
item->color = color;
}
DefineEngineMethod( GuiListBoxCtrl, clearItemColor, void, (S32 index),,
"@brief Removes any custom coloring from an item at the defined index id in the list.\n\n"
"@param index Index id for the item to clear any custom color from.\n"
"@tsexample\n"
"// Define the index id\n"
"%index = \"4\";\n\n"
"// Request the GuiListBoxCtrl object to remove any custom coloring from the defined index entry\n"
"%thisGuiListBoxCtrl.clearItemColor(%index);\n"
"@endtsexample\n\n"
"@see GuiControl")
{
object->clearItemColor(index);
}
void GuiListBoxCtrl::clearItemColor( S32 index )
{
if ((index >= mItems.size()) || index < 0)
{
Con::warnf("GuiListBoxCtrl::setItemColor - invalid index");
return;
}
LBItem* item = mItems[index];
item->hasColor = false;
}
DefineEngineMethod( GuiListBoxCtrl, insertItem, void, (const char* text, S32 index),,
"@brief Inserts an item into the list at the specified index and returns the index assigned or -1 on error.\n\n"
"@param text Text item to add.\n"
"@param index Index id to insert the list item text at.\n"
"@tsexample\n"
"// Define the text to insert\n"
"%text = \"Secret Agent Gideon\";\n\n"
"// Define the index entry to insert the text at\n"
"%index = \"14\";\n\n"
"// In form the GuiListBoxCtrl object to insert the text at the defined index.\n"
"%assignedId = %thisGuiListBoxCtrl.insertItem(%text,%index);\n"
"@endtsexample\n\n"
"@return If successful will return the index id assigned. If unsuccessful, will return -1.\n\n"
"@see GuiControl")
{
object->insertItem( index, text );
}
S32 GuiListBoxCtrl::insertItem( S32 index, StringTableEntry text, void *itemData )
{
// If the index is greater than our list size, insert it at the end
if( index >= mItems.size() )
index = mItems.size();
// Sanity checking
if( !text )
{
Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL string" );
return -1;
}
LBItem *newItem = new LBItem;
// Assign item data
newItem->itemText = StringTable->insert(text, true);
newItem->itemData = itemData;
newItem->isSelected = false;
newItem->hasColor = false;
// Add to list
mItems.insert(index);
mItems[index] = newItem;
// Resize our list to fit our items
updateSize();
// Return our index in list (last)
return index;
}
S32 GuiListBoxCtrl::insertItemWithColor( S32 index, StringTableEntry text, LinearColorF color, void *itemData )
{
// If the index is greater than our list size, insert it at the end
if( index >= mItems.size() )
index = mItems.size();
// Sanity checking
if( !text )
{
Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL string" );
return -1;
}
if( color == LinearColorF(-1, -1, -1) )
{
Con::warnf("GuiListBoxCtrl::insertItem - cannot add NULL color" );
return -1;
}
LBItem *newItem = new LBItem;
// Assign item data
newItem->itemText = StringTable->insert(text, true);
newItem->itemData = itemData;
newItem->isSelected = false;
newItem->hasColor = true;
newItem->color = color;
// Add to list
mItems.insert(index);
mItems[index] = newItem;
// Resize our list to fit our items
updateSize();
// Return our index in list (last)
return index;
}
DefineEngineMethod( GuiListBoxCtrl, deleteItem, void, (S32 itemIndex),,
"@brief Removes the list entry at the requested index id from the control and clears the memory associated with it.\n\n"
"@param itemIndex Index id location to remove the item from.\n"
"@tsexample\n"
"// Define the index id we want to remove from the list\n"
"%itemIndex = \"8\";\n\n"
"// Inform the GuiListBoxCtrl object to remove the item at the defined index id.\n"
"%thisGuiListBoxCtrl.deleteItem(%itemIndex);\n"
"@endtsexample\n\n"
"@see References")
{
object->deleteItem( itemIndex );
}
void GuiListBoxCtrl::deleteItem( S32 index )
{
// Range Check
if( index >= mItems.size() || index < 0 )
{
Con::warnf("GuiListBoxCtrl::deleteItem - index out of range!" );
return;
}
// Grab our item
LBItem* item = mItems[ index ];
if( !item )
{
Con::warnf("GuiListBoxCtrl::deleteItem - Bad Item Data!" );
return;
}
// Remove it from the selected list.
if( item->isSelected )
{
for( VectorPtr<LBItem*>::iterator i = mSelectedItems.begin(); i != mSelectedItems.end(); i++ )
{
if( item == *i )
{
mSelectedItems.erase_fast( i );
break;
}
}
}
// Remove it from the list
mItems.erase( &mItems[ index ] );
// Free the memory associated with it
delete item;
}
DefineEngineMethod( GuiListBoxCtrl, getItemText, const char*, (S32 index),,
"@brief Returns the text of the item at the specified index.\n\n"
"@param index Index id to return the item text from.\n"
"@tsexample\n"
"// Define the index id entry to request the text from\n"
"%index = \"12\";\n\n"
"// Request the item id text from the GuiListBoxCtrl object.\n"
"%text = %thisGuiListBoxCtrl.getItemText(%index);\n"
"@endtsexample\n\n"
"@return The text of the requested index id.\n\n"
"@see GuiControl")
{
return object->getItemText( index );
}
StringTableEntry GuiListBoxCtrl::getItemText( S32 index )
{
// Range Checking
if( index > mItems.size() || index < 0 )
{
Con::warnf( "GuiListBoxCtrl::getItemText - index out of range!" );
return StringTable->lookup("");
}
return mItems[ index ]->itemText;
}
DefineEngineMethod( GuiListBoxCtrl, getItemObject, const char*, (S32 index),,
"@brief Returns the object associated with an item. This only makes sense if you are mirroring a simset.\n\n"
"@param index Index id to request the associated item from.\n"
"@tsexample\n"
"// Define the index id\n"
"%index = \"12\";\n\n"
"// Request the item from the GuiListBoxCtrl object\n"
"%object = %thisGuiListBoxCtrl.getItemObject(%index);\n"
"@endtsexample\n\n"
"@return The object associated with the item in the list.\n\n"
"@see References")
{
SimObject *outObj = object->getItemObject( index );
if ( !outObj )
return NULL;
return outObj->getIdString();
}
SimObject* GuiListBoxCtrl::getItemObject( S32 index )
{
// Range Checking
if( index > mItems.size() || index < 0 )
{
Con::warnf( "GuiListBoxCtrl::getItemObject - index out of range!" );
return NULL;
}
SimObject *outObj;
Sim::findObject( (SimObjectId)(uintptr_t)(mItems[ index ]->itemData), outObj );
return outObj;
}
DefineEngineMethod( GuiListBoxCtrl, setItemText, void, (S32 index, const char* newtext),,
"@brief Sets the items text at the specified index.\n\n"
"@param index Index id to set the item text at.\n"
"@param newtext Text to change the list item at index id to.\n"
"@tsexample\n"
"// Define the index id/n"
"%index = \"12\";\n\n"
"// Define the text to set the list item to\n"
"%newtext = \"Gideon's Fancy Goggles\";\n\n"
"// Inform the GuiListBoxCtrl object to change the text at the requested index\n"
"%thisGuiListBoxCtrl.setItemText(%index,%newText);\n"
"@endtsexample\n\n"
"@see GuiControl")
{
object->setItemText(index, newtext );
}
void GuiListBoxCtrl::setItemText( S32 index, StringTableEntry text )
{
// Sanity Checking
if( !text )
{
Con::warnf("GuiListBoxCtrl::setItemText - Invalid Text Specified!" );
return;
}
// Range Checking
if( index > mItems.size() || index < 0 )
{
Con::warnf( "GuiListBoxCtrl::getItemText - index out of range!" );
return;
}
mItems[ index ]->itemText = StringTable->insert( text, true );
}
DefineEngineMethod( GuiListBoxCtrl, setItemTooltip, void, (S32 index, const char* text),,
"@brief Set the tooltip text to display for the given list item.\n\n"
"@param index Index id to change the tooltip text\n"
"@param text Text for the tooltip.\n"
"@tsexample\n"
"// Define the index id\n"
"%index = \"12\";\n\n"
"// Define the tooltip text\n"
"%tooltip = \"Gideon's goggles can see through space and time.\"\n\n"
"// Inform the GuiListBoxCtrl object to set the tooltop for the item at the defined index id\n"
"%thisGuiListBoxCtrl.setItemToolTip(%index,%tooltip);\n"
"@endtsexample\n\n"
"@see GuiControl")
{
if( index > object->mItems.size() || index < 0 )
{
Con::errorf( "GuiListBoxCtrl::setItemTooltip - index '%i' out of range", index );
return;
}
object->mItems[ index ]->itemTooltip = text;
}
DefineEngineMethod( GuiListBoxCtrl, getLastClickItem, S32, (),,
"@brief Request the item index for the item that was last clicked.\n\n"
"@tsexample\n"
"// Request the item index for the last clicked item in the list\n"
"%lastClickedIndex = %thisGuiListBoxCtrl.getLastClickItem();\n"
"@endtsexample\n\n"
"@return Index id for the last clicked item in the list.\n\n"
"@see GuiControl")
{
GuiListBoxCtrl::LBItem *lastItem = object->mLastClickItem;
if ( !lastItem )
return -1;
return object->getItemIndex( lastItem );
}
//-----------------------------------------------------------------------------
// Sizing Functions
//-----------------------------------------------------------------------------
void GuiListBoxCtrl::updateSize()
{