2
0

guiToolboxButtonCtrl.cpp 5.4 KB

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