object_layer.cpp 4.0 KB

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