Skip to content

Commit bc83fbe

Browse files
authored
Reset state of tool being switched (#155)
Fixes (#153) "Segmentation fault when switching modes mid-stroke"
1 parent 80fdf7c commit bc83fbe

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

lorien/InfiniteCanvas/InfiniteCanvas.gd

+5-3
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func center_to_mouse() -> void:
7171
# -------------------------------------------------------------------------------------------------
7272
func use_tool(tool_type: int) -> void:
7373
_active_tool.enabled = false
74-
_selection_tool.deselect_all_strokes()
75-
74+
var prev_tool := _active_tool
75+
7676
match tool_type:
7777
Types.Tool.BRUSH:
7878
_active_tool = _brush_tool
@@ -92,7 +92,9 @@ func use_tool(tool_type: int) -> void:
9292
Types.Tool.SELECT:
9393
_active_tool = _selection_tool
9494
_use_optimizer = false
95-
95+
96+
if prev_tool != _active_tool:
97+
prev_tool.reset()
9698
_active_tool.enabled = true
9799
_active_tool.get_cursor()._on_zoom_changed(_camera.zoom.x)
98100

lorien/InfiniteCanvas/Tools/CanvasTool.gd

+4
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@ func end_stroke() -> void:
8989
# -------------------------------------------------------------------------------------------------
9090
func xform_vector2(v: Vector2) -> Vector2:
9191
return _canvas.get_camera().xform(v)
92+
93+
# -------------------------------------------------------------------------------------------------
94+
func reset() -> void:
95+
end_stroke()

lorien/InfiniteCanvas/Tools/EraserTool.gd

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ func _input(event: InputEvent) -> void:
2222
_update_bounding_boxes()
2323
performing_stroke = true
2424
elif !event.pressed:
25-
_bounding_box_cache.clear()
26-
performing_stroke = false
25+
reset()
2726

2827
# -------------------------------------------------------------------------------------------------
2928
func _process(delta: float) -> void:
@@ -73,3 +72,8 @@ func _update_bounding_boxes() -> void:
7372
var strokes: Array = _canvas.get_all_strokes()
7473
_bounding_box_cache = Utils.calculte_bounding_boxes(strokes, BOUNDING_BOX_MARGIN)
7574
#$"../Viewport/DebugDraw".set_bounding_boxes(_bounding_box_cache.values())
75+
76+
# ------------------------------------------------------------------------------------------------
77+
func reset() -> void:
78+
_bounding_box_cache.clear()
79+
performing_stroke = false

lorien/InfiniteCanvas/Tools/SelectionTool.gd

+8
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,11 @@ func get_selected_strokes() -> Array:
245245
func _on_brush_color_changed(color: Color) -> void:
246246
var strokes := get_selected_strokes()
247247
_modify_strokes_colors(strokes, color)
248+
249+
# ------------------------------------------------------------------------------------------------
250+
func reset() -> void:
251+
_state = State.NONE
252+
_selection_rectangle.reset()
253+
_selection_rectangle.update()
254+
_commit_strokes_under_selection_rectangle()
255+
deselect_all_strokes()

0 commit comments

Comments
 (0)