diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2021-09-03 00:37:49 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-04 03:33:44 +0200 |
commit | d28fb8926f923fcba033ddb245cf9feafbcc8d61 (patch) | |
tree | 36dde8ac9f3c36b1d2ba31edceebc281cc5e0d3d | |
parent | 8d36893ddfb795855507fa84f27e5364a0239226 (diff) | |
download | serenity-d28fb8926f923fcba033ddb245cf9feafbcc8d61.zip |
PixelPaint: Add mechanism for tools to use keyboard shortcuts
There are quite a few tools that might want to change certain values
based on consistent keyboard shortcuts. This commit allows tools to
hook up a "primary" and "secondary" slider with the base `Tool`
class, which can then handle updating those sliders with the common
shortcuts.
Note that any derived classes that want to override the `on_keydown`
function will manually need to call `Tool::on_keydown()`.
-rw-r--r-- | Userland/Applications/PixelPaint/Tool.cpp | 25 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Tool.h | 10 |
2 files changed, 34 insertions, 1 deletions
diff --git a/Userland/Applications/PixelPaint/Tool.cpp b/Userland/Applications/PixelPaint/Tool.cpp index 82b3b37424..0d747807b0 100644 --- a/Userland/Applications/PixelPaint/Tool.cpp +++ b/Userland/Applications/PixelPaint/Tool.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -28,4 +29,28 @@ void Tool::set_action(GUI::Action* action) m_action = action; } +void Tool::on_keydown(GUI::KeyEvent& event) +{ + switch (event.key()) { + case KeyCode::Key_LeftBracket: + if (m_primary_slider) + m_primary_slider->set_value(m_primary_slider->value() - 1); + break; + case KeyCode::Key_RightBracket: + if (m_primary_slider) + m_primary_slider->set_value(m_primary_slider->value() + 1); + break; + case KeyCode::Key_LeftBrace: + if (m_secondary_slider) + m_secondary_slider->set_value(m_secondary_slider->value() - 1); + break; + case KeyCode::Key_RightBrace: + if (m_secondary_slider) + m_secondary_slider->set_value(m_secondary_slider->value() + 1); + break; + default: + break; + } +} + } diff --git a/Userland/Applications/PixelPaint/Tool.h b/Userland/Applications/PixelPaint/Tool.h index 6466d051ae..be2d783166 100644 --- a/Userland/Applications/PixelPaint/Tool.h +++ b/Userland/Applications/PixelPaint/Tool.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +9,7 @@ #include <LibGUI/Event.h> #include <LibGUI/Forward.h> +#include <LibGUI/ValueSlider.h> #include <LibGfx/StandardCursor.h> namespace PixelPaint { @@ -54,7 +56,7 @@ public: virtual void on_context_menu(Layer*, GUI::ContextMenuEvent&) { } virtual void on_tool_button_contextmenu(GUI::ContextMenuEvent&) { } virtual void on_second_paint(Layer const*, GUI::PaintEvent&) { } - virtual void on_keydown(GUI::KeyEvent&) { } + virtual void on_keydown(GUI::KeyEvent&); virtual void on_keyup(GUI::KeyEvent&) { } virtual void on_tool_activation() { } virtual GUI::Widget* get_properties_widget() { return nullptr; } @@ -73,6 +75,12 @@ protected: Tool(); WeakPtr<ImageEditor> m_editor; RefPtr<GUI::Action> m_action; + + void set_primary_slider(GUI::ValueSlider* primary) { m_primary_slider = primary; } + void set_secondary_slider(GUI::ValueSlider* secondary) { m_secondary_slider = secondary; } + + GUI::ValueSlider* m_primary_slider { nullptr }; + GUI::ValueSlider* m_secondary_slider { nullptr }; }; } |