Exemplo n.º 1
0
        private void Open()
        {
            nodes.Clear();
            connections.Clear();

            string path = EditorUtility.OpenFilePanel("Open file", openFilePath, "json");

            if (path.Length > 0)
            {
                openFileName = Path.GetFileName(path);
                openFilePath = path.Substring(0, path.Length - openFileName.Length);

                GUI.changed = true;

                string jsonString = File.ReadAllText(path);

                JSONParserTemplate json = new JSONParserTemplate(jsonString);

                string storyName = json.GetString("storyName");
                openFileName = storyName;
                authorInput  = json.GetString("author");

                #region vẽ các nodes
                string nodesString = json.GetChildJSONString("nodes");

                JSONParserTemplate jsonNodes = new JSONParserTemplate(nodesString);

                for (int i = 0; ; i++)
                {
                    string nodeData = jsonNodes.GetChildJSONString("" + i);

                    if (nodeData == null)
                    {
                        break;
                    }
                    else
                    {
                        JSONParserTemplate nodeJSON = new JSONParserTemplate(nodeData);
                        string             nodeType = nodeJSON.GetString("type");
                        int posX = nodeJSON.GetInt("posX");
                        int posY = nodeJSON.GetInt("posY");
                        int id   = i;
                        if (nodeType.Equals("start"))
                        {
                            StartNode startNode = new StartNode(new Vector2(posX, posY), 100, 100, OnClickOutPoint);
                            startNode.SetId(id);
                            nodes.Add(startNode);
                        }
                        else if (nodeType.Equals("end"))
                        {
                            EndNode endNode = new EndNode(new Vector2(posX, posY), 100, 100, OnClickInPoint);
                            endNode.SetId(id);
                            nodes.Add(endNode);
                        }
                        else if (nodeType.Equals("dialogue"))
                        {
                            StoryNode storyNode = new StoryNode(new Vector2(posX, posY), 200, 50, OnClickInPoint, OnClickOutPoint);
                            storyNode.nameInput     = nodeJSON.GetString("name");
                            storyNode.dialogueInput = nodeJSON.GetString("dialogue");
                            storyNode.SetId(id);
                            nodes.Add(storyNode);
                        }
                    }
                }
                #endregion

                #region Vẽ các connections
                string connectionsString = json.GetChildJSONString("connections");

                JSONParserTemplate jsonConnections = new JSONParserTemplate(connectionsString);

                for (int i = 0; ; i++)
                {
                    string connectionData = jsonConnections.GetChildJSONString("" + i);

                    if (connectionData == null)
                    {
                        break;
                    }
                    else
                    {
                        JSONParserTemplate connectionJSON = new JSONParserTemplate(connectionData);
                        int        fromId     = connectionJSON.GetInt("fromId");
                        int        toId       = connectionJSON.GetInt("toId");
                        StoryNode  outNode    = nodes.Find(x => x.GetId() == fromId);
                        StoryNode  inNode     = nodes.Find(x => x.GetId() == toId);
                        Connection connection = new Connection(inNode.inPoint, outNode.outPoint, OnClickRemoveConnection);
                        connections.Add(connection);
                    }
                }
                #endregion
            }
        }