-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSofaContextEditor.cs
More file actions
197 lines (169 loc) · 6.9 KB
/
SofaContextEditor.cs
File metadata and controls
197 lines (169 loc) · 6.9 KB
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
using UnityEditor;
using UnityEngine;
using SofaUnity;
using System.Collections;
using System.Collections.Generic;
namespace SofaUnity
{
/// <summary>
/// Editor Class to define the creation and UI of SofaContext GameObject
/// </summary>
[CustomEditor(typeof(SofaContext))]
public class SofaContextEditor : Editor
{
/// <summary>
/// Add SofaContext Object creation to the SofaUnity Menu
/// </summary>
/// <returns>Pointer to the SofaContext GameObject</returns>
[MenuItem("Tools/SofaUnity/SofaContext")]
[MenuItem("GameObject/Tools/SofaUnity/SofaContext")] //right click menu
public static GameObject CreateNew()
{
int cpt = 0;
if (GameObject.FindObjectOfType<SofaContext>() != null)
{
Debug.LogWarning("The Scene already includes a SofaContext. Only one context is possible for the moment.");
cpt++;
return null;
}
//GameObject go = new GameObject("SofaContext_" + cpt.ToString());
GameObject go = new GameObject("SofaContext");
go.AddComponent<SofaContext>();
return go;
}
/// <summary>
/// Create Sofa logo for the Editor Menu
/// </summary>
private static Texture2D m_SofaLogo;
public static Texture2D SofaLogo
{
get
{
if (m_SofaLogo == null)
{
Object logo = Resources.Load("icons/sofa_sprite");
if (logo == null)
Debug.LogError("logo not found");
m_SofaLogo = (Texture2D)logo;
}
return m_SofaLogo;
}
}
/// <summary>
/// Method to set the UI of the SofaContext GameObject
/// </summary>
public override void OnInspectorGUI()
{
SofaContext context = (SofaContext)this.target;
// Add Sofa Logo
GUIStyle logoGUIStyle = new GUIStyle();
logoGUIStyle.border = new RectOffset(0, 0, 0, 0);
EditorGUILayout.LabelField(new GUIContent(SofaLogo), GUILayout.MinHeight(100.0f), GUILayout.ExpandWidth(true));
// Add field for gravity
context.Gravity = EditorGUILayout.Vector3Field("Gravity", context.Gravity);
EditorGUILayout.Separator();
// Add field for timestep
context.TimeStep = EditorGUILayout.FloatField("TimeStep", context.TimeStep);
EditorGUILayout.Separator();
{
if (Application.isPlaying)
{
EditorGUI.BeginDisabledGroup(true);
context.AsyncSimulation = EditorGUILayout.Toggle("Asynchronous Simulation", context.AsyncSimulation);
EditorGUI.EndDisabledGroup();
}
else
{
EditorGUI.BeginDisabledGroup(true);
context.AsyncSimulation = EditorGUILayout.Toggle("Asynchronous Simulation", context.AsyncSimulation);
EditorGUI.EndDisabledGroup();
}
context.CatchSofaMessages = EditorGUILayout.Toggle("Activate Sofa Logs", context.CatchSofaMessages);
context.IsSofaUpdating = EditorGUILayout.Toggle("Animate SOFA simulation", context.IsSofaUpdating);
EditorGUILayout.Separator();
if (GUILayout.Button("Step"))
{
context.StepbyStep = true;
context.IsSofaUpdating = true;
}
}
EditorGUILayout.Separator();
EditorGUILayout.Separator();
// Add scene file section
SceneFileSection(context);
EditorGUILayout.Separator();
EditorGUILayout.Separator();
// Add plugin section
PluginSection(context);
if (GUI.changed)
{
EditorUtility.SetDirty(context);
}
}
bool showPlugins = true;
void PluginSection(SofaContext context)
{
if (context.PluginManagerInterface == null)
{
EditorGUILayout.IntField("Plugins Count", 0);
return;
}
showPlugins = EditorGUILayout.Foldout(showPlugins, "Plugin List");
if (showPlugins)
{
EditorGUI.BeginChangeCheck();
List<PluginInfo> plugins = context.PluginManagerInterface.GetPluginList();
EditorGUI.indentLevel += 1;
foreach (PluginInfo plug in plugins)
{
EditorGUI.BeginDisabledGroup(!plug.IsAvailable);
plug.IsEnable = EditorGUILayout.Toggle(plug.Name, plug.IsEnable);
EditorGUI.EndDisabledGroup();
}
EditorGUI.indentLevel -= 1;
if (EditorGUI.EndChangeCheck())
{
context.PluginManagerInterface.LoadPlugins();
}
}
else
{
int nbrPlugin = EditorGUILayout.IntField("Plugins Count", context.PluginManagerInterface.GetNbrPlugins());
}
if (GUILayout.Button("Clear saved plugins"))
{
context.PluginManagerInterface.ClearSavedPlugin();
}
}
void SceneFileSection(SofaContext context)
{
EditorGUILayout.Separator();
if (context.SceneFileMgr == null)
{
EditorGUILayout.LabelField("No scene file manager available");
return;
}
// Add Button to load a filename
if (GUILayout.Button("Load SOFA Scene (.scn) file"))
{
string absolutePath = EditorUtility.OpenFilePanel("Load file scene (*.scn)", Application.dataPath, "scn");
if (absolutePath.Length > 0)
context.SceneFileMgr.SceneFilename = absolutePath.Substring(Application.dataPath.Length);
EditorGUILayout.Separator();
}
else if (GUILayout.Button("Load SOFA Python Scene (.py) file"))
{
string absolutePath = EditorUtility.OpenFilePanel("Load file scene (*.py)", "", "py");
if (absolutePath.Length > 0)
context.SceneFileMgr.PythonSceneFilename = absolutePath.Substring(Application.dataPath.Length);
EditorGUILayout.Separator();
}
EditorGUILayout.Separator();
// Label of the filename loaded
EditorGUILayout.LabelField("Scene Filename: ", context.SceneFileMgr.SceneFilename);
context.UnLoadScene = GUILayout.Button("Unload Scene file");
context.ReloadScene = GUILayout.Button("Reload Sofa Scene file");
EditorGUILayout.Separator();
}
}
}