From 5ca18d272a6892b2244e9b3d674db12d0e1b4b5d Mon Sep 17 00:00:00 2001 From: NGnius Date: Sat, 4 Apr 2020 14:48:12 -0400 Subject: [PATCH] Add WIP GUI input functionality --- GamecraftModdingAPI/Input/FakeInput.cs | 115 +++++++++++++++++++ GamecraftModdingAPI/Input/FakeInputEngine.cs | 62 ++++++++++ GamecraftModdingAPI/Inventory/Hotbar.cs | 2 +- GamecraftModdingAPI/Main.cs | 2 + 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 GamecraftModdingAPI/Input/FakeInput.cs create mode 100644 GamecraftModdingAPI/Input/FakeInputEngine.cs diff --git a/GamecraftModdingAPI/Input/FakeInput.cs b/GamecraftModdingAPI/Input/FakeInput.cs new file mode 100644 index 0000000..e2ce676 --- /dev/null +++ b/GamecraftModdingAPI/Input/FakeInput.cs @@ -0,0 +1,115 @@ +using System; + +using RobocraftX.Common; +using RobocraftX.Common.Input; +using Svelto.ECS; + +using GamecraftModdingAPI.Utility; + +namespace GamecraftModdingAPI.Input +{ + public static class FakeInput + { + private static readonly FakeInputEngine inputEngine = new FakeInputEngine(); + + /// + /// Customize the player input. + /// + /// The custom input. + /// The player. Omit this to use the local player. + public static void CustomInput(InputEntityStruct input, uint playerID = uint.MaxValue) + { + if (playerID == uint.MaxValue) + { + playerID = inputEngine.GetLocalPlayerID(); + } + inputEngine.SendCustomInput(input, playerID); + } + + public static InputEntityStruct GetInput(uint playerID = uint.MaxValue) + { + if (playerID == uint.MaxValue) + { + playerID = inputEngine.GetLocalPlayerID(); + } + return inputEngine.GetInput(playerID); + } + + /// + /// Fake a GUI input. + /// Omit any parameter you do not want to affect. + /// Parameters that end with "?" don't do anything... yet. + /// + /// The player. Omit this to use the local player. + /// Select the hotbar slot by number. + /// Select the hotbar hand. + /// Toggle the command line? + /// Open inventory? + /// Open escape menu? + /// Page return? + /// Toggle debug display? + /// Select next? + /// Select previous? + /// Tab? + /// Toggle to hotbar colour mode? + /// Select the hotbar page by number? + /// Quicksave? + /// Paste? + public static void GuiInput(uint playerID = uint.MaxValue, int hotbar = -1, bool hotbarHand = false, bool commandLine = false, bool inventory = false, bool escape = false, bool enter = false, bool debug = false, bool next = false, bool previous = false, bool tab = false, bool colour = false, int hotbarPage = -1, bool quickSave = false, bool paste = false) + { + if (playerID == uint.MaxValue) + { + playerID = inputEngine.GetLocalPlayerID(); + } + ref InputEntityStruct currentInput = ref inputEngine.GetInputRef(playerID); + // set inputs + switch(hotbar) + { + case 0: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_0; break; + case 1: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_1; break; + case 2: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_2; break; + case 3: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_3; break; + case 4: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_4; break; + case 5: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_5; break; + case 6: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_6; break; + case 7: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_7; break; + case 8: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_8; break; + case 9: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_9; break; + case 10: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_Hand; break; + default: break; + } + if (hotbarHand) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_Hand; + if (commandLine) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.ToggleCommandLine; + if (inventory) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Inventory; + if (escape) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Escape; + if (enter) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Return; + if (debug) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.ToggleDebugDisplay; + if (next) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.SelectNext; + if (previous) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.SelectPrev; + if (tab) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Tab; + if (colour) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.Hotbar_Colour; + switch (hotbarPage) + { + case 1: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage1; break; + case 2: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage2; break; + case 3: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage3; break; + case 4: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage4; break; + case 5: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage5; break; + case 6: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage6; break; + case 7: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage7; break; + case 8: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage8; break; + case 9: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage9; break; + case 10: currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.HotbarPage10; break; + default: break; + } + if (quickSave) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.QuickSave; + if (paste) currentInput.guiMask |= RobocraftX.Common.Input.GuiInput.PasteSelection; + } + + public static void Init() + { + GameEngineManager.AddGameEngine(inputEngine); + MenuEngineManager.AddMenuEngine(inputEngine); + } + } +} diff --git a/GamecraftModdingAPI/Input/FakeInputEngine.cs b/GamecraftModdingAPI/Input/FakeInputEngine.cs new file mode 100644 index 0000000..dea0f3e --- /dev/null +++ b/GamecraftModdingAPI/Input/FakeInputEngine.cs @@ -0,0 +1,62 @@ +using System; + +using RobocraftX.Common.Input; +using RobocraftX.Players; +using Svelto.ECS; + +using GamecraftModdingAPI.Utility; + +namespace GamecraftModdingAPI.Input +{ + public class FakeInputEngine : IApiEngine + { + public string Name { get; } = "GamecraftModdingAPIFakeInputEngine"; + + public EntitiesDB entitiesDB { set; private get; } + + public bool IsReady = false; + + public void Dispose() + { + IsReady = false; + } + + public void Ready() + { + IsReady = true; + } + + public bool SendCustomInput(InputEntityStruct input, uint playerID, bool remote = false) + { + EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); + if (entitiesDB.Exists(egid)) + { + ref InputEntityStruct ies = ref entitiesDB.QueryEntity(egid); + ies = input; + return true; + } + else return false; + } + + public InputEntityStruct GetInput(uint playerID, bool remote = false) + { + EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); + if (entitiesDB.Exists(egid)) + { + return entitiesDB.QueryEntity(egid); + } + else return default(InputEntityStruct); + } + + public ref InputEntityStruct GetInputRef(uint playerID, bool remote = false) + { + EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); + return ref entitiesDB.QueryEntity(egid); + } + + public uint GetLocalPlayerID() + { + return LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB); + } + } +} diff --git a/GamecraftModdingAPI/Inventory/Hotbar.cs b/GamecraftModdingAPI/Inventory/Hotbar.cs index 01bd1e0..c649a66 100644 --- a/GamecraftModdingAPI/Inventory/Hotbar.cs +++ b/GamecraftModdingAPI/Inventory/Hotbar.cs @@ -11,7 +11,7 @@ namespace GamecraftModdingAPI.Inventory { public static class Hotbar { - private static HotbarEngine hotbarEngine = new HotbarEngine(); + private static readonly HotbarEngine hotbarEngine = new HotbarEngine(); /// /// Switch the block in the player's hand diff --git a/GamecraftModdingAPI/Main.cs b/GamecraftModdingAPI/Main.cs index 70f59e8..0767bab 100644 --- a/GamecraftModdingAPI/Main.cs +++ b/GamecraftModdingAPI/Main.cs @@ -67,6 +67,8 @@ namespace GamecraftModdingAPI Blocks.Tweakable.Init(); // init inventory Inventory.Hotbar.Init(); + // init input + Input.FakeInput.Init(); Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized"); }