Skip to content

Commit 58c9660

Browse files
[DynamicData] Add dynamic data folder with multiple scripts to manipulate SOFA Data using UI (#225)
1 parent 8ba94c5 commit 58c9660

11 files changed

Lines changed: 4468 additions & 0 deletions

Core/Scripts/UI/DataManager.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
using SofaUnity;
7+
using SofaUnityAPI;
8+
using TMPro;
9+
10+
namespace SofaUnityXR
11+
{
12+
/// <summary>
13+
/// Data types
14+
/// </summary>
15+
public enum SofaDataType
16+
{
17+
Int,
18+
Float,
19+
Double,
20+
Bool,
21+
Vectord,
22+
Vec3
23+
}
24+
25+
[System.Serializable]
26+
public class SofaDataReference
27+
{
28+
[Header("Give EditorLink OR UniqueId :")]
29+
public SofaBaseComponent sofaComponent;
30+
public string UniqueId;
31+
[Header("Data Name and type :")]
32+
public string dataName;
33+
public SofaDataType dataType;
34+
public string optionalCustomName;
35+
[Header("Is +/- 20% if not set :")]
36+
public float MIN;
37+
public float MAX;
38+
39+
public SofaDataReference(SofaBaseComponent sofaComponent, string dataName, SofaDataType dataType,string uid)
40+
{
41+
this.sofaComponent = sofaComponent;
42+
this.dataName = dataName;
43+
this.dataType = dataType;
44+
this.UniqueId = uid;
45+
}
46+
}
47+
48+
49+
public class DynamicSDataManager : MonoBehaviour
50+
{
51+
[SerializeField] private List<SofaDataReference> DSDataList = new List<SofaDataReference>();
52+
public GameObject UIContainer;
53+
public GameObject DSDataprefab;
54+
public GameObject Vec3_DSDataprefab;
55+
//public SofaBaseComponent BaseCompTest;
56+
57+
58+
59+
60+
void Start()
61+
{
62+
//To get the type and exact name use this line :
63+
//Debug.Log(BaseCompTest.m_impl.LoadAllData());
64+
65+
for (int i = 0; i < DSDataList.Count; i++)
66+
{
67+
CreateUIElement(DSDataList[i]);
68+
}
69+
70+
//manual test :
71+
//SofaDataReference dynamicData = new SofaDataReference(BaseCompTest, "youngModulus", 0);
72+
//CreateUIElement(dynamicData);
73+
}
74+
75+
/// <summary>
76+
/// Creat the ui element with a slider link to the given proprety name and type
77+
/// </summary>
78+
/// </param>
79+
public void CreateUIElement (SofaDataReference data)
80+
{
81+
if (data.sofaComponent == null && (string.IsNullOrEmpty(data.UniqueId)))
82+
{
83+
//No Link No Id
84+
Debug.LogError("DynamicSDataManager: missing name or id");
85+
86+
}
87+
else if (!string.IsNullOrEmpty(data.UniqueId) && data.sofaComponent != null)
88+
{
89+
//Link and Id
90+
FindSofaComponentInObjectById(data);
91+
92+
}else if (!string.IsNullOrEmpty(data.UniqueId) && data.sofaComponent == null)
93+
{
94+
//No Link Just Id
95+
FindSofaComponentInScene(data);
96+
97+
}
98+
99+
if (data.sofaComponent == null) {
100+
return;
101+
}
102+
103+
SofaBaseComponent SBcomp = data.sofaComponent;
104+
string dataName = data.dataName;
105+
SofaDataType dataType = data.dataType;
106+
107+
if (UIContainer == null || DSDataprefab == null)
108+
{
109+
Debug.LogError("DynamicSDataManager: missing reference");
110+
return;
111+
}
112+
113+
GameObject instance;
114+
if (dataType == SofaDataType.Vec3)
115+
{
116+
instance = Instantiate(Vec3_DSDataprefab);
117+
instance.transform.SetParent(UIContainer.transform, false);
118+
DynamicSdata[] components = instance.GetComponents<DynamicSdata>();
119+
120+
//To help you undersant what happend :
121+
//DynamicSdata X_DS= components[0];
122+
//DynamicSdata Y_DS = components[1];
123+
//DynamicSdata Z_DS = components[2];
124+
125+
for(int i = 0; i < components.Length; i++)
126+
{
127+
components[i].MIN= data.MIN;
128+
components[i].MAX= data.MAX;
129+
components[i].SetDataName(dataName);
130+
components[i].SetDataType(dataType);
131+
components[i].DynamicSdataSetup(SBcomp);
132+
}
133+
134+
135+
}
136+
else
137+
{
138+
instance = Instantiate(DSDataprefab);
139+
instance.transform.SetParent(UIContainer.transform, false);
140+
DynamicSdata dynamicSdata = instance.GetComponent<DynamicSdata>();
141+
if (dynamicSdata == null)
142+
return;
143+
144+
145+
dynamicSdata.SetDataName(dataName);
146+
dynamicSdata.SetDataType(dataType);
147+
dynamicSdata.MIN = data.MIN;
148+
dynamicSdata.MAX = data.MAX;
149+
150+
if (string.IsNullOrEmpty(data.optionalCustomName))
151+
{
152+
dynamicSdata.SetUIName(dataName);//No custom name so default
153+
}
154+
else
155+
{
156+
dynamicSdata.SetUIName(data.optionalCustomName);
157+
}
158+
159+
dynamicSdata.DynamicSdataSetup(SBcomp);
160+
}
161+
}
162+
163+
/// <summary>
164+
/// Finding element the hard way by looking at every sofaComponent in the scene
165+
/// cost in time
166+
/// </summary>
167+
/// <param name="data"></param>
168+
void FindSofaComponentInScene(SofaDataReference data)
169+
{
170+
SofaBaseComponent[] allBaseComponents = FindObjectsOfType<SofaBaseComponent>();
171+
172+
bool found = false;
173+
174+
foreach (SofaBaseComponent thisComponent in allBaseComponents)
175+
{
176+
if (thisComponent.UniqueNameId == data.UniqueId)
177+
{
178+
data.sofaComponent = thisComponent;
179+
found = true;
180+
break;
181+
}
182+
}
183+
184+
if (!found)
185+
{
186+
Debug.LogError("CreateUIElement: can't find unique id " + data.UniqueId);
187+
188+
}
189+
}
190+
191+
/// <summary>
192+
/// Look for all Base component in a given object,
193+
/// sould be use for sofaobject with multiple SofaComponents
194+
/// </summary>
195+
/// <param name="data"></param>
196+
void FindSofaComponentInObjectById(SofaDataReference data)
197+
{
198+
GameObject go = data.sofaComponent.gameObject;
199+
List<SofaBaseComponent> baseComponents = new List<SofaBaseComponent>();
200+
go.GetComponents(baseComponents);
201+
bool found = false;
202+
203+
foreach (SofaBaseComponent thisComponent in baseComponents)
204+
{
205+
if (thisComponent.UniqueNameId == data.UniqueId)
206+
{
207+
data.sofaComponent = thisComponent;
208+
found = true;
209+
break;
210+
}
211+
}
212+
if (!found)
213+
{
214+
Debug.LogError("CreateUIElement: can't find unique id " + data.UniqueId);
215+
216+
}
217+
}
218+
219+
}//end class
220+
}// end namespace

Core/Scripts/UI/DataManager/DynamicSDataManager.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)