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