Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "EllipseTool.h"
28#include "PaintableWidget.h"
29#include <LibGfx/Rect.h>
30#include <LibGUI/Action.h>
31#include <LibGUI/Menu.h>
32#include <LibGUI/Painter.h>
33#include <LibM/math.h>
34
35EllipseTool::EllipseTool()
36{
37}
38
39EllipseTool::~EllipseTool()
40{
41}
42
43void EllipseTool::draw_using(GUI::Painter& painter)
44{
45 auto ellipse_intersecting_rect = Gfx::Rect::from_two_points(m_ellipse_start_position, m_ellipse_end_position);
46 switch (m_mode) {
47 case Mode::Outline:
48 painter.draw_ellipse_intersecting(ellipse_intersecting_rect, m_widget->color_for(m_drawing_button), m_thickness);
49 break;
50 default:
51 ASSERT_NOT_REACHED();
52 }
53}
54
55void EllipseTool::on_mousedown(GUI::MouseEvent& event)
56{
57 if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
58 return;
59
60 if (m_drawing_button != GUI::MouseButton::None)
61 return;
62
63 m_drawing_button = event.button();
64 m_ellipse_start_position = event.position();
65 m_ellipse_end_position = event.position();
66 m_widget->update();
67}
68
69void EllipseTool::on_mouseup(GUI::MouseEvent& event)
70{
71 if (event.button() == m_drawing_button) {
72 GUI::Painter painter(m_widget->bitmap());
73 draw_using(painter);
74 m_drawing_button = GUI::MouseButton::None;
75 m_widget->update();
76 }
77}
78
79void EllipseTool::on_mousemove(GUI::MouseEvent& event)
80{
81 if (m_drawing_button == GUI::MouseButton::None)
82 return;
83
84 if (!m_widget->rect().contains(event.position()))
85 return;
86
87 m_ellipse_end_position = event.position();
88 m_widget->update();
89}
90
91void EllipseTool::on_second_paint(GUI::PaintEvent& event)
92{
93 if (m_drawing_button == GUI::MouseButton::None)
94 return;
95
96 GUI::Painter painter(*m_widget);
97 painter.add_clip_rect(event.rect());
98 draw_using(painter);
99}
100
101void EllipseTool::on_keydown(GUI::KeyEvent& event)
102{
103 if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
104 m_drawing_button = GUI::MouseButton::None;
105 m_widget->update();
106 event.accept();
107 }
108}
109
110void EllipseTool::on_contextmenu(GUI::ContextMenuEvent& event)
111{
112 if (!m_context_menu) {
113 m_context_menu = GUI::Menu::construct();
114 m_context_menu->add_action(GUI::Action::create("Outline", [this](auto&) {
115 m_mode = Mode::Outline;
116 }));
117 m_context_menu->add_separator();
118 m_thickness_actions.set_exclusive(true);
119 auto insert_action = [&](int size, bool checked = false) {
120 auto action = GUI::Action::create(String::number(size), [this, size](auto& action) {
121 m_thickness = size;
122 action.set_checked(true);
123 });
124 action->set_checkable(true);
125 action->set_checked(checked);
126 m_thickness_actions.add_action(*action);
127 m_context_menu->add_action(move(action));
128 };
129 insert_action(1, true);
130 insert_action(2);
131 insert_action(3);
132 insert_action(4);
133 }
134 m_context_menu->popup(event.screen_position());
135}