guiConsoleTextCtrl.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "graphics/color.h"
  25. #include "gui/guiConsoleTextCtrl.h"
  26. #include "graphics/dgl.h"
  27. IMPLEMENT_CONOBJECT(GuiConsoleTextCtrl);
  28. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  29. GuiConsoleTextCtrl::GuiConsoleTextCtrl()
  30. {
  31. //default fonts
  32. mConsoleExpression = NULL;
  33. mResult = NULL;
  34. }
  35. void GuiConsoleTextCtrl::initPersistFields()
  36. {
  37. Parent::initPersistFields();
  38. addGroup("GuiConsoleTextCtrl");
  39. addField("expression", TypeCaseString, Offset(mConsoleExpression, GuiConsoleTextCtrl));
  40. endGroup("GuiConsoleTextCtrl");
  41. }
  42. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  43. bool GuiConsoleTextCtrl::onWake()
  44. {
  45. if (! Parent::onWake())
  46. return false;
  47. mFont = mProfile->mFont;
  48. return true;
  49. }
  50. void GuiConsoleTextCtrl::onSleep()
  51. {
  52. Parent::onSleep();
  53. mFont = NULL;
  54. }
  55. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  56. void GuiConsoleTextCtrl::setText(const char *txt)
  57. {
  58. //make sure we don't call this before onAdd();
  59. AssertFatal(mProfile, "Can't call setText() until setProfile() has been called.");
  60. if (txt)
  61. mConsoleExpression = StringTable->insert(txt);
  62. else
  63. mConsoleExpression = NULL;
  64. //Make sure we have a font
  65. mProfile->incRefCount();
  66. mFont = mProfile->mFont;
  67. setUpdate();
  68. //decrement the profile referrence
  69. mProfile->decRefCount();
  70. }
  71. void GuiConsoleTextCtrl::calcResize()
  72. {
  73. if (!mResult)
  74. return;
  75. //resize
  76. if (mProfile->mAutoSizeWidth)
  77. {
  78. if (mProfile->mAutoSizeHeight)
  79. resize(mBounds.point, Point2I(mFont->getStrWidth((const UTF8 *)mResult) + 4, mFont->getHeight() + 4));
  80. else
  81. resize(mBounds.point, Point2I(mFont->getStrWidth((const UTF8 *)mResult) + 4, mBounds.extent.y));
  82. }
  83. else if (mProfile->mAutoSizeHeight)
  84. {
  85. resize(mBounds.point, Point2I(mBounds.extent.x, mFont->getHeight() + 4));
  86. }
  87. }
  88. void GuiConsoleTextCtrl::onPreRender()
  89. {
  90. if (mConsoleExpression)
  91. mResult = Con::evaluatef("$temp = %s;", mConsoleExpression);
  92. calcResize();
  93. }
  94. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  95. void GuiConsoleTextCtrl::onRender(Point2I offset, const RectI &updateRect)
  96. {
  97. // draw the background rectangle
  98. RectI r(offset, mBounds.extent);
  99. dglDrawRectFill(r, ColorI(255,255,255));
  100. // draw the border
  101. r.extent += r.point;
  102. glColor4ub(0, 0, 0, 0);
  103. #ifdef TORQUE_OS_IOS
  104. Point2I topleft(r.point.x, r.point.y);
  105. Point2I bottomRight(r.extent.x-1, r.extent.y-1);
  106. //this was the same drawing as dglDrawRect
  107. dglDrawRect( topleft, bottomRight, ColorI(0, 0, 0, 0) );// PUAP -Mat untested
  108. #else
  109. glBegin(GL_LINE_LOOP);
  110. glVertex2i(r.point.x, r.point.y);
  111. glVertex2i(r.extent.x-1, r.point.y);
  112. glVertex2i(r.extent.x-1, r.extent.y-1);
  113. glVertex2i(r.point.x, r.extent.y-1);
  114. glEnd();
  115. #endif
  116. if (mResult)
  117. {
  118. S32 txt_w = mFont->getStrWidth((const UTF8 *)mResult);
  119. Point2I localStart;
  120. switch (mProfile->mAlignment)
  121. {
  122. case GuiControlProfile::RightJustify:
  123. localStart.set(mBounds.extent.x - txt_w-2, 0);
  124. break;
  125. case GuiControlProfile::CenterJustify:
  126. localStart.set((mBounds.extent.x - txt_w) / 2, 0);
  127. break;
  128. default:
  129. // GuiControlProfile::LeftJustify
  130. localStart.set(2,0);
  131. break;
  132. }
  133. Point2I globalStart = localToGlobalCoord(localStart);
  134. //draw the text
  135. dglSetBitmapModulation(mProfile->mFontColor);
  136. dglDrawText(mFont, globalStart, mResult, mProfile->mFontColors);
  137. }
  138. //render the child controls
  139. renderChildControls(offset, updateRect);
  140. }
  141. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  142. const char *GuiConsoleTextCtrl::getScriptValue()
  143. {
  144. return getText();
  145. }
  146. void GuiConsoleTextCtrl::setScriptValue(const char *val)
  147. {
  148. setText(val);
  149. }
  150. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  151. // EOF //