InspectableDegree.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2019 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  5. {
  6. /** @addtogroup Inspector
  7. * @{
  8. */
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing a value in degrees.
  11. /// </summary>
  12. [CustomInspector(typeof(Degree))]
  13. public class InspectableDegree : InspectableField
  14. {
  15. private GUIFloatField guiFloatField;
  16. private GUISliderField guiSliderField;
  17. private InspectableState state;
  18. private InspectableFieldStyleInfo style;
  19. /// <summary>
  20. /// Creates a new inspectable float GUI for the specified property.
  21. /// </summary>
  22. /// <param name="context">Context shared by all inspectable fields created by the same parent.</param>
  23. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  24. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  25. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  26. /// contain other fields, in which case you should increase this value by one.</param>
  27. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  28. /// <param name="property">Serializable property referencing the field whose contents to display.</param>
  29. /// <param name="style">Information that can be used for customizing field rendering and behaviour.</param>
  30. public InspectableDegree(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  31. SerializableProperty property, InspectableFieldStyleInfo style)
  32. : base(context, title, path, SerializableProperty.FieldType.Object, depth, layout, property)
  33. {
  34. this.style = style;
  35. }
  36. /// <inheritoc/>
  37. protected internal override void Initialize(int layoutIndex)
  38. {
  39. if (property != null)
  40. {
  41. bool isSlider = style?.RangeStyle != null && style.RangeStyle.Slider;
  42. if (isSlider)
  43. {
  44. guiSliderField = new GUISliderField(style.RangeStyle.Min, style.RangeStyle.Max, new GUIContent(title));
  45. if (style.StepStyle != null && style.StepStyle.Step != 0)
  46. guiSliderField.Step = style.StepStyle.Step;
  47. guiSliderField.OnChanged += OnFieldValueChanged;
  48. guiSliderField.OnFocusLost += OnFieldValueConfirm;
  49. guiSliderField.OnFocusGained += StartUndo;
  50. layout.AddElement(layoutIndex, guiSliderField);
  51. }
  52. else
  53. {
  54. guiFloatField = new GUIFloatField(new GUIContent(title));
  55. if (style != null)
  56. {
  57. if (style.StepStyle != null && style.StepStyle.Step != 0)
  58. guiFloatField.Step = style.StepStyle.Step;
  59. if (style.RangeStyle != null)
  60. guiFloatField.SetRange(style.RangeStyle.Min, style.RangeStyle.Max);
  61. }
  62. guiFloatField.OnChanged += OnFieldValueChanged;
  63. guiFloatField.OnConfirmed += () =>
  64. {
  65. OnFieldValueConfirm();
  66. StartUndo();
  67. };
  68. guiFloatField.OnFocusLost += OnFieldValueConfirm;
  69. guiFloatField.OnFocusGained += StartUndo;
  70. layout.AddElement(layoutIndex, guiFloatField);
  71. }
  72. }
  73. }
  74. /// <inheritdoc/>
  75. public override InspectableState Refresh(int layoutIndex, bool force = false)
  76. {
  77. if (guiFloatField != null)
  78. {
  79. if ((!guiFloatField.HasInputFocus || force))
  80. guiFloatField.Value = property.GetValue<Degree>().Degrees;
  81. }
  82. else if (guiSliderField != null)
  83. {
  84. if ((!guiSliderField.HasInputFocus || force))
  85. guiSliderField.Value = property.GetValue<Degree>().Degrees;
  86. }
  87. InspectableState oldState = state;
  88. if (state.HasFlag(InspectableState.Modified))
  89. state = InspectableState.NotModified;
  90. return oldState;
  91. }
  92. /// <inheritdoc />
  93. public override void SetHasFocus(string subFieldName = null)
  94. {
  95. if (guiFloatField != null)
  96. guiFloatField.Focus = true;
  97. else if (guiSliderField != null)
  98. guiSliderField.Focus = true;
  99. }
  100. /// <summary>
  101. /// Triggered when the user inputs a new floating point value.
  102. /// </summary>
  103. /// <param name="newValue">New value of the float field.</param>
  104. private void OnFieldValueChanged(float newValue)
  105. {
  106. property.SetValue(new Degree(newValue));
  107. state |= InspectableState.ModifyInProgress;
  108. }
  109. /// <summary>
  110. /// Triggered when the user confirms input in the float field.
  111. /// </summary>
  112. private void OnFieldValueConfirm()
  113. {
  114. if (state.HasFlag(InspectableState.ModifyInProgress))
  115. state |= InspectableState.Modified;
  116. EndUndo();
  117. }
  118. }
  119. /** @} */
  120. }