Skip to content

Commit

Permalink
Fixed some crashes when switching scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-kish committed Feb 17, 2024
1 parent 69fa2e5 commit 3f784f8
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 69 deletions.
8 changes: 4 additions & 4 deletions addons/gloot/ui/ctrl_dragable.gd
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static func get_grab_offset() -> Vector2:


func drag_start() -> void:
if drag_preview == null:
if !is_instance_valid(drag_preview):
return

drag_preview.mouse_filter = Control.MOUSE_FILTER_IGNORE
Expand All @@ -94,7 +94,7 @@ func _get_global_preview_position() -> Vector2:


func drag_end() -> void:
if drag_preview == null:
if !is_instance_valid(drag_preview):
return

_preview_canvas_layer.remove_child(drag_preview)
Expand All @@ -103,12 +103,12 @@ func drag_end() -> void:


func _notification(what) -> void:
if what == NOTIFICATION_PREDELETE && _preview_canvas_layer:
if what == NOTIFICATION_PREDELETE && is_instance_valid(_preview_canvas_layer):
_preview_canvas_layer.queue_free()


func _process(_delta) -> void:
if drag_preview:
if is_instance_valid(drag_preview):
drag_preview.global_position = _get_global_preview_position()


Expand Down
14 changes: 7 additions & 7 deletions addons/gloot/ui/ctrl_inventory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ signal inventory_item_context_activated(item)
if is_inside_tree():
assert(node is Inventory)

self.inventory = node
inventory = node
update_configuration_warnings()


Expand Down Expand Up @@ -50,7 +50,7 @@ func _get_configuration_warnings() -> PackedStringArray:
func _ready():
if Engine.is_editor_hint():
# Clean up, in case it is duplicated in the editor
if _vbox_container:
if is_instance_valid(_vbox_container):
_vbox_container.queue_free()

_vbox_container = VBoxContainer.new()
Expand All @@ -68,13 +68,13 @@ func _ready():
_vbox_container.add_child(_item_list)

if has_node(inventory_path):
self.inventory = get_node(inventory_path)
inventory = get_node(inventory_path)

_refresh()


func _connect_inventory_signals() -> void:
if !inventory:
if !is_instance_valid(inventory):
return

if !inventory.contents_changed.is_connected(_refresh):
Expand All @@ -84,7 +84,7 @@ func _connect_inventory_signals() -> void:


func _disconnect_inventory_signals() -> void:
if !inventory:
if !is_instance_valid(inventory):
return

if inventory.contents_changed.is_connected(_refresh):
Expand Down Expand Up @@ -113,12 +113,12 @@ func _refresh() -> void:


func _clear_list() -> void:
if _item_list:
if is_instance_valid(_item_list):
_item_list.clear()


func _populate_list() -> void:
if inventory == null:
if !is_instance_valid(inventory):
return

for item in inventory.get_items():
Expand Down
56 changes: 26 additions & 30 deletions addons/gloot/ui/ctrl_inventory_grid.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const CtrlDragable = preload("res://addons/gloot/ui/ctrl_dragable.gd")
if is_inside_tree():
assert(node is InventoryGrid)

self.inventory = node
inventory = node
update_configuration_warnings()
@export var default_item_texture: Texture2D :
set(new_default_item_texture):
Expand All @@ -69,8 +69,8 @@ var inventory: InventoryGrid = null :
_connect_inventory_signals()

_refresh()
var _ctrl_item_container: WeakRef = weakref(null)
var _ctrl_drop_zone: CtrlDropZone
var _ctrl_item_container: Control = null
var _ctrl_drop_zone: CtrlDropZone = null
var _selected_item: InventoryItem = null


Expand All @@ -85,17 +85,15 @@ func _get_configuration_warnings() -> PackedStringArray:
func _ready() -> void:
if Engine.is_editor_hint():
# Clean up, in case it is duplicated in the editor
var ctrl_item_container = _ctrl_item_container.get_ref()
if ctrl_item_container:
ctrl_item_container.queue_free()

var ctrl_item_container = Control.new()
ctrl_item_container.size_flags_horizontal = SIZE_EXPAND_FILL
ctrl_item_container.size_flags_vertical = SIZE_EXPAND_FILL
ctrl_item_container.anchor_right = 1.0
ctrl_item_container.anchor_bottom = 1.0
add_child(ctrl_item_container)
_ctrl_item_container = weakref(ctrl_item_container)
if is_instance_valid(_ctrl_item_container):
_ctrl_item_container.queue_free()

_ctrl_item_container = Control.new()
_ctrl_item_container.size_flags_horizontal = SIZE_EXPAND_FILL
_ctrl_item_container.size_flags_vertical = SIZE_EXPAND_FILL
_ctrl_item_container.anchor_right = 1.0
_ctrl_item_container.anchor_bottom = 1.0
add_child(_ctrl_item_container)

_ctrl_drop_zone = CtrlDropZone.new()
_ctrl_drop_zone.dragable_dropped.connect(_on_dragable_dropped)
Expand All @@ -112,16 +110,16 @@ func _ready() -> void:
add_child(_ctrl_drop_zone)
_ctrl_drop_zone.deactivate()

ctrl_item_container.resized.connect(func(): _ctrl_drop_zone.size = ctrl_item_container.size)
_ctrl_item_container.resized.connect(func(): _ctrl_drop_zone.size = _ctrl_item_container.size)

if has_node(inventory_path):
self.inventory = get_node_or_null(inventory_path)
inventory = get_node_or_null(inventory_path)

_refresh()


func _connect_inventory_signals() -> void:
if !inventory:
if !is_instance_valid(inventory):
return

if !inventory.contents_changed.is_connected(_refresh):
Expand All @@ -135,7 +133,7 @@ func _connect_inventory_signals() -> void:


func _disconnect_inventory_signals() -> void:
if !inventory:
if !is_instance_valid(inventory):
return

if inventory.contents_changed.is_connected(_refresh):
Expand Down Expand Up @@ -169,7 +167,7 @@ func _refresh() -> void:


func _draw() -> void:
if !inventory:
if !is_instance_valid(inventory):
return
if draw_grid:
_draw_grid(Vector2.ZERO, inventory.size.x, inventory.size.y, field_dimensions, item_spacing)
Expand Down Expand Up @@ -213,26 +211,24 @@ func _get_inventory_size_px() -> Vector2:


func _refresh_grid_container() -> void:
if !inventory:
if !is_instance_valid(inventory):
return

custom_minimum_size = _get_inventory_size_px()
size = custom_minimum_size


func _clear_list() -> void:
var ctrl_item_container = _ctrl_item_container.get_ref()
if !ctrl_item_container:
if !is_instance_valid(_ctrl_item_container):
return

for ctrl_inventory_item in ctrl_item_container.get_children():
ctrl_item_container.remove_child(ctrl_inventory_item)
for ctrl_inventory_item in _ctrl_item_container.get_children():
_ctrl_item_container.remove_child(ctrl_inventory_item)
ctrl_inventory_item.queue_free()


func _populate_list() -> void:
var ctrl_item_container = _ctrl_item_container.get_ref()
if inventory == null || ctrl_item_container == null:
if !is_instance_valid(inventory) || !is_instance_valid(_ctrl_item_container):
return

for item in inventory.get_items():
Expand All @@ -253,7 +249,7 @@ func _populate_list() -> void:
# Position the item centered when it's not streched
ctrl_inventory_item.position += _get_unstreched_sprite_offset(item)

ctrl_item_container.add_child(ctrl_inventory_item)
_ctrl_item_container.add_child(ctrl_inventory_item)

_refresh_selection()

Expand All @@ -262,10 +258,10 @@ func _refresh_selection() -> void:
if !draw_selections:
return

if !_ctrl_item_container.get_ref():
if !is_instance_valid(_ctrl_item_container):
return

for ctrl_item in _ctrl_item_container.get_ref().get_children():
for ctrl_item in _ctrl_item_container.get_children():
ctrl_item.selected = ctrl_item.item && (ctrl_item.item == _selected_item)
ctrl_item.selection_bg_color = selection_color

Expand Down Expand Up @@ -359,7 +355,7 @@ func _on_dragable_dropped(dragable: CtrlDragable, drop_position: Vector2) -> voi
if item == null:
return

if !inventory:
if !is_instance_valid(inventory):
return

if inventory.has_item(item):
Expand Down
12 changes: 6 additions & 6 deletions addons/gloot/ui/ctrl_inventory_grid_ex.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func _refresh() -> void:

func _refresh_selection() -> void:
super._refresh_selection()
if !_selection_panel:
if !is_instance_valid(_selection_panel):
return
_selection_panel.visible = (_selected_item != null) && (selection_style != null)
if _selected_item:
Expand All @@ -59,7 +59,7 @@ func _refresh_selection() -> void:


func _refresh_field_background_grid() -> void:
if _field_background_grid:
if is_instance_valid(_field_background_grid):
remove_child(_field_background_grid)
_field_background_grid.queue_free()
_field_background_grid = null
Expand All @@ -69,7 +69,7 @@ func _refresh_field_background_grid() -> void:


func _create_field_background_grid() -> void:
if !inventory || _field_background_grid != null:
if !is_instance_valid(inventory) || is_instance_valid(_field_background_grid):
return

_field_background_grid = Control.new()
Expand All @@ -89,7 +89,7 @@ func _create_field_background_grid() -> void:


func _create_selection_panel() -> void:
if _selection_panel != null:
if !is_instance_valid(_selection_panel):
return
_selection_panel = Panel.new()
add_child(_selection_panel);
Expand All @@ -116,7 +116,7 @@ func _ready() -> void:


func _on_selection_changed() -> void:
if !inventory:
if !is_instance_valid(inventory):
return
if !field_selected_style:
return
Expand All @@ -135,7 +135,7 @@ func _on_inventory_resized() -> void:
func _input(event) -> void:
if !(event is InputEventMouseMotion):
return
if !inventory:
if !is_instance_valid(inventory):
return

var hovered_field_coords := Vector2i(-1, -1)
Expand Down
16 changes: 8 additions & 8 deletions addons/gloot/ui/ctrl_inventory_item_rect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func _connect_item_signals(new_item: InventoryItem) -> void:


func _disconnect_item_signals() -> void:
if item == null:
if !is_instance_valid(item):
return

if item.protoset_changed.is_connected(_refresh):
Expand All @@ -71,13 +71,13 @@ func _disconnect_item_signals() -> void:


func _get_item_size() -> Vector2:
if item && item.get_inventory():
if is_instance_valid(item) && item.get_inventory():
return item.get_inventory().get_item_size(item)
return Vector2(1, 1)


func _get_item_position() -> Vector2:
if item && item.get_inventory():
if is_instance_valid(item) && item.get_inventory():
return item.get_inventory().get_item_position(item)
return Vector2(0, 0)

Expand Down Expand Up @@ -106,18 +106,18 @@ func _ready() -> void:
_refresh()

func _update_selection() -> void:
if _selection_rect == null:
if !is_instance_valid(_selection_rect):
return
_selection_rect.visible = selected
_selection_rect.color = selection_bg_color
_selection_rect.size = size


func _update_texture() -> void:
if _texture_rect == null:
if !is_instance_valid(_texture_rect):
return
_texture_rect.texture = texture
if item == null:
if !is_instance_valid(item):
return
if GridConstraint.is_item_rotated(item):
_texture_rect.size = Vector2(size.y, size.x)
Expand All @@ -135,9 +135,9 @@ func _update_texture() -> void:


func _update_stack_size() -> void:
if _stack_size_label == null:
if !is_instance_valid(_stack_size_label):
return
if item == null:
if !is_instance_valid(item):
_stack_size_label.text = ""
return
var stack_size: int = StacksConstraint.get_item_stack_size(item)
Expand Down
4 changes: 2 additions & 2 deletions addons/gloot/ui/ctrl_inventory_stacked.gd
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func _disconnect_inventory_signals() -> void:

func _refresh():
super._refresh()
if _label:
if is_instance_valid(_label):
_label.visible = label_visible
_label.text = "%d/%d" % [inventory.occupied_space, inventory.capacity]
if _progress_bar:
if is_instance_valid(_progress_bar):
_progress_bar.visible = progress_bar_visible
_progress_bar.min_value = 0
_progress_bar.max_value = inventory.capacity
Expand Down
Loading

0 comments on commit 3f784f8

Please sign in to comment.