InspectableAABox.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.OnValueChanged += 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.OnConfirm += x =>
  53. {
  54. OnFieldValueConfirm();
  55. StartUndo("center." + x.ToString());
  56. };
  57. centerField.OnComponentFocusChanged += (focus, comp) =>
  58. {
  59. if (focus)
  60. StartUndo("center." + comp.ToString());
  61. else
  62. OnFieldValueConfirm();
  63. };
  64. sizeField.OnValueChanged += x =>
  65. {
  66. AABox bounds = property.GetValue<AABox>();
  67. Vector3 min = bounds.Center - x * 0.5f;
  68. Vector3 max = bounds.Center + x * 0.5f;
  69. property.SetValue(new AABox(min, max));
  70. state |= InspectableState.ModifyInProgress;
  71. };
  72. sizeField.OnConfirm += x =>
  73. {
  74. OnFieldValueConfirm();
  75. StartUndo("size." + x.ToString());
  76. };
  77. sizeField.OnComponentFocusChanged += (focus, comp) =>
  78. {
  79. if (focus)
  80. StartUndo("size." + comp.ToString());
  81. else
  82. OnFieldValueConfirm();
  83. };
  84. }
  85. /// <inheritdoc/>
  86. public override InspectableState Refresh(int layoutIndex)
  87. {
  88. if ((centerField != null && !centerField.HasInputFocus) &&
  89. (sizeField != null && !sizeField.HasInputFocus))
  90. {
  91. AABox box = property.GetValue<AABox>();
  92. centerField.Value = box.Center;
  93. sizeField.Value = box.Size;
  94. }
  95. InspectableState oldState = state;
  96. if (state.HasFlag(InspectableState.Modified))
  97. state = InspectableState.NotModified;
  98. return oldState;
  99. }
  100. /// <inheritdoc />
  101. public override void SetHasFocus(string subFieldName = null)
  102. {
  103. if (subFieldName != null && subFieldName.StartsWith("center."))
  104. {
  105. string component = subFieldName.Remove(0, "center.".Length);
  106. if (component == "X")
  107. centerField.SetInputFocus(VectorComponent.X, true);
  108. else if (component == "Y")
  109. centerField.SetInputFocus(VectorComponent.Y, true);
  110. else if (component == "Z")
  111. centerField.SetInputFocus(VectorComponent.Z, true);
  112. else
  113. centerField.SetInputFocus(VectorComponent.X, true);
  114. }
  115. if (subFieldName != null && subFieldName.StartsWith("size."))
  116. {
  117. string component = subFieldName.Remove(0, "size.".Length);
  118. if (component == "X")
  119. sizeField.SetInputFocus(VectorComponent.X, true);
  120. else if (component == "Y")
  121. sizeField.SetInputFocus(VectorComponent.Y, true);
  122. else if (component == "Z")
  123. sizeField.SetInputFocus(VectorComponent.Z, true);
  124. else
  125. sizeField.SetInputFocus(VectorComponent.X, true);
  126. }
  127. }
  128. /// <summary>
  129. /// Triggered when the user confirms input in either of the 3D vector fields.
  130. /// </summary>
  131. private void OnFieldValueConfirm()
  132. {
  133. if (state.HasFlag(InspectableState.ModifyInProgress))
  134. state |= InspectableState.Modified;
  135. EndUndo();
  136. }
  137. }
  138. /** @} */
  139. }