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()`.
This commit is contained in:
Mustafa Quraish 2021-09-03 00:37:49 -04:00 committed by Andreas Kling
parent 8d36893ddf
commit d28fb8926f
2 changed files with 34 additions and 1 deletions

View file

@ -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;
}
}
}

View file

@ -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 };
};
}