CursorTool.cpp 7.0 KB

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