TechbloxModdingAPI/TechbloxModdingAPI/Interface/IMGUI/Group.cs
Norbi Peti 99f077a917 Update to Techblox 2021.06.08.16.19
Added check for time mode toggle to avoid crashing the game
Added support for having the ref folder outside the solution in gen_csproj.py
Removed BlockIdentifiers class
Added check for invalid player ID when placing blocks
Resolved compilation errors
2021-06-09 20:11:31 +02:00

157 lines
5.1 KiB
C#

using System;
using TechbloxModdingAPI.Utility;
using Svelto.DataStructures;
using UnityEngine;
namespace TechbloxModdingAPI.Interface.IMGUI
{
/// <summary>
/// A group of elements.
/// This wraps Unity's GUILayout Area and GUI Group system.
/// </summary>
public class Group : UIElement
{
private bool automaticLayout;
private FasterList<UIElement> elements = new FasterList<UIElement>();
/// <summary>
/// The rectangular area in the window that the UI group can use
/// </summary>
public Rect Box { get; set; }
public void OnGUI()
{
/*if (Constants.Default == null) return;
if (Constants.Default.box == null) return;*/
GUIStyle guiStyle = Constants.Default.box;
UIElement[] elems = elements.ToArrayFast(out int count);
if (automaticLayout)
{
GUILayout.BeginArea(Box, guiStyle);
for (uint i = 0; i < count; i++)
{
/*try
{
if (elems[i].Enabled)
elems[i].OnGUI();
}
catch (ArgumentException)
{
// ignore these, since this is (hopefully) just Unity being dumb
}
catch (Exception e)
{
Logging.MetaDebugLog($"Element '{elems[i].Name}' threw exception:\n{e.ToString()}");
}*/
if (elems[i].Enabled)
elems[i].OnGUI();
}
GUILayout.EndArea();
}
else
{
GUI.BeginGroup(Box, guiStyle);
for (uint i = 0; i < count; i++)
{
if (elems[i].Enabled)
elems[i].OnGUI();
}
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.
/// </summary>
public int Length
{
get => elements.count;
}
/// <summary>
/// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.Interface.IMGUI.Group"/> class.
/// </summary>
/// <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)
{
Box = box;
if (name == null)
{
this.Name = typeof(Group).FullName + "::" + box.ToString().Replace(" ", "");
}
else
{
this.Name = name;
}
this.automaticLayout = automaticLayout;
IMGUIManager.AddElement(this);
}
/// <summary>
/// Add an element to the group.
/// </summary>
/// <param name="element">The element to add.</param>
/// <returns>Index of the new element.</returns>
public int AddElement(UIElement element)
{
IMGUIManager.RemoveElement(element); // groups manage internal elements themselves
elements.Add(element);
return elements.count - 1;
}
/// <summary>
/// Remove an element from the group.
/// </summary>
/// <param name="element">The element to remove.</param>
/// <returns>Whether removal was successful.</returns>
public bool RemoveElement(UIElement element)
{
int index = IndexOf(element);
return RemoveAt(index);
}
/// <summary>
/// Remove the element in a specific location.
/// </summary>
/// <param name="index">Index of the element.</param>
/// <returns>Whether removal was successful.</returns>
public bool RemoveAt(int index)
{
if (index < 0 || index >= elements.count) return false;
IMGUIManager.AddElement(elements[index]); // re-add to global manager
elements.RemoveAt((uint) index);
return true;
}
/// <summary>
/// Get the index of an element.
/// </summary>
/// <param name="element">The element to search for.</param>
/// <returns>The element's index, or -1 if not found.</returns>
public int IndexOf(UIElement element)
{
UIElement[] elems = elements.ToArrayFast(out int count);
for (int i = 0; i < count; i++)
{
if (elems[i].Name == element.Name)
{
return i;
}
}
return -1;
}
}
}