Skip to content

Commit

Permalink
Behaviors tab now shows when drag+dropping a new behavior on a component
Browse files Browse the repository at this point in the history
added popup example to MonoGameGumFormsSample
  • Loading branch information
vchelaru committed Dec 31, 2024
1 parent 37b8dbe commit 01c15df
Show file tree
Hide file tree
Showing 8 changed files with 519 additions and 77 deletions.
40 changes: 0 additions & 40 deletions Gum/Controls/MainPanelControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,48 +46,8 @@
</Grid>

<TabControl x:Name="LeftTabControl" Grid.Row="0"></TabControl>

<GridSplitter Width="4" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"></GridSplitter>

<GridSplitter Width="4" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Stretch"></GridSplitter>


<!--<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="{Binding TopPanelHeight}"></RowDefinition>
<RowDefinition Height="{Binding TopSplitterHeight}"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="{Binding BottomSplitterHeight}"></RowDefinition>
<RowDefinition Height="{Binding BottomPanelHeight}"></RowDefinition>
</Grid.RowDefinitions>
--><!--<controls:ToolbarControl x:Name="ToolbarControl"></controls:ToolbarControl>--><!--
<TabControl x:Name="TopTabControl" Grid.Row="1"></TabControl>
<GridSplitter Height="4" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Center" ></GridSplitter>
<Grid Grid.Row="3" x:Name="CenterGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding LeftPanelWidth}" ></ColumnDefinition>
<ColumnDefinition Width="{Binding LeftSplitterWidth}"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TabControl x:Name="LeftTabControl"></TabControl>
<GridSplitter Width="4" HorizontalAlignment="Center" VerticalAlignment="Stretch" Grid.Column="1"></GridSplitter>
<TabControl x:Name="CenterTabControl" Grid.Column="2"></TabControl>
<GridSplitter Width="4" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Stretch"></GridSplitter>
<TabControl x:Name="RightTabControl" Grid.Column="4"></TabControl>
</Grid>
<GridSplitter Height="4" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Row="4"></GridSplitter>
<TabControl Grid.Row="5" x:Name="BottomTabControl"></TabControl>-->
</Grid>
</UserControl>
72 changes: 46 additions & 26 deletions Gum/Plugins/InternalPlugins/Behaviors/MainBehaviorsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -195,7 +215,7 @@ private void UpdateTabPresence()
{
viewModel.UpdateTo(asComponent);

this.behaviorsTab.Show(focus:false);
this.behaviorsTab.Show(focus: false);
}
else
{
Expand Down
24 changes: 14 additions & 10 deletions Gum/ToolCommands/ElementCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,29 @@ public void Initialize()
this.GetFrameworkElementByName<RadioButton>("TouchScreenRadioButton").SetBinding(
nameof(RadioButton.IsChecked),
nameof(viewModel.IsTouchscreenChecked));

this.GetFrameworkElementByName<Button>("DetectResolutionsButton").Click += (not, used) =>
{
ShowPopup();
};
}

private void ShowPopup()
{
var element = ObjectFinder.Self.GetElementSave("Controls/MessageBox");
var popupGue = element.ToGraphicalUiElement(SystemManagers.Default, addToManagers: false);
popupGue.Parent = FrameworkElement.ModalRoot;

popupGue.GetFrameworkElementByName<Button>("OkButton").Click += (not, used) =>
{
popupGue.RemoveFromManagers();
popupGue.Parent = null;
};

popupGue.GetFrameworkElementByName<Button>("CancelButton").Click += (not, used) =>
{
popupGue.RemoveFromManagers();
popupGue.Parent = null;
};
}
}
Loading

0 comments on commit 01c15df

Please sign in to comment.