Torque3D Documentation / _generateds / guiConsoleTextCtrl.cpp

guiConsoleTextCtrl.cpp

Engine/source/gui/controls/guiConsoleTextCtrl.cpp

More...

Public Functions

ConsoleDocClass(GuiConsoleTextCtrl , "@brief Used by GUIConsole system <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">internally.\n\n</a>" "@internal" )

Detailed Description

Public Functions

ConsoleDocClass(GuiConsoleTextCtrl , "@brief Used by GUIConsole system <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">internally.\n\n</a>" "@internal" )

IMPLEMENT_CONOBJECT(GuiConsoleTextCtrl )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "gui/controls/guiConsoleTextCtrl.h"
 26
 27#include "console/consoleTypes.h"
 28#include "console/console.h"
 29#include "core/color.h"
 30#include "gfx/gfxDrawUtil.h"
 31#include "gui/core/guiDefaultControlRender.h"
 32
 33IMPLEMENT_CONOBJECT(GuiConsoleTextCtrl);
 34
 35ConsoleDocClass( GuiConsoleTextCtrl,
 36   "@brief Used by GUIConsole system internally.\n\n"
 37   "@internal"
 38);
 39
 40GuiConsoleTextCtrl::GuiConsoleTextCtrl()
 41{
 42}
 43
 44GuiConsoleTextCtrl::~GuiConsoleTextCtrl()
 45{
 46}
 47
 48void GuiConsoleTextCtrl::initPersistFields()
 49{
 50   addGroup("GuiConsoleTextCtrl");     
 51   addField("expression",  TypeRealString,  Offset(mConsoleExpression, GuiConsoleTextCtrl));
 52   endGroup("GuiConsoleTextCtrl");
 53   Parent::initPersistFields();
 54}
 55
 56bool GuiConsoleTextCtrl::onWake()
 57{
 58   if (! Parent::onWake())
 59      return false;
 60
 61   mFont = mProfile->mFont;
 62   return true;
 63}
 64
 65void GuiConsoleTextCtrl::onSleep()
 66{
 67   Parent::onSleep();
 68   mFont = NULL;
 69}
 70
 71void GuiConsoleTextCtrl::setText(const char *txt)
 72{
 73   //make sure we don't call this before onAdd();
 74   AssertFatal(mProfile, "Can't call setText() until setProfile() has been called.");
 75
 76   if (txt)
 77      mConsoleExpression = txt;
 78   else
 79      mConsoleExpression = String::EmptyString;
 80
 81   // make sure we have a font
 82   mProfile->incLoadCount();
 83   mFont = mProfile->mFont;
 84
 85   setUpdate();
 86
 87   // decrement the profile reference
 88   mProfile->decLoadCount();
 89}
 90
 91void GuiConsoleTextCtrl::calcResize()
 92{
 93   if ( mResult.isEmpty() )
 94      return;
 95
 96   // The width is the longest line.
 97   U32 ctrlWidth = 0;
 98   for ( U32 i = 0; i < mLineLen.size(); i++ )
 99   {
100      U32 width = mFont->getStrNWidth( mResult.c_str() + mStartLineOffset[i], mLineLen[i] );
101
102      if ( width > ctrlWidth )
103         ctrlWidth = width;
104   }
105
106   // The height is the number of lines times the height of the font.
107   U32 ctrlHeight = mLineLen.size() * mFont->getHeight();   
108
109   setExtent( Point2I( ctrlWidth, ctrlHeight ) + mProfile->mTextOffset * 2 );
110}
111
112
113void GuiConsoleTextCtrl::onPreRender()
114{   
115   if ( mConsoleExpression.isNotEmpty() )
116   {
117      Con::evaluatef( "$guiConsoleTextCtrlTemp = %s;", mConsoleExpression.c_str() );
118      
119      //Fixes a bug with the above not always grabbing the console text.
120      mResult = Con::getVariable("$guiConsoleTextCtrlTemp");
121      
122      // Of the resulting string we will be printing,
123      // Find the number of lines and length of each.      
124      mProfile->mFont->wrapString( mResult, U32_MAX, mStartLineOffset, mLineLen );
125   }
126   else
127      mResult = String::EmptyString;         
128
129   calcResize();
130}
131
132void GuiConsoleTextCtrl::onRender( Point2I offset, const RectI &updateRect )
133{
134   RectI ctrlRect( offset, getExtent() );
135
136   // if opaque, fill the update rect with the fill color
137   if ( mProfile->mOpaque )
138      GFX->getDrawUtil()->drawRectFill( ctrlRect, mProfile->mFillColor );
139
140   // if there's a border, draw the border
141   if ( mProfile->mBorder )
142      renderBorder( ctrlRect, mProfile );
143
144   // If we have text to render.
145   if ( mResult.isNotEmpty() )
146   {
147      GFont *font = mProfile->mFont;      
148      
149      GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
150
151      for ( U32 i = 0; i < mLineLen.size(); i++ )
152      {      
153         Point2I tempOffset = offset; 
154         tempOffset += mProfile->mTextOffset;
155         tempOffset.y += i * font->getHeight();
156         
157         const UTF8 *line = mResult.c_str() + mStartLineOffset[i];
158         U32 lineLen = mLineLen[i];
159         GFX->getDrawUtil()->drawTextN( font, tempOffset, line, lineLen, mProfile->mFontColors );
160      }
161   }
162
163   // render the child controlsmResult
164   renderChildControls(offset, updateRect);
165}
166
167const char *GuiConsoleTextCtrl::getScriptValue()
168{
169   return getText();
170}
171
172void GuiConsoleTextCtrl::setScriptValue(const char *val)
173{
174   setText(val);
175}
176