GuideTool.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "GuideTool.h"
  8. #include "../EditGuideDialog.h"
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/Menu.h>
  13. #include <LibGUI/ValueSlider.h>
  14. #include <LibGUI/Widget.h>
  15. namespace PixelPaint {
  16. RefPtr<Guide> GuideTool::closest_guide(Gfx::IntPoint const& point)
  17. {
  18. auto guides = editor()->guides();
  19. Guide* closest_guide = nullptr;
  20. int closest_guide_distance = NumericLimits<int>::max();
  21. for (auto& guide : guides) {
  22. int relevant_position = 0;
  23. if (guide.orientation() == Guide::Orientation::Horizontal)
  24. relevant_position = point.y();
  25. else if (guide.orientation() == Guide::Orientation::Vertical)
  26. relevant_position = point.x();
  27. auto distance = abs(relevant_position - (int)guide.offset());
  28. if (distance < closest_guide_distance) {
  29. closest_guide = &guide;
  30. closest_guide_distance = distance;
  31. }
  32. }
  33. if (closest_guide_distance < 20)
  34. return closest_guide;
  35. return nullptr;
  36. }
  37. void GuideTool::on_mousedown(Layer*, MouseEvent& event)
  38. {
  39. if (!m_editor)
  40. return;
  41. auto& image_event = event.image_event();
  42. if (image_event.button() != GUI::MouseButton::Primary)
  43. return;
  44. m_editor->set_guide_visibility(true);
  45. RefPtr<Guide> new_guide;
  46. if (image_event.position().x() < 0 || image_event.position().x() > editor()->image().size().width()) {
  47. new_guide = make_ref_counted<Guide>(Guide::Orientation::Vertical, image_event.position().x());
  48. } else if (image_event.position().y() < 0 || image_event.position().y() > editor()->image().size().height()) {
  49. new_guide = make_ref_counted<Guide>(Guide::Orientation::Horizontal, image_event.position().y());
  50. }
  51. if (new_guide) {
  52. m_selected_guide = new_guide;
  53. m_guide_origin = 0;
  54. editor()->add_guide(new_guide.release_nonnull());
  55. return;
  56. }
  57. m_event_origin = image_event.position();
  58. m_selected_guide = closest_guide(image_event.position());
  59. if (m_selected_guide) {
  60. m_guide_origin = m_selected_guide->offset();
  61. GUI::Application::the()->show_tooltip_immediately(String::formatted("{}", m_guide_origin), GUI::Application::the()->tooltip_source_widget());
  62. }
  63. }
  64. void GuideTool::on_mouseup(Layer*, MouseEvent&)
  65. {
  66. m_guide_origin = 0;
  67. m_event_origin = { 0, 0 };
  68. GUI::Application::the()->hide_tooltip();
  69. if (!m_selected_guide)
  70. return;
  71. if (m_selected_guide->offset() < 0
  72. || (m_selected_guide->orientation() == Guide::Orientation::Horizontal && m_selected_guide->offset() > editor()->image().size().height())
  73. || (m_selected_guide->orientation() == Guide::Orientation::Vertical && m_selected_guide->offset() > editor()->image().size().width())) {
  74. editor()->remove_guide(*m_selected_guide);
  75. editor()->layers_did_change();
  76. }
  77. m_selected_guide = nullptr;
  78. }
  79. void GuideTool::on_mousemove(Layer*, MouseEvent& event)
  80. {
  81. if (!m_selected_guide)
  82. return;
  83. auto& image_event = event.image_event();
  84. auto delta = image_event.position() - m_event_origin;
  85. auto relevant_offset = 0;
  86. if (m_selected_guide->orientation() == Guide::Orientation::Horizontal)
  87. relevant_offset = delta.y();
  88. else if (m_selected_guide->orientation() == Guide::Orientation::Vertical)
  89. relevant_offset = delta.x();
  90. auto new_offset = (float)relevant_offset + m_guide_origin;
  91. if (image_event.shift() && m_snap_size > 0) {
  92. float snap_size_half = m_snap_size / 2.0;
  93. new_offset -= fmodf(new_offset + snap_size_half, m_snap_size) - snap_size_half;
  94. }
  95. m_selected_guide->set_offset(new_offset);
  96. GUI::Application::the()->show_tooltip_immediately(String::formatted("{}", new_offset), GUI::Application::the()->tooltip_source_widget());
  97. editor()->layers_did_change();
  98. }
  99. void GuideTool::on_context_menu(Layer*, GUI::ContextMenuEvent& event)
  100. {
  101. if (!m_editor)
  102. return;
  103. m_editor->set_guide_visibility(true);
  104. if (!m_context_menu) {
  105. m_context_menu = GUI::Menu::construct();
  106. m_context_menu->add_action(GUI::Action::create(
  107. "Set &Offset", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/gear.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  108. if (!m_context_menu_guide)
  109. return;
  110. auto dialog = EditGuideDialog::construct(
  111. editor()->window(),
  112. String::formatted("{}", m_context_menu_guide->offset()),
  113. m_context_menu_guide->orientation());
  114. if (dialog->exec() != GUI::Dialog::ExecResult::OK)
  115. return;
  116. auto offset = dialog->offset_as_pixel(*editor());
  117. if (!offset.has_value())
  118. return;
  119. m_context_menu_guide->set_offset(offset.release_value());
  120. m_context_menu_guide->set_orientation(dialog->orientation());
  121. editor()->layers_did_change();
  122. },
  123. editor()));
  124. m_context_menu->add_action(GUI::Action::create(
  125. "&Delete Guide", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  126. if (!m_context_menu_guide)
  127. return;
  128. editor()->remove_guide(*m_context_menu_guide);
  129. m_selected_guide = nullptr;
  130. m_guide_origin = 0;
  131. editor()->layers_did_change();
  132. },
  133. editor()));
  134. }
  135. auto image_position = editor()->frame_to_content_position(event.position());
  136. m_context_menu_guide = closest_guide({ (int)image_position.x(), (int)image_position.y() });
  137. if (m_context_menu_guide)
  138. m_context_menu->popup(event.screen_position());
  139. }
  140. void GuideTool::on_tool_activation()
  141. {
  142. if (m_editor)
  143. m_editor->set_guide_visibility(true);
  144. }
  145. GUI::Widget* GuideTool::get_properties_widget()
  146. {
  147. if (!m_properties_widget) {
  148. m_properties_widget = GUI::Widget::construct();
  149. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  150. auto& snapping_container = m_properties_widget->add<GUI::Widget>();
  151. snapping_container.set_fixed_height(20);
  152. snapping_container.set_layout<GUI::HorizontalBoxLayout>();
  153. auto& snapping_label = snapping_container.add<GUI::Label>("Snap offset:");
  154. snapping_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  155. snapping_label.set_fixed_size(80, 20);
  156. snapping_label.set_tooltip("Press Shift to snap");
  157. auto& snapping_slider = snapping_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
  158. snapping_slider.set_range(0, 50);
  159. snapping_slider.set_value(m_snap_size);
  160. snapping_slider.on_change = [&](int value) {
  161. m_snap_size = value;
  162. };
  163. set_primary_slider(&snapping_slider);
  164. }
  165. return m_properties_widget.ptr();
  166. }
  167. }