mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 10:22:05 -05:00
PaintBrush: Add a "rectangle tool"
Fill, line, and gradient modes initially supported :^)
This commit is contained in:
parent
70a2355963
commit
b830639912
6 changed files with 145 additions and 1 deletions
|
@ -5,6 +5,7 @@ OBJS = \
|
|||
Tool.o \
|
||||
PenTool.o \
|
||||
LineTool.o \
|
||||
RectangleTool.o \
|
||||
EraseTool.o \
|
||||
BucketTool.o \
|
||||
ColorDialog.o \
|
||||
|
|
105
Applications/PaintBrush/RectangleTool.cpp
Normal file
105
Applications/PaintBrush/RectangleTool.cpp
Normal file
|
@ -0,0 +1,105 @@
|
|||
#include "RectangleTool.h"
|
||||
#include "PaintableWidget.h"
|
||||
#include <LibDraw/Rect.h>
|
||||
#include <LibGUI/GAction.h>
|
||||
#include <LibGUI/GMenu.h>
|
||||
#include <LibGUI/GPainter.h>
|
||||
#include <LibM/math.h>
|
||||
|
||||
RectangleTool::RectangleTool()
|
||||
{
|
||||
}
|
||||
|
||||
RectangleTool::~RectangleTool()
|
||||
{
|
||||
}
|
||||
|
||||
void RectangleTool::draw_using(Painter& painter)
|
||||
{
|
||||
auto rect_to_draw = Rect::from_two_points(m_rectangle_start_position, m_rectangle_end_position);
|
||||
switch (m_mode) {
|
||||
case Mode::Fill:
|
||||
painter.fill_rect(rect_to_draw, m_widget->color_for(m_drawing_button));
|
||||
break;
|
||||
case Mode::Outline:
|
||||
painter.draw_rect(rect_to_draw, m_widget->color_for(m_drawing_button));
|
||||
break;
|
||||
case Mode::Gradient:
|
||||
painter.fill_rect_with_gradient(rect_to_draw, m_widget->primary_color(), m_widget->secondary_color());
|
||||
break;
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
void RectangleTool::on_mousedown(GMouseEvent& event)
|
||||
{
|
||||
if (event.button() != GMouseButton::Left && event.button() != GMouseButton::Right)
|
||||
return;
|
||||
|
||||
if (m_drawing_button != GMouseButton::None)
|
||||
return;
|
||||
|
||||
m_drawing_button = event.button();
|
||||
m_rectangle_start_position = event.position();
|
||||
m_rectangle_end_position = event.position();
|
||||
m_widget->update();
|
||||
}
|
||||
|
||||
void RectangleTool::on_mouseup(GMouseEvent& event)
|
||||
{
|
||||
if (event.button() == m_drawing_button) {
|
||||
GPainter painter(m_widget->bitmap());
|
||||
draw_using(painter);
|
||||
m_drawing_button = GMouseButton::None;
|
||||
m_widget->update();
|
||||
}
|
||||
}
|
||||
|
||||
void RectangleTool::on_mousemove(GMouseEvent& event)
|
||||
{
|
||||
if (m_drawing_button == GMouseButton::None)
|
||||
return;
|
||||
|
||||
if (!m_widget->rect().contains(event.position()))
|
||||
return;
|
||||
|
||||
m_rectangle_end_position = event.position();
|
||||
m_widget->update();
|
||||
}
|
||||
|
||||
void RectangleTool::on_second_paint(GPaintEvent& event)
|
||||
{
|
||||
if (m_drawing_button == GMouseButton::None)
|
||||
return;
|
||||
|
||||
GPainter painter(*m_widget);
|
||||
painter.add_clip_rect(event.rect());
|
||||
draw_using(painter);
|
||||
}
|
||||
|
||||
void RectangleTool::on_keydown(GKeyEvent& event)
|
||||
{
|
||||
if (event.key() == Key_Escape && m_drawing_button != GMouseButton::None) {
|
||||
m_drawing_button = GMouseButton::None;
|
||||
m_widget->update();
|
||||
event.accept();
|
||||
}
|
||||
}
|
||||
|
||||
void RectangleTool::on_contextmenu(GContextMenuEvent& event)
|
||||
{
|
||||
if (!m_context_menu) {
|
||||
m_context_menu = GMenu::construct();
|
||||
m_context_menu->add_action(GAction::create("Fill", [this](auto&) {
|
||||
m_mode = Mode::Fill;
|
||||
}));
|
||||
m_context_menu->add_action(GAction::create("Outline", [this](auto&) {
|
||||
m_mode = Mode::Outline;
|
||||
}));
|
||||
m_context_menu->add_action(GAction::create("Gradient", [this](auto&) {
|
||||
m_mode = Mode::Gradient;
|
||||
}));
|
||||
}
|
||||
m_context_menu->popup(event.screen_position());
|
||||
}
|
36
Applications/PaintBrush/RectangleTool.h
Normal file
36
Applications/PaintBrush/RectangleTool.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include "Tool.h"
|
||||
#include <LibDraw/Point.h>
|
||||
|
||||
class GMenu;
|
||||
class Painter;
|
||||
|
||||
class RectangleTool final : public Tool {
|
||||
public:
|
||||
RectangleTool();
|
||||
virtual ~RectangleTool() override;
|
||||
|
||||
virtual void on_mousedown(GMouseEvent&) override;
|
||||
virtual void on_mousemove(GMouseEvent&) override;
|
||||
virtual void on_mouseup(GMouseEvent&) override;
|
||||
virtual void on_contextmenu(GContextMenuEvent&) override;
|
||||
virtual void on_second_paint(GPaintEvent&) override;
|
||||
virtual void on_keydown(GKeyEvent&) override;
|
||||
|
||||
private:
|
||||
enum class Mode {
|
||||
Outline,
|
||||
Fill,
|
||||
Gradient,
|
||||
};
|
||||
|
||||
virtual const char* class_name() const override { return "RectangleTool"; }
|
||||
void draw_using(Painter& painter);
|
||||
|
||||
GMouseButton m_drawing_button { GMouseButton::None };
|
||||
Point m_rectangle_start_position;
|
||||
Point m_rectangle_end_position;
|
||||
RefPtr<GMenu> m_context_menu;
|
||||
Mode m_mode { Mode::Outline };
|
||||
};
|
|
@ -5,6 +5,7 @@
|
|||
#include "PaintableWidget.h"
|
||||
#include "PenTool.h"
|
||||
#include "PickerTool.h"
|
||||
#include "RectangleTool.h"
|
||||
#include "SprayTool.h"
|
||||
#include <LibDraw/PNGLoader.h>
|
||||
#include <LibGUI/GBoxLayout.h>
|
||||
|
@ -70,6 +71,7 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
|
|||
add_tool("Color Picker", "picker", make<PickerTool>());
|
||||
add_tool("Erase", "eraser", make<EraseTool>());
|
||||
add_tool("Line", "line", make<LineTool>());
|
||||
add_tool("Rectangle", "rectangle", make<RectangleTool>());
|
||||
}
|
||||
|
||||
ToolboxWidget::~ToolboxWidget()
|
||||
|
|
BIN
Base/res/icons/paintbrush/rectangle.png
Normal file
BIN
Base/res/icons/paintbrush/rectangle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 859 B |
|
@ -5,9 +5,9 @@
|
|||
#include "Rect.h"
|
||||
#include "Size.h"
|
||||
#include <AK/String.h>
|
||||
#include <AK/Utf8View.h>
|
||||
#include <LibDraw/TextAlignment.h>
|
||||
#include <LibDraw/TextElision.h>
|
||||
#include <AK/Utf8View.h>
|
||||
|
||||
class CharacterBitmap;
|
||||
class GlyphBitmap;
|
||||
|
|
Loading…
Add table
Reference in a new issue