BlockObjectPlacement.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace O3DWB
  6. {
  7. [Serializable]
  8. public class BlockObjectPlacement
  9. {
  10. #region Private Variables
  11. [SerializeField]
  12. private ObjectPlacementBlock _objectPlacementBlock = new ObjectPlacementBlock();
  13. #endregion
  14. #region Public Properties
  15. public List<OrientedBox> AllOrientedBoxesInBlock { get { return _objectPlacementBlock.GetAllOrientedBoxes(); } }
  16. public ObjectPlacementBlockSettings BlockSettings { get { return _objectPlacementBlock.Settings; } }
  17. public ObjectPlacementBlockRenderSettings BlockRenderSettings { get { return _objectPlacementBlock.RenderSettings; } }
  18. public ObjectPlacementExtensionPlaneRenderSettings BlockExtensionPlaneRenderSettings { get { return _objectPlacementBlock.ExtensionPlaneRenderSettings; } }
  19. public bool IsBlockUnderManualConstruction { get { return _objectPlacementBlock.IsUnderManualConstruction; } }
  20. #endregion
  21. #region Public Static Functions
  22. public static BlockObjectPlacement Get()
  23. {
  24. return ObjectPlacement.Get().BlockObjectPlacement;
  25. }
  26. #endregion
  27. #region Public Methods
  28. public void RenderGizmos()
  29. {
  30. _objectPlacementBlock.RenderGizmos();
  31. }
  32. public void RenderHandles()
  33. {
  34. _objectPlacementBlock.RenderHandles();
  35. }
  36. public void CancelManualBlockConstruction()
  37. {
  38. ObjectPlacementGuide.Active = true;
  39. _objectPlacementBlock.CancelManualConstruction();
  40. }
  41. public void HandleMouseMoveEvent(Event e)
  42. {
  43. if (CanAdjustGuidePositionAndRotation())
  44. {
  45. AdjustGuidePositionAndRotation();
  46. // Note: This is necessary just in case the placement guide has changed.
  47. _objectPlacementBlock.SetStartObject(ObjectPlacementGuide.SceneObject);
  48. }
  49. else
  50. if (CanUpdateBlock()) _objectPlacementBlock.UpdateForMouseMoveEvent();
  51. }
  52. public void HandleMouseButtonDownEvent(Event e)
  53. {
  54. if (CanPlaceBlock())
  55. {
  56. e.DisableInSceneView();
  57. PlaceBlock();
  58. }
  59. else
  60. if (CanBeginBlockManualConstruction() && e.InvolvesLeftMouseButton())
  61. {
  62. e.DisableInSceneView();
  63. BeginManualBlockConstruction();
  64. }
  65. }
  66. public void HandleKeyboardButtonDownEvent(Event e)
  67. {
  68. if (AllShortcutCombos.Instance.CancelManualBlockConstruction.IsActive())
  69. {
  70. e.DisableInSceneView();
  71. CancelManualBlockConstruction();
  72. }
  73. else
  74. if (AllShortcutCombos.Instance.ManualRaiseBlock.IsActive())
  75. {
  76. e.DisableInSceneView();
  77. _objectPlacementBlock.ManualRaise();
  78. }
  79. else
  80. if (AllShortcutCombos.Instance.ManualLowerBlock.IsActive())
  81. {
  82. e.DisableInSceneView();
  83. _objectPlacementBlock.ManualLower();
  84. }
  85. else
  86. if (AllShortcutCombos.Instance.NextBlockExtensionPlane.IsActive())
  87. {
  88. e.DisableInSceneView();
  89. _objectPlacementBlock.NextExtensionPlane();
  90. }
  91. }
  92. #endregion
  93. #region Private Methods
  94. private bool CanAdjustGuidePositionAndRotation()
  95. {
  96. return !_objectPlacementBlock.IsUnderManualConstruction && ObjectPlacementGuide.ExistsInSceneAndIsActive;
  97. }
  98. private bool CanUpdateBlock()
  99. {
  100. return ObjectPlacementGuide.ExistsInScene;
  101. }
  102. private bool CanPlaceBlock()
  103. {
  104. return ObjectPlacementGuide.ExistsInScene && _objectPlacementBlock.IsUnderManualConstruction && AllShortcutCombos.Instance.PlaceBlockOnClick.IsActive();
  105. }
  106. private bool CanBeginBlockManualConstruction()
  107. {
  108. return ObjectPlacementGuide.ExistsInScene && !_objectPlacementBlock.IsUnderManualConstruction && AllShortcutCombos.Instance.BeginManualBlockConstruction.IsActive();
  109. }
  110. private void PlaceBlock()
  111. {
  112. List<GameObject> placedHierarchyRoots = Octave3DScene.Get().InstantiateObjectHirarchiesFromPlacementDataCollection(_objectPlacementBlock.EndManualConstruction());
  113. ObjectHierarchyRootsWerePlacedInSceneMessage.SendToInterestedListeners(placedHierarchyRoots, ObjectHierarchyRootsWerePlacedInSceneMessage.PlacementType.ObjectPlacement);
  114. ObjectPlacementGuide.Active = true;
  115. }
  116. private void BeginManualBlockConstruction()
  117. {
  118. _objectPlacementBlock.SetStartObject(ObjectPlacementGuide.SceneObject);
  119. _objectPlacementBlock.BeginManualConstruction();
  120. ObjectPlacementGuide.Active = false;
  121. }
  122. private void AdjustGuidePositionAndRotation()
  123. {
  124. ObjectPlacementGuide.Instance.Snap();
  125. AxisAlignment.AlignObjectAxis(ObjectPlacementGuide.SceneObject, BlockObjectPlacementSettings.Get().PlacementGuideSurfaceAlignmentSettings, ObjectSnapping.Get().ObjectSnapSurfacePlane.normal);
  126. }
  127. #endregion
  128. }
  129. }
  130. #endif