diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2021-09-15 19:46:54 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-17 11:54:13 +0200 |
commit | 6ba3fc559f10df13743cfcfd3668ce33a3d8a88e (patch) | |
tree | dce7c33f730ea734cac2a7d5d16e9461d38de38f | |
parent | f14c891ba545f39091a2b86206c6505be1cb6fd4 (diff) | |
download | serenity-6ba3fc559f10df13743cfcfd3668ce33a3d8a88e.zip |
PixelPaint: Allow selecting a custom aspect ratio for EllipseTool
If you enter a custom aspect ratio, and are not holding down shift,
the bounding rect for the ellipse will be constrained to the entered
aspect ratio
-rw-r--r-- | Userland/Applications/PixelPaint/EllipseTool.cpp | 33 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/EllipseTool.h | 6 |
2 files changed, 38 insertions, 1 deletions
diff --git a/Userland/Applications/PixelPaint/EllipseTool.cpp b/Userland/Applications/PixelPaint/EllipseTool.cpp index 74816e1afe..03fdea3a17 100644 --- a/Userland/Applications/PixelPaint/EllipseTool.cpp +++ b/Userland/Applications/PixelPaint/EllipseTool.cpp @@ -14,6 +14,7 @@ #include <LibGUI/Menu.h> #include <LibGUI/Painter.h> #include <LibGUI/RadioButton.h> +#include <LibGUI/TextBox.h> #include <LibGUI/ValueSlider.h> #include <LibGfx/Rect.h> @@ -91,6 +92,8 @@ void EllipseTool::on_mousemove(Layer*, MouseEvent& event) if (event.layer_event().shift()) m_ellipse_end_position = m_ellipse_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0); + else if (m_aspect_ratio.has_value()) + m_ellipse_end_position = m_ellipse_start_position.end_point_for_aspect_ratio(event.layer_event().position(), m_aspect_ratio.value()); else m_ellipse_end_position = event.layer_event().position(); @@ -162,6 +165,36 @@ GUI::Widget* EllipseTool::get_properties_widget() }; outline_mode_radio.set_checked(true); + + auto& aspect_container = m_properties_widget->add<GUI::Widget>(); + aspect_container.set_fixed_height(20); + aspect_container.set_layout<GUI::HorizontalBoxLayout>(); + + auto& aspect_label = aspect_container.add<GUI::Label>("Aspect Ratio:"); + aspect_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); + aspect_label.set_fixed_size(80, 20); + + m_aspect_w_textbox = aspect_container.add<GUI::TextBox>(); + 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<GUI::Label>("x"); + multiply_label.set_text_alignment(Gfx::TextAlignment::Center); + multiply_label.set_fixed_size(10, 20); + + m_aspect_h_textbox = aspect_container.add<GUI::TextBox>(); + 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/EllipseTool.h b/Userland/Applications/PixelPaint/EllipseTool.h index 2a955f05d5..885ca2e781 100644 --- a/Userland/Applications/PixelPaint/EllipseTool.h +++ b/Userland/Applications/PixelPaint/EllipseTool.h @@ -8,7 +8,7 @@ #pragma once #include "Tool.h" -#include <LibGUI/ActionGroup.h> +#include <LibGUI/Forward.h> #include <LibGfx/Point.h> namespace PixelPaint { @@ -40,12 +40,16 @@ private: void draw_using(GUI::Painter&, Gfx::IntPoint const& start_position, Gfx::IntPoint const& end_position, int thickness); RefPtr<GUI::Widget> m_properties_widget; + RefPtr<GUI::TextBox> m_aspect_w_textbox; + RefPtr<GUI::TextBox> m_aspect_h_textbox; + GUI::MouseButton m_drawing_button { GUI::MouseButton::None }; Gfx::IntPoint m_ellipse_start_position; Gfx::IntPoint m_ellipse_end_position; int m_thickness { 1 }; FillMode m_fill_mode { FillMode::Outline }; DrawMode m_draw_mode { DrawMode::FromCorner }; + Optional<float> m_aspect_ratio; }; } |