InspectableAABox.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 an AABox.
  11. /// </summary>
  12. [CustomInspector(typeof(AABox))]
  13. public class InspectableAABox : InspectableField
  14. {
  15. private GUIVector3Field centerField;
  16. private GUIVector3Field sizeField;
  17. private InspectableState state;
  18. /// <summary>
  19. /// Creates a new inspectable AABox 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 InspectableAABox(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. /// <inheritoc/>
  34. protected internal override void Initialize(int layoutIndex)
  35. {
  36. GUILayoutX boundsLayout = new GUILayoutX();
  37. centerField = new GUIVector3Field(new LocEdString("Center"), 50);
  38. sizeField = new GUIVector3Field(new LocEdString("Size"), 50);
  39. layout.AddElement(layoutIndex, boundsLayout);
  40. boundsLayout.AddElement(new GUILabel(new LocEdString(title), GUIOption.FixedWidth(100)));
  41. GUILayoutY boundsContent = boundsLayout.AddLayoutY();
  42. boundsContent.AddElement(centerField);
  43. boundsContent.AddElement(sizeField);
  44. centerField.OnChanged += x =>
  45. {
  46. AABox bounds = property.GetValue<AABox>();
  47. Vector3 min = x - bounds.Size * 0.5f;
  48. Vector3 max = x + bounds.Size * 0.5f;
  49. property.SetValue(new AABox(min, max));
  50. state |= InspectableState.ModifyInProgress;
  51. };
  52. centerField.OnConfirmed += OnFieldValueConfirm;
  53. centerField.OnFocusLost += OnFieldValueConfirm;
  54. sizeField.OnChanged += x =>
  55. {
  56. AABox bounds = property.GetValue<AABox>();
  57. Vector3 min = bounds.Center - x * 0.5f;
  58. Vector3 max = bounds.Center + x * 0.5f;
  59. property.SetValue(new AABox(min, max));
  60. state |= InspectableState.ModifyInProgress;
  61. };
  62. sizeField.OnConfirmed += OnFieldValueConfirm;
  63. sizeField.OnFocusLost += OnFieldValueConfirm;
  64. }
  65. /// <inheritdoc/>
  66. public override InspectableState Refresh(int layoutIndex)
  67. {
  68. if ((centerField != null && !centerField.HasInputFocus) &&
  69. (sizeField != null && !sizeField.HasInputFocus))
  70. {
  71. AABox box = property.GetValue<AABox>();
  72. centerField.Value = box.Center;
  73. sizeField.Value = box.Size;
  74. }
  75. InspectableState oldState = state;
  76. if (state.HasFlag(InspectableState.Modified))
  77. state = InspectableState.NotModified;
  78. return oldState;
  79. }
  80. /// <summary>
  81. /// Triggered when the user confirms input in either of the 3D vector fields.
  82. /// </summary>
  83. private void OnFieldValueConfirm()
  84. {
  85. if (state.HasFlag(InspectableState.ModifyInProgress))
  86. state |= InspectableState.Modified;
  87. }
  88. }
  89. /** @} */
  90. }