Serenity Operating System
at hosted 131 lines 4.3 kB view raw
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 "RectangleTool.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 35RectangleTool::RectangleTool() 36{ 37} 38 39RectangleTool::~RectangleTool() 40{ 41} 42 43void RectangleTool::draw_using(GUI::Painter& painter) 44{ 45 auto rect_to_draw = Gfx::Rect::from_two_points(m_rectangle_start_position, m_rectangle_end_position); 46 switch (m_mode) { 47 case Mode::Fill: 48 painter.fill_rect(rect_to_draw, m_widget->color_for(m_drawing_button)); 49 break; 50 case Mode::Outline: 51 painter.draw_rect(rect_to_draw, m_widget->color_for(m_drawing_button)); 52 break; 53 case Mode::Gradient: 54 painter.fill_rect_with_gradient(rect_to_draw, m_widget->primary_color(), m_widget->secondary_color()); 55 break; 56 default: 57 ASSERT_NOT_REACHED(); 58 } 59} 60 61void RectangleTool::on_mousedown(GUI::MouseEvent& event) 62{ 63 if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right) 64 return; 65 66 if (m_drawing_button != GUI::MouseButton::None) 67 return; 68 69 m_drawing_button = event.button(); 70 m_rectangle_start_position = event.position(); 71 m_rectangle_end_position = event.position(); 72 m_widget->update(); 73} 74 75void RectangleTool::on_mouseup(GUI::MouseEvent& event) 76{ 77 if (event.button() == m_drawing_button) { 78 GUI::Painter painter(m_widget->bitmap()); 79 draw_using(painter); 80 m_drawing_button = GUI::MouseButton::None; 81 m_widget->update(); 82 } 83} 84 85void RectangleTool::on_mousemove(GUI::MouseEvent& event) 86{ 87 if (m_drawing_button == GUI::MouseButton::None) 88 return; 89 90 if (!m_widget->rect().contains(event.position())) 91 return; 92 93 m_rectangle_end_position = event.position(); 94 m_widget->update(); 95} 96 97void RectangleTool::on_second_paint(GUI::PaintEvent& event) 98{ 99 if (m_drawing_button == GUI::MouseButton::None) 100 return; 101 102 GUI::Painter painter(*m_widget); 103 painter.add_clip_rect(event.rect()); 104 draw_using(painter); 105} 106 107void RectangleTool::on_keydown(GUI::KeyEvent& event) 108{ 109 if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) { 110 m_drawing_button = GUI::MouseButton::None; 111 m_widget->update(); 112 event.accept(); 113 } 114} 115 116void RectangleTool::on_contextmenu(GUI::ContextMenuEvent& event) 117{ 118 if (!m_context_menu) { 119 m_context_menu = GUI::Menu::construct(); 120 m_context_menu->add_action(GUI::Action::create("Fill", [this](auto&) { 121 m_mode = Mode::Fill; 122 })); 123 m_context_menu->add_action(GUI::Action::create("Outline", [this](auto&) { 124 m_mode = Mode::Outline; 125 })); 126 m_context_menu->add_action(GUI::Action::create("Gradient", [this](auto&) { 127 m_mode = Mode::Gradient; 128 })); 129 } 130 m_context_menu->popup(event.screen_position()); 131}