2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-12-26 16:43:59 -03:00
|
|
|
#include "RectangleTool.h"
|
2020-05-12 18:44:46 -03:00
|
|
|
#include "ImageEditor.h"
|
|
|
|
|
#include "Layer.h"
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/Menu.h>
|
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-05-12 18:44:46 -03:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-08-12 05:44:45 -03:00
|
|
|
#include <math.h>
|
2019-12-26 16:43:59 -03:00
|
|
|
|
2020-05-20 15:35:35 -03:00
|
|
|
namespace PixelPaint {
|
2020-05-12 18:44:46 -03:00
|
|
|
|
2019-12-26 16:43:59 -03:00
|
|
|
RectangleTool::RectangleTool()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RectangleTool::~RectangleTool()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 05:57:59 -03:00
|
|
|
void RectangleTool::draw_using(GUI::Painter& painter, const Gfx::IntRect& rect)
|
2019-12-26 16:43:59 -03:00
|
|
|
{
|
|
|
|
|
switch (m_mode) {
|
|
|
|
|
case Mode::Fill:
|
2020-05-22 09:57:10 -03:00
|
|
|
painter.fill_rect(rect, m_editor->color_for(m_drawing_button));
|
2019-12-26 16:43:59 -03:00
|
|
|
break;
|
|
|
|
|
case Mode::Outline:
|
2020-05-22 09:57:10 -03:00
|
|
|
painter.draw_rect(rect, m_editor->color_for(m_drawing_button));
|
2019-12-26 16:43:59 -03:00
|
|
|
break;
|
|
|
|
|
case Mode::Gradient:
|
2020-05-22 09:57:10 -03:00
|
|
|
painter.fill_rect_with_gradient(rect, m_editor->primary_color(), m_editor->secondary_color());
|
2019-12-26 16:43:59 -03:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 19:08:47 -03:00
|
|
|
void RectangleTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
|
2019-12-26 16:43:59 -03:00
|
|
|
{
|
2020-02-02 11:07:41 -03:00
|
|
|
if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
|
2019-12-26 16:43:59 -03:00
|
|
|
return;
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
if (m_drawing_button != GUI::MouseButton::None)
|
2019-12-26 16:43:59 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_drawing_button = event.button();
|
|
|
|
|
m_rectangle_start_position = event.position();
|
|
|
|
|
m_rectangle_end_position = event.position();
|
2020-05-12 18:44:46 -03:00
|
|
|
m_editor->update();
|
2019-12-26 16:43:59 -03:00
|
|
|
}
|
|
|
|
|
|
2020-05-12 19:08:47 -03:00
|
|
|
void RectangleTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
|
2019-12-26 16:43:59 -03:00
|
|
|
{
|
|
|
|
|
if (event.button() == m_drawing_button) {
|
2020-05-12 18:44:46 -03:00
|
|
|
GUI::Painter painter(layer.bitmap());
|
2020-06-10 05:57:59 -03:00
|
|
|
auto rect = Gfx::IntRect::from_two_points(m_rectangle_start_position, m_rectangle_end_position);
|
2020-05-22 09:57:10 -03:00
|
|
|
draw_using(painter, rect);
|
2020-02-02 11:07:41 -03:00
|
|
|
m_drawing_button = GUI::MouseButton::None;
|
2020-05-25 17:49:50 -03:00
|
|
|
layer.did_modify_bitmap(*m_editor->image());
|
2020-10-17 13:47:34 -03:00
|
|
|
m_editor->did_complete_action();
|
2019-12-26 16:43:59 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 19:08:47 -03:00
|
|
|
void RectangleTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
|
2019-12-26 16:43:59 -03:00
|
|
|
{
|
2020-02-02 11:07:41 -03:00
|
|
|
if (m_drawing_button == GUI::MouseButton::None)
|
2019-12-26 16:43:59 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_rectangle_end_position = event.position();
|
2020-05-12 18:44:46 -03:00
|
|
|
m_editor->update();
|
2019-12-26 16:43:59 -03:00
|
|
|
}
|
|
|
|
|
|
2020-05-13 08:41:12 -03:00
|
|
|
void RectangleTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)
|
2019-12-26 16:43:59 -03:00
|
|
|
{
|
2020-02-02 11:07:41 -03:00
|
|
|
if (m_drawing_button == GUI::MouseButton::None)
|
2019-12-26 16:43:59 -03:00
|
|
|
return;
|
|
|
|
|
|
2020-05-13 08:41:12 -03:00
|
|
|
GUI::Painter painter(*m_editor);
|
2019-12-26 16:43:59 -03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2020-07-26 01:31:47 -03:00
|
|
|
auto rect = Gfx::IntRect::from_two_points(
|
|
|
|
|
m_editor->layer_position_to_editor_position(layer, m_rectangle_start_position).to_type<int>(),
|
|
|
|
|
m_editor->layer_position_to_editor_position(layer, m_rectangle_end_position).to_type<int>());
|
2020-05-22 09:57:10 -03:00
|
|
|
draw_using(painter, rect);
|
2019-12-26 16:43:59 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void RectangleTool::on_keydown(GUI::KeyEvent& event)
|
2019-12-26 16:43:59 -03:00
|
|
|
{
|
2020-02-02 11:07:41 -03:00
|
|
|
if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
|
|
|
|
|
m_drawing_button = GUI::MouseButton::None;
|
2020-05-12 18:44:46 -03:00
|
|
|
m_editor->update();
|
2019-12-26 16:43:59 -03:00
|
|
|
event.accept();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 16:51:43 -03:00
|
|
|
void RectangleTool::on_tool_button_contextmenu(GUI::ContextMenuEvent& event)
|
2019-12-26 16:43:59 -03:00
|
|
|
{
|
|
|
|
|
if (!m_context_menu) {
|
2020-02-02 11:07:41 -03:00
|
|
|
m_context_menu = GUI::Menu::construct();
|
|
|
|
|
m_context_menu->add_action(GUI::Action::create("Fill", [this](auto&) {
|
2019-12-26 16:43:59 -03:00
|
|
|
m_mode = Mode::Fill;
|
|
|
|
|
}));
|
2020-02-02 11:07:41 -03:00
|
|
|
m_context_menu->add_action(GUI::Action::create("Outline", [this](auto&) {
|
2019-12-26 16:43:59 -03:00
|
|
|
m_mode = Mode::Outline;
|
|
|
|
|
}));
|
2020-02-02 11:07:41 -03:00
|
|
|
m_context_menu->add_action(GUI::Action::create("Gradient", [this](auto&) {
|
2019-12-26 16:43:59 -03:00
|
|
|
m_mode = Mode::Gradient;
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
m_context_menu->popup(event.screen_position());
|
|
|
|
|
}
|
2020-05-12 18:44:46 -03:00
|
|
|
|
|
|
|
|
}
|