guiConsole.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. }
  60. //-----------------------------------------------------------------------------
  61. bool GuiConsole::onWake()
  62. {
  63. if (! Parent::onWake())
  64. return false;
  65. //get the font
  66. mFont = mProfile->mFont;
  67. return true;
  68. }
  69. //-----------------------------------------------------------------------------
  70. S32 GuiConsole::getMaxWidth(S32 startIndex, S32 endIndex)
  71. {
  72. //sanity check
  73. U32 size;
  74. ConsoleLogEntry *log;
  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)
  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. onNewMessage_callback(errorCount, warnCount, normalCount);
  123. }
  124. Con::unlockLog();
  125. }
  126. //-----------------------------------------------------------------------------
  127. void GuiConsole::onPreRender()
  128. {
  129. //see if the size has changed
  130. U32 prevSize = getHeight() / mCellSize.y;
  131. refreshLogText();
  132. //first, find out if the console was scrolled up
  133. bool scrolled = false;
  134. GuiScrollCtrl *parent = dynamic_cast<GuiScrollCtrl*>(getParent());
  135. if(parent)
  136. scrolled = parent->isScrolledToBottom();
  137. //find the max cell width for the new entries
  138. S32 newMax = getMaxWidth(prevSize, mFilteredLog.size() - 1);
  139. if(newMax > mCellSize.x)
  140. mCellSize.set(newMax, mFont->getHeight());
  141. //set the array size
  142. mSize.set(1, mFilteredLog.size());
  143. //resize the control
  144. setExtent(Point2I(mCellSize.x, mCellSize.y * mFilteredLog.size()));
  145. //if the console was not scrolled, make the last entry visible
  146. if (scrolled)
  147. scrollCellVisible(Point2I(0,mSize.y - 1));
  148. }
  149. //-----------------------------------------------------------------------------
  150. void GuiConsole::onRenderCell(Point2I offset, Point2I cell, bool /*selected*/, bool /*mouseOver*/)
  151. {
  152. U32 size;
  153. ConsoleLogEntry *log;
  154. ConsoleLogEntry &entry = mFilteredLog[cell.y];
  155. switch (entry.mLevel)
  156. {
  157. case ConsoleLogEntry::Normal: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColor); break;
  158. case ConsoleLogEntry::Warning: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorHL); break;
  159. case ConsoleLogEntry::Error: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorNA); break;
  160. default: AssertFatal(false, "GuiConsole::onRenderCell - Unrecognized ConsoleLogEntry type, update this.");
  161. }
  162. GFX->getDrawUtil()->drawText(mFont, Point2I(offset.x + 3, offset.y), entry.mString, mProfile->mFontColors);
  163. }
  164. //-----------------------------------------------------------------------------
  165. void GuiConsole::onCellSelected( Point2I cell )
  166. {
  167. Parent::onCellSelected( cell );
  168. U32 size;
  169. ConsoleLogEntry* log;
  170. ConsoleLogEntry& entry = mFilteredLog[cell.y];
  171. onMessageSelected_callback( entry.mLevel, entry.mString );
  172. }
  173. void GuiConsole::setDisplayFilters(bool errors, bool warns, bool normal)
  174. {
  175. mDisplayErrors = errors;
  176. mDisplayWarnings = warns;
  177. mDisplayNormalMessages = normal;
  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. }
  188. DefineEngineMethod(GuiConsole, setDisplayFilters, void, (bool errors, bool warns, bool normal), (true, true, true),
  189. "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"
  190. "@param errors If true, the console gui will display any error messages that were emitted.\n\n"
  191. "@param warns If true, the console gui will display any warning messages that were emitted.\n\n"
  192. "@param normal If true, the console gui will display any regular messages that were emitted.\n\n")
  193. {
  194. object->setDisplayFilters(errors, warns, normal);
  195. }
  196. DefineEngineMethod(GuiConsole, getErrorFilter, bool, (), ,
  197. "Returns if the error filter is on or not.")
  198. {
  199. return object->getErrorFilter();
  200. }
  201. DefineEngineMethod(GuiConsole, getWarnFilter, bool, (), ,
  202. "Returns if the warning filter is on or not.")
  203. {
  204. return object->getWarnFilter();
  205. }
  206. DefineEngineMethod(GuiConsole, getNormalFilter, bool, (), ,
  207. "Returns if the normal message filter is on or not.")
  208. {
  209. return object->getNormalFilter();
  210. }
  211. DefineEngineMethod(GuiConsole, toggleErrorFilter, void, (), ,
  212. "Toggles the error filter.")
  213. {
  214. object->toggleErrorFilter();
  215. }
  216. DefineEngineMethod(GuiConsole, toggleWarnFilter, void, (), ,
  217. "Toggles the warning filter.")
  218. {
  219. object->toggleWarnFilter();
  220. }
  221. DefineEngineMethod(GuiConsole, toggleNormalFilter, void, (), ,
  222. "Toggles the normal messages filter.")
  223. {
  224. object->toggleNormalFilter();
  225. }