Beispiel #1
0
 void Awake()
 {
     instance         = this;
     activeChipEditor = FindObjectOfType <ChipEditor> ();
     FindObjectOfType <CreateMenu> ().onChipCreatePressed   += SaveAndPackageChip;
     FindObjectOfType <UpdateButton> ().onChipUpdatePressed += UpdateChip;
 }
Beispiel #2
0
    public static void Save(ChipEditor chipEditor)
    {
        ChipSaveData chipSaveData = new ChipSaveData(chipEditor);

        // Generate new chip save string
        var    compositeChip = new SavedChip(chipSaveData);
        string saveString    = JsonUtility.ToJson(compositeChip, usePrettyPrint);

        // Generate save string for wire layout
        var    wiringSystem     = new SavedWireLayout(chipSaveData);
        string wiringSaveString = JsonUtility.ToJson(wiringSystem, usePrettyPrint);

        // Write to file
        string savePath = SaveSystem.GetPathToSaveFile(chipEditor.chipName);

        using (StreamWriter writer = new StreamWriter(savePath))
        {
            writer.Write(saveString);
        }

        string wireLayoutSavePath = SaveSystem.GetPathToWireSaveFile(chipEditor.chipName);

        using (StreamWriter writer = new StreamWriter(wireLayoutSavePath))
        {
            writer.Write(wiringSaveString);
        }
    }
 void RefreshChipEditorReference()
 {
     if (chipEditor == null)
     {
         chipEditor = FindObjectOfType <ChipEditor> ();
     }
 }
Beispiel #4
0
    public static List <Chip> MarkAllCycles(ChipEditor chipEditor)
    {
        var chipsWithCycles = new List <Chip>();

        HashSet <Chip> examinedChips = new HashSet <Chip>();

        Chip[] chips = chipEditor.chipInteraction.allChips.ToArray();

        // Clear all cycle markings
        for (int i = 0; i < chips.Length; i++)
        {
            for (int j = 0; j < chips[i].inputPins.Length; j++)
            {
                chips[i].inputPins[j].cyclic = false;
            }
        }
        // Mark cycles
        for (int i = 0; i < chips.Length; i++)
        {
            examinedChips.Clear();
            currentChipHasCycle = false;
            MarkCycles(chips[i], chips[i], examinedChips);
            if (currentChipHasCycle)
            {
                chipsWithCycles.Add(chips[i]);
            }
        }
        return(chipsWithCycles);
    }
Beispiel #5
0
 void LoadNewEditor()
 {
     if (activeChipEditor)
     {
         Destroy(activeChipEditor.gameObject);
     }
     activeChipEditor = Instantiate(chipEditorPrefab, Vector3.zero, Quaternion.identity);
     activeChipEditor.creationIndex = currentChipCreationIndex;
 }
    void Awake()
    {
        chipEditor           = FindObjectOfType <ChipEditor> ();
        editorDisplayOptions = FindObjectOfType <ChipEditorOptions> ();
        chipEditor.pinAndWireInteraction.onMouseOverPin += OnMouseOverPin;
        chipEditor.pinAndWireInteraction.onMouseExitPin += OnMouseExitPin;

        pinNameDisplays = new List <PinNameDisplay> ();
        pinsToDisplay   = new List <Pin> ();
    }
Beispiel #7
0
    public ChipSaveData(ChipEditor chipEditor)
    {
        List <Chip> componentChipList = new List <Chip> ();

        var sortedInputs = chipEditor.inputsEditor.signals;

        sortedInputs.Sort((a, b) => b.transform.position.y.CompareTo(a.transform.position.y));
        var sortedOutputs = chipEditor.outputsEditor.signals;

        sortedOutputs.Sort((a, b) => b.transform.position.y.CompareTo(a.transform.position.y));

        componentChipList.AddRange(sortedInputs);
        componentChipList.AddRange(sortedOutputs);

        componentChipList.AddRange(chipEditor.chipInteraction.allChips);
        componentChips = componentChipList.ToArray();

        wires          = chipEditor.pinAndWireInteraction.allWires.ToArray();
        chipName       = chipEditor.chipName;
        chipColour     = chipEditor.chipColour;
        chipNameColour = chipEditor.chipNameColour;
        creationIndex  = chipEditor.creationIndex;
    }
Beispiel #8
0
    public void PackageCustomChip(ChipEditor chipEditor)
    {
        gameObject.name = chipEditor.chipName;
        nameText.text   = chipEditor.chipName;
        nameText.color  = chipEditor.chipNameColour;
        SetColour(chipEditor.chipColour);

        // Add and set up the custom chip component
        CustomChip chip = gameObject.AddComponent <CustomChip> ();

        chip.chipName = chipEditor.chipName;

        // Set input signals
        chip.inputSignals = new InputSignal[chipEditor.inputsEditor.signals.Count];
        for (int i = 0; i < chip.inputSignals.Length; i++)
        {
            chip.inputSignals[i] = (InputSignal)chipEditor.inputsEditor.signals[i];
        }

        // Set output signals
        chip.outputSignals = new OutputSignal[chipEditor.outputsEditor.signals.Count];
        for (int i = 0; i < chip.outputSignals.Length; i++)
        {
            chip.outputSignals[i] = (OutputSignal)chipEditor.outputsEditor.signals[i];
        }

        // Create pins and set set package size
        SpawnPins(chip);
        SetSizeAndSpacing(chip);

        // Parent chip holder to the template, and hide
        Transform implementationHolder = chipEditor.chipImplementationHolder;

        implementationHolder.parent        = transform;
        implementationHolder.localPosition = Vector3.zero;
        implementationHolder.gameObject.SetActive(false);
    }
Beispiel #9
0
    public static void Update(ChipEditor chipEditor, Chip chip)
    {
        ChipSaveData chipSaveData = new ChipSaveData(chipEditor);

        // Generate new chip save string
        var    compositeChip = new SavedChip(chipSaveData);
        string saveString    = JsonUtility.ToJson(compositeChip, usePrettyPrint);

        // Generate save string for wire layout
        var    wiringSystem     = new SavedWireLayout(chipSaveData);
        string wiringSaveString = JsonUtility.ToJson(wiringSystem, usePrettyPrint);

        // Write to file
        string savePath = SaveSystem.GetPathToSaveFile(chipEditor.chipName);

        using (StreamWriter writer = new StreamWriter(savePath))
        {
            writer.Write(saveString);
        }

        string wireLayoutSavePath = SaveSystem.GetPathToWireSaveFile(chipEditor.chipName);

        using (StreamWriter writer = new StreamWriter(wireLayoutSavePath))
        {
            writer.Write(wiringSaveString);
        }

        // Update parent chips using this chip
        string currentChipName = chipEditor.chipName;

        SavedChip[] savedChips = SaveSystem.GetAllSavedChips();
        for (int i = 0; i < savedChips.Length; i++)
        {
            if (savedChips[i].componentNameList.Contains(currentChipName))
            {
                int currentChipIndex = Array.FindIndex(savedChips[i].savedComponentChips, c => c.chipName == currentChipName);
                SavedComponentChip updatedComponentChip = new SavedComponentChip(chipSaveData, chip);
                SavedComponentChip oldComponentChip     = savedChips[i].savedComponentChips[currentChipIndex];

                // Update component chip I/O
                for (int j = 0; j < updatedComponentChip.inputPins.Length; j++)
                {
                    for (int k = 0; k < oldComponentChip.inputPins.Length; k++)
                    {
                        if (updatedComponentChip.inputPins[j].name == oldComponentChip.inputPins[k].name)
                        {
                            updatedComponentChip.inputPins[j].parentChipIndex       = oldComponentChip.inputPins[k].parentChipIndex;
                            updatedComponentChip.inputPins[j].parentChipOutputIndex = oldComponentChip.inputPins[k].parentChipOutputIndex;
                            updatedComponentChip.inputPins[j].isCylic = oldComponentChip.inputPins[k].isCylic;
                        }
                    }
                }

                // Write to file
                string parentSaveString = JsonUtility.ToJson(savedChips[i], usePrettyPrint);
                string parentSavePath   = SaveSystem.GetPathToSaveFile(savedChips[i].name);
                using (StreamWriter writer = new StreamWriter(parentSavePath))
                {
                    writer.Write(parentSaveString);
                }
            }
        }
    }
    public static ChipSaveData GetChipSaveData(Chip chip, Chip[] builtinChips, List <Chip> spawnableChips, Wire wirePrefab, ChipEditor chipEditor)
    {
        // @NOTE: chipEditor can be removed here if:
        //     * Chip & wire instatiation is inside their respective implementation holders is inside the chipEditor
        //     * the wire connections are done inside ChipEditor.LoadFromSaveData instead of ChipLoader.LoadChipWithWires

        SavedChip chipToTryLoad;

        SavedChip[] savedChips = SaveSystem.GetAllSavedChips();

        using (StreamReader reader = new StreamReader(SaveSystem.GetPathToSaveFile(chip.name)))
        {
            string chipSaveString = reader.ReadToEnd();
            chipToTryLoad = JsonUtility.FromJson <SavedChip>(chipSaveString);
        }

        if (chipToTryLoad == null)
        {
            return(null);
        }

        SortChipsByOrderOfCreation(ref savedChips);
        // Maintain dictionary of loaded chips (initially just the built-in chips)
        Dictionary <string, Chip> loadedChips = new Dictionary <string, Chip> ();

        for (int i = 0; i < builtinChips.Length; i++)
        {
            Chip builtinChip = builtinChips[i];
            loadedChips.Add(builtinChip.chipName, builtinChip);
        }
        foreach (Chip loadedChip in spawnableChips)
        {
            if (loadedChips.ContainsKey(loadedChip.chipName))
            {
                continue;
            }
            loadedChips.Add(loadedChip.chipName, loadedChip);
        }

        ChipSaveData    loadedChipData = LoadChipWithWires(chipToTryLoad, loadedChips, wirePrefab, chipEditor);
        SavedWireLayout wireLayout     = LoadWiringFile(SaveSystem.GetPathToWireSaveFile(loadedChipData.chipName));

        // Set wires anchor points
        for (int i = 0; i < wireLayout.serializableWires.Length; i++)
        {
            string startPinName = loadedChipData.componentChips[wireLayout.serializableWires[i].parentChipIndex].outputPins[wireLayout.serializableWires[i].parentChipOutputIndex].pinName;
            string endPinName   = loadedChipData.componentChips[wireLayout.serializableWires[i].childChipIndex].inputPins[wireLayout.serializableWires[i].childChipInputIndex].pinName;

            int wireIndex = Array.FindIndex(loadedChipData.wires, w => w.startPin.pinName == startPinName && w.endPin.pinName == endPinName);
            if (wireIndex >= 0)
            {
                loadedChipData.wires[wireIndex].SetAnchorPoints(wireLayout.serializableWires[i].anchorPoints);
            }
        }

        return(loadedChipData);
    }
    static ChipSaveData LoadChipWithWires(SavedChip chipToLoad, Dictionary <string, Chip> previouslyLoadedChips, Wire wirePrefab, ChipEditor chipEditor)
    {
        ChipSaveData loadedChipData = new ChipSaveData();
        int          numComponents  = chipToLoad.savedComponentChips.Length;

        loadedChipData.componentChips = new Chip[numComponents];
        loadedChipData.chipName       = chipToLoad.name;
        loadedChipData.chipColour     = chipToLoad.colour;
        loadedChipData.chipNameColour = chipToLoad.nameColour;
        loadedChipData.creationIndex  = chipToLoad.creationIndex;
        List <Wire> wiresToLoad = new List <Wire>();

        // Spawn component chips (the chips used to create this chip)
        // These will have been loaded already, and stored in the previouslyLoadedChips dictionary
        for (int i = 0; i < numComponents; i++)
        {
            SavedComponentChip componentToLoad = chipToLoad.savedComponentChips[i];
            string             componentName   = componentToLoad.chipName;
            Vector2            pos             = new Vector2((float)componentToLoad.posX, (float)componentToLoad.posY);

            if (!previouslyLoadedChips.ContainsKey(componentName))
            {
                Debug.LogError("Failed to load sub component: " + componentName + " While loading " + chipToLoad.name);
            }

            Chip loadedComponentChip = GameObject.Instantiate(previouslyLoadedChips[componentName], pos, Quaternion.identity, chipEditor.chipImplementationHolder);
            loadedComponentChip.gameObject.SetActive(true);
            loadedChipData.componentChips[i] = loadedComponentChip;

            // Load input pin names
            for (int inputIndex = 0; inputIndex < componentToLoad.inputPins.Length && inputIndex < loadedChipData.componentChips[i].inputPins.Length; inputIndex++)
            {
                loadedChipData.componentChips[i].inputPins[inputIndex].pinName = componentToLoad.inputPins[inputIndex].name;
            }

            // Load output pin names
            for (int ouputIndex = 0; ouputIndex < componentToLoad.outputPins.Length && ouputIndex < loadedChipData.componentChips[i].outputPins.Length; ouputIndex++)
            {
                loadedChipData.componentChips[i].outputPins[ouputIndex].pinName = componentToLoad.outputPins[ouputIndex].name;
            }
        }

        // Connect pins with wires
        for (int chipIndex = 0; chipIndex < chipToLoad.savedComponentChips.Length; chipIndex++)
        {
            Chip loadedComponentChip = loadedChipData.componentChips[chipIndex];
            for (int inputPinIndex = 0; inputPinIndex < loadedComponentChip.inputPins.Length && inputPinIndex < chipToLoad.savedComponentChips[chipIndex].inputPins.Length; inputPinIndex++)
            {
                SavedInputPin savedPin = chipToLoad.savedComponentChips[chipIndex].inputPins[inputPinIndex];
                Pin           pin      = loadedComponentChip.inputPins[inputPinIndex];

                // If this pin should receive input from somewhere, then wire it up to that pin
                if (savedPin.parentChipIndex != -1)
                {
                    Pin connectedPin = loadedChipData.componentChips[savedPin.parentChipIndex].outputPins[savedPin.parentChipOutputIndex];
                    pin.cyclic = savedPin.isCylic;
                    if (Pin.TryConnect(connectedPin, pin))
                    {
                        Wire loadedWire = GameObject.Instantiate(wirePrefab, chipEditor.wireHolder);
                        loadedWire.Connect(connectedPin, pin);
                        wiresToLoad.Add(loadedWire);
                    }
                }
            }
        }

        loadedChipData.wires = wiresToLoad.ToArray();

        return(loadedChipData);
    }