From ad9d8cfdbcea96341541cbb57675363ccb8cc242 Mon Sep 17 00:00:00 2001
From: Manolis Papadeas <35376950+OverloadedOrama@users.noreply.github.com>
Date: Thu, 3 Feb 2022 19:57:53 +0200
Subject: [PATCH] Properly fix the palette panel initialization issue

And remove the hacky way. The issue is that the swatches were being created and then queue_freed at the end of the frame and not immediately.
---
 src/Main.gd                   | 6 ------
 src/Palette/PaletteGrid.gd    | 1 +
 src/Palette/PalettePanel.tscn | 6 +++---
 3 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/src/Main.gd b/src/Main.gd
index d666c2a1c..f3e23ca77 100644
--- a/src/Main.gd
+++ b/src/Main.gd
@@ -72,12 +72,6 @@ func _ready() -> void:
 
 	_show_splash_screen()
 
-	# Hacky way to update the panel's size... Unsure why this happens.
-	yield(get_tree(), "idle_frame")
-	ui.set_control_hidden(Global.palette_panel, true)
-	yield(get_tree(), "idle_frame")
-	ui.set_control_hidden(Global.palette_panel, false)
-
 
 func _input(event: InputEvent) -> void:
 	Global.left_cursor.position = get_global_mouse_position() + Vector2(-32, 32)
diff --git a/src/Palette/PaletteGrid.gd b/src/Palette/PaletteGrid.gd
index 70338d0b2..be81338b6 100644
--- a/src/Palette/PaletteGrid.gd
+++ b/src/Palette/PaletteGrid.gd
@@ -94,6 +94,7 @@ func scroll_palette(origin: Vector2) -> void:
 func clear_swatches() -> void:
 	swatches.clear()
 	for swatch in get_children():
+		remove_child(swatch)  # To remove the child immediately and not at the end of the frame
 		swatch.queue_free()
 
 
diff --git a/src/Palette/PalettePanel.tscn b/src/Palette/PalettePanel.tscn
index 16adc90f9..558cfa569 100644
--- a/src/Palette/PalettePanel.tscn
+++ b/src/Palette/PalettePanel.tscn
@@ -9,8 +9,8 @@
 [ext_resource path="res://src/Palette/PalettePanel.gd" type="Script" id=8]
 
 [node name="PalettePanel" type="PanelContainer"]
-margin_right = 300.0
-margin_bottom = 319.0
+margin_right = 318.0
+margin_bottom = 118.0
 rect_min_size = Vector2( 318, 0 )
 size_flags_horizontal = 3
 size_flags_vertical = 0
@@ -23,7 +23,7 @@ __meta__ = {
 margin_left = 7.0
 margin_top = 7.0
 margin_right = 311.0
-margin_bottom = 312.0
+margin_bottom = 111.0
 size_flags_horizontal = 3
 
 [node name="PalettesLabel" type="Label" parent="PaletteVBoxContainer"]