Skip to content

Commit

Permalink
Reset state of tool being switched (#155)
Browse files Browse the repository at this point in the history
Fixes (#153) "Segmentation fault when switching modes mid-stroke"
  • Loading branch information
hansemro committed Jun 16, 2022
1 parent 80fdf7c commit bc83fbe
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lorien/InfiniteCanvas/InfiniteCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func center_to_mouse() -> void:
# -------------------------------------------------------------------------------------------------
func use_tool(tool_type: int) -> void:
_active_tool.enabled = false
_selection_tool.deselect_all_strokes()
var prev_tool := _active_tool

match tool_type:
Types.Tool.BRUSH:
_active_tool = _brush_tool
Expand All @@ -92,7 +92,9 @@ func use_tool(tool_type: int) -> void:
Types.Tool.SELECT:
_active_tool = _selection_tool
_use_optimizer = false


if prev_tool != _active_tool:
prev_tool.reset()
_active_tool.enabled = true
_active_tool.get_cursor()._on_zoom_changed(_camera.zoom.x)

Expand Down
4 changes: 4 additions & 0 deletions lorien/InfiniteCanvas/Tools/CanvasTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ func end_stroke() -> void:
# -------------------------------------------------------------------------------------------------
func xform_vector2(v: Vector2) -> Vector2:
return _canvas.get_camera().xform(v)

# -------------------------------------------------------------------------------------------------
func reset() -> void:
end_stroke()
8 changes: 6 additions & 2 deletions lorien/InfiniteCanvas/Tools/EraserTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func _input(event: InputEvent) -> void:
_update_bounding_boxes()
performing_stroke = true
elif !event.pressed:
_bounding_box_cache.clear()
performing_stroke = false
reset()

# -------------------------------------------------------------------------------------------------
func _process(delta: float) -> void:
Expand Down Expand Up @@ -73,3 +72,8 @@ func _update_bounding_boxes() -> void:
var strokes: Array = _canvas.get_all_strokes()
_bounding_box_cache = Utils.calculte_bounding_boxes(strokes, BOUNDING_BOX_MARGIN)
#$"../Viewport/DebugDraw".set_bounding_boxes(_bounding_box_cache.values())

# ------------------------------------------------------------------------------------------------
func reset() -> void:
_bounding_box_cache.clear()
performing_stroke = false
8 changes: 8 additions & 0 deletions lorien/InfiniteCanvas/Tools/SelectionTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,11 @@ func get_selected_strokes() -> Array:
func _on_brush_color_changed(color: Color) -> void:
var strokes := get_selected_strokes()
_modify_strokes_colors(strokes, color)

# ------------------------------------------------------------------------------------------------
func reset() -> void:
_state = State.NONE
_selection_rectangle.reset()
_selection_rectangle.update()
_commit_strokes_under_selection_rectangle()
deselect_all_strokes()

0 comments on commit bc83fbe

Please sign in to comment.