From 01c15dfa137d5d32aa77d8275c115371f5b058b9 Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 31 Dec 2024 03:25:07 -0700 Subject: [PATCH] Behaviors tab now shows when drag+dropping a new behavior on a component added popup example to MonoGameGumFormsSample --- Gum/Controls/MainPanelControl.xaml | 40 -- .../Behaviors/MainBehaviorsPlugin.cs | 72 ++-- Gum/ToolCommands/ElementCommands.cs | 24 +- .../GumFormsSampleGame.cs | 2 +- .../Screens/DemoScreenGumRuntime.cs | 24 ++ .../Components/Controls/MessageBox.gucx | 366 ++++++++++++++++++ .../EventExport/gum_events.json | 63 +++ .../Content/FormsGumProject/GumProject.gumx | 5 + 8 files changed, 519 insertions(+), 77 deletions(-) create mode 100644 Samples/GumFormsSample/MonoGameGumFormsSample/Content/FormsGumProject/Components/Controls/MessageBox.gucx diff --git a/Gum/Controls/MainPanelControl.xaml b/Gum/Controls/MainPanelControl.xaml index 4facbd844..3c4ddbd9f 100644 --- a/Gum/Controls/MainPanelControl.xaml +++ b/Gum/Controls/MainPanelControl.xaml @@ -46,48 +46,8 @@ - - - - diff --git a/Gum/Plugins/InternalPlugins/Behaviors/MainBehaviorsPlugin.cs b/Gum/Plugins/InternalPlugins/Behaviors/MainBehaviorsPlugin.cs index e22ed46b6..239498462 100644 --- a/Gum/Plugins/InternalPlugins/Behaviors/MainBehaviorsPlugin.cs +++ b/Gum/Plugins/InternalPlugins/Behaviors/MainBehaviorsPlugin.cs @@ -52,7 +52,9 @@ private void AssignEvents() this.BehaviorSelected += HandleBehaviorSelected; this.StateWindowTreeNodeSelected += HandleStateSelected; this.BehaviorReferencesChanged += HandleBehaviorReferencesChanged; + this.RefreshBehaviorView += HandleRefreshBehaviorView; + this.StateAdd += HandleStateAdd; this.StateMovedToCategory += HandleStateMovedToCategory; } @@ -126,52 +128,70 @@ private void HandleBehaviorSelected(BehaviorSave obj) } + bool isApplyingChanges = false; private void HandleApplyBehaviorChanges(object sender, EventArgs e) { + var component = SelectedState.Self.SelectedComponent; if (component == null) return; - using var undoLock = UndoManager.Self.RequestLock(); + isApplyingChanges = true; - var selectedBehaviorNames = viewModel.AllBehaviors - .Where(item => item.IsChecked) - .Select(item => item.Name) - .ToList(); + try + { + using var undoLock = UndoManager.Self.RequestLock(); - var addedBehaviors = selectedBehaviorNames - .Except(component.Behaviors.Select(item => item.BehaviorName)) - .ToList(); + var selectedBehaviorNames = viewModel.AllBehaviors + .Where(item => item.IsChecked) + .Select(item => item.Name) + .ToList(); - var removedBehaviors = component.Behaviors.Select(item => item.BehaviorName) - .Except(selectedBehaviorNames) - .ToList(); + var addedBehaviors = selectedBehaviorNames + .Except(component.Behaviors.Select(item => item.BehaviorName)) + .ToList(); - if (removedBehaviors.Any()) - { - // ask the user what to do - } + var removedBehaviors = component.Behaviors.Select(item => item.BehaviorName) + .Except(selectedBehaviorNames) + .ToList(); - component.Behaviors.Clear(); - foreach (var behavior in viewModel.AllBehaviors.Where(item => item.IsChecked)) - { - GumCommands.Self.ProjectCommands.ElementCommands.AddBehaviorTo(behavior.Name, component, performSave: false); - } + if (removedBehaviors.Any()) + { + // ask the user what to do + } - GumCommands.Self.GuiCommands.RefreshStateTreeView(); - GumCommands.Self.FileCommands.TryAutoSaveElement(component); + if (removedBehaviors.Any() || addedBehaviors.Any()) + { + component.Behaviors.Clear(); + foreach (var behavior in viewModel.AllBehaviors.Where(item => item.IsChecked)) + { + GumCommands.Self.ProjectCommands.ElementCommands.AddBehaviorTo(behavior.Name, component, performSave: false); + } - viewModel.UpdateTo(component); + GumCommands.Self.GuiCommands.RefreshStateTreeView(); + GumCommands.Self.FileCommands.TryAutoSaveElement(component); + } + viewModel.UpdateTo(component); - if (removedBehaviors.Any() || addedBehaviors.Any()) + } + finally { - PluginManager.Self.BehaviorReferencesChanged(component); + isApplyingChanges = false; } } private void HandleBehaviorReferencesChanged(ElementSave element) { + if (isApplyingChanges) + { + return; + } HandleElementSelected(element); + + if(element == _selectedState.SelectedElement) + { + this.behaviorsTab.Show(focus: true); + } } private void HandleElementSelected(ElementSave element) @@ -195,7 +215,7 @@ private void UpdateTabPresence() { viewModel.UpdateTo(asComponent); - this.behaviorsTab.Show(focus:false); + this.behaviorsTab.Show(focus: false); } else { diff --git a/Gum/ToolCommands/ElementCommands.cs b/Gum/ToolCommands/ElementCommands.cs index a5f8cabdf..0904c1ecd 100644 --- a/Gum/ToolCommands/ElementCommands.cs +++ b/Gum/ToolCommands/ElementCommands.cs @@ -367,24 +367,28 @@ public void AddBehaviorTo(BehaviorSave behavior, ComponentSave componentSave, bo public void AddBehaviorTo(string behaviorName, ComponentSave componentSave, bool performSave = true) { - var behaviorReference = new ElementBehaviorReference(); - behaviorReference.BehaviorName = behaviorName; - componentSave.Behaviors.Add(behaviorReference); - var project = ProjectManager.Self.GumProjectSave; var behaviorSave = project.Behaviors.FirstOrDefault(item => item.Name == behaviorName); - GumCommands.Self.ProjectCommands.ElementCommands.AddCategoriesFromBehavior(behaviorSave, componentSave); + if(behaviorSave != null) + { + var behaviorReference = new ElementBehaviorReference(); + behaviorReference.BehaviorName = behaviorName; + componentSave.Behaviors.Add(behaviorReference); + + GumCommands.Self.ProjectCommands.ElementCommands.AddCategoriesFromBehavior(behaviorSave, componentSave); - GumCommands.Self.GuiCommands.PrintOutput($"Added behavior {behaviorName} to {componentSave}"); + PluginManager.Self.BehaviorReferencesChanged(componentSave); - if (performSave) - { - GumCommands.Self.FileCommands.TryAutoSaveElement(componentSave); + GumCommands.Self.GuiCommands.PrintOutput($"Added behavior {behaviorName} to {componentSave}"); + + if (performSave) + { + GumCommands.Self.FileCommands.TryAutoSaveElement(componentSave); + } } } - void AddCategoriesFromBehavior(BehaviorSave behaviorSave, ElementSave element) { foreach (var behaviorCategory in behaviorSave.Categories) diff --git a/Samples/GumFormsSample/GumFormsSampleCommon/GumFormsSampleGame.cs b/Samples/GumFormsSample/GumFormsSampleCommon/GumFormsSampleGame.cs index f5b252828..9710cf585 100644 --- a/Samples/GumFormsSample/GumFormsSampleCommon/GumFormsSampleGame.cs +++ b/Samples/GumFormsSample/GumFormsSampleCommon/GumFormsSampleGame.cs @@ -43,7 +43,7 @@ protected override void Initialize() { var gumProject = GumService.Default.Initialize(_graphics.GraphicsDevice, "FormsGumProject/GumProject.gumx"); - const int screenNumber = 1; + const int screenNumber = 0; switch (screenNumber) { diff --git a/Samples/GumFormsSample/GumFormsSampleCommon/Screens/DemoScreenGumRuntime.cs b/Samples/GumFormsSample/GumFormsSampleCommon/Screens/DemoScreenGumRuntime.cs index 26f0fc3aa..2cf28e48b 100644 --- a/Samples/GumFormsSample/GumFormsSampleCommon/Screens/DemoScreenGumRuntime.cs +++ b/Samples/GumFormsSample/GumFormsSampleCommon/Screens/DemoScreenGumRuntime.cs @@ -56,5 +56,29 @@ public void Initialize() this.GetFrameworkElementByName("TouchScreenRadioButton").SetBinding( nameof(RadioButton.IsChecked), nameof(viewModel.IsTouchscreenChecked)); + + this.GetFrameworkElementByName