guiBitmapButtonCtrl.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. StringTableEntry mTextureNormalAssetId;
  77. AssetPtr<ImageAsset> mTextureNormalAsset;
  78. GFXTexHandle mTextureNormal;
  79. /// Texture for highlight state.
  80. StringTableEntry mTextureHilightAssetId;
  81. AssetPtr<ImageAsset> mTextureHilightAsset;
  82. GFXTexHandle mTextureHilight;
  83. /// Texture for depressed state.
  84. StringTableEntry mTextureDepressedAssetId;
  85. AssetPtr<ImageAsset> mTextureDepressedAsset;
  86. GFXTexHandle mTextureDepressed;
  87. /// Texture for inactive state.
  88. StringTableEntry mTextureInactiveAssetId;
  89. AssetPtr<ImageAsset> mTextureInactiveAsset;
  90. GFXTexHandle mTextureInactive;
  91. };
  92. /// Make control extents equal to bitmap size.
  93. bool mAutoFitExtents;
  94. /// Allow switching out images according to modifier presses.
  95. bool mUseModifiers;
  96. /// Allow switching images according to mouse states. On by default.
  97. /// Switch off when not needed as it otherwise results in a lot of costly
  98. /// texture loads.
  99. bool mUseStates;
  100. ///
  101. BitmapMode mBitmapMode;
  102. DECLARE_IMAGEASSET(GuiBitmapButtonCtrl, Bitmap, onBitmapChange, GFXDefaultGUIProfile);
  103. DECLARE_ASSET_SETGET(GuiBitmapButtonCtrl, Bitmap);
  104. /// alpha masking
  105. bool mMasked;
  106. ///
  107. Textures mTextures[ NumModifiers ];
  108. ColorI mColor;
  109. virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
  110. static bool _setAutoFitExtents( void *object, const char *index, const char *data );
  111. //static bool _setBitmap( void *object, const char *index, const char *data );
  112. State getState() const
  113. {
  114. if( mActive )
  115. {
  116. if( mDepressed || mStateOn ) return DEPRESSED;
  117. if( mHighlighted ) return HILIGHT;
  118. return NORMAL;
  119. }
  120. else
  121. return INACTIVE;
  122. }
  123. Modifier getCurrentModifier();
  124. GFXTexHandle& getTextureForCurrentState();
  125. /// @name Callbacks
  126. /// @{
  127. DECLARE_CALLBACK( void, onDefaultClick, () );
  128. DECLARE_CALLBACK( void, onCtrlClick, () );
  129. DECLARE_CALLBACK( void, onAltClick, () );
  130. DECLARE_CALLBACK( void, onShiftClick, () );
  131. /// @}
  132. void onBitmapChange()
  133. {
  134. setBitmap(getBitmap());
  135. }
  136. public:
  137. GuiBitmapButtonCtrl();
  138. void setAutoFitExtents( bool state );
  139. void setBitmap( StringTableEntry name );
  140. void setBitmapHandles( GFXTexHandle normal, GFXTexHandle highlighted, GFXTexHandle depressed, GFXTexHandle inactive );
  141. //Parent methods
  142. virtual bool onWake();
  143. virtual void onSleep();
  144. virtual void onAction();
  145. virtual void inspectPostApply();
  146. virtual void onRender(Point2I offset, const RectI &updateRect);
  147. static void initPersistFields();
  148. bool pointInControl(const Point2I& parentCoordPoint);
  149. DECLARE_CONOBJECT(GuiBitmapButtonCtrl);
  150. DECLARE_DESCRIPTION( "A button control rendered entirely from bitmaps.\n"
  151. "The individual button states are represented with separate bitmaps." );
  152. //Basically a wrapper function to do our special state handling setup when the fields change
  153. static bool _setBitmapFieldData(void* obj, const char* index, const char* data)
  154. {
  155. GuiBitmapButtonCtrl* object = static_cast<GuiBitmapButtonCtrl*>(obj);
  156. object->setBitmap(StringTable->insert(data));
  157. return false;
  158. }
  159. };
  160. typedef GuiBitmapButtonCtrl::BitmapMode GuiBitmapMode;
  161. DefineEnumType( GuiBitmapMode );
  162. /// Extension of GuiBitmapButtonCtrl that also display a text label on the button.
  163. class GuiBitmapButtonTextCtrl : public GuiBitmapButtonCtrl
  164. {
  165. public:
  166. typedef GuiBitmapButtonCtrl Parent;
  167. protected:
  168. virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
  169. public:
  170. DECLARE_CONOBJECT( GuiBitmapButtonTextCtrl );
  171. DECLARE_DESCRIPTION( "An extension of GuiBitmapButtonCtrl that also renders a text\n"
  172. "label on the button." );
  173. };
  174. #endif //_GUI_BITMAP_BUTTON_CTRL_H