guiConsoleTextCtrl.cpp 5.0 KB

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