guiSwatchButtonCtrl.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/guiSwatchButtonCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. #include "gui/core/guiCanvas.h"
  30. #include "gui/core/guiDefaultControlRender.h"
  31. IMPLEMENT_CONOBJECT( GuiSwatchButtonCtrl );
  32. ConsoleDocClass( GuiSwatchButtonCtrl,
  33. "@brief A button that is used to represent color; often used in correlation with a color picker.\n\n"
  34. "A swatch button is a push button that uses its color field to designate the color drawn over an image, on top of a button.\n\n"
  35. "The color itself is a float value stored inside the GuiSwatchButtonCtrl::color field. The texture path that represents\n"
  36. "the image underlying the color is stored inside the GuiSwatchButtonCtrl::gridBitmap field.\n"
  37. "The default value assigned toGuiSwatchButtonCtrl::color is \"1 1 1 1\"( White ). The default/fallback image assigned to \n"
  38. "GuiSwatchButtonCtrl::gridBitmap is \"tools/gui/images/transp_grid\".\n\n"
  39. "@tsexample\n"
  40. "// Create a GuiSwatchButtonCtrl that calls randomFunction with its current color when clicked\n"
  41. "%swatchButton = new GuiSwatchButtonCtrl()\n"
  42. "{\n"
  43. " profile = \"GuiInspectorSwatchButtonProfile\";\n"
  44. " command = \"randomFunction( $ThisControl.color );\";\n"
  45. "};\n"
  46. "@endtsexample\n\n"
  47. "@ingroup GuiButtons"
  48. );
  49. //-----------------------------------------------------------------------------
  50. GuiSwatchButtonCtrl::GuiSwatchButtonCtrl()
  51. : mSwatchColor(1, 1, 1, 1)
  52. {
  53. mButtonText = StringTable->insert( "" );
  54. setExtent(140, 30);
  55. static StringTableEntry sProfile = StringTable->insert( "profile" );
  56. setDataField( sProfile, NULL, "GuiInspectorSwatchButtonProfile" );
  57. mGridBitmap = "tools/gui/images/transp_grid";
  58. }
  59. void GuiSwatchButtonCtrl::initPersistFields()
  60. {
  61. addField("color", TypeColorF, Offset(mSwatchColor, GuiSwatchButtonCtrl), "The foreground color of GuiSwatchButtonCtrl");
  62. addField( "gridBitmap", TypeRealString, Offset( mGridBitmap, GuiSwatchButtonCtrl ), "The bitmap used for the transparent grid" );
  63. Parent::initPersistFields();
  64. }
  65. bool GuiSwatchButtonCtrl::onWake()
  66. {
  67. if ( !Parent::onWake() )
  68. return false;
  69. if ( mGrid.isNull() )
  70. mGrid.set( mGridBitmap, &GFXDefaultGUIProfile, avar("%s() - mGrid (line %d)", __FUNCTION__, __LINE__) );
  71. return true;
  72. }
  73. void GuiSwatchButtonCtrl::onRender( Point2I offset, const RectI &updateRect )
  74. {
  75. bool highlight = mHighlighted;
  76. ColorI borderColor = mActive ? ( highlight ? mProfile->mBorderColorHL : mProfile->mBorderColor ) : mProfile->mBorderColorNA;
  77. RectI renderRect( offset, getExtent() );
  78. if ( !highlight )
  79. renderRect.inset( 1, 1 );
  80. GFXDrawUtil *drawer = GFX->getDrawUtil();
  81. drawer->clearBitmapModulation();
  82. // Draw background transparency grid texture...
  83. if ( mGrid.isValid() )
  84. drawer->drawBitmapStretch( mGrid, renderRect );
  85. // Draw swatch color as fill...
  86. drawer->drawRectFill(renderRect, mSwatchColor.toColorI());
  87. // Draw any borders...
  88. drawer->drawRect( renderRect, borderColor );
  89. }
  90. //-----------------------------------------------------------------------------
  91. DefineEngineMethod( GuiSwatchButtonCtrl, setColor, void, ( const char* newColor ),,
  92. "Set the color of the swatch control.\n"
  93. "@param newColor The new color string given to the swatch control in float format \"r g b a\".\n"
  94. "@note It's also important to note that when setColor is called causes\n"
  95. "the control's altCommand field to be executed." )
  96. {
  97. object->setField( "color", newColor );
  98. object->execAltConsoleCallback();
  99. }