コード例 #1
0
        protected override void SelfInit()
        {
            FontSettings.Color = Style.ForegroundColor;
            BackgroundColor    = Style.ButtonBackgroundColor;
            BorderColor        = Style.ButtonAccent;
            BorderWidth        = Style.ButtonBorderWidth;
            HoverColor         = Style.ButtonHoverColor;
            PressedColor       = Style.ButtonPressedColor;
            DisabledColor      = Style.ButtonDisabledColor;

            SFont = FontSettings.Init(Style);

            TextColor         = Style.ButtonTextColor;
            HoverTextColor    = Style.ButtonHoverTextColor;
            PressedTextColor  = Style.ButtonPressedTextColor;
            DisabledTextColor = Style.ButtonDisabledTextColor;

            if (AutoWidth)
            {
                // in autowidth mode, make the button as wide as the text
                int twid = TextHelper.GetWidth(SFont, Text, FontSettings);
                Width = twid + 4 + (HasBorder ? BorderWidth * 2 : 0);
            }
            else if (AutoHeight) // note the else: can't have both
            {
                // in autoheight mode, make the button as high as the text
                int thei = TextHelper.DetermineHeightFromWidth(SFont, Text, FontSettings, Width);
                Height = thei + 4 + (HasBorder ? BorderWidth * 2 : 0);
            }

            ProcessText();
        }
コード例 #2
0
ファイル: TextHelper.cs プロジェクト: n0n4/RelaUI
 public static int GetWidth(RelaFont font, string text, TextSettings settings, int prevTabDistance = 0)
 {
     if (settings != null && settings.Monospaced)
     {
         int sum = 0;
         for (int i = 0; i < text.Length; i++)
         {
             char c = text[i];
             if (c == '\t')
             {
                 sum += Draw.DetermineTabDistance((settings.TabWidth / settings.MonospaceSize) * settings.MonospaceSize, prevTabDistance + sum);
             }
             else
             {
                 sum += settings.MonospaceSize;
             }
         }
         return(sum);// text.Length * settings.MonospaceSize;
     }
     text = text.Replace("\t", "    ");
     if (font.IsDynamic)
     {
         return((int)Math.Ceiling(font.DFont.MeasureString(text).X));
     }
     return((int)Math.Ceiling(font.SFont.MeasureString(text).X));
 }
コード例 #3
0
ファイル: TextHelper.cs プロジェクト: n0n4/RelaUI
 public static int GetHeight(RelaFont font, string text, TextSettings settings)
 {
     if (font.IsDynamic)
     {
         return((int)Math.Ceiling(font.DFont.MeasureString(text).Y));
     }
     return((int)Math.Ceiling(font.SFont.MeasureString(text).Y));
 }
コード例 #4
0
        protected override void SelfInit()
        {
            FontSettings.Color = Style.ForegroundColor;
            BackgroundColor    = Style.BackgroundColor;
            BorderColor        = Style.BackgroundAccent;
            BorderWidth        = Style.BorderWidth;

            SFont = FontSettings.Init(Style);

            ProcessText();
        }
コード例 #5
0
ファイル: FontManager.cs プロジェクト: n0n4/RelaUI
        public RelaFont ResolveRelaFont(string font, int fontsize)
        {
            RelaFont rfont = new RelaFont();

            rfont.Size = fontsize;

            if (DynamicFonts.ContainsKey(font) || !Fonts.ContainsKey(font))
            {
                rfont.IsDynamic = true;
                rfont.DFont     = ResolveDynamicFont(font, fontsize);
                return(rfont);
            }

            rfont.IsDynamic = false;
            rfont.SFont     = ResolveFont(font, fontsize);
            return(rfont);
        }
コード例 #6
0
ファイル: TextHelper.cs プロジェクト: n0n4/RelaUI
        public static Tuple <int, int> GetSize(RelaFont font, string text, TextSettings settings)
        {
            if (font.IsDynamic)
            {
                if (settings != null && settings.Monospaced)
                {
                    return(new Tuple <int, int>(text.Length * settings.MonospaceSize, (int)Math.Ceiling(font.DFont.MeasureString(text).Y)));
                }
                return(new Tuple <int, int>((int)Math.Ceiling(font.DFont.MeasureString(text).X), (int)Math.Ceiling(font.DFont.MeasureString(text).Y)));
            }

            if (settings != null && settings.Monospaced)
            {
                return(new Tuple <int, int>(text.Length * settings.MonospaceSize, (int)Math.Ceiling(font.SFont.MeasureString(text).Y)));
            }
            return(new Tuple <int, int>((int)Math.Ceiling(font.SFont.MeasureString(text).X), (int)Math.Ceiling(font.SFont.MeasureString(text).Y)));
        }
コード例 #7
0
ファイル: Draw.cs プロジェクト: n0n4/RelaUI
        public static void DrawText(GraphicsDevice g, SpriteBatch s, RelaFont font, float x, float y, string text,
                                    TextSettings settings, int prevTabSum = 0)
        {
            if (settings.HasShadow && settings.ShadowColor != null)
            {
                /*s.DrawString(font,
                 *  text,
                 *  new Vector2(x + settings.ShadowDepth, y + settings.ShadowDepth),
                 *  (Color)settings.ShadowColor);*/
                DrawString(g, s, font, text, x + settings.ShadowDepth, y + settings.ShadowDepth, settings.ShadowColor, settings.Monospaced, settings.MonospaceSize, settings.TabWidth, prevTabSum);
            }

            /*s.DrawString(font,
             *  text,
             *  new Vector2(x, y),
             *  settings.Color);*/
            DrawString(g, s, font, text, x, y, settings.Color, settings.Monospaced, settings.MonospaceSize, settings.TabWidth, prevTabSum);
        }
コード例 #8
0
ファイル: Draw.cs プロジェクト: n0n4/RelaUI
 public static void DrawString(GraphicsDevice g, SpriteBatch s, RelaFont font, string text, float x, float y, Color c,
                               bool monospaced, int monosize, int tabWidth, int prevTabSum = 0)
 {
     if (!monospaced)
     {
         text = text.Replace("\t", "    ");
         if (font.IsDynamic)
         {
             s.DrawString(font.DFont, text, new Vector2(x, y), c);
         }
         else
         {
             s.DrawString(font.SFont, text, new Vector2(x, y), c);
         }
     }
     else
     {
         int sum     = 0;
         int tabsize = (tabWidth / monosize) * monosize;
         for (int i = 0; i < text.Length; i++)
         {
             char letter = text[i];
             if (letter == '\t')
             {
                 sum += DetermineTabDistance(tabsize, prevTabSum + sum);
             }
             else
             {
                 if (font.IsDynamic)
                 {
                     s.DrawString(font.DFont, "" + letter, new Vector2(x + sum, y), c);
                 }
                 else
                 {
                     s.DrawString(font.SFont, "" + letter, new Vector2(x + sum, y), c);
                 }
                 sum += monosize;
             }
         }
     }
 }
コード例 #9
0
ファイル: TextSettings.cs プロジェクト: n0n4/RelaUI
        public RelaFont Init(UIStyle Style)
        {
            Color       = Style.ForegroundColor;
            ShadowColor = Style.BackgroundAccent;

            if (string.IsNullOrWhiteSpace(FontName))
            {
                // if we have no font, inherit the default
                FontName = Style.FontManager.DefaultDynamicFont;
            }
            if (FontSize == 0)
            {
                // If we have no font size, inherit the default
                FontSize = Style.FontManager.DefaultSize;
            }

            RelaFont f = Style.FontManager.ResolveRelaFont(FontName, FontSize);

            if (FontName.ToLower().Contains("_mono"))
            {
                Monospaced    = true;
                MonospaceSize = 0;
                char[] test = new char[] { '_', '@', '#', ' ', '%', 'O', 'M', 'W' };
                foreach (char c in test)
                {
                    int size = TextHelper.GetWidth(f, "" + c, null);
                    if (size > MonospaceSize)
                    {
                        MonospaceSize = size;
                    }
                }
                if (FontName.ToLower().Contains("_monoshort"))
                {
                    MonospaceSize -= 1; // testing, put them closer together...
                }
            }

            return(f);
        }
コード例 #10
0
ファイル: Draw.cs プロジェクト: n0n4/RelaUI
        public static void DrawTextMultiStyles(GraphicsDevice g, SpriteBatch s, RelaFont font, float x, float y, string text,
                                               TextSettings[] settingsStyles, int[] styleSwitchIndices, int[] styleSwitchStyles)
        {
            // draw each chunk
            int totalWidth = 0;

            for (int styleIndex = 0; styleIndex < styleSwitchIndices.Length; styleIndex++)
            {
                int pos = styleSwitchIndices[styleIndex];
                if (pos >= text.Length)
                {
                    continue; // skip this pos, it's out of bounds
                }
                TextSettings settings = settingsStyles[styleSwitchStyles[styleIndex]];

                string subtext = null;
                if (styleIndex + 1 < styleSwitchIndices.Length)
                {
                    int nextpos = styleSwitchIndices[styleIndex + 1];
                    if (nextpos >= text.Length)
                    {
                        nextpos = text.Length;
                    }
                    if (nextpos <= pos)
                    {
                        continue; // skip this pos, the next one is right on top of it
                    }
                    subtext = text.Substring(pos, nextpos - pos);
                }
                else
                {
                    subtext = text.Substring(pos);
                }

                DrawText(g, s, font, totalWidth + x, y, subtext, settings, totalWidth);
                totalWidth += TextHelper.GetWidth(font, subtext, settings, totalWidth);
            }
        }
コード例 #11
0
ファイル: TextHelper.cs プロジェクト: n0n4/RelaUI
        public static int GetWidthMultiStyles(RelaFont font, string text,
                                              TextSettings[] settingsStyles, int[] styleSwitchIndices, int[] styleSwitchStyles)
        {
            int totalWidth = 0;

            for (int styleIndex = 0; styleIndex < styleSwitchIndices.Length; styleIndex++)
            {
                int pos = styleSwitchIndices[styleIndex];
                if (pos >= text.Length)
                {
                    continue; // skip this pos, it's out of bounds
                }
                TextSettings settings = settingsStyles[styleSwitchStyles[styleIndex]];

                string subtext = null;
                if (styleIndex + 1 < styleSwitchIndices.Length)
                {
                    int nextpos = styleSwitchIndices[styleIndex + 1];
                    if (nextpos >= text.Length)
                    {
                        nextpos = text.Length;
                    }
                    if (nextpos <= pos)
                    {
                        continue; // skip this pos, the next one is right on top of it
                    }
                    subtext = text.Substring(pos, nextpos - pos);
                }
                else
                {
                    subtext = text.Substring(pos);
                }

                totalWidth += TextHelper.GetWidth(font, subtext, settings, totalWidth);
            }
            return(totalWidth);
        }
コード例 #12
0
ファイル: Draw.cs プロジェクト: n0n4/RelaUI
 public static void DrawTextMultiStyles(GraphicsDevice g, SpriteBatch s, RelaFont font, float x, float y, string text,
                                        TextStyles styles)
 {
     DrawTextMultiStyles(g, s, font, x, y, text, styles.Styles, styles.StyleSwitchIndices, styles.StyleSwitchStyles);
 }
コード例 #13
0
ファイル: TextHelper.cs プロジェクト: n0n4/RelaUI
 public static int GetWidthMultiStyles(RelaFont font, string text,
                                       TextStyles styles)
 {
     return(GetWidthMultiStyles(font, text, styles.Styles, styles.StyleSwitchIndices, styles.StyleSwitchStyles));
 }
コード例 #14
0
ファイル: TextHelper.cs プロジェクト: n0n4/RelaUI
        // TODO: this doesn't use the height at all, you need to manually remove the overflow lines afterwards
        // this should be refactored so that the height is an optional parameter and if it is passed in
        // it will automatically remove overflow lines
        public static List <string> FitToBox(RelaFont font, string text, int w, int h, TextSettings settings, bool splitwords = false)
        {
            List <string> outs = new List <string>();

            if (text.Length <= 1)
            {
                outs.Add(text);
                return(outs);
            }
            int currentwidth = 0;
            int cstart       = 0;
            int lastspace    = 0;
            int c            = 0;

            while (c < text.Length)
            {
                if (text[c] == ' ')
                {
                    lastspace = c;
                }
                int width = GetWidth(font, text.Substring(c, 1), settings);
                if (text[c] == '\n')
                {
                    // force new line
                    if (c - cstart <= 0) // case where you have two \n in a row
                    {
                        outs.Add("");
                    }
                    else
                    {
                        outs.Add(text.Substring(cstart, c - cstart));
                    }
                    cstart       = c + 1;
                    currentwidth = 0;
                }
                else if (currentwidth + width > w)
                {
                    // we're in excess, so we need to move to a new line
                    if (splitwords || lastspace <= cstart) // failsafe, if the word is too long split it anyways
                    {
                        string line = string.Empty;
                        if ((c - 2) - cstart > 0)
                        {
                            line   = text.Substring(cstart, (c - 2) - cstart);// + "-"
                            cstart = c - 2;
                        }
                        else
                        {
                            line   = text.Substring(cstart, 1);
                            cstart = c - 1;
                        }
                        if (line[line.Length - 1] != ' ')
                        {
                            line += "-";
                        }
                        outs.Add(line);

                        currentwidth = 0;
                        if (c - cstart > 0)
                        {
                            currentwidth = GetWidth(font, text.Substring(cstart, c - cstart), settings);
                        }
                    }
                    else
                    {
                        outs.Add(text.Substring(cstart, lastspace - cstart));
                        cstart       = lastspace + 1;
                        currentwidth = 0;
                        if (c - cstart > 0)
                        {
                            currentwidth = GetWidth(font, text.Substring(cstart, c - cstart), settings);
                        }
                    }
                }
                else
                {
                    currentwidth += width;
                }
                c++;
            }
            outs.Add(text.Substring(cstart));
            return(outs);
        }
コード例 #15
0
ファイル: TextHelper.cs プロジェクト: n0n4/RelaUI
        public static int DetermineHeightFromWidth(RelaFont font, string text, TextSettings settings, int width, bool splitwords = false)
        {
            List <string> TextLines = TextHelper.FitToBox(font, text, width, 0, settings, splitwords);

            return(TextLines.Count * GetHeight(font, text, settings));
        }
コード例 #16
0
        protected override void SelfInit()
        {
            Color       = Style.BackgroundColor;
            BorderColor = Style.BackgroundAccent;
            BorderWidth = Style.BorderWidth;

            TitleBackgroundColor = Style.BackgroundAccent;

            TitleSFont = TitleFontSettings.Init(Style);

            InnerX     = InnerMargin;
            InnerY     = InnerMargin + GetTitleHeight();
            BaseInnerX = InnerX;
            BaseInnerY = InnerY;

            if (OldAutoComponents.EqualOrderContent(AutoComponents))
            {
                DoneAuto = false;
                AutoOriginalPositions = new Dictionary <int, Vector2>();
            }

            // handle auto components
            int totalheight = (int)InnerY;
            int totalwidth  = (int)InnerX;
            int ax          = 0; // GetInnerX();
            int ay          = 0; // GetInnerY();
            int c           = 0;

            foreach (UIComponent comp in AutoComponents)
            {
                // if auto has already happened, we need to revert the components to their original
                // positions before spacing them again.
                if (!DoneAuto)
                {
                    AutoOriginalPositions.Add(c, new Vector2(comp.x, comp.y));
                }
                else
                {
                    comp.x = AutoOriginalPositions[c].X;
                    comp.y = AutoOriginalPositions[c].Y;
                }
                comp.x += ax;
                comp.y += ay;
                if (AutoOrientation == eUIOrientation.HORIZONTAL)
                {
                    ax         += comp.GetWidth();
                    totalwidth += comp.GetWidth();
                    if ((int)InnerY + comp.GetHeight() > totalheight)
                    {
                        totalheight = (int)InnerY + comp.GetHeight();
                    }
                }
                else if (AutoOrientation == eUIOrientation.VERTICAL)
                {
                    ay          += comp.GetHeight();
                    totalheight += comp.GetHeight();
                    if ((int)InnerX + comp.GetWidth() > totalwidth)
                    {
                        totalwidth = (int)InnerX + comp.GetWidth();
                    }
                }
                c++;
            }
            OldAutoComponents = AutoComponents;
            DoneAuto          = true;

            if (AutoScrollHeight)
            {
                ScrollHeight = totalheight;
            }
            if (AutoScrollWidth)
            {
                ScrollWidth = totalwidth;
            }

            if (HasCloseButton)
            {
                CloseButton.Height = GetTitleHeight() - 4;
                CloseButton.Width  = CloseButton.Height;
                CloseButton.x      = Width - BorderWidth - TitleMargin - CloseButton.Width - InnerX - 2;
                CloseButton.y      = TitleMargin + BorderWidth - InnerY + 2;
            }
        }