private void onAddGraphClick()
    {
        //when the user wants to add a graph to the terminal

        EnterTextContent content = new EnterTextContent("Give a unique name to the graph", (string enteredText) =>
        {
            this.terminalController.Terminal.addExtension(new LogicChip(100, 100, enteredText));
            this.refreshExtensionList();
        }, () => { }, 50);

        content.addErrorCheck((string value) =>
        {
            bool result = true;
            int counter = 0;

            while (result && counter < this.terminalController.Terminal.extensionLength())
            {
                if (this.terminalController.Terminal.extensionAt(counter).Name.Equals(value))
                {
                    result = false;
                }

                counter++;
            }

            return(result);
        }, "That name already exists in this terminal");

        Window window = new Window("Add new Graph to " + this.terminalController.Terminal.Name, 200, 200, content);

        this.spawnChildWindow(window);
    }
    public void saveButton()
    {
        //saves the current state of all the terminals

        string path = Application.dataPath + "/Default/Resources/Default/Saves";

        EnterTextContent saveContent = new EnterTextContent("Enter in the name for your save", (string enteredText) => {
            string illegal     = enteredText + "~" + System.DateTime.Now.ToString();
            string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            Regex r            = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
            illegal            = r.Replace(illegal, " ");

            Save.makeDirectory(path, illegal);

            path += "/" + illegal + "/";

            string terminalFolder = "Terminals";
            Save.makeDirectory(path, terminalFolder);

            path += "/" + terminalFolder;


            foreach (TerminalController ter in this.terminalManager.TerminalControllers)
            {
                //makes terminal directory
                Save.makeDirectory(path, ter.Terminal.Name);
                string tempPath = path + "/" + ter.Terminal.Name + "/";

                //saves terminal json
                Save.saveJson <TerminalData>(new TerminalData(ter.Terminal), tempPath, ter.Terminal.Name + ".json");

                //saves the connections
                Save.saveJson <TExtensionConnectionsData>(new TExtensionConnectionsData(ter.Terminal), tempPath, typeof(TExtensionConnectionsData).ToString() + ".json");

                //makes directory for logic graphs
                Save.makeDirectory(tempPath, "LogicGraphs");
                tempPath += "/LogicGraphs";

                for (int i = 0; i < ter.Terminal.extensionLength(); i++)
                {
                    TExtension extension = ter.Terminal.extensionAt(i);
                    if (extension.GetType() == typeof(LogicChip))
                    {
                        string name = extension.Name + ".json";

                        LogicChip lg = (LogicChip)extension;
                        Save.saveJson <LogicGraphData>(new LogicGraphData(lg), tempPath, name);
                    }
                }
            }
        }, () => { }, 50);

        WindowManager.Instance.spawnWindow(new Window("Save Game", 200, 200, saveContent));
    }
Example #3
0
    public override void spawnContents(WindowController windowController, Transform contentPanel, Canvas canvas)
    {
        CameraManager cameraManager = GameObject.Find("CameraManager").GetComponent <CameraManager>();
        Camera        cam           = cameraManager.makeNewCamera(false);

        this.camera = cam;

        Vector2 graphPosition = this.camera.transform.position;

        this.logicGraphController = this.logicGraphManager.displayLogicGraph(this.graph, graphPosition);

        GameObject image = GameObject.Instantiate((GameObject)SceneResouces.SceneObjects["Default"][typeof(GameObject)]["GraphEditor"]);

        this.rawImage = image.GetComponent <RawImage>();
        image.transform.SetParent(contentPanel.transform, false);
        this.rawImageRect = image.GetComponent <RectTransform>();

        //only for testing
        this.mouseObject = new GameObject("Mouse Graph Position");
        this.mouseObject.transform.position = new Vector3(this.camera.transform.position.x, this.camera.transform.position.y, 0);

        this.renderTexture    = new RenderTexture(512, 512, 16);
        this.rawImage.texture = this.renderTexture;

        this.camera.targetTexture = this.renderTexture;
        this.renderTexture.Create();

        LogicGraphCameraController lgcc = this.camera.gameObject.AddComponent <LogicGraphCameraController>();

        lgcc.setUp(graphPosition, this.logicGraphController.Graph, this.inputs, this.rawImage);

        #region GUIButtons

        Transform basePanel = image.transform.Find("Panel");

        Button andButton           = basePanel.Find("AndButton").GetComponent <Button>();
        Button orButton            = basePanel.Find("OrButton").GetComponent <Button>();
        Button notButton           = basePanel.Find("NotButton").GetComponent <Button>();
        Button bufferButton        = basePanel.Find("BufferButton").GetComponent <Button>();
        Button nandButton          = basePanel.Find("NandButton").GetComponent <Button>();
        Button norButton           = basePanel.Find("NorButton").GetComponent <Button>();
        Button xorButton           = basePanel.Find("XorButton").GetComponent <Button>();
        Button xnorButton          = basePanel.Find("XnorButton").GetComponent <Button>();
        Button reflector           = basePanel.Find("ReflectorButton").GetComponent <Button>();
        Button splitterButton      = basePanel.Find("SplitterButton").GetComponent <Button>();
        Button sendBridgeButton    = basePanel.Find("SendBridgeButton").GetComponent <Button>();
        Button receiveBridgeButton = basePanel.Find("ReceiveBridgeButton").GetComponent <Button>();
        Button deleteButton        = basePanel.Find("DeleteButton").GetComponent <Button>();

        //adds the component to the button
        andButton.onClick.AddListener(() => {
            this.component = new AndGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        orButton.onClick.AddListener(() => {
            this.component = new OrGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        notButton.onClick.AddListener(() => {
            this.component = new NotGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        bufferButton.onClick.AddListener(() => {
            this.component = new BufferGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        norButton.onClick.AddListener(() => {
            this.component = new NorGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        xorButton.onClick.AddListener(() => {
            this.component = new XorGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        xnorButton.onClick.AddListener(() => {
            this.component = new XnorGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        nandButton.onClick.AddListener(() => {
            this.component = new NandGate(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        splitterButton.onClick.AddListener(() => {
            this.component = new Splitter(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        reflector.onClick.AddListener(() => {
            this.component = new Reflector(this.previousGridPosition, this.rotation, this.flipped);
            this.changeComponent();
        });
        sendBridgeButton.onClick.AddListener(() => {
            this.component = new GraphOutput(this.previousGridPosition, this.rotation, this.flipped, new ExtensionNode("Blank", ExtensionNode.ExtensionState.SEND));
            this.changeComponent();
        });
        receiveBridgeButton.onClick.AddListener(() => {
            this.component = new GraphInput(this.previousGridPosition, this.rotation, this.flipped, new ExtensionNode("Blank", ExtensionNode.ExtensionState.RECEIVE));
            this.changeComponent();
        });

        deleteButton.onClick.AddListener(() => {
            this.currentState = EditorState.DeleteComponent;
            this.destroyMouseChildren();
            GameObject xSprite = this.makeX().gameObject;
            xSprite.transform.SetParent(this.mouseObject.transform);
            xSprite.transform.localPosition = new Vector2(.5f, .5f);
        });

        //selects the first button
        andButton.onClick.Invoke();
        andButton.Select();

        #endregion

        #region KeyboardInputs
        //rotations
        this.inputs.addInput(new KeyCombination(KeyCode.Q, KeyStatus.Down), () => {
            this.rotation = (rotation + 1) % 4;
            this.changeComponent();
        });
        this.inputs.addInput(new KeyCombination(KeyCode.E, KeyStatus.Down), () => {
            this.rotation = this.rotation - 1;
            if (this.rotation < 0)
            {
                rotation = 3;
            }
            this.changeComponent();
        });

        //flip
        this.inputs.addInput(new KeyCombination(KeyCode.F, KeyStatus.Down), () => {
            this.flipped = !flipped;
            this.changeComponent();
        });

        //mouse rest
        this.inputs.addInput(new KeyCombination(KeyCode.Mouse0, KeyStatus.Rest), () => {
            if (this.logicGraphController.BottomLeftWorld != this.previousGridPosition)
            {
                Vector2Int currentMousePos = this.getMouseLocalGridPosition();

                if (!this.previousGridPosition.Equals(currentMousePos))
                {
                    this.previousGridPosition = currentMousePos;

                    if (this.currentState == EditorState.AddCompoent)
                    {
                        this.component.Position = this.previousGridPosition;
                        bool canPlace           = this.logicGraphController.Graph.LightGraph.canPlace(this.component);

                        this.mouseObject.transform.Find("X").gameObject.SetActive(!canPlace);
                        Vector2 worldMousePosition          = this.getMouseWorldGridPosition();
                        this.mouseObject.transform.position = worldMousePosition;
                    }
                    else if (this.currentState == EditorState.DeleteComponent)
                    {
                        Vector2 worldMousePosition          = this.getMouseWorldGridPosition();
                        this.mouseObject.transform.position = worldMousePosition;

                        GraphComponent graphComp = this.logicGraphController.Graph.LightGraph.getComponentAt(
                            this.previousGridPosition.x, this.previousGridPosition.y);

                        SpriteRenderer rend = this.mouseObject.transform.GetChild(0).GetComponent <SpriteRenderer>();

                        if (graphComp != null)
                        {
                            rend.color = Color.red;
                        }
                        else
                        {
                            rend.color = Color.gray;
                        }
                    }
                }
            }
        });

        //mouse down
        this.inputs.addInput(new KeyCombination(KeyCode.Mouse0, KeyStatus.Down), () => {
            if (this.currentState == EditorState.AddCompoent)
            {
                //adds the component
                LightComponent comp = this.duplicateAt(this.component.GetType(), this.previousGridPosition, this.rotation, this.flipped);

                if (this.logicGraphController.Graph.LightGraph.canPlace(comp) &&
                    this.inputs.CurrentFrameData.RaycastResults.Count != 0 &&
                    this.inputs.CurrentFrameData.RaycastResults[0].gameObject.Equals(this.rawImage.gameObject))
                {
                    if (comp.GetType().IsSubclassOf(typeof(LinkComponent)))
                    {
                        EnterTextContent etc = new EnterTextContent(
                            "Type in a unique name for the Bridge." +
                            " The name can not be the same as another bridge on this Graph",
                            (string inputFieldText) => {
                            //confrim
                            ((LinkComponent)comp).getExtensionConnection().Name = inputFieldText;
                            addComponentToGraph(comp);
                        },
                            () => {},
                            15);

                        //names exits
                        etc.addErrorCheck((string value) => {
                            bool result = true;
                            int counter = 0;

                            ExtensionNode[] checkBridges = this.logicGraphController.Graph.AllBridges();

                            while (result && counter < checkBridges.Length)
                            {
                                if (value.Equals(checkBridges[counter].Name))
                                {
                                    result = false;
                                }

                                counter++;
                            }

                            return(result);
                        }, "Name Already Exits");

                        //can place component after entering value
                        etc.addErrorCheck((string value) => {
                            return(this.logicGraphController.Graph.LightGraph.canPlace(comp));
                        }, "Can not Place there");

                        this.spawnChildWindow(new Window(comp.GetType() + " Name", 200, 200, etc));
                    }
                    else
                    {
                        this.addComponentToGraph(comp);
                    }
                }
            }
            else if (currentState == EditorState.DeleteComponent)
            {
                //removes the component

                Vector2Int gridPosition     = this.getMouseLocalGridPosition();
                LightComponent hitComponent = this.graph.LightGraph.getComponentAt(gridPosition.x, gridPosition.y);

                if (hitComponent != null)
                {
                    bool removedGOState  = this.logicGraphController.ComponentManager.removeComponent(hitComponent);
                    bool removeDataState = this.graph.LightGraph.removeComponent(hitComponent);

                    if (removedGOState && removeDataState)
                    {
                        this.logicGraphController.ComponentManager.reconnectRays();
                    }
                    else
                    {
                        throw new System.Exception("ComponentNotFound: GameObjectRemoval:" + removedGOState + " DataRemoval" + removeDataState);
                    }
                }
            }
        });

        #endregion
    }