guiToolboxButtonCtrl.cpp 7.0 KB

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