Jump to content

GUILayout.Toggle not spacing properly


Recommended Posts

I've come across an odd problem. It seems that the standard KSP skin doesn't define the GUILayout.Toggle width properly. I've tried it with just GUILayout.Toggle, and also using CalcSize, and in both cases the effective width is just the size of the radiobutton. Has anyone else seen this? It's hit me in two mods I'm working on right now

When I use the standard Unity skin, it works.

I've opened a bug about this:  https://bugs.kerbalspaceprogram.com/issues/24417

The following images show the issue:

Unity skin, everything working properly
237TSZ6.png

 

KSP skin, left shows the problem, right is working due to using CalcSize
V5pYuw4.png

 

Here is the code you can try out.  Click the toggle labeled "Use Alt.Skin" to toggle between the standard Unity skin and the KSP skin:

using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;
using KSP.UI.Screens;

namespace BadDemo
{
    [KSPAddon(KSPAddon.Startup.FlightEditorAndKSC, false)]
    public partial class FlightEditSaver : MonoBehaviour
    {
        const string ALT = "Use Alt.Skin";
        const float REVERTWIDTH = 450;
        const float REVERTHEIGHT = 100;
        Rect _revertRect = new Rect((Screen.width - REVERTWIDTH) * .75f, (Screen.height - REVERTHEIGHT) / 2, REVERTWIDTH, REVERTHEIGHT);
        Rect _revertRect2 = new Rect((Screen.width - REVERTWIDTH) * .25f, (Screen.height - REVERTHEIGHT) / 2, REVERTWIDTH, REVERTHEIGHT);

        bool useAltSkin = true;
        void OnGUI()
        {
            if (!useAltSkin)
                GUI.skin = HighLogic.Skin;
            _revertRect = GUILayout.Window(this.GetInstanceID() + 1, _revertRect, new GUI.WindowFunction(DrawWithCalc), "Using CalcSize", new GUILayoutOption[0]);
            _revertRect2 = GUILayout.Window(this.GetInstanceID() + 2, _revertRect2, new GUI.WindowFunction(DrawNoCalc), "No Size Calculation", new GUILayoutOption[0]);

        }

        void DrawWithCalc(int id)
        {
            GUILayout.BeginHorizontal();
            Display("one");
            Display("Two");
            Display("three");
            GUILayout.FlexibleSpace();
            Display(ALT);
            GUILayout.EndHorizontal();
            GUI.DragWindow();
        }
        void DrawNoCalc(int id)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Toggle(false, "one");
            GUILayout.Toggle(false, "Two");
            GUILayout.Toggle(false, "three");
            GUILayout.FlexibleSpace();
            useAltSkin = GUILayout.Toggle(useAltSkin, ALT);
            GUILayout.EndHorizontal();
            GUI.DragWindow();
        }


        void Display(string str)
        {
            GUIContent content = new GUIContent(str);
            var size = GUI.skin.textField.CalcSize(content);
            if (str == ALT)
                useAltSkin = GUILayout.Toggle(useAltSkin, content, GUILayout.Width(size.x + 24));
            else
                GUILayout.Toggle(false, content, GUILayout.Width(size.x + 24));
        }
    }

}

 

Edited by linuxgurugamer
Link to comment
Share on other sites

  • 1 year later...
This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...