objmap_path_node.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmail.com>
  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 "objmap_path_node.hpp"
  17. #include "color.hpp"
  18. #include "graphic_context.hpp"
  19. #include "objmap_object_impl.hpp"
  20. class ObjMapPathNodeImpl : public ObjMapObjectImpl
  21. {
  22. public:
  23. ObjMapPathNodeImpl* prev;
  24. ObjMapPathNodeImpl* next;
  25. ObjMapPathNodeImpl();
  26. void draw(GraphicContext& gc);
  27. Rectf get_bound_rect() const override;
  28. };
  29. ObjMapPathNodeImpl::ObjMapPathNodeImpl()
  30. {
  31. next = 0;
  32. prev = 0;
  33. }
  34. void
  35. ObjMapPathNodeImpl::draw(GraphicContext& gc)
  36. {
  37. gc.fill_rect(Rect(Point(pos) - Point(16,16), Size(32, 32)),
  38. Color(200, 255, 200));
  39. if (next)
  40. {
  41. gc.draw_line(static_cast<int>(pos.x), static_cast<int>(pos.y),
  42. static_cast<int>((pos.x + next->pos.x)/2),
  43. static_cast<int>((pos.y+next->pos.y)/2),
  44. Color(255, 255, 0));
  45. gc.draw_line(static_cast<int>((pos.x + next->pos.x)/2),
  46. static_cast<int>((pos.y+next->pos.y)/2),
  47. static_cast<int>(next->pos.x),
  48. static_cast<int>(next->pos.y),
  49. Color(255, 0, 0));
  50. }
  51. }
  52. Rectf
  53. ObjMapPathNodeImpl::get_bound_rect() const
  54. {
  55. return Rectf(pos - Pointf(16,16), Sizef(32, 32));
  56. }
  57. ObjMapPathNode::ObjMapPathNode(const Pointf& pos_,
  58. const MetaData& data_)
  59. : impl(new ObjMapPathNodeImpl())
  60. {
  61. impl->pos = pos_;
  62. impl->data = data_;
  63. }
  64. void
  65. ObjMapPathNode::disconnect()
  66. {
  67. impl->next = 0;
  68. impl->prev = 0;
  69. impl->next->prev = 0;
  70. impl->prev->next = 0;
  71. }
  72. void
  73. ObjMapPathNode::connect(ObjMapPathNode next)
  74. {
  75. if (next.impl->next != impl.get()) // avoid circular link between two nodes
  76. {
  77. if (next.impl->prev) // ensure that each node links exactly to one prev and one next node
  78. {
  79. next.impl->prev->next = 0;
  80. next.impl->prev = 0;
  81. }
  82. impl->next = next.impl.get();
  83. next.impl->prev = impl.get();
  84. }
  85. }
  86. ObjMapObject
  87. ObjMapPathNode::to_object()
  88. {
  89. return ObjMapObject(impl);
  90. }
  91. /* EOF */