object_selector.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/Core/core_iostream.h>
  18. #include <ClanLib/display.h>
  19. //#include "editor_map.hpp"
  20. #include "editor_map_component.hpp"
  21. #include "object_selector.hpp"
  22. #include "object_add_command.hpp"
  23. ObjectSelector::ObjectSelector(const CL_Rect& rect,
  24. int obj_w, int obj_h,
  25. CL_Component* parent)
  26. : CL_Component(rect, parent),
  27. width(rect.get_width()/obj_w), height(rect.get_height()/obj_h),
  28. obj_width(obj_w), obj_height(obj_h)
  29. {
  30. index = 0;
  31. slots.push_back(sig_paint().connect(this, &ObjectSelector::draw));
  32. slots.push_back(sig_mouse_move().connect(this, &ObjectSelector::mouse_move));
  33. slots.push_back(sig_mouse_down().connect(this, &ObjectSelector::mouse_down));
  34. slots.push_back(sig_mouse_up().connect(this, &ObjectSelector::mouse_up));
  35. mouse_over_tile = -1;
  36. scrolling = false;
  37. offset = 0;
  38. scale = 1.0f;
  39. drag_obj = -1;
  40. }
  41. ObjectSelector::~ObjectSelector()
  42. {
  43. }
  44. void
  45. ObjectSelector::mouse_up(const CL_InputEvent& event)
  46. {
  47. switch(event.id)
  48. {
  49. case CL_MOUSE_LEFT:
  50. {
  51. if (drag_obj != -1)
  52. {
  53. release_mouse();
  54. if (!has_mouse_over())
  55. {
  56. CL_Point screen(event.mouse_pos.x + get_screen_rect().left,
  57. event.mouse_pos.y + get_screen_rect().top);
  58. CL_Point target(screen.x - EditorMapComponent::current()->get_screen_rect().left,
  59. screen.y - EditorMapComponent::current()->get_screen_rect().top);
  60. // FIXME: Move this to the scripting layer
  61. //ObjectAddCommand command(ObjectLayer::current());
  62. //ObjMapObject obj = brushes[drag_obj].to_sprite_object
  63. //(EditorMapComponent::current()->screen2world(target)).to_object();
  64. //command.add_object(obj);
  65. //Workspace::current().get_map().execute(command.to_command());
  66. //std::cout << "C++: Calling on_drop" << std::endl;
  67. on_drop(brushes[drag_obj], target);
  68. //std::cout << "C++: Calling on_drop: done" << std::endl;
  69. }
  70. drag_obj = -1;
  71. }
  72. }
  73. break;
  74. case CL_MOUSE_MIDDLE:
  75. scrolling = false;
  76. release_mouse();
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. void
  83. ObjectSelector::mouse_down(const CL_InputEvent& event)
  84. {
  85. switch(event.id)
  86. {
  87. case CL_MOUSE_LEFT:
  88. {
  89. if (mouse_over_tile != -1)
  90. {
  91. drag_obj = mouse_over_tile;
  92. capture_mouse();
  93. }
  94. }
  95. break;
  96. case CL_MOUSE_MIDDLE:
  97. scrolling = true;
  98. click_pos = event.mouse_pos;
  99. old_offset = offset;
  100. capture_mouse();
  101. break;
  102. case CL_MOUSE_WHEEL_UP:
  103. offset -= static_cast<int>(obj_height*scale);
  104. break;
  105. case CL_MOUSE_WHEEL_DOWN:
  106. offset += static_cast<int>(obj_height*scale);
  107. break;
  108. }
  109. }
  110. void
  111. ObjectSelector::mouse_move(const CL_InputEvent& event)
  112. {
  113. if (scrolling)
  114. {
  115. offset = old_offset + (click_pos.y - event.mouse_pos.y);
  116. }
  117. mouse_pos = event.mouse_pos;
  118. int x = (event.mouse_pos.x)/static_cast<int>(obj_width);
  119. int y = (event.mouse_pos.y+offset)/static_cast<int>(obj_height);
  120. mouse_over_tile = y * width + x;
  121. if (mouse_over_tile < 0 || mouse_over_tile >= (int)brushes.size())
  122. mouse_over_tile = -1;
  123. }
  124. void
  125. ObjectSelector::draw()
  126. {
  127. if (offset < 0)
  128. offset = 0;
  129. CL_Display::push_cliprect(get_screen_rect());
  130. // Handle scrolling in the Component
  131. CL_Display::push_modelview();
  132. CL_Display::add_translate(0, -offset);
  133. CL_Display::add_translate(get_screen_x(), get_screen_y());
  134. for(int i = 0; i < (int)brushes.size(); ++i)
  135. {
  136. int x = i%width;
  137. int y = i/width;
  138. CL_Rectf rect(CL_Pointf(x * obj_width, y * obj_height),
  139. CL_Sizef(obj_width, obj_height));
  140. CL_Sprite sprite = brushes[i].get_sprite();
  141. sprite.set_alignment(origin_center, 0, 0);
  142. sprite.set_scale(std::min(1.0f, (float)obj_width/(float)sprite.get_width()),
  143. std::min(1.0f, (float)obj_height/(float)sprite.get_height()));
  144. sprite.draw(x * obj_width + obj_width/2,
  145. y * obj_height + obj_height/2);
  146. //CL_Display::draw_rect(rect, CL_Color(0,0,0,128));
  147. if (mouse_over_tile == i && has_mouse_over())
  148. {
  149. CL_Display::fill_rect(rect, CL_Color(0,0,255, 20));
  150. }
  151. }
  152. CL_Display::pop_modelview();
  153. CL_Display::pop_cliprect();
  154. // Draw drag sprite
  155. if (drag_obj != -1)
  156. {
  157. CL_Display::set_cliprect(CL_Rect(CL_Point(0, 0),
  158. CL_Size(CL_Display::get_width(),
  159. CL_Display::get_height())));
  160. CL_Sprite sprite = brushes[drag_obj].get_sprite();
  161. sprite.set_alpha(0.5f);
  162. sprite.draw(mouse_pos.x + get_screen_x(), mouse_pos.y + get_screen_y());
  163. }
  164. }
  165. void
  166. ObjectSelector::add_brush(const ObjectBrush& brush)
  167. {
  168. brushes.push_back(brush);
  169. }
  170. CL_Signal_v2<ObjectBrush, CL_Point>&
  171. ObjectSelector::sig_drop()
  172. {
  173. return on_drop;
  174. }
  175. /* EOF */