guiTextCtrl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. docsURL;
  83. addProtectedField("text", TypeCaseString, Offset(mInitialText, GuiTextCtrl), setText, getTextProperty,
  84. "The text to show on the control.");
  85. addField( "textID", TypeString, Offset( mInitialTextID, GuiTextCtrl ),
  86. "Maps the text of this control to a variable used in localization, rather than raw text.");
  87. addField( "maxLength", TypeS32, Offset( mMaxStrLen, GuiTextCtrl ),
  88. "Defines the maximum length of the text. The default is 1024." );
  89. Parent::initPersistFields();
  90. }
  91. bool GuiTextCtrl::onAdd()
  92. {
  93. if(!Parent::onAdd())
  94. return false;
  95. dStrncpy(mText, (UTF8*)mInitialText, MAX_STRING_LENGTH);
  96. mText[MAX_STRING_LENGTH] = '\0';
  97. return true;
  98. }
  99. void GuiTextCtrl::inspectPostApply()
  100. {
  101. Parent::inspectPostApply();
  102. if(mInitialTextID && *mInitialTextID != 0)
  103. setTextID(mInitialTextID);
  104. else if( mConsoleVariable[ 0 ] )
  105. setText( getVariable() );
  106. else
  107. setText(mInitialText);
  108. }
  109. bool GuiTextCtrl::onWake()
  110. {
  111. if ( !Parent::onWake() )
  112. return false;
  113. if( !mProfile->mFont )
  114. {
  115. Con::errorf( "GuiTextCtrl::onWake() - no valid font in profile '%s'", mProfile->getName() );
  116. return false;
  117. }
  118. if(mInitialTextID && *mInitialTextID != 0)
  119. setTextID(mInitialTextID);
  120. if ( mConsoleVariable[0] )
  121. {
  122. const char *txt = Con::getVariable( mConsoleVariable );
  123. if ( txt )
  124. {
  125. if ( dStrlen( txt ) > mMaxStrLen )
  126. {
  127. char* buf = new char[mMaxStrLen + 1];
  128. dStrncpy( buf, txt, mMaxStrLen );
  129. buf[mMaxStrLen] = 0;
  130. setScriptValue( buf );
  131. delete [] buf;
  132. }
  133. else
  134. setScriptValue( txt );
  135. }
  136. }
  137. //resize
  138. autoResize();
  139. return true;
  140. }
  141. void GuiTextCtrl::autoResize()
  142. {
  143. if( mProfile->mAutoSizeWidth || mProfile->mAutoSizeHeight)
  144. {
  145. if( !mProfile->mFont )
  146. {
  147. mProfile->loadFont();
  148. if( !mProfile->mFont )
  149. return;
  150. }
  151. Point2I newExtents = getExtent();
  152. if ( mProfile->mAutoSizeWidth )
  153. newExtents.x = mProfile->mFont->getStrWidth((const UTF8 *) mText );
  154. if ( mProfile->mAutoSizeHeight )
  155. newExtents.y = mProfile->mFont->getHeight() + 4;
  156. setExtent( newExtents );
  157. }
  158. }
  159. void GuiTextCtrl::setText(const char *txt)
  160. {
  161. //make sure we don't call this before onAdd();
  162. if( !mProfile )
  163. return;
  164. // The txt pointer is sometimes the same as the mText pointer, so make sure
  165. // we don't call strncpy with overlapping src and dest.
  166. if (txt && txt != mText)
  167. dStrncpy(mText, (UTF8*)txt, MAX_STRING_LENGTH);
  168. mText[MAX_STRING_LENGTH] = '\0';
  169. setVariable((char*)mText);
  170. setUpdate();
  171. autoResize();
  172. }
  173. void GuiTextCtrl::setTextID(const char *id)
  174. {
  175. S32 n = Con::getIntVariable(id, -1);
  176. if(n != -1)
  177. {
  178. mInitialTextID = StringTable->insert(id);
  179. setTextID(n);
  180. }
  181. }
  182. void GuiTextCtrl::setTextID(S32 id)
  183. {
  184. const UTF8 *str = getGUIString(id);
  185. if(str)
  186. setText((const char*)str);
  187. //mInitialTextID = id;
  188. }
  189. void GuiTextCtrl::onPreRender()
  190. {
  191. Parent::onPreRender();
  192. const char * var = getVariable();
  193. if(var && var[0] && dStricmp((char*)mText, var))
  194. setText(var);
  195. }
  196. void GuiTextCtrl::onRender(Point2I offset, const RectI &updateRect)
  197. {
  198. renderBorder( RectI( offset, getExtent() ), mProfile );
  199. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  200. renderJustifiedText(offset, getExtent(), (char*)mText);
  201. //render the child controls
  202. renderChildControls(offset, updateRect);
  203. }
  204. const char *GuiTextCtrl::getScriptValue()
  205. {
  206. return getText();
  207. }
  208. void GuiTextCtrl::setScriptValue(const char *val)
  209. {
  210. setText(val);
  211. }