CursorTool.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "CursorTool.h"
  27. #include "FormEditorWidget.h"
  28. #include "FormWidget.h"
  29. #include "WidgetTreeModel.h"
  30. #include <AK/LogStream.h>
  31. #include <LibGfx/Palette.h>
  32. //#define DEBUG_CURSOR_TOOL
  33. namespace HackStudio {
  34. void CursorTool::on_mousedown(GUI::MouseEvent& event)
  35. {
  36. #ifdef DEBUG_CURSOR_TOOL
  37. dbgln("CursorTool::on_mousedown");
  38. #endif
  39. auto& form_widget = m_editor.form_widget();
  40. auto result = form_widget.hit_test(event.position(), GUI::Widget::ShouldRespectGreediness::No);
  41. if (event.button() == GUI::MouseButton::Left) {
  42. if (result.widget && result.widget != &form_widget) {
  43. if (event.modifiers() & Mod_Ctrl) {
  44. m_editor.selection().toggle(*result.widget);
  45. } else if (!event.modifiers()) {
  46. if (!m_editor.selection().contains(*result.widget)) {
  47. #ifdef DEBUG_CURSOR_TOOL
  48. dbg() << "Selection didn't contain " << *result.widget << ", making it the only selected one";
  49. #endif
  50. m_editor.selection().set(*result.widget);
  51. }
  52. m_drag_origin = event.position();
  53. m_positions_before_drag.clear();
  54. m_editor.selection().for_each([&](auto& widget) {
  55. m_positions_before_drag.set(&widget, widget.relative_position());
  56. return IterationDecision::Continue;
  57. });
  58. }
  59. } else {
  60. m_editor.selection().clear();
  61. m_rubber_banding = true;
  62. m_rubber_band_origin = event.position();
  63. m_rubber_band_position = event.position();
  64. form_widget.update();
  65. }
  66. // FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
  67. form_widget.update();
  68. }
  69. }
  70. void CursorTool::on_mouseup(GUI::MouseEvent& event)
  71. {
  72. #ifdef DEBUG_CURSOR_TOOL
  73. dbgln("CursorTool::on_mouseup");
  74. #endif
  75. if (event.button() == GUI::MouseButton::Left) {
  76. auto& form_widget = m_editor.form_widget();
  77. auto result = form_widget.hit_test(event.position(), GUI::Widget::ShouldRespectGreediness::No);
  78. if (!m_dragging && !(event.modifiers() & Mod_Ctrl)) {
  79. if (result.widget && result.widget != &form_widget) {
  80. m_editor.selection().set(*result.widget);
  81. // FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
  82. form_widget.update();
  83. }
  84. }
  85. m_dragging = false;
  86. m_rubber_banding = false;
  87. form_widget.update();
  88. }
  89. }
  90. void CursorTool::on_mousemove(GUI::MouseEvent& event)
  91. {
  92. #ifdef DEBUG_CURSOR_TOOL
  93. dbgln("CursorTool::on_mousemove");
  94. #endif
  95. auto& form_widget = m_editor.form_widget();
  96. if (m_rubber_banding) {
  97. set_rubber_band_position(event.position());
  98. return;
  99. }
  100. if (!m_dragging && event.buttons() & GUI::MouseButton::Left && event.position() != m_drag_origin) {
  101. auto result = form_widget.hit_test(event.position(), GUI::Widget::ShouldRespectGreediness::No);
  102. if (result.widget && result.widget != &form_widget) {
  103. if (!m_editor.selection().contains(*result.widget)) {
  104. m_editor.selection().set(*result.widget);
  105. // FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
  106. form_widget.update();
  107. }
  108. }
  109. m_dragging = true;
  110. }
  111. if (m_dragging) {
  112. auto movement_delta = event.position() - m_drag_origin;
  113. m_editor.selection().for_each([&](auto& widget) {
  114. auto new_rect = widget.relative_rect();
  115. new_rect.set_location(m_positions_before_drag.get(&widget).value_or({}).translated(movement_delta));
  116. new_rect.set_x(new_rect.x() - (new_rect.x() % m_editor.form_widget().grid_size()));
  117. new_rect.set_y(new_rect.y() - (new_rect.y() % m_editor.form_widget().grid_size()));
  118. widget.set_relative_rect(new_rect);
  119. return IterationDecision::Continue;
  120. });
  121. m_editor.model().update();
  122. return;
  123. }
  124. }
  125. void CursorTool::on_keydown(GUI::KeyEvent& event)
  126. {
  127. #ifdef DEBUG_CURSOR_TOOL
  128. dbgln("CursorTool::on_keydown");
  129. #endif
  130. auto move_selected_widgets_by = [this](int x, int y) {
  131. m_editor.selection().for_each([&](auto& widget) {
  132. widget.move_by(x, y);
  133. return IterationDecision::Continue;
  134. });
  135. };
  136. if (event.modifiers() == 0) {
  137. switch (event.key()) {
  138. case Key_Down:
  139. move_selected_widgets_by(0, m_editor.form_widget().grid_size());
  140. break;
  141. case Key_Up:
  142. move_selected_widgets_by(0, -m_editor.form_widget().grid_size());
  143. break;
  144. case Key_Left:
  145. move_selected_widgets_by(-m_editor.form_widget().grid_size(), 0);
  146. break;
  147. case Key_Right:
  148. move_selected_widgets_by(m_editor.form_widget().grid_size(), 0);
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. }
  155. void CursorTool::set_rubber_band_position(const Gfx::IntPoint& position)
  156. {
  157. if (m_rubber_band_position == position)
  158. return;
  159. m_rubber_band_position = position;
  160. auto rubber_band_rect = this->rubber_band_rect();
  161. m_editor.selection().clear();
  162. m_editor.form_widget().for_each_child_widget([&](auto& child) {
  163. if (child.relative_rect().intersects(rubber_band_rect))
  164. m_editor.selection().add(child);
  165. return IterationDecision::Continue;
  166. });
  167. m_editor.form_widget().update();
  168. }
  169. Gfx::IntRect CursorTool::rubber_band_rect() const
  170. {
  171. if (!m_rubber_banding)
  172. return {};
  173. return Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_position);
  174. }
  175. void CursorTool::on_second_paint(GUI::Painter& painter, GUI::PaintEvent&)
  176. {
  177. if (!m_rubber_banding)
  178. return;
  179. auto rect = rubber_band_rect();
  180. painter.fill_rect(rect, m_editor.palette().rubber_band_fill());
  181. painter.draw_rect(rect, m_editor.palette().rubber_band_border());
  182. }
  183. }