From be3284b759e83835848e3cb5b12eedf2b2d6b547 Mon Sep 17 00:00:00 2001 From: Clara Hobbs Date: Thu, 21 Dec 2023 10:05:50 -0500 Subject: [PATCH 1/2] Add support for InputEventMouseMotion.pen_inverted This commit adds support for stylus erasers, both for drawing and choosing tools. This may be supported on some styli by inverting them as the property name suggests, or by holding a button while drawing with the nib. --- src/Autoload/Tools.gd | 11 ++++++++--- src/UI/ToolsPanel/ToolButtons.gd | 9 ++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Autoload/Tools.gd b/src/Autoload/Tools.gd index 89221d1bb9bb..84dc134f56da 100644 --- a/src/Autoload/Tools.gd +++ b/src/Autoload/Tools.gd @@ -18,6 +18,7 @@ var pen_pressure := 1.0 var pen_pressure_min := 0.2 var pen_pressure_max := 0.8 var pressure_buf := [0, 0] # past pressure value buffer +var pen_inverted := false var mouse_velocity := 1.0 var mouse_velocity_min_thres := 0.2 var mouse_velocity_max_thres := 0.8 @@ -485,16 +486,18 @@ func handle_draw(position: Vector2i, event: InputEvent) -> void: if Global.mirror_view: draw_pos.x = Global.current_project.size.x - position.x - 1 - if event.is_action_pressed("activate_left_tool") and _active_button == -1: + if event.is_action_pressed("activate_left_tool") and _active_button == -1 and not pen_inverted: _active_button = MOUSE_BUTTON_LEFT _slots[_active_button].tool_node.draw_start(draw_pos) elif event.is_action_released("activate_left_tool") and _active_button == MOUSE_BUTTON_LEFT: _slots[_active_button].tool_node.draw_end(draw_pos) _active_button = -1 - elif event.is_action_pressed("activate_right_tool") and _active_button == -1: + elif ((event.is_action_pressed("activate_right_tool") and _active_button == -1 and not pen_inverted) + or (event.is_action_pressed("activate_left_tool") and _active_button == -1 and pen_inverted)): _active_button = MOUSE_BUTTON_RIGHT _slots[_active_button].tool_node.draw_start(draw_pos) - elif event.is_action_released("activate_right_tool") and _active_button == MOUSE_BUTTON_RIGHT: + elif ((event.is_action_released("activate_right_tool") and _active_button == MOUSE_BUTTON_RIGHT) + or (event.is_action_released("activate_left_tool") and _active_button == MOUSE_BUTTON_RIGHT)): _slots[_active_button].tool_node.draw_end(draw_pos) _active_button = -1 @@ -511,6 +514,8 @@ func handle_draw(position: Vector2i, event: InputEvent) -> void: pressure_buf.push_front(pen_pressure) pen_pressure = remap(pen_pressure, pen_pressure_min, pen_pressure_max, 0.0, 1.0) pen_pressure = clampf(pen_pressure, 0.0, 1.0) + + pen_inverted = event.pen_inverted mouse_velocity = event.velocity.length() / mouse_velocity_max mouse_velocity = remap( diff --git a/src/UI/ToolsPanel/ToolButtons.gd b/src/UI/ToolsPanel/ToolButtons.gd index 4a0585db4e47..f09fd5f8821e 100644 --- a/src/UI/ToolsPanel/ToolButtons.gd +++ b/src/UI/ToolsPanel/ToolButtons.gd @@ -1,10 +1,13 @@ extends FlowContainer +var pen_inverted := false + func _input(event: InputEvent) -> void: - if not Global.has_focus or not Global.can_draw: - return if event is InputEventMouseMotion: + pen_inverted = event.pen_inverted + return + if not Global.has_focus or not Global.can_draw: return for action in ["undo", "redo"]: if event.is_action_pressed(action): @@ -29,6 +32,6 @@ func _input(event: InputEvent) -> void: func _on_Tool_pressed(tool_pressed: BaseButton) -> void: var button := -1 button = MOUSE_BUTTON_LEFT if Input.is_action_just_released("left_mouse") else button - button = MOUSE_BUTTON_RIGHT if Input.is_action_just_released("right_mouse") else button + button = MOUSE_BUTTON_RIGHT if Input.is_action_just_released("right_mouse") or (pen_inverted and Input.is_action_just_released("left_mouse")) else button if button != -1: Tools.assign_tool(tool_pressed.name, button) From 8429e5bb9840f4418a4400740c672cfbd501dffe Mon Sep 17 00:00:00 2001 From: Clara Hobbs Date: Sun, 24 Dec 2023 06:49:02 -0500 Subject: [PATCH 2/2] Formatting fixes --- src/Autoload/Tools.gd | 18 +++++++++++++----- src/UI/ToolsPanel/ToolButtons.gd | 11 +++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Autoload/Tools.gd b/src/Autoload/Tools.gd index 84dc134f56da..0c7332964212 100644 --- a/src/Autoload/Tools.gd +++ b/src/Autoload/Tools.gd @@ -492,12 +492,20 @@ func handle_draw(position: Vector2i, event: InputEvent) -> void: elif event.is_action_released("activate_left_tool") and _active_button == MOUSE_BUTTON_LEFT: _slots[_active_button].tool_node.draw_end(draw_pos) _active_button = -1 - elif ((event.is_action_pressed("activate_right_tool") and _active_button == -1 and not pen_inverted) - or (event.is_action_pressed("activate_left_tool") and _active_button == -1 and pen_inverted)): + elif ( + ( + event.is_action_pressed("activate_right_tool") + and _active_button == -1 + and not pen_inverted + ) + or (event.is_action_pressed("activate_left_tool") and _active_button == -1 and pen_inverted) + ): _active_button = MOUSE_BUTTON_RIGHT _slots[_active_button].tool_node.draw_start(draw_pos) - elif ((event.is_action_released("activate_right_tool") and _active_button == MOUSE_BUTTON_RIGHT) - or (event.is_action_released("activate_left_tool") and _active_button == MOUSE_BUTTON_RIGHT)): + elif ( + (event.is_action_released("activate_right_tool") and _active_button == MOUSE_BUTTON_RIGHT) + or (event.is_action_released("activate_left_tool") and _active_button == MOUSE_BUTTON_RIGHT) + ): _slots[_active_button].tool_node.draw_end(draw_pos) _active_button = -1 @@ -514,7 +522,7 @@ func handle_draw(position: Vector2i, event: InputEvent) -> void: pressure_buf.push_front(pen_pressure) pen_pressure = remap(pen_pressure, pen_pressure_min, pen_pressure_max, 0.0, 1.0) pen_pressure = clampf(pen_pressure, 0.0, 1.0) - + pen_inverted = event.pen_inverted mouse_velocity = event.velocity.length() / mouse_velocity_max diff --git a/src/UI/ToolsPanel/ToolButtons.gd b/src/UI/ToolsPanel/ToolButtons.gd index f09fd5f8821e..f1b794898769 100644 --- a/src/UI/ToolsPanel/ToolButtons.gd +++ b/src/UI/ToolsPanel/ToolButtons.gd @@ -1,8 +1,8 @@ extends FlowContainer - var pen_inverted := false + func _input(event: InputEvent) -> void: if event is InputEventMouseMotion: pen_inverted = event.pen_inverted @@ -32,6 +32,13 @@ func _input(event: InputEvent) -> void: func _on_Tool_pressed(tool_pressed: BaseButton) -> void: var button := -1 button = MOUSE_BUTTON_LEFT if Input.is_action_just_released("left_mouse") else button - button = MOUSE_BUTTON_RIGHT if Input.is_action_just_released("right_mouse") or (pen_inverted and Input.is_action_just_released("left_mouse")) else button + button = ( + MOUSE_BUTTON_RIGHT + if ( + Input.is_action_just_released("right_mouse") + or (pen_inverted and Input.is_action_just_released("left_mouse")) + ) + else button + ) if button != -1: Tools.assign_tool(tool_pressed.name, button)