Skip to content

Commit

Permalink
Disable stroke while panning or zooming (#302)
Browse files Browse the repository at this point in the history
* Disable stroke while panning or zooming
- End stroking of brush tool when zooming

* Rename symbols
  • Loading branch information
Zacrain authored Sep 20, 2024
1 parent 0bd6fff commit fbd62e6
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
# Mono-specific ignores
.mono/
data_*/

# Visual Studio Code-specific ignores
.vscode/
9 changes: 7 additions & 2 deletions lorien/InfiniteCanvas/InfiniteCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ func _ready() -> void:

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

for child in get_children():
if child is CanvasTool:
_camera.panning_toggled.connect(child._on_panning_toggled)
_camera.zooming_toggled.connect(child._on_zooming_toggled)

_camera.zoom_changed.connect(_on_zoom_changed)
_camera.position_changed.connect(_on_camera_moved)
Expand Down
8 changes: 8 additions & 0 deletions lorien/InfiniteCanvas/PanZoomCamera.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
extends Camera2D

# -------------------------------------------------------------------------------------------------
signal zooming_toggled(value: bool)
signal zoom_changed(value: float)
signal panning_toggled(value: bool)
signal position_changed(value: Vector2)

# -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -39,8 +41,10 @@ func tool_event(event: InputEvent) -> void:
if event is InputEventKey:
if Utils.is_action_pressed("canvas_pan_key", event):
_pan_active = true
panning_toggled.emit(true)
if Utils.is_action_released("canvas_pan_key", event):
_pan_active = false
panning_toggled.emit(false)
if event is InputEventMouseButton:

# Scroll wheel up/down to zoom
Expand All @@ -55,10 +59,14 @@ func tool_event(event: InputEvent) -> void:
if event.button_index == MOUSE_BUTTON_MIDDLE:
if !event.ctrl_pressed:
_pan_active = event.is_pressed()
panning_toggled.emit(_pan_active)
_zoom_active = false
zooming_toggled.emit(false)
else:
_zoom_active = event.is_pressed()
zooming_toggled.emit(_zoom_active)
_pan_active = false
panning_toggled.emit(false)
_start_mouse_pos = get_local_mouse_position()

elif event is InputEventMouseMotion:
Expand Down
4 changes: 3 additions & 1 deletion lorien/InfiniteCanvas/Tools/BrushTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func tool_event(event: InputEvent) -> void:
_current_pressure = event.pressure
if performing_stroke:
_cursor.set_pressure(event.pressure)
if zooming_detected && performing_stroke:
end_stroke()

elif event is InputEventMouseButton:
elif event is InputEventMouseButton && !disable_stroke:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
start_stroke()
Expand Down
16 changes: 16 additions & 0 deletions lorien/InfiniteCanvas/Tools/CanvasTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var _cursor: BaseCursor
var _canvas: InfiniteCanvas
var enabled := false: get = get_enabled, set = set_enabled
var performing_stroke := false
var disable_stroke := false
var panning_detected := false
var zooming_detected := false

# -------------------------------------------------------------------------------------------------
func _ready() -> void:
Expand All @@ -32,6 +35,16 @@ func _on_brush_color_changed(color: Color) -> void:
func _on_brush_size_changed(size: int) -> void:
_cursor.change_size(size)

# -------------------------------------------------------------------------------------------------
func _on_panning_toggled(panning_enabled: bool) -> void:
panning_detected = panning_enabled
_eval_disable_stroke()

# -------------------------------------------------------------------------------------------------
func _on_zooming_toggled(zooming_enabled: bool) -> void:
zooming_detected = zooming_enabled
_eval_disable_stroke()

# -------------------------------------------------------------------------------------------------
func get_cursor() -> BaseCursor:
return _cursor
Expand Down Expand Up @@ -89,6 +102,9 @@ func end_stroke() -> void:
_canvas.end_stroke()
performing_stroke = false

func _eval_disable_stroke() -> void:
disable_stroke = panning_detected || zooming_detected

# TODO(gd4): probably don't need this anymore
# -------------------------------------------------------------------------------------------------
#func xform_vector2(v: Vector2) -> Vector2:
Expand Down
2 changes: 1 addition & 1 deletion lorien/InfiniteCanvas/Tools/CircleTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func tool_event(event: InputEvent) -> void:
_make_ellipse(PRESSURE, STEP_IN_MOTION, should_draw_circle)

# Start + End
elif event is InputEventMouseButton:
elif event is InputEventMouseButton && !disable_stroke:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
start_stroke()
Expand Down
2 changes: 1 addition & 1 deletion lorien/InfiniteCanvas/Tools/EraserTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _bounding_box_cache := {} # BrushStroke -> Rect2
func tool_event(event: InputEvent) -> void:
if event is InputEventMouseMotion:
_last_mouse_position = _cursor.global_position
if event is InputEventMouseButton:
if event is InputEventMouseButton && !disable_stroke:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
if _bounding_box_cache.is_empty():
Expand Down
2 changes: 1 addition & 1 deletion lorien/InfiniteCanvas/Tools/LineTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func tool_event(event: InputEvent) -> void:
_tail = _add_point_at_mouse_pos(0.5)

# Start + End
elif event is InputEventMouseButton:
elif event is InputEventMouseButton && !disable_stroke:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
start_stroke()
Expand Down
2 changes: 1 addition & 1 deletion lorien/InfiniteCanvas/Tools/RectangleTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func tool_event(event: InputEvent) -> void:
_make_rectangle(PRESSURE)

# Start + End
elif event is InputEventMouseButton:
elif event is InputEventMouseButton && !disable_stroke:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
start_stroke()
Expand Down
2 changes: 1 addition & 1 deletion lorien/InfiniteCanvas/Tools/SelectionTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func tool_event(event: InputEvent) -> void:
_cursor.mode = SelectionCursor.Mode.MOVE
_paste_strokes(strokes)

if event is InputEventMouseButton:
if event is InputEventMouseButton && !disable_stroke:
if event.button_index == MOUSE_BUTTON_LEFT:
# LMB down - decide if we should select/multiselect or move the selection
if event.pressed:
Expand Down

0 comments on commit fbd62e6

Please sign in to comment.