2024-12-04 23:15:05 +08:00
|
|
|
|
using UnityEngine;
|
2024-08-06 18:09:32 +08:00
|
|
|
|
using VirtualNes.Core;
|
|
|
|
|
|
|
|
|
|
namespace AxibugEmuOnline.Client
|
|
|
|
|
{
|
2024-08-06 18:52:11 +08:00
|
|
|
|
public class NesControllerMapper
|
2024-08-06 18:09:32 +08:00
|
|
|
|
{
|
2024-11-26 16:54:51 +08:00
|
|
|
|
public MapperSetter Player1 = new MapperSetter(1);
|
|
|
|
|
public MapperSetter Player2 = new MapperSetter(2);
|
|
|
|
|
public MapperSetter Player3 = new MapperSetter(3);
|
|
|
|
|
public MapperSetter Player4 = new MapperSetter(4);
|
2024-08-06 18:52:11 +08:00
|
|
|
|
|
2024-08-06 18:09:32 +08:00
|
|
|
|
public ControllerState CreateState()
|
|
|
|
|
{
|
|
|
|
|
var state1 = Player1.GetButtons();
|
|
|
|
|
var state2 = Player2.GetButtons();
|
|
|
|
|
var state3 = Player3.GetButtons();
|
|
|
|
|
var state4 = Player4.GetButtons();
|
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
var result = new ControllerState(state1, state2, state3, state4);
|
|
|
|
|
return result;
|
2024-08-06 18:09:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
public class Mapper
|
2024-08-06 18:52:11 +08:00
|
|
|
|
{
|
2024-11-26 16:54:51 +08:00
|
|
|
|
MapperSetter m_setter;
|
|
|
|
|
EnumButtonType m_buttonType;
|
|
|
|
|
IKeyListener m_keyListener;
|
|
|
|
|
int m_controllerIndex => m_setter.ControllerIndex;
|
|
|
|
|
|
|
|
|
|
public Mapper(MapperSetter setter, EnumButtonType buttonType)
|
2024-08-06 18:52:11 +08:00
|
|
|
|
{
|
2024-11-26 16:54:51 +08:00
|
|
|
|
m_setter = setter;
|
|
|
|
|
m_buttonType = buttonType;
|
|
|
|
|
|
|
|
|
|
loadConfig();
|
2024-08-06 18:52:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
private void loadConfig()
|
|
|
|
|
{
|
2024-11-26 17:18:20 +08:00
|
|
|
|
m_keyListener = MapperSetter.GetKey(m_controllerIndex, m_buttonType);
|
2024-08-06 18:52:11 +08:00
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
}
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
public EnumButtonType SampleKey()
|
2024-08-06 18:09:32 +08:00
|
|
|
|
{
|
2024-11-26 16:54:51 +08:00
|
|
|
|
return m_keyListener.IsPressing() ? m_buttonType : 0;
|
2024-08-06 18:09:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MapperSetter
|
|
|
|
|
{
|
2024-11-26 16:54:51 +08:00
|
|
|
|
/// <summary> 控制器序号(手柄1,2,3,4) </summary>
|
|
|
|
|
public int ControllerIndex { get; }
|
|
|
|
|
public Mapper UP { get; private set; }
|
|
|
|
|
public Mapper DOWN { get; private set; }
|
|
|
|
|
public Mapper LEFT { get; private set; }
|
|
|
|
|
public Mapper RIGHT { get; private set; }
|
|
|
|
|
public Mapper A { get; private set; }
|
|
|
|
|
public Mapper B { get; private set; }
|
|
|
|
|
public Mapper SELECT { get; private set; }
|
|
|
|
|
public Mapper START { get; private set; }
|
|
|
|
|
public Mapper MIC { get; private set; }
|
|
|
|
|
|
|
|
|
|
public MapperSetter(int controllerIndex)
|
|
|
|
|
{
|
|
|
|
|
ControllerIndex = controllerIndex;
|
|
|
|
|
UP = new Mapper(this, EnumButtonType.UP);
|
|
|
|
|
DOWN = new Mapper(this, EnumButtonType.DOWN);
|
|
|
|
|
LEFT = new Mapper(this, EnumButtonType.LEFT);
|
|
|
|
|
RIGHT = new Mapper(this, EnumButtonType.RIGHT);
|
|
|
|
|
A = new Mapper(this, EnumButtonType.A);
|
|
|
|
|
B = new Mapper(this, EnumButtonType.B);
|
|
|
|
|
SELECT = new Mapper(this, EnumButtonType.SELECT);
|
|
|
|
|
START = new Mapper(this, EnumButtonType.START);
|
|
|
|
|
MIC = new Mapper(this, EnumButtonType.MIC);
|
|
|
|
|
}
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
|
|
|
|
public EnumButtonType GetButtons()
|
|
|
|
|
{
|
|
|
|
|
EnumButtonType res = 0;
|
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
res |= UP.SampleKey();
|
|
|
|
|
res |= DOWN.SampleKey();
|
|
|
|
|
res |= LEFT.SampleKey();
|
|
|
|
|
res |= RIGHT.SampleKey();
|
|
|
|
|
res |= A.SampleKey();
|
|
|
|
|
res |= B.SampleKey();
|
|
|
|
|
res |= SELECT.SampleKey();
|
|
|
|
|
res |= START.SampleKey();
|
|
|
|
|
res |= MIC.SampleKey();
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 17:18:20 +08:00
|
|
|
|
public static IKeyListener GetKey(int controllerInput, EnumButtonType nesConBtnType)
|
2024-11-26 16:54:51 +08:00
|
|
|
|
{
|
|
|
|
|
string configKey = $"NES_{controllerInput}_{nesConBtnType}";
|
|
|
|
|
if (PlayerPrefs.HasKey(configKey))
|
|
|
|
|
{
|
2024-11-26 17:18:20 +08:00
|
|
|
|
return new KeyListener(PlayerPrefs.GetString(configKey));
|
2024-11-26 16:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var defaultKeyCode = GetDefaultKey();
|
|
|
|
|
PlayerPrefs.SetString(configKey, defaultKeyCode.ToString());
|
|
|
|
|
return defaultKeyCode;
|
|
|
|
|
}
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 17:18:20 +08:00
|
|
|
|
KeyListener GetDefaultKey()
|
2024-11-26 16:54:51 +08:00
|
|
|
|
{
|
|
|
|
|
switch (controllerInput)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
2024-11-26 17:18:20 +08:00
|
|
|
|
if (nesConBtnType == EnumButtonType.LEFT) return new KeyListener(KeyCode.A);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.RIGHT) return new KeyListener(KeyCode.D);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.UP) return new KeyListener(KeyCode.W);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.DOWN) return new KeyListener(KeyCode.S);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.START) return new KeyListener(KeyCode.B);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.SELECT) return new KeyListener(KeyCode.V);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.A) return new KeyListener(KeyCode.K);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.B) return new KeyListener(KeyCode.J);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.MIC) return new KeyListener(KeyCode.M);
|
2024-11-26 16:54:51 +08:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2024-11-26 17:18:20 +08:00
|
|
|
|
if (nesConBtnType == EnumButtonType.LEFT) return new KeyListener(KeyCode.Delete);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.RIGHT) return new KeyListener(KeyCode.PageDown);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.UP) return new KeyListener(KeyCode.Home);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.DOWN) return new KeyListener(KeyCode.End);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.START) return new KeyListener(KeyCode.PageUp);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.SELECT) return new KeyListener(KeyCode.Insert);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.A) return new KeyListener(KeyCode.Keypad5);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.B) return new KeyListener(KeyCode.Keypad4);
|
|
|
|
|
if (nesConBtnType == EnumButtonType.MIC) return new KeyListener(KeyCode.KeypadPeriod);
|
2024-11-26 16:54:51 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return default;
|
|
|
|
|
}
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
public interface IKeyListener
|
|
|
|
|
{
|
|
|
|
|
bool IsPressing();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 17:18:20 +08:00
|
|
|
|
public struct KeyListener : IKeyListener
|
2024-11-26 16:54:51 +08:00
|
|
|
|
{
|
2024-11-26 17:18:20 +08:00
|
|
|
|
private KeyCode m_key;
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 17:18:20 +08:00
|
|
|
|
public KeyListener(KeyCode key)
|
2024-11-26 16:54:51 +08:00
|
|
|
|
{
|
2024-11-26 17:18:20 +08:00
|
|
|
|
m_key = key;
|
2024-11-26 16:54:51 +08:00
|
|
|
|
}
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 16:54:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 从配置表字符串构建
|
|
|
|
|
/// </summary>
|
2024-11-26 17:18:20 +08:00
|
|
|
|
public KeyListener(string confStr)
|
2024-11-26 16:54:51 +08:00
|
|
|
|
{
|
2024-11-26 17:18:20 +08:00
|
|
|
|
m_key = KeyCode.None;
|
2024-08-06 18:09:32 +08:00
|
|
|
|
|
2024-11-26 17:18:20 +08:00
|
|
|
|
if (int.TryParse(confStr, out int result))
|
|
|
|
|
m_key = (KeyCode)result;
|
2024-11-26 16:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 17:18:20 +08:00
|
|
|
|
public readonly bool IsPressing()
|
2024-11-26 16:54:51 +08:00
|
|
|
|
{
|
2024-11-26 17:18:20 +08:00
|
|
|
|
if (Input.GetKey(m_key)) return true;
|
2024-11-26 16:54:51 +08:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2024-11-26 17:18:20 +08:00
|
|
|
|
return ((int)(m_key)).ToString();
|
2024-08-06 18:09:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-26 16:54:51 +08:00
|
|
|
|
|
|
|
|
|
|
2024-08-06 18:09:32 +08:00
|
|
|
|
}
|