Jump to content

(Solved)OnGUI Assistance


Recommended Posts

I added the chunk of code shown in for testing : (since rendermanager isn't a thing anymore)

Spoiler

 

It seems that the display does appear on the top left hand side of the screen without a window. I am probably missing something simple, but I have been unable to find any documentation through my searches.

 

Example code,

Spoiler

        private void OnGUI()
        {
            if (Event.current.type == EventType.Repaint || Event.current.isMouse)
            {
                myPreDrawQueue(); // Your current on preDrawQueue code
            }
            drawGUI(); // Your current on postDrawQueue code
        }

        void myPreDrawQueue()
        {
            Debug.Log ("PreDraw"); // added for testing

        }

        void drawGUI()
        {
            Debug.Log ("Drawing"); // added for testing
            GUILayout.BeginHorizontal (GUILayout.Width (100f)); // my code, test
            GUILayout.Label 
("test") ; // my code, test
            GUILayout.EndHorizontal (); // my cod,e test
            GUI.DragWindow (); / /my code, test
        }

Any advice on what I am doing wrong, or a point in the right direction to find more documentation for the gui draw system used by ksp would be amazing.

Edited by Electr0ninja
Link to comment
Share on other sites

I got the window to appear by adding " GUILayout.Window (0, Window_Position, null, "Test"); " to the myPreDrawQueue()

and I can set its position and size by setting the Window_Position;

 

Window_Position is just a rectangle in the class.

but the text seems very dim

Edited by Electr0ninja
Link to comment
Share on other sites

well i got everything but the window drag, and the text dimming figured out,

Spoiler

        public Rect Window_Position = new Rect ();

        public override void OnStart(StartState state){
            Window_Position.Set (500, 500, 50, 50);
            base.OnStart (state);
        }

        public override void OnFixedUpdate(){


            if (this.vessel == FlightGlobals.ActiveVessel) {

            }

            base.OnFixedUpdate ();
        }


        private void OnGUI()
        {
            if (Event.current.type == EventType.Repaint || Event.current.isMouse)
            {
                myPreDrawQueue(); // Your current on preDrawQueue code
            }
            drawGUI(); // Your current on postDrawQueue code
        }

        void myPreDrawQueue()
        {
                GUILayout.Window (10, Window_Position, Drag, "Test");       
        }

        void drawGUI()
        {
            GUILayout.BeginArea (Window_Position);

            GUILayout.BeginHorizontal (GUILayout.Width (50f));
            GUILayout.Label ("Test");
            GUILayout.EndHorizontal ();


            GUILayout.EndArea ();

        }

        private void Drag(int windowID){

            GUI.DragWindow ();
        }

 

Link to comment
Share on other sites

For dragging, you need to assign the result of GuiLayout.Window back to your position rect

For text dimming, that's because the window is being drawn on top of your other stuff. You want to put everything inside "drawGUI" into the top of "Drag" (probably lose the area code too)

Edited by Crzyrndm
Link to comment
Share on other sites

First of all, thank you for the assistance.

Although, I did a code revision. The window remains un-draggable, and the text label is no longer visible.  Am I misunderstanding your advice?

Spoiler

public Rect Window_Position = new Rect ();

        public override void OnStart(StartState state){
            Window_Position.Set (500, 500, 50, 50);
            base.OnStart (state);
        }
        private void OnGUI()
        {
            if (Event.current.type == EventType.Repaint || Event.current.isMouse)
            {
                myPreDrawQueue(); // Your current on preDrawQueue code
            }
            drawGUI(); // Your current on postDrawQueue code
        }

        void myPreDrawQueue()
        {
            Window_Position = GUILayout.Window (10, Window_Position, Drag, "Test");       
        }

        void drawGUI()
        {


        }

        private void Drag(int windowID){

            GUILayout.BeginHorizontal (GUILayout.Width (50f));
            GUILayout.Label ("Test123");
            GUILayout.EndHorizontal ();

            GUI.DragWindow ();
        }

Okay I stripped the program down to absolute minimums for testing.

Still can't get the window to be draggable.

new code:

Spoiler

        public Rect Window_Position = new Rect ();


        public override void OnStart(StartState state){
            Window_Position.Set (500, 500, 50, 50);
            base.OnStart (state);
        }


        private void OnGUI()
        {
            if (Event.current.type == EventType.Repaint || Event.current.isMouse)
            {
                Window_Position = GUILayout.Window (10, Window_Position, Drag, "Test");      
            }

        }

        private void Drag(int windowID){

            GUI.DragWindow ();
        }

 

Edited by Electr0ninja
Link to comment
Share on other sites

Example Skeleton code for a Drag-able Part Gui Window for 1.2pre: (If any of this is wrong feel free to correct me)

Spoiler

public class Example_Class : PartModule 

{

        private Rect Window_Position = new Rect (); // basically a window build template(if I am not mistaken)

        public override void OnStart(StartState state){ // called on "part" start

            Window_Position.Set (500, 500, 150, 150); // Set Window Size and position
            base.OnStart (state); // Call base Onstart Function
        }


        private void OnGUI() // called by unity
        {
            // Generate window and assign it to the Window Position.
                Window_Position = GUILayout.Window (10, Window_Position, Drag, "Test");      

        }

        public void Drag(int windowID){ // called because of GUILayout.Window() assignment

            //Begin a horizontal line, Assign a label, and End horizontal line
            GUILayout.BeginHorizontal (GUILayout.Width (165f));
            GUILayout.Label ("Test_Label");
            GUILayout.EndHorizontal ();

            //Drag the window if needed
            GUI.DragWindow ();

        }

} // Class Definition

Credit to Sarbian and Crzyrndm for their assistance, thank you both.

Edited by Electr0ninja
Link to comment
Share on other sites

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...