guiSeparatorCtrl.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/editor/guiSeparatorCtrl.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "console/console.h"
  27. #include "console/consoleTypes.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "gui/core/guiDefaultControlRender.h"
  30. IMPLEMENT_CONOBJECT(GuiSeparatorCtrl);
  31. ConsoleDocClass( GuiSeparatorCtrl,
  32. "@brief A control that renders a horizontal or vertical separator with "
  33. "an optional text label (horizontal only)\n\n"
  34. "@tsexample\n"
  35. "new GuiSeparatorCtrl()\n"
  36. "{\n"
  37. " profile = \"GuiDefaultProfile\";\n"
  38. " position = \"505 0\";\n"
  39. " extent = \"10 17\";\n"
  40. " minExtent = \"10 17\";\n"
  41. " canSave = \"1\";\n"
  42. " visible = \"1\";\n"
  43. " horizSizing = \"left\";\n"
  44. "};\n"
  45. "@endtsexample\n\n"
  46. "@ingroup GuiControls\n");
  47. ImplementEnumType( GuiSeparatorType,
  48. "GuiSeparatorCtrl orientations\n\n"
  49. "@ingroup GuiControls" )
  50. { GuiSeparatorCtrl::separatorTypeVertical, "Vertical" },
  51. { GuiSeparatorCtrl::separatorTypeHorizontal,"Horizontal" }
  52. EndImplementEnumType;
  53. //--------------------------------------------------------------------------
  54. GuiSeparatorCtrl::GuiSeparatorCtrl() : GuiControl()
  55. {
  56. mInvisible = false;
  57. mTextLeftMargin = 0;
  58. mMargin = 2;
  59. setExtent( 12, 35 );
  60. mSeparatorType = GuiSeparatorCtrl::separatorTypeVertical;
  61. }
  62. //--------------------------------------------------------------------------
  63. void GuiSeparatorCtrl::initPersistFields()
  64. {
  65. addField("caption", TypeRealString, Offset(mText, GuiSeparatorCtrl),
  66. "Optional text label to display." );
  67. addField("type", TYPEID< separatorTypeOptions >(), Offset(mSeparatorType, GuiSeparatorCtrl),
  68. "Orientation of separator." );
  69. addField("borderMargin", TypeS32, Offset(mMargin, GuiSeparatorCtrl));
  70. addField("invisible", TypeBool, Offset(mInvisible, GuiSeparatorCtrl));// Nonsense. Should use GuiControl's visibility.
  71. addField("leftMargin", TypeS32, Offset(mTextLeftMargin, GuiSeparatorCtrl),
  72. "Left margin of text label." );
  73. Parent::initPersistFields();
  74. }
  75. //--------------------------------------------------------------------------
  76. void GuiSeparatorCtrl::onRender(Point2I offset, const RectI &updateRect)
  77. {
  78. Parent::onRender( offset, updateRect );
  79. if( mInvisible )
  80. return;
  81. if( mText.isNotEmpty() && mSeparatorType != separatorTypeVertical )
  82. {
  83. // If text is present and we have a left margin, then draw some separator, then the
  84. // text, and then the rest of the separator.
  85. S32 posx = offset.x + mMargin;
  86. S32 fontheight = mProfile->mFont->getHeight();
  87. S32 seppos = (fontheight - 2) / 2 + offset.y;
  88. if( mTextLeftMargin > 0 )
  89. {
  90. RectI rect( Point2I( posx, seppos ), Point2I( mTextLeftMargin, 2 ) );
  91. renderSlightlyLoweredBox(rect, mProfile);
  92. posx += mTextLeftMargin;
  93. }
  94. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  95. posx = GFX->getDrawUtil()->drawText(mProfile->mFont, Point2I(posx,offset.y), mText, mProfile->mFontColors);
  96. RectI rect( Point2I( posx, seppos ), Point2I( getWidth() - posx + offset.x, 2 ) );
  97. // Space text and separator a bit apart at right end.
  98. rect.point.x += 2;
  99. rect.extent.x -= 2;
  100. if( rect.extent.x > 0 )
  101. renderSlightlyLoweredBox( rect, mProfile );
  102. }
  103. else
  104. {
  105. if( mSeparatorType == separatorTypeHorizontal )
  106. {
  107. S32 seppos = getHeight() / 2 + offset.y;
  108. RectI rect(Point2I(offset.x + mMargin ,seppos),Point2I(getWidth() - (mMargin * 2),2));
  109. renderSlightlyLoweredBox(rect, mProfile);
  110. }
  111. else
  112. {
  113. S32 seppos = getWidth() / 2 + offset.x;
  114. RectI rect(Point2I(seppos, offset.y + mMargin),Point2I(2, getHeight() - (mMargin * 2)));
  115. renderSlightlyLoweredBox(rect, mProfile);
  116. }
  117. }
  118. renderChildControls(offset, updateRect);
  119. }