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.

68 lines
1.9KB

  1. using System;
  2. using Unity.Mathematics;
  3. using UnityEngine;
  4. namespace TechbloxModdingAPI.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 override 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. /// Initialize a new button with automatic layout.
  41. /// </summary>
  42. /// <param name="text">The text to display on the button.</param>
  43. /// <param name="name">The button's name.</param>
  44. public Button(string text, string name = null) : base(text, name)
  45. {
  46. automaticLayout = true;
  47. this.text = text;
  48. }
  49. /// <summary>
  50. /// Initialize a new button.
  51. /// </summary>
  52. /// <param name="text">The text to display on the button.</param>
  53. /// <param name="box">Rectangular area for the button to use.</param>
  54. /// <param name="name">The button's name.</param>
  55. public Button(string text, Rect box, string name = null) : this(text, name)
  56. {
  57. automaticLayout = false;
  58. this.Box = box;
  59. }
  60. }
  61. }