Skip to content

Commit

Permalink
Add support for InputEventMouseMotion.pen_inverted (#966)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Ratfink authored Dec 25, 2023
1 parent dffaf1d commit 3d2d9e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/Autoload/Tools.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/UI/ToolsPanel/ToolButtons.gd
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)

0 comments on commit 3d2d9e4

Please sign in to comment.