重构一下,让KeyMapper支持各种输入设备

This commit is contained in:
ALIENJACK\alien 2024-07-18 15:26:43 +08:00
parent 8b3b54f731
commit 2a20131e00
7 changed files with 140 additions and 62 deletions

View File

@ -7,24 +7,17 @@ namespace AxibugEmuOnline.Client.Input
{ {
public class InputManager : MonoBehaviour public class InputManager : MonoBehaviour
{ {
private KeyMapper m_p1Mapper = new KeyMapper(); private KeyMapper m_p1Mapper = new LocalKeyMapper();
private KeyMapper m_p2Mapper = new KeyMapper(); private KeyMapper m_p2Mapper = new NetKeyMapper();
private KeyMapper m_p3Mapper = new KeyMapper(); private KeyMapper m_p3Mapper = new NetKeyMapper();
private KeyMapper m_p4Mapper = new KeyMapper(); private KeyMapper m_p4Mapper = new NetKeyMapper();
private void Awake() private void Awake()
{ {
m_p1Mapper.SetKeyMapper(KeyCode.W, EnumKeyKind.Up); m_p1Mapper.Init();
m_p1Mapper.SetKeyMapper(KeyCode.S, EnumKeyKind.Down); m_p2Mapper.Init();
m_p1Mapper.SetKeyMapper(KeyCode.A, EnumKeyKind.Left); m_p3Mapper.Init();
m_p1Mapper.SetKeyMapper(KeyCode.D, EnumKeyKind.Right); m_p4Mapper.Init();
m_p1Mapper.SetKeyMapper(KeyCode.V, EnumKeyKind.Select);
m_p1Mapper.SetKeyMapper(KeyCode.B, EnumKeyKind.Start);
m_p1Mapper.SetKeyMapper(KeyCode.J, EnumKeyKind.B);
m_p1Mapper.SetKeyMapper(KeyCode.K, EnumKeyKind.A);
m_p1Mapper.SetKeyMapper(KeyCode.U, EnumKeyKind.TurboB);
m_p1Mapper.SetKeyMapper(KeyCode.I, EnumKeyKind.TurboA);
m_p1Mapper.SetComplete();
} }
private void Update() private void Update()

View File

@ -5,53 +5,11 @@ using UnityEngine;
namespace AxibugEmuOnline.Client.Input namespace AxibugEmuOnline.Client.Input
{ {
public class KeyMapper public abstract class KeyMapper
{ {
private Dictionary<KeyCode, EnumKeyKind> m_mapper = new Dictionary<KeyCode, EnumKeyKind>(); public abstract void Init();
private Dictionary<EnumKeyKind, KeyCode> m_mapperOpp = new Dictionary<EnumKeyKind, KeyCode>(); public abstract void Update();
private Dictionary<EnumKeyKind, int> m_keyIndexTable = new Dictionary<EnumKeyKind, int>(); public abstract bool IsPressing(EnumKeyKind keyKind);
private EnumKeyKind[] m_focusKeys;
private bool[] m_keyStates;
public void SetKeyMapper(KeyCode inputKeycode, EnumKeyKind joyKey)
{
if (m_mapperOpp.TryGetValue(joyKey, out KeyCode keyCode))//如果该映射已设置过,移除之前的映射
{
m_mapperOpp.Remove(joyKey);
m_mapper.Remove(keyCode);
}
m_mapper[inputKeycode] = joyKey;
m_mapperOpp[joyKey] = inputKeycode;
}
public void SetComplete()
{
m_focusKeys = m_mapperOpp.Keys.ToArray();
m_keyStates = new bool[m_focusKeys.Length];
m_keyIndexTable.Clear();
for (int i = 0; i < m_focusKeys.Length; i++)
{
m_keyIndexTable[m_focusKeys[i]] = i;
}
}
public void Update()
{
if (m_focusKeys == null) return;
for (int i = 0; i < m_focusKeys.Length; i++)
{
var keyCode = m_mapperOpp[m_focusKeys[i]];
m_keyStates[i] = UnityEngine.Input.GetKey(keyCode);
}
}
public bool IsPressing(EnumKeyKind keyKind)
{
if (!m_keyIndexTable.TryGetValue(keyKind, out int index)) return false;//没有设置映射,直接false
return m_keyStates[index];
}
} }
} }

View File

@ -0,0 +1,72 @@
using MyNes.Core;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AxibugEmuOnline.Client.Input
{
public class LocalKeyMapper : KeyMapper
{
private Dictionary<KeyCode, EnumKeyKind> m_mapper = new Dictionary<KeyCode, EnumKeyKind>();
private Dictionary<EnumKeyKind, KeyCode> m_mapperOpp = new Dictionary<EnumKeyKind, KeyCode>();
private Dictionary<EnumKeyKind, int> m_keyIndexTable = new Dictionary<EnumKeyKind, int>();
private EnumKeyKind[] m_focusKeys;
private bool[] m_keyStates;
public override void Init()
{
SetKeyMapper(KeyCode.W, EnumKeyKind.Up);
SetKeyMapper(KeyCode.S, EnumKeyKind.Down);
SetKeyMapper(KeyCode.A, EnumKeyKind.Left);
SetKeyMapper(KeyCode.D, EnumKeyKind.Right);
SetKeyMapper(KeyCode.V, EnumKeyKind.Select);
SetKeyMapper(KeyCode.B, EnumKeyKind.Start);
SetKeyMapper(KeyCode.J, EnumKeyKind.B);
SetKeyMapper(KeyCode.K, EnumKeyKind.A);
SetKeyMapper(KeyCode.U, EnumKeyKind.TurboB);
SetKeyMapper(KeyCode.I, EnumKeyKind.TurboA);
SetComplete();
}
void SetKeyMapper(KeyCode inputKeycode, EnumKeyKind joyKey)
{
if (m_mapperOpp.TryGetValue(joyKey, out KeyCode keyCode))//如果该映射已设置过,移除之前的映射
{
m_mapperOpp.Remove(joyKey);
m_mapper.Remove(keyCode);
}
m_mapper[inputKeycode] = joyKey;
m_mapperOpp[joyKey] = inputKeycode;
}
void SetComplete()
{
m_focusKeys = m_mapperOpp.Keys.ToArray();
m_keyStates = new bool[m_focusKeys.Length];
m_keyIndexTable.Clear();
for (int i = 0; i < m_focusKeys.Length; i++)
{
m_keyIndexTable[m_focusKeys[i]] = i;
}
}
public override void Update()
{
if (m_focusKeys == null) return;
for (int i = 0; i < m_focusKeys.Length; i++)
{
var keyCode = m_mapperOpp[m_focusKeys[i]];
m_keyStates[i] = UnityEngine.Input.GetKey(keyCode);
}
}
public override bool IsPressing(EnumKeyKind keyKind)
{
if (!m_keyIndexTable.TryGetValue(keyKind, out int index)) return false;//没有设置映射,直接false
return m_keyStates[index];
}
}
}

View File

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

View File

@ -6,7 +6,7 @@ namespace AxibugEmuOnline.Client
public class NesJoyController : IJoypadConnecter public class NesJoyController : IJoypadConnecter
{ {
private EnumJoyIndex m_joyIndex; private EnumJoyIndex m_joyIndex;
private bool turbo;
public NesJoyController(EnumJoyIndex joyIndex) public NesJoyController(EnumJoyIndex joyIndex)
{ {
@ -14,6 +14,7 @@ namespace AxibugEmuOnline.Client
} }
public override void Update() public override void Update()
{ {
turbo = !turbo;
DATA = 0; DATA = 0;
var state = MyNesMain.Supporter; var state = MyNesMain.Supporter;
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.A)) if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.A))
@ -24,6 +25,14 @@ namespace AxibugEmuOnline.Client
{ {
DATA |= 2; DATA |= 2;
} }
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.TurboA) && turbo)
{
DATA |= 1;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.TurboB) && turbo)
{
DATA |= 2;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Select)) if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Select))
{ {
DATA |= 4; DATA |= 4;

View File

@ -0,0 +1,24 @@
using AxibugEmuOnline.Client.Input;
using MyNes.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class NetKeyMapper : KeyMapper
{
public override void Init()
{
}
public override void Update()
{
}
public override bool IsPressing(EnumKeyKind keyKind)
{
return false;
}
}
}

View File

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