Skip to content

Commit

Permalink
Fixed behavior assignment.
Browse files Browse the repository at this point in the history
Improved handling of existing children in the input device selector stack.
  • Loading branch information
vchelaru committed Feb 27, 2024
1 parent 336664d commit bfdb354
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using FlatRedBall.Forms.Managers;
using FlatRedBall.Input;
using Gum.Wireframe;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;

namespace FlatRedBall.Forms.Controls.Games
{
public class InputDeviceSelector : FrameworkElement
{
#region Fields/Properties

GraphicalUiElement InputDeviceContainerInstance;

public List<IInputDevice> AllConnectedInputDevices { get; private set; }
Expand All @@ -29,6 +33,8 @@ public int MaxPlayers

List<InputDeviceSelectionItem> InputDeviceSelectionItemsInternal = new List<InputDeviceSelectionItem>();

#endregion

#region Initialize

public InputDeviceSelector() : base()
Expand Down Expand Up @@ -59,6 +65,12 @@ private void DoCommonInitialization()
protected override void ReactToVisualChanged()
{
InputDeviceContainerInstance = this.Visual.GetGraphicalUiElementByName("InputDeviceContainerInstance");
// If using the built-in instantiation from the runtime,
// the Form association is made before variables are assigned.
// This means that InputDeviceContainerInstance.Children may not
// yet be populated, and will be populated after - when the "initial state"
// is set. Therefore, to handle this case there is also an if-check in Activity
// to make sure there isn't a mismatch.
InputDeviceContainerInstance.Children.Clear();

if (!hasAssignedMaxPlayers)
Expand All @@ -71,9 +83,18 @@ protected override void ReactToVisualChanged()
UpdateInputDeviceSelectionItemsCount();
}

FrameworkElementManager.Self.AddFrameworkElement(this);
Visual.RemovedFromGuiManager += HandleRemovedFromGuiManager;


base.ReactToVisualChanged();
}

private void HandleRemovedFromGuiManager(object sender, EventArgs e)
{
FrameworkElementManager.Self.RemoveFrameworkElement(this);
}

#endregion

#region Update in response to changes
Expand Down Expand Up @@ -128,15 +149,32 @@ private void UpdateInputDeviceSelectionItemsCount()
InputDeviceSelectionItemsInternal.Remove(lastItem);
InputDeviceContainerInstance.Children.Remove(lastItem.Visual);
}
// This can happen if an item is directly added to children (such as loaded from .gumx)
while(InputDeviceContainerInstance.Children.Count > InputDeviceSelectionItemsInternal.Count)
{
for(int i = 0; i < InputDeviceContainerInstance.Children.Count; i++)
{
GraphicalUiElement child = (GraphicalUiElement)InputDeviceContainerInstance.Children[i];
if(!InputDeviceSelectionItemsInternal.Any(item => item.Visual == child))
{
InputDeviceContainerInstance.Children.Remove(child);
}
}
}
}


#endregion

List<IInputDevice> devicesUnjoinedThisFrame = new List<IInputDevice>();

public override void Activity()
{
// See ReactToVisualChanged for why this is necessary
if(InputDeviceSelectionItemsInternal.Count != MaxPlayers ||
InputDeviceContainerInstance.Children.Count != MaxPlayers)
{
UpdateInputDeviceSelectionItemsCount();
}
devicesUnjoinedThisFrame.Clear();

bool DidUnjoin(IInputDevice inputDevice) =>
Expand Down Expand Up @@ -196,6 +234,8 @@ private void HandleJoin(IInputDevice inputDevice)
}
}

#region ObservableArray Class

public class ObservableArrayIndexChangeArgs
{
public int Index { get; private set; }
Expand Down Expand Up @@ -234,4 +274,6 @@ void OnIndexChanged(T oldItem, int index)
CollectionChanged?.Invoke(this, new ObservableArrayIndexChangeArgs(index));
}
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class FormsControlInfo
public string BehaviorName;
public string ControlName;

public override string ToString()
{
return $"{BehaviorName} -> {ControlName}";
}

static FormsControlInfo()
{
List<FormsControlInfo> tempList = new List<FormsControlInfo>();
Expand All @@ -29,8 +34,8 @@ static FormsControlInfo()

Add("DialogBoxBehavior", "FlatRedBall.Forms.Controls.Games.DialogBox");

Add("InputDeviceSelector", "FlatRedBall.Forms.Controls.Games.InputDeviceSelector");
Add("InputDeviceSelectionItem", "FlatRedBall.Forms.Controls.Games.InputDeviceSelectionItem");
Add("InputDeviceSelectorBehavior", "FlatRedBall.Forms.Controls.Games.InputDeviceSelector");
Add("InputDeviceSelectionItemBehavior", "FlatRedBall.Forms.Controls.Games.InputDeviceSelectionItem");


Add("LabelBehavior", "Label");
Expand Down

0 comments on commit bfdb354

Please sign in to comment.