From 090962823b923fb5fc93a2b46028cece8217d2d2 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Wed, 15 Sep 2021 19:49:53 -0400 Subject: [PATCH] PixelPaint: Allow selecting a custom aspect ratio for RectangleTool If you enter a custom aspect ratio, and are not holding down shift, the rectangle will be constrained to the entered aspect ratio --- .../Applications/PixelPaint/RectangleTool.cpp | 33 +++++++++++++++++++ .../Applications/PixelPaint/RectangleTool.h | 4 +++ 2 files changed, 37 insertions(+) diff --git a/Userland/Applications/PixelPaint/RectangleTool.cpp b/Userland/Applications/PixelPaint/RectangleTool.cpp index 6ff2b57d98..cec747c25c 100644 --- a/Userland/Applications/PixelPaint/RectangleTool.cpp +++ b/Userland/Applications/PixelPaint/RectangleTool.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -97,6 +98,8 @@ void RectangleTool::on_mousemove(Layer* layer, MouseEvent& event) if (event.layer_event().shift()) m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0); + else if (m_aspect_ratio.has_value()) + m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), m_aspect_ratio.value()); else m_rectangle_end_position = event.layer_event().position(); @@ -172,6 +175,36 @@ GUI::Widget* RectangleTool::get_properties_widget() }; outline_mode_radio.set_checked(true); + + auto& aspect_container = m_properties_widget->add(); + aspect_container.set_fixed_height(20); + aspect_container.set_layout(); + + auto& aspect_label = aspect_container.add("Aspect Ratio:"); + aspect_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); + aspect_label.set_fixed_size(80, 20); + + m_aspect_w_textbox = aspect_container.add(); + m_aspect_w_textbox->set_fixed_height(20); + m_aspect_w_textbox->set_fixed_width(25); + m_aspect_w_textbox->on_change = [&] { + auto x = m_aspect_w_textbox->text().to_int().value_or(0); + auto y = m_aspect_h_textbox->text().to_int().value_or(0); + if (x > 0 && y > 0) { + m_aspect_ratio = (float)x / (float)y; + } else { + m_aspect_ratio = {}; + } + }; + + auto& multiply_label = aspect_container.add("x"); + multiply_label.set_text_alignment(Gfx::TextAlignment::Center); + multiply_label.set_fixed_size(10, 20); + + m_aspect_h_textbox = aspect_container.add(); + m_aspect_h_textbox->set_fixed_height(20); + m_aspect_h_textbox->set_fixed_width(25); + m_aspect_h_textbox->on_change = [&] { m_aspect_w_textbox->on_change(); }; } return m_properties_widget.ptr(); diff --git a/Userland/Applications/PixelPaint/RectangleTool.h b/Userland/Applications/PixelPaint/RectangleTool.h index be673eaaf7..6ff2bb1e12 100644 --- a/Userland/Applications/PixelPaint/RectangleTool.h +++ b/Userland/Applications/PixelPaint/RectangleTool.h @@ -41,12 +41,16 @@ private: void draw_using(GUI::Painter&, Gfx::IntPoint const& start_position, Gfx::IntPoint const& end_position, int thickness); RefPtr m_properties_widget; + RefPtr m_aspect_w_textbox; + RefPtr m_aspect_h_textbox; + GUI::MouseButton m_drawing_button { GUI::MouseButton::None }; Gfx::IntPoint m_rectangle_start_position; Gfx::IntPoint m_rectangle_end_position; FillMode m_fill_mode { FillMode::Outline }; DrawMode m_draw_mode { DrawMode::FromCorner }; int m_thickness { 1 }; + Optional m_aspect_ratio; }; }