object_move_command.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "object_layer.hpp"
  17. #include "object_move_command.hpp"
  18. class ObjectMoveCommandImpl : public CommandImpl
  19. {
  20. public:
  21. ObjectMoveCommandImpl() {}
  22. virtual ~ObjectMoveCommandImpl() {}
  23. ObjectLayer objmap;
  24. struct Obj {
  25. CL_Pointf old_pos;
  26. CL_Pointf new_pos;
  27. ObjMapObject obj;
  28. };
  29. typedef std::vector<Obj> Objects;
  30. Objects objects;
  31. void execute();
  32. void redo();
  33. void undo();
  34. std::string serialize();
  35. };
  36. ObjectMoveCommand::ObjectMoveCommand(const ObjectLayer& o)
  37. : impl(new ObjectMoveCommandImpl())
  38. {
  39. impl->objmap = o;
  40. }
  41. ObjectMoveCommand::~ObjectMoveCommand()
  42. {
  43. }
  44. void
  45. ObjectMoveCommandImpl::execute()
  46. {
  47. }
  48. void
  49. ObjectMoveCommand::add_obj(const ObjMapObject& obj)
  50. {
  51. ObjectMoveCommandImpl::Obj o;
  52. o.obj = obj;
  53. o.old_pos = obj.get_pos();
  54. impl->objects.push_back(o);
  55. }
  56. void
  57. ObjectMoveCommand::move_by(const CL_Pointf& delta)
  58. {
  59. for(ObjectMoveCommandImpl::Objects::iterator i = impl->objects.begin();
  60. i != impl->objects.end();
  61. ++i)
  62. {
  63. i->new_pos = i->old_pos + delta;
  64. i->obj.set_pos(i->new_pos);
  65. i->obj.sig_move()(i->obj);
  66. }
  67. }
  68. void
  69. ObjectMoveCommandImpl::redo()
  70. {
  71. for(ObjectMoveCommandImpl::Objects::iterator i = objects.begin();
  72. i != objects.end();
  73. ++i)
  74. {
  75. i->obj.set_pos(i->new_pos);
  76. }
  77. }
  78. void
  79. ObjectMoveCommandImpl::undo()
  80. {
  81. for(ObjectMoveCommandImpl::Objects::iterator i = objects.begin();
  82. i != objects.end();
  83. ++i)
  84. {
  85. i->obj.set_pos(i->old_pos);
  86. }
  87. }
  88. std::string
  89. ObjectMoveCommandImpl::serialize()
  90. {
  91. return "";
  92. }
  93. Command
  94. ObjectMoveCommand::to_command()
  95. {
  96. return Command(impl);
  97. }
  98. /* EOF */