public VRopeMain() { try { ConfigFilePath = (Directory.GetCurrentDirectory() + "\\scripts\\VRope.ini"); ProcessConfigFile(); SortKeyTuples(); if (EnableXBoxControllerInput) { XBoxController.CheckForController(); SortButtonTuples(); } Tick += OnTick; KeyDown += OnKeyDown; //KeyUp += OnKeyUp; Interval = UPDATE_INTERVAL; } catch (Exception exc) { UI.Notify("VRope Init Error:\n" + GetErrorMessage(exc)); } }
public void OnTick(object sender, EventArgs e) { try { //long firstTime = Watch.ElapsedMilliseconds; GlobalSubtitle = ""; DebugInfo = ""; if (!ModActive) { Script.Wait(1); return; } CheckCurrentModState(); //---------------------------------------------------------------------------------- if (!ModRunning) { return; } if (DebugMode) { UpdateDebugStuff(); } if (XBoxController.IsControllerConnected()) { ProcessXBoxControllerInput(); } CheckForKeysHeldDown(); CheckForKeysReleased(); ProcessHooks(); if (TestAction) { ThisWillRunEveryFrame(); } //long elapsedTime = Watch.ElapsedMilliseconds - firstTime; //DebugInfo += "\n Loop Time(" + elapsedTime + " ms) "; ShowScreenInfo(); } catch (Exception exc) { UI.Notify("VRope Runtime Error:\n" + GetErrorMessage(exc) + "\nMod execution halted."); DeleteAllHooks(); ModRunning = false; ModActive = false; } }
public static void RegisterControlButton(String name, String buttonData, Action callback, TriggerCondition condition) { ControllerState buttonState = XBoxController.TranslateButtonStringToButtonData(buttonData); if (buttonState.buttonPressedCount == -1) { UI.Notify("VRope ControlButton Error:\n Button combination for \"" + name + "\" is invalid. \nThe control was disabled."); return; } ControlButtons.Add(new ControlButton(name, buttonState, callback, condition)); }
public static void CheckCurrentModState() { ModRunning = (Game.Player.Exists() && !FirstTime && Game.Player.IsAlive && Game.Player.CanControlCharacter); if (FirstTime && Game.IsScreenFadedIn) { Script.Wait(500); UI.Notify(MOD_NAME + " " + GetModVersion() + "\nby " + MOD_DEVELOPER, true); if (XBoxController.IsControllerConnected()) { UI.Notify("XBox controller detected.", false); } FirstTime = false; } }
public static void ProcessXBoxControllerInput() { XBoxController.UpdateStateBegin(); for (int i = 0; i < ControlButtons.Count; i++) { var controlButton = ControlButtons[i]; ControllerState button = controlButton.state; TriggerCondition condition = controlButton.condition; if ((condition == TriggerCondition.PRESSED && XBoxController.WasControllerButtonPressed(button)) || (condition == TriggerCondition.RELEASED && XBoxController.WasControllerButtonReleased(button)) || (condition == TriggerCondition.HELD && XBoxController.IsControllerButtonPressed(button)) || (condition == TriggerCondition.ANY)) { controlButton.callback.Invoke(); break; } } XBoxController.UpdateStateEnd(); }
public static ControllerState TranslateButtonStringToButtonData(String buttonData) { if (buttonData == null || buttonData.Length == 0) { return(new ControllerState()); } buttonData = buttonData.Replace(" ", ""); Gamepad resultData = new Gamepad(); bool hasInvalidKey = false; String[] buttonStrings = buttonData.Split(SEPARATOR_CHAR); for (int i = 0; i < buttonStrings.Length; i++) { String buttonString = buttonStrings[i]; if (buttonString == "LeftTrigger") { resultData.LeftTrigger = XBoxController.LEFT_TRIGGER_THRESHOLD; } else if (buttonString == "RightTrigger") { resultData.RightTrigger = XBoxController.RIGHT_TRIGGER_THRESHOLD; } else if (buttonString == "LeftStickUp") { resultData.LeftThumbY = XBoxController.LEFT_STICK_THRESHOLD; } else if (buttonString == "LeftStickDown") { resultData.LeftThumbY = (short)-XBoxController.LEFT_STICK_THRESHOLD; } else if (buttonString == "LeftStickLeft") { resultData.LeftThumbX = (short)-XBoxController.LEFT_STICK_THRESHOLD; } else if (buttonString == "LeftStickRight") { resultData.LeftThumbX = XBoxController.LEFT_STICK_THRESHOLD; } else if (buttonString == "RightStickUp") { resultData.RightThumbY = XBoxController.RIGHT_STICK_THRESHOLD; } else if (buttonString == "RightStickDown") { resultData.RightThumbY = (short)-XBoxController.RIGHT_STICK_THRESHOLD; } else if (buttonString == "RightStickLeft") { resultData.RightThumbX = (short)-XBoxController.RIGHT_STICK_THRESHOLD; } else if (buttonString == "RightStickRight") { resultData.RightThumbX = XBoxController.RIGHT_STICK_THRESHOLD; } else if (Enum.IsDefined(typeof(GamepadButtonFlags), buttonString)) { GamepadButtonFlags buttonFlag = (GamepadButtonFlags)Enum.Parse(typeof(GamepadButtonFlags), buttonString); resultData.Buttons |= buttonFlag; } else { hasInvalidKey = true; break; } } if (!hasInvalidKey) { return(new ControllerState(resultData, XBoxController.GetButtonPressedCount(resultData))); } else { return(new ControllerState(resultData, -1)); } }