Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
4 * Copyright (c) 2022, the SerenityOS developers.
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9#include "Tool.h"
10#include "../ImageEditor.h"
11#include "../Layer.h"
12#include <LibGUI/Action.h>
13
14namespace PixelPaint {
15
16void Tool::setup(ImageEditor& editor)
17{
18 m_editor = editor;
19}
20
21void Tool::set_action(GUI::Action* action)
22{
23 m_action = action;
24}
25
26bool Tool::on_keydown(GUI::KeyEvent& event)
27{
28 switch (event.key()) {
29 case KeyCode::Key_LeftBracket:
30 if (m_primary_slider) {
31 m_primary_slider->decrease_slider_by(1);
32 return true;
33 }
34 break;
35 case KeyCode::Key_RightBracket:
36 if (m_primary_slider) {
37 m_primary_slider->increase_slider_by(1);
38 return true;
39 }
40 break;
41 case KeyCode::Key_LeftBrace:
42 if (m_secondary_slider) {
43 m_secondary_slider->decrease_slider_by(1);
44 return true;
45 }
46 break;
47 case KeyCode::Key_RightBrace:
48 if (m_secondary_slider) {
49 m_secondary_slider->increase_slider_by(1);
50 return true;
51 }
52 break;
53 default:
54 break;
55 }
56
57 return false;
58}
59
60Gfx::IntPoint Tool::editor_layer_location(Layer const& layer) const
61{
62 return (Gfx::FloatPoint { layer.location() } * m_editor->scale()).to_rounded<int>();
63}
64
65Gfx::IntPoint Tool::editor_stroke_position(Gfx::IntPoint pixel_coords, int stroke_thickness) const
66{
67 auto position = m_editor->content_to_frame_position(pixel_coords);
68 auto offset = (stroke_thickness % 2 == 0) ? 0 : m_editor->scale() / 2;
69 position = position.translated(offset, offset);
70 return position.to_type<int>();
71}
72
73Gfx::IntPoint Tool::constrain_line_angle(Gfx::IntPoint start_pos, Gfx::IntPoint end_pos, float angle_increment)
74{
75 float current_angle = AK::atan2<float>(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + float { M_PI * 2 };
76
77 float constrained_angle = ((int)((current_angle + angle_increment / 2) / angle_increment)) * angle_increment;
78
79 auto diff = end_pos - start_pos;
80 float line_length = AK::hypot<float>(diff.x(), diff.y());
81
82 return { start_pos.x() + (int)(AK::cos(constrained_angle) * line_length),
83 start_pos.y() + (int)(AK::sin(constrained_angle) * line_length) };
84}
85
86}