InspectableRadian.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2019 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /** @addtogroup Inspector
  7. * @{
  8. */
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing a value in radians.
  11. /// </summary>
  12. [CustomInspector(typeof(Radian))]
  13. public class InspectableRadian : InspectableField
  14. {
  15. private GUIFloatField guiFloatField;
  16. private InspectableState state;
  17. private InspectableFieldStyleInfo style;
  18. /// <summary>
  19. /// Creates a new inspectable float GUI for the specified property.
  20. /// </summary>
  21. /// <param name="parent">Parent Inspector this field belongs to.</param>
  22. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  23. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  24. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  25. /// contain other fields, in which case you should increase this value by one.</param>
  26. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  27. /// <param name="property">Serializable property referencing the field whose contents to display.</param>
  28. /// <param name="style">Information that can be used for customizing field rendering and behaviour.</param>
  29. public InspectableRadian(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  30. SerializableProperty property, InspectableFieldStyleInfo style)
  31. : base(parent, title, path, SerializableProperty.FieldType.Object, depth, layout, property)
  32. {
  33. this.style = style;
  34. }
  35. /// <inheritoc/>
  36. protected internal override void Initialize(int layoutIndex)
  37. {
  38. if (property != null)
  39. {
  40. guiFloatField = new GUIFloatField(new GUIContent(title));
  41. if (style != null)
  42. {
  43. if (style.StepStyle != null && style.StepStyle.Step != 0)
  44. guiFloatField.Step = style.StepStyle.Step;
  45. if (style.RangeStyle != null)
  46. guiFloatField.SetRange(style.RangeStyle.Min, style.RangeStyle.Max);
  47. }
  48. guiFloatField.OnChanged += OnFieldValueChanged;
  49. guiFloatField.OnConfirmed += OnFieldValueConfirm;
  50. guiFloatField.OnFocusLost += OnFieldValueConfirm;
  51. layout.AddElement(layoutIndex, guiFloatField);
  52. }
  53. }
  54. /// <inheritdoc/>
  55. public override InspectableState Refresh(int layoutIndex)
  56. {
  57. if (guiFloatField != null && !guiFloatField.HasInputFocus)
  58. guiFloatField.Value = property.GetValue<Radian>().Radians;
  59. InspectableState oldState = state;
  60. if (state.HasFlag(InspectableState.Modified))
  61. state = InspectableState.NotModified;
  62. return oldState;
  63. }
  64. /// <summary>
  65. /// Triggered when the user inputs a new floating point value.
  66. /// </summary>
  67. /// <param name="newValue">New value of the float field.</param>
  68. private void OnFieldValueChanged(float newValue)
  69. {
  70. property.SetValue(new Radian(newValue));
  71. state |= InspectableState.ModifyInProgress;
  72. }
  73. /// <summary>
  74. /// Triggered when the user confirms input in the float field.
  75. /// </summary>
  76. private void OnFieldValueConfirm()
  77. {
  78. if (state.HasFlag(InspectableState.ModifyInProgress))
  79. state |= InspectableState.Modified;
  80. }
  81. }
  82. /** @} */
  83. }