public void DeserializeParts(JArray arr, out uint[] refIDs, out uint[] newIDs) { refIDs = new uint[arr.Count]; newIDs = new uint[arr.Count]; parts.Clear(); int i = 0; foreach (JObject p in arr) { Part part = null; var type = (int)p["Type"]; if (type == 2) { var ch = p["Character"].ToObject <char>(); part = BuiltinCircuit.FromChar(Vector2.Zero, ch); } else if (type == 0) { part = new BasicNeuron(); } else if (type == 1) { part = new EditableCircuit(); } else { throw new Exception(); } var pos = p["Position"]; part.Position = new Vector2((float)pos["x"], (float)pos["y"]); refIDs[i] = (uint)p["ID"]; newIDs[i] = part.ID; parts.Add(part); i++; } for (i = 0; i < parts.Count; i++) { parts[i].Deserialize((JObject)arr[i], refIDs, newIDs, parts.ToArray()); if (parts[i] is EditableCircuit c) { childEditors.Add(c.Editor); } } }
private void SaveLoadCircuit() { if (IsKeyDown(Keys.S)) { if (IsPartOnPos(MousePos, out var p)) { if (p is Circuit c) { using (var sw = new StreamWriter("circuit.json")) using (var jw = new JsonTextWriter(sw)) c.Serialize().WriteTo(jw); } } } if (IsKeyDown(Keys.L)) { using (var sr = new StreamReader("circuit.json")) using (var jr = new JsonTextReader(sr)) { var loaded = JObject.Load(jr); Circuit c = null; if ((int)loaded["Type"] == 1) { c = new EditableCircuit(loaded); c.Position = MousePos; childEditors.Add((c as EditableCircuit).Editor); } else { var ch = loaded["Character"].ToObject <char>(); c = BuiltinCircuit.FromChar(MousePos, ch); } parts.Add(c); } } if (!IsPartOnPos(MousePos, out var _)) { if (IsKeyDown(Keys.A)) { parts.Add(BuiltinCircuit.AndCircuit(MousePos)); } if (IsKeyDown(Keys.N)) { parts.Add(BuiltinCircuit.NotCircuit(MousePos)); } if (IsKeyDown(Keys.O)) { parts.Add(BuiltinCircuit.OrCircuit(MousePos)); } if (IsKeyDown(Keys.R)) { parts.Add(BuiltinCircuit.NorCircuit(MousePos)); } if (IsKeyDown(Keys.X)) { parts.Add(BuiltinCircuit.XorCircuit(MousePos)); } if (IsKeyDown(Keys.B)) { parts.Add(new Parts.Bulb(MousePos)); } if (IsKeyDown(Keys.T)) { parts.Add(new Parts.ToggleSwitch(MousePos)); } } }