//public IEnumerable<(char character, Image<Argb32> image)> BuildFont_new(char[] chars)
        //{
        //    var sizes = chars.Where(char.IsLetterOrDigit).Select(c => SixLabors.Fonts.TextMeasurer.Measure($"{c}", new RendererOptions()))
        //}

        public Dictionary <char, Bitmap> BuildFont(char[] chars)
        {
            var dic        = new Dictionary <char, Bitmap>();
            var sizes      = chars.Where(char.IsLetterOrDigit).Select(c => TextRenderer.MeasureText($"{c}", Font));
            var maxH       = sizes.Max(r => r.Height) + PaddingY;
            var maxW       = sizes.Max(r => r.Width) + PaddingX;
            var textformat = new StringFormat()
            {
                LineAlignment = StringAlignment.Center,
                Alignment     = StringAlignment.Center
            };

            foreach (var c in chars)
            {
                RectangleF drawArea;
                if (char.IsLetterOrDigit(c) && Monospace)
                {
                    drawArea = new RectangleF(0, 0, maxW, maxH);
                }
                else
                {
                    var size = TextRenderer.MeasureText($"{c}", Font);
                    drawArea = new RectangleF(0, 0, size.Width + PaddingX, size.Height + PaddingY);
                }

                var img = new Bitmap((int)drawArea.Width, (int)drawArea.Height);
                using (var g = Graphics.FromImage(img))
                {
                    using (var bb = new SolidBrush(BackColor))
                    {
                        g.FillRectangle(bb, drawArea);

                        if (Forecolor != Color.Transparent)
                        {
                            using (var fb = new SolidBrush(Forecolor))
                            {
                                g.DrawString($"{c}", Font, fb, drawArea, textformat);
                            }
                        }
                        else
                        {
                            var chromaKey = Color.FromArgb(byte.MaxValue - BackColor.R, byte.MaxValue - BackColor.G, byte.MaxValue - BackColor.B);
                            using (var fb = new SolidBrush(chromaKey))
                            {
                                g.DrawString($"{c}", Font, fb, drawArea, textformat);
                                g.Flush();
                                img.MakeTransparent(chromaKey);
                            }
                        }
                    }
                }

                dic.Add(c, img);
            }

            return(dic);
        }
Beispiel #2
0
        static string CalcSeparator(int total, System.Drawing.Font f, string left, string right)
        {
            var w1        = TextRenderer.MeasureText(left, f).Width;
            var w2        = TextRenderer.MeasureText(right, f).Width;
            var t1        = total - (w1 + w2);
            var separator = " ";

            while (TextRenderer.MeasureText(separator, f).Width <= t1)
            {
                separator += " ";
            }
            return(separator);
        }
Beispiel #3
0
        public override void InitializeComponent()
        {
            //Creates a System.Drawing.Font of my default font
            Font font = new Font("Ariel", 24);

            //Adds each list of game stat labels to StatLabels
            StatLabels.AddRange(new List <List <Label> > {
                Names, Wins, Eliminations, Crashes, SelfCrashes
            });

            //Sets the height and width of the screen
            int x = (int)data.size.X;
            int y = (int)data.size.Y;

            //Sets default winner name and end text
            string winnerName = "";
            string endText    = " Wins!";

            try
            {
                winnerName = data.names[data.winner - 1];
            }
            //If there is no winner, catch and change the text shown
            catch
            {
                endText = "Draw!";
            }

            //WINNER LABEL
            var Winner = new Label
            {
                Text            = winnerName + endText,
                BackgroundColor = Color.MidnightBlue,
                TextColor       = Color.White,
            };

            Winner.Size     = new Vector2(TR.MeasureText(Winner.Text, font).Width, TR.MeasureText(Winner.Text, font).Height);
            Winner.Location = new Vector2((x / 2) - Winner.Size.X / 2, (y / 16) * 3 - Winner.Size.Y / 2);
            this.Controls.Add(Winner);

            //Creates a list of the column headings as well as a single list containing all the player data
            List <string> HeadingNames = new List <string> {
                "Names", "Wins", "Eliminations", "Crashes", "SelfCrashes"
            };
            List <IList> playerData = new List <IList> {
                data.names, data.wins, data.eliminations, data.crashes, data.selfCrashes
            };

            //For each column in the table
            for (int i = 0; i < StatLabels.Count; i++)
            {
                //COLUMN HEADING LABELS
                Headings.Add(new Label
                {
                    Text            = HeadingNames[i],
                    BackgroundColor = Color.Black,
                    TextColor       = Color.White,
                });
                Headings[i].Size     = new Vector2(TR.MeasureText(Headings[i].Text, font).Width, TR.MeasureText(Headings[i].Text, font).Height);
                Headings[i].Location = new Vector2((x / 6 * (i + 1)) - Headings[i].Size.X / 2, y / 16 * 6 - Headings[i].Size.Y / 2);
                this.Controls.Add(Headings[i]);

                //For each row of data
                for (int j = 0; j < data.num; j++)
                {
                    //STAT LABELS
                    StatLabels[i].Add(new Label
                    {
                        Text            = playerData[i][j].ToString(),
                        BackgroundColor = Color.Black,
                        TextColor       = Color.White,
                    });
                    StatLabels[i][j].Size     = new Vector2(TR.MeasureText(StatLabels[i][j].Text, font).Width, TR.MeasureText(StatLabels[i][j].Text, font).Height);
                    StatLabels[i][j].Location = new Vector2((x / 6 * (i + 1)) - StatLabels[i][j].Size.X / 2, y / 16 * (7 + j) - Headings[i].Size.Y / 2);
                    this.Controls.Add(StatLabels[i][j]);
                }
            }

            //NEWROUND BUTTON
            NewRound = new Label
            {
                Text            = "New Round",
                BackgroundColor = Color.MidnightBlue,
                TextColor       = Color.White,
                Size            = new Vector2(156, 36),
            };
            NewRound.Location    = new Vector2((x / 20) * 5 - NewRound.Size.X / 2, y / 16 * 12 - NewRound.Size.Y / 2);
            NewRound.Clicked    += StartNewRound;
            NewRound.MouseEnter += NewRound_MouseOver;
            NewRound.MouseLeave += NewRound_MouseOff;
            this.Controls.Add(NewRound);

            //EXIT BUTTON
            Exit = new Label
            {
                Text            = "Exit",
                BackgroundColor = Color.MidnightBlue,
                TextColor       = Color.White,
                Size            = new Vector2(51, 37),
            };
            Exit.Location    = new Vector2((x / 20) * 15 - Exit.Size.X / 2, y / 16 * 12 - Exit.Size.Y / 2);
            Exit.Clicked    += ExitGame;
            Exit.MouseEnter += Exit_MouseOver;
            Exit.MouseLeave += Exit_MouseOff;
            this.Controls.Add(Exit);
        }