guiConsoleTextCtrl.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/controls/guiConsoleTextCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/console.h"
  26. #include "core/color.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "gui/core/guiDefaultControlRender.h"
  29. IMPLEMENT_CONOBJECT(GuiConsoleTextCtrl);
  30. ConsoleDocClass( GuiConsoleTextCtrl,
  31. "@brief Used by GUIConsole system internally.\n\n"
  32. "@internal"
  33. );
  34. GuiConsoleTextCtrl::GuiConsoleTextCtrl()
  35. {
  36. }
  37. GuiConsoleTextCtrl::~GuiConsoleTextCtrl()
  38. {
  39. }
  40. void GuiConsoleTextCtrl::initPersistFields()
  41. {
  42. addGroup("GuiConsoleTextCtrl");
  43. addField("expression", TypeRealString, Offset(mConsoleExpression, GuiConsoleTextCtrl));
  44. endGroup("GuiConsoleTextCtrl");
  45. Parent::initPersistFields();
  46. }
  47. bool GuiConsoleTextCtrl::onWake()
  48. {
  49. if (! Parent::onWake())
  50. return false;
  51. mFont = mProfile->mFont;
  52. return true;
  53. }
  54. void GuiConsoleTextCtrl::onSleep()
  55. {
  56. Parent::onSleep();
  57. mFont = NULL;
  58. }
  59. void GuiConsoleTextCtrl::setText(const char *txt)
  60. {
  61. //make sure we don't call this before onAdd();
  62. AssertFatal(mProfile, "Can't call setText() until setProfile() has been called.");
  63. if (txt)
  64. mConsoleExpression = txt;
  65. else
  66. mConsoleExpression = String::EmptyString;
  67. // make sure we have a font
  68. mProfile->incLoadCount();
  69. mFont = mProfile->mFont;
  70. setUpdate();
  71. // decrement the profile reference
  72. mProfile->decLoadCount();
  73. }
  74. void GuiConsoleTextCtrl::calcResize()
  75. {
  76. if ( mResult.isEmpty() )
  77. return;
  78. // The width is the longest line.
  79. U32 ctrlWidth = 0;
  80. for ( U32 i = 0; i < mLineLen.size(); i++ )
  81. {
  82. U32 width = mFont->getStrNWidth( mResult.c_str() + mStartLineOffset[i], mLineLen[i] );
  83. if ( width > ctrlWidth )
  84. ctrlWidth = width;
  85. }
  86. // The height is the number of lines times the height of the font.
  87. U32 ctrlHeight = mLineLen.size() * mFont->getHeight();
  88. setExtent( Point2I( ctrlWidth, ctrlHeight ) + mProfile->mTextOffset * 2 );
  89. }
  90. void GuiConsoleTextCtrl::onPreRender()
  91. {
  92. if ( mConsoleExpression.isNotEmpty() )
  93. {
  94. Con::evaluatef( "$guiConsoleTextCtrlTemp = %s;", mConsoleExpression.c_str() );
  95. //Fixes a bug with the above not always grabbing the console text.
  96. mResult = Con::getVariable("$guiConsoleTextCtrlTemp");
  97. // Of the resulting string we will be printing,
  98. // Find the number of lines and length of each.
  99. mProfile->mFont->wrapString( mResult, U32_MAX, mStartLineOffset, mLineLen );
  100. }
  101. else
  102. mResult = String::EmptyString;
  103. calcResize();
  104. }
  105. void GuiConsoleTextCtrl::onRender( Point2I offset, const RectI &updateRect )
  106. {
  107. RectI ctrlRect( offset, getExtent() );
  108. // if opaque, fill the update rect with the fill color
  109. if ( mProfile->mOpaque )
  110. GFX->getDrawUtil()->drawRectFill( ctrlRect, mProfile->mFillColor );
  111. // if there's a border, draw the border
  112. if ( mProfile->mBorder )
  113. renderBorder( ctrlRect, mProfile );
  114. // If we have text to render.
  115. if ( mResult.isNotEmpty() )
  116. {
  117. GFont *font = mProfile->mFont;
  118. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  119. for ( U32 i = 0; i < mLineLen.size(); i++ )
  120. {
  121. Point2I tempOffset = offset;
  122. tempOffset += mProfile->mTextOffset;
  123. tempOffset.y += i * font->getHeight();
  124. const UTF8 *line = mResult.c_str() + mStartLineOffset[i];
  125. U32 lineLen = mLineLen[i];
  126. GFX->getDrawUtil()->drawTextN( font, tempOffset, line, lineLen, mProfile->mFontColors );
  127. }
  128. }
  129. // render the child controlsmResult
  130. renderChildControls(offset, updateRect);
  131. }
  132. const char *GuiConsoleTextCtrl::getScriptValue()
  133. {
  134. return getText();
  135. }
  136. void GuiConsoleTextCtrl::setScriptValue(const char *val)
  137. {
  138. setText(val);
  139. }