From 70f027cc3fb7310403f388b8927cd2ea41f50177 Mon Sep 17 00:00:00 2001
From: EliteMasterEric <ericmyllyoja@gmail.com>
Date: Thu, 6 Jun 2024 16:44:47 -0400
Subject: [PATCH] Fix another crash with the chart editor event toolbox

---
 .../debug/charting/commands/SetItemSelectionCommand.hx | 10 +++++++++-
 .../charting/toolboxes/ChartEditorEventDataToolbox.hx  |  6 ++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/source/funkin/ui/debug/charting/commands/SetItemSelectionCommand.hx b/source/funkin/ui/debug/charting/commands/SetItemSelectionCommand.hx
index 73cf80fa0..661c44d85 100644
--- a/source/funkin/ui/debug/charting/commands/SetItemSelectionCommand.hx
+++ b/source/funkin/ui/debug/charting/commands/SetItemSelectionCommand.hx
@@ -35,7 +35,15 @@ class SetItemSelectionCommand implements ChartEditorCommand
     {
       var eventSelected = this.events[0];
 
-      state.eventKindToPlace = eventSelected.eventKind;
+      if (state.eventKindToPlace == eventSelected.eventKind)
+      {
+        trace('Target event kind matches selection: ${eventSelected.eventKind}');
+      }
+      else
+      {
+        trace('Switching target event kind to match selection: ${state.eventKindToPlace} != ${eventSelected.eventKind}');
+        state.eventKindToPlace = eventSelected.eventKind;
+      }
 
       // This code is here to parse event data that's not built as a struct for some reason.
       // TODO: Clean this up or get rid of it.
diff --git a/source/funkin/ui/debug/charting/toolboxes/ChartEditorEventDataToolbox.hx b/source/funkin/ui/debug/charting/toolboxes/ChartEditorEventDataToolbox.hx
index 4e4d5a077..8f021840a 100644
--- a/source/funkin/ui/debug/charting/toolboxes/ChartEditorEventDataToolbox.hx
+++ b/source/funkin/ui/debug/charting/toolboxes/ChartEditorEventDataToolbox.hx
@@ -90,6 +90,7 @@ class ChartEditorEventDataToolbox extends ChartEditorBaseToolbox
       }
     }
     var startingEventValue = ChartEditorDropdowns.populateDropdownWithSongEvents(toolboxEventsEventKind, chartEditorState.eventKindToPlace);
+    trace('ChartEditorToolboxHandler.buildToolboxEventDataLayout() - Starting event kind: ${startingEventValue}');
     toolboxEventsEventKind.value = startingEventValue;
   }
 
@@ -103,7 +104,7 @@ class ChartEditorEventDataToolbox extends ChartEditorBaseToolbox
     {
       throw 'ChartEditorToolboxHandler.buildToolboxEventDataLayout() - Event kind not in dropdown: ${chartEditorState.eventKindToPlace}';
     }
-    else if (toolboxEventsEventKind.value != newDropdownElement)
+    else if (toolboxEventsEventKind.value != newDropdownElement || lastEventKind != toolboxEventsEventKind.value.id)
     {
       toolboxEventsEventKind.value = newDropdownElement;
 
@@ -114,12 +115,13 @@ class ChartEditorEventDataToolbox extends ChartEditorBaseToolbox
       }
       else
       {
+        trace('ChartEditorToolboxHandler.buildToolboxEventDataLayout() - Event kind changed: ${toolboxEventsEventKind.value.id} != ${newDropdownElement.id} != ${lastEventKind}, rebuilding form');
         buildEventDataFormFromSchema(toolboxEventsDataGrid, schema, chartEditorState.eventKindToPlace);
       }
     }
     else
     {
-      trace('ChartEditorToolboxHandler.buildToolboxEventDataLayout() - Event kind not changed: ${toolboxEventsEventKind.value} == ${newDropdownElement}');
+      trace('ChartEditorToolboxHandler.buildToolboxEventDataLayout() - Event kind not changed: ${toolboxEventsEventKind.value} == ${newDropdownElement} == ${lastEventKind}');
     }
 
     for (pair in chartEditorState.eventDataToPlace.keyValueIterator())