guiTextCtrl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/guiTextCtrl.h"
  24. #include "gui/core/guiDefaultControlRender.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/console.h"
  27. #include "core/color.h"
  28. #include "i18n/lang.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "console/engineAPI.h"
  31. IMPLEMENT_CONOBJECT( GuiTextCtrl );
  32. ConsoleDocClass( GuiTextCtrl,
  33. "@brief GUI control object this displays a single line of text, without TorqueML.\n\n"
  34. "@tsexample\n"
  35. " new GuiTextCtrl()\n"
  36. " {\n"
  37. " text = \"Hello World\";\n"
  38. " textID = \"\"STR_HELLO\"\";\n"
  39. " maxlength = \"1024\";\n"
  40. " //Properties not specific to this control have been omitted from this example.\n"
  41. " };\n"
  42. "@endtsexample\n\n"
  43. "@see GuiControl\n"
  44. "@see Localization\n\n"
  45. "@ingroup GuiCore\n"
  46. );
  47. GuiTextCtrl::GuiTextCtrl()
  48. {
  49. //default fonts
  50. mInitialText = StringTable->EmptyString();
  51. mInitialTextID = StringTable->EmptyString();
  52. mText[0] = '\0';
  53. mMaxStrLen = GuiTextCtrl::MAX_STRING_LENGTH;
  54. }
  55. DefineEngineMethod( GuiTextCtrl, setText, void, (const char* text),,
  56. "@brief Sets the text in the control.\n\n"
  57. "@param text Text to display in the control.\n"
  58. "@tsexample\n"
  59. "// Set the text to show in the control\n"
  60. "%text = \"Gideon - Destroyer of World\";\n\n"
  61. "// Inform the GuiTextCtrl control to change its text to the defined value\n"
  62. "%thisGuiTextCtrl.setText(%text);\n"
  63. "@endtsexample\n\n"
  64. "@see GuiControl")
  65. {
  66. object->setText( text );
  67. }
  68. DefineEngineMethod( GuiTextCtrl, setTextID, void, (const char* textID),,
  69. "@brief Maps the text ctrl to a variable used in localization, rather than raw text.\n\n"
  70. "@param textID Name of variable text should be mapped to\n"
  71. "@tsexample\n"
  72. "// Inform the GuiTextCtrl control of the textID to use\n"
  73. "%thisGuiTextCtrl.setTextID(\"STR_QUIT\");\n"
  74. "@endtsexample\n\n"
  75. "@see GuiControl"
  76. "@see Localization")
  77. {
  78. object->setTextID( textID );
  79. }
  80. void GuiTextCtrl::initPersistFields()
  81. {
  82. addProtectedField("text", TypeCaseString, Offset(mInitialText, GuiTextCtrl), setText, getTextProperty,
  83. "The text to show on the control.");
  84. addField( "textID", TypeString, Offset( mInitialTextID, GuiTextCtrl ),
  85. "Maps the text of this control to a variable used in localization, rather than raw text.");
  86. addField( "maxLength", TypeS32, Offset( mMaxStrLen, GuiTextCtrl ),
  87. "Defines the maximum length of the text. The default is 1024." );
  88. Parent::initPersistFields();
  89. }
  90. bool GuiTextCtrl::onAdd()
  91. {
  92. if(!Parent::onAdd())
  93. return false;
  94. dStrncpy(mText, (UTF8*)mInitialText, MAX_STRING_LENGTH);
  95. mText[MAX_STRING_LENGTH] = '\0';
  96. return true;
  97. }
  98. void GuiTextCtrl::inspectPostApply()
  99. {
  100. Parent::inspectPostApply();
  101. if(mInitialTextID && *mInitialTextID != 0)
  102. setTextID(mInitialTextID);
  103. else if( mConsoleVariable[ 0 ] )
  104. setText( getVariable() );
  105. else
  106. setText(mInitialText);
  107. }
  108. bool GuiTextCtrl::onWake()
  109. {
  110. if ( !Parent::onWake() )
  111. return false;
  112. if( !mProfile->mFont )
  113. {
  114. Con::errorf( "GuiTextCtrl::onWake() - no valid font in profile '%s'", mProfile->getName() );
  115. return false;
  116. }
  117. if(mInitialTextID && *mInitialTextID != 0)
  118. setTextID(mInitialTextID);
  119. if ( mConsoleVariable[0] )
  120. {
  121. const char *txt = Con::getVariable( mConsoleVariable );
  122. if ( txt )
  123. {
  124. if ( dStrlen( txt ) > mMaxStrLen )
  125. {
  126. char* buf = new char[mMaxStrLen + 1];
  127. dStrncpy( buf, txt, mMaxStrLen );
  128. buf[mMaxStrLen] = 0;
  129. setScriptValue( buf );
  130. delete [] buf;
  131. }
  132. else
  133. setScriptValue( txt );
  134. }
  135. }
  136. //resize
  137. autoResize();
  138. return true;
  139. }
  140. void GuiTextCtrl::autoResize()
  141. {
  142. if( mProfile->mAutoSizeWidth || mProfile->mAutoSizeHeight)
  143. {
  144. if( !mProfile->mFont )
  145. {
  146. mProfile->loadFont();
  147. if( !mProfile->mFont )
  148. return;
  149. }
  150. Point2I newExtents = getExtent();
  151. if ( mProfile->mAutoSizeWidth )
  152. newExtents.x = mProfile->mFont->getStrWidth((const UTF8 *) mText );
  153. if ( mProfile->mAutoSizeHeight )
  154. newExtents.y = mProfile->mFont->getHeight() + 4;
  155. setExtent( newExtents );
  156. }
  157. }
  158. void GuiTextCtrl::setText(const char *txt)
  159. {
  160. //make sure we don't call this before onAdd();
  161. if( !mProfile )
  162. return;
  163. // The txt pointer is sometimes the same as the mText pointer, so make sure
  164. // we don't call strncpy with overlapping src and dest.
  165. if (txt && txt != mText)
  166. dStrncpy(mText, (UTF8*)txt, MAX_STRING_LENGTH);
  167. mText[MAX_STRING_LENGTH] = '\0';
  168. setVariable((char*)mText);
  169. setUpdate();
  170. autoResize();
  171. }
  172. void GuiTextCtrl::setTextID(const char *id)
  173. {
  174. S32 n = Con::getIntVariable(id, -1);
  175. if(n != -1)
  176. {
  177. mInitialTextID = StringTable->insert(id);
  178. setTextID(n);
  179. }
  180. }
  181. void GuiTextCtrl::setTextID(S32 id)
  182. {
  183. const UTF8 *str = getGUIString(id);
  184. if(str)
  185. setText((const char*)str);
  186. //mInitialTextID = id;
  187. }
  188. void GuiTextCtrl::onPreRender()
  189. {
  190. Parent::onPreRender();
  191. const char * var = getVariable();
  192. if(var && var[0] && dStricmp((char*)mText, var))
  193. setText(var);
  194. }
  195. void GuiTextCtrl::onRender(Point2I offset, const RectI &updateRect)
  196. {
  197. renderBorder( RectI( offset, getExtent() ), mProfile );
  198. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  199. renderJustifiedText(offset, getExtent(), (char*)mText);
  200. //render the child controls
  201. renderChildControls(offset, updateRect);
  202. }
  203. const char *GuiTextCtrl::getScriptValue()
  204. {
  205. return getText();
  206. }
  207. void GuiTextCtrl::setScriptValue(const char *val)
  208. {
  209. setText(val);
  210. }