Skip to content

Commit

Permalink
fix: #55 orphaned nodes, fix: default preset trying to load inexistan…
Browse files Browse the repository at this point in the history
…t resource, chg: cleanup useless code and signals
  • Loading branch information
HungryProton committed Oct 11, 2021
1 parent 53cb1fa commit 169b9d4
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 90 deletions.
2 changes: 1 addition & 1 deletion plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="Scatter"
description="Scatter other scenes in a manually defined area"
author="HungryProton"
version="2.8.1"
version="2.8.2"
script="plugin.gd"
44 changes: 7 additions & 37 deletions presets/default.tscn

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/core/modifier_stack.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ var just_created := false
var undo_redo: UndoRedo


func _ready():
for m in stack:
if not m.get_parent():
add_child(m)


func update(transforms, random_seed) -> void:
for modifier in stack:
if modifier.enabled:
Expand All @@ -26,6 +32,7 @@ func duplicate_stack() -> Array:
func add_modifier(modifier) -> void:
var restore = duplicate_stack()
stack.push_back(modifier)
add_child(modifier)
_create_undo_action("Added Modifier", restore)
emit_signal("stack_changed")

Expand Down Expand Up @@ -61,6 +68,7 @@ func remove(modifier) -> void:
if stack.has(modifier):
var restore = duplicate_stack()
stack.erase(modifier)
modifier.queue_free()
_create_undo_action("Removed Modifier", restore)
emit_signal("stack_changed")

Expand All @@ -74,5 +82,11 @@ func _create_undo_action(name, restore) -> void:


func _restore_stack(s) -> void:
for c in get_children():
c.queue_free()

stack = s
for m in stack:
add_child(m)

emit_signal("stack_changed")
2 changes: 1 addition & 1 deletion src/core/namespace.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tool
extends Node
extends Reference

# DO NOT replace the load by a preload, this will cause a cyclic dependency
# with scatter.gd and prevent the plugin from loading.
Expand Down
61 changes: 38 additions & 23 deletions src/core/scatter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ var undo_redo setget _set_undo_redo
var _transforms
var _items := []
var _total_proportion: int
var _was_duplicated := false


func _ready() -> void:
if not modifier_stack:
modifier_stack = Scatter.ModifierStack.new()
modifier_stack.just_created = true

var _err = self.connect("curve_updated", self, "update")
_ensure_stack_exists()
_discover_items()

if force_update_when_loaded:
Expand Down Expand Up @@ -68,25 +64,14 @@ func _get(property):


func _set(property, value):
if property == "modifier_stack":
# TODO: This duplicate is there because I couldn't find a way to detect
# when a node is duplicated from the editor and I don't want multiple
# scatter nodes to share the same stack.
modifier_stack = value.duplicate(7)
call_deferred("clear")
return true
if not Engine.editor_hint:
return false

# For some reason, set_modifier_stack is not always called when duplicating
# a node, but other parameters like transforms are so we check that as well
# This is to detect when the node was duplicated from the editor.
if property == "transform":
if modifier_stack:
modifier_stack = modifier_stack.duplicate(7)
else:
modifier_stack = Scatter.ModifierStack.new()
modifier_stack.just_created = true
# Duplicate the curve item too. If someone want to share data, it has
# to be explicitely done by the user

call_deferred("_ensure_stack_exists")
call_deferred("_make_curve_unique")
call_deferred("clear")

Expand Down Expand Up @@ -260,7 +245,7 @@ func _setup_multi_mesh(item, count):
instance.translation = Vector3.ZERO
item.update_shadows()

var mesh_instance: MeshInstance = item.get_mesh_instance()
var mesh_instance: MeshInstance = item.get_mesh_instance_copy()
if not mesh_instance:
_delete_multimeshes()
return
Expand All @@ -280,6 +265,8 @@ func _setup_multi_mesh(item, count):
instance.set_meta("_edit_group_", make_children_unselectable)
instance.set_meta("_edit_lock_", true)

mesh_instance.queue_free()

return instance


Expand Down Expand Up @@ -346,8 +333,16 @@ func _set_modifier_stack(val) -> void:
if not val:
return

modifier_stack = Scatter.ModifierStack.new()
modifier_stack.stack = val.duplicate_stack()
if modifier_stack:
modifier_stack.queue_free()

if val.get_parent(): # Trying to reference an existing stack, unwanted.
modifier_stack = Scatter.ModifierStack.new()
modifier_stack.stack = val.duplicate_stack()
else:
modifier_stack = val

add_child(modifier_stack)

if not modifier_stack.is_connected("stack_changed", self, "update"):
modifier_stack.connect("stack_changed", self, "update")
Expand All @@ -358,6 +353,26 @@ func _make_curve_unique() -> void:
_update_from_curve()


func _ensure_stack_exists() -> void:
if modifier_stack:
var parent: Node = modifier_stack.get_parent()
if not parent:
add_child(modifier_stack)
return

if parent == self:
return

# Parent is another node, this an old reference
modifier_stack = modifier_stack.duplicate(7)
parent.remove_child(modifier_stack)
add_child(modifier_stack)
else:
modifier_stack = Scatter.ModifierStack.new()
modifier_stack.just_created = true
add_child(modifier_stack)


func _reset_all_colliders(node) -> void:
if node is CollisionShape and not node.disabled:
node.disabled = true
Expand Down
30 changes: 15 additions & 15 deletions src/core/scatter_item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,23 @@ func update():
_parent.update()


func get_mesh_instance() -> MeshInstance:
# Check the supplied local path.
func get_mesh_instance_copy() -> MeshInstance:
var root = null

if local_item_path:
if has_node(local_item_path):
var mesh = _get_mesh_from_scene(get_node(local_item_path))
if mesh:
_save_initial_data(mesh)
return mesh
root = get_node_or_null(local_item_path)

# Check the remote scene.
if item_path:
var node = load(item_path)
if node:
var mesh = _get_mesh_from_scene(node.instance())
if mesh:
_save_initial_data(mesh)
return mesh
var scene = load(item_path)
if scene:
root = scene.instance()

if root:
var mesh = _get_mesh_from_scene(root)
root.queue_free()
if mesh:
_save_initial_data(mesh)
return mesh

# Nothing found, print the relevant warning in the console.
if local_item_path:
Expand Down Expand Up @@ -192,7 +192,7 @@ func _get_mesh_from_scene(node):
# Finds the first MeshInstance in the given hierarchy and returns it.
func _get_first_mesh_from_scene(node):
if node is MeshInstance:
return node
return node.duplicate()

for c in node.get_children():
var res = _get_first_mesh_from_scene(c)
Expand Down
2 changes: 1 addition & 1 deletion src/core/transforms.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tool
extends Node
extends Reference


var list := []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func _on_button_pressed() -> void:

func _on_id_pressed(id: int) -> void:
var idx = _popup.get_item_index(id)
print("id ", id, " - idx ", idx)
var checked = not _popup.is_item_checked(idx)
_buttons[id].pressed = checked
_popup.set_item_checked(idx, checked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func _rebuild_ui():

instance.queue_free()


for category in _category_root.get_children():
var header = category.get_child(0)
_sort_children_by_name(category)
Expand Down
12 changes: 2 additions & 10 deletions src/tools/path_gizmo/scatter_path_gizmo_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func set_handle(gizmo: EditorSpatialGizmo, index: int, camera: Camera, point: Ve
if shift_pressed:
path.curve.set_point_in(p_index, -(local_pos - base))

redraw(gizmo)


# Handle Undo / Redo after a handle was moved.
func commit_handle(gizmo: EditorSpatialGizmo, index: int, restore, _cancel: bool = false) -> void:
Expand Down Expand Up @@ -186,9 +188,6 @@ func create_custom_material(name, color := Color.white):

func set_selected(path) -> void:
if _selected and is_instance_valid(_selected):
if _selected.is_connected("curve_updated", self, "_on_curve_updated"):
_selected.disconnect("curve_updated", self, "_on_curve_updated")

if _selected.is_connected("curve_changed", self, "_on_curve_changed"):
_selected.disconnect("curve_changed", self, "_on_curve_changed")

Expand All @@ -201,9 +200,6 @@ func set_selected(path) -> void:
path = null
return

if not path.is_connected("curve_updated", self, "_on_curve_updated"):
path.connect("curve_updated", self, "_on_curve_updated")

if not path.is_connected("curve_changed", self, "_on_curve_changed"):
path.connect("curve_changed", self, "_on_curve_changed")

Expand Down Expand Up @@ -380,10 +376,6 @@ func _on_option_changed() -> void:
redraw(_cached_gizmo)


func _on_curve_updated() -> void:
redraw(_cached_gizmo)


# Force the newly added points on the plane if the option is enabled
# Could have been avoided if Scatter didn't inherited from Path
func _on_curve_changed() -> void:
Expand Down

0 comments on commit 169b9d4

Please sign in to comment.