guiBitmapButtonCtrl.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. ///
  97. Textures mTextures[ NumModifiers ];
  98. virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
  99. static bool _setAutoFitExtents( void *object, const char *index, const char *data );
  100. static bool _setBitmap( void *object, const char *index, const char *data );
  101. State getState() const
  102. {
  103. if( mActive )
  104. {
  105. if( mDepressed || mStateOn ) return DEPRESSED;
  106. if( mMouseOver ) return HILIGHT;
  107. return NORMAL;
  108. }
  109. else
  110. return INACTIVE;
  111. }
  112. Modifier getCurrentModifier();
  113. GFXTexHandle& getTextureForCurrentState();
  114. /// @name Callbacks
  115. /// @{
  116. DECLARE_CALLBACK( void, onDefaultClick, () );
  117. DECLARE_CALLBACK( void, onCtrlClick, () );
  118. DECLARE_CALLBACK( void, onAltClick, () );
  119. DECLARE_CALLBACK( void, onShiftClick, () );
  120. /// @}
  121. public:
  122. GuiBitmapButtonCtrl();
  123. void setAutoFitExtents( bool state );
  124. void setBitmap( const String& name );
  125. void setBitmapHandles( GFXTexHandle normal, GFXTexHandle highlighted, GFXTexHandle depressed, GFXTexHandle inactive );
  126. //Parent methods
  127. virtual bool onWake();
  128. virtual void onSleep();
  129. virtual void onAction();
  130. virtual void inspectPostApply();
  131. virtual void onRender(Point2I offset, const RectI &updateRect);
  132. static void initPersistFields();
  133. DECLARE_CONOBJECT(GuiBitmapButtonCtrl);
  134. DECLARE_DESCRIPTION( "A button control rendered entirely from bitmaps.\n"
  135. "The individual button states are represented with separate bitmaps." );
  136. };
  137. typedef GuiBitmapButtonCtrl::BitmapMode GuiBitmapMode;
  138. DefineEnumType( GuiBitmapMode );
  139. /// Extension of GuiBitmapButtonCtrl that also display a text label on the button.
  140. class GuiBitmapButtonTextCtrl : public GuiBitmapButtonCtrl
  141. {
  142. public:
  143. typedef GuiBitmapButtonCtrl Parent;
  144. protected:
  145. virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
  146. public:
  147. DECLARE_CONOBJECT( GuiBitmapButtonTextCtrl );
  148. DECLARE_DESCRIPTION( "An extension of GuiBitmapButtonCtrl that also renders a text\n"
  149. "label on the button." );
  150. };
  151. #endif //_GUI_BITMAP_BUTTON_CTRL_H