guiConsoleTextCtrl.cpp 5.0 KB

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