Skip to content

Commit

Permalink
Fixed so that the Shape Resize Tool is active when its tool button is…
Browse files Browse the repository at this point in the history
… active. Hold left shift for symmetric resize.
  • Loading branch information
nmalg committed Sep 3, 2021
1 parent 74e81a8 commit 195a149
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
63 changes: 46 additions & 17 deletions Editor/AGXUnityEditor/Tools/ShapeResizeTool.cs
@@ -1,4 +1,5 @@
using UnityEngine;
using System.Linq;
using UnityEngine;
using UnityEditor;
using AGXUnity.Collide;
using AGXUnity.Utils;
Expand All @@ -25,11 +26,6 @@ public static bool SupportsShape( Shape shape )
return shape != null && shape.GetUtils() != null;
}

/// <summary>
/// Key code to activate this tool.
/// </summary>
public Utils.KeyHandler ActivateKey { get { return GetKeyHandler( "Activate" ); } }

/// <summary>
/// Key code for symmetric scale/resize.
/// </summary>
Expand All @@ -48,54 +44,87 @@ 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 )
{
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;

utils.UpdateSize( localSizeChange, dir );

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;
}
}
6 changes: 2 additions & 4 deletions Editor/AGXUnityEditor/Tools/ShapeTool.cs
Expand Up @@ -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 );

Expand Down

0 comments on commit 195a149

Please sign in to comment.