guiToolboxButtonCtrl.cpp 5.5 KB

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