MechJeb2

MechJeb2

4M Downloads

MechJeb windows need tooltip support

lamont-granquist opened this issue ยท 1 comments

commented

It'd be nice to be able to put up tooltips on mouseovers on labels.

Unity's support for tooltips is really bad, its simple enough to shove a string into GUI.tooltip but then you have to render it yourself, and my GUI skills aren't up to it.

commented

I got this far trying to copy stuff from websites and plagiarize off the MechJeb ComboBox before deciding I was getting too frustrated:

       public float stillTime = 1f;
       public float maxTipWidth = 300f;

       Vector2 lastMousePos;
       float timeOfLastMove;

       protected void DrawToolTip()
       {
           Vector2 mousePos = Event.current.mousePosition;

           if ((mousePos - lastMousePos).sqrMagnitude > 4) {
               lastMousePos = mousePos;
               timeOfLastMove = Time.time;
           }

           if (Time.time - timeOfLastMove < stillTime)
               return;

           Rect r = new Rect(5,5,100, 100);

           GUIStyle style = GUI.skin.GetStyle("Label");
           GUIContent tip = new GUIContent(GUI.tooltip);

           float minWidth, maxWidth;
           style.CalcMinMaxWidth(tip, out minWidth, out maxWidth);
           r.yMin = r.yMax + 5;
           r.width = Mathf.Min(maxWidth, maxTipWidth);
           r.height = style.CalcHeight(tip, r.width);

           Texture2D background = new Texture2D(16, 16, TextureFormat.RGBA32, false);
           background.wrapMode = TextureWrapMode.Clamp;

           for (int x = 0; x < background.width; x++)
               for (int y = 0; y < background.height; y++)
               {
                   if (x == 0 || x == background.width-1 || y == 0 || y == background.height-1)
                       background.SetPixel(x, y, new Color(0, 0, 0, 1));
                   else
                       background.SetPixel(x, y, new Color(0.05f, 0.05f, 0.05f, 0.95f));
               }

           background.Apply();

           GUIStyle s = new GUIStyle(GUI.skin.window);
           s.normal.background = background;
           s.onNormal.background = background;
           s.border.top = s.border.bottom;
           s.padding.top = s.padding.bottom;
           s.normal.textColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
           GUI.Label(r, GUI.tooltip, s);
      }

(called from WindowGUI() in DisplayModule.cs)