guiConsoleTextCtrl.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. //DEPRECIATED
  77. /*
  78. if (mProfile->mAutoSizeWidth)
  79. {
  80. if (mProfile->mAutoSizeHeight)
  81. resize(mBounds.point, Point2I(mFont->getStrWidth((const UTF8 *)mResult) + 4, mFont->getHeight() + 4));
  82. else
  83. resize(mBounds.point, Point2I(mFont->getStrWidth((const UTF8 *)mResult) + 4, mBounds.extent.y));
  84. }
  85. else if (mProfile->mAutoSizeHeight)
  86. {
  87. resize(mBounds.point, Point2I(mBounds.extent.x, mFont->getHeight() + 4));
  88. }
  89. */
  90. }
  91. void GuiConsoleTextCtrl::onPreRender()
  92. {
  93. if (mConsoleExpression)
  94. mResult = Con::evaluatef("$temp = %s;", mConsoleExpression);
  95. calcResize();
  96. }
  97. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  98. void GuiConsoleTextCtrl::onRender(Point2I offset, const RectI &updateRect)
  99. {
  100. // draw the background rectangle
  101. RectI r(offset, mBounds.extent);
  102. dglDrawRectFill(r, ColorI(255,255,255));
  103. // draw the border
  104. r.extent += r.point;
  105. glColor4ub(0, 0, 0, 0);
  106. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  107. Point2I topleft(r.point.x, r.point.y);
  108. Point2I bottomRight(r.extent.x-1, r.extent.y-1);
  109. //this was the same drawing as dglDrawRect
  110. dglDrawRect( topleft, bottomRight, ColorI(0, 0, 0, 0) );// PUAP -Mat untested
  111. #else
  112. glBegin(GL_LINE_LOOP);
  113. glVertex2i(r.point.x, r.point.y);
  114. glVertex2i(r.extent.x-1, r.point.y);
  115. glVertex2i(r.extent.x-1, r.extent.y-1);
  116. glVertex2i(r.point.x, r.extent.y-1);
  117. glEnd();
  118. #endif
  119. if (mResult)
  120. {
  121. S32 txt_w = mFont->getStrWidth((const UTF8 *)mResult);
  122. Point2I localStart;
  123. switch (mProfile->mAlignment)
  124. {
  125. case GuiControlProfile::RightAlign:
  126. localStart.set(mBounds.extent.x - txt_w-2, 0);
  127. break;
  128. case GuiControlProfile::CenterAlign:
  129. localStart.set((mBounds.extent.x - txt_w) / 2, 0);
  130. break;
  131. default:
  132. // GuiControlProfile::LeftAlign
  133. localStart.set(2,0);
  134. break;
  135. }
  136. Point2I globalStart = localToGlobalCoord(localStart);
  137. //draw the text
  138. dglSetBitmapModulation(mProfile->mFontColor);
  139. dglDrawText(mFont, globalStart, mResult, mProfile->mFontColors);
  140. }
  141. //render the child controls
  142. renderChildControls(offset, mBounds, updateRect);
  143. }
  144. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  145. const char *GuiConsoleTextCtrl::getScriptValue()
  146. {
  147. return getText();
  148. }
  149. void GuiConsoleTextCtrl::setScriptValue(const char *val)
  150. {
  151. setText(val);
  152. }
  153. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  154. // EOF //