guiConsole.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "graphics/dgl.h"
  24. #include "gui/guiTypes.h"
  25. #include "gui/guiControl.h"
  26. #include "gui/guiConsole.h"
  27. #include "gui/containers/guiScrollCtrl.h"
  28. IMPLEMENT_CONOBJECT(GuiConsole);
  29. GuiConsole::GuiConsole()
  30. {
  31. mBounds.extent.set(64, 64);
  32. mCellSize.set(1, 1);
  33. mSize.set(1, 0);
  34. }
  35. bool GuiConsole::onWake()
  36. {
  37. if (! Parent::onWake())
  38. return false;
  39. return true;
  40. }
  41. S32 GuiConsole::getMaxWidth(S32 startIndex, S32 endIndex)
  42. {
  43. //sanity check
  44. U32 size;
  45. ConsoleLogEntry *log;
  46. Con::getLockLog(log, size);
  47. if(startIndex < 0 || (U32)endIndex >= size || startIndex > endIndex)
  48. return 0;
  49. S32 result = 0;
  50. for(S32 i = startIndex; i <= endIndex; i++)
  51. result = getMax(result, (S32)(mProfile->getFont(mFontSizeAdjust)->getStrWidth((const UTF8 *)log[i].mString)));
  52. Con::unlockLog();
  53. return(result + 6);
  54. }
  55. void GuiConsole::onPreRender()
  56. {
  57. //see if the size has changed
  58. U32 size;
  59. ConsoleLogEntry *log;
  60. Con::getLockLog(log, size);
  61. Con::unlockLog(); // we unlock immediately because we only use size here, not log.
  62. U32 prevSize = mBounds.extent.y / mCellSize.y;
  63. if ( prevSize > size )
  64. prevSize = 0;
  65. if(size != prevSize)
  66. {
  67. //first, find out if the console was scrolled up
  68. bool scrolled = false;
  69. GuiScrollCtrl *parent = dynamic_cast<GuiScrollCtrl*>(getParent());
  70. if(parent)
  71. scrolled = parent->isScrolledToBottom();
  72. //find the max cell width for the new entries
  73. S32 newMax = getMaxWidth(0, size - 1);
  74. if(newMax > mCellSize.x)
  75. mCellSize.set(newMax, mProfile->getFont(mFontSizeAdjust)->getHeight());
  76. //set the array size
  77. mSize.set(1, size);
  78. //resize the control
  79. resize(mBounds.point, Point2I(mCellSize.x, mCellSize.y * size));
  80. //if the console was scrolled to the bottom then make sure any new row is also visible
  81. if (scrolled)
  82. scrollCellVisible(Point2I(0,mSize.y - 1));
  83. }
  84. }
  85. void GuiConsole::onRenderCell(Point2I offset, Point2I cell, bool selected, bool mouseOver)
  86. {
  87. U32 size;
  88. ConsoleLogEntry *log;
  89. Con::getLockLog(log, size);
  90. ConsoleLogEntry &entry = log[cell.y];
  91. switch (entry.mLevel)
  92. {
  93. case ConsoleLogEntry::Normal: dglSetBitmapModulation(mProfile->mFontColor); break;
  94. case ConsoleLogEntry::Warning: dglSetBitmapModulation(mProfile->mFontColorHL); break;
  95. case ConsoleLogEntry::Error: dglSetBitmapModulation(mProfile->mFontColorNA); break;
  96. case ConsoleLogEntry::NUM_CLASS: Con::errorf("Unhandled case in GuiConsole::onRenderCell, NUM_CLASS");
  97. }
  98. dglDrawText(mProfile->getFont(mFontSizeAdjust), Point2I(offset.x + 3, offset.y), entry.mString, mProfile->mFontColors);
  99. Con::unlockLog();
  100. }