MapOverlayLogVisualizer.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/IRenderHandler.h"
  19. #include "../render/Graphics.h"
  20. #include "../gui/TextAlignment.h"
  21. #include "../gui/CGuiHandler.h"
  22. MapOverlayLogVisualizer::MapOverlayLogVisualizer(Canvas & target, std::shared_ptr<MapViewModel> model)
  23. : target(target), model(model)
  24. {
  25. }
  26. void MapOverlayLogVisualizer::drawLine(int3 start, int3 end)
  27. {
  28. const Point offset = Point(30, 30);
  29. auto level = model->getLevel();
  30. if(start.z != level || end.z != level)
  31. return;
  32. auto pStart = model->getTargetTileArea(start).topLeft();
  33. auto pEnd = model->getTargetTileArea(end).topLeft();
  34. auto viewPort = target.getRenderArea();
  35. pStart.x += 3;
  36. pEnd.x -= 3;
  37. pStart += offset;
  38. pEnd += offset;
  39. if(viewPort.isInside(pStart) && viewPort.isInside(pEnd))
  40. {
  41. target.drawLine(pStart, pEnd, ColorRGBA(255, 255, 0), ColorRGBA(255, 0, 0));
  42. }
  43. }
  44. void MapOverlayLogVisualizer::drawText(
  45. int3 tile,
  46. int lineNumber,
  47. const std::string & text,
  48. const std::optional<ColorRGBA> & background)
  49. {
  50. const Point offset = Point(6, 6);
  51. auto level = model->getLevel();
  52. if(tile.z != level)
  53. return;
  54. auto pStart = offset + model->getTargetTileArea(tile).topLeft();
  55. auto viewPort = target.getRenderArea();
  56. ColorRGBA color = Colors::YELLOW;
  57. if(background)
  58. {
  59. color = ((background->b + background->r + background->g) < 300)
  60. ? Colors::WHITE
  61. : Colors::BLACK;
  62. }
  63. if(viewPort.isInside(pStart))
  64. {
  65. const auto & font = GH.renderHandler().loadFont(FONT_TINY);
  66. int w = font->getStringWidth(text);
  67. int h = font->getLineHeight();
  68. pStart.y += h * lineNumber;
  69. if(background)
  70. {
  71. target.drawColor(Rect(pStart, Point(w + 4, h)), *background);
  72. pStart.x += 2;
  73. }
  74. target.drawText(pStart, EFonts::FONT_TINY, color, ETextAlignment::TOPLEFT, text);
  75. }
  76. }