forked from TorqueGameEngines/Torque3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguiControl.cpp
More file actions
2980 lines (2347 loc) · 91.7 KB
/
guiControl.cpp
File metadata and controls
2980 lines (2347 loc) · 91.7 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.
//-----------------------------------------------------------------------------
//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
// Copyright (C) 2015 Faust Logic, Inc.
//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
#include "platform/platform.h"
#include "gui/core/guiControl.h"
#include "console/consoleTypes.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/engineAPI.h"
#include "console/script.h"
#include "gfx/bitmap/gBitmap.h"
#include "sim/actionMap.h"
#include "gui/core/guiCanvas.h"
#include "gui/core/guiDefaultControlRender.h"
#include "gui/editor/guiEditCtrl.h"
#include "gfx/gfxDrawUtil.h"
#include "console/typeValidators.h"
//#define DEBUG_SPEW
IMPLEMENT_CONOBJECT_CHILDREN( GuiControl );
ConsoleDocClass( GuiControl,
"@brief Base class for all Gui control objects.\n\n"
"GuiControl is the basis for the Gui system. It represents an individual control that can be placed on the canvas and with which "
"the mouse and keyboard can potentially interact with.\n\n"
"@section GuiControl_Hierarchy Control Hierarchies\n"
"GuiControls are arranged in a hierarchy. All children of a control are placed in their parent's coordinate space, i.e. their "
"coordinates are relative to the upper left corner of their immediate parent. When a control is moved, all its child controls "
"are moved along with it.\n\n"
"Since GuiControl's are SimGroups, hierarchy also implies ownership. This means that if a control is destroyed, all its children "
"are destroyed along with it. It also means that a given control can only be part of a single GuiControl hierarchy. When adding a "
"control to another control, it will automatically be reparented from another control it may have previously been parented to.\n\n"
"@section GuiControl_Layout Layout System\n"
"GuiControls have a two-dimensional position and are rectangular in shape.\n\n"
"@section GuiControl_Events Event System\n"
"@section GuiControl_Profiles Control Profiles\n"
"Common data accessed by GuiControls is stored in so-called \"Control Profiles.\" This includes font, color, and texture information. "
"By pooling this data in shared objects, the appearance of any number of controls can be changed quickly and easily by modifying "
"only the shared profile object.\n\n"
"If not explicitly assigned a profile, a control will by default look for a profile object that matches its class name. This means "
"that the class GuiMyCtrl, for example, will look for a profile called 'GuiMyProfile'. If this profile cannot be found, the control "
"will fall back to GuiDefaultProfile which must be defined in any case for the Gui system to work.\n\n"
"In addition to its primary profile, a control may be assigned a second profile called 'tooltipProfile' that will be used to render "
"tooltip popups for the control.\n\n"
"@section GuiControl_Actions Triggered Actions\n"
"@section GuiControl_FirstResponders First Responders\n"
"At any time, a single control can be what is called the \"first responder\" on the GuiCanvas is placed on. This control "
"will be the first control to receive keyboard events not bound in the global ActionMap. If the first responder choses to "
"handle a particular keyboard event, \n\n"
"@section GuiControl_Waking Waking and Sleeping\n"
"@section GuiControl_VisibleActive Visibility and Activeness\n"
"By default, a GuiControl is active which means that it\n\n"
"@see GuiCanvas\n"
"@see GuiControlProfile\n"
"@ingroup GuiCore\n"
);
IMPLEMENT_CALLBACK( GuiControl, onAdd, void, (), (),
"Called when the control object is registered with the system after the control has been created." );
IMPLEMENT_CALLBACK( GuiControl, onRemove, void, (), (),
"Called when the control object is removed from the system before it is deleted." );
IMPLEMENT_CALLBACK( GuiControl, onWake, void, (), (),
"Called when the control is woken up.\n"
"@ref GuiControl_Waking" );
IMPLEMENT_CALLBACK( GuiControl, onSleep, void, (), (),
"Called when the control is put to sleep.\n"
"@ref GuiControl_Waking" );
IMPLEMENT_CALLBACK( GuiControl, onGainFirstResponder, void, (), (),
"Called when the control gains first responder status on the GuiCanvas.\n"
"@see setFirstResponder\n"
"@see makeFirstResponder\n"
"@see isFirstResponder\n"
"@ref GuiControl_FirstResponders" );
IMPLEMENT_CALLBACK( GuiControl, onLoseFirstResponder, void, (), (),
"Called when the control loses first responder status on the GuiCanvas.\n"
"@see setFirstResponder\n"
"@see makeFirstResponder\n"
"@see isFirstResponder\n"
"@ref GuiControl_FirstResponders" );
IMPLEMENT_CALLBACK( GuiControl, onAction, void, (), (),
"Called when the control's associated action is triggered and no 'command' is defined for the control.\n"
"@ref GuiControl_Actions" );
IMPLEMENT_CALLBACK( GuiControl, onVisible, void, ( bool state ), ( state ),
"Called when the control changes its visibility state, i.e. when going from visible to invisible or vice versa.\n"
"@param state The new visibility state.\n"
"@see isVisible\n"
"@see setVisible\n"
"@ref GuiControl_VisibleActive" );
IMPLEMENT_CALLBACK( GuiControl, onActive, void, ( bool state ), ( state ),
"Called when the control changes its activeness state, i.e. when going from active to inactive or vice versa.\n"
"@param stat The new activeness state.\n"
"@see isActive\n"
"@see setActive\n"
"@ref GuiControl_VisibleActive" );
IMPLEMENT_CALLBACK( GuiControl, onDialogPush, void, (), (),
"Called when the control is pushed as a dialog onto the canvas.\n"
"@see GuiCanvas::pushDialog" );
IMPLEMENT_CALLBACK( GuiControl, onDialogPop, void, (), (),
"Called when the control is removed as a dialog from the canvas.\n"
"@see GuiCanvas::popDialog" );
IMPLEMENT_CALLBACK( GuiControl, onControlDragEnter, void, ( GuiControl* control, const Point2I& dropPoint ), ( control, dropPoint ),
"Called when a drag&drop operation through GuiDragAndDropControl has entered the control. This is only called for "
"topmost visible controls as the GuiDragAndDropControl moves over them.\n\n"
"@param control The payload of the drag operation.\n"
"@param dropPoint The point at which the payload would be dropped if it were released now. Relative to the canvas." );
IMPLEMENT_CALLBACK( GuiControl, onControlDragExit, void, ( GuiControl* control, const Point2I& dropPoint ), ( control, dropPoint ),
"Called when a drag&drop operation through GuiDragAndDropControl has exited the control and moved over a different control. This is only called for "
"topmost visible controls as the GuiDragAndDropControl moves off of them.\n\n"
"@param control The payload of the drag operation.\n"
"@param dropPoint The point at which the payload would be dropped if it were released now. Relative to the canvas." );
IMPLEMENT_CALLBACK( GuiControl, onControlDragged, void, ( GuiControl* control, const Point2I& dropPoint ), ( control, dropPoint ),
"Called when a drag&drop operation through GuiDragAndDropControl is moving across the control after it has entered it. This is only called for "
"topmost visible controls as the GuiDragAndDropControl moves across them.\n\n"
"@param control The payload of the drag operation.\n"
"@param dropPoint The point at which the payload would be dropped if it were released now. Relative to the canvas." );
IMPLEMENT_CALLBACK( GuiControl, onControlDropped, void, ( GuiControl* control, const Point2I& dropPoint ), ( control, dropPoint ),
"Called when a drag&drop operation through GuiDragAndDropControl has completed and is dropping its payload onto the control. "
"This is only called for topmost visible controls as the GuiDragAndDropControl drops its payload on them.\n\n"
"@param control The control that is being dropped onto this control.\n"
"@param dropPoint The point at which the control is being dropped. Relative to the canvas." );
GuiControl *GuiControl::smPrevResponder = NULL;
GuiControl *GuiControl::smCurResponder = NULL;
GuiEditCtrl*GuiControl::smEditorHandle = NULL;
bool GuiControl::smDesignTime = false;
GuiControl* GuiControl::smThisControl;
IMPLEMENT_SCOPE( GuiAPI, Gui,, "" );
ImplementEnumType( GuiHorizontalSizing,
"Horizontal sizing behavior of a GuiControl.\n\n"
"@ingroup GuiCore" )
{ GuiControl::horizResizeRight, "right" },
{ GuiControl::horizResizeWidth, "width" },
{ GuiControl::horizResizeLeft, "left" },
{ GuiControl::horizResizeCenter, "center" },
{ GuiControl::horizResizeRelative, "relative" },
{ GuiControl::horizResizeAspectLeft, "aspectLeft" },
{ GuiControl::horizResizeAspectRight, "aspectRight" },
{ GuiControl::horizResizeAspectCenter, "aspectCenter" },
{ GuiControl::horizResizeWindowRelative, "windowRelative" }
EndImplementEnumType;
ImplementEnumType( GuiVerticalSizing,
"Vertical sizing behavior of a GuiControl.\n\n"
"@ingroup GuiCore" )
{ GuiControl::vertResizeBottom, "bottom" },
{ GuiControl::vertResizeHeight, "height" },
{ GuiControl::vertResizeTop, "top" },
{ GuiControl::vertResizeCenter, "center" },
{ GuiControl::vertResizeRelative, "relative" },
{ GuiControl::vertResizeAspectTop, "aspectTop" },
{ GuiControl::vertResizeAspectBottom, "aspectBottom" },
{ GuiControl::vertResizeAspectCenter, "aspectCenter" },
{ GuiControl::vertResizeWindowRelative, "windowRelative" }
EndImplementEnumType;
//-----------------------------------------------------------------------------
GuiControl::GuiControl() : mAddGroup( NULL ),
mBounds(0,0,64,64),
mProfile(NULL),
mTooltipProfile(NULL),
mTipHoverTime(1000),
mVisible(true),
mActive(true),
mAwake(false),
mIsContainer(false),
mCanResize(true),
mCanHit( true ),
mLayer(0),
mMinExtent(8,2),
mLangTable(NULL),
mFirstResponder(NULL),
mHorizSizing(horizResizeRight),
mVertSizing(vertResizeBottom),
mCategory(StringTable->EmptyString())
{
mConsoleVariable = StringTable->EmptyString();
mAcceleratorKey = StringTable->EmptyString();
mLangTableName = StringTable->EmptyString();
mTooltip = StringTable->EmptyString();
mRenderTooltipDelegate.bind( this, &GuiControl::defaultTooltipRender );
mCanSaveFieldDictionary = false;
mNotifyChildrenResized = true;
fade_amt = 1.0f;
}
//-----------------------------------------------------------------------------
GuiControl::~GuiControl()
{
}
//-----------------------------------------------------------------------------
void GuiControl::consoleInit()
{
Con::addVariable( "$ThisControl", TYPEID< GuiControl >(), &smThisControl,
"The control for which a command is currently being evaluated. Only set during 'command' "
"and altCommand callbacks to the control for which the command or altCommand is invoked.\n"
"@ingroup GuiCore");
}
//-----------------------------------------------------------------------------
void GuiControl::initPersistFields()
{
docsURL;
addGroup( "Layout" );
addField("position", TypePoint2I, Offset(mBounds.point, GuiControl),
"The position relative to the parent control." );
addField("extent", TypePoint2I, Offset(mBounds.extent, GuiControl),
"The width and height of the control." );
addField("minExtent", TypePoint2I, Offset(mMinExtent, GuiControl),
"The minimum width and height of the control. The control will not be resized smaller than this." );
addField("horizSizing", TYPEID< horizSizingOptions >(), Offset(mHorizSizing, GuiControl),
"The horizontal resizing behavior." );
addField("vertSizing", TYPEID< vertSizingOptions >(), Offset(mVertSizing, GuiControl),
"The vertical resizing behavior." );
endGroup( "Layout" );
addGroup( "Control");
addProtectedField("profile", TYPEID< GuiControlProfile >(), Offset(mProfile, GuiControl), &setProfileProt, &defaultProtectedGetFn,
"The control profile that determines fill styles, font settings, etc." );
addProtectedField( "visible", TypeBool, Offset(mVisible, GuiControl), &_setVisible, &defaultProtectedGetFn,
"Whether the control is visible or hidden." );
addProtectedField( "active", TypeBool, Offset( mActive, GuiControl ), &_setActive, &defaultProtectedGetFn,
"Whether the control is enabled for user interaction." );
addDeprecatedField("modal");
addDeprecatedField("setFirstResponder");
addField("variable", TypeString, Offset(mConsoleVariable, GuiControl),
"Name of the variable to which the value of this control will be synchronized." );
addField("command", TypeCommand, Offset(mConsoleCommand, GuiControl),
"Command to execute on the primary action of the control.\n\n"
"@note Within this script snippet, the control on which the #command is being executed is bound to "
"the global variable $ThisControl." );
addField("altCommand", TypeCommand, Offset(mAltConsoleCommand, GuiControl),
"Command to execute on the secondary action of the control.\n\n"
"@note Within this script snippet, the control on which the #altCommand is being executed is bound to "
"the global variable $ThisControl." );
addField("accelerator", TypeString, Offset(mAcceleratorKey, GuiControl),
"Key combination that triggers the control's primary action when the control is on the canvas." );
addField("category", TypeString, Offset(mCategory, GuiControl),
"Name of the category this gui control should be grouped into for organizational purposes. Primarily for tooling.");
endGroup( "Control" );
addGroup( "ToolTip" );
addProtectedField("tooltipProfile", TYPEID< GuiControlProfile >(), Offset(mTooltipProfile, GuiControl), &setTooltipProfileProt, &defaultProtectedGetFn,
"Control profile to use when rendering tooltips for this control." );
addField("tooltip", TypeRealString, Offset(mTooltip, GuiControl),
"String to show in tooltip for this control." );
addFieldV("hovertime", TypeRangedS32, Offset(mTipHoverTime, GuiControl), &CommonValidators::PositiveInt,
"Time for mouse to hover over control until tooltip is shown (in milliseconds)." );
endGroup( "ToolTip" );
addGroup( "Editing" );
addField("isContainer", TypeBool, Offset(mIsContainer, GuiControl),
"If true, the control may contain child controls." );
endGroup( "Editing" );
addGroup( "Localization" );
addField("langTableMod", TypeString, Offset(mLangTableName, GuiControl),
"Name of string table to use for lookup of internationalized text." );
endGroup( "Localization" );
Parent::initPersistFields();
}
//-----------------------------------------------------------------------------
bool GuiControl::processArguments(S32 argc, ConsoleValue *argv)
{
// argv[0] - The GuiGroup to add this control to when it's created.
// this is an optional parameter that may be specified at
// object creation time to organize a gui control into a
// subgroup of GuiControls and is useful in helping the
// gui editor group and sort the existent gui's in the Sim.
// Specified group?
if( argc == 1 )
{
StringTableEntry steIntName = StringTable->insert(argv[0]);
mAddGroup = dynamic_cast<SimGroup*>(Sim::getGuiGroup()->findObjectByInternalName( steIntName ));
if( mAddGroup == NULL )
{
mAddGroup = new SimGroup();
if( mAddGroup->registerObject() )
{
mAddGroup->setInternalName( steIntName );
Sim::getGuiGroup()->addObject( mAddGroup );
}
else
{
SAFE_DELETE( mAddGroup );
return false;
}
}
mAddGroup->addObject(this);
}
return true;
}
//-----------------------------------------------------------------------------
void GuiControl::awaken()
{
PROFILE_SCOPE( GuiControl_awaken );
#ifdef DEBUG_SPEW
Platform::outputDebugString( "[GuiControl] Waking up %i:%s (%s:%s) awake=%s",
getId(), getClassName(), getName(), getInternalName(),
mAwake ? "true" : "false" );
#endif
if( mAwake )
return;
// Wake up visible children.
for( GuiControl::iterator iter = begin(); iter != end(); ++ iter )
{
GuiControl* ctrl = static_cast< GuiControl* >( *iter );
if( ctrl->isVisible() && !ctrl->isAwake() )
ctrl->awaken();
}
if( !mAwake && !onWake() )
{
Con::errorf( ConsoleLogEntry::General, "GuiControl::awaken: failed onWake for obj: %i:%s (%s)",
getId(), getClassName(), getName() );
mAwake = false;
}
}
//-----------------------------------------------------------------------------
void GuiControl::sleep()
{
#ifdef DEBUG_SPEW
Platform::outputDebugString( "[GuiControl] Putting to sleep %i:%s (%s:%s) awake=%s",
getId(), getClassName(), getName(), getInternalName(),
mAwake ? "true" : "false" );
#endif
if( !mAwake )
return;
iterator i;
for(i = begin(); i != end(); i++)
{
GuiControl *ctrl = static_cast<GuiControl *>(*i);
if( ctrl->isAwake() )
ctrl->sleep();
}
if( mAwake )
onSleep();
}
//=============================================================================
// Rendering.
//=============================================================================
// MARK: ---- Rendering ----
//-----------------------------------------------------------------------------
void GuiControl::preRender()
{
if( !mAwake )
return;
iterator i;
for(i = begin(); i != end(); i++)
{
GuiControl *ctrl = static_cast<GuiControl *>(*i);
ctrl->preRender();
}
onPreRender();
}
//-----------------------------------------------------------------------------
void GuiControl::onPreRender()
{
// do nothing.
}
//-----------------------------------------------------------------------------
void GuiControl::onRender(Point2I offset, const RectI &updateRect)
{
RectI ctrlRect(offset, getExtent());
//if opaque, fill the update rect with the fill color
if ( mProfile->mOpaque )
GFX->getDrawUtil()->drawRectFill(ctrlRect, mProfile->mFillColor);
//if there's a border, draw the border
if ( mProfile->mBorder )
renderBorder(ctrlRect, mProfile);
// Render Children
renderChildControls(offset, updateRect);
}
//-----------------------------------------------------------------------------
bool GuiControl::defaultTooltipRender( const Point2I &hoverPos, const Point2I &cursorPos, const char* tipText )
{
// Short Circuit.
if (!mAwake)
return false;
if ( dStrlen( mTooltip ) == 0 && ( tipText == NULL || dStrlen( tipText ) == 0 ) )
return false;
String renderTip( mTooltip );
if ( tipText != NULL )
renderTip = tipText;
// Need to have root.
GuiCanvas *root = getRoot();
if ( !root )
return false;
GFont *font = mTooltipProfile->mFont;
GFXDrawUtil* drawUtil = GFX->getDrawUtil();
// Support for multi-line tooltip text...
Vector<U32> startLineOffsets, lineLengths;
font->wrapString( renderTip, U32_MAX, startLineOffsets, lineLengths );
// The width is the longest line.
U32 tipWidth = 0;
for ( U32 i = 0; i < lineLengths.size(); i++ )
{
U32 width = font->getStrNWidth( renderTip.c_str() + startLineOffsets[i], lineLengths[i] );
if ( width > tipWidth )
tipWidth = width;
}
// The height is the number of lines times the height of the font.
U32 tipHeight = lineLengths.size() * font->getHeight();
// Vars used:
// Screensize (for position check)
// Offset to get position of cursor
// textBounds for text extent.
Point2I screensize = getRoot()->getWindowSize();
Point2I offset = hoverPos;
Point2I textBounds;
// Offset below cursor image
offset.y += 20; // TODO: Attempt to fix?: root->getCursorExtent().y;
// Create text bounds...
// Pixels above/below the text
const U32 vMargin = 2;
// Pixels left/right of the text
const U32 hMargin = 4;
textBounds.x = tipWidth + hMargin * 2;
textBounds.y = tipHeight + vMargin * 2;
// Check position/width to make sure all of the tooltip will be rendered
// 5 is given as a buffer against the edge
if ( screensize.x < offset.x + textBounds.x + 5 )
offset.x = screensize.x - textBounds.x - 5;
// And ditto for the height
if ( screensize.y < offset.y + textBounds.y + 5 )
offset.y = hoverPos.y - textBounds.y - 5;
RectI oldClip = GFX->getClipRect();
// Set rectangle for the box, and set the clip rectangle.
RectI rect( offset, textBounds );
GFX->setClipRect( rect );
// Draw Filler bit, then border on top of that
drawUtil->drawRectFill( rect, mTooltipProfile->mFillColor );
drawUtil->drawRect( rect, mTooltipProfile->mBorderColor );
// Draw the text centered in the tool tip box...
drawUtil->setBitmapModulation( mTooltipProfile->mFontColor );
for ( U32 i = 0; i < lineLengths.size(); i++ )
{
Point2I start( hMargin, vMargin + i * font->getHeight() );
const UTF8 *line = renderTip.c_str() + startLineOffsets[i];
U32 lineLen = lineLengths[i];
drawUtil->drawTextN( font, start + offset, line, lineLen, mProfile->mFontColors );
}
GFX->setClipRect( oldClip );
return true;
}
//-----------------------------------------------------------------------------
void GuiControl::renderChildControls(Point2I offset, const RectI &updateRect)
{
// Save the current clip rect
// so we can restore it at the end of this method.
RectI savedClipRect = GFX->getClipRect();
// offset is the upper-left corner of this control in screen coordinates
// updateRect is the intersection rectangle in screen coords of the control
// hierarchy. This can be set as the clip rectangle in most cases.
RectI clipRect = updateRect;
iterator i;
for(i = begin(); i != end(); i++)
{
GuiControl *ctrl = static_cast<GuiControl *>(*i);
if (ctrl->mVisible)
{
Point2I childPosition = offset + ctrl->getPosition();
RectI childClip(childPosition, ctrl->getExtent() + Point2I(1,1));
if (childClip.intersect(clipRect))
{
GFX->setClipRect( childClip );
GFX->setStateBlock(mDefaultGuiSB);
ctrl->onRender(childPosition, childClip);
}
}
}
// Restore the clip rect to what it was at the start
// of this method.
GFX->setClipRect( savedClipRect );
}
//-----------------------------------------------------------------------------
void GuiControl::setUpdateRegion(Point2I pos, Point2I ext)
{
Point2I upos = localToGlobalCoord(pos);
GuiCanvas *root = getRoot();
if (root)
{
root->addUpdateRegion(upos, ext);
}
}
//-----------------------------------------------------------------------------
void GuiControl::setUpdate()
{
setUpdateRegion(Point2I(0,0), getExtent());
}
//-----------------------------------------------------------------------------
void GuiControl::renderJustifiedText(Point2I offset, Point2I extent, const char *text)
{
GFont *font = mProfile->mFont;
if(!font)
return;
S32 textWidth = font->getStrWidthPrecise((const UTF8*)text);
U32 textHeight = font->getHeight();
Point2I start( 0, 0 );
// Align horizontal.
if( textWidth > extent.x )
{
// If the text is longer then the box size, (it'll get clipped) so
// force Left Justify
start.x = 0;
}
else
switch( mProfile->mAlignment )
{
case GuiControlProfile::RightJustify:
start.x = extent.x - textWidth;
break;
case GuiControlProfile::TopJustify:
case GuiControlProfile::BottomJustify:
case GuiControlProfile::CenterJustify:
start.x = ( extent.x - textWidth) / 2;
break;
case GuiControlProfile::LeftJustify:
start.x = 0;
break;
}
// Align vertical.
if( textHeight > extent.y )
{
start.y = 0 - ( textHeight - extent.y ) / 2;
}
else
switch( mProfile->mAlignment )
{
case GuiControlProfile::TopJustify:
start.y = 0;
break;
case GuiControlProfile::BottomJustify:
start.y = extent.y - textHeight - 1;
break;
default:
// Center vertical.
start.y = ( extent.y - font->getHeight() ) / 2;
break;
}
GFX->getDrawUtil()->drawText( font, start + offset, text, mProfile->mFontColors );
}
//=============================================================================
// Events.
//=============================================================================
// MARK: ---- Events ----
//-----------------------------------------------------------------------------
bool GuiControl::onAdd()
{
// Let Parent Do Work.
if ( !Parent::onAdd() )
return false;
// Grab the classname of this object
const char *cName = getClassName();
// if we're a pure GuiControl, then we're a container by default.
if ( String::compare( "GuiControl", cName ) == 0 )
mIsContainer = true;
// Add to root group.
if ( mAddGroup == NULL )
mAddGroup = Sim::getGuiGroup();
mAddGroup->addObject(this);
// If we don't have a profile we must assign one now.
// Try assigning one based on the control's class name...
if ( !mProfile )
{
String name = getClassName();
if ( name.isNotEmpty() )
{
U32 pos = name.find( "Ctrl" );
if ( pos != -1 )
name.replace( pos, 4, "Profile" );
else
name += "Profile";
GuiControlProfile *profile = NULL;
if ( Sim::findObject( name, profile ) )
setControlProfile( profile );
}
}
// Try assigning the default profile...
if ( !mProfile )
{
GuiControlProfile *profile = NULL;
Sim::findObject( "GuiDefaultProfile", profile );
AssertISV( profile != NULL, avar("GuiControl::onAdd() unable to find specified profile and GuiDefaultProfile does not exist!") );
setControlProfile( profile );
}
// We must also assign a valid TooltipProfile...
if ( !mTooltipProfile )
{
GuiControlProfile *profile = NULL;
Sim::findObject( "GuiTooltipProfile", profile );
AssertISV( profile != NULL, avar("GuiControl::onAdd() unable to find specified tooltip profile and GuiTooltipProfile does not exist!") );
setTooltipProfile( profile );
}
// Notify Script.
onAdd_callback();
GFXStateBlockDesc d;
d.cullDefined = true;
d.cullMode = GFXCullNone;
d.zDefined = true;
d.zEnable = false;
mDefaultGuiSB = GFX->createStateBlock( d );
// Return Success.
return true;
}
//-----------------------------------------------------------------------------
void GuiControl::onRemove()
{
// If control is still awake, put it to sleep first. Otherwise, we'll see an
// onSleep() call triggered when we are removed from our parent.
if( mAwake )
sleep();
// Only invoke script callbacks if they can be received
onRemove_callback();
if ( mProfile )
{
mProfile->decUseCount();
mProfile = NULL;
}
if ( mTooltipProfile )
{
mTooltipProfile->decUseCount();
mTooltipProfile = NULL;
}
clearFirstResponder();
Parent::onRemove();
}
//-----------------------------------------------------------------------------
void GuiControl::onDeleteNotify(SimObject *object)
{
if( object == mProfile )
{
GuiControlProfile* profile;
Sim::findObject( "GuiDefaultProfile", profile );
if ( profile == mProfile )
mProfile = NULL;
else
setControlProfile( profile );
}
if (object == mTooltipProfile)
{
GuiControlProfile* profile;
Sim::findObject( "GuiDefaultProfile", profile );
if ( profile == mTooltipProfile )
mTooltipProfile = NULL;
else
setTooltipProfile( profile );
}
}
//-----------------------------------------------------------------------------
bool GuiControl::onWake()
{
PROFILE_SCOPE( GuiControl_onWake );
AssertFatal( !mAwake, "GuiControl::onWake() - control is already awake" );
if( mAwake )
return true;
// [tom, 4/18/2005] Cause mLangTable to be refreshed in case it was changed
mLangTable = NULL;
//set the flag
mAwake = true;
//set the layer
GuiCanvas *root = getRoot();
GuiControl *parent = getParent();
if (parent && parent != root)
mLayer = parent->mLayer;
//make sure the first responder exists
if (! mFirstResponder)
mFirstResponder = findFirstTabable();
//increment the profile
mProfile->incLoadCount();
if (mTooltipProfile)
{
mTooltipProfile->incLoadCount();
}
// Only invoke script callbacks if we have a namespace in which to do so
// This will suppress warnings
onWake_callback();
return true;
}
//-----------------------------------------------------------------------------
void GuiControl::onSleep()
{
AssertFatal( mAwake, "GuiControl::onSleep() - control is already asleep" );
if( !mAwake )
return;
clearFirstResponder();
mouseUnlock();
// Only invoke script callbacks if we have a namespace in which to do so
// This will suppress warnings
onSleep_callback();
//decrement the profile reference
mProfile->decLoadCount();
if (mTooltipProfile)
{
mTooltipProfile->decLoadCount();
}
// Set Flag
mAwake = false;
}
//-----------------------------------------------------------------------------
void GuiControl::onChildAdded( GuiControl *child )
{
// Base class does not make use of this
}
//-----------------------------------------------------------------------------
void GuiControl::onChildRemoved( GuiControl *child )
{
// Base does nothing with this
}
//-----------------------------------------------------------------------------
void GuiControl::onGroupRemove()
{
// If we have a first responder in our hierarchy,
// make sure to kill it off.
if( mFirstResponder )
mFirstResponder->clearFirstResponder();
else
{
GuiCanvas* root = getRoot();
if( root )
{
GuiControl* firstResponder = root->getFirstResponder();
if( firstResponder && firstResponder->isChildOfGroup( this ) )
firstResponder->clearFirstResponder();
}
}
// If we are awake, put us to sleep.
if( isAwake() )
sleep();
}
//-----------------------------------------------------------------------------
bool GuiControl::onInputEvent(const InputEventInfo &event)
{
// Do nothing by default...
return( false );
}
//-----------------------------------------------------------------------------
bool GuiControl::onKeyDown(const GuiEvent &event)
{
//pass the event to the parent
GuiControl *parent = getParent();
if (parent)
return parent->onKeyDown(event);
else
return false;
}
//-----------------------------------------------------------------------------
bool GuiControl::onKeyRepeat(const GuiEvent &event)
{
// default to just another key down.
return onKeyDown(event);
}
//-----------------------------------------------------------------------------
bool GuiControl::onKeyUp(const GuiEvent &event)
{
//pass the event to the parent
GuiControl *parent = getParent();
if (parent)
return parent->onKeyUp(event);
else
return false;
}
//-----------------------------------------------------------------------------
void GuiControl::onMouseUp(const GuiEvent &event)
{
}
//-----------------------------------------------------------------------------
void GuiControl::onMouseDown(const GuiEvent &event)
{
if ( !mVisible || !mAwake )
return;
execConsoleCallback();
}
//-----------------------------------------------------------------------------
void GuiControl::onMouseMove(const GuiEvent &event)
{
//if this control is a dead end, make sure the event stops here
if ( !mVisible || !mAwake )
return;
//pass the event to the parent
GuiControl *parent = getParent();
if ( parent )
parent->onMouseMove( event );