guiBitmapButtonCtrl.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GUIBITMAPBUTTON_H_
  23. #define _GUIBITMAPBUTTON_H_
  24. #ifndef _GUIBUTTONCTRL_H_
  25. #include "gui/buttons/guiButtonCtrl.h"
  26. #endif
  27. #ifndef _GFXTEXTUREMANAGER_H_
  28. #include "gfx/gfxTextureManager.h"
  29. #endif
  30. /// A button control that uses bitmaps as its different button states.
  31. ///
  32. /// Set 'bitmap' console field to base name of bitmaps to use. This control will
  33. ///
  34. /// append '_n' for normal
  35. /// append '_h' for highlighted
  36. /// append '_d' for depressed
  37. /// append '_i' for inactive
  38. ///
  39. /// If a bitmap cannot be found it will use the default bitmap to render.
  40. ///
  41. /// Additionally, a bitmap button can be made to react to keyboard modifiers. These can be
  42. /// either CTRL/CMD, ALT, or SHIFT (but no combination of them.) To assign a different bitmap
  43. /// for a modifier state, prepend "_ctrl", _"alt", or "_shift" to the state postfix.
  44. ///
  45. /// To implement different handlers for the modifier states, use the "onDefaultClick",
  46. /// "onCtrlClick", "onAltClick", and "onShiftClick" methods.
  47. ///
  48. class GuiBitmapButtonCtrl : public GuiButtonCtrl
  49. {
  50. public:
  51. typedef GuiButtonCtrl Parent;
  52. enum BitmapMode
  53. {
  54. BitmapStretched,
  55. BitmapCentered,
  56. };
  57. protected:
  58. enum Modifier
  59. {
  60. ModifierNone,
  61. ModifierCtrl,
  62. ModifierAlt,
  63. ModifierShift,
  64. NumModifiers
  65. };
  66. enum State
  67. {
  68. NORMAL,
  69. HILIGHT,
  70. DEPRESSED,
  71. INACTIVE
  72. };
  73. struct Textures
  74. {
  75. /// Texture for normal state.
  76. GFXTexHandle mTextureNormal;
  77. /// Texture for highlight state.
  78. GFXTexHandle mTextureHilight;
  79. /// Texture for depressed state.
  80. GFXTexHandle mTextureDepressed;
  81. /// Texture for inactive state.
  82. GFXTexHandle mTextureInactive;
  83. };
  84. /// Make control extents equal to bitmap size.
  85. bool mAutoFitExtents;
  86. /// Allow switching out images according to modifier presses.
  87. bool mUseModifiers;
  88. /// Allow switching images according to mouse states. On by default.
  89. /// Switch off when not needed as it otherwise results in a lot of costly
  90. /// texture loads.
  91. bool mUseStates;
  92. ///
  93. BitmapMode mBitmapMode;
  94. /// File name for bitmap.
  95. String mBitmapName;
  96. /// alpha masking
  97. bool mMasked;
  98. ///
  99. Textures mTextures[ NumModifiers ];
  100. virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
  101. static bool _setAutoFitExtents( void *object, const char *index, const char *data );
  102. static bool _setBitmap( void *object, const char *index, const char *data );
  103. State getState() const
  104. {
  105. if( mActive )
  106. {
  107. if( mDepressed || mStateOn ) return DEPRESSED;
  108. if( mMouseOver ) return HILIGHT;
  109. return NORMAL;
  110. }
  111. else
  112. return INACTIVE;
  113. }
  114. Modifier getCurrentModifier();
  115. GFXTexHandle& getTextureForCurrentState();
  116. /// @name Callbacks
  117. /// @{
  118. DECLARE_CALLBACK( void, onDefaultClick, () );
  119. DECLARE_CALLBACK( void, onCtrlClick, () );
  120. DECLARE_CALLBACK( void, onAltClick, () );
  121. DECLARE_CALLBACK( void, onShiftClick, () );
  122. /// @}
  123. public:
  124. GuiBitmapButtonCtrl();
  125. void setAutoFitExtents( bool state );
  126. void setBitmap( const String& name );
  127. void setBitmapHandles( GFXTexHandle normal, GFXTexHandle highlighted, GFXTexHandle depressed, GFXTexHandle inactive );
  128. //Parent methods
  129. virtual bool onWake();
  130. virtual void onSleep();
  131. virtual void onAction();
  132. virtual void inspectPostApply();
  133. virtual void onRender(Point2I offset, const RectI &updateRect);
  134. static void initPersistFields();
  135. bool pointInControl(const Point2I& parentCoordPoint);
  136. DECLARE_CONOBJECT(GuiBitmapButtonCtrl);
  137. DECLARE_DESCRIPTION( "A button control rendered entirely from bitmaps.\n"
  138. "The individual button states are represented with separate bitmaps." );
  139. };
  140. typedef GuiBitmapButtonCtrl::BitmapMode GuiBitmapMode;
  141. DefineEnumType( GuiBitmapMode );
  142. /// Extension of GuiBitmapButtonCtrl that also display a text label on the button.
  143. class GuiBitmapButtonTextCtrl : public GuiBitmapButtonCtrl
  144. {
  145. public:
  146. typedef GuiBitmapButtonCtrl Parent;
  147. protected:
  148. virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
  149. public:
  150. DECLARE_CONOBJECT( GuiBitmapButtonTextCtrl );
  151. DECLARE_DESCRIPTION( "An extension of GuiBitmapButtonCtrl that also renders a text\n"
  152. "label on the button." );
  153. };
  154. #endif //_GUI_BITMAP_BUTTON_CTRL_H