Jump to content

Question on GUILayout Repaint Event


Recommended Posts

Hi all.
I'm new in the KSP modding and Unity hooks too and I was hoping you could help on something I cannot understand.

I'm trying to build a layout window using GUILayout class on which I want to draw a simple graph / curve.

        public void OnGUI()
        {
            // Register the window. Notice the 3rd parameter
            logWindowPos = GUILayout.Window(GetType().FullName.GetHashCode(), logWindowPos, WindowGUI, "Landing Control", GUILayout.Width(logWindowPos.width), GUILayout.Height(logWindowPos.height));
        }
        protected void WindowGUI(int windowID)
        {

            GUILayout.BeginVertical();
            GUILayout.Box(Texture2D.blackTexture, GUILayout.Width(200), GUILayout.Height(200));
            Rect graphBoxRect = GUILayoutUtility.GetLastRect();
            GUILayout.EndVertical();
            GUI.DragWindow();

            if (Event.current.type == EventType.Repaint)
            {
                if (graphData.Length > 1)
                    for (int i = 0; i < graphData.Length - 1; i++)
                    {
                        Drawing.DrawLine(
                            new Vector2(graphBoxRect.xMin + graphData[i].x , graphBoxRect.yMax - graphData[i].y ),
                            new Vector2(graphBoxRect.xMin + graphData[i + 1].x , graphBoxRect.yMax - graphData[i + 1].y ), 			Color.white, 2, true);
                    }
            }
        }

It draws what I want and I can see the white lines as expected.
The window is dragable etc.
Though the thing that eludes me is that I don't know how to force a redraw / repaint of the window when I add a dot to the curve.

How do you force a redraw of a GUILayout window ?
I suppose clicking on a toggle button in it may call a repaint but what if there is no control and you want it to refresh from outside ?
I've been unable to find a method or a variable to set to let the engine know.

Support greatly appreciated, thanks.

Link to comment
Share on other sites

16 minutes ago, MaverickWoe said:

How do you force a redraw of a GUILayout window ?

Usually you don't need to; OnGUI is called every frame to redraw the whole window from scratch. That's why it's considered a performance hog (and is semi-deprecated) and updating to DialogGUI is recommended where possible:

So in theory, if you update your graphData array, the latest lines should always be drawn. If you're already trying to update graphData, I would guess that those efforts are not succeeding due to some problem in code we cannot see here.

Link to comment
Share on other sites

34 minutes ago, HebaruSan said:

So in theory, if you update your graphData array, the latest lines should always be drawn. If you're already trying to update graphData, I would guess that those efforts are not succeeding due to some problem in code we cannot see here.

Thanks Hebaru

You were right, I was focusing on the wrong problem, the update of the array was broken. My bad
I'll also look into the DialogGUI if its easier on the CPU, thanks for pointing it out.

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