guiConsole.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "console/console.h"
  23. #include "gfx/gfxDrawUtil.h"
  24. #include "gui/core/guiTypes.h"
  25. #include "gui/core/guiControl.h"
  26. #include "gui/controls/guiConsole.h"
  27. #include "gui/containers/guiScrollCtrl.h"
  28. #include "console/engineAPI.h"
  29. #include <console/consoleInternal.h>
  30. IMPLEMENT_CONOBJECT(GuiConsole);
  31. ConsoleDocClass( GuiConsole,
  32. "@brief The on-screen, in-game console. Calls getLog() to get the on-screen console entries, then renders them as needed.\n\n"
  33. "@tsexample\n"
  34. " new GuiConsole()\n"
  35. " {\n"
  36. " //Properties not specific to this control have been omitted from this example.\n"
  37. " };\n"
  38. "@endtsexample\n\n"
  39. "@see GuiControl\n\n"
  40. "@ingroup GuiCore"
  41. );
  42. IMPLEMENT_CALLBACK( GuiConsole, onMessageSelected, void, ( ConsoleLogEntry::Level level, const char* message ), ( level, message ),
  43. "Called when a message in the log is clicked.\n\n"
  44. "@param level Diagnostic level of the message.\n"
  45. "@param message Message text.\n" );
  46. IMPLEMENT_CALLBACK(GuiConsole, onNewMessage, void, (U32 errorCount, U32 warnCount, U32 normalCount), (errorCount, warnCount, normalCount),
  47. "Called when a new message is logged.\n\n"
  48. "@param errorCount The number of error messages logged.\n"
  49. "@param warnCount The number of warning messages logged.\n"
  50. "@param normalCount The number of normal messages logged.\n");
  51. //-----------------------------------------------------------------------------
  52. GuiConsole::GuiConsole()
  53. {
  54. setExtent(64, 64);
  55. mCellSize.set(1, 1);
  56. mSize.set(1, 0);
  57. mDisplayErrors = true;
  58. mDisplayWarnings = true;
  59. mDisplayNormalMessages = true;
  60. mFiltersDirty = true;
  61. }
  62. //-----------------------------------------------------------------------------
  63. bool GuiConsole::onWake()
  64. {
  65. if (! Parent::onWake())
  66. return false;
  67. //get the font
  68. mFont = mProfile->mFont;
  69. return true;
  70. }
  71. //-----------------------------------------------------------------------------
  72. S32 GuiConsole::getMaxWidth(S32 startIndex, S32 endIndex)
  73. {
  74. //sanity check
  75. if (startIndex < 0 || (U32)endIndex >= mFilteredLog.size() || startIndex > endIndex)
  76. return 0;
  77. S32 result = 0;
  78. for(S32 i = startIndex; i <= endIndex; i++)
  79. result = getMax(result, (S32)(mFont->getStrWidth((const UTF8 *)mFilteredLog[i].mString)));
  80. return(result + 6);
  81. }
  82. void GuiConsole::refreshLogText()
  83. {
  84. U32 size;
  85. ConsoleLogEntry *log;
  86. Con::getLockLog(log, size);
  87. if (mFilteredLog.size() != size || mFiltersDirty)
  88. {
  89. mFilteredLog.clear();
  90. U32 errorCount = 0;
  91. U32 warnCount = 0;
  92. U32 normalCount = 0;
  93. //Filter the log if needed
  94. for (U32 i = 0; i < size; ++i)
  95. {
  96. ConsoleLogEntry &entry = log[i];
  97. if (entry.mLevel == ConsoleLogEntry::Error)
  98. {
  99. errorCount++;
  100. if (mDisplayErrors)
  101. {
  102. mFilteredLog.push_back(entry);
  103. }
  104. }
  105. else if (entry.mLevel == ConsoleLogEntry::Warning)
  106. {
  107. warnCount++;
  108. if (mDisplayWarnings)
  109. {
  110. mFilteredLog.push_back(entry);
  111. }
  112. }
  113. else if (entry.mLevel == ConsoleLogEntry::Normal)
  114. {
  115. normalCount++;
  116. if (mDisplayNormalMessages)
  117. {
  118. mFilteredLog.push_back(entry);
  119. }
  120. }
  121. }
  122. bool tracing = Con::gTraceOn;
  123. Con::gTraceOn = false;
  124. onNewMessage_callback(errorCount, warnCount, normalCount);
  125. Con::gTraceOn = tracing;
  126. }
  127. Con::unlockLog();
  128. }
  129. //-----------------------------------------------------------------------------
  130. void GuiConsole::onPreRender()
  131. {
  132. //see if the size has changed
  133. U32 prevSize = getHeight() / mCellSize.y;
  134. refreshLogText();
  135. //first, find out if the console was scrolled up
  136. bool scrolled = false;
  137. GuiScrollCtrl *parent = dynamic_cast<GuiScrollCtrl*>(getParent());
  138. if(parent)
  139. scrolled = parent->isScrolledToBottom();
  140. //find the max cell width for the new entries
  141. S32 newMax = getMaxWidth(prevSize, mFilteredLog.size() - 1);
  142. if(newMax > mCellSize.x)
  143. mCellSize.set(newMax, mFont->getHeight());
  144. //set the array size
  145. mSize.set(1, mFilteredLog.size());
  146. //resize the control
  147. setExtent(Point2I(mCellSize.x, mCellSize.y * mFilteredLog.size()));
  148. //if the console was not scrolled, make the last entry visible
  149. if (scrolled)
  150. scrollCellVisible(Point2I(0,mSize.y - 1));
  151. }
  152. //-----------------------------------------------------------------------------
  153. void GuiConsole::onRenderCell(Point2I offset, Point2I cell, bool /*selected*/, bool /*mouseOver*/)
  154. {
  155. ConsoleLogEntry &entry = mFilteredLog[cell.y];
  156. switch (entry.mLevel)
  157. {
  158. case ConsoleLogEntry::Normal: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColor); break;
  159. case ConsoleLogEntry::Warning: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorHL); break;
  160. case ConsoleLogEntry::Error: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorNA); break;
  161. default: AssertFatal(false, "GuiConsole::onRenderCell - Unrecognized ConsoleLogEntry type, update this.");
  162. }
  163. GFX->getDrawUtil()->drawText(mFont, Point2I(offset.x + 3, offset.y), entry.mString, mProfile->mFontColors);
  164. }
  165. //-----------------------------------------------------------------------------
  166. void GuiConsole::onCellSelected( Point2I cell )
  167. {
  168. Parent::onCellSelected( cell );
  169. ConsoleLogEntry& entry = mFilteredLog[cell.y];
  170. onMessageSelected_callback( entry.mLevel, entry.mString );
  171. }
  172. void GuiConsole::setDisplayFilters(bool errors, bool warns, bool normal)
  173. {
  174. mDisplayErrors = errors;
  175. mDisplayWarnings = warns;
  176. mDisplayNormalMessages = normal;
  177. mFiltersDirty = true;
  178. refreshLogText();
  179. //find the max cell width for the new entries
  180. S32 newMax = getMaxWidth(0, mFilteredLog.size() - 1);
  181. mCellSize.set(newMax, mFont->getHeight());
  182. //set the array size
  183. mSize.set(1, mFilteredLog.size());
  184. //resize the control
  185. setExtent(Point2I(mCellSize.x, mCellSize.y * mFilteredLog.size()));
  186. scrollCellVisible(Point2I(0, mSize.y - 1));
  187. mFiltersDirty = false;
  188. }
  189. DefineEngineMethod(GuiConsole, setDisplayFilters, void, (bool errors, bool warns, bool normal), (true, true, true),
  190. "Sets the current display filters for the console gui. Allows you to indicate if it should display errors, warns and/or normal messages.\n\n"
  191. "@param errors If true, the console gui will display any error messages that were emitted.\n\n"
  192. "@param warns If true, the console gui will display any warning messages that were emitted.\n\n"
  193. "@param normal If true, the console gui will display any regular messages that were emitted.\n\n")
  194. {
  195. object->setDisplayFilters(errors, warns, normal);
  196. }
  197. DefineEngineMethod(GuiConsole, getErrorFilter, bool, (), ,
  198. "Returns if the error filter is on or not.")
  199. {
  200. return object->getErrorFilter();
  201. }
  202. DefineEngineMethod(GuiConsole, getWarnFilter, bool, (), ,
  203. "Returns if the warning filter is on or not.")
  204. {
  205. return object->getWarnFilter();
  206. }
  207. DefineEngineMethod(GuiConsole, getNormalFilter, bool, (), ,
  208. "Returns if the normal message filter is on or not.")
  209. {
  210. return object->getNormalFilter();
  211. }
  212. DefineEngineMethod(GuiConsole, toggleErrorFilter, void, (), ,
  213. "Toggles the error filter.")
  214. {
  215. object->toggleErrorFilter();
  216. }
  217. DefineEngineMethod(GuiConsole, toggleWarnFilter, void, (), ,
  218. "Toggles the warning filter.")
  219. {
  220. object->toggleWarnFilter();
  221. }
  222. DefineEngineMethod(GuiConsole, toggleNormalFilter, void, (), ,
  223. "Toggles the normal messages filter.")
  224. {
  225. object->toggleNormalFilter();
  226. }
  227. DefineEngineMethod(GuiConsole, refresh, void, (), ,
  228. "Refreshes the displayed messages.")
  229. {
  230. object->refresh();
  231. }