Skip to content

Commit

Permalink
PaintBrush: Allow canceling a line by pressing the Escape key
Browse files Browse the repository at this point in the history
Sometimes you change your mind mid-line, and just want to get out of
the situation. You can now do that :^)
  • Loading branch information
awesomekling committed Nov 29, 2019
1 parent 4e6cd54 commit b09ac26
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Applications/PaintBrush/LineTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ void LineTool::on_second_paint(GPaintEvent& event)
painter.draw_line(m_line_start_position, m_line_end_position, m_widget->color_for(m_drawing_button), m_thickness);
}

void LineTool::on_keydown(GKeyEvent& event)
{
if (event.key() == Key_Escape && m_drawing_button != GMouseButton::None) {
m_drawing_button = GMouseButton::None;
m_widget->update();
event.accept();
}
}

void LineTool::on_contextmenu(GContextMenuEvent& event)
{
if (!m_context_menu) {
Expand Down
1 change: 1 addition & 0 deletions Applications/PaintBrush/LineTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class LineTool final : public Tool {
virtual void on_mouseup(GMouseEvent&) override;
virtual void on_contextmenu(GContextMenuEvent&) override;
virtual void on_second_paint(GPaintEvent&) override;
virtual void on_keydown(GKeyEvent&) override;

private:
virtual const char* class_name() const override { return "LineTool"; }
Expand Down
7 changes: 7 additions & 0 deletions Applications/PaintBrush/PaintableWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ void PaintableWidget::second_paint_event(GPaintEvent& event)
GWidget::second_paint_event(event);
}

void PaintableWidget::keydown_event(GKeyEvent& event)
{
if (m_tool)
m_tool->on_keydown(event);
GWidget::keydown_event(event);
}

void PaintableWidget::set_primary_color(Color color)
{
if (m_primary_color == color)
Expand Down
2 changes: 2 additions & 0 deletions Applications/PaintBrush/PaintableWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ class PaintableWidget final : public GWidget {
Function<void(Color)> on_secondary_color_change;

private:
virtual bool accepts_focus() const override { return true; }
virtual void paint_event(GPaintEvent&) override;
virtual void second_paint_event(GPaintEvent&) override;
virtual void mousedown_event(GMouseEvent&) override;
virtual void mouseup_event(GMouseEvent&) override;
virtual void mousemove_event(GMouseEvent&) override;
virtual void keydown_event(GKeyEvent&) override;

RefPtr<GraphicsBitmap> m_bitmap;

Expand Down
2 changes: 1 addition & 1 deletion Applications/PaintBrush/Tool.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "PaintableWidget.h"
class GMouseEvent;

class Tool {
public:
Expand All @@ -14,6 +13,7 @@ class Tool {
virtual void on_mouseup(GMouseEvent&) {}
virtual void on_contextmenu(GContextMenuEvent&) {}
virtual void on_second_paint(GPaintEvent&) {}
virtual void on_keydown(GKeyEvent&) {}

void clear() { m_widget = nullptr; }
void setup(PaintableWidget& widget) { m_widget = widget.make_weak_ptr(); }
Expand Down
1 change: 1 addition & 0 deletions Applications/PaintBrush/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int main(int argc, char** argv)
vertical_container->layout()->set_spacing(0);

auto paintable_widget = PaintableWidget::construct(vertical_container);
paintable_widget->set_focus(true);
PaletteWidget::construct(*paintable_widget, vertical_container);

window->show();
Expand Down

0 comments on commit b09ac26

Please sign in to comment.