Skip to content

Commit

Permalink
events for menus (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingalek authored Jan 6, 2020
1 parent 92c50a4 commit d73bf52
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 16 deletions.
2 changes: 1 addition & 1 deletion OWML.Common/IModMenus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public interface IModMenus
{
IModMenu MainMenu { get; }
IModMenu PauseMenu { get; }
IModPopupMenu PauseMenu { get; }
}
}
16 changes: 16 additions & 0 deletions OWML.Common/IModPopupMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace OWML.Common
{
public interface IModPopupMenu : IModMenu
{
Action OnOpen { get; set; }
Action OnClose { get; set; }

bool IsOpen { get; }

void Open();
void Close();
void Toggle();
}
}
1 change: 1 addition & 0 deletions OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="IModAssets.cs" />
<Compile Include="IModAsset.cs" />
<Compile Include="IModConfig.cs" />
<Compile Include="IModPopupMenu.cs" />
<Compile Include="IOwmlConfig.cs" />
<Compile Include="IModEvents.cs" />
<Compile Include="IModFinder.cs" />
Expand Down
2 changes: 1 addition & 1 deletion OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OWML.Launcher
{
public class App
{
private const string Version = "0.3.14";
private const string Version = "0.3.15";

private readonly IOwmlConfig _config;
private readonly IModConsole _writer;
Expand Down
2 changes: 1 addition & 1 deletion OWML.ModHelper.Events/HarmonyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private HarmonyInstance CreateInstance()
try
{
_logger.Log("Creating harmony instance");
harmony = HarmonyInstance.Create("com.owml" + _manifest.UniqueName);
harmony = HarmonyInstance.Create("com.owml." + _manifest.UniqueName);
_logger.Log("Created harmony instance");
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion OWML.ModHelper.Menus/ModMenus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OWML.ModHelper.Menus
public class ModMenus : IModMenus
{
public IModMenu MainMenu { get; }
public IModMenu PauseMenu { get; }
public IModPopupMenu PauseMenu { get; }

public ModMenus(IModLogger logger, IModConsole console)
{
Expand Down
70 changes: 60 additions & 10 deletions OWML.ModHelper.Menus/ModPauseMenu.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using OWML.Common;
using OWML.ModHelper.Events;
Expand All @@ -7,13 +8,17 @@

namespace OWML.ModHelper.Menus
{
public class ModPauseMenu : IModMenu
public class ModPauseMenu : IModPopupMenu
{
public Action OnOpen { get; set; }
public Action OnClose { get; set; }
public bool IsOpen { get; private set; }

private readonly IModLogger _logger;
private readonly IModConsole _console;
private PauseMenuManager _pauseMenuManager;
private Menu _menu;
private LayoutGroup _layoutGroup;
private bool _isInitialized;

public ModPauseMenu(IModLogger logger, IModConsole console)
{
Expand All @@ -23,7 +28,7 @@ public ModPauseMenu(IModLogger logger, IModConsole console)

public List<Button> GetButtons()
{
if (!_isInitialized)
if (_menu == null)
{
_console.WriteLine("Warning: can't get pause buttons before player wakes up");
return new List<Button>();
Expand All @@ -35,12 +40,12 @@ public Button AddButton(string name, int index)
{
_console.WriteLine("Adding pause button: " + name);

if (!_isInitialized)
if (_menu == null)
{
Initialize();
}

if (!_isInitialized)
if (_menu == null)
{
_console.WriteLine("Warning: can't add pause buttons before player wakes up.");
return null;
Expand All @@ -64,15 +69,60 @@ public Button AddButton(string name, int index)
private void Initialize()
{
_logger.Log("Trying to initialize pause menu");
var pauseMenuManager = GameObject.FindObjectOfType<PauseMenuManager>();
if (pauseMenuManager == null)
_pauseMenuManager = GameObject.FindObjectOfType<PauseMenuManager>();
if (_pauseMenuManager == null)
{
_console.WriteLine("Warning: can't initialize pause menu before player wakes up");
return;
}
_menu = pauseMenuManager.GetValue<Menu>("_pauseMenu");
_menu = _pauseMenuManager.GetValue<Menu>("_pauseMenu");
_layoutGroup = _menu.GetComponentInChildren<LayoutGroup>();
_isInitialized = true;
_menu.OnActivateMenu += OnActivateMenu;
_menu.OnDeactivateMenu += OnDeactivateMenu;
}

private void OnDeactivateMenu()
{
IsOpen = false;
OnClose?.Invoke();
}

private void OnActivateMenu()
{
IsOpen = true;
OnOpen?.Invoke();
}

public void Open()
{
if (_menu == null)
{
Console.WriteLine("Warning: can't open menu, it doesn't exist.");
return;
}
_pauseMenuManager.TryOpenPauseMenu();
}

public void Close()
{
if (_menu == null)
{
Console.WriteLine("Warning: can't close menu, it doesn't exist.");
return;
}
_menu.Deactivate();
}

public void Toggle()
{
if (IsOpen)
{
Close();
}
else
{
Open();
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion OWML.SampleMods/OWML.EnableDebugMode/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"name": "EnableDebugMode",
"uniqueName": "Alek.EnableDebugMode",
"version": "0.2",
"owmlVersion": "0.3.14",
"owmlVersion": "0.3.15",
"enabled": false
}
2 changes: 1 addition & 1 deletion OWML.SampleMods/OWML.LoadCustomAssets/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"name": "LoadCustomAssets",
"uniqueName": "Alek.LoadCustomAssets",
"version": "0.4",
"owmlVersion": "0.3.14",
"owmlVersion": "0.3.15",
"enabled": false
}

0 comments on commit d73bf52

Please sign in to comment.