/** * <summary> * Adds a <see cref="Command"/> to the <see cref="PartCommandList"/>, if no command name is specified it uses <see cref="RainbowCommand"/> * </summary> */ public void AddCommand(PartCommandList part, string name = "rainbow") { if (name == "rainbow") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new RainbowCommand(obj)); } else if (name == "rainbowStationary") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new StatRainbowCommand(obj)); } else if (name == "theaterChaseRainbow") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new TheaterRainbowCommand(obj)); } else if (name == "theaterChase") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new TheaterCommand(obj)); } else if (name == "colorWipe") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new ColorwipeCommand(obj)); } else if (name == "setColor") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new SetColorCommand(obj)); } else if (name == "interpolate") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new InterpolateCommand(obj)); } else if (name == "wait") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new WaitCommand(obj)); } else if (name == "split") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new SplitCommand(obj)); } else if (name == "join") { GameObject obj = Instantiate(commandPrefab, part.listObject.transform); part.Add(new JoinCommand(obj)); } CheckLists(); if (save.autosave) { Save(); } }
/** * <summary> * Adds a new <see cref="PartCommandList"/> to the <see cref="CommandList"/> and the screen * </summary> */ public PartCommandList AddList() { PartCommandList part = new PartCommandList(); part.listObject = Instantiate(partCommandPrefab, commandsObject.transform); commands.lists.Add(part); CheckLists(); return(part); }
/** * <summary> * Parses the <see cref="CommandList"/> with the specified file name to an object * </summary> */ public static CommandList Parse(string name = "base") { Dictionary <string, object> jsonData; // Check if the files Directory exists, if not, create it string dataPath = Application.persistentDataPath + "/commandlists"; if (!Directory.Exists(dataPath)) { Directory.CreateDirectory(dataPath); } // Read from the file, handle cases the specified file, or even the standard file doesnt exist if (File.Exists(dataPath + "/" + name + ".json")) { using (StreamReader sr = new StreamReader(dataPath + "/" + name + ".json")) { jsonData = JsonConvert.DeserializeObject <Dictionary <string, object> >(sr.ReadLine()); } } else if (File.Exists(dataPath + "/base.json")) { using (StreamReader sr = new StreamReader(dataPath + "/base.json")) { jsonData = JsonConvert.DeserializeObject <Dictionary <string, object> >(sr.ReadLine()); } PlayerPrefs.SetString("list", "base"); } else { jsonData = new Dictionary <string, object>(); } // Parse the string to an object CommandList newCommandList; if (jsonData.Count > 0) { // Try get the name of the list if (jsonData.TryGetValue("name", out object dataName)) { newCommandList = new CommandList(name, (string)dataName); } else { newCommandList = new CommandList(name, "Some List"); } // Try get the content of the list if (jsonData.TryGetValue("parts", out object partlistobj)) { // Iterate over each PartCommandList in the JSON data foreach (List <Dictionary <string, object> > part in ((Newtonsoft.Json.Linq.JArray)partlistobj).ToObject <List <List <Dictionary <string, object> > > >()) { PartCommandList newPart = new PartCommandList(); // Iterate over each Command in that list in the JSON data foreach (Dictionary <string, object> cmd in part) { if (cmd.TryGetValue("commandTitle", out object commandTitle)) { // Incase there is a commandTitle specified in JSON, try and get meaning from it Command newCmd = null; switch (commandTitle) { // Incase the title means something, try and get the values for that specific Command case "rainbow": newCmd = new RainbowCommand(); if (cmd.TryGetValue("wait_ms", out object wait_ms)) { ((RainbowCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms); } break; case "rainbowStationary": newCmd = new StatRainbowCommand(); if (cmd.TryGetValue("wait_ms", out wait_ms)) { ((StatRainbowCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms); } break; case "colorWipe": newCmd = new ColorwipeCommand(); if (cmd.TryGetValue("color", out object color)) { ((ColorwipeCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color)); } if (cmd.TryGetValue("wait_ms", out wait_ms)) { ((ColorwipeCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms); } break; case "setColor": newCmd = new SetColorCommand(); if (cmd.TryGetValue("color", out color)) { ((SetColorCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color)); } break; case "theaterChase": newCmd = new TheaterCommand(); if (cmd.TryGetValue("color", out color)) { ((TheaterCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color)); } if (cmd.TryGetValue("wait_ms", out wait_ms)) { ((TheaterCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms); } if (cmd.TryGetValue("iterations", out object iterations)) { ((TheaterCommand)newCmd).iterations = Convert.ToInt32(iterations); } break; case "theaterChaseRainbow": newCmd = new TheaterRainbowCommand(); if (cmd.TryGetValue("wait_ms", out wait_ms)) { ((TheaterRainbowCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms); } break; case "interpolate": newCmd = new InterpolateCommand(); if (cmd.TryGetValue("color", out color)) { ((InterpolateCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color)); } if (cmd.TryGetValue("color2", out object color2)) { ((InterpolateCommand)newCmd).color2 = Helper.ToColor(Convert.ToInt32(color2)); } if (cmd.TryGetValue("duration_ms", out object duration_ms)) { ((InterpolateCommand)newCmd).duration_ms = Convert.ToInt32(duration_ms); } if (cmd.TryGetValue("goback", out object goback)) { ((InterpolateCommand)newCmd).goback = (bool)goback; } break; case "wait": newCmd = new WaitCommand(); if (cmd.TryGetValue("duration_ms", out duration_ms)) { ((WaitCommand)newCmd).duration_ms = Convert.ToInt32(duration_ms); } break; case "split": newCmd = new SplitCommand(); if (cmd.TryGetValue("newList", out object newList)) { ((SplitCommand)newCmd).newList = Convert.ToInt32(newList); } break; case "join": newCmd = new JoinCommand(); if (cmd.TryGetValue("listtojoin", out object listtojoin)) { ((JoinCommand)newCmd).listtojoin = Convert.ToInt32(listtojoin); } if (cmd.TryGetValue("waitlist", out object waitlist)) { ((JoinCommand)newCmd).waitlist = (bool)waitlist; } break; } // Only add to list if the title meant sth if (newCmd != null) { if (cmd.TryGetValue("title", out object title)) { newCmd.title = (string)title; } if (cmd.TryGetValue("lamps", out object lamps)) { newCmd.lamps = ((Newtonsoft.Json.Linq.JArray)lamps).ToObject <List <int> >(); } newPart.AddHidden(newCmd); } } } newCommandList.lists.Add(newPart); } } return(newCommandList); } return(new CommandList()); }