object_layer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "object_layer.hpp"
  17. #include "layer_impl.hpp"
  18. #include "graphic_context.hpp"
  19. #include "graphic_context_state.hpp"
  20. ObjectLayer ObjectLayer::current_;
  21. class ObjectLayerImpl : public LayerImpl
  22. {
  23. public:
  24. ObjectLayer::Objects objects;
  25. ObjectLayer::ControlPoints control_points;
  26. ObjectLayerImpl() {}
  27. virtual ~ObjectLayerImpl() {}
  28. void draw(GraphicContext& gc) override ;
  29. bool has_bounding_rect() const override { return false; }
  30. };
  31. ObjectLayer::ObjectLayer() :
  32. impl(new ObjectLayerImpl)
  33. {
  34. }
  35. ObjectLayer::~ObjectLayer()
  36. {
  37. }
  38. void
  39. ObjectLayerImpl::draw(GraphicContext& gc)
  40. {
  41. for(ObjectLayer::Objects::iterator i = objects.begin(); i != objects.end(); ++i)
  42. {
  43. if (gc.get_clip_rect().is_overlapped((*i).get_bound_rect()))
  44. {
  45. (*i).draw(gc);
  46. }
  47. }
  48. for(ObjectLayer::ControlPoints::iterator i = control_points.begin(); i != control_points.end(); ++i)
  49. {
  50. (*i).draw(gc);
  51. }
  52. }
  53. ObjMapControlPoint
  54. ObjectLayer::find_control_point(const Pointf& click_pos)
  55. {
  56. for(ControlPoints::reverse_iterator i = impl->control_points.rbegin();
  57. i != impl->control_points.rend();
  58. ++i)
  59. {
  60. Rect rect = (*i).get_bound_rect();
  61. if (rect.is_inside(Point(click_pos)))
  62. return *i;
  63. }
  64. return ObjMapControlPoint();
  65. }
  66. ObjMapObject
  67. ObjectLayer::find_object(const Pointf& click_pos)
  68. {
  69. for(Objects::reverse_iterator i = impl->objects.rbegin(); i != impl->objects.rend(); ++i)
  70. {
  71. Rectf rect = (*i).get_bound_rect();
  72. if (rect.is_inside(Point(click_pos)))
  73. return *i;
  74. }
  75. return ObjMapObject();
  76. }
  77. void
  78. ObjectLayer::delete_object(const ObjMapObject& obj)
  79. {
  80. for(Objects::iterator i = impl->objects.begin(); i != impl->objects.end(); ++i)
  81. {
  82. if (obj == (*i))
  83. {
  84. impl->objects.erase(i);
  85. break;
  86. }
  87. }
  88. }
  89. ObjectLayer::Objects
  90. ObjectLayer::get_selection(const Rectf& rect)
  91. {
  92. std::cout << "========= get_selection: " << impl->objects.size() << std::endl;
  93. Objects selection;
  94. for(Objects::iterator i = impl->objects.begin(); i != impl->objects.end(); ++i)
  95. {
  96. std::cout << " Rect("
  97. << rect.left << ", " << rect.top << ", "
  98. << rect.right << ", " << rect.bottom << ") === "
  99. << (*i).get_pos().x << ", " << (*i).get_pos().y
  100. << std::endl;
  101. if (rect.is_inside((*i).get_pos()))
  102. {
  103. selection.push_back(*i);
  104. }
  105. }
  106. return selection;
  107. }
  108. ObjectLayer::Objects
  109. ObjectLayer::get_objects()
  110. {
  111. return impl->objects;
  112. }
  113. void
  114. ObjectLayer::add_object(const ObjMapObject& obj)
  115. {
  116. impl->objects.push_back(obj);
  117. }
  118. void
  119. ObjectLayer::add_control_point(const ObjMapControlPoint& obj)
  120. {
  121. impl->control_points.push_back(obj);
  122. }
  123. void
  124. ObjectLayer::delete_control_points()
  125. {
  126. impl->control_points.clear();
  127. }
  128. Layer
  129. ObjectLayer::to_layer()
  130. {
  131. return Layer(impl);
  132. }
  133. int
  134. ObjectLayer::get_object_index(const ObjMapObject& obj)
  135. {
  136. Objects::size_type i;
  137. for(i = 0; i < impl->objects.size(); ++i)
  138. {
  139. if (impl->objects[i] == obj)
  140. {
  141. return i;
  142. }
  143. }
  144. return -1;
  145. }
  146. void
  147. ObjectLayer::move_to(const ObjMapObject& obj, int height)
  148. {
  149. // FIXME: Implement me
  150. }
  151. void
  152. ObjectLayer::raise(const ObjMapObject& obj)
  153. {
  154. int i = get_object_index(obj);
  155. if (i != -1 && impl->objects.size() > 1 && i < int(impl->objects.size())-1)
  156. {
  157. std::swap(impl->objects[i], impl->objects[i+1]);
  158. }
  159. }
  160. void
  161. ObjectLayer::lower(const ObjMapObject& obj)
  162. {
  163. int i = get_object_index(obj);
  164. if (i != -1 && i > 0)
  165. {
  166. std::swap(impl->objects[i], impl->objects[i-1]);
  167. }
  168. }
  169. /* EOF */