Skip to content

Commit

Permalink
Now ModPopupInputs properly react to buttons (#177)
Browse files Browse the repository at this point in the history
* Now ModPopupInputs properly react to buttons
  • Loading branch information
TAImatem authored Jul 12, 2020
1 parent f6485df commit e594e15
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion OWML.ModHelper.Menus/ModInputCombinationElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ private void SetupButtons()
{
var commandObject = new GameObject();
var commandComponent = commandObject.AddComponent<ModCommandListener>();
commandComponent.AddToListener(InputLibrary.interact);
commandComponent.AddToListener(InputLibrary.select);
commandComponent.AddToListener(InputLibrary.enter);
commandComponent.AddToListener(InputLibrary.enter2);
commandComponent.OnNewlyReleased += OnEditButton;
commandComponent.BlockNextRelease();
YesButton.Title = "Edit";
YesButton.OnClick += OnEditClick;

Expand All @@ -78,6 +81,7 @@ private void SetupButtons()
commandComponent = commandObject.AddComponent<ModCommandListener>();
commandComponent.AddToListener(deleteCommand);
commandComponent.OnNewlyReleased += OnDeleteButton;
commandComponent.BlockNextRelease();
NoButton.Title = "Delete";
NoButton.OnClick += OnDeleteClick;
}
Expand Down
2 changes: 1 addition & 1 deletion OWML.ModHelper.Menus/ModMenuWithSelectables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ModMenuWithSelectables : ModPopupMenu, IModMenuWithSelectables

protected ModMenuWithSelectables(IModConsole console) : base(console) { }

protected virtual void SetupCommands()
private void SetupCommands()
{
var listenerObject = new GameObject("CombinationMenu_Listener");
CommandListener = listenerObject.AddComponent<ModCommandListener>();
Expand Down
25 changes: 25 additions & 0 deletions OWML.ModHelper.Menus/ModPopupInput.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using OWML.Common.Menus;
using OWML.ModHelper.Events;
using OWML.ModHelper.Input;
using UnityEngine;
using UnityEngine.UI;

namespace OWML.ModHelper.Menus
{
public abstract class ModPopupInput<T> : ModInput<T>
{
protected readonly TwoButtonToggleElement ToggleElement;
protected ModCommandListener CommandListener;

public override bool IsSelected => ToggleElement.GetValue<bool>("_amISelected");

Expand All @@ -23,13 +26,35 @@ protected ModPopupInput(TwoButtonToggleElement toggle, IModMenu menu) : base(tog
layoutGroup.childForceExpandWidth = true;

buttonParent.GetComponent<LayoutElement>().preferredWidth = 100;

SetupCommands();
}

private void SetupCommands()
{
var listenerObject = new GameObject();
CommandListener = listenerObject.AddComponent<ModCommandListener>();
CommandListener.AddToListener(InputLibrary.select);
CommandListener.AddToListener(InputLibrary.enter);
CommandListener.AddToListener(InputLibrary.enter2);
CommandListener.OnNewlyPressed += OnOpenCommand;
}

protected void Subscribe(IModButton button)
{
button.OnClick += Open;
}

protected virtual void OnOpenCommand(SingleAxisCommand command)
{
if (IsSelected && (command == InputLibrary.select || command == InputLibrary.enter || command == InputLibrary.enter2))
{
command.ConsumeInput();
command.BlockNextRelease();
Open();
}
}

protected abstract void Open();

}
Expand Down

0 comments on commit e594e15

Please sign in to comment.