Exemplo n.º 1
0
    // Draw the player's UI
    void OnGUI()
    {
        // Get a UI scaling variable
        float UIScale = Screen.width / 2560f;

        // Draw the panel UI if need be
        if (_LevelManager.ViewingPanel)
        {
            // Draw a 'toolbox' at the top of the screen
            // First the tool box itself
            GUI.DrawTexture(new Rect(0, 0, 2560f * UIScale, 360f * UIScale), ToolboxImage);

            // Then the tools. Loop through the 'all logic gates' list
            float ToolImageWidth = 240f * UIScale;
            float ToolBaseX      = 50f * UIScale;

            // AND Gate
            HandleToolClick(true, "AND", UIScale, ToolBaseX, 0, Gates.Gate_AND.GateSprite.texture);

            // OR Gate
            HandleToolClick(true, "OR", UIScale, ToolBaseX, 1, Gates.Gate_OR.GateSprite.texture);

            // XOR Gate
            HandleToolClick(true, "XOR", UIScale, ToolBaseX, 2, Gates.Gate_XOR.GateSprite.texture);

            // NAND Gate
            HandleToolClick(true, "NAND", UIScale, ToolBaseX, 3, Gates.Gate_NAND.GateSprite.texture);

            // NOR Gate
            HandleToolClick(true, "NOR", UIScale, ToolBaseX, 4, Gates.Gate_NOR.GateSprite.texture);

            // XNOR Gate
            HandleToolClick(true, "XNOR", UIScale, ToolBaseX, 5, Gates.Gate_XNOR.GateSprite.texture);

            // NOT Gate
            HandleToolClick(true, "NOT", UIScale, ToolBaseX, 6, Gates.Gate_NOT.GateSprite.texture);

            // Wire tool
            HandleToolClick(false, "Wire", UIScale, ToolBaseX, 7, WireToolIcon);

            // Return to ship
            HandleToolClick(false, "Ship", UIScale, ToolBaseX, 8, ShipIcon);

            // Render tooltips
            // AND Gate
            HandleTooltip("AND", UIScale, ToolBaseX, 0);

            // OR Gate
            HandleTooltip("OR", UIScale, ToolBaseX, 1);

            // XOR Gate
            HandleTooltip("XOR", UIScale, ToolBaseX, 2);

            // NAND Gate
            HandleTooltip("NAND", UIScale, ToolBaseX, 3);

            // NOR Gate
            HandleTooltip("NOR", UIScale, ToolBaseX, 4);

            // XNOR Gate
            HandleTooltip("XNOR", UIScale, ToolBaseX, 5);

            // NOT Gate
            HandleTooltip("NOT", UIScale, ToolBaseX, 6);

            // Wire tool
            HandleTooltip("Wire", UIScale, ToolBaseX, 7);

            // Return to ship
            HandleTooltip("Ship", UIScale, ToolBaseX, 8);

            // Render the inputs and outputs
            for (int i = 0; i < _Gates.InputCount; i++)
            {
                // Calculate positions
                float ButtonX     = SourceBasePos.x;
                float ButtonY     = SourceBasePos.y + 90f * i * UIScale;
                float ButtonWidth = 28 * UIScale;

                // Draw the button. Stay consistent in the colour and icons as the rest of the gates
                if (GUI.Button(new Rect(ButtonX, ButtonY, ButtonWidth, ButtonWidth), "", OutputButtonStyle) && CurrentTool == PlayerTool.CreateWire)
                {
                    // If we aren't already dragging, make it so
                    if (!IsDraggingWire)
                    {
                        // Play sound
                        PickupSound.Play();

                        // Pickup wire
                        IsDraggingWire     = true;
                        DraggingOutput     = false;
                        DraggingSource     = true;
                        DraggingEnd        = false;
                        GateDraggingFrom   = null;
                        SourceDraggingFrom = _Gates.InputConnections[i];
                        DraggingOrigin     = new Vector2(ButtonX, Screen.height - ButtonY);
                    }
                    else if (IsDraggingWire && !DraggingOutput && !DraggingSource && !DraggingEnd)
                    {
                        // Play sound
                        DropSound.Play();

                        // Stop dragging wire
                        IsDraggingWire = false;

                        // Connect the source
                        _Gates.InputConnections[i].Gates.Add(GateDraggingFrom);
                        _Gates.InputConnections[i].GateInputIDs.Add(GateDraggingFromInputID);

                        // Connect the gate
                        GateDraggingFrom.Sources[GateDraggingFromInputID] = _Gates.InputConnections[i];

                        // Create a wire
                        LineRenderer NewWire = CreateSourceEndWire(true, ButtonWidth, ButtonX, ButtonY);

                        // Parent it to the gate
                        NewWire.transform.parent = GateDraggingFrom.transform;

                        // Assign it to the gate
                        if (DraggingOutput)
                        {
                            // Assign it as an end wire
                            GateDraggingFrom.EndWire = NewWire;
                        }
                        else
                        {
                            // Assign it as a source wire
                            GateDraggingFrom.SourceWires[GateDraggingFromInputID] = NewWire;
                        }
                    }
                }

                // Handle the tooltip
                HandleTooltip(_Gates.InputConnections[i].Label, UIScale, 0, i, _Gates.InputConnections[i].Description, true);
            }

            // Render outputs
            for (int i = 0; i < _Gates.OutputCount; i++)
            {
                // Calculate positions
                float ButtonX     = OutBasePos.x;
                float ButtonY     = OutBasePos.y + 90f * i * UIScale;
                float ButtonWidth = 28 * UIScale;

                // Draw the button. Stay consistent in the colour and icons as the rest of the gates
                if (GUI.Button(new Rect(ButtonX, ButtonY, ButtonWidth, ButtonWidth), "", InputButtonStyle) && CurrentTool == PlayerTool.CreateWire)
                {
                    // If we aren't already dragging, make it so
                    if (!IsDraggingWire)
                    {
                        // Play sound
                        PickupSound.Play();

                        // Pickup wire
                        IsDraggingWire     = true;
                        DraggingOutput     = false;
                        DraggingSource     = false;
                        DraggingEnd        = true;
                        GateDraggingFrom   = null;
                        SourceDraggingFrom = _Gates.OutputConnections[i];
                        DraggingOrigin     = new Vector2(ButtonX, Screen.height - ButtonY);
                    }
                    else if (IsDraggingWire && DraggingOutput && !DraggingSource && !DraggingEnd)
                    {
                        // Sever connections first if the output is connected already
                        if (_Gates.OutputConnections[i].Gates.Count > 0)
                        {
                            _Gates.OutputConnections[i].RemoveAllConnections();
                        }

                        // Play sound
                        DropSound.Play();

                        // Stop dragging wire
                        IsDraggingWire = false;

                        // Connect the source
                        _Gates.OutputConnections[i].Gates.Add(GateDraggingFrom);
                        _Gates.OutputConnections[i].GateInputIDs.Add(GateDraggingFromInputID);

                        // Connect the gate
                        GateDraggingFrom.EndConnection = _Gates.OutputConnections[i];

                        // Create a wire
                        LineRenderer NewWire = CreateSourceEndWire(false, ButtonWidth, ButtonX, ButtonY);

                        // Parent it to the gate
                        NewWire.transform.parent = GateDraggingFrom.transform;

                        // Assign it as an end wire
                        GateDraggingFrom.EndWire = NewWire;
                    }
                }

                // Handle the tooltip
                HandleTooltip(_Gates.OutputConnections[i].Label, UIScale, 0, i, _Gates.OutputConnections[i].Description, true, false);
            }

            // Fetch a local copy of the gates list
            List <GateBehaviour> GateList = Gates.CurrentGates;

            // Loop through each gate and draw 4 buttons; one for each input,
            // one for the output, and one to delete it
            for (int i = 0; i < GateList.Count; i++)
            {
                // Get the screen position of the logic gate
                Vector2 GatePosition = Camera.main.WorldToScreenPoint(GateList[i].transform.position);
                GatePosition.y = Screen.height - GatePosition.y;

                // Draw the output button
                float OutputX     = GatePosition.x + 46f * UIScale;
                float OutputY     = GatePosition.y + 16f * UIScale;
                float CrossY      = GatePosition.y - 44f * UIScale;
                float OutputWidth = 28 * UIScale;

                // Toggle the player dragging status if clicked on
                if (GUI.Button(new Rect(OutputX, OutputY, OutputWidth, OutputWidth), "", OutputButtonStyle) && CurrentTool == PlayerTool.CreateWire)
                {
                    HandleOutputClicking(OutputX, OutputY, OutputWidth, i);
                }

                // Now draw the input buttons
                if (!GateList[i].LogicGate.HasOneInput)
                {
                    // Two inputs
                    float InputX     = GatePosition.x - 68f * UIScale;
                    float InputY1    = GatePosition.y - 44f * UIScale;
                    float InputY2    = GatePosition.y + 16f * UIScale;
                    float InputWidth = 28 * UIScale;

                    // Handle clicking (button 0)
                    HandleInputClicking(InputX, InputY1, InputWidth, i, 0);

                    // Handle clicking (button 1)
                    HandleInputClicking(InputX, InputY2, InputWidth, i, 1);
                }
                else
                {
                    // One input
                    float InputX     = GatePosition.x - 68f * UIScale;
                    float InputY     = GatePosition.y - 14f * UIScale;
                    float InputWidth = 28 * UIScale;

                    // Handle clicking
                    HandleInputClicking(InputX, InputY, InputWidth, i, 0);
                }

                // Gate delete button
                if (GUI.Button(new Rect(OutputX, CrossY, OutputWidth, OutputWidth), "", CrossButtonStyle))
                {
                    // Play sound
                    DropSound.Play();

                    // Destroy gate
                    Gates.DestroyGate(Gates.GetGateID(Gates.CurrentGates[i]));
                }
            }
        }
        else
        {
            // Setup a GUI style
            ToolMiscStyle.normal.background = PanelIcon;
            ToolMiscStyle.hover.background  = PanelIconHover;
            ToolMiscStyle.active.background = PanelIcon;

            // Create the button
            if (GUI.Button(new Rect(50f * UIScale, 50f * UIScale, 160f * UIScale, 160f * UIScale), "", ToolMiscStyle))
            {
                // Play sound
                PickupSound.Play();

                // View panel
                _LevelManager.ViewingPanel = true;
                _LevelManager.RecalculateUI();
            }
        }
    }