MapOverlayLogVisualizer.cpp 2.1 KB

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