SceneController.cs 2.44 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
    public void Start()
    {
17
18
19
20
21
        if (semanticBehaviours == null)
        {
            FindSemanticLabels();
        }

Sebastian Höffner's avatar
Sebastian Höffner committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
        // 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>();
43
44
    }

Sebastian Höffner's avatar
Sebastian Höffner committed
45
    public JSONArray GenerateContextJSON()
46
    {
Sebastian Höffner's avatar
Sebastian Höffner committed
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
        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))
            {
                sceneDict[sb.name] = new JSONObject();
                sceneDict[sb.name].Add("is_a", new JSONArray());
                sceneArray.Add(sceneDict[sb.name]);
            }
            jsonObject = sceneDict[sb.name];
            jsonObject["is_a"].Add(sb.is_a);
            switch (sb)
            {
                case InstanceIdentifier identifier: HandleInstanceIdentifier(jsonObject, identifier); break;
                default:
                    Debug.LogWarning("Trying to add SemanticBehaviour to JSON, but no case exists.", sb);
                    break;
            }
73
        }
Sebastian Höffner's avatar
Sebastian Höffner committed
74
        return sceneArray;
75
    }
76

Sebastian Höffner's avatar
Sebastian Höffner committed
77
    private void HandleInstanceIdentifier(JSONObject jsonObject, InstanceIdentifier identifier)
78
    {
Sebastian Höffner's avatar
Sebastian Höffner committed
79
        jsonObject["name"] = identifier.name;
80
    }
81
82
83
}

}