guiTypes.h

Engine/source/gui/core/guiTypes.h

More...

Classes:

class

Represents the Sizing Options for a GuiControl.

class
class

Represent a mouse event with a 3D position and vector.

class

A GuiControlProfile is used by every GuiObject and is akin to a datablock.

class

Represents a single GUI event.

Namespaces:

namespace

Docking Flag

Docking Options available to all GuiControl subclasses.

GuiDockingType 

Margin Padding Structure

Public Typedefs

GuiFontCharset 

Detailed Description

Docking Flag

Docking Options available to all GuiControl subclasses.

DefineEnumType(GuiDockingType )

typedef Docking::DockingType GuiDockingType 

Margin Padding Structure

DECLARE_STRUCT(RectSpacingI )

DefineConsoleType(TypeRectSpacingI , RectSpacingI )

Public Typedefs

typedef GuiControlProfile::AlignmentType GuiAlignmentType 
typedef FontCharset GuiFontCharset 

Public Functions

DefineEnumType(GuiAlignmentType )

DefineEnumType(GuiFontCharset )

GFX_DeclareTextureProfile(GFXDefaultGUIProfile )

GFX_DeclareTextureProfile(GFXGuiCursorProfile )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GUITYPES_H_
 25#define _GUITYPES_H_
 26
 27#ifndef _GFONT_H_
 28#include "gfx/gFont.h"
 29#endif
 30#ifndef _COLOR_H_
 31#include "core/color.h"
 32#endif
 33#ifndef _SIMBASE_H_
 34#include "console/simBase.h"
 35#endif
 36#ifndef _DYNAMIC_CONSOLETYPES_H_
 37#include "console/dynamicTypes.h"
 38#endif
 39
 40
 41#include "gfx/gfxDevice.h"
 42#include "platform/input/event.h"
 43
 44class GBitmap;
 45class SFXTrack;
 46
 47/// Represents a single GUI event.
 48///
 49/// This is passed around to all the relevant controls so they know what's going on.
 50struct GuiEvent
 51{
 52   U16                  ascii;            ///< ascii character code 'a', 'A', 'b', '*', etc (if device==keyboard) - possibly a uchar or something
 53   U8                   modifier;         ///< SI_LSHIFT, etc
 54   InputObjectInstances keyCode;          ///< for unprintables, 'tab', 'return', ...
 55   Point2I              mousePoint;       ///< for mouse events
 56   U8                   mouseClickCount;  ///< to determine double clicks, etc...
 57   U8                   mouseAxis;        ///< mousewheel axis (0 == X, 1 == Y)
 58   F32                  fval;             ///< used for mousewheel events
 59   
 60   GuiEvent()
 61      : ascii( 0 ),
 62        modifier( 0 ),
 63        keyCode( KEY_NULL ),
 64        mousePoint( 0, 0 ),
 65        mouseClickCount( 0 ),
 66        mouseAxis( 0 ),
 67        fval( 0.f ) {}
 68};
 69
 70/// Represent a mouse event with a 3D position and vector.
 71///
 72/// This event used by the EditTSCtrl derived controls.
 73struct Gui3DMouseEvent : public GuiEvent
 74{
 75   Point3F     vec;
 76   Point3F     pos;
 77   
 78   Gui3DMouseEvent()
 79      : vec( 0.f, 0.f, 0.f ),
 80        pos( 0.f, 0.f, 0.f ) {}
 81};
 82
 83
 84/// @name Docking Flag
 85/// @{
 86/// @brief Docking Options available to all GuiControl subclasses.
 87namespace Docking
 88{
 89   enum DockingType
 90   {
 91      dockNone    = BIT(0), ///< Do not align this control to it's parent, let the control specify it's position/extent (default)
 92      dockClient  = BIT(1), ///< Align this control to the client area available in the parent
 93      dockTop     = BIT(2), ///< Align this control to the topmost border of it's parent (Width will be parent width)
 94      dockBottom  = BIT(3), ///< Align this control to the bottommost border of it's parent (Width will be parent width)
 95      dockLeft    = BIT(4), ///< Align this control to the leftmost border of it's parent (Height will be parent height)
 96      dockRight   = BIT(5), ///< Align this control to the rightmost border of it's parent (Height will be parent height)
 97      dockInvalid = BIT(6),     ///< Default NOT specified docking mode, this allows old sizing to takeover when needed by controls
 98      dockAny     = dockClient | dockTop | dockBottom | dockLeft | dockRight
 99   };
100};
101
102typedef Docking::DockingType GuiDockingType;
103DefineEnumType( GuiDockingType );
104
105/// @}
106
107
108/// @name Margin Padding Structure
109/// @{
110struct RectSpacingI
111{
112   S32 left;
113   S32 top;
114   S32 bottom;
115   S32 right;
116   RectSpacingI() { left = right = top = bottom = 0; };
117   RectSpacingI( S32 in_top, S32 in_bottom, S32 in_left, S32 in_right )
118   {
119      top = in_top;
120      bottom = in_bottom;
121      left = in_left;
122      right = in_right;
123   }
124   void setAll( S32 value ) { left = right = top = bottom = value; };
125   void set( S32 in_top, S32 in_bottom, S32 in_left, S32 in_right )
126   {
127      top = in_top;
128      bottom = in_bottom;
129      left = in_left;
130      right = in_right;
131   }
132   void insetRect( RectI &rectRef )
133   {
134      // Inset by padding
135      rectRef.point.x += left;
136      rectRef.point.y += top;
137      rectRef.extent.x -= (left + right );
138      rectRef.extent.y -= (bottom + top );
139   }
140   void expandRect( RectI &rectRef )
141   {
142      // Inset by padding
143      rectRef.point.x -= left;
144      rectRef.point.y -= top;
145      rectRef.extent.x += (left + right );
146      rectRef.extent.y += (bottom + top );
147   }
148
149
150};
151
152DECLARE_STRUCT( RectSpacingI );
153DefineConsoleType( TypeRectSpacingI, RectSpacingI );
154/// @}
155
156
157/// @name Axis-Aligned Edge Structure
158/// @{
159///
160struct Edge
161{
162   Point2F normal;  ///< The Normal of this edge
163   Point2I position;///< The Position of the edge
164   Point2I extent;  ///< The X/Y extents of the edge
165   F32     margin;   ///< The Size of the edge
166
167   Edge(): normal(0.f,0.f),
168      position(0,0),
169      extent(0,0),
170      margin(1.f){};
171   Edge( const Point2I &inPoint, const Point2F &inNormal )
172   {
173      normal = inNormal;
174      margin = 2.f;
175
176      if( normal.x == 1.f || normal.x == -1.f )  
177      {
178         // Vertical Edge
179         position.x = inPoint.x;
180         position.y = 0;
181
182         extent.x = 1;
183         extent.y = 1;
184      }
185      else if( normal.y == 1.f || normal.y == -1.f )
186      {
187         // Horizontal Edge
188         position.y = inPoint.y;
189         position.x = 0;
190
191         extent.x = 1;
192         extent.y = 1;
193      }
194      else
195         AssertFatal(false,"Edge point constructor cannot construct an Edge without an axis-aligned normal.");
196   }
197
198   // Copy Constructor
199   Edge( const Edge &inEdge )
200   {
201      normal   = inEdge.normal;
202      position = inEdge.position;
203      extent   = inEdge.extent;
204      margin   = inEdge.margin;
205   }     
206
207   // RectI cast operator overload
208   operator const RectI() const
209   {
210      if( normal.x == 1.f || normal.x == -1.f )  
211      {
212         // Vertical Edge
213         RectI retRect = RectI( position.x, position.y, 1, position.y + extent.y );
214         // Expand Rect by Margin along the X Axis
215         retRect.inset(-margin,0);
216
217         return retRect;
218      }
219      else if( normal.y == 1.f || normal.y == -1.f )
220      {
221         // Horizontal Edge
222         RectI retRect =  RectI( position.x, position.y , position.x + extent.x,  1 );
223         // Expand Rect by Margin along the Y Axis
224         retRect.inset(0,-margin);
225         return retRect;
226      }
227
228      // CodeReview this code only deals with axis-aligned edges [6/8/2007 justind]
229      AssertFatal(false,"Edge cast operator cannot construct a Rect from an Edge that is not axis-aligned.");
230      return RectI( 0,0,0,0 );
231   }
232
233   inline bool hit( const Edge &inEdge ) const
234   {
235      const RectI thisRect = *this;
236      const RectI thatRect = inEdge;
237
238      return thisRect.overlaps( thatRect );
239   }
240};
241/// @}
242
243
244struct EdgeRectI
245{
246   Edge left;
247   Edge top;
248   Edge bottom;
249   Edge right;
250
251   EdgeRectI(){ }
252
253   EdgeRectI( const RectI &inRect, F32 inMargin )
254   {
255      // Left Edge 
256      left.normal    = Point2F( -1.f, 0.f );
257      left.position.x= inRect.point.x;
258      left.position.y= 0;
259      left.extent    = Point2I(inRect.point.y, inRect.point.y + inRect.extent.y);
260      left.margin    = inMargin;
261
262      // Right Edge
263      right.normal     = Point2F( 1.f, 0.f );
264      right.position.x = inRect.point.x + inRect.extent.x;
265      right.position.y = 0;
266      right.extent     = Point2I(inRect.point.y, inRect.point.y + inRect.extent.y);
267      right.margin     = inMargin;
268
269      // Top Edge
270      top.normal   = Point2F( 0.f, 1.f );
271      top.position.y = inRect.point.y;
272      top.position.x = 0;
273      top.extent   = Point2I(inRect.point.x + inRect.extent.x, inRect.point.x);
274      top.margin   = inMargin;
275
276      // Bottom Edge
277      bottom.normal   = Point2F( 0.f, -1.f );
278      bottom.position.y= inRect.point.y + inRect.extent.y;
279      bottom.position.x=0;
280      bottom.extent   = Point2I(inRect.point.x + inRect.extent.x, inRect.point.x);
281      bottom.margin   = inMargin;
282   }
283
284   // Copy constructor
285   EdgeRectI( const EdgeRectI &inEdgeRect )
286   {
287      left     = inEdgeRect.left;
288      right    = inEdgeRect.right;
289      top      = inEdgeRect.top;
290      bottom   = inEdgeRect.bottom;
291   }
292};
293
294
295/// Represents the Sizing Options for a GuiControl
296struct ControlSizing
297{
298   ControlSizing()
299   {
300      mDocking = Docking::dockInvalid;
301      mPadding.setAll( 0 );
302      mInternalPadding.setAll( 0 );
303
304      // Default anchors to full top/left
305      mAnchorBottom  = false;
306      mAnchorLeft    = true;
307      mAnchorTop     = true;
308      mAnchorRight   = false;
309   };
310
311   S32   mDocking; ///< Docking Flag
312
313   RectSpacingI mPadding; ///< Padding for each side of the control to have as spacing between other controls
314   ///  For example 1,1,1,1 would mean one pixel at least of spacing between this control and the
315   ///  one next to it.  
316   RectSpacingI mInternalPadding; ///< Interior Spacing of the control
317
318
319   /// @name Anchoring
320   /// @{
321   /// @brief Anchors are applied to @b ONLY controls that are children of any derivative of a
322   /// GuiContainer control.  Anchors are applied when a parent is resized and a child
323   /// element should be resized to accommodate the new parent extent
324   ///
325   /// Anchors are specified as true or false and control whether a certain edge of a control
326   /// will be locked to a certain edge of a parent, when the parent resizes.  Anchors are specified
327   /// as a Mask and therefore you may lock any number of edges to a parent container and when the parent
328   /// is resized, any locked edges on a control will remain the same distance from the parent edge it
329   /// is locked to, after the resize happens.  
330   ///
331   bool mAnchorTop;     ///< Anchor to the Top edge of the parent when created
332   bool mAnchorBottom;  ///< Anchor to the Bottom edge of the parent when created
333   bool mAnchorLeft;    ///< Anchor to the Left edge of the parent when created
334   bool mAnchorRight;   ///< Anchor to the Right edge of the parent when created
335   /// @}
336
337};
338
339class GuiCursor : public SimObject
340{
341private:
342   typedef SimObject Parent;
343   StringTableEntry mBitmapName;
344
345   Point2I mHotSpot;
346   Point2F mRenderOffset;
347   Point2I mExtent;
348   GFXTexHandle mTextureObject;
349
350public:
351   Point2I getHotSpot() { return mHotSpot; }
352   Point2I getExtent() { return mExtent; }
353
354   DECLARE_CONOBJECT(GuiCursor);
355   GuiCursor(void);
356   ~GuiCursor(void);
357   static void initPersistFields();
358
359   bool onAdd(void);
360   void onRemove();
361   void render(const Point2I &pos);
362};
363
364/// A GuiControlProfile is used by every GuiObject and is akin to a
365/// datablock. It is used to control information that does not change
366/// or is unlikely to change during execution of a program. It is also
367/// a level of abstraction between script and GUI control so that you can
368/// use the same control, say a button, and have it look completely different
369/// just with a different profile.
370class GuiControlProfile : public SimObject
371{
372private:
373   typedef SimObject Parent;
374
375public:
376   static StringTableEntry  sFontCacheDirectory;   ///< Directory where Torque will store font *.uft files.
377
378   U32  mUseCount;                                 ///< Total number of controls currently referencing this profile.
379   U32  mLoadCount;                                ///< Number of controls in woken state using this profile; resources for the profile are loaded when this is >0.
380   bool mTabable;                                  ///< True if this object is accessible from using the tab key
381
382   bool mCanKeyFocus;                              ///< True if the object can be given keyboard focus (in other words, made a first responder @see GuiControl)
383   bool mModal;                                    ///< True if this is a Modeless dialog meaning it will pass input through instead of taking it all
384
385   bool mOpaque;                                   ///< True if this object is not translucent, and should draw a fill
386   ColorI mFillColor;                              ///< Fill color, this is used to fill the bounds of the control if it is opaque
387   ColorI mFillColorHL;                            ///< This is used instead of mFillColor if the object is highlighted
388   ColorI mFillColorNA;                            ///< This is used instead of mFillColor if the object is not active or disabled
389   ColorI mFillColorERR;                           ///< This is used instead of mFillColor if the object has an error or is invalid
390   ColorI mFillColorSEL;                           ///< This is used instead of mFillColor if the object is selected
391
392   S32 mBorder;                                    ///< For most controls, if mBorder is > 0 a border will be drawn, some controls use this to draw different types of borders however @see guiDefaultControlRender.cc
393   S32 mBorderThickness;                           ///< Border thickness
394   ColorI mBorderColor;                            ///< Border color, used to draw a border around the bounds if border is enabled
395   ColorI mBorderColorHL;                          ///< Used instead of mBorderColor when the object is highlighted
396   ColorI mBorderColorNA;                          ///< Used instead of mBorderColor when the object is not active or disabled
397
398   ColorI mBevelColorHL;                          ///< Used for the high-light part of the bevel
399   ColorI mBevelColorLL;                          ///< Used for the low-light part of the bevel
400
401   // font members
402   StringTableEntry  mFontType;                    ///< Font face name for the control
403   S32               mFontSize;                    ///< Font size for the control
404   enum {
405      BaseColor = 0,
406      ColorHL,
407      ColorNA,
408      ColorSEL,
409      ColorUser0,
410      ColorUser1,
411      ColorUser2,
412      ColorUser3,
413      ColorUser4,
414      ColorUser5,
415   };
416   ColorI  mFontColors[10];                        ///< Array of font colors used for drawText with escape characters for changing color mid-string
417   ColorI& mFontColor;                             ///< Main font color
418   ColorI& mFontColorHL;                           ///< Highlighted font color
419   ColorI& mFontColorNA;                           ///< Font color when object is not active/disabled
420   ColorI& mFontColorSEL;                          ///< Font color when object/text is selected
421   FontCharset mFontCharset;                       ///< Font character set
422
423   Resource<GFont>   mFont;                        ///< Font resource
424
425   enum AlignmentType
426   {
427      LeftJustify,
428      RightJustify,
429      CenterJustify,
430      TopJustify,
431      BottomJustify
432   };
433
434   AlignmentType mAlignment;                       ///< Horizontal text alignment
435   bool mAutoSizeWidth;                            ///< Auto-size the width-bounds of the control to fit it's contents
436   bool mAutoSizeHeight;                           ///< Auto-size the height-bounds of the control to fit it's contents
437   bool mReturnTab;                                ///< Used in GuiTextEditCtrl to specify if a tab-event should be simulated when return is pressed.
438   bool mNumbersOnly;                              ///< For text controls, true if this should only accept numerical data
439   bool mMouseOverSelected;                        ///< True if this object should be "selected" while the mouse is over it
440   ColorI mCursorColor;                            ///< Color for the blinking cursor in text fields (for example)
441      
442   Point2I mTextOffset;                            ///< Text offset for the control
443
444   // bitmap members
445   StringTableEntry mBitmapName;                   ///< Bitmap file name for the bitmap of the control
446   bool mUseBitmapArray;                           ///< Flag to use the bitmap array or to fallback to non-array rendering
447   GFXTexHandle mTextureObject;
448   Vector<RectI> mBitmapArrayRects;                ///< Used for controls which use an array of bitmaps such as checkboxes
449
450   // sound members
451   SimObjectPtr< SFXTrack> mSoundButtonDown;                   ///< Sound played when the object is "down" ie a button is pushed
452   SimObjectPtr< SFXTrack> mSoundButtonOver;                   ///< Sound played when the mouse is over the object
453
454   StringTableEntry mChildrenProfileName;       ///< The name of the profile to use for the children controls
455
456   /// Returns our children profile (and finds the profile if it hasn't been set yet)
457   GuiControlProfile* getChildrenProfile();
458   
459   /// Category name for editing in the Gui Editor.
460   String mCategory;
461
462    /// Sets the children profile for this profile
463    ///
464    /// @see GuiControlProfile
465    /// @param   prof   Tooltip profile to apply
466    void setChildrenProfile(GuiControlProfile *prof);
467protected:
468   GuiControlProfile* mChildrenProfile;         ///< Profile used with children controls (such as the scroll bar on a popup menu) when defined.
469
470   static bool protectedSetBitmap( void *object, const char *index, const char *data );
471   static bool protectedSetSoundButtonDown( void* object, const char* index, const char* data );
472   static bool protectedSetSoundButtonOver( void* object, const char* index, const char* data );
473   static const char* protectedGetSoundButtonDown( void* object, const char* data );
474   static const char* protectedGetSoundButtonOver( void* object, const char* data );
475
476public:
477   DECLARE_CONOBJECT(GuiControlProfile);
478   GuiControlProfile();
479   ~GuiControlProfile();
480   static void initPersistFields();
481   
482   bool onAdd();
483
484   void onStaticModified(const char* slotName, const char* newValue = NULL );
485
486   /// Called when mProfileForChildren is deleted
487    virtual void onDeleteNotify(SimObject *object);
488
489   /// This method creates an array of bitmaps from one single bitmap with
490   /// separator color. The separator color is whatever color is in pixel 0,0
491   /// of the bitmap. For an example see darkWindow.png and some of the other
492   /// UI textures. It returns the number of bitmaps in the array it created
493   /// It also stores the sizes in the mBitmapArrayRects vector.
494   S32 constructBitmapArray();
495   
496   /// This method returns the ith bitmap array rect, first ensuring that i is a
497   /// valid index into mBitmapArrayRects. If the vector is empty, we call
498   /// constructBitmapArray() automatically. If it is still empty, we return a 
499   /// zeroed RectI.
500   RectI getBitmapArrayRect(U32 i);
501
502   ///
503   bool isInUse() const { return ( mUseCount != 0 ); }
504   
505   void incUseCount() { mUseCount ++; }
506   void decUseCount() { if( mUseCount > 0 ) mUseCount --; }
507
508   void incLoadCount();
509   void decLoadCount();
510   
511   bool loadFont();
512
513   void setBitmapHandle(GFXTexHandle handle); 
514};
515
516typedef GuiControlProfile::AlignmentType GuiAlignmentType;
517DefineEnumType( GuiAlignmentType );
518
519typedef FontCharset GuiFontCharset;
520DefineEnumType( GuiFontCharset );
521
522GFX_DeclareTextureProfile(GFXGuiCursorProfile);
523GFX_DeclareTextureProfile(GFXDefaultGUIProfile);
524
525#endif //_GUITYPES_H
526