guiSeparatorCtrl.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. docsURL;
  66. addField("caption", TypeRealString, Offset(mText, GuiSeparatorCtrl),
  67. "Optional text label to display." );
  68. addField("type", TYPEID< separatorTypeOptions >(), Offset(mSeparatorType, GuiSeparatorCtrl),
  69. "Orientation of separator." );
  70. addField("borderMargin", TypeS32, Offset(mMargin, GuiSeparatorCtrl));
  71. addField("invisible", TypeBool, Offset(mInvisible, GuiSeparatorCtrl));// Nonsense. Should use GuiControl's visibility.
  72. addField("leftMargin", TypeS32, Offset(mTextLeftMargin, GuiSeparatorCtrl),
  73. "Left margin of text label." );
  74. Parent::initPersistFields();
  75. }
  76. //--------------------------------------------------------------------------
  77. void GuiSeparatorCtrl::onRender(Point2I offset, const RectI &updateRect)
  78. {
  79. Parent::onRender( offset, updateRect );
  80. if( mInvisible )
  81. return;
  82. if( mText.isNotEmpty() && mSeparatorType != separatorTypeVertical )
  83. {
  84. // If text is present and we have a left margin, then draw some separator, then the
  85. // text, and then the rest of the separator.
  86. S32 posx = offset.x + mMargin;
  87. S32 fontheight = mProfile->mFont->getHeight();
  88. S32 seppos = (fontheight - 2) / 2 + offset.y;
  89. if( mTextLeftMargin > 0 )
  90. {
  91. RectI rect( Point2I( posx, seppos ), Point2I( mTextLeftMargin, 2 ) );
  92. renderSlightlyLoweredBox(rect, mProfile);
  93. posx += mTextLeftMargin;
  94. }
  95. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  96. posx = GFX->getDrawUtil()->drawText(mProfile->mFont, Point2I(posx,offset.y), mText, mProfile->mFontColors);
  97. RectI rect( Point2I( posx, seppos ), Point2I( getWidth() - posx + offset.x, 2 ) );
  98. // Space text and separator a bit apart at right end.
  99. rect.point.x += 2;
  100. rect.extent.x -= 2;
  101. if( rect.extent.x > 0 )
  102. renderSlightlyLoweredBox( rect, mProfile );
  103. }
  104. else
  105. {
  106. if( mSeparatorType == separatorTypeHorizontal )
  107. {
  108. S32 seppos = getHeight() / 2 + offset.y;
  109. RectI rect(Point2I(offset.x + mMargin ,seppos),Point2I(getWidth() - (mMargin * 2),2));
  110. renderSlightlyLoweredBox(rect, mProfile);
  111. }
  112. else
  113. {
  114. S32 seppos = getWidth() / 2 + offset.x;
  115. RectI rect(Point2I(seppos, offset.y + mMargin),Point2I(2, getHeight() - (mMargin * 2)));
  116. renderSlightlyLoweredBox(rect, mProfile);
  117. }
  118. }
  119. renderChildControls(offset, updateRect);
  120. }