guiSeparatorCtrl.cpp 5.2 KB

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