GraphicalPrimitiveCanvas.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * GraphicalPrimitiveCanvas.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 "GraphicalPrimitiveCanvas.h"
  12. #include "../render/Canvas.h"
  13. GraphicalPrimitiveCanvas::GraphicalPrimitiveCanvas(Rect dimensions)
  14. {
  15. pos = dimensions + pos.topLeft();
  16. }
  17. void GraphicalPrimitiveCanvas::showAll(Canvas & to)
  18. {
  19. auto const & translatePoint = [this](const Point & input){
  20. int x = input.x < 0 ? pos.w + input.x : input.x;
  21. int y = input.y < 0 ? pos.h + input.y : input.y;
  22. return Point(x,y);
  23. };
  24. for (auto const & entry : primitives)
  25. {
  26. switch (entry.type)
  27. {
  28. case PrimitiveType::LINE:
  29. {
  30. to.drawLine(pos.topLeft() + translatePoint(entry.a), pos.topLeft() + translatePoint(entry.b), entry.color, entry.color);
  31. break;
  32. }
  33. case PrimitiveType::FILLED_BOX:
  34. {
  35. to.drawColorBlended(Rect(pos.topLeft() + translatePoint(entry.a), translatePoint(entry.b)), entry.color);
  36. break;
  37. }
  38. case PrimitiveType::RECTANGLE:
  39. {
  40. to.drawBorder(Rect(pos.topLeft() + translatePoint(entry.a), translatePoint(entry.b)), entry.color);
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. void GraphicalPrimitiveCanvas::addLine(const Point & from, const Point & to, const ColorRGBA & color)
  47. {
  48. primitives.push_back({color, from, to, PrimitiveType::LINE});
  49. }
  50. void GraphicalPrimitiveCanvas::addBox(const Point & topLeft, const Point & size, const ColorRGBA & color)
  51. {
  52. primitives.push_back({color, topLeft, size, PrimitiveType::FILLED_BOX});
  53. }
  54. void GraphicalPrimitiveCanvas::addRectangle(const Point & topLeft, const Point & size, const ColorRGBA & color)
  55. {
  56. primitives.push_back({color, topLeft, size, PrimitiveType::RECTANGLE});
  57. }
  58. void GraphicalPrimitiveCanvas::clear()
  59. {
  60. primitives.clear();
  61. redraw();
  62. }
  63. TransparentFilledRectangle::TransparentFilledRectangle(Rect position, ColorRGBA color) :
  64. GraphicalPrimitiveCanvas(position)
  65. {
  66. addBox(Point(0,0), Point(-1, -1), color);
  67. }
  68. TransparentFilledRectangle::TransparentFilledRectangle(Rect position, ColorRGBA color, ColorRGBA colorLine, int width) :
  69. GraphicalPrimitiveCanvas(position)
  70. {
  71. addBox(Point(0,0), Point(-1, -1), color);
  72. for (int i = 0; i < width; ++i)
  73. addRectangle(Point(i,i), Point(-1-i*2, -1-i*2), colorLine);
  74. }
  75. SimpleLine::SimpleLine(Point pos1, Point pos2, ColorRGBA color) :
  76. GraphicalPrimitiveCanvas(Rect(pos1, pos2 - pos1))
  77. {
  78. addLine(Point(0,0), Point(-1, -1), color);
  79. }