using System; using UnityEngine; namespace GamecraftModdingAPI.Interface.IMGUI { /// /// A text input field. /// This wraps Unity's IMGUI TextField and TextArea. /// public class Text : UIElement { private bool automaticLayout; /// /// Whether the text input field is multiline (true -> TextArea) or not (false -> TextField). /// public bool Multiline { get; set; } private string text; /// /// The rectangular area that the text field can use. /// public Rect Box { get; set; } = Rect.zero; /// /// An event that fires whenever the text input is edited. /// public event EventHandler OnEdit; public void OnGUI() { string editedText = null; if (automaticLayout) { if (Multiline) { editedText = GUILayout.TextArea(text, Constants.Default.textArea); } else { editedText = GUILayout.TextField(text, Constants.Default.textField); } } else { if (Multiline) { editedText = GUI.TextArea(Box, text, Constants.Default.textArea); } else { editedText = GUI.TextField(Box, text, Constants.Default.textField); } } if (editedText != null && editedText != text) { OnEdit?.Invoke(this, editedText); text = editedText; } } /// /// 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) { this.Multiline = multiline; automaticLayout = true; text = initialText ?? ""; if (name == null) { this.Name = typeof(Text).FullName + "::" + text; } else { this.Name = name; } IMGUIManager.AddElement(this); } /// /// Initialize the text input field. /// /// Rectangular area for the text field. /// Initial text in the input field. /// The text field's name. /// Allow multiple lines? public Text(Rect box, string initialText = null, string name = null, bool multiline = false) : this(initialText, name, multiline) { this.Box = box; automaticLayout = false; } } }