示例#1
0
        public SceneTree()
        {
            current = this;

            root      = new SceneNode(this);
            root.name = "Root";
        }
示例#2
0
        public static string SaveToJson(SceneTree instance)
        {
            string rtnData;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (Utf8JsonWriter writer = new Utf8JsonWriter(memoryStream))
                {
                    writer.WriteStartObject();
                    writer.WriteNumber("HighestUuid", instance.uuidManager.GetInternals().highest);

                    writer.WriteStartArray("FreedUuids");
                    foreach (ulong freeId in instance.uuidManager.GetInternals().freed)
                    {
                        writer.WriteNumberValue(freeId);
                    }
                    writer.WriteEndArray();

                    writer.WriteStartArray("Nodes");
                    foreach (SceneNode node in instance.allNodes.Values)
                    {
                        writer.WriteStartObject();
                        node.ToJson(writer);
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();

                    writer.WriteEndObject();
                    writer.Flush();
                }
                rtnData = System.Text.Encoding.UTF8.GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
            }

            return(rtnData);
        }
示例#3
0
        public void SetTree(SceneTree tree)
        {
            this.tree = tree;

            selectedNode = tree.root.uuid;
            openNodes    = new List <ulong>();
        }
示例#4
0
 internal SceneNode(SceneTree tree, ulong id, bool isComp = false)
 {
     isComponent = isComp;
     uuid        = id;
     this.tree   = tree;
     this.tree.allNodes.Add(uuid, this);
 }
示例#5
0
 internal static SceneTree GetCurrent()
 {
     if (current == null)
     {
         current = new SceneTree();
     }
     return(current);
 }
示例#6
0
        internal SceneNode(SceneTree tree = null, bool isComp = false)
        {
            this.tree = (tree == null ? SceneTree.GetCurrent() : tree);
            this.uuid = this.tree.uuidManager.GenerateId();
            this.tree.allNodes.Add(uuid, this);

            this.isComponent = isComp;
        }
示例#7
0
        protected override void Init()
        {
            ObjectDatabase.Init();

            string jsonData = ResourceManager.Global.GetRef("Scenes/ExampleScene.json").Get <TextResource>().resource;

            tree       = SceneTree.LoadFromJson(jsonData);
            visualizer = new TreeVisualizer(tree);
        }
示例#8
0
        public static SceneTree LoadFromJson(string jsonFile)
        {
            SceneTree    localTree = new SceneTree();
            JsonDocument jdoc      = JsonDocument.Parse(jsonFile);
            JsonElement  jdocRoot  = jdoc.RootElement;

            ulong?highestId = null;

            if (jdocRoot.TryGetProperty("HighestUuid", out JsonElement newHighestProperty) &&
                newHighestProperty.TryGetUInt64(out ulong newHighestUuid))
            {
                highestId = newHighestUuid;
            }

            Queue <ulong> newFreeds = new Queue <ulong>();

            if (jdocRoot.TryGetProperty("FreeUuids", out JsonElement newFreedUuidsProperty) &&
                newFreedUuidsProperty.GetArrayLength() > 0)
            {
                foreach (JsonElement freedProp in newFreedUuidsProperty.EnumerateArray())
                {
                    newFreeds.Enqueue(freedProp.GetUInt64());
                }
            }
            localTree.uuidManager = new UuidManager(highestId, newFreeds);

            if (jdocRoot.TryGetProperty("Nodes", out JsonElement nodesProperty))
            {
                foreach (JsonElement element in nodesProperty.EnumerateArray())
                {
                    LoadNodeJson(localTree, element);
                }
            }

            foreach (SceneNode node in localTree.allNodes.Values)
            {
                if (node == localTree.root)
                {
                    continue;
                }

                if (!localTree.allNodes.ContainsKey(node.parent))
                {
                    Log.WriteError("Could not parent SceneNode, parent id not found in tree, removing node.");
                    localTree.FreeNode(node.uuid);
                    continue;
                }

                SceneNode parent = localTree.allNodes[node.parent];
                parent.children.Add(node.uuid);
            }

            return(localTree);
        }
示例#9
0
        protected override void Update(DeltaTime delta)
        {
            if (Input.IsKeyReleased(InputKey.F1))
            {
                renderVisualizer = !renderVisualizer;
            }
            if (Input.IsKeyReleased(InputKey.F2))
            {
                string jsonData = SceneTree.SaveToJson(tree);
                File.WriteAllText("_Data/Scenes/SavedDebugScene.json", jsonData);
            }

            if (renderVisualizer)
            {
                visualizer.Update(delta);
            }
        }
示例#10
0
        private static void LoadNodeJson(SceneTree tree, JsonElement nodeElement)
        {
            ulong uuid        = 0;
            bool  isComponent = false;

            if (nodeElement.TryGetProperty("UUID", out JsonElement idElement) &&
                nodeElement.TryGetProperty("IsComponent", out JsonElement isCompElement))
            {
                uuid        = idElement.GetUInt64();
                isComponent = isCompElement.GetBoolean();
            }
            else
            {
                Log.WriteError("Could not loaded SceneNode, UUID/IsComponent missing. Skipping node (note this may cause further failures in tree).");
                return;
            }

            SceneNode node = new SceneNode(tree, uuid, isComponent);

            node.FromJson(nodeElement);
        }
示例#11
0
        public TreeVisualizer(SceneTree tree)
        {
            bodyFont = ResourceManager.Global.GetRef("Fonts/NotoMono_Regular.ttf");

            SetTree(tree);
        }