InspectableRadian.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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="context">Context shared by all inspectable fields created by the same parent.</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(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  30. SerializableProperty property, InspectableFieldStyleInfo style)
  31. : base(context, 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 += () =>
  50. {
  51. OnFieldValueConfirm();
  52. StartUndo();
  53. };
  54. guiFloatField.OnFocusLost += OnFieldValueConfirm;
  55. guiFloatField.OnFocusGained += StartUndo;
  56. layout.AddElement(layoutIndex, guiFloatField);
  57. }
  58. }
  59. /// <inheritdoc/>
  60. public override InspectableState Refresh(int layoutIndex)
  61. {
  62. if (guiFloatField != null && !guiFloatField.HasInputFocus)
  63. guiFloatField.Value = property.GetValue<Radian>().Radians;
  64. InspectableState oldState = state;
  65. if (state.HasFlag(InspectableState.Modified))
  66. state = InspectableState.NotModified;
  67. return oldState;
  68. }
  69. /// <inheritdoc />
  70. public override void SetHasFocus(string subFieldName = null)
  71. {
  72. guiFloatField.Focus = true;
  73. }
  74. /// <summary>
  75. /// Triggered when the user inputs a new floating point value.
  76. /// </summary>
  77. /// <param name="newValue">New value of the float field.</param>
  78. private void OnFieldValueChanged(float newValue)
  79. {
  80. property.SetValue(new Radian(newValue));
  81. state |= InspectableState.ModifyInProgress;
  82. }
  83. /// <summary>
  84. /// Triggered when the user confirms input in the float field.
  85. /// </summary>
  86. private void OnFieldValueConfirm()
  87. {
  88. if (state.HasFlag(InspectableState.ModifyInProgress))
  89. state |= InspectableState.Modified;
  90. EndUndo();
  91. }
  92. }
  93. /** @} */
  94. }