Skip to content

Commit

Permalink
Refactor design of colors in configgui.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed May 20, 2017
1 parent 527cb58 commit ae4bc56
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 1,864 deletions.
76 changes: 34 additions & 42 deletions ConfigGUI/ChatPreview.cs
Expand Up @@ -11,46 +11,17 @@
namespace fCraft.ConfigGUI {
sealed partial class ChatPreview : UserControl {

struct ColorPair {
public ColorPair( int r, int g, int b ) {
Foreground = new SolidBrush( System.Drawing.Color.FromArgb( r, g, b ) );

// 25% opacity for shadow/background colour
r /= 4; g /= 4; b /= 4;
Shadow = new SolidBrush( System.Drawing.Color.FromArgb( r, g, b ) );
}
public readonly Brush Foreground, Shadow;
}

struct ColorPair { public Brush Foreground, Shadow; }
static readonly PrivateFontCollection Fonts;
static readonly Font MinecraftFont;
static readonly ColorPair[] ColorPairs;
static Dictionary<char, ColorPair> brushes = new Dictionary<char, ColorPair>();

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),
new ColorPair(0,0,191),
new ColorPair(0,191,0),
new ColorPair(0,191,191),
new ColorPair(191,0,0),
new ColorPair(191,0,191),
new ColorPair(191,191,0),
new ColorPair(191,191,191),

new ColorPair(64,64,64),
new ColorPair(64,64,255),
new ColorPair(64,255,64),
new ColorPair(64,255,255),
new ColorPair(255,64,64),
new ColorPair(255,64,255),
new ColorPair(255,255,64),
new ColorPair(255,255,255)
};
}


Expand All @@ -62,12 +33,31 @@ struct ColorPair {

sealed class TextSegment {
public string Text;
public ColorPair Color;
public char ColorCode;
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 );
ColorPair pair;
if( !brushes.TryGetValue( ColorCode, out pair ) ) {
pair = MakeColorPair();
brushes[ColorCode] = pair;
}

g.DrawString( Text, MinecraftFont, pair.Shadow, X + 2, Y + 2 );
g.DrawString( Text, MinecraftFont, pair.Foreground, X, Y );
}

ColorPair MakeColorPair() {
ColorPair pair;
System.Drawing.Color textCol;

System.Drawing.Color c = ColorPicker.LookupColor( ColorCode, out textCol );
pair.Foreground = new SolidBrush( System.Drawing.Color.FromArgb( c.R, c.G, c.B ) );

// 25% opacity for shadow/background colour
c = System.Drawing.Color.FromArgb( c.R / 4, c.G / 4, c.B / 4 );
pair.Shadow = new SolidBrush( System.Drawing.Color.FromArgb( c.R, c.G, c.B ) );
return pair;
}
}

Expand All @@ -86,19 +76,21 @@ sealed class TextSegment {
int x = 5;
string[] plainTextSegments = SplitByColorRegex.Split( lines[i] );

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

char colorCode = 'f';
for( int j = 0; j < plainTextSegments.Length; j++ ) {
if( plainTextSegments[j].Length == 0 ) continue;
if( plainTextSegments[j][0] == '&' ) {
color = MainForm.ParseToIndex( plainTextSegments[j] );
colorCode = plainTextSegments[j][1];
// Conver system color codes into actual color codes
string converted = Color.Parse( colorCode );
if( converted != null ) colorCode = converted[1];
} else {
newSegments.Add( new TextSegment {
Color = ColorPairs[color],
Text = plainTextSegments[j],
X = x,
Y = y
} );
ColorCode = colorCode,
Text = plainTextSegments[j],
X = x,
Y = y
} );
x += (int)g.MeasureString( plainTextSegments[j], MinecraftFont ).Width;
}
}
Expand Down

0 comments on commit ae4bc56

Please sign in to comment.