CursorTool.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. void CursorTool::on_mousedown(GUI::MouseEvent& event)
  32. {
  33. dbg() << "CursorTool::on_mousedown";
  34. auto& form_widget = m_editor.form_widget();
  35. auto result = form_widget.hit_test(event.position(), GUI::Widget::ShouldRespectGreediness::No);
  36. if (event.button() == GUI::MouseButton::Left) {
  37. if (result.widget && result.widget != &form_widget) {
  38. if (event.modifiers() & Mod_Ctrl) {
  39. m_editor.selection().toggle(*result.widget);
  40. } else if (!event.modifiers()) {
  41. if (!m_editor.selection().contains(*result.widget)) {
  42. dbg() << "Selection didn't contain " << *result.widget << ", making it the only selected one";
  43. m_editor.selection().set(*result.widget);
  44. }
  45. m_drag_origin = event.position();
  46. m_positions_before_drag.clear();
  47. m_editor.selection().for_each([&](auto& widget) {
  48. m_positions_before_drag.set(&widget, widget.relative_position());
  49. return IterationDecision::Continue;
  50. });
  51. }
  52. } else {
  53. m_editor.selection().clear();
  54. m_rubber_banding = true;
  55. m_rubber_band_origin = event.position();
  56. m_rubber_band_position = event.position();
  57. form_widget.update();
  58. }
  59. // FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
  60. form_widget.update();
  61. }
  62. }
  63. void CursorTool::on_mouseup(GUI::MouseEvent& event)
  64. {
  65. dbg() << "CursorTool::on_mouseup";
  66. if (event.button() == GUI::MouseButton::Left) {
  67. auto& form_widget = m_editor.form_widget();
  68. auto result = form_widget.hit_test(event.position(), GUI::Widget::ShouldRespectGreediness::No);
  69. if (!m_dragging && !(event.modifiers() & Mod_Ctrl)) {
  70. if (result.widget && result.widget != &form_widget) {
  71. m_editor.selection().set(*result.widget);
  72. // FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
  73. form_widget.update();
  74. }
  75. }
  76. m_dragging = false;
  77. m_rubber_banding = false;
  78. form_widget.update();
  79. }
  80. }
  81. void CursorTool::on_mousemove(GUI::MouseEvent& event)
  82. {
  83. dbg() << "CursorTool::on_mousemove";
  84. auto& form_widget = m_editor.form_widget();
  85. if (m_rubber_banding) {
  86. set_rubber_band_position(event.position());
  87. return;
  88. }
  89. if (!m_dragging && event.buttons() & GUI::MouseButton::Left && event.position() != m_drag_origin) {
  90. auto result = form_widget.hit_test(event.position(), GUI::Widget::ShouldRespectGreediness::No);
  91. if (result.widget && result.widget != &form_widget) {
  92. if (!m_editor.selection().contains(*result.widget)) {
  93. m_editor.selection().set(*result.widget);
  94. // FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
  95. form_widget.update();
  96. }
  97. }
  98. m_dragging = true;
  99. }
  100. if (m_dragging) {
  101. auto movement_delta = event.position() - m_drag_origin;
  102. m_editor.selection().for_each([&](auto& widget) {
  103. auto new_rect = widget.relative_rect();
  104. new_rect.set_location(m_positions_before_drag.get(&widget).value_or({}).translated(movement_delta));
  105. new_rect.set_x(new_rect.x() - (new_rect.x() % m_editor.form_widget().grid_size()));
  106. new_rect.set_y(new_rect.y() - (new_rect.y() % m_editor.form_widget().grid_size()));
  107. widget.set_relative_rect(new_rect);
  108. return IterationDecision::Continue;
  109. });
  110. m_editor.model().update();
  111. return;
  112. }
  113. }
  114. void CursorTool::on_keydown(GUI::KeyEvent& event)
  115. {
  116. dbg() << "CursorTool::on_keydown";
  117. auto move_selected_widgets_by = [this](int x, int y) {
  118. m_editor.selection().for_each([&](auto& widget) {
  119. widget.move_by(x, y);
  120. return IterationDecision::Continue;
  121. });
  122. };
  123. if (event.modifiers() == 0) {
  124. switch (event.key()) {
  125. case Key_Down:
  126. move_selected_widgets_by(0, m_editor.form_widget().grid_size());
  127. break;
  128. case Key_Up:
  129. move_selected_widgets_by(0, -m_editor.form_widget().grid_size());
  130. break;
  131. case Key_Left:
  132. move_selected_widgets_by(-m_editor.form_widget().grid_size(), 0);
  133. break;
  134. case Key_Right:
  135. move_selected_widgets_by(m_editor.form_widget().grid_size(), 0);
  136. break;
  137. }
  138. }
  139. }
  140. void CursorTool::set_rubber_band_position(const Gfx::Point& position)
  141. {
  142. if (m_rubber_band_position == position)
  143. return;
  144. m_rubber_band_position = position;
  145. auto rubber_band_rect = this->rubber_band_rect();
  146. m_editor.selection().clear();
  147. m_editor.form_widget().for_each_child_widget([&](auto& child) {
  148. if (child.relative_rect().intersects(rubber_band_rect))
  149. m_editor.selection().add(child);
  150. return IterationDecision::Continue;
  151. });
  152. m_editor.form_widget().update();
  153. }
  154. Rect CursorTool::rubber_band_rect() const
  155. {
  156. if (!m_rubber_banding)
  157. return {};
  158. return Rect::from_two_points(m_rubber_band_origin, m_rubber_band_position);
  159. }
  160. void CursorTool::on_second_paint(GUI::Painter& painter, GUI::PaintEvent&)
  161. {
  162. if (!m_rubber_banding)
  163. return;
  164. auto rect = rubber_band_rect();
  165. painter.fill_rect(rect, m_editor.palette().rubber_band_fill());
  166. painter.draw_rect(rect, m_editor.palette().rubber_band_border());
  167. }