MapOverlayLogVisualizer.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * MapOverlayLogVisualizer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MapOverlayLogVisualizer.h"
  12. #include "MapViewModel.h"
  13. #include "../../lib/logging/VisualLogger.h"
  14. #include "../render/Canvas.h"
  15. #include "../render/Colors.h"
  16. #include "../render/EFont.h"
  17. #include "../render/IFont.h"
  18. #include "../render/IScreenHandler.h"
  19. #include "../render/IRenderHandler.h"
  20. #include "../render/Graphics.h"
  21. #include "../gui/TextAlignment.h"
  22. #include "../gui/CGuiHandler.h"
  23. MapOverlayLogVisualizer::MapOverlayLogVisualizer(Canvas & target, std::shared_ptr<MapViewModel> model)
  24. : target(target), model(model)
  25. {
  26. }
  27. void MapOverlayLogVisualizer::drawLine(int3 start, int3 end)
  28. {
  29. auto level = model->getLevel();
  30. if(start.z != level || end.z != level)
  31. return;
  32. int scaling = GH.screenHandler().getScalingFactor();
  33. auto pStart = model->getTargetTileArea(start).center();
  34. auto pEnd = model->getTargetTileArea(end).center();
  35. Rect viewPortRaw = target.getRenderArea();
  36. Rect viewPort(viewPortRaw.topLeft() / scaling, viewPortRaw.dimensions() / scaling );
  37. Point workaroundOffset(8,8); // not sure why it is needed. Removing leads to incorrect clipping near view edges
  38. if(viewPort.isInside(pStart + workaroundOffset) && viewPort.isInside(pEnd + workaroundOffset))
  39. {
  40. target.drawLine(pStart, pEnd, ColorRGBA(255, 255, 0), ColorRGBA(255, 0, 0));
  41. }
  42. }
  43. void MapOverlayLogVisualizer::drawText(
  44. int3 tile,
  45. int lineNumber,
  46. const std::string & text,
  47. const std::optional<ColorRGBA> & background)
  48. {
  49. const Point offset = Point(6, 6);
  50. auto level = model->getLevel();
  51. if(tile.z != level)
  52. return;
  53. auto pStart = offset + model->getTargetTileArea(tile).topLeft();
  54. auto viewPort = target.getRenderArea();
  55. ColorRGBA color = Colors::YELLOW;
  56. if(background)
  57. {
  58. color = ((background->b + background->r + background->g) < 300)
  59. ? Colors::WHITE
  60. : Colors::BLACK;
  61. }
  62. if(viewPort.isInside(pStart))
  63. {
  64. const auto & font = GH.renderHandler().loadFont(FONT_TINY);
  65. int w = font->getStringWidth(text);
  66. int h = font->getLineHeight();
  67. pStart.y += h * lineNumber;
  68. if(background)
  69. {
  70. target.drawColor(Rect(pStart, Point(w + 4, h)), *background);
  71. pStart.x += 2;
  72. }
  73. target.drawText(pStart, EFonts::FONT_TINY, color, ETextAlignment::TOPLEFT, text);
  74. }
  75. }