Skip to content

Commit

Permalink
HackStudio: Allow moving the selected widgets using the arrow keys
Browse files Browse the repository at this point in the history
This is a nice complement to moving widgets with the mouse. :^)
  • Loading branch information
awesomekling committed Nov 10, 2019
1 parent 567769e commit c8637e0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
29 changes: 29 additions & 0 deletions DevTools/HackStudio/CursorTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,32 @@ void CursorTool::on_mousemove(GMouseEvent& event)
return;
}
}

void CursorTool::on_keydown(GKeyEvent& event)
{
dbg() << "CursorTool::on_keydown";

auto move_selected_widgets_by = [this](int x, int y) {
m_editor.selection().for_each([&](auto& widget) {
widget.move_by(x, y);
return IterationDecision::Continue;
});
};

if (event.modifiers() == 0) {
switch (event.key()) {
case Key_Down:
move_selected_widgets_by(0, m_editor.form_widget().grid_size());
break;
case Key_Up:
move_selected_widgets_by(0, -m_editor.form_widget().grid_size());
break;
case Key_Left:
move_selected_widgets_by(-m_editor.form_widget().grid_size(), 0);
break;
case Key_Right:
move_selected_widgets_by(m_editor.form_widget().grid_size(), 0);
break;
}
}
}
1 change: 1 addition & 0 deletions DevTools/HackStudio/CursorTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CursorTool final : public Tool {
virtual void on_mousedown(GMouseEvent&) override;
virtual void on_mouseup(GMouseEvent&) override;
virtual void on_mousemove(GMouseEvent&) override;
virtual void on_keydown(GKeyEvent&) override;

Point m_drag_origin;
HashMap<GWidget*, Point> m_positions_before_drag;
Expand Down
5 changes: 5 additions & 0 deletions DevTools/HackStudio/FormWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ void FormWidget::mousemove_event(GMouseEvent& event)
{
editor().tool().on_mousemove(event);
}

void FormWidget::keydown_event(GKeyEvent& event)
{
editor().tool().on_keydown(event);
}
3 changes: 3 additions & 0 deletions DevTools/HackStudio/FormWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class FormWidget final : public GWidget {
int grid_size() const { return m_grid_size; }

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;

explicit FormWidget(FormEditorWidget& parent);

Expand Down
2 changes: 2 additions & 0 deletions DevTools/HackStudio/Tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <AK/Noncopyable.h>

class FormEditorWidget;
class GKeyEvent;
class GMouseEvent;

class Tool {
Expand All @@ -14,6 +15,7 @@ class Tool {
virtual void on_mousedown(GMouseEvent&) = 0;
virtual void on_mouseup(GMouseEvent&) = 0;
virtual void on_mousemove(GMouseEvent&) = 0;
virtual void on_keydown(GKeyEvent&) = 0;

virtual const char* class_name() const = 0;

Expand Down
6 changes: 6 additions & 0 deletions DevTools/HackStudio/WidgetTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ void WidgetTool::on_mousemove(GMouseEvent& event)
(void)event;
dbg() << "WidgetTool::on_mousemove";
}

void WidgetTool::on_keydown(GKeyEvent& event)
{
(void)event;
dbg() << "WidgetTool::on_keydown";
}
1 change: 1 addition & 0 deletions DevTools/HackStudio/WidgetTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class WidgetTool final : public Tool {
virtual void on_mousedown(GMouseEvent&) override;
virtual void on_mouseup(GMouseEvent&) override;
virtual void on_mousemove(GMouseEvent&) override;
virtual void on_keydown(GKeyEvent&) override;

const GWidgetClassRegistration& m_meta_class;
};

0 comments on commit c8637e0

Please sign in to comment.