|
| 1 | +using Source.Common.Commands; |
| 2 | + |
| 3 | +namespace Game.Client; |
| 4 | + |
| 5 | +public enum GameAxis |
| 6 | +{ |
| 7 | + None = 0, |
| 8 | + Forward, |
| 9 | + Pitch, |
| 10 | + Side, |
| 11 | + Yaw, |
| 12 | + Max |
| 13 | +} |
| 14 | + |
| 15 | +public enum JoystickAxis |
| 16 | +{ |
| 17 | + X = 0, |
| 18 | + Y, |
| 19 | + Z, |
| 20 | + R, |
| 21 | + U, |
| 22 | + V, |
| 23 | + Max |
| 24 | +} |
| 25 | + |
| 26 | +public struct JoyAxis |
| 27 | +{ |
| 28 | + public uint AxisMap; |
| 29 | + public uint ControlMap; |
| 30 | +} |
| 31 | + |
| 32 | +public partial class Input |
| 33 | +{ |
| 34 | + const uint JOY_ABSOLUTE_AXIS = 0x00000000; |
| 35 | + const uint JOY_RELATIVE_AXIS = 0x00000010; |
| 36 | + |
| 37 | + static readonly ConVar joy_name = new("joy_name", "joystick", FCvar.Archive); |
| 38 | + static readonly ConVar joy_advanced = new("joy_advanced", "1", FCvar.Archive); |
| 39 | + static readonly ConVar joy_advaxisx = new("joy_advaxisx", "4", FCvar.Archive); |
| 40 | + static readonly ConVar joy_advaxisy = new("joy_advaxisy", "2", FCvar.Archive); |
| 41 | + static readonly ConVar joy_advaxisz = new("joy_advaxisz", "0", FCvar.Archive); |
| 42 | + static readonly ConVar joy_advaxisr = new("joy_advaxisr", "1", FCvar.Archive); |
| 43 | + static readonly ConVar joy_advaxisu = new("joy_advaxisu", "3", FCvar.Archive); |
| 44 | + static readonly ConVar joy_advaxisv = new("joy_advaxisv", "0", FCvar.Archive); |
| 45 | + static readonly ConVar joy_movement_stick = new("joy_movement_stick", "0", FCvar.Archive | FCvar.ArchiveXbox, "Which stick controls movement (0 is left stick)", callback: joy_movement_stick_Callback); |
| 46 | + static readonly ConVar joy_xcontroller_cfg_loaded = new("joy_xcontroller_cfg_loaded", "0", FCvar.Archive, "If 0, the 360controller.cfg file will be executed on startup & option changes."); |
| 47 | + |
| 48 | + // todo: the rest of in_joystick.cpp (Init_Joystick, JoyStickMove and their ConVars) |
| 49 | + |
| 50 | + readonly JoyAxis[] Axes = new JoyAxis[(int)JoystickAxis.Max]; |
| 51 | + static ConVarRef? joy_xcontroller_found; |
| 52 | + |
| 53 | + static void joy_movement_stick_Callback(IConVar var, in ConVarChangeContext ctx) { |
| 54 | + engine.ClientCmd("joyadvancedupdate"); |
| 55 | + } |
| 56 | + |
| 57 | + static ReadOnlySpan<char> DescribeAxis(uint index) { |
| 58 | + switch ((GameAxis)index) { |
| 59 | + case GameAxis.Forward: |
| 60 | + return "Forward"; |
| 61 | + case GameAxis.Pitch: |
| 62 | + return "Look"; |
| 63 | + case GameAxis.Side: |
| 64 | + return "Side"; |
| 65 | + case GameAxis.Yaw: |
| 66 | + return "Turn"; |
| 67 | + case GameAxis.None: |
| 68 | + default: |
| 69 | + return "Unknown"; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static void DescribeJoystickAxis(string axis, in JoyAxis mapping) { |
| 74 | + if (mapping.AxisMap == 0) |
| 75 | + Msg("%s: unmapped\n", axis); |
| 76 | + else |
| 77 | + Msg("%s: mapped to %s (%s)\n", axis, DescribeAxis(mapping.AxisMap).ToString(), mapping.ControlMap != 0 ? "relative" : "absolute"); |
| 78 | + } |
| 79 | + |
| 80 | + void SetJoystickAxis(JoystickAxis axis, string name, int value) { |
| 81 | + uint temp = unchecked((uint)value); |
| 82 | + Axes[(int)axis].AxisMap = temp & 0x0000000f; |
| 83 | + Axes[(int)axis].ControlMap = temp & JOY_RELATIVE_AXIS; |
| 84 | + |
| 85 | + DescribeJoystickAxis(name, in Axes[(int)axis]); |
| 86 | + } |
| 87 | + |
| 88 | + public void Joystick_Advanced() { |
| 89 | + for (int i = 0; i < (int)JoystickAxis.Max; i++) { |
| 90 | + Axes[i].AxisMap = (uint)GameAxis.None; |
| 91 | + Axes[i].ControlMap = JOY_ABSOLUTE_AXIS; |
| 92 | + } |
| 93 | + |
| 94 | + if (!joy_advanced.GetBool()) { |
| 95 | + Axes[(int)JoystickAxis.X].AxisMap = (uint)GameAxis.Yaw; |
| 96 | + Axes[(int)JoystickAxis.Y].AxisMap = (uint)GameAxis.Forward; |
| 97 | + } |
| 98 | + else { |
| 99 | + if (stricmp(joy_name.GetString(), "joystick") != 0) |
| 100 | + Msg("Using joystick '%s' configuration\n", joy_name.GetString()); |
| 101 | + |
| 102 | + SetJoystickAxis(JoystickAxis.X, "JOY_AXIS_X", joy_movement_stick.GetBool() ? joy_advaxisu.GetInt() : joy_advaxisx.GetInt()); |
| 103 | + SetJoystickAxis(JoystickAxis.Y, "JOY_AXIS_Y", joy_movement_stick.GetBool() ? joy_advaxisr.GetInt() : joy_advaxisy.GetInt()); |
| 104 | + SetJoystickAxis(JoystickAxis.Z, "JOY_AXIS_Z", joy_advaxisz.GetInt()); |
| 105 | + SetJoystickAxis(JoystickAxis.R, "JOY_AXIS_R", joy_movement_stick.GetBool() ? joy_advaxisy.GetInt() : joy_advaxisr.GetInt()); |
| 106 | + SetJoystickAxis(JoystickAxis.U, "JOY_AXIS_U", joy_movement_stick.GetBool() ? joy_advaxisx.GetInt() : joy_advaxisu.GetInt()); |
| 107 | + SetJoystickAxis(JoystickAxis.V, "JOY_AXIS_V", joy_advaxisv.GetInt()); |
| 108 | + |
| 109 | + Msg("Advanced Joystick settings initialized\n"); |
| 110 | + } |
| 111 | + |
| 112 | + ConVarRef found = joy_xcontroller_found ??= new("joy_xcontroller_found"); |
| 113 | + if (found.IsValid() && found.GetBool() && in_joystick.GetBool()) { |
| 114 | + if (joy_xcontroller_cfg_loaded.GetInt() < 2) { |
| 115 | + engine.ClientCmd_Unrestricted("exec 360controller.cfg"); |
| 116 | +#if LINUX |
| 117 | + engine.ClientCmd_Unrestricted("exec 360controller-linux.cfg"); |
| 118 | +#endif |
| 119 | + joy_xcontroller_cfg_loaded.SetValue(2); |
| 120 | + } |
| 121 | + } |
| 122 | + else if (joy_xcontroller_cfg_loaded.GetInt() > 0) { |
| 123 | + engine.ClientCmd_Unrestricted("exec undo360controller.cfg"); |
| 124 | + joy_xcontroller_cfg_loaded.SetValue(0); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments