/// <summary>
            /// Updates formatting based on input from the toolbar controls
            /// </summary>
            private void UpdateFormat(object sender, EventArgs args)
            {
                if (sizeList.Selection != null && fontList.Selection != null)
                {
                    float      textSize = sizeList.Selection.AssocMember;
                    IFontMin   font     = fontList.Selection.AssocMember;
                    FontStyles style    = FontStyles.Regular;

                    // Bolding requires a separate set of texture atlases, and as such, isn't always
                    // available
                    boldToggle.Enabled = font.IsStyleDefined(FontStyles.Bold);

                    if (boldToggle.Selected)
                    {
                        style |= FontStyles.Bold;
                    }

                    // Underlining and italics are render as effects and are available
                    // for every font
                    if (underlineToggle.Selected)
                    {
                        style |= FontStyles.Underline;
                    }

                    if (italicToggle.Selected)
                    {
                        style |= FontStyles.Italic;
                    }

                    _format = new GlyphFormat(_format.Color, _format.Alignment, textSize, font.GetStyleIndex(style));
                    FormatChanged?.Invoke();
                }
            }
                /// <summary>
                /// Retrieves the font with the given name.
                /// </summary>
                public static IFontMin GetFont(string name)
                {
                    FontMembers?members = Instance.GetFontFunc(name);
                    IFontMin    font    = null;

                    if (members != null)
                    {
                        font = new FontData(members.Value);
                    }

                    return(font);
                }
                /// <summary>
                /// Attempts to register a new font using API data. Returns the font created.
                /// </summary>
                public static bool TryAddFont(FontDefinition fontData, out IFontMin font)
                {
                    FontMembers?members = Instance.TryAddFontFunc(fontData);

                    if (members != null)
                    {
                        font = new FontData(members.Value);
                        return(true);
                    }
                    else
                    {
                        font = null;
                        return(false);
                    }
                }
                /// <summary>
                /// Retrieves the font style index of the font with the given name and style.
                /// </summary>
                public static Vector2I GetStyleIndex(string name, FontStyles style = FontStyles.Regular)
                {
                    IFontMin font = GetFont(name);

                    return(new Vector2I(font.Index, (int)style));
                }
            public EditorToolBar(HudParentBase parent = null) : base(parent)
            {
                var background = new TexturedBox(this)
                {
                    DimAlignment = DimAlignments.Both,
                    Color        = new Color(41, 54, 62),
                };

                // Font selection
                fontList = new EditorDropdown <IFontMin>()
                {
                    Height = 24f,
                    Width  = 140f,
                };

                foreach (IFontMin font in FontManager.Fonts)
                {
                    fontList.Add(new RichText(font.Name, GlyphFormat.White.WithFont(font.Regular)), font);
                }

                // Text size
                sizeList = new EditorDropdown <float>()
                {
                    Height = 24f,
                    Width  = 60f,
                };

                for (int n = 0; n < textSizes.Length; n++)
                {
                    sizeList.Add(textSizes[n].ToString(), textSizes[n]);
                }

                // Builder mode
                textBuilderModes = new EditorDropdown <TextBuilderModes>()
                {
                    Height = 24f,
                    Width  = 140f,
                };

                textBuilderModes.Add("Unlined", TextBuilderModes.Unlined);
                textBuilderModes.Add("Lined", TextBuilderModes.Lined);
                textBuilderModes.Add("Wrapped", TextBuilderModes.Wrapped);

                // Font style toggle
                IFontMin    abhaya       = FontManager.GetFont("AbhayaLibreMedium");
                GlyphFormat buttonFormat = new GlyphFormat(Color.White, TextAlignment.Center, 1.0f, abhaya.Regular);

                boldToggle = new EditorToggleButton()
                {
                    Format = buttonFormat,
                    Text   = "B",
                };

                underlineToggle = new EditorToggleButton()
                {
                    Format = buttonFormat.WithStyle(FontStyles.Underline),
                    Text   = "U",
                };

                italicToggle = new EditorToggleButton()
                {
                    Format = buttonFormat.WithStyle(FontStyles.Italic),
                    Text   = "I",
                };

                /* HudChain is useful for organizing collections of elements into straight lines with regular spacing,
                 * either vertically horizontally. In this case, I'm organizing elements horizontally from left to right
                 * in the same order indicated by the collection initializer below.
                 *
                 * HudChain and its related types, like ScrollBox and the SelectionBox types, are powerful tools for
                 * organizing UI elements, especially when used in conjunction with oneanother.
                 */
                layout = new HudChain(false, this) // Set to alignVertical false to align the elements horizontally
                {
                    // Automatically resize the height of the elements to match that of the chain and allow the chain to be
                    // wider than the total size of the members
                    SizingMode = HudChainSizingModes.FitMembersOffAxis | HudChainSizingModes.ClampChainAlignAxis,
                    // Match the height of the chain and its children to the toolbar
                    DimAlignment = DimAlignments.Height | DimAlignments.IgnorePadding,
                    // The width of the parent could very well be greater than the width of the controls.
                    ParentAlignment = ParentAlignments.Left | ParentAlignments.InnerH | ParentAlignments.UsePadding,
                    // The order the elements will appear on the toolbar from left to right.
                    CollectionContainer = { fontList, sizeList, boldToggle, underlineToggle, italicToggle, textBuilderModes }
                };

                fontList.SelectionChanged              += UpdateFormat;
                sizeList.SelectionChanged              += UpdateFormat;
                boldToggle.MouseInput.LeftClicked      += UpdateFormat;
                underlineToggle.MouseInput.LeftClicked += UpdateFormat;
                italicToggle.MouseInput.LeftClicked    += UpdateFormat;

                Height  = 30f;
                Padding = new Vector2(16f, 0f);
                _format = GlyphFormat.White;
            }
Esempio n. 6
0
            public GlyphFormat(Color color = default(Color), TextAlignment alignment = TextAlignment.Left, float textSize = 1f, FontStyles style = FontStyles.Regular, IFontMin font = null)
            {
                if (color == default(Color))
                {
                    color = Color.Black;
                }

                if (font == null)
                {
                    font = FontManager.GetFont(FontManager.Default.X);
                }

                Data = new GlyphFormatMembers((byte)alignment, textSize, font.GetStyleIndex(style), color);
            }