123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using BepInEx.Configuration;
- using UnityEngine;
- namespace MeidoPhotoStudio.Plugin;
- public enum MpsKey
- {
- // MeidoPhotoStudio
- Activate,
- Screenshot,
- ToggleUI,
- ToggleMessage,
- // MeidoManager
- MeidoUndressing,
- // Camera
- CameraLayer,
- CameraReset,
- CameraSave,
- CameraLoad,
- // Dragpoint
- DragSelect,
- DragDelete,
- DragMove,
- DragRotate,
- DragScale,
- DragFinger,
- // Scene management
- SaveScene,
- LoadScene,
- OpenSceneManager,
- }
- public static class InputManager
- {
- public const KeyCode UpperKeyCode = KeyCode.F15;
- public const string ConfigHeader = "Controls";
- public static readonly AcceptableValueBase ControlRange;
- private static readonly Dictionary<MpsKey, KeyCode> ActionKeys = new();
- private static readonly Dictionary<MpsKey, ConfigEntry<KeyCode>> ConfigEntries = new();
- private static InputListener inputListener;
- static InputManager() =>
- ControlRange = new AcceptableValueRange<KeyCode>(default, UpperKeyCode);
- public static event EventHandler KeyChange;
- public static KeyCode CurrentKeyCode { get; private set; }
- public static bool Listening { get; private set; }
- public static bool Control =>
- Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
- public static bool Alt =>
- Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
- public static bool Shift =>
- Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
- public static void Register(MpsKey action, KeyCode key, string description)
- {
- key = Clamp(key, default, UpperKeyCode);
- if (ConfigEntries.ContainsKey(action))
- {
- Rebind(action, key);
- }
- else
- {
- var configDescription = new ConfigDescription(description, ControlRange);
- ConfigEntries[action] =
- Configuration.Config.Bind(ConfigHeader, action.ToString(), key, configDescription);
- key = ConfigEntries[action].Value;
- ActionKeys[action] = key;
- }
- }
- public static void Rebind(MpsKey action, KeyCode key)
- {
- key = Clamp(key, default, UpperKeyCode);
- if (ConfigEntries.ContainsKey(action))
- ConfigEntries[action].Value = key;
- ActionKeys[action] = key;
- }
- public static KeyCode Clamp(KeyCode value, KeyCode min, KeyCode max) =>
- value < min ? min : value > max ? max : value;
- public static KeyCode GetActionKey(MpsKey action) =>
- ActionKeys.TryGetValue(action, out var keyCode) ? keyCode : default;
- public static void StartListening()
- {
- if (!inputListener)
- inputListener = new GameObject().AddComponent<InputListener>();
- else if (inputListener.gameObject.activeSelf)
- StopListening();
- inputListener.gameObject.SetActive(true);
- inputListener.KeyChange += OnKeyChange;
- CurrentKeyCode = KeyCode.None;
- Listening = true;
- }
- public static void StopListening()
- {
- if (!inputListener || !inputListener.gameObject.activeSelf)
- return;
- inputListener.gameObject.SetActive(false);
- inputListener.KeyChange -= OnKeyChange;
- CurrentKeyCode = KeyCode.None;
- Listening = false;
- Input.ResetInputAxes();
- }
- public static bool GetKey(MpsKey action) =>
- !Listening && ActionKeys.ContainsKey(action) && Input.GetKey(ActionKeys[action]);
- public static bool GetKeyDown(MpsKey action) =>
- !Listening && ActionKeys.ContainsKey(action) && Input.GetKeyDown(ActionKeys[action]);
- public static void Deactivate()
- {
- StopListening();
- // TODO: Null propagation does not work with UnityEngine.Object
- UnityEngine.Object.Destroy(inputListener?.gameObject);
- inputListener = null;
- }
- private static void OnKeyChange(object sender, KeyChangeEventArgs args)
- {
- CurrentKeyCode = args.Key;
- KeyChange?.Invoke(null, EventArgs.Empty);
- StopListening();
- }
- /* Listener taken from https://forum.unity.com/threads/find-out-which-key-was-pressed.385250/ */
- private class InputListener : MonoBehaviour
- {
- private static readonly KeyCode[] KeyCodes;
- static InputListener() =>
- KeyCodes = Enum.GetValues(typeof(KeyCode))
- .Cast<KeyCode>()
- .Where(keyCode => keyCode <= UpperKeyCode)
- .ToArray();
- public event EventHandler<KeyChangeEventArgs> KeyChange;
- private void Awake() =>
- DontDestroyOnLoad(this);
- private void Update()
- {
- if (!Input.anyKeyDown)
- return;
- foreach (var key in KeyCodes)
- {
- if (!Input.GetKeyDown(key))
- continue;
- KeyChange?.Invoke(this, new(key));
- break;
- }
- }
- }
- private class KeyChangeEventArgs : EventArgs
- {
- public KeyChangeEventArgs(KeyCode key) =>
- Key = key;
- public KeyCode Key { get; }
- }
- }
|