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.

Text.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using UnityEngine;
  3. namespace GamecraftModdingAPI.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 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. /// The text field's unique name.
  58. /// </summary>
  59. public string Name { get; }
  60. /// <summary>
  61. /// Whether to display the text field.
  62. /// </summary>
  63. public bool Enabled { set; get; } = true;
  64. /// <summary>
  65. /// Initialize the text input field with automatic layout.
  66. /// </summary>
  67. /// <param name="initialText">Initial text in the input field.</param>
  68. /// <param name="name">The text field's name.</param>
  69. /// <param name="multiline">Allow multiple lines?</param>
  70. public Text(string initialText = null, string name = null, bool multiline = false)
  71. {
  72. this.Multiline = multiline;
  73. automaticLayout = true;
  74. text = initialText ?? "";
  75. if (name == null)
  76. {
  77. this.Name = typeof(Text).FullName + "::" + text;
  78. }
  79. else
  80. {
  81. this.Name = name;
  82. }
  83. IMGUIManager.AddElement(this);
  84. }
  85. /// <summary>
  86. /// Initialize the text input field.
  87. /// </summary>
  88. /// <param name="box">Rectangular area for the text field.</param>
  89. /// <param name="initialText">Initial text in the input field.</param>
  90. /// <param name="name">The text field's name.</param>
  91. /// <param name="multiline">Allow multiple lines?</param>
  92. public Text(Rect box, string initialText = null, string name = null, bool multiline = false) : this(initialText, name, multiline)
  93. {
  94. this.Box = box;
  95. automaticLayout = false;
  96. }
  97. }
  98. }