using System;
using Unity.Mathematics;
using UnityEngine;
namespace GamecraftModdingAPI.Interface.IMGUI
{
///
/// A clickable button.
/// This wraps Unity's IMGUI Button.
///
public class Button : UIElement
{
private bool automaticLayout = false;
private string text;
///
/// The rectangular area that the button can use.
///
public Rect Box { get; set; } = Rect.zero;
///
/// An event that fires when the button is clicked.
///
public event EventHandler OnClick;
public void OnGUI()
{
if (automaticLayout)
{
if (GUILayout.Button(text, Constants.Default.button))
{
OnClick?.Invoke(this, true);
}
}
else
{
if (GUI.Button(Box, text, Constants.Default.button))
{
OnClick?.Invoke(this, true);
}
}
}
///
/// 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)
{
automaticLayout = true;
this.text = text;
if (name == null)
{
this.Name = typeof(Button).FullName + "::" + text;
}
else
{
this.Name = name;
}
IMGUIManager.AddElement(this);
}
///
/// Initialize a new button.
///
/// The text to display on the button.
/// Rectangular area for the button to use.
/// The button's name.
public Button(string text, Rect box, string name = null) : this(text, name)
{
automaticLayout = false;
this.Box = box;
}
~Button()
{
IMGUIManager.RemoveElement(this);
}
}
}