1
0
Fork 0

first successful attempt at decoupled input module

This commit is contained in:
lightling 2022-07-11 17:45:46 -04:00
parent 6e9f383d27
commit 2bae0582f9
Signed by: lightling
GPG key ID: 016F11E0AA296B67
10 changed files with 308 additions and 0 deletions

View file

@ -0,0 +1,13 @@
using System;
using UnityEngine;
namespace Goldenwere.GWSU.CTRL.CHAR.Core
{
public abstract class BaseInputModule : MonoBehaviour
{
public abstract void RegisterModuleAction(BaseModuleAction _action);
public abstract void RegisterModuleAction<T>(BaseModuleAction<T> _action);
public abstract void UnregisterModuleAction(BaseModuleAction _action);
public abstract void UnregisterModuleAction<T>(BaseModuleAction<T> _action);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2563e6c15d3cc124bb77cf676545f879
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,64 @@
using System;
using UnityEngine;
namespace Goldenwere.GWSU.CTRL.CHAR.Core
{
public enum ModuleActionType : uint
{
Held = 0,
Pressed = 1,
HeldOrPressed = 2,
}
public class BaseModuleAction
{
[SerializeField] private ScriptableObject emitter;
[SerializeField] private Action listener;
[SerializeField] private ModuleActionType type;
private Guid guid;
public Guid GUID => guid;
public Action Listener => listener;
public ScriptableObject Emitter => emitter;
public ModuleActionType Type => type;
public BaseModuleAction(
Action _listener,
ScriptableObject _emitter,
ModuleActionType _type
)
{
guid = Guid.NewGuid();
listener = _listener;
emitter = _emitter;
type = _type;
}
}
public class BaseModuleAction<T>
{
[SerializeField] private ScriptableObject emitter;
[SerializeField] private Action<T> listener;
[SerializeField] private ModuleActionType type;
private Guid guid;
public Guid GUID => guid;
public Action<T> Listener => listener;
public ScriptableObject Emitter => emitter;
public ModuleActionType Type => type;
public BaseModuleAction(
Action<T> _listener,
ScriptableObject _emitter,
ModuleActionType _type
)
{
guid = Guid.NewGuid();
listener = _listener;
emitter = _emitter;
type = _type;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 670fd3287f776fd4abd946ae86e91e06
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,17 @@
{
"name": "Goldenwere.GWSU.CTRL.CHAR.InputSystemModule",
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:7118095471e21fa4782c349e8d42f7a3"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4d883c6428850a645aecf33a1ee4c832
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using Goldenwere.GWSU.CTRL.CHAR.Core;
namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule
{
public class InputModule : BaseInputModule
{
internal struct Callbacks
{
public Action<InputAction.CallbackContext> canceled;
public Action<InputAction.CallbackContext> performed;
public Action<InputAction.CallbackContext> started;
}
private Dictionary<Guid, Callbacks> registeredCallbacks;
private Dictionary<Guid, Callbacks> RegisteredCallbacks
{
get
{
if (registeredCallbacks == null)
{
registeredCallbacks = new Dictionary<Guid, Callbacks>();
}
return registeredCallbacks;
}
}
public override void RegisterModuleAction(BaseModuleAction _action)
{
if (TryGetReference(_action.Emitter, out InputActionReference _reference))
{
Action<InputAction.CallbackContext> performed = (InputAction.CallbackContext ctx) =>
{
_action.Listener.Invoke();
};
_reference.action.performed += performed;
RegisteredCallbacks.Add
(
_action.GUID,
new Callbacks
{
performed = performed,
}
);
}
}
public override void RegisterModuleAction<T>(BaseModuleAction<T> _action)
{
if (TryGetReference(_action.Emitter, out InputActionReference _reference))
{
}
}
public override void UnregisterModuleAction(BaseModuleAction _action)
{
if (TryGetReference(_action.Emitter, out InputActionReference _reference))
{
Unregister(_reference, _action.GUID);
}
}
public override void UnregisterModuleAction<T>(BaseModuleAction<T> _action)
{
if (TryGetReference(_action.Emitter, out InputActionReference _reference))
{
Unregister(_reference, _action.GUID);
}
}
private bool CheckForCallback(Guid id)
{
if (RegisteredCallbacks.ContainsKey(id))
{
return true;
}
Debug.LogError("Attempted to unregister a module action that was not yet registered!");
return false;
}
private bool TryGetReference(ScriptableObject _emitter, out InputActionReference _reference)
{
if (_emitter is InputActionReference reference)
{
_reference = reference;
return true;
}
else
{
Debug.LogError($"Emitter was not an InputActionReference!");
}
_reference = null;
return false;
}
private void Unregister(InputActionReference _reference, Guid _id)
{
if (CheckForCallback(_id))
{
_reference.action.performed -= RegisteredCallbacks[_id].performed;
RegisteredCallbacks.Remove(_id);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2ae55851646730b41a9efe1ad3aa3b5b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,53 @@
using UnityEngine;
using Goldenwere.GWSU.CTRL.CHAR.Core;
namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.SampleProject
{
public class FakeControllerModule : BaseControllerModule
{
[SerializeField] private BaseInputModule module;
[SerializeField] private ScriptableObject emitterOnCrouch;
[SerializeField] private ScriptableObject emitterOnJump;
[SerializeField] private ScriptableObject emitterOnMovement;
private BaseModuleAction<bool> actionOnCrouch;
private BaseModuleAction actionOnJump;
private BaseModuleAction<Vector2> actionOnMovement;
private void Awake()
{
actionOnCrouch = new BaseModuleAction<bool>(OnCrouch, emitterOnCrouch, ModuleActionType.HeldOrPressed);
actionOnJump = new BaseModuleAction(OnJump, emitterOnJump, ModuleActionType.Pressed);
actionOnMovement = new BaseModuleAction<Vector2>(OnMovement, emitterOnJump, ModuleActionType.Held);
}
private void OnEnable()
{
//module.RegisterModuleAction(actionOnCrouch);
module.RegisterModuleAction(actionOnJump);
//module.RegisterModuleAction(actionOnMovement);
}
private void OnDisable()
{
//module.UnregisterModuleAction(actionOnCrouch);
module.UnregisterModuleAction(actionOnJump);
//module.UnregisterModuleAction(actionOnMovement);
}
private void OnCrouch(bool pressed)
{
Debug.Log($"Crouch was {(pressed ? "pressed" : "released")}");
}
private void OnJump()
{
Debug.Log("Jumped");
}
private void OnMovement(Vector2 value)
{
Debug.Log($"Value was: {value}");
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 838b4ff14e50cc2449fd122b9daf073b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: