editor_map_component.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <iostream>
  17. #include <ClanLib/Display/keyboard.h>
  18. #include <ClanLib/Display/mouse.h>
  19. #include <ClanLib/Display/display.h>
  20. #include <ClanLib/Display/display_window.h>
  21. #include "editor_map.hpp"
  22. #include "scrollbar.hpp"
  23. #include "editor_map_component.hpp"
  24. EditorMapComponent* EditorMapComponent::current_ = 0;
  25. class EditorMapComponentImpl
  26. {
  27. public:
  28. EditorMapComponent* parent;
  29. GraphicContextState gc_state;
  30. Scrollbar* scrollbar_h;
  31. Scrollbar* scrollbar_v;
  32. CL_SlotContainer slots;
  33. Workspace workspace;
  34. CL_Signal_v2<int, int> key_bindings[256];
  35. EditorMapComponentImpl()
  36. :workspace(true)
  37. {}
  38. void draw();
  39. void mouse_up (const CL_InputEvent& event);
  40. void mouse_down(const CL_InputEvent& event);
  41. void mouse_move(const CL_InputEvent& event);
  42. void on_key_up(const CL_InputEvent& event);
  43. void on_key_down(const CL_InputEvent& event);
  44. void on_resize(int old_w, int old_h);
  45. };
  46. EditorMapComponent::EditorMapComponent(const CL_Rect& rect, CL_Component* parent)
  47. : CL_Component(rect, parent),
  48. impl(new EditorMapComponentImpl())
  49. {
  50. impl->parent = this;
  51. impl->gc_state = GraphicContextState(rect.get_width(), rect.get_height());
  52. current_ = this;
  53. impl->scrollbar_v = new Scrollbar(CL_Rect(CL_Point(rect.get_width() - 14, 2) + CL_Point(rect.left, rect.top),
  54. CL_Size(12, rect.get_height() - 4 - 14)),
  55. Scrollbar::VERTICAL,
  56. parent);
  57. impl->scrollbar_h = new Scrollbar(CL_Rect(CL_Point(2, rect.get_height() - 14) + CL_Point(rect.left, rect.top),
  58. CL_Size(rect.get_width() - 4 - 14, 12)),
  59. Scrollbar::HORIZONTAL,
  60. parent);
  61. impl->slots.connect(impl->scrollbar_h->sig_scrollbar_move(), this, &EditorMapComponent::move_to_x);
  62. impl->slots.connect(impl->scrollbar_v->sig_scrollbar_move(), this, &EditorMapComponent::move_to_y);
  63. impl->slots.connect(sig_paint(), impl.get(), &EditorMapComponentImpl::draw);
  64. impl->slots.connect(sig_mouse_up(), impl.get(), &EditorMapComponentImpl::mouse_up);
  65. impl->slots.connect(sig_mouse_down(), impl.get(), &EditorMapComponentImpl::mouse_down);
  66. impl->slots.connect(sig_mouse_move(), impl.get(), &EditorMapComponentImpl::mouse_move);
  67. impl->slots.connect(sig_key_down(), impl.get(), &EditorMapComponentImpl::on_key_down);
  68. impl->slots.connect(sig_key_up(), impl.get(), &EditorMapComponentImpl::on_key_up);
  69. impl->slots.connect(sig_resize(), impl.get(), &EditorMapComponentImpl::on_resize);
  70. }
  71. EditorMapComponent::~EditorMapComponent()
  72. {
  73. std::cout << "~EditorMapComponent()" << std::endl;
  74. }
  75. Workspace
  76. EditorMapComponent::get_workspace() const
  77. {
  78. return impl->workspace;
  79. }
  80. void
  81. EditorMapComponent::set_workspace(Workspace m)
  82. {
  83. impl->workspace = m;
  84. }
  85. void
  86. EditorMapComponentImpl::on_key_down(const CL_InputEvent& event)
  87. {
  88. if (event.id >= 0 && event.id < 256)
  89. {
  90. CL_Rect rect = parent->get_position();
  91. key_bindings[event.id](CL_Mouse::get_x() - rect.left,
  92. CL_Mouse::get_y() - rect.top);
  93. }
  94. if (event.repeat_count == 0)
  95. {
  96. CL_Rect rect = parent->get_position();
  97. CL_InputEvent ev2 = event;
  98. ev2.mouse_pos = CL_Point(CL_Mouse::get_x() - rect.left,
  99. CL_Mouse::get_y() - rect.top);
  100. workspace.key_down(ev2);
  101. }
  102. }
  103. void
  104. EditorMapComponentImpl::on_key_up(const CL_InputEvent& event)
  105. {
  106. CL_Rect rect = parent->get_position();
  107. CL_InputEvent ev2 = event;
  108. ev2.mouse_pos = CL_Point(CL_Mouse::get_x() - rect.left,
  109. CL_Mouse::get_y() - rect.top);
  110. workspace.key_up(ev2);
  111. }
  112. void
  113. EditorMapComponentImpl::mouse_up(const CL_InputEvent& event)
  114. {
  115. workspace.mouse_up(event);
  116. }
  117. void
  118. EditorMapComponentImpl::mouse_move(const CL_InputEvent& event)
  119. {
  120. workspace.mouse_move(event);
  121. }
  122. void
  123. EditorMapComponentImpl::mouse_down(const CL_InputEvent& event)
  124. {
  125. workspace.mouse_down(event);
  126. }
  127. void
  128. EditorMapComponentImpl::draw ()
  129. {
  130. if (workspace.get_map().is_null()) return;
  131. CL_Display::push_cliprect(parent->get_screen_rect());
  132. CL_Display::push_translate(parent->get_screen_x(), parent->get_screen_y());
  133. // Update scrollbars (FIXME: move me to function)
  134. scrollbar_v->set_range(0, workspace.get_map().get_bounding_rect().get_height());
  135. scrollbar_v->set_pagesize(parent->get_height()/gc_state.get_zoom());
  136. scrollbar_v->set_pos(gc_state.get_pos().y);
  137. scrollbar_h->set_range(0, workspace.get_map().get_bounding_rect().get_width());
  138. scrollbar_h->set_pagesize(parent->get_width()/gc_state.get_zoom());
  139. scrollbar_h->set_pos(gc_state.get_pos().x);
  140. gc_state.push();
  141. workspace.draw(gc_state, CL_Display::get_current_window()->get_gc());
  142. gc_state.pop();
  143. CL_Display::pop_modelview();
  144. CL_Display::pop_cliprect();
  145. }
  146. CL_Pointf
  147. EditorMapComponent::screen2world(const CL_Point& pos)
  148. {
  149. return impl->gc_state.screen2world(pos);
  150. }
  151. void
  152. EditorMapComponent::set_zoom(float z)
  153. {
  154. impl->gc_state.set_zoom(z);
  155. }
  156. void
  157. EditorMapComponent::zoom_out(CL_Point pos)
  158. {
  159. impl->gc_state.set_zoom(CL_Pointf(pos.x, pos.y),
  160. impl->gc_state.get_zoom()/1.25f);
  161. }
  162. void
  163. EditorMapComponent::zoom_in(CL_Point pos)
  164. {
  165. impl->gc_state.set_zoom(CL_Pointf(pos.x, pos.y),
  166. impl->gc_state.get_zoom()*1.25f);
  167. }
  168. void
  169. EditorMapComponent::zoom_to(CL_Rectf rect)
  170. {
  171. impl->gc_state.zoom_to(rect);
  172. }
  173. CL_Rectf
  174. EditorMapComponent::get_clip_rect()
  175. {
  176. return impl->gc_state.get_clip_rect();
  177. }
  178. void
  179. EditorMapComponent::move_to(int x, int y)
  180. {
  181. impl->gc_state.set_pos(CL_Pointf(x, y));
  182. }
  183. void
  184. EditorMapComponent::move_to_x(float x)
  185. {
  186. impl->gc_state.set_pos(CL_Pointf(x, impl->gc_state.get_pos().y));
  187. }
  188. void
  189. EditorMapComponent::move_to_y(float y)
  190. {
  191. impl->gc_state.set_pos(CL_Pointf(impl->gc_state.get_pos().x, y));
  192. }
  193. void
  194. EditorMapComponentImpl::on_resize(int old_w, int old_h)
  195. {
  196. CL_Rect rect = parent->get_screen_rect();
  197. scrollbar_v->set_position(rect.get_width() - 14 + rect.left, 2 + rect.top);
  198. scrollbar_v->set_size(12, rect.get_height() - 4 - 14);
  199. scrollbar_h->set_position(2 + rect.left, rect.get_height() - 14 + rect.top);
  200. scrollbar_h->set_size(rect.get_width() - 4 - 14, 12);
  201. gc_state.set_size(rect.get_width(), rect.get_height());
  202. }
  203. CL_Signal_v2<int, int>&
  204. EditorMapComponent::sig_on_key(const std::string& str)
  205. {
  206. int id = CL_Keyboard::get_device().string_to_keyid(str);
  207. //std::cout << str << " => " << id << std::endl;
  208. if (id > 0 && id < 256)
  209. {
  210. return impl->key_bindings[id];
  211. }
  212. else
  213. {
  214. std::cout << "EditorMapComponent::sig_on_key: invalid key id: " << id << std::endl;
  215. return impl->key_bindings[0];
  216. }
  217. }
  218. GraphicContextState&
  219. EditorMapComponent::get_gc_state()
  220. {
  221. return impl->gc_state;
  222. }
  223. /* EOF */