A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9KB

  1. using System;
  2. using UnityEngine;
  3. namespace TechbloxModdingAPI.Interface.IMGUI
  4. {
  5. /// <summary>
  6. /// A text input field.
  7. /// This wraps Unity's IMGUI TextField and TextArea.
  8. /// </summary>
  9. public class Text : UIElement
  10. {
  11. private bool automaticLayout;
  12. /// <summary>
  13. /// Whether the text input field is multiline (true -> TextArea) or not (false -> TextField).
  14. /// </summary>
  15. public bool Multiline { get; set; }
  16. private string text;
  17. /// <summary>
  18. /// The rectangular area that the text field can use.
  19. /// </summary>
  20. public Rect Box { get; set; } = Rect.zero;
  21. /// <summary>
  22. /// An event that fires whenever the text input is edited.
  23. /// </summary>
  24. public event EventHandler<string> OnEdit;
  25. public override void OnGUI()
  26. {
  27. string editedText = null;
  28. if (automaticLayout)
  29. {
  30. if (Multiline)
  31. {
  32. editedText = GUILayout.TextArea(text, Constants.Default.textArea);
  33. }
  34. else
  35. {
  36. editedText = GUILayout.TextField(text, Constants.Default.textField);
  37. }
  38. }
  39. else
  40. {
  41. if (Multiline)
  42. {
  43. editedText = GUI.TextArea(Box, text, Constants.Default.textArea);
  44. }
  45. else
  46. {
  47. editedText = GUI.TextField(Box, text, Constants.Default.textField);
  48. }
  49. }
  50. if (editedText != null && editedText != text)
  51. {
  52. OnEdit?.Invoke(this, editedText);
  53. text = editedText;
  54. }
  55. }
  56. /// <summary>
  57. /// Initialize the text input field with automatic layout.
  58. /// </summary>
  59. /// <param name="initialText">Initial text in the input field.</param>
  60. /// <param name="name">The text field's name.</param>
  61. /// <param name="multiline">Allow multiple lines?</param>
  62. public Text(string initialText = null, string name = null, bool multiline = false) : base(initialText, name)
  63. {
  64. this.Multiline = multiline;
  65. automaticLayout = true;
  66. text = initialText ?? "";
  67. }
  68. /// <summary>
  69. /// Initialize the text input field.
  70. /// </summary>
  71. /// <param name="box">Rectangular area for the text field.</param>
  72. /// <param name="initialText">Initial text in the input field.</param>
  73. /// <param name="name">The text field's name.</param>
  74. /// <param name="multiline">Allow multiple lines?</param>
  75. public Text(Rect box, string initialText = null, string name = null, bool multiline = false) : this(initialText, name, multiline)
  76. {
  77. this.Box = box;
  78. automaticLayout = false;
  79. }
  80. }
  81. }