EnumControl.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using SRF;
  3. using SRF.UI;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace SRDebugger.UI.Controls.Data
  7. {
  8. public class EnumControl : DataBoundControl
  9. {
  10. protected override void Start()
  11. {
  12. base.Start();
  13. }
  14. protected override void OnBind(string propertyName, Type t)
  15. {
  16. base.OnBind(propertyName, t);
  17. this.Title.text = propertyName;
  18. this.Spinner.interactable = !base.IsReadOnly;
  19. if (this.DisableOnReadOnly != null)
  20. {
  21. foreach (GameObject gameObject in this.DisableOnReadOnly)
  22. {
  23. gameObject.SetActive(!base.IsReadOnly);
  24. }
  25. }
  26. this._names = Enum.GetNames(t);
  27. this._values = Enum.GetValues(t);
  28. string text = string.Empty;
  29. for (int j = 0; j < this._names.Length; j++)
  30. {
  31. if (this._names[j].Length > text.Length)
  32. {
  33. text = this._names[j];
  34. }
  35. }
  36. if (this._names.Length == 0)
  37. {
  38. return;
  39. }
  40. float preferredWidth = this.Value.cachedTextGeneratorForLayout.GetPreferredWidth(text, this.Value.GetGenerationSettings(new Vector2(float.MaxValue, this.Value.preferredHeight)));
  41. this.ContentLayoutElement.preferredWidth = preferredWidth;
  42. }
  43. protected override void OnValueUpdated(object newValue)
  44. {
  45. this._lastValue = newValue;
  46. this.Value.text = newValue.ToString();
  47. }
  48. public override bool CanBind(Type type, bool isReadOnly)
  49. {
  50. return type.IsEnum;
  51. }
  52. private void SetIndex(int i)
  53. {
  54. base.UpdateValue(this._values.GetValue(i));
  55. this.Refresh();
  56. }
  57. public void GoToNext()
  58. {
  59. int num = Array.IndexOf(this._values, this._lastValue);
  60. this.SetIndex(SRMath.Wrap(this._values.Length, num + 1));
  61. }
  62. public void GoToPrevious()
  63. {
  64. int num = Array.IndexOf(this._values, this._lastValue);
  65. this.SetIndex(SRMath.Wrap(this._values.Length, num - 1));
  66. }
  67. private object _lastValue;
  68. private string[] _names;
  69. private Array _values;
  70. [RequiredField]
  71. public LayoutElement ContentLayoutElement;
  72. public GameObject[] DisableOnReadOnly;
  73. [RequiredField]
  74. public SRSpinner Spinner;
  75. [RequiredField]
  76. public Text Title;
  77. [RequiredField]
  78. public Text Value;
  79. }
  80. }