Example #1
0
    /// <summary>
    /// The class that builds the gui
    /// </summary>
    void OnGUI()
    {
        //We create two labels for the hack and steps counters
        var topleftStyle = GUI.skin.GetStyle("Label");

        topleftStyle.alignment = TextAnchor.UpperLeft;
        GUI.Label(new Rect(0, 0, Screen.width, 30), levelStats.getHackString(), topleftStyle);
        GUI.Label(new Rect(0, 20, Screen.width, 30), levelStats.getStepString(), topleftStyle);
        GUI.Label(new Rect(0, 40, Screen.width, 30), PlayerController.getKeysString(), topleftStyle);

        //If the hack gui is on we display
        if (displayHackGUI)
        {
            //Here is the box
            GUI.Box(new Rect(hackGUIx, hackGUIy, hackGUIWidth, hackGUIHeight), hackObject.name);

            //Then we display each property one ontop of another
            int index = 0;
            foreach (HackableProperty property in properties)
            {
                //Height is calculated for all the property elements
                float propertyHeight = hackGUIy + (indvidualHeight * (index + 1));
                //A label is created for the property name
                GUI.Label(new Rect(nameX, propertyHeight, 65, 20), property.name);

                //Then we get the property changing part which varrys depending on the hackable property
                Rect valueRectangle = new Rect(valueX, propertyHeight, 65, 20);
                if (property is HackableBool)
                {
                    //HackableBools are represented as checkboxes
                    ((HackableBool)property).value = GUI.Toggle(valueRectangle, ((HackableBool)property).value, "");
                }
                else if (property is HackableString)
                {
                    //HackableString are represented as text fields
                    ((HackableString)property).value = GUI.TextField(valueRectangle, ((HackableString)property).value);
                }
                else if (property is HackableNumber)
                {
                    //HackableNumber are represented as text fields but it should only accept numbers
                    string numberString = GUI.TextField(valueRectangle, ((HackableNumber)property).getString());
                    int.TryParse(numberString, out ((HackableNumber)property).value);
                }
                else if (property is HackableEnum)
                {
                    //HackableEnums are buttons with content linked to that of the current value
                    //Pressing the button changes the index thus changing the button content next frame
                    if (GUI.Button(valueRectangle, ((HackableEnum)property).getValue()))
                    {
                        ((HackableEnum)property).incrementIndex();
                    }
                }

                index++;
            }

            //Next confirm and cancel buttons are added

            float buttonHeights = hackGUIy + (indvidualHeight * (properties.Length + 1));
            if (GUI.Button(new Rect(nameX, buttonHeights, 70, 20), "Confirm"))
            {
                //Confirm button checks differences but only changes them if there are hacks available
                if (levelStats.attemptHacks(calcuateDifferences()))
                {
                    hackObject.updateProperties(properties);
                }
                else
                {
                    displayTimedMessage("Exceeded Hack Limit");
                }
                //Hack menu closes afterwards
                displayHackGUI = false;
            }
            if (GUI.Button(new Rect(valueX, buttonHeights, 65, 20), "Cancel"))
            {
                //Hack menu closes when cancel is pressed
                displayHackGUI = false;
            }
        }

        if (timedMessageCountdown > 0)
        {
            //Timed message is just a label in the center it decrements the counter until the this code no longer runs
            var centralStyle = GUI.skin.GetStyle("Label");
            centralStyle.alignment = TextAnchor.MiddleCenter;
            GUI.Label(new Rect(0, 0, Screen.width, Screen.height / 2), timedMessage, centralStyle);
            timedMessageCountdown--;
        }
    }