From 195a14999eb1b7537e59d4f3d758cbb2924e7e02 Mon Sep 17 00:00:00 2001 From: Niklas Melin Date: Fri, 3 Sep 2021 13:29:42 +0200 Subject: [PATCH] Fixed so that the Shape Resize Tool is active when its tool button is active. Hold left shift for symmetric resize. --- .../AGXUnityEditor/Tools/ShapeResizeTool.cs | 63 ++++++++++++++----- Editor/AGXUnityEditor/Tools/ShapeTool.cs | 6 +- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/Editor/AGXUnityEditor/Tools/ShapeResizeTool.cs b/Editor/AGXUnityEditor/Tools/ShapeResizeTool.cs index 5ed983f6..c12c1aae 100644 --- a/Editor/AGXUnityEditor/Tools/ShapeResizeTool.cs +++ b/Editor/AGXUnityEditor/Tools/ShapeResizeTool.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System.Linq; +using UnityEngine; using UnityEditor; using AGXUnity.Collide; using AGXUnity.Utils; @@ -25,11 +26,6 @@ public static bool SupportsShape( Shape shape ) return shape != null && shape.GetUtils() != null; } - /// - /// Key code to activate this tool. - /// - public Utils.KeyHandler ActivateKey { get { return GetKeyHandler( "Activate" ); } } - /// /// Key code for symmetric scale/resize. /// @@ -48,24 +44,38 @@ public static bool SupportsShape( Shape shape ) public ShapeResizeTool( Shape shape ) : base( isSingleInstanceTool: true ) { - AddKeyHandler( "Activate", new Utils.KeyHandler( KeyCode.LeftControl ) ); - AddKeyHandler( "Symmetric", new Utils.KeyHandler( KeyCode.LeftControl, KeyCode.LeftShift ) ); + AddKeyHandler( "Symmetric", new Utils.KeyHandler( KeyCode.LeftShift ) ); Shape = shape; } + public override void OnAdd() + { + HideDefaultHandlesEnableWhenRemoved(); + SizeUpdated = false; + } + + public override void OnRemove() + { + if ( SizeUpdated ) + OnSizeUpdatedUpdateMassProperties(); + } + public override void OnSceneViewGUI( SceneView sceneView ) { if ( RemoveOnKeyEscape && Manager.KeyEscapeDown ) { - EditorUtility.SetDirty( Shape ); + // Avoiding delay of this tool being active in the Inspector. + // It's not enough to dirty our Shape because this tool could + // be activated in an recursive editor. + if ( Selection.activeGameObject != null ) + EditorUtility.SetDirty( Selection.activeGameObject ); PerformRemoveFromParent(); return; } - if ( ActivateKey.IsDown || SymmetricScaleKey.IsDown ) - Update( SymmetricScaleKey.IsDown ); + Update( SymmetricScaleKey.IsDown ); } private void Update( bool symmetricScale ) @@ -73,20 +83,23 @@ private void Update( bool symmetricScale ) if ( Shape == null ) return; - ShapeUtils utils = Shape.GetUtils(); + var utils = Shape.GetUtils(); if ( utils == null ) return; + if ( SizeUpdated && EditorApplication.timeSinceStartup - LastChangeTime > 0.333 ) + OnSizeUpdatedUpdateMassProperties(); + Undo.RecordObject( Shape, "ShapeResizeTool" ); Undo.RecordObject( Shape.transform, "ShapeResizeToolTransform" ); - Color color = Color.gray; - float scale = 0.35f; + var color = Color.gray; + var scale = 0.35f; foreach ( ShapeUtils.Direction dir in System.Enum.GetValues( typeof( ShapeUtils.Direction ) ) ) { - Vector3 delta = DeltaSliderTool( utils.GetWorldFace( dir ), utils.GetWorldFaceDirection( dir ), color, scale ); + var delta = DeltaSliderTool( utils.GetWorldFace( dir ), utils.GetWorldFaceDirection( dir ), color, scale ); if ( delta.magnitude > 1.0E-5f ) { - Vector3 localSizeChange = Shape.transform.InverseTransformDirection( delta ); - Vector3 localPositionDelta = 0.5f * localSizeChange; + var localSizeChange = Shape.transform.InverseTransformDirection( delta ); + var localPositionDelta = 0.5f * localSizeChange; if ( !symmetricScale && utils.IsHalfSize( dir ) ) localSizeChange *= 0.5f; @@ -94,8 +107,24 @@ private void Update( bool symmetricScale ) if ( !symmetricScale ) Shape.transform.position += Shape.transform.TransformDirection( localPositionDelta ); + + SizeUpdated = true; + LastChangeTime = EditorApplication.timeSinceStartup; } } } + + private void OnSizeUpdatedUpdateMassProperties() + { + var rb = Shape.RigidBody; + if ( rb != null ) { + rb.UpdateMassProperties(); + EditorUtility.SetDirty( rb ); + } + SizeUpdated = false; + } + + private bool SizeUpdated { get; set; } = false; + private double LastChangeTime { get; set; } = 0.0; } } diff --git a/Editor/AGXUnityEditor/Tools/ShapeTool.cs b/Editor/AGXUnityEditor/Tools/ShapeTool.cs index b8dab4c4..e73d5c20 100644 --- a/Editor/AGXUnityEditor/Tools/ShapeTool.cs +++ b/Editor/AGXUnityEditor/Tools/ShapeTool.cs @@ -29,10 +29,8 @@ public bool ShapeResizeTool if ( value && !ShapeResizeTool ) { RemoveAllChildren(); - var shapeResizeTool = new ShapeResizeTool( Shape ); - shapeResizeTool.ActivateKey.HideDefaultHandlesWhenIsDown = true; - shapeResizeTool.SymmetricScaleKey.HideDefaultHandlesWhenIsDown = true; - shapeResizeTool.RemoveOnKeyEscape = true; + var shapeResizeTool = new ShapeResizeTool( Shape ); + shapeResizeTool.RemoveOnKeyEscape = true; AddChild( shapeResizeTool );