guiConsole.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. //-----------------------------------------------------------------------------
  46. GuiConsole::GuiConsole()
  47. {
  48. setExtent(64, 64);
  49. mCellSize.set(1, 1);
  50. mSize.set(1, 0);
  51. }
  52. //-----------------------------------------------------------------------------
  53. bool GuiConsole::onWake()
  54. {
  55. if (! Parent::onWake())
  56. return false;
  57. //get the font
  58. mFont = mProfile->mFont;
  59. return true;
  60. }
  61. //-----------------------------------------------------------------------------
  62. S32 GuiConsole::getMaxWidth(S32 startIndex, S32 endIndex)
  63. {
  64. //sanity check
  65. U32 size;
  66. ConsoleLogEntry *log;
  67. Con::getLockLog(log, size);
  68. if(startIndex < 0 || (U32)endIndex >= size || startIndex > endIndex)
  69. return 0;
  70. S32 result = 0;
  71. for(S32 i = startIndex; i <= endIndex; i++)
  72. result = getMax(result, (S32)(mFont->getStrWidth((const UTF8 *)log[i].mString)));
  73. Con::unlockLog();
  74. return(result + 6);
  75. }
  76. //-----------------------------------------------------------------------------
  77. void GuiConsole::onPreRender()
  78. {
  79. //see if the size has changed
  80. U32 prevSize = getHeight() / mCellSize.y;
  81. U32 size;
  82. ConsoleLogEntry *log;
  83. Con::getLockLog(log, size);
  84. Con::unlockLog(); // we unlock immediately because we only use size here, not log.
  85. if(size != prevSize)
  86. {
  87. //first, find out if the console was scrolled up
  88. bool scrolled = false;
  89. GuiScrollCtrl *parent = dynamic_cast<GuiScrollCtrl*>(getParent());
  90. if(parent)
  91. scrolled = parent->isScrolledToBottom();
  92. //find the max cell width for the new entries
  93. S32 newMax = getMaxWidth(prevSize, size - 1);
  94. if(newMax > mCellSize.x)
  95. mCellSize.set(newMax, mFont->getHeight());
  96. //set the array size
  97. mSize.set(1, size);
  98. //resize the control
  99. setExtent( Point2I(mCellSize.x, mCellSize.y * size));
  100. //if the console was not scrolled, make the last entry visible
  101. if (scrolled)
  102. scrollCellVisible(Point2I(0,mSize.y - 1));
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. void GuiConsole::onRenderCell(Point2I offset, Point2I cell, bool /*selected*/, bool /*mouseOver*/)
  107. {
  108. U32 size;
  109. ConsoleLogEntry *log;
  110. Con::getLockLog(log, size);
  111. ConsoleLogEntry &entry = log[cell.y];
  112. switch (entry.mLevel)
  113. {
  114. case ConsoleLogEntry::Normal: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColor); break;
  115. case ConsoleLogEntry::Warning: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorHL); break;
  116. case ConsoleLogEntry::Error: GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColorNA); break;
  117. default: AssertFatal(false, "GuiConsole::onRenderCell - Unrecognized ConsoleLogEntry type, update this.");
  118. }
  119. GFX->getDrawUtil()->drawText(mFont, Point2I(offset.x + 3, offset.y), entry.mString, mProfile->mFontColors);
  120. Con::unlockLog();
  121. }
  122. //-----------------------------------------------------------------------------
  123. void GuiConsole::onCellSelected( Point2I cell )
  124. {
  125. Parent::onCellSelected( cell );
  126. U32 size;
  127. ConsoleLogEntry* log;
  128. Con::getLockLog(log, size);
  129. ConsoleLogEntry& entry = log[ cell.y ];
  130. onMessageSelected_callback( entry.mLevel, entry.mString );
  131. Con::unlockLog();
  132. }