Browse Source

Improve UI elements (IMGUI)

tags/v2.3.0
NorbiPeti 2 years ago
parent
commit
2db7b607f0
11 changed files with 37 additions and 132 deletions
  1. +2
    -26
      TechbloxModdingAPI/Interface/IMGUI/Button.cs
  2. +2
    -21
      TechbloxModdingAPI/Interface/IMGUI/Group.cs
  3. +4
    -3
      TechbloxModdingAPI/Interface/IMGUI/IMGUIManager.cs
  4. +2
    -29
      TechbloxModdingAPI/Interface/IMGUI/Image.cs
  5. +2
    -21
      TechbloxModdingAPI/Interface/IMGUI/Label.cs
  6. +2
    -21
      TechbloxModdingAPI/Interface/IMGUI/Text.cs
  7. +15
    -4
      TechbloxModdingAPI/Interface/IMGUI/UIElement.cs
  8. +2
    -2
      TechbloxModdingAPI/Player.cs
  9. +1
    -1
      TechbloxModdingAPI/Players/PlayerEngine.cs
  10. +2
    -1
      TechbloxModdingAPI/SimBody.cs
  11. +3
    -3
      TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs

+ 2
- 26
TechbloxModdingAPI/Interface/IMGUI/Button.cs View File

@@ -24,7 +24,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI
/// </summary>
public event EventHandler<bool> OnClick;
public void OnGUI()
public override void OnGUI()
{
if (automaticLayout)
{
@@ -42,34 +42,15 @@ namespace TechbloxModdingAPI.Interface.IMGUI
}
}

/// <summary>
/// The button's unique name.
/// </summary>
public string Name { get; private set; }

/// <summary>
/// Whether to display the button.
/// </summary>
public bool Enabled { get; set; } = true;

/// <summary>
/// Initialize a new button with automatic layout.
/// </summary>
/// <param name="text">The text to display on the button.</param>
/// <param name="name">The button's name.</param>
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);
}

/// <summary>
@@ -83,10 +64,5 @@ namespace TechbloxModdingAPI.Interface.IMGUI
automaticLayout = false;
this.Box = box;
}
~Button()
{
IMGUIManager.RemoveElement(this);
}
}
}

+ 2
- 21
TechbloxModdingAPI/Interface/IMGUI/Group.cs View File

@@ -20,7 +20,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI
/// </summary>
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();
}
}

/// <summary>
/// The group's unique name.
/// </summary>
public string Name { get; }
/// <summary>
/// Whether to display the group and everything in it.
/// </summary>
public bool Enabled { set; get; } = true;
/// <summary>
/// The amount of elements in the group.
@@ -85,19 +75,10 @@ namespace TechbloxModdingAPI.Interface.IMGUI
/// <param name="box">The rectangular area to use in the window.</param>
/// <param name="name">Name of the group.</param>
/// <param name="automaticLayout">Whether to use automatic UI layout.</param>
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);
}

/// <summary>


+ 4
- 3
TechbloxModdingAPI/Interface/IMGUI/IMGUIManager.cs View File

@@ -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
/// </summary>
public static class IMGUIManager
{
internal static OnGuiRunner ImguiScheduler = new OnGuiRunner("TechbloxModdingAPI_IMGUIScheduler");
private static Dictionary<string, UIElement> _activeElements = new Dictionary<string,UIElement>();
internal static OnGuiRunner ImguiScheduler = new("TechbloxModdingAPI_IMGUIScheduler");
private static readonly WeakDictionary<string, UIElement> _activeElements = new();

/// <summary>
/// Add an UIElement instance to be managed by IMGUIManager.


+ 2
- 29
TechbloxModdingAPI/Interface/IMGUI/Image.cs View File

@@ -20,7 +20,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI
/// </summary>
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);
}
}

/// <summary>
/// The image element's unique name.
/// </summary>
public string Name { get; }
/// <summary>
/// Whether to display the image and everything in it.
/// </summary>
public bool Enabled { set; get; } = true;
/// <summary>
/// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.Interface.IMGUI.Image"/> class with automatic layout.
/// </summary>
/// <param name="texture">Image to display.</param>
/// <param name="name">The element's name.</param>
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);
}

/// <summary>


+ 2
- 21
TechbloxModdingAPI/Interface/IMGUI/Label.cs View File

@@ -20,7 +20,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI
/// </summary>
public Rect Box { get; set; } = Rect.zero;
public void OnGUI()
public override void OnGUI()
{
if (automaticLayout)
{
@@ -32,34 +32,15 @@ namespace TechbloxModdingAPI.Interface.IMGUI
}
}

/// <summary>
/// The label's unique name.
/// </summary>
public string Name { get; }
/// <summary>
/// Whether to display the label.
/// </summary>
public bool Enabled { set; get; } = true;

/// <summary>
/// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.Interface.IMGUI.Label"/> class with automatic layout.
/// </summary>
/// <param name="initialText">Initial string to display on the label.</param>
/// <param name="name">The element's name.</param>
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);
}

/// <summary>


+ 2
- 21
TechbloxModdingAPI/Interface/IMGUI/Text.cs View File

@@ -28,7 +28,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI
/// </summary>
public event EventHandler<string> OnEdit;
public void OnGUI()
public override void OnGUI()
{
string editedText = null;
if (automaticLayout)
@@ -61,36 +61,17 @@ namespace TechbloxModdingAPI.Interface.IMGUI
}
}

/// <summary>
/// The text field's unique name.
/// </summary>
public string Name { get; }
/// <summary>
/// Whether to display the text field.
/// </summary>
public bool Enabled { set; get; } = true;

/// <summary>
/// Initialize the text input field with automatic layout.
/// </summary>
/// <param name="initialText">Initial text in the input field.</param>
/// <param name="name">The text field's name.</param>
/// <param name="multiline">Allow multiple lines?</param>
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);
}

/// <summary>


+ 15
- 4
TechbloxModdingAPI/Interface/IMGUI/UIElement.cs View File

@@ -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.
/// </summary>
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);
}
/// <summary>
/// GUI operations to perform in the OnGUI scope.
/// This is basically equivalent to a MonoBehaviour's OnGUI method.
/// </summary>
void OnGUI();
public abstract void OnGUI();
/// <summary>
/// The element's name.
/// This should be unique for every instance of the class.
/// </summary>
string Name { get; }
public string Name { get; }
/// <summary>
/// Whether to display the UI element or not.
/// </summary>
bool Enabled { get; }
public bool Enabled { get; set; } = true;
}
}

+ 2
- 2
TechbloxModdingAPI/Player.cs View File

@@ -195,8 +195,8 @@ namespace TechbloxModdingAPI
/// The player's mass.
/// </summary>
/// <value>The mass.</value>
public float Mass =>
1f / playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().physicsMass.InverseMass;
[Obsolete] // We cannot get it clientside or something
public float Mass => 0;

/// <summary>
/// The player's latest network ping time.


+ 1
- 1
TechbloxModdingAPI/Players/PlayerEngine.cs View File

@@ -139,7 +139,7 @@ namespace TechbloxModdingAPI.Players
group = default;
if (GameState.IsBuildMode())
return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, LocalBuildingDrone.BuildGroup));
var characterGroups = CharacterExclusiveGroups.AllCharacters;
for (int i = 0; i < characterGroups.count; i++)
{


+ 2
- 1
TechbloxModdingAPI/SimBody.cs View File

@@ -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);
}



+ 3
- 3
TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs View File

@@ -261,7 +261,7 @@ namespace TechbloxModdingAPI.Tests
/*((FasterList<GuiInputMap.GuiInputMapElement>)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<CubeListData>()
@@ -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) =>


Loading…
Cancel
Save