paint_command.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <sstream>
  17. #include "paint_command.hpp"
  18. class PaintCommandImpl : public CommandImpl
  19. {
  20. public:
  21. typedef std::vector<CL_Point> Points;
  22. Points points;
  23. TilemapLayer tilemap;
  24. TileBrush brush;
  25. /** Copy of the field used to generate undo informations */
  26. Field<int> undo_field;
  27. CL_Point pos;
  28. TileBrush* redo_brush;
  29. TileBrush* undo_brush;
  30. PaintCommandImpl() {}
  31. virtual ~PaintCommandImpl() {}
  32. void execute();
  33. void redo();
  34. void undo();
  35. std::string serialize();
  36. };
  37. PaintCommand::PaintCommand(TilemapLayer t, const TileBrush& b)
  38. : impl(new PaintCommandImpl())
  39. {
  40. impl->tilemap = t;
  41. impl->brush = b;
  42. impl->undo_field = *(impl->tilemap.get_field());
  43. impl->redo_brush = 0;
  44. impl->undo_brush = 0;
  45. }
  46. PaintCommand::~PaintCommand()
  47. {
  48. delete impl->redo_brush;
  49. delete impl->undo_brush;
  50. }
  51. void
  52. PaintCommand::add_point(const CL_Point& pos)
  53. {
  54. // FIXME: undo_field is unneeded, should just record the overwritten color
  55. impl->points.push_back(pos);
  56. impl->tilemap.draw_tile(impl->brush, pos);
  57. }
  58. void
  59. PaintCommandImpl::execute()
  60. {
  61. assert(!points.empty());
  62. // Calc bounding rect
  63. CL_Rect rect(points.front().x,
  64. points.front().y,
  65. points.front().x + brush.get_width(),
  66. points.front().y + brush.get_height());
  67. for(PaintCommandImpl::Points::iterator i = points.begin(); i != points.end(); ++i)
  68. {
  69. rect.left = std::min(rect.left, (*i).x);
  70. rect.top = std::min(rect.top, (*i).y);
  71. rect.right = std::max(rect.right, (*i).x + brush.get_width());
  72. rect.bottom = std::max(rect.bottom, (*i).y + brush.get_height());
  73. }
  74. pos.x = rect.left;
  75. pos.y = rect.top;
  76. redo_brush = new TileBrush(*(tilemap.get_field()), rect.get_width(), rect.get_height(),
  77. -pos.x, -pos.y);
  78. // FIXME: undo_field is unneeded, should just record the overwritten color
  79. undo_brush = new TileBrush(undo_field, rect.get_width(), rect.get_height(),
  80. -pos.x, -pos.y);
  81. redo_brush->set_opaque();
  82. undo_brush->set_opaque();
  83. undo_field.clear();
  84. }
  85. void
  86. PaintCommandImpl::redo()
  87. {
  88. TilemapLayer::draw_tiles(tilemap.get_field(), *redo_brush, pos);
  89. }
  90. void
  91. PaintCommandImpl::undo()
  92. {
  93. TilemapLayer::draw_tiles(tilemap.get_field(), *undo_brush, pos);
  94. }
  95. std::string
  96. PaintCommandImpl::serialize()
  97. {
  98. std::stringstream s;
  99. s << "_ = PaintCommand(" << &tilemap << ", " << &brush << ")" << std::endl;
  100. for(PaintCommandImpl::Points::iterator i = points.begin(); i != points.end(); ++i)
  101. {
  102. s << "_.add_paint(" << i->x << ", " << i->y << ")" << std::endl;
  103. }
  104. s << "_ = None" << std::endl;
  105. return s.str();
  106. }
  107. Command
  108. PaintCommand::to_command()
  109. {
  110. return Command(impl);
  111. }
  112. /* EOF */