From 2db7b607f0f1059437b91a54c17980906b313514 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 23 Feb 2022 02:25:34 +0100 Subject: [PATCH] Improve UI elements (IMGUI) --- TechbloxModdingAPI/Interface/IMGUI/Button.cs | 28 ++--------------- TechbloxModdingAPI/Interface/IMGUI/Group.cs | 23 ++------------ .../Interface/IMGUI/IMGUIManager.cs | 7 +++-- TechbloxModdingAPI/Interface/IMGUI/Image.cs | 31 ++----------------- TechbloxModdingAPI/Interface/IMGUI/Label.cs | 23 ++------------ TechbloxModdingAPI/Interface/IMGUI/Text.cs | 23 ++------------ .../Interface/IMGUI/UIElement.cs | 19 +++++++++--- TechbloxModdingAPI/Player.cs | 4 +-- TechbloxModdingAPI/Players/PlayerEngine.cs | 2 +- TechbloxModdingAPI/SimBody.cs | 3 +- .../Tests/TechbloxModdingAPIPluginTest.cs | 6 ++-- 11 files changed, 37 insertions(+), 132 deletions(-) diff --git a/TechbloxModdingAPI/Interface/IMGUI/Button.cs b/TechbloxModdingAPI/Interface/IMGUI/Button.cs index de8e7cc..6cf5fc8 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/Button.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/Button.cs @@ -24,7 +24,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// public event EventHandler OnClick; - public void OnGUI() + public override void OnGUI() { if (automaticLayout) { @@ -42,34 +42,15 @@ namespace TechbloxModdingAPI.Interface.IMGUI } } - /// - /// The button's unique name. - /// - public string Name { get; private set; } - - /// - /// Whether to display the button. - /// - public bool Enabled { get; set; } = true; - /// /// Initialize a new button with automatic layout. /// /// The text to display on the button. /// The button's name. - public Button(string text, string name = null) + public Button(string text, string name = null) : base(text, name) { automaticLayout = true; this.text = text; - if (name == null) - { - this.Name = typeof(Button).FullName + "::" + text; - } - else - { - this.Name = name; - } - IMGUIManager.AddElement(this); } /// @@ -83,10 +64,5 @@ namespace TechbloxModdingAPI.Interface.IMGUI automaticLayout = false; this.Box = box; } - - ~Button() - { - IMGUIManager.RemoveElement(this); - } } } \ No newline at end of file diff --git a/TechbloxModdingAPI/Interface/IMGUI/Group.cs b/TechbloxModdingAPI/Interface/IMGUI/Group.cs index b1dcccc..88a36c2 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/Group.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/Group.cs @@ -20,7 +20,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// public Rect Box { get; set; } - public void OnGUI() + public override void OnGUI() { /*if (Constants.Default == null) return; if (Constants.Default.box == null) return;*/ @@ -60,16 +60,6 @@ namespace TechbloxModdingAPI.Interface.IMGUI GUI.EndGroup(); } } - - /// - /// The group's unique name. - /// - public string Name { get; } - - /// - /// Whether to display the group and everything in it. - /// - public bool Enabled { set; get; } = true; /// /// The amount of elements in the group. @@ -85,19 +75,10 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// The rectangular area to use in the window. /// Name of the group. /// Whether to use automatic UI layout. - public Group(Rect box, string name = null, bool automaticLayout = false) + public Group(Rect box, string name = null, bool automaticLayout = false) : base(box.ToString().Replace(" ", ""), name) { Box = box; - if (name == null) - { - this.Name = typeof(Group).FullName + "::" + box.ToString().Replace(" ", ""); - } - else - { - this.Name = name; - } this.automaticLayout = automaticLayout; - IMGUIManager.AddElement(this); } /// diff --git a/TechbloxModdingAPI/Interface/IMGUI/IMGUIManager.cs b/TechbloxModdingAPI/Interface/IMGUI/IMGUIManager.cs index c27c484..d68f14b 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/IMGUIManager.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/IMGUIManager.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using Svelto.Tasks; using Svelto.Tasks.ExtraLean; using TechbloxModdingAPI.Tasks; +using TechbloxModdingAPI.Utility; using UnityEngine; namespace TechbloxModdingAPI.Interface.IMGUI @@ -14,9 +15,9 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// public static class IMGUIManager { - internal static OnGuiRunner ImguiScheduler = new OnGuiRunner("TechbloxModdingAPI_IMGUIScheduler"); - - private static Dictionary _activeElements = new Dictionary(); + internal static OnGuiRunner ImguiScheduler = new("TechbloxModdingAPI_IMGUIScheduler"); + + private static readonly WeakDictionary _activeElements = new(); /// /// Add an UIElement instance to be managed by IMGUIManager. diff --git a/TechbloxModdingAPI/Interface/IMGUI/Image.cs b/TechbloxModdingAPI/Interface/IMGUI/Image.cs index 2bea1f4..88e82c1 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/Image.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/Image.cs @@ -20,7 +20,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// public Rect Box { get; set; } = Rect.zero; - public void OnGUI() + public override void OnGUI() { //if (Texture == null) return; if (automaticLayout) @@ -32,43 +32,16 @@ namespace TechbloxModdingAPI.Interface.IMGUI GUI.Label(Box, Texture, Constants.Default.label); } } - - /// - /// The image element's unique name. - /// - public string Name { get; } - - /// - /// Whether to display the image and everything in it. - /// - public bool Enabled { set; get; } = true; /// /// Initializes a new instance of the class with automatic layout. /// /// Image to display. /// The element's name. - public Image(Texture texture = null, string name = null) + public Image(Texture texture = null, string name = null) : base(texture == null ? "" : $"{texture.name}({texture.width}x{texture.height})", name) { automaticLayout = true; Texture = texture; - if (name == null) - { - if (texture == null) - { - this.Name = typeof(Image).FullName + "::" + texture; - } - else - { - this.Name = typeof(Image).FullName + "::" + texture.name + "(" + texture.width + "x" + texture.height + ")"; - } - - } - else - { - this.Name = name; - } - IMGUIManager.AddElement(this); } /// diff --git a/TechbloxModdingAPI/Interface/IMGUI/Label.cs b/TechbloxModdingAPI/Interface/IMGUI/Label.cs index 960a6ca..e394f35 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/Label.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/Label.cs @@ -20,7 +20,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// public Rect Box { get; set; } = Rect.zero; - public void OnGUI() + public override void OnGUI() { if (automaticLayout) { @@ -32,34 +32,15 @@ namespace TechbloxModdingAPI.Interface.IMGUI } } - /// - /// The label's unique name. - /// - public string Name { get; } - - /// - /// Whether to display the label. - /// - public bool Enabled { set; get; } = true; - /// /// Initializes a new instance of the class with automatic layout. /// /// Initial string to display on the label. /// The element's name. - public Label(string initialText = null, string name = null) + public Label(string initialText = null, string name = null) : base(initialText, name) { automaticLayout = true; Text = initialText; - if (name == null) - { - this.Name = typeof(Label).FullName + "::" + initialText; - } - else - { - this.Name = name; - } - IMGUIManager.AddElement(this); } /// diff --git a/TechbloxModdingAPI/Interface/IMGUI/Text.cs b/TechbloxModdingAPI/Interface/IMGUI/Text.cs index bdf6738..8f6033d 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/Text.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/Text.cs @@ -28,7 +28,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// public event EventHandler OnEdit; - public void OnGUI() + public override void OnGUI() { string editedText = null; if (automaticLayout) @@ -61,36 +61,17 @@ namespace TechbloxModdingAPI.Interface.IMGUI } } - /// - /// The text field's unique name. - /// - public string Name { get; } - - /// - /// Whether to display the text field. - /// - public bool Enabled { set; get; } = true; - /// /// Initialize the text input field with automatic layout. /// /// Initial text in the input field. /// The text field's name. /// Allow multiple lines? - public Text(string initialText = null, string name = null, bool multiline = false) + public Text(string initialText = null, string name = null, bool multiline = false) : base(initialText, name) { this.Multiline = multiline; automaticLayout = true; text = initialText ?? ""; - if (name == null) - { - this.Name = typeof(Text).FullName + "::" + text; - } - else - { - this.Name = name; - } - IMGUIManager.AddElement(this); } /// diff --git a/TechbloxModdingAPI/Interface/IMGUI/UIElement.cs b/TechbloxModdingAPI/Interface/IMGUI/UIElement.cs index dd66af6..fb25c38 100644 --- a/TechbloxModdingAPI/Interface/IMGUI/UIElement.cs +++ b/TechbloxModdingAPI/Interface/IMGUI/UIElement.cs @@ -6,23 +6,34 @@ namespace TechbloxModdingAPI.Interface.IMGUI /// GUI Element like a text field, button or picture. /// This interface is used to wrap many elements from Unity's IMGUI system. /// - public interface UIElement + public abstract class UIElement { + protected UIElement(string text, string name) + { + Name = name ?? GetType().FullName + "::" + text; + IMGUIManager.AddElement(this); + } + + ~UIElement() + { + IMGUIManager.RemoveElement(this); + } + /// /// GUI operations to perform in the OnGUI scope. /// This is basically equivalent to a MonoBehaviour's OnGUI method. /// - void OnGUI(); + public abstract void OnGUI(); /// /// The element's name. /// This should be unique for every instance of the class. /// - string Name { get; } + public string Name { get; } /// /// Whether to display the UI element or not. /// - bool Enabled { get; } + public bool Enabled { get; set; } = true; } } \ No newline at end of file diff --git a/TechbloxModdingAPI/Player.cs b/TechbloxModdingAPI/Player.cs index a535269..32901ad 100644 --- a/TechbloxModdingAPI/Player.cs +++ b/TechbloxModdingAPI/Player.cs @@ -195,8 +195,8 @@ namespace TechbloxModdingAPI /// The player's mass. /// /// The mass. - public float Mass => - 1f / playerEngine.GetCharacterStruct(Id).Get().physicsMass.InverseMass; + [Obsolete] // We cannot get it clientside or something + public float Mass => 0; /// /// The player's latest network ping time. diff --git a/TechbloxModdingAPI/Players/PlayerEngine.cs b/TechbloxModdingAPI/Players/PlayerEngine.cs index 728f93f..7e53270 100644 --- a/TechbloxModdingAPI/Players/PlayerEngine.cs +++ b/TechbloxModdingAPI/Players/PlayerEngine.cs @@ -139,7 +139,7 @@ namespace TechbloxModdingAPI.Players group = default; if (GameState.IsBuildMode()) return entitiesDB.QueryEntityOptional(new EGID(playerId, LocalBuildingDrone.BuildGroup)); - + var characterGroups = CharacterExclusiveGroups.AllCharacters; for (int i = 0; i < characterGroups.count; i++) { diff --git a/TechbloxModdingAPI/SimBody.cs b/TechbloxModdingAPI/SimBody.cs index 158927a..06e152b 100644 --- a/TechbloxModdingAPI/SimBody.cs +++ b/TechbloxModdingAPI/SimBody.cs @@ -73,9 +73,10 @@ namespace TechbloxModdingAPI } } + [Obsolete] //Cannot get mass even from UECS public float Mass { - get => 0f; //TODO: Get mass from UECS entity + get => 0f; //set => GetStruct().physicsMass.InverseMass = math.rcp(value); } diff --git a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs index 12ae6e4..7564816 100644 --- a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs +++ b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs @@ -261,7 +261,7 @@ namespace TechbloxModdingAPI.Tests /*((FasterList)AccessTools.Property(typeof(GuiInputMap), "GuiInputsButtonDown").GetValue(null)) .Add(new GuiInputMap.GuiInputMapElement(RewiredConsts.Action.ToggleCommandLine, GuiIn))*/ - Game.Enter += (sender, e) => + /*Game.Enter += (sender, e) => { ushort lastKey = ushort.MaxValue; foreach (var kv in FullGameFields._dataDb.GetValues() @@ -299,7 +299,7 @@ namespace TechbloxModdingAPI.Tests return result; }) .Aggregate((a, b) => a + "\n" + b)); - }; + };*/ CommandBuilder.Builder("takeScreenshot", "Enables the screenshot taker") .Action(() => @@ -368,7 +368,7 @@ namespace TechbloxModdingAPI.Tests }, () => shouldTestGhostBlock)); Logging.CommandLog("Test enabled"); }).Build(); - + Client.EnterMenu += (sender, args) => Scheduler.Schedule(new Once(() => Client.Instance.CloseBetaPopup())); Game.Enter += (sender, args) =>