guiBitmapButtonCtrl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, protected AssetPtrCallback
  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. private:
  103. AssetPtr<ImageAsset> mBitmapAsset;
  104. String mBitmapFile;
  105. public:
  106. void _setBitmap(StringTableEntry _in) {
  107. if (mBitmapAsset.getAssetId() == _in) return; if (!AssetDatabase.isDeclaredAsset(_in)) {
  108. StringTableEntry imageAssetId = ImageAsset::smNoImageAssetFallback; AssetQuery query; S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); if (foundAssetcount != 0) {
  109. imageAssetId = query.mAssetList[0];
  110. } mBitmapAsset = imageAssetId;
  111. }
  112. else {
  113. mBitmapAsset = _in;
  114. mBitmapName = _in;
  115. mBitmap = getBitmap();
  116. }
  117. }; inline StringTableEntry _getBitmap(void) const {
  118. return mBitmapAsset.getAssetId();
  119. } GFXTexHandle getBitmap() {
  120. return mBitmapAsset.notNull() ? mBitmapAsset->getTexture(&GFXDefaultGUIProfile) : 0;
  121. } AssetPtr<ImageAsset> getBitmapAsset(void) {
  122. return mBitmapAsset;
  123. } static bool _setBitmapData(void* obj, const char* index, const char* data) {
  124. static_cast<GuiBitmapButtonCtrl*>(obj)->_setBitmap(_getStringTable()->insert(data)); return false;
  125. }
  126. StringTableEntry getBitmapFile() { return mBitmapAsset.notNull() ? mBitmapAsset->getImageFile() : ""; }
  127. protected:
  128. void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
  129. {
  130. setBitmap(mBitmapName);
  131. }
  132. GFXTexHandle mBitmap;
  133. StringTableEntry mBitmapName;
  134. /// alpha masking
  135. bool mMasked;
  136. ///
  137. Textures mTextures[ NumModifiers ];
  138. ColorI mColor;
  139. virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
  140. static bool _setAutoFitExtents( void *object, const char *index, const char *data );
  141. //static bool _setBitmap( void *object, const char *index, const char *data );
  142. State getState() const
  143. {
  144. if( mActive )
  145. {
  146. if( mDepressed || mStateOn ) return DEPRESSED;
  147. if( mHighlighted ) return HILIGHT;
  148. return NORMAL;
  149. }
  150. else
  151. return INACTIVE;
  152. }
  153. Modifier getCurrentModifier();
  154. GFXTexHandle& getTextureForCurrentState();
  155. /// @name Callbacks
  156. /// @{
  157. DECLARE_CALLBACK( void, onDefaultClick, () );
  158. DECLARE_CALLBACK( void, onCtrlClick, () );
  159. DECLARE_CALLBACK( void, onAltClick, () );
  160. DECLARE_CALLBACK( void, onShiftClick, () );
  161. /// @}
  162. public:
  163. GuiBitmapButtonCtrl();
  164. void setAutoFitExtents( bool state );
  165. void setBitmap( StringTableEntry name );
  166. void setBitmapHandles( GFXTexHandle normal, GFXTexHandle highlighted, GFXTexHandle depressed, GFXTexHandle inactive );
  167. //Parent methods
  168. bool onWake() override;
  169. void onSleep() override;
  170. void onAction() override;
  171. void inspectPostApply() override;
  172. void onRender(Point2I offset, const RectI &updateRect) override;
  173. static void initPersistFields();
  174. bool pointInControl(const Point2I& parentCoordPoint) override;
  175. DECLARE_CONOBJECT(GuiBitmapButtonCtrl);
  176. DECLARE_DESCRIPTION( "A button control rendered entirely from bitmaps.\n"
  177. "The individual button states are represented with separate bitmaps." );
  178. };
  179. typedef GuiBitmapButtonCtrl::BitmapMode GuiBitmapMode;
  180. DefineEnumType( GuiBitmapMode );
  181. /// Extension of GuiBitmapButtonCtrl that also display a text label on the button.
  182. class GuiBitmapButtonTextCtrl : public GuiBitmapButtonCtrl
  183. {
  184. public:
  185. typedef GuiBitmapButtonCtrl Parent;
  186. protected:
  187. void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect ) override;
  188. public:
  189. DECLARE_CONOBJECT( GuiBitmapButtonTextCtrl );
  190. DECLARE_DESCRIPTION( "An extension of GuiBitmapButtonCtrl that also renders a text\n"
  191. "label on the button." );
  192. };
  193. #endif //_GUI_BITMAP_BUTTON_CTRL_H