SceneController.cs 2.9 KB
Newer Older
1
2
using UnityEngine;
using SimpleJSON;
Sebastian Höffner's avatar
Sebastian Höffner committed
3
using System.Collections.Generic;
4
5
6
7
8
9

namespace dlu
{

public class SceneController : MonoBehaviour
{
Sebastian Höffner's avatar
Sebastian Höffner committed
10
11
    public string instructionJson = "instruction.json";
    public string contextJson = "context.json";
12
13
14
    [HideInInspector]
    public SemanticBehaviour[] semanticBehaviours;

15
16
17
18
19
20
    public SceneController()
    {
        JSONNode.VectorContainerType = JSONContainerType.Object;
        JSONNode.QuaternionContainerType = JSONContainerType.Object;
    }

21
22
    public void Start()
    {
23
24
25
26
27
        if (semanticBehaviours == null)
        {
            FindSemanticLabels();
        }

Sebastian Höffner's avatar
Sebastian Höffner committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        // string jsonContent = Resources.Load<TextAsset>(instructionJson).text;
        // JSONArray scene = JSON.Parse(jsonContent) as JSONArray;
        // foreach (JSONObject obj in scene)
        // {
            // SetGameObjectState(obj);
        // }
    }

    // private void SetGameObjectState(JSONObject sceneObject)
    // {
    //     string name = sceneObject["name"];
    //     // Debug.Log($"name {name}");
    //     GameObject go = GameObject.Find($"Objects/{name}");
    //     if (go != null) {
    //         // Debug.Log(go);
    //     }
    // }

    public void FindSemanticLabels()
    {
        this.semanticBehaviours = GameObject.FindObjectsOfType<SemanticBehaviour>();
49
50
    }

Sebastian Höffner's avatar
Sebastian Höffner committed
51
    public JSONArray GenerateContextJSON()
52
    {
Sebastian Höffner's avatar
Sebastian Höffner committed
53
54
55
56
57
58
59
60
61
62
63
64
65
        if (this.semanticBehaviours == null)
        {
            FindSemanticLabels();
        }

        Dictionary<string, JSONObject> sceneDict = new Dictionary<string, JSONObject>();

        JSONArray sceneArray = new JSONArray();
        foreach (SemanticBehaviour sb in this.semanticBehaviours)
        {
            JSONObject jsonObject = null;
            if (!sceneDict.ContainsKey(sb.name))
            {
66
67
68
69
70
71
                jsonObject = new JSONObject();
                jsonObject.Add("is_a", new JSONArray());
                jsonObject.Add("name", sb.GetComponent<InstanceIdentifier>().name);
                jsonObject.Add("components", new JSONObject());

                sceneDict[sb.name] = jsonObject;
Sebastian Höffner's avatar
Sebastian Höffner committed
72
73
74
75
                sceneArray.Add(sceneDict[sb.name]);
            }
            jsonObject = sceneDict[sb.name];
            jsonObject["is_a"].Add(sb.is_a);
76

Sebastian Höffner's avatar
Sebastian Höffner committed
77
78
            switch (sb)
            {
79
                case Entity entity: Handle(jsonObject, entity); break;
Sebastian Höffner's avatar
Sebastian Höffner committed
80
                default:
81
                    Debug.LogWarning($"No Handler for SemanticBehaviour \"{sb.GetType().Name}\"");
Sebastian Höffner's avatar
Sebastian Höffner committed
82
83
                    break;
            }
84
        }
Sebastian Höffner's avatar
Sebastian Höffner committed
85
        return sceneArray;
86
    }
87

88
    private void Handle(JSONObject jsonObject, Entity entity)
89
    {
90
91
92
93
        JSONObject transform = new JSONObject();
        transform["position"] = entity.transform.position;
        transform["orientation"] = entity.transform.rotation;
        jsonObject["components"]["transform"] = transform;
94
    }
95
96
97
}

}