guiToolboxButtonCtrl.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #include "console/console.h"
  23. #include "graphics/dgl.h"
  24. #include "console/consoleTypes.h"
  25. #include "platform/platformAudio.h"
  26. #include "gui/guiCanvas.h"
  27. #include "gui/guiDefaultControlRender.h"
  28. #include "gui/buttons/guiToolboxButtonCtrl.h"
  29. IMPLEMENT_CONOBJECT(GuiToolboxButtonCtrl);
  30. //-------------------------------------
  31. GuiToolboxButtonCtrl::GuiToolboxButtonCtrl()
  32. {
  33. mNormalBitmapName = StringTable->EmptyString;
  34. mLoweredBitmapName = StringTable->insert("sceneeditor/client/images/buttondown");
  35. mHoverBitmapName = StringTable->insert("sceneeditor/client/images/buttonup");
  36. mMinExtent.set( 16, 16 );
  37. mBounds.extent.set(48, 48);
  38. mButtonType = ButtonTypeRadio;
  39. mTipHoverTime = 100;
  40. }
  41. //-------------------------------------
  42. void GuiToolboxButtonCtrl::initPersistFields()
  43. {
  44. Parent::initPersistFields();
  45. addField("normalBitmap", TypeFilename, Offset(mNormalBitmapName, GuiToolboxButtonCtrl));
  46. addField("loweredBitmap", TypeFilename, Offset(mLoweredBitmapName, GuiToolboxButtonCtrl));
  47. addField("hoverBitmap", TypeFilename, Offset(mHoverBitmapName, GuiToolboxButtonCtrl));
  48. }
  49. //-------------------------------------
  50. bool GuiToolboxButtonCtrl::onWake()
  51. {
  52. if (! Parent::onWake())
  53. return false;
  54. setActive( true );
  55. setNormalBitmap( mNormalBitmapName );
  56. setLoweredBitmap( mLoweredBitmapName );
  57. setHoverBitmap( mHoverBitmapName );
  58. return true;
  59. }
  60. //-------------------------------------
  61. void GuiToolboxButtonCtrl::onSleep()
  62. {
  63. mTextureNormal = NULL;
  64. mTextureLowered = NULL;
  65. mTextureHover = NULL;
  66. Parent::onSleep();
  67. }
  68. //-------------------------------------
  69. ConsoleMethod( GuiToolboxButtonCtrl, setNormalBitmap, void, 3, 3, "( filepath name ) Sets the bitmap that shows when the button is active"
  70. "@param name The path of the desired bitmap file\n"
  71. "@return No Return Value.")
  72. {
  73. object->setNormalBitmap(argv[2]);
  74. }
  75. ConsoleMethod( GuiToolboxButtonCtrl, setLoweredBitmap, void, 3, 3, "( filepath name ) Sets the bitmap that shows when the button is disabled"
  76. "@param name The path of the desired bitmap file\n"
  77. "@return No Return Value.")
  78. {
  79. object->setLoweredBitmap(argv[2]);
  80. }
  81. ConsoleMethod( GuiToolboxButtonCtrl, setHoverBitmap, void, 3, 3, "( filepath name ) Sets the bitmap that shows when the button is disabled"
  82. "@param name The path of the desired bitmap file\n"
  83. "@return No Return Value.")
  84. {
  85. object->setHoverBitmap(argv[2]);
  86. }
  87. //-------------------------------------
  88. void GuiToolboxButtonCtrl::inspectPostApply()
  89. {
  90. // if the extent is set to (0,0) in the gui editor and appy hit, this control will
  91. // set it's extent to be exactly the size of the normal bitmap (if present)
  92. Parent::inspectPostApply();
  93. if ((mBounds.extent.x == 0) && (mBounds.extent.y == 0) && mTextureNormal)
  94. {
  95. TextureObject *texture = (TextureObject *) mTextureNormal;
  96. mBounds.extent.x = texture->getBitmapWidth();
  97. mBounds.extent.y = texture->getBitmapHeight();
  98. }
  99. }
  100. //-------------------------------------
  101. void GuiToolboxButtonCtrl::setNormalBitmap( StringTableEntry bitmapName )
  102. {
  103. mNormalBitmapName = StringTable->insert( bitmapName );
  104. if(!isAwake())
  105. return;
  106. if ( *mNormalBitmapName )
  107. mTextureNormal = TextureHandle( mNormalBitmapName, TextureHandle::BitmapTexture, true );
  108. else
  109. mTextureNormal = NULL;
  110. setUpdate();
  111. }
  112. void GuiToolboxButtonCtrl::setLoweredBitmap( StringTableEntry bitmapName )
  113. {
  114. mLoweredBitmapName = StringTable->insert( bitmapName );
  115. if(!isAwake())
  116. return;
  117. if ( *mLoweredBitmapName )
  118. mTextureLowered = TextureHandle( mLoweredBitmapName, TextureHandle::BitmapTexture, true );
  119. else
  120. mTextureLowered = NULL;
  121. setUpdate();
  122. }
  123. void GuiToolboxButtonCtrl::setHoverBitmap( StringTableEntry bitmapName )
  124. {
  125. mHoverBitmapName = StringTable->insert( bitmapName );
  126. if(!isAwake())
  127. return;
  128. if ( *mHoverBitmapName )
  129. mTextureHover = TextureHandle( mHoverBitmapName, TextureHandle::BitmapTexture, true );
  130. else
  131. mTextureHover = NULL;
  132. setUpdate();
  133. }
  134. //-------------------------------------
  135. void GuiToolboxButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  136. {
  137. // Only render the state rect (hover/down) if we're active
  138. if (mActive)
  139. {
  140. RectI r(offset, mBounds.extent);
  141. if ( mDepressed || mStateOn )
  142. renderStateRect( mTextureLowered , r );
  143. else if (mMouseOver)
  144. renderStateRect( mTextureHover , r );
  145. }
  146. // Now render the image
  147. if( mTextureNormal )
  148. {
  149. renderButton( mTextureNormal, offset, updateRect );
  150. return;
  151. }
  152. Point2I textPos = offset;
  153. if( mDepressed )
  154. textPos += Point2I(1,1);
  155. // Make sure we take the profile's textOffset into account.
  156. textPos += mProfile->mTextOffset;
  157. dglSetBitmapModulation( mProfile->mFontColor );
  158. renderJustifiedText(textPos, mBounds.extent, mButtonText);
  159. }
  160. void GuiToolboxButtonCtrl::renderStateRect( TextureHandle &texture, const RectI& rect )
  161. {
  162. if (texture)
  163. {
  164. dglClearBitmapModulation();
  165. dglDrawBitmapStretch( texture, rect );
  166. }
  167. }
  168. //------------------------------------------------------------------------------
  169. void GuiToolboxButtonCtrl::renderButton(TextureHandle &texture, Point2I &offset, const RectI& updateRect)
  170. {
  171. if (texture)
  172. {
  173. Point2I finalOffset = offset;
  174. finalOffset.x += ( ( mBounds.extent.x / 2 ) - ( texture.getWidth() / 2 ) );
  175. finalOffset.y += ( ( mBounds.extent.y / 2 ) - ( texture.getHeight() / 2 ) );
  176. dglClearBitmapModulation();
  177. dglDrawBitmap(texture, finalOffset);
  178. renderChildControls( offset, updateRect);
  179. }
  180. }