guiButtonCtrl.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/guiButtonCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "gui/core/guiDefaultControlRender.h"
  30. IMPLEMENT_CONOBJECT(GuiButtonCtrl);
  31. ConsoleDocClass( GuiButtonCtrl,
  32. "@brief The most widely used button class.\n\n"
  33. "GuiButtonCtrl renders seperately of, but utilizes all of the functionality of GuiBaseButtonCtrl.\n"
  34. "This grants GuiButtonCtrl the versatility to be either of the 3 button types.\n\n"
  35. "@tsexample\n"
  36. "// Create a PushButton GuiButtonCtrl that calls randomFunction when clicked\n"
  37. "%button = new GuiButtonCtrl()\n"
  38. "{\n"
  39. " profile = \"GuiButtonProfile\";\n"
  40. " buttonType = \"PushButton\";\n"
  41. " command = \"randomFunction();\";\n"
  42. "};\n"
  43. "@endtsexample\n\n"
  44. "@ingroup GuiButtons"
  45. );
  46. //-----------------------------------------------------------------------------
  47. GuiButtonCtrl::GuiButtonCtrl()
  48. {
  49. setExtent(140, 30);
  50. mButtonText = StringTable->EmptyString();
  51. mHasTheme = false;
  52. }
  53. //-----------------------------------------------------------------------------
  54. bool GuiButtonCtrl::onWake()
  55. {
  56. if( !Parent::onWake() )
  57. return false;
  58. // Button Theme?
  59. if( mProfile->constructBitmapArray() >= 36 )
  60. mHasTheme = true;
  61. else
  62. mHasTheme = false;
  63. return true;
  64. }
  65. //-----------------------------------------------------------------------------
  66. void GuiButtonCtrl::onRender(Point2I offset,
  67. const RectI& updateRect)
  68. {
  69. bool highlight = mHighlighted;
  70. bool depressed = mDepressed;
  71. ColorI fontColor = mActive ? ( highlight ? mProfile->mFontColorHL : mProfile->mFontColor ) : mProfile->mFontColorNA;
  72. ColorI fillColor = mActive ? ( highlight ? mProfile->mFillColorHL : mProfile->mFillColor ) : mProfile->mFillColorNA;
  73. ColorI borderColor = mActive ? ( highlight ? mProfile->mBorderColorHL : mProfile->mBorderColor ) : mProfile->mBorderColorNA;
  74. RectI boundsRect(offset, getExtent());
  75. if( !mHasTheme )
  76. {
  77. if( mProfile->mBorder != 0 )
  78. renderFilledBorder( boundsRect, borderColor, fillColor, mProfile->mBorderThickness );
  79. else
  80. GFX->getDrawUtil()->drawRectFill( boundsRect, fillColor );
  81. }
  82. else
  83. {
  84. S32 indexMultiplier = 1;
  85. if( !mActive )
  86. indexMultiplier = 4;
  87. else if ( mDepressed || mStateOn )
  88. indexMultiplier = 2;
  89. else if ( mHighlighted )
  90. indexMultiplier = 3;
  91. renderSizableBitmapBordersFilled( boundsRect, indexMultiplier, mProfile );
  92. }
  93. Point2I textPos = offset;
  94. if( depressed )
  95. textPos += Point2I( 1, 1 );
  96. GFX->getDrawUtil()->setBitmapModulation( fontColor );
  97. renderJustifiedText( textPos, getExtent(), mButtonText );
  98. //render the children
  99. renderChildControls( offset, updateRect);
  100. }