From 3d2d9e47a80277399b9edde4bf25f7dd4a1c7803 Mon Sep 17 00:00:00 2001 From: Clara Hobbs Date: Sun, 24 Dec 2023 19:50:10 -0500 Subject: [PATCH] Add support for InputEventMouseMotion.pen_inverted (#966) * 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. * Formatting fixes --- src/Autoload/Tools.gd | 19 ++++++++++++++++--- src/UI/ToolsPanel/ToolButtons.gd | 16 +++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Autoload/Tools.gd b/src/Autoload/Tools.gd index 89272a0f1..468567d61 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 @@ -483,16 +484,26 @@ 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 @@ -510,6 +521,8 @@ func handle_draw(position: Vector2i, event: InputEvent) -> void: 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( mouse_velocity, mouse_velocity_min_thres, mouse_velocity_max_thres, 0.0, 1.0 diff --git a/src/UI/ToolsPanel/ToolButtons.gd b/src/UI/ToolsPanel/ToolButtons.gd index 4a0585db4..f1b794898 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,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") 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)