InspectableVector2.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. /// <summary>
  5. /// Displays GUI for a serializable property containing a 2D vector.
  6. /// </summary>
  7. public class InspectableVector2 : InspectableField
  8. {
  9. private GUIVector2Field guiField;
  10. private InspectableState state;
  11. /// <summary>
  12. /// Creates a new inspectable 2D vector GUI for the specified property.
  13. /// </summary>
  14. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  15. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  16. /// contain other fields, in which case you should increase this value by one.</param>
  17. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  18. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  19. public InspectableVector2(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  20. : base(title, SerializableProperty.FieldType.Vector2, depth, layout, property)
  21. {
  22. }
  23. /// <inheritoc/>
  24. protected internal override void Initialize(int layoutIndex)
  25. {
  26. if (property.Type == SerializableProperty.FieldType.Vector2)
  27. {
  28. guiField = new GUIVector2Field(new GUIContent(title));
  29. guiField.OnChanged += OnFieldValueChanged;
  30. guiField.OnConfirmed += OnFieldValueConfirm;
  31. guiField.OnFocusLost += OnFieldValueConfirm;
  32. layout.AddElement(layoutIndex, guiField);
  33. }
  34. }
  35. /// <inheritdoc/>
  36. public override InspectableState Refresh(int layoutIndex)
  37. {
  38. if (guiField != null && !guiField.HasInputFocus)
  39. guiField.Value = property.GetValue<Vector2>();
  40. InspectableState oldState = state;
  41. if (state.HasFlag(InspectableState.Modified))
  42. state = InspectableState.NotModified;
  43. return oldState;
  44. }
  45. /// <summary>
  46. /// Triggered when the user changes the field value.
  47. /// </summary>
  48. /// <param name="newValue">New value of the 2D vector field.</param>
  49. private void OnFieldValueChanged(Vector2 newValue)
  50. {
  51. property.SetValue(newValue);
  52. state |= InspectableState.ModifyInProgress;
  53. }
  54. /// <summary>
  55. /// Triggered when the user confirms input in the 2D vector field.
  56. /// </summary>
  57. private void OnFieldValueConfirm()
  58. {
  59. if (state.HasFlag(InspectableState.ModifyInProgress))
  60. state |= InspectableState.Modified;
  61. }
  62. }
  63. }