/
ShapeTool.cs
209 lines (182 loc) · 7.37 KB
/
ShapeTool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Linq;
using UnityEngine;
using UnityEditor;
using AGXUnity.Collide;
using AGXUnity.Rendering;
using GUI = AGXUnity.Utils.GUI;
using Object = UnityEngine.Object;
namespace AGXUnityEditor.Tools
{
[CustomTool( typeof( Shape ) )]
public class ShapeTool : CustomTargetTool
{
public Shape Shape
{
get
{
return Targets[ 0 ] as Shape;
}
}
public bool ShapeResizeTool
{
get { return GetChild<ShapeResizeTool>() != null; }
set
{
if ( value && !ShapeResizeTool ) {
RemoveAllChildren();
var shapeResizeTool = new ShapeResizeTool( Shape );
shapeResizeTool.RemoveOnKeyEscape = true;
AddChild( shapeResizeTool );
Manager.RequestSceneViewFocus();
}
else if ( !value )
RemoveChild( GetChild<ShapeResizeTool>() );
}
}
public bool DisableCollisionsTool
{
get { return GetChild<DisableCollisionsTool>() != null; }
set
{
if ( value && !DisableCollisionsTool ) {
RemoveAllChildren();
var disableCollisionsTool = new DisableCollisionsTool( Shape.gameObject );
AddChild( disableCollisionsTool );
}
else if ( !value )
RemoveChild( GetChild<DisableCollisionsTool>() );
}
}
public bool ShapeCreateTool
{
get { return GetChild<ShapeCreateTool>() != null; }
set
{
if ( value && !ShapeCreateTool ) {
RemoveAllChildren();
var shapeCreateTool = new ShapeCreateTool( Shape.gameObject );
AddChild( shapeCreateTool );
}
else if ( !value )
RemoveChild( GetChild<ShapeCreateTool>() );
}
}
public bool ShapeVisualCreateTool
{
get { return GetChild<ShapeVisualCreateTool>() != null; }
set
{
if ( value && !ShapeVisualCreateTool ) {
RemoveAllChildren();
var createShapeVisualTool = new ShapeVisualCreateTool( Shape );
AddChild( createShapeVisualTool );
}
else if ( !value )
RemoveChild( GetChild<ShapeVisualCreateTool>() );
}
}
public ShapeTool( Object[] targets )
: base( targets )
{
}
public override void OnAdd()
{
}
public override void OnRemove()
{
}
public override void OnPreTargetMembersGUI()
{
if ( IsMultiSelect )
return;
var skin = InspectorEditor.Skin;
bool toggleShapeResizeTool = false;
bool toggleShapeCreate = false;
bool toggleDisableCollisions = false;
bool toggleShapeVisualCreate = false;
InspectorGUI.ToolButtons( InspectorGUI.ToolButtonData.Create( ToolIcon.ShapeResize,
ShapeResizeTool,
"Shape resize tool",
() => toggleShapeResizeTool = true,
Tools.ShapeResizeTool.SupportsShape( Shape ) ),
InspectorGUI.ToolButtonData.Create( ToolIcon.CreateShapeGivenVisual,
ShapeCreateTool,
"Create shape from visual objects",
() => toggleShapeCreate = true ),
InspectorGUI.ToolButtonData.Create( ToolIcon.DisableCollisions,
DisableCollisionsTool,
"Disable collisions against other objects",
() => toggleDisableCollisions = true ),
InspectorGUI.ToolButtonData.Create( ToolIcon.CreateVisual,
ShapeVisualCreateTool,
"Create visual representation of the physical shape",
() => toggleShapeVisualCreate = true,
Tools.ShapeVisualCreateTool.CanCreateVisual( Shape ) ) );
if ( ShapeCreateTool ) {
GetChild<ShapeCreateTool>().OnInspectorGUI();
}
if ( DisableCollisionsTool ) {
GetChild<DisableCollisionsTool>().OnInspectorGUI();
}
if ( ShapeVisualCreateTool ) {
GetChild<ShapeVisualCreateTool>().OnInspectorGUI();
}
if ( toggleShapeResizeTool )
ShapeResizeTool = !ShapeResizeTool;
if ( toggleShapeCreate )
ShapeCreateTool = !ShapeCreateTool;
if ( toggleDisableCollisions )
DisableCollisionsTool = !DisableCollisionsTool;
if ( toggleShapeVisualCreate )
ShapeVisualCreateTool = !ShapeVisualCreateTool;
}
public override void OnPostTargetMembersGUI()
{
if ( IsMultiSelect )
return;
var shapeVisual = ShapeVisual.Find( Shape );
if ( shapeVisual == null )
return;
var materials = shapeVisual.GetMaterials();
if ( materials.Length > 1 ) {
var names = ( from renderer in shapeVisual.GetComponentsInChildren<MeshRenderer>()
from material in renderer.sharedMaterials
select renderer.name ).ToArray();
var distinctMaterials = materials.Distinct().ToArray();
var isExtended = false;
if ( distinctMaterials.Length == 1 )
isExtended = ShapeVisualMaterialGUI( "Common Render Material",
distinctMaterials[ 0 ],
newMaterial => shapeVisual.SetMaterial( newMaterial ) );
else
isExtended = InspectorGUI.Foldout( EditorData.Instance.GetData( Shape, "Render Materials" ),
GUI.MakeLabel( "Render Materials" ) );
if ( isExtended )
using ( InspectorGUI.IndentScope.Single )
for ( int i = 0; i < materials.Length; ++i ) {
ShapeVisualMaterialGUI( names[ i ],
materials[ i ],
newMaterial => shapeVisual.ReplaceMaterial( i, newMaterial ) );
}
}
else if ( materials.Length == 1 ) {
ShapeVisualMaterialGUI( "Render Material",
materials[ 0 ],
newMaterial => shapeVisual.ReplaceMaterial( 0, newMaterial ) );
}
}
private bool ShapeVisualMaterialGUI( string name, Material material, Action<Material> onNewMaterial )
{
var editorData = EditorData.Instance.GetData( Shape, "Visual_" + name );
var result = InspectorGUI.FoldoutObjectField( GUI.MakeLabel( name ),
material,
typeof( Material ),
editorData,
false ) as Material;
if ( result != material )
onNewMaterial?.Invoke( result );
return editorData.Bool;
}
}
}