-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathChatPreview.cs
128 lines (108 loc) · 4.74 KB
/
ChatPreview.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2009-2013 Matvei Stefarov <[email protected]>
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using fCraft.ConfigGUI.Properties;
namespace fCraft.ConfigGUI {
sealed partial class ChatPreview : UserControl {
private struct ColorPair {
public ColorPair( int r, int g, int b, int sr, int sg, int sb ) {
Foreground = new SolidBrush( System.Drawing.Color.FromArgb( r, g, b ) );
Shadow = new SolidBrush( System.Drawing.Color.FromArgb( sr, sg, sb ) );
}
public readonly Brush Foreground, Shadow;
}
private static readonly PrivateFontCollection Fonts;
private static readonly Font MinecraftFont;
private static readonly ColorPair[] ColorPairs;
unsafe static ChatPreview() {
Fonts = new PrivateFontCollection();
fixed ( byte* fontPointer = Resources.MinecraftFont ) {
Fonts.AddMemoryFont( ( IntPtr )fontPointer, Resources.MinecraftFont.Length );
}
MinecraftFont = new Font( Fonts.Families[0], 12, FontStyle.Regular );
ColorPairs = new[]{
new ColorPair(0,0,0,0,0,0),
new ColorPair(0,0,191,0,0,47),
new ColorPair(0,191,0,0,47,0),
new ColorPair(0,191,191,0,47,47),
new ColorPair(191,0,0,47,0,0),
new ColorPair(191,0,191,47,0,47),
new ColorPair(191,191,0,47,47,0),
new ColorPair(191,191,191,47,47,47),
new ColorPair(64,64,64,16,16,16),
new ColorPair(64,64,255,16,16,63),
new ColorPair(64,255,64,16,63,16),
new ColorPair(64,255,255,16,63,63),
new ColorPair(255,64,64,63,16,16),
new ColorPair(255,64,255,63,16,63),
new ColorPair(255,255,64,63,63,16),
new ColorPair(255,255,255,63,63,63)
};
}
public ChatPreview() {
InitializeComponent();
DoubleBuffered = true;
}
private sealed class TextSegment {
public string Text;
public ColorPair Color;
public int X, Y;
public void Draw( Graphics g ) {
g.DrawString( Text, MinecraftFont, Color.Shadow, X + 2, Y + 2 );
g.DrawString( Text, MinecraftFont, Color.Foreground, X, Y );
}
}
private static readonly Regex SplitByColorRegex = new Regex( "(&[0-9a-zA-Z])", RegexOptions.Compiled );
private TextSegment[] segments;
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;
foreach (string t in lines)
{
if ( string.IsNullOrEmpty(t) )
continue;
int x = 5;
string[] plainTextSegments = SplitByColorRegex.Split( t );
int color = Color.ParseToIndex( Color.White );
foreach (string t1 in plainTextSegments.Where(t1 => t1.Length != 0))
{
if ( t1[0] == '&' ) {
color = Color.ParseToIndex( t1 );
} else {
newSegments.Add( new TextSegment {
Color = ColorPairs[color],
Text = t1,
X = x,
Y = y
} );
x += ( int )g.MeasureString( t1, MinecraftFont ).Width;
}
}
y += 20;
}
}
}
segments = newSegments.ToArray();
Invalidate();
}
protected override void OnPaint( PaintEventArgs e ) {
e.Graphics.DrawImageUnscaledAndClipped( Resources.ChatBackground, e.ClipRectangle );
e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
if ( segments != null && segments.Length > 0 ) {
foreach (TextSegment t in segments)
{
t.Draw( e.Graphics );
}
}
base.OnPaint( e );
}
}
}