guiToolboxButtonCtrl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include "platform/platform.h"
  23. #include "gui/buttons/guiToolboxButtonCtrl.h"
  24. #include "console/console.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "console/consoleTypes.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "gui/core/guiDefaultControlRender.h"
  30. IMPLEMENT_CONOBJECT(GuiToolboxButtonCtrl);
  31. ConsoleDocClass( GuiToolboxButtonCtrl,
  32. "@brief Unimplemented GUI control meant to interact with Toolbox.\n\n"
  33. "For Torque 3D editors only, soon to be deprecated\n\n"
  34. "@internal"
  35. );
  36. //-------------------------------------
  37. GuiToolboxButtonCtrl::GuiToolboxButtonCtrl()
  38. {
  39. mNormalBitmapName = StringTable->insert("");
  40. mLoweredBitmapName = StringTable->insert("sceneeditor/client/images/buttondown");
  41. mHoverBitmapName = StringTable->insert("sceneeditor/client/images/buttonup");
  42. setMinExtent(Point2I(16,16));
  43. setExtent(48, 48);
  44. mButtonType = ButtonTypeRadio;
  45. mTipHoverTime = 100;
  46. }
  47. //-------------------------------------
  48. void GuiToolboxButtonCtrl::initPersistFields()
  49. {
  50. addField("normalBitmap", TypeFilename, Offset(mNormalBitmapName, GuiToolboxButtonCtrl));
  51. addField("loweredBitmap", TypeFilename, Offset(mLoweredBitmapName, GuiToolboxButtonCtrl));
  52. addField("hoverBitmap", TypeFilename, Offset(mHoverBitmapName, GuiToolboxButtonCtrl));
  53. Parent::initPersistFields();
  54. }
  55. //-------------------------------------
  56. bool GuiToolboxButtonCtrl::onWake()
  57. {
  58. if (! Parent::onWake())
  59. return false;
  60. setActive( true );
  61. setNormalBitmap( mNormalBitmapName );
  62. setLoweredBitmap( mLoweredBitmapName );
  63. setHoverBitmap( mHoverBitmapName );
  64. return true;
  65. }
  66. //-------------------------------------
  67. void GuiToolboxButtonCtrl::onSleep()
  68. {
  69. mTextureNormal = NULL;
  70. mTextureLowered = NULL;
  71. mTextureHover = NULL;
  72. Parent::onSleep();
  73. }
  74. //-------------------------------------
  75. ConsoleMethod( GuiToolboxButtonCtrl, setNormalBitmap, void, 3, 3, "( filepath name ) sets the bitmap that shows when the button is active")
  76. {
  77. object->setNormalBitmap(argv[2]);
  78. }
  79. ConsoleMethod( GuiToolboxButtonCtrl, setLoweredBitmap, void, 3, 3, "( filepath name ) sets the bitmap that shows when the button is disabled")
  80. {
  81. object->setLoweredBitmap(argv[2]);
  82. }
  83. ConsoleMethod( GuiToolboxButtonCtrl, setHoverBitmap, void, 3, 3, "( filepath name ) sets the bitmap that shows when the button is disabled")
  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 ((getWidth() == 0) && (getHeight() == 0) && mTextureNormal)
  94. {
  95. setExtent( mTextureNormal->getWidth(), mTextureNormal->getHeight());
  96. }
  97. }
  98. //-------------------------------------
  99. void GuiToolboxButtonCtrl::setNormalBitmap( StringTableEntry bitmapName )
  100. {
  101. mNormalBitmapName = StringTable->insert( bitmapName );
  102. if(!isAwake())
  103. return;
  104. if ( *mNormalBitmapName )
  105. mTextureNormal = GFXTexHandle( mNormalBitmapName, &GFXDefaultPersistentProfile, avar("%s() - mTextureNormal (line %d)", __FUNCTION__, __LINE__) );
  106. else
  107. mTextureNormal = NULL;
  108. setUpdate();
  109. }
  110. void GuiToolboxButtonCtrl::setLoweredBitmap( StringTableEntry bitmapName )
  111. {
  112. mLoweredBitmapName = StringTable->insert( bitmapName );
  113. if(!isAwake())
  114. return;
  115. if ( *mLoweredBitmapName )
  116. mTextureLowered = GFXTexHandle( mLoweredBitmapName, &GFXDefaultPersistentProfile, avar("%s() - mTextureLowered (line %d)", __FUNCTION__, __LINE__) );
  117. else
  118. mTextureLowered = NULL;
  119. setUpdate();
  120. }
  121. void GuiToolboxButtonCtrl::setHoverBitmap( StringTableEntry bitmapName )
  122. {
  123. mHoverBitmapName = StringTable->insert( bitmapName );
  124. if(!isAwake())
  125. return;
  126. if ( *mHoverBitmapName )
  127. mTextureHover = GFXTexHandle( mHoverBitmapName, &GFXDefaultPersistentProfile, avar("%s() - mTextureHover (line %d)", __FUNCTION__, __LINE__) );
  128. else
  129. mTextureHover = NULL;
  130. setUpdate();
  131. }
  132. //-------------------------------------
  133. void GuiToolboxButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  134. {
  135. // Only render the state rect (hover/down) if we're active
  136. if (mActive)
  137. {
  138. RectI r(offset, getExtent());
  139. if ( mDepressed || mStateOn )
  140. renderStateRect( mTextureLowered , r );
  141. else if ( mMouseOver )
  142. renderStateRect( mTextureHover , r );
  143. }
  144. // Now render the image
  145. if( mTextureNormal )
  146. {
  147. renderButton( mTextureNormal, offset, updateRect );
  148. return;
  149. }
  150. Point2I textPos = offset;
  151. if( mDepressed )
  152. textPos += Point2I(1,1);
  153. // Make sure we take the profile's textOffset into account.
  154. textPos += mProfile->mTextOffset;
  155. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  156. renderJustifiedText(textPos, getExtent(), mButtonText);
  157. }
  158. void GuiToolboxButtonCtrl::renderStateRect( GFXTexHandle &texture, const RectI& rect )
  159. {
  160. if (texture)
  161. {
  162. GFX->getDrawUtil()->clearBitmapModulation();
  163. GFX->getDrawUtil()->drawBitmapStretch( texture, rect );
  164. }
  165. }
  166. //------------------------------------------------------------------------------
  167. void GuiToolboxButtonCtrl::renderButton(GFXTexHandle &texture, Point2I &offset, const RectI& updateRect)
  168. {
  169. if (texture)
  170. {
  171. Point2I finalOffset = offset;
  172. finalOffset.x += ( ( getWidth() / 2 ) - ( texture.getWidth() / 2 ) );
  173. finalOffset.y += ( ( getHeight() / 2 ) - ( texture.getHeight() / 2 ) );
  174. GFX->getDrawUtil()->clearBitmapModulation();
  175. GFX->getDrawUtil()->drawBitmap(texture, finalOffset);
  176. renderChildControls( offset, updateRect);
  177. }
  178. }