Skip to content

Commit

Permalink
Canvas keybinding controls (mbrlabs#177)
Browse files Browse the repository at this point in the history
* Zoom in/out shortcuts

* Add missing shortcut translations

* Canvas pan controls

* Add I18N strings and make pan direction match language
  • Loading branch information
MrApplejuice authored Jul 9, 2022
1 parent e65b641 commit b017256
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lorien/Assets/I18n/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ ACTION_duplicate_strokes Duplicate strokes
ACTION_toggle_distraction_free_mode Toggle distraction free mode
ACTION_toggle_player EFF TWELVE
ACTION_toggle_fullscreen Toggle fullscreen
ACTION_canvas_zoom_in Zoom in
ACTION_canvas_zoom_out Zoom out
ACTION_canvas_pan_up Pan up
ACTION_canvas_pan_down Pan down
ACTION_canvas_pan_right Pan right
ACTION_canvas_pan_left Pan left

# -----------------------------------------------------------------------------
# Kebindings dialog messages
Expand Down
16 changes: 15 additions & 1 deletion lorien/InfiniteCanvas/Cursor/BaseCursor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ extends Sprite
var _brush_size: int
var _pressure := 1.0

onready var _camera: Camera2D = get_viewport().get_node("Camera2D")

# -------------------------------------------------------------------------------------------------
func _ready() -> void:
pass

# -------------------------------------------------------------------------------------------------
func _input(event):
if event is InputEventMouseMotion:
_update_position()

# -------------------------------------------------------------------------------------------------
func _update_position():
global_position = _camera.get_global_mouse_position()

# -------------------------------------------------------------------------------------------------
func set_pressure(pressure: float) -> void:
pass
Expand All @@ -17,7 +28,10 @@ func set_pressure(pressure: float) -> void:
func change_size(value: int) -> void:
pass

# -------------------------------------------------------------------------------------------------
func _on_canvas_position_changed(pos: Vector2) -> void:
_update_position()

# -------------------------------------------------------------------------------------------------
func _on_zoom_changed(value: float) -> void:
pass

7 changes: 6 additions & 1 deletion lorien/InfiniteCanvas/InfiniteCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func _ready():
_active_tool.enabled = false

get_tree().get_root().connect("size_changed", self, "_on_window_resized")
_camera.connect("zoom_changed", $Viewport/SelectionCursor, "_on_zoom_changed")

for child in $Viewport.get_children():
if child is BaseCursor:
_camera.connect("zoom_changed", child, "_on_zoom_changed")
_camera.connect("position_changed", child, "_on_canvas_position_changed")

_camera.connect("zoom_changed", self, "_on_zoom_changed")
_camera.connect("position_changed", self, "_on_camera_moved")
_viewport.size = OS.window_size
Expand Down
25 changes: 25 additions & 0 deletions lorien/InfiniteCanvas/PanZoomCamera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ signal position_changed(value)
const ZOOM_INCREMENT := 1.1 # Feel free to modify (Krita uses sqrt(2))
const MIN_ZOOM_LEVEL := 0.1
const MAX_ZOOM_LEVEL := 100
const KEYBOARD_PAN_CONSTANT := 20

var _is_input_enabled := true

Expand Down Expand Up @@ -56,6 +57,30 @@ func tool_event(event: InputEvent) -> void:
_do_pan(event.relative)
elif _zoom_active:
_do_zoom_drag(event.relative.y)

elif Utils.event_pressed_bug_workaround("canvas_zoom_in", event):
_do_zoom_scroll(-1)
get_tree().set_input_as_handled()

elif Utils.event_pressed_bug_workaround("canvas_zoom_out", event):
_do_zoom_scroll(1)
get_tree().set_input_as_handled()

elif Utils.event_pressed_bug_workaround("canvas_pan_left", event):
_do_pan(-Vector2.LEFT * KEYBOARD_PAN_CONSTANT)
get_tree().set_input_as_handled()

elif Utils.event_pressed_bug_workaround("canvas_pan_right", event):
_do_pan(-Vector2.RIGHT * KEYBOARD_PAN_CONSTANT)
get_tree().set_input_as_handled()

elif Utils.event_pressed_bug_workaround("canvas_pan_up", event):
_do_pan(-Vector2.UP * KEYBOARD_PAN_CONSTANT)
get_tree().set_input_as_handled()

elif Utils.event_pressed_bug_workaround("canvas_pan_down", event):
_do_pan(-Vector2.DOWN * KEYBOARD_PAN_CONSTANT)
get_tree().set_input_as_handled()

# -------------------------------------------------------------------------------------------------
func _do_pan(pan: Vector2) -> void:
Expand Down
1 change: 0 additions & 1 deletion lorien/InfiniteCanvas/Tools/BrushTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func tool_event(event: InputEvent) -> void:
_cursor.set_pressure(1.0)

if event is InputEventMouseMotion:
_cursor.global_position = xform_vector2(event.global_position)
_current_position = event.position
_current_pressure = event.pressure
if performing_stroke:
Expand Down
2 changes: 0 additions & 2 deletions lorien/InfiniteCanvas/Tools/CanvasTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func set_enabled(e: bool) -> void:
set_process(enabled)
set_process_input(enabled)
_cursor.set_visible(enabled)
if enabled && _canvas:
_cursor.global_position = xform_vector2(get_viewport().get_mouse_position())

# -------------------------------------------------------------------------------------------------
func get_enabled() -> bool:
Expand Down
1 change: 0 additions & 1 deletion lorien/InfiniteCanvas/Tools/CircleTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func tool_event(event: InputEvent) -> void:
var should_draw_circle := Input.is_key_pressed(KEY_SHIFT)

if event is InputEventMouseMotion:
_cursor.global_position = xform_vector2(event.global_position)
if performing_stroke:
_cursor.set_pressure(event.pressure)
remove_all_stroke_points()
Expand Down
1 change: 0 additions & 1 deletion lorien/InfiniteCanvas/Tools/EraserTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var _bounding_box_cache = {} # BrushStroke -> Rect2
func tool_event(event: InputEvent) -> void:
if event is InputEventMouseMotion:
_last_mouse_position = xform_vector2(event.global_position)
_cursor.global_position = _last_mouse_position
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.pressed:
Expand Down
1 change: 0 additions & 1 deletion lorien/InfiniteCanvas/Tools/LineTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func tool_event(event: InputEvent) -> void:

# Moving the tail
elif event is InputEventMouseMotion:
_cursor.global_position = xform_vector2(event.global_position)
if performing_stroke:
_cursor.set_pressure(event.pressure)
remove_last_stroke_point()
Expand Down
1 change: 0 additions & 1 deletion lorien/InfiniteCanvas/Tools/RectangleTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func tool_event(event: InputEvent) -> void:
_cursor.set_pressure(1.0)

if event is InputEventMouseMotion:
_cursor.global_position = xform_vector2(event.global_position)
if performing_stroke:
_cursor.set_pressure(event.pressure)
remove_all_stroke_points()
Expand Down
1 change: 0 additions & 1 deletion lorien/InfiniteCanvas/Tools/SelectionTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func tool_event(event: InputEvent) -> void:
# Mouse movement: move the selection
elif event is InputEventMouseMotion:
var event_pos := xform_vector2(event.global_position)
_cursor.global_position = event_pos
if _state == State.SELECTING:
_selecting_end_pos = event_pos
compute_selection(_selecting_start_pos, _selecting_end_pos)
Expand Down
30 changes: 30 additions & 0 deletions lorien/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,36 @@ shortcut_export_project={
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":true,"meta":true,"command":true,"pressed":false,"scancode":69,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
canvas_zoom_in={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":43,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
canvas_zoom_out={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":45,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
canvas_pan_up={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
canvas_pan_down={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
canvas_pan_left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
canvas_pan_right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}

[locale]

Expand Down

0 comments on commit b017256

Please sign in to comment.