guiConsole.cpp 8.8 KB

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