MeasureString() static private method

Measures the size of a string as it would be rendered using the specified font.
static private MeasureString ( Fonts font, string text ) : Size
font Fonts The font to use.
text string The string to render.
return Size
Ejemplo n.º 1
0
        /// <summary>Renders the list of game (textual) messages</summary>
        /// <param name="Element">The HUD element these are to be rendered onto</param>
        /// <param name="TimeElapsed">The time elapsed</param>
        private static void RenderGameMessages(HUD.Element Element, double TimeElapsed)
        {
            //Calculate the size of the viewing plane
            int n = MessageManager.TextualMessages.Count;
            //Minimum initial width is 16px
            double totalwidth = 16.0f;

            for (int j = 0; j < n; j++)
            {
                //Update font size for the renderer
                System.Drawing.Size size = Renderer.MeasureString(Element.Font, (string)MessageManager.TextualMessages[j].MessageToDisplay);
                MessageManager.TextualMessages[j].Width  = size.Width;
                MessageManager.TextualMessages[j].Height = size.Height;
                //Run through the list of current messages
                double a = MessageManager.TextualMessages[j].Width - j * (double)Element.Value1;
                //If our width is wider than the old, use this as the NEW viewing plane width
                if (a > totalwidth)
                {
                    totalwidth = a;
                }
            }
            //Calculate the X-width of the viewing plane
            Game.MessagesRendererSize.X += 16.0 * TimeElapsed * ((double)totalwidth - Game.MessagesRendererSize.X);
            totalwidth = (float)Game.MessagesRendererSize.X;
            double lcrh, lw, rw;

            //Calculate final viewing plane size to pass to openGL
            CalculateViewingPlaneSize(Element, out lw, out rw, out lcrh);

            // start
            double w = totalwidth + lw + rw;
            double h = Element.Value2 * n;
            double x = Element.Alignment.X < 0 ? 0.0 : Element.Alignment.X > 0 ? Screen.Width - w : 0.5 * (Screen.Width - w);
            double y = Element.Alignment.Y < 0 ? 0.0 : Element.Alignment.Y > 0 ? Screen.Height - h : 0.5 * (Screen.Height - h);

            x += Element.Position.X;
            y += Element.Position.Y;
            int m = 0;

            for (int j = 0; j < n; j++)
            {
                var   mm = MessageManager.TextualMessages[j];
                float br, bg, bb, ba;
                CreateBackColor(Element.BackgroundColor, mm.Color, out br, out bg, out bb, out ba);
                float tr, tg, tb, ta;
                CreateTextColor(Element.TextColor, mm.Color, out tr, out tg, out tb, out ta);
                float or, og, ob, oa;
                CreateBackColor(Element.OverlayColor, mm.Color, out or, out og, out ob, out oa);
                double tx, ty;
                bool   preserve = false;
                if ((Element.Transition & HUD.Transition.Move) != 0)
                {
                    if (Game.SecondsSinceMidnight < mm.Timeout)
                    {
                        if (mm.RendererAlpha == 0.0)
                        {
                            mm.RendererPosition.X = x + Element.TransitionVector.X;
                            mm.RendererPosition.Y = y + Element.TransitionVector.Y;
                            mm.RendererAlpha      = 1.0;
                        }
                        tx       = x;
                        ty       = y + m * Element.Value2;
                        preserve = true;
                    }
                    else if (Element.Transition == HUD.Transition.MoveAndFade)
                    {
                        tx = x;
                        ty = y + m * Element.Value2;
                    }
                    else
                    {
                        tx = x + Element.TransitionVector.X;
                        ty = y + (j + 1) * Element.TransitionVector.Y;
                    }
                    const double speed = 2.0;
                    double       dx    = (speed * Math.Abs(tx - mm.RendererPosition.X) + 0.1) * TimeElapsed;
                    double       dy    = (speed * Math.Abs(ty - mm.RendererPosition.Y) + 0.1) * TimeElapsed;
                    if (Math.Abs(tx - mm.RendererPosition.X) < dx)
                    {
                        mm.RendererPosition.X = tx;
                    }
                    else
                    {
                        mm.RendererPosition.X += Math.Sign(tx - mm.RendererPosition.X) * dx;
                    }
                    if (Math.Abs(ty - mm.RendererPosition.Y) < dy)
                    {
                        mm.RendererPosition.Y = ty;
                    }
                    else
                    {
                        mm.RendererPosition.Y += Math.Sign(ty - mm.RendererPosition.Y) * dy;
                    }
                }
                else
                {
                    tx = x;
                    ty = y + m * Element.Value2;
                    mm.RendererPosition.X = 0.0;
                    const double speed = 12.0;
                    double       dy    = (speed * Math.Abs(ty - mm.RendererPosition.Y) + 0.1) * TimeElapsed;
                    mm.RendererPosition.X = x;
                    if (Math.Abs(ty - mm.RendererPosition.Y) < dy)
                    {
                        mm.RendererPosition.Y = ty;
                    }
                    else
                    {
                        mm.RendererPosition.Y += Math.Sign(ty - mm.RendererPosition.Y) * dy;
                    }
                }
                if ((Element.Transition & HUD.Transition.Fade) != 0)
                {
                    if (Game.SecondsSinceMidnight >= mm.Timeout)
                    {
                        mm.RendererAlpha -= TimeElapsed;
                        if (mm.RendererAlpha < 0.0)
                        {
                            mm.RendererAlpha = 0.0;
                        }
                    }
                    else
                    {
                        mm.RendererAlpha += TimeElapsed;
                        if (mm.RendererAlpha > 1.0)
                        {
                            mm.RendererAlpha = 1.0;
                        }
                        preserve = true;
                    }
                }
                else if (Game.SecondsSinceMidnight > mm.Timeout)
                {
                    if (Math.Abs(mm.RendererPosition.X - tx) < 0.1 & Math.Abs(mm.RendererPosition.Y - ty) < 0.1)
                    {
                        mm.RendererAlpha = 0.0;
                    }
                }
                if (preserve)
                {
                    m++;
                }
                double px    = mm.RendererPosition.X + (double)j * (double)Element.Value1;
                double py    = mm.RendererPosition.Y;
                float  alpha = (float)(mm.RendererAlpha * mm.RendererAlpha);
                // graphics
                HUD.Image Left = j == 0
                                        ? Element.TopLeft
                                        : j < n - 1
                                                ? Element.CenterLeft
                                                : Element.BottomLeft;
                HUD.Image Middle = j == 0
                                        ? Element.TopMiddle
                                        : j < n - 1
                                                ? Element.CenterMiddle
                                                : Element.BottomMiddle;
                HUD.Image Right = j == 0
                                        ? Element.TopRight
                                        : j < n - 1
                                                ? Element.CenterRight
                                                : Element.BottomRight;
                // left background
                if (Left.BackgroundTexture != null)
                {
                    if (Textures.LoadTexture(Left.BackgroundTexture, Textures.OpenGlTextureWrapMode.ClampClamp))
                    {
                        double u = (double)Left.BackgroundTexture.Width;
                        double v = (double)Left.BackgroundTexture.Height;
                        GL.Color4(br, bg, bb, ba * alpha);
                        RenderOverlayTexture(Left.BackgroundTexture, px, py, px + u, py + v);
                    }
                }
                // right background
                if (Right.BackgroundTexture != null)
                {
                    if (Textures.LoadTexture(Right.BackgroundTexture, Textures.OpenGlTextureWrapMode.ClampClamp))
                    {
                        double u = (double)Right.BackgroundTexture.Width;
                        double v = (double)Right.BackgroundTexture.Height;
                        GL.Color4(br, bg, bb, ba * alpha);
                        RenderOverlayTexture(Right.BackgroundTexture, px + w - u, py, px + w, py + v);
                    }
                }
                // middle background
                if (Middle.BackgroundTexture != null)
                {
                    if (Textures.LoadTexture(Middle.BackgroundTexture, Textures.OpenGlTextureWrapMode.ClampClamp))
                    {
                        double v = (double)Middle.BackgroundTexture.Height;
                        GL.Color4(br, bg, bb, ba * alpha);
                        RenderOverlayTexture(Middle.BackgroundTexture, px + lw, py, px + w - rw, py + v);
                    }
                }
                {
                    // text
                    string t = (string)mm.MessageToDisplay;
                    double u = mm.Width;
                    double v = mm.Height;
                    double p =
                        Math.Round(
                            (Element.TextAlignment.X < 0
                                                                ? px
                                                                : Element.TextAlignment.X > 0
                                                                        ? px + w - u
                                                                        : px + 0.5 * (w - u)) - j * Element.Value1);
                    double q = Math.Round(Element.TextAlignment.Y < 0
                                                ? py
                                                : Element.TextAlignment.Y > 0
                                                        ? py + lcrh - v
                                                        : py + 0.5 * (lcrh - v));
                    p += Element.TextPosition.X;
                    q += Element.TextPosition.Y;
                    DrawString(Element.Font, t, new System.Drawing.Point((int)p, (int)q),
                               TextAlignment.TopLeft, new Color128(tr, tg, tb, ta * alpha), Element.TextShadow);
                }
                // left overlay
                if (Left.OverlayTexture != null)
                {
                    if (Textures.LoadTexture(Left.OverlayTexture, Textures.OpenGlTextureWrapMode.ClampClamp))
                    {
                        double u = (double)Left.OverlayTexture.Width;
                        double v = (double)Left.OverlayTexture.Height;
                        GL.Color4(or, og, ob, oa * alpha);
                        RenderOverlayTexture(Left.OverlayTexture, px, py, px + u, py + v);
                    }
                }
                // right overlay
                if (Right.OverlayTexture != null)
                {
                    if (Textures.LoadTexture(Right.OverlayTexture, Textures.OpenGlTextureWrapMode.ClampClamp))
                    {
                        double u = (double)Right.OverlayTexture.Width;
                        double v = (double)Right.OverlayTexture.Height;
                        GL.Color4(or, og, ob, oa * alpha);
                        RenderOverlayTexture(Right.OverlayTexture, px + w - u, py, px + w, py + v);
                    }
                }
                // middle overlay
                if (Middle.OverlayTexture != null)
                {
                    if (Textures.LoadTexture(Middle.OverlayTexture, Textures.OpenGlTextureWrapMode.ClampClamp))
                    {
                        double v = (double)Middle.OverlayTexture.Height;
                        GL.Color4(or, og, ob, oa * alpha);
                        RenderOverlayTexture(Middle.OverlayTexture, px + lw, py, px + w - rw, py + v);
                    }
                }
            }
        }
Ejemplo n.º 2
0
            public int TopItem;                     // the top displayed menu item


            /********************
             *      MENU C'TOR
             *********************/
            public SingleMenu(MenuType menuType, int data = 0)
            {
                int  i, menuItem;
                int  jump = 0;
                Size size;

                Align     = TextAlignment.TopMiddle;
                Height    = Width = 0;
                Selection = 0;                                      // defaults to first menu item
                switch (menuType)
                {
                case MenuType.Top:                                  // top level menu
                    for (i = 0; i < Game.Stations.Length; i++)
                    {
                        if (Game.PlayerStopsAtStation(i) & Game.Stations[i].Stops.Length > 0)
                        {
                            jump = 1;
                            break;
                        }
                    }
                    Items    = new MenuEntry[4 + jump];
                    Items[0] = new MenuCommand(Translations.GetInterfaceString("menu_resume"), MenuTag.BackToSim, 0);
                    if (jump > 0)
                    {
                        Items[1] = new MenuCommand(Translations.GetInterfaceString("menu_jump"), MenuTag.MenuJumpToStation, 0);
                    }
                    if (!Interface.CurrentOptions.KioskMode)
                    {
                        //Don't allow quitting or customisation of the controls in kiosk mode
                        Items[1 + jump] = new MenuCommand(Translations.GetInterfaceString("menu_exit"), MenuTag.MenuExitToMainMenu, 0);
                        Items[2 + jump] = new MenuCommand(Translations.GetInterfaceString("menu_customize_controls"), MenuTag.MenuControls, 0);
                        Items[3 + jump] = new MenuCommand(Translations.GetInterfaceString("menu_quit"), MenuTag.MenuQuit, 0);
                    }
                    else
                    {
                        Array.Resize(ref Items, Items.Length - 3);
                    }
                    break;

                case MenuType.JumpToStation:                            // list of stations to jump to
                    // count the number of available stations
                    menuItem = 0;
                    for (i = 0; i < Game.Stations.Length; i++)
                    {
                        if (Game.PlayerStopsAtStation(i) & Game.Stations[i].Stops.Length > 0)
                        {
                            menuItem++;
                        }
                    }
                    // list available stations, selecting the next station as predefined choice
                    jump     = 0;                                                   // no jump found yet
                    Items    = new MenuEntry[menuItem + 1];
                    Items[0] = new MenuCommand(Translations.GetInterfaceString("menu_back"), MenuTag.MenuBack, 0);
                    menuItem = 1;
                    for (i = 0; i < Game.Stations.Length; i++)
                    {
                        if (Game.PlayerStopsAtStation(i) & Game.Stations[i].Stops.Length > 0)
                        {
                            Items[menuItem] = new MenuCommand(Game.Stations[i].Name, MenuTag.JumpToStation, i);
                            // if no preferred jump-to-station found yet and this station is
                            // after the last station the user stopped at, select this item
                            if (jump == 0 && i > TrainManager.PlayerTrain.LastStation)
                            {
                                jump      = i;
                                Selection = menuItem;
                            }
                            menuItem++;
                        }
                    }
                    Align = TextAlignment.TopLeft;
                    break;

                case MenuType.ExitToMainMenu:
                    Items     = new MenuEntry[3];
                    Items[0]  = new MenuCaption(Translations.GetInterfaceString("menu_exit_question"));
                    Items[1]  = new MenuCommand(Translations.GetInterfaceString("menu_exit_no"), MenuTag.MenuBack, 0);
                    Items[2]  = new MenuCommand(Translations.GetInterfaceString("menu_exit_yes"), MenuTag.ExitToMainMenu, 0);
                    Selection = 1;
                    break;

                case MenuType.Quit:                                 // ask for quit confirmation
                    Items     = new MenuEntry[3];
                    Items[0]  = new MenuCaption(Translations.GetInterfaceString("menu_quit_question"));
                    Items[1]  = new MenuCommand(Translations.GetInterfaceString("menu_quit_no"), MenuTag.MenuBack, 0);
                    Items[2]  = new MenuCommand(Translations.GetInterfaceString("menu_quit_yes"), MenuTag.Quit, 0);
                    Selection = 1;
                    break;

                case MenuType.Controls:
                    //Refresh the joystick list
                    Program.Joysticks.RefreshJoysticks();
                    Items    = new MenuEntry[Interface.CurrentControls.Length + 1];
                    Items[0] = new MenuCommand(Translations.GetInterfaceString("menu_back"), MenuTag.MenuBack, 0);
                    for (i = 0; i < Interface.CurrentControls.Length; i++)
                    {
                        Items[i + 1] = new MenuCommand(Interface.CurrentControls[i].Command.ToString(), MenuTag.Control, i);
                    }
                    Align = TextAlignment.TopLeft;
                    break;

                case MenuType.Control:
                    //Refresh the joystick list
                    Program.Joysticks.RefreshJoysticks();
                    Selection = SelectionNone;
                    Items     = new MenuEntry[4];
                    // get code name and description
                    Interface.Control loadedControl = Interface.CurrentControls[data];
                    for (int h = 0; h < Translations.CommandInfos.Length; h++)
                    {
                        if (Translations.CommandInfos[h].Command == loadedControl.Command)
                        {
                            Items[0] = new MenuCommand(loadedControl.Command.ToString() + " - " +
                                                       Translations.CommandInfos[h].Description, MenuTag.None, 0);
                            break;
                        }
                    }
                    // get assignment
                    String str = "";
                    switch (loadedControl.Method)
                    {
                    case Interface.ControlMethod.Keyboard:
                        string keyName = loadedControl.Key.ToString();
                        for (int k = 0; k < Translations.TranslatedKeys.Length; k++)
                        {
                            if (Translations.TranslatedKeys[k].Key == loadedControl.Key)
                            {
                                keyName = Translations.TranslatedKeys[k].Description;
                                break;
                            }
                        }
                        if (loadedControl.Modifier != Interface.KeyboardModifier.None)
                        {
                            str = Translations.GetInterfaceString("menu_keyboard") + " [" + loadedControl.Modifier + "-" + keyName + "]";
                        }
                        else
                        {
                            str = Translations.GetInterfaceString("menu_keyboard") + " [" + keyName + "]";
                        }
                        break;

                    case Interface.ControlMethod.Joystick:
                        str = Translations.GetInterfaceString("menu_joystick") + " " + loadedControl.Device + " [" + loadedControl.Component + " " + loadedControl.Element + "]";
                        switch (loadedControl.Component)
                        {
                        case Interface.JoystickComponent.FullAxis:
                        case Interface.JoystickComponent.Axis:
                            str += " " + (loadedControl.Direction == 1 ? Translations.GetInterfaceString("menu_joystickdirection_positive") : Translations.GetInterfaceString("menu_joystickdirection_negative"));
                            break;

                        //						case Interface.JoystickComponent.Button:	// NOTHING TO DO FOR THIS CASE!
                        //							str = str;
                        //							break;
                        case Interface.JoystickComponent.Hat:
                            str += " " + (OpenTK.Input.HatPosition)loadedControl.Direction;
                            break;

                        case Interface.JoystickComponent.Invalid:
                            str = Translations.GetInterfaceString("menu_joystick_notavailable");
                            break;
                        }
                        break;

                    case Interface.ControlMethod.RailDriver:
                        str = "RailDriver [" + loadedControl.Component + " " + loadedControl.Element + "]";
                        switch (loadedControl.Component)
                        {
                        case Interface.JoystickComponent.FullAxis:
                        case Interface.JoystickComponent.Axis:
                            str += " " + (loadedControl.Direction == 1 ? Translations.GetInterfaceString("menu_joystickdirection_positive") : Translations.GetInterfaceString("menu_joystickdirection_negative"));
                            break;

                        case Interface.JoystickComponent.Invalid:
                            str = Translations.GetInterfaceString("menu_joystick_notavailable");
                            break;
                        }
                        break;

                    case Interface.ControlMethod.Invalid:
                        str = Translations.GetInterfaceString("menu_joystick_notavailable");
                        break;
                    }
                    Items[1] = new MenuCommand(Translations.GetInterfaceString("menu_assignment_current") + " " + str, MenuTag.None, 0);
                    Items[2] = new MenuCommand(" ", MenuTag.None, 0);
                    Items[3] = new MenuCommand(Translations.GetInterfaceString("menu_assign"), MenuTag.None, 0);
                    break;
                }

                // compute menu extent
                for (i = 0; i < Items.Length; i++)
                {
                    if (Items[i] == null)
                    {
                        continue;
                    }
                    size = Renderer.MeasureString(Game.Menu.MenuFont, Items[i].Text);
                    if (size.Width > Width)
                    {
                        Width = size.Width;
                    }
                    if (!(Items[i] is MenuCaption) && size.Width > ItemWidth)
                    {
                        ItemWidth = size.Width;
                    }
                }
                Height  = Items.Length * Game.Menu.LineHeight;
                TopItem = 0;
            }