Skip to content

Commit

Permalink
added bind for playing movie
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Jun 29, 2023
1 parent 6bfe21a commit 27dac28
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
18 changes: 17 additions & 1 deletion UniTAS/Patcher/Implementations/GUI/Windows/MoviePlayWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using BepInEx.Logging;
using UniTAS.Patcher.Interfaces.DependencyInjection;
using UniTAS.Patcher.Interfaces.GUI;
using UniTAS.Patcher.Models.Customization;
using UniTAS.Patcher.Models.GUI;
using UniTAS.Patcher.Services.Customization;
using UniTAS.Patcher.Services.Logging;
using UniTAS.Patcher.Services.Movie;
using UniTAS.Patcher.Utils;
Expand All @@ -25,13 +27,19 @@ public class MoviePlayWindow : Window
private readonly IMovieLogger _movieLogger;
private readonly IMovieRunner _movieRunner;

public MoviePlayWindow(WindowDependencies windowDependencies, IMovieLogger movieLogger, IMovieRunner movieRunner) :
private readonly Bind _playMovieBind;

public MoviePlayWindow(WindowDependencies windowDependencies, IMovieLogger movieLogger, IMovieRunner movieRunner,
IBinds binds) :
base(windowDependencies,
new(defaultWindowRect: GUIUtils.WindowRect(600, 200), windowName: "Movie Play"))
{
_movieLogger = movieLogger;
_movieRunner = movieRunner;
movieLogger.OnLog += OnMovieLog;

_playMovieBind = binds.Create(new("PlayMovie", KeyCode.Slash));
windowDependencies.UpdateEvents.OnUpdateUnconditional += UpdateUnconditional;
}

protected override void OnGUI()
Expand All @@ -43,6 +51,14 @@ protected override void OnGUI()
GUILayout.EndVertical();
}

public void UpdateUnconditional()
{
if (_playMovieBind.IsPressed())
{
RunMovieWithLogs();
}
}

private readonly GUILayoutOption[] _moviePathOptions = { GUILayout.ExpandWidth(false) };

private void TASPath()
Expand Down
6 changes: 6 additions & 0 deletions UniTAS/Patcher/Implementations/MonoBehEventInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public event Action OnFixedUpdateActual
remove => MonoBehaviourEvents.OnFixedUpdateActual -= value;
}

public event Action OnUpdateUnconditional
{
add => MonoBehaviourEvents.OnUpdateUnconditional += value;
remove => MonoBehaviourEvents.OnUpdateUnconditional -= value;
}

public event Action OnGUIEventUnconditional
{
add => MonoBehaviourEvents.OnGUIUnconditional += value;
Expand Down
94 changes: 94 additions & 0 deletions UniTAS/Patcher/Models/Customization/Bind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Diagnostics.CodeAnalysis;
using BepInEx;
using BepInEx.Configuration;
using UniTAS.Patcher.Services;
using UnityEngine;

namespace UniTAS.Patcher.Models.Customization;

[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
public class Bind : IEquatable<Bind>
{
private KeyCode Key
{
get
{
var configEntry = _keyConfigEntry?.Value;
if (configEntry != null && _key.ToString() != configEntry && Enum.IsDefined(typeof(KeyCode), configEntry))
{
_key = (KeyCode)Enum.Parse(typeof(KeyCode), configEntry);
}

return _key;
}
}

public string Name { get; }
private ConfigEntry<string> _keyConfigEntry;

private readonly IPatchReverseInvoker _patchReverseInvoker;
private readonly IConfig _config;
private KeyCode _key;

private const string CONFIG_SECTION = "Binds";

public Bind(BindConfig bindConfig, IPatchReverseInvoker patchReverseInvoker, IConfig config)
{
if (bindConfig == null) throw new ArgumentNullException(nameof(bindConfig));
_key = bindConfig.Key;
_patchReverseInvoker = patchReverseInvoker;
_config = config;
Name = bindConfig.Name;
}

private bool _initialized;

public void InitConfig()
{
if (_initialized) return;
_initialized = true;
_keyConfigEntry = _config.ConfigFile.Bind(CONFIG_SECTION, Name, _key.ToString(),
"Key to press to trigger this bind. See https://docs.unity3d.com/ScriptReference/KeyCode.html for a list of keys.");
}

public bool IsPressed()
{
return _patchReverseInvoker.Invoke(key => UnityInput.Current.GetKeyDown(key), Key);
}

public bool Equals(Bind other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Key == other.Key && string.Equals(Name, other.Name, StringComparison.InvariantCulture);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Bind)obj);
}

public override int GetHashCode()
{
unchecked
{
return ((int)Key * 397) ^ StringComparer.InvariantCulture.GetHashCode(Name);
}
}
}

public class BindConfig
{
public string Name { get; }
public KeyCode Key { get; }

public BindConfig(string name, KeyCode key)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Key = key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IUpdateEvents
event Action OnAwakeActual;
event Action OnStartActual;
event Action OnFixedUpdateActual;
event Action OnUpdateUnconditional;
event Action OnGUIEventUnconditional;
event InputSystemEvents.InputUpdateCall OnInputUpdateActual;
}

0 comments on commit 27dac28

Please sign in to comment.