guiToolboxButtonCtrl.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. INIT_ASSET(NormalBitmap);
  41. INIT_ASSET(LoweredBitmap);
  42. INIT_ASSET(HoverBitmap);
  43. setMinExtent(Point2I(16,16));
  44. setExtent(48, 48);
  45. mButtonType = ButtonTypeRadio;
  46. mTipHoverTime = 100;
  47. }
  48. //-------------------------------------
  49. void GuiToolboxButtonCtrl::initPersistFields()
  50. {
  51. docsURL;
  52. INITPERSISTFIELD_IMAGEASSET(NormalBitmap, GuiToolboxButtonCtrl, "");
  53. INITPERSISTFIELD_IMAGEASSET(LoweredBitmap, GuiToolboxButtonCtrl, "");
  54. INITPERSISTFIELD_IMAGEASSET(HoverBitmap, GuiToolboxButtonCtrl, "");
  55. Parent::initPersistFields();
  56. }
  57. //-------------------------------------
  58. bool GuiToolboxButtonCtrl::onWake()
  59. {
  60. if (! Parent::onWake())
  61. return false;
  62. setActive( true );
  63. setNormalBitmap( getNormalBitmap() );
  64. setLoweredBitmap( getLoweredBitmap() );
  65. setHoverBitmap( getHoverBitmap() );
  66. return true;
  67. }
  68. //-------------------------------------
  69. void GuiToolboxButtonCtrl::onSleep()
  70. {
  71. Parent::onSleep();
  72. }
  73. //-------------------------------------
  74. void GuiToolboxButtonCtrl::inspectPostApply()
  75. {
  76. // if the extent is set to (0,0) in the gui editor and appy hit, this control will
  77. // set it's extent to be exactly the size of the normal bitmap (if present)
  78. Parent::inspectPostApply();
  79. if ((getWidth() == 0) && (getHeight() == 0) && mNormalBitmap)
  80. {
  81. setExtent(mNormalBitmap->getWidth(), mNormalBitmap->getHeight());
  82. }
  83. }
  84. //-------------------------------------
  85. void GuiToolboxButtonCtrl::setNormalBitmap( StringTableEntry bitmapName )
  86. {
  87. _setNormalBitmap(bitmapName);
  88. if(!isAwake())
  89. return;
  90. setUpdate();
  91. }
  92. void GuiToolboxButtonCtrl::setLoweredBitmap( StringTableEntry bitmapName )
  93. {
  94. _setLoweredBitmap(bitmapName);
  95. if(!isAwake())
  96. return;
  97. setUpdate();
  98. }
  99. void GuiToolboxButtonCtrl::setHoverBitmap( StringTableEntry bitmapName )
  100. {
  101. _setHoverBitmap(bitmapName);
  102. if(!isAwake())
  103. return;
  104. setUpdate();
  105. }
  106. //-------------------------------------
  107. void GuiToolboxButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  108. {
  109. // Only render the state rect (hover/down) if we're active
  110. if (mActive)
  111. {
  112. RectI r(offset, getExtent());
  113. if ( mDepressed || mStateOn )
  114. renderStateRect( mLoweredBitmap , r );
  115. else if ( mHighlighted )
  116. renderStateRect( mHoverBitmap , r );
  117. }
  118. // Now render the image
  119. if( mNormalBitmap )
  120. {
  121. renderButton(mNormalBitmap, offset, updateRect );
  122. return;
  123. }
  124. Point2I textPos = offset;
  125. if( mDepressed )
  126. textPos += Point2I(1,1);
  127. // Make sure we take the profile's textOffset into account.
  128. textPos += mProfile->mTextOffset;
  129. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  130. renderJustifiedText(textPos, getExtent(), mButtonText);
  131. }
  132. void GuiToolboxButtonCtrl::renderStateRect( GFXTexHandle &texture, const RectI& rect )
  133. {
  134. if (texture)
  135. {
  136. GFX->getDrawUtil()->clearBitmapModulation();
  137. GFX->getDrawUtil()->drawBitmapStretch( texture, rect );
  138. }
  139. }
  140. //------------------------------------------------------------------------------
  141. void GuiToolboxButtonCtrl::renderButton(GFXTexHandle &texture, Point2I &offset, const RectI& updateRect)
  142. {
  143. if (texture)
  144. {
  145. Point2I finalOffset = offset;
  146. finalOffset.x += ( ( getWidth() / 2 ) - ( texture.getWidth() / 2 ) );
  147. finalOffset.y += ( ( getHeight() / 2 ) - ( texture.getHeight() / 2 ) );
  148. GFX->getDrawUtil()->clearBitmapModulation();
  149. GFX->getDrawUtil()->drawBitmap(texture, finalOffset);
  150. renderChildControls( offset, updateRect);
  151. }
  152. }
  153. DEF_ASSET_BINDS(GuiToolboxButtonCtrl, NormalBitmap);
  154. DEF_ASSET_BINDS(GuiToolboxButtonCtrl, LoweredBitmap);
  155. DEF_ASSET_BINDS(GuiToolboxButtonCtrl, HoverBitmap);