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.

Button.cs 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using Unity.Mathematics;
  3. using UnityEngine;
  4. namespace GamecraftModdingAPI.Interface.IMGUI
  5. {
  6. /// <summary>
  7. /// A clickable button.
  8. /// This wraps Unity's IMGUI Button.
  9. /// </summary>
  10. public class Button : UIElement
  11. {
  12. private bool automaticLayout = false;
  13. private string text;
  14. /// <summary>
  15. /// The rectangular area that the button can use.
  16. /// </summary>
  17. public Rect Box { get; set; } = Rect.zero;
  18. /// <summary>
  19. /// An event that fires when the button is clicked.
  20. /// </summary>
  21. public event EventHandler<bool> OnClick;
  22. public void OnGUI()
  23. {
  24. if (automaticLayout)
  25. {
  26. if (GUILayout.Button(text, Constants.Default.button))
  27. {
  28. OnClick?.Invoke(this, true);
  29. }
  30. }
  31. else
  32. {
  33. if (GUI.Button(Box, text, Constants.Default.button))
  34. {
  35. OnClick?.Invoke(this, true);
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// The button's unique name.
  41. /// </summary>
  42. public string Name { get; private set; }
  43. /// <summary>
  44. /// Whether to display the button.
  45. /// </summary>
  46. public bool Enabled { get; set; } = true;
  47. /// <summary>
  48. /// Initialize a new button with automatic layout.
  49. /// </summary>
  50. /// <param name="text">The text to display on the button.</param>
  51. /// <param name="name">The button's name.</param>
  52. public Button(string text, string name = null)
  53. {
  54. automaticLayout = true;
  55. this.text = text;
  56. if (name == null)
  57. {
  58. this.Name = typeof(Button).FullName + "::" + text;
  59. }
  60. else
  61. {
  62. this.Name = name;
  63. }
  64. IMGUIManager.AddElement(this);
  65. }
  66. /// <summary>
  67. /// Initialize a new button.
  68. /// </summary>
  69. /// <param name="text">The text to display on the button.</param>
  70. /// <param name="box">Rectangular area for the button to use.</param>
  71. /// <param name="name">The button's name.</param>
  72. public Button(string text, Rect box, string name = null) : this(text, name)
  73. {
  74. automaticLayout = false;
  75. this.Box = box;
  76. }
  77. ~Button()
  78. {
  79. IMGUIManager.RemoveElement(this);
  80. }
  81. }
  82. }