2
0

guiConsole.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. if (startIndex < 0 || (U32)endIndex >= mFilteredLog.size() || startIndex > endIndex)
  75. return 0;
  76. S32 result = 0;
  77. for(S32 i = startIndex; i <= endIndex; i++)
  78. result = getMax(result, (S32)(mFont->getStrWidth((const UTF8 *)mFilteredLog[i].mString)));
  79. return(result + 6);
  80. }
  81. void GuiConsole::refreshLogText()
  82. {
  83. U32 size;
  84. ConsoleLogEntry *log;
  85. Con::getLockLog(log, size);
  86. if (mFilteredLog.size() != size || mFiltersDirty)
  87. {
  88. mFilteredLog.clear();
  89. U32 errorCount = 0;
  90. U32 warnCount = 0;
  91. U32 normalCount = 0;
  92. //Filter the log if needed
  93. for (U32 i = 0; i < size; ++i)
  94. {
  95. ConsoleLogEntry &entry = log[i];
  96. if (entry.mLevel == ConsoleLogEntry::Error)
  97. {
  98. errorCount++;
  99. if (mDisplayErrors)
  100. {
  101. mFilteredLog.push_back(entry);
  102. }
  103. }
  104. else if (entry.mLevel == ConsoleLogEntry::Warning)
  105. {
  106. warnCount++;
  107. if (mDisplayWarnings)
  108. {
  109. mFilteredLog.push_back(entry);
  110. }
  111. }
  112. else if (entry.mLevel == ConsoleLogEntry::Normal)
  113. {
  114. normalCount++;
  115. if (mDisplayNormalMessages)
  116. {
  117. mFilteredLog.push_back(entry);
  118. }
  119. }
  120. }
  121. onNewMessage_callback(errorCount, warnCount, normalCount);
  122. }
  123. Con::unlockLog();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void GuiConsole::onPreRender()
  127. {
  128. //see if the size has changed
  129. U32 prevSize = getHeight() / mCellSize.y;
  130. refreshLogText();
  131. //first, find out if the console was scrolled up
  132. bool scrolled = false;
  133. GuiScrollCtrl *parent = dynamic_cast<GuiScrollCtrl*>(getParent());
  134. if(parent)
  135. scrolled = parent->isScrolledToBottom();
  136. //find the max cell width for the new entries
  137. S32 newMax = getMaxWidth(prevSize, mFilteredLog.size() - 1);
  138. if(newMax > mCellSize.x)
  139. mCellSize.set(newMax, mFont->getHeight());
  140. //set the array size
  141. mSize.set(1, mFilteredLog.size());
  142. //resize the control
  143. setExtent(Point2I(mCellSize.x, mCellSize.y * mFilteredLog.size()));
  144. //if the console was not scrolled, make the last entry visible
  145. if (scrolled)
  146. scrollCellVisible(Point2I(0,mSize.y - 1));
  147. }
  148. //-----------------------------------------------------------------------------
  149. void GuiConsole::onRenderCell(Point2I offset, Point2I cell, bool /*selected*/, bool /*mouseOver*/)
  150. {
  151. ConsoleLogEntry &entry = mFilteredLog[cell.y];
  152. switch (entry.mLevel)
  153. {
  154. case ConsoleLogEntry::Normal: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColor); break;
  155. case ConsoleLogEntry::Warning: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorHL); break;
  156. case ConsoleLogEntry::Error: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorNA); break;
  157. default: AssertFatal(false, "GuiConsole::onRenderCell - Unrecognized ConsoleLogEntry type, update this.");
  158. }
  159. GFX->getDrawUtil()->drawText(mFont, Point2I(offset.x + 3, offset.y), entry.mString, mProfile->mFontColors);
  160. }
  161. //-----------------------------------------------------------------------------
  162. void GuiConsole::onCellSelected( Point2I cell )
  163. {
  164. Parent::onCellSelected( cell );
  165. ConsoleLogEntry& entry = mFilteredLog[cell.y];
  166. onMessageSelected_callback( entry.mLevel, entry.mString );
  167. }
  168. void GuiConsole::setDisplayFilters(bool errors, bool warns, bool normal)
  169. {
  170. mDisplayErrors = errors;
  171. mDisplayWarnings = warns;
  172. mDisplayNormalMessages = normal;
  173. mFiltersDirty = true;
  174. refreshLogText();
  175. //find the max cell width for the new entries
  176. S32 newMax = getMaxWidth(0, mFilteredLog.size() - 1);
  177. mCellSize.set(newMax, mFont->getHeight());
  178. //set the array size
  179. mSize.set(1, mFilteredLog.size());
  180. //resize the control
  181. setExtent(Point2I(mCellSize.x, mCellSize.y * mFilteredLog.size()));
  182. scrollCellVisible(Point2I(0, mSize.y - 1));
  183. mFiltersDirty = false;
  184. }
  185. DefineEngineMethod(GuiConsole, setDisplayFilters, void, (bool errors, bool warns, bool normal), (true, true, true),
  186. "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"
  187. "@param errors If true, the console gui will display any error messages that were emitted.\n\n"
  188. "@param warns If true, the console gui will display any warning messages that were emitted.\n\n"
  189. "@param normal If true, the console gui will display any regular messages that were emitted.\n\n")
  190. {
  191. object->setDisplayFilters(errors, warns, normal);
  192. }
  193. DefineEngineMethod(GuiConsole, getErrorFilter, bool, (), ,
  194. "Returns if the error filter is on or not.")
  195. {
  196. return object->getErrorFilter();
  197. }
  198. DefineEngineMethod(GuiConsole, getWarnFilter, bool, (), ,
  199. "Returns if the warning filter is on or not.")
  200. {
  201. return object->getWarnFilter();
  202. }
  203. DefineEngineMethod(GuiConsole, getNormalFilter, bool, (), ,
  204. "Returns if the normal message filter is on or not.")
  205. {
  206. return object->getNormalFilter();
  207. }
  208. DefineEngineMethod(GuiConsole, toggleErrorFilter, void, (), ,
  209. "Toggles the error filter.")
  210. {
  211. object->toggleErrorFilter();
  212. }
  213. DefineEngineMethod(GuiConsole, toggleWarnFilter, void, (), ,
  214. "Toggles the warning filter.")
  215. {
  216. object->toggleWarnFilter();
  217. }
  218. DefineEngineMethod(GuiConsole, toggleNormalFilter, void, (), ,
  219. "Toggles the normal messages filter.")
  220. {
  221. object->toggleNormalFilter();
  222. }
  223. DefineEngineMethod(GuiConsole, refresh, void, (), ,
  224. "Refreshes the displayed messages.")
  225. {
  226. object->refresh();
  227. }