PixelPaint: Add snapping to the GuideTool

When holding Shift, the Guide snaps to a multiple of the specified
number from the properties widget.
This commit is contained in:
Tobias Christiansen 2021-08-07 14:13:46 +02:00 committed by Andreas Kling
parent 58cb668257
commit 4f5c69a04c
Notes: sideshowbarker 2024-07-18 07:17:34 +09:00
2 changed files with 42 additions and 0 deletions

View file

@ -7,7 +7,11 @@
#include "GuideTool.h"
#include "ImageEditor.h"
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/ValueSlider.h>
#include <LibGUI/Widget.h>
namespace PixelPaint {
@ -110,6 +114,12 @@ void GuideTool::on_mousemove(Layer&, GUI::MouseEvent&, GUI::MouseEvent& image_ev
relevant_offset = delta.x();
auto new_offset = (float)relevant_offset + m_guide_origin;
if (image_event.shift() && m_snap_size > 0) {
float snap_size_half = m_snap_size / 2.0;
new_offset -= fmodf(new_offset + snap_size_half, m_snap_size) - snap_size_half;
}
m_selected_guide->set_offset(new_offset);
GUI::Application::the()->show_tooltip_immediately(String::formatted("{}", new_offset), GUI::Application::the()->tooltip_source_widget());
@ -138,4 +148,31 @@ void GuideTool::on_context_menu(Layer&, GUI::ContextMenuEvent& event)
m_context_menu->popup(event.screen_position());
}
GUI::Widget* GuideTool::get_properties_widget()
{
if (!m_properties_widget) {
m_properties_widget = GUI::Widget::construct();
m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
auto& snapping_container = m_properties_widget->add<GUI::Widget>();
snapping_container.set_fixed_height(20);
snapping_container.set_layout<GUI::HorizontalBoxLayout>();
auto& snapping_label = snapping_container.add<GUI::Label>("Snap offset:");
snapping_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
snapping_label.set_fixed_size(80, 20);
snapping_label.set_tooltip("Press Shift to snap");
auto& snapping_slider = snapping_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
snapping_slider.set_range(0, 50);
snapping_slider.set_value(m_snap_size);
snapping_slider.on_change = [&](int value) {
m_snap_size = value;
};
}
return m_properties_widget.ptr();
}
}

View file

@ -23,13 +23,18 @@ public:
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& image_event) override;
virtual void on_context_menu(Layer&, GUI::ContextMenuEvent&) override;
virtual GUI::Widget* get_properties_widget() override;
private:
RefPtr<Guide> closest_guide(Gfx::IntPoint const&);
RefPtr<GUI::Widget> m_properties_widget;
RefPtr<Guide> m_selected_guide;
RefPtr<Guide> m_context_menu_guide;
Gfx::IntPoint m_event_origin;
float m_guide_origin { 0 };
RefPtr<GUI::Menu> m_context_menu;
int m_snap_size { 10 };
};
}