Example #1
0
        public void SetText(string[] lines)
        {
            List <TextSegment> newSegments = new List <TextSegment>();

            using (Bitmap b = new Bitmap(1, 1)) {
                using (Graphics g = Graphics.FromImage(b)) {    // graphics for string mesaurement
                    g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;

                    int y = 5;
                    for (int i = 0; i < lines.Length; i++)
                    {
                        if (lines[i] == null || lines[i].Length == 0)
                        {
                            continue;
                        }
                        int      x = 5;
                        string[] plainTextSegments = SplitByColorRegex.Split(lines[i]);

                        int color = MainForm.ParseToIndex(Color.White);

                        for (int j = 0; j < plainTextSegments.Length; j++)
                        {
                            if (plainTextSegments[j].Length == 0)
                            {
                                continue;
                            }
                            if (plainTextSegments[j][0] == '&')
                            {
                                color = MainForm.ParseToIndex(plainTextSegments[j]);
                            }
                            else
                            {
                                newSegments.Add(new TextSegment {
                                    Color = ColorPairs[color],
                                    Text  = plainTextSegments[j],
                                    X     = x,
                                    Y     = y
                                });
                                x += (int)g.MeasureString(plainTextSegments[j], MinecraftFont).Width;
                            }
                        }
                        y += 20;
                    }
                }
            }
            segments = newSegments.ToArray();
            Invalidate();
        }