Create and implement the CpeTextColours extension.

This commit is contained in:
UnknownShadow200 2016-01-10 21:05:46 +11:00
parent 7a866f351e
commit b79de4c457
14 changed files with 57 additions and 30 deletions

View File

@ -146,6 +146,18 @@ namespace ClassicalSharp {
return Platform.CreateBmp( Utils.NextPowerOf2( size.Width ), Utils.NextPowerOf2( size.Height ) ); return Platform.CreateBmp( Utils.NextPowerOf2( size.Width ), Utils.NextPowerOf2( size.Height ) );
} }
public FastColour[] Colours = new FastColour[256];
public IDrawer2D() {
for( int i = 0; i <= 9; i++ )
Colours['0' + i] = FastColour.GetHexEncodedCol( i );
for( int i = 10; i <= 15; i++) {
Colours['a' + i - 10] = FastColour.GetHexEncodedCol( i );
Colours['A' + i - 10] = FastColour.GetHexEncodedCol( i );
}
}
protected List<TextPart> parts = new List<TextPart>( 64 ); protected List<TextPart> parts = new List<TextPart>( 64 );
protected struct TextPart { protected struct TextPart {
public string Text; public string Text;
@ -168,25 +180,31 @@ namespace ClassicalSharp {
} }
protected void SplitText( string value ) { protected void SplitText( string value ) {
int code = 0xF; char code = 'F';
for( int i = 0; i < value.Length; i++ ) { for( int i = 0; i < value.Length; i++ ) {
int nextAnd = value.IndexOf( '&', i ); int nextAnd = value.IndexOf( '&', i );
int partLength = nextAnd == -1 ? value.Length - i : nextAnd - i; int partLength = nextAnd == -1 ? value.Length - i : nextAnd - i;
if( partLength > 0 ) { if( partLength > 0 ) {
string part = value.Substring( i, partLength ); string part = value.Substring( i, partLength );
FastColour col = FastColour.GetHexEncodedCol( code ); FastColour col = Colours[code];
parts.Add( new TextPart( part, col ) ); parts.Add( new TextPart( part, col ) );
} }
i += partLength + 1; i += partLength + 1;
if( nextAnd >= 0 && nextAnd + 1 < value.Length ) { if( nextAnd >= 0 && nextAnd + 1 < value.Length ) {
if( !Utils.TryParseHex( value[nextAnd + 1], out code ) ) { if( !ValidColour( value[nextAnd + 1] ) ) {
code = 0xF; code = 'F';
i--;// include the character that isn't a colour code. i--; // include the character that isn't a colour code.
} else {
code = value[nextAnd + 1];
} }
} }
} }
} }
internal bool ValidColour( char c ) {
return Colours[c].A > 0;
}
} }
} }

View File

@ -136,7 +136,7 @@ namespace ClassicalSharp {
indices[i] = -1; indices[i] = -1;
Metadata = indices; Metadata = indices;
ChatLog chat = game.Chat; Chat chat = game.Chat;
chatIndex = chat.Log.Count - chatLines; chatIndex = chat.Log.Count - chatLines;
ResetChat(); ResetChat();
status.SetText( 0, chat.Status1.Text ); status.SetText( 0, chat.Status1.Text );

View File

@ -96,8 +96,7 @@ namespace ClassicalSharp {
// url and word both need to have %e at the start. // url and word both need to have %e at the start.
if( colIndex >= 0 && colIndex < line.Length - 1 ) { if( colIndex >= 0 && colIndex < line.Length - 1 ) {
int hex; if( game.Drawer2D.ValidColour( line[colIndex + 1] ) )
if( Utils.TryParseHex( line[colIndex + 1], out hex ) )
part = "&" + line[colIndex + 1] + part; part = "&" + line[colIndex + 1] + part;
} }
return part; return part;

View File

@ -69,7 +69,7 @@ namespace ClassicalSharp {
public override void Init() { public override void Init() {
X = 5; X = 5;
chatInputText.WordWrap( ref parts, ref partLens, 64 ); chatInputText.WordWrap( game.Drawer2D, ref parts, ref partLens, 64 );
maxWidth = 0; maxWidth = 0;
DrawTextArgs args = new DrawTextArgs( null, font, true ); DrawTextArgs args = new DrawTextArgs( null, font, true );
@ -148,16 +148,15 @@ namespace ClassicalSharp {
void CalculateCaretCol() { void CalculateCaretCol() {
int x = indexX; int x = indexX;
IDrawer2D drawer = game.Drawer2D;
for( int y = indexY; y >= 0; y-- ) { for( int y = indexY; y >= 0; y-- ) {
string part = parts[y];
if( x == partLens[y] ) x = partLens[y] - 1; if( x == partLens[y] ) x = partLens[y] - 1;
int start = parts[y].LastIndexOf( '&', x, x + 1 ); int start = part.LastIndexOf( '&', x, x + 1 );
int hex;
bool validIndex = start >= 0 && start < partLens[y] - 1; bool validIndex = start >= 0 && start < partLens[y] - 1;
if( validIndex && Utils.TryParseHex( parts[y][start + 1], out hex ) ) { if( validIndex && drawer.ValidColour( part[start + 1] ) ) {
caretCol = FastColour.GetHexEncodedCol( hex ); caretCol = drawer.Colours[part[start + 1]]; return;
return;
} }
if( y > 0 ) x = partLens[y - 1] - 1; if( y > 0 ) x = partLens[y - 1] - 1;
} }

View File

@ -6,14 +6,14 @@ using ClassicalSharp.Commands;
namespace ClassicalSharp { namespace ClassicalSharp {
public sealed class ChatLog : IDisposable { public sealed class Chat : IDisposable {
public ChatLine Status1, Status2, Status3, BottomRight1, public ChatLine Status1, Status2, Status3, BottomRight1,
BottomRight2, BottomRight3, Announcement; BottomRight2, BottomRight3, Announcement;
public ChatLine[] ClientStatus = new ChatLine[6]; public ChatLine[] ClientStatus = new ChatLine[6];
Game game; Game game;
public ChatLog( Game game ) { public Chat( Game game ) {
this.game = game; this.game = game;
} }

View File

@ -79,7 +79,7 @@ namespace ClassicalSharp {
public EntityEvents EntityEvents = new EntityEvents(); public EntityEvents EntityEvents = new EntityEvents();
public MapEvents MapEvents = new MapEvents(); public MapEvents MapEvents = new MapEvents();
public InputHandler InputHandler; public InputHandler InputHandler;
public ChatLog Chat; public Chat Chat;
public BlockHandRenderer BlockHandRenderer; public BlockHandRenderer BlockHandRenderer;
public AudioPlayer AudioPlayer; public AudioPlayer AudioPlayer;
public AxisLinesRenderer AxisLinesRenderer; public AxisLinesRenderer AxisLinesRenderer;

View File

@ -50,7 +50,7 @@ namespace ClassicalSharp {
UserViewDistance = ViewDistance; UserViewDistance = ViewDistance;
CameraClipping = Options.GetBool( OptionsKey.CameraClipping, true ); CameraClipping = Options.GetBool( OptionsKey.CameraClipping, true );
InputHandler = new InputHandler( this ); InputHandler = new InputHandler( this );
Chat = new ChatLog( this ); Chat = new Chat( this );
ParticleManager = new ParticleManager( this ); ParticleManager = new ParticleManager( this );
HudScale = Options.GetFloat( OptionsKey.HudScale, 0.25f, 5f, 1f ); HudScale = Options.GetFloat( OptionsKey.HudScale, 0.25f, 5f, 1f );
ChatScale = Options.GetFloat( OptionsKey.ChatScale, 0.35f, 5f, 1f ); ChatScale = Options.GetFloat( OptionsKey.ChatScale, 0.35f, 5f, 1f );

View File

@ -214,7 +214,7 @@ namespace ClassicalSharp.GraphicsAPI {
} }
/// <summary> Adds a warning to chat if this graphics API has problems with the current user's GPU. </summary> /// <summary> Adds a warning to chat if this graphics API has problems with the current user's GPU. </summary>
public virtual void WarnIfNecessary( ChatLog chat ) { public virtual void WarnIfNecessary( Chat chat ) {
} }
/// <summary> Informs the graphic api to update its state in preparation for a new frame. </summary> /// <summary> Informs the graphic api to update its state in preparation for a new frame. </summary>

View File

@ -393,7 +393,7 @@ namespace ClassicalSharp.GraphicsAPI {
isIntelRenderer = renderer.Contains( "Intel" ); isIntelRenderer = renderer.Contains( "Intel" );
} }
public override void WarnIfNecessary( ChatLog chat ) { public override void WarnIfNecessary( Chat chat ) {
if( !isIntelRenderer ) return; if( !isIntelRenderer ) return;
chat.Add( "&cIntel graphics cards are known to have issues with the OpenGL build." ); chat.Add( "&cIntel graphics cards are known to have issues with the OpenGL build." );

View File

@ -372,7 +372,7 @@ namespace ClassicalSharp.GraphicsAPI {
isIntelRenderer = renderer.Contains( "Intel" ); isIntelRenderer = renderer.Contains( "Intel" );
} }
public override void WarnIfNecessary( ChatLog chat ) { public override void WarnIfNecessary( Chat chat ) {
if( !isIntelRenderer ) return; if( !isIntelRenderer ) return;
chat.Add( "&cIntel graphics cards are known to have issues with the OpenGL build." ); chat.Add( "&cIntel graphics cards are known to have issues with the OpenGL build." );

View File

@ -43,6 +43,7 @@ namespace ClassicalSharp {
CpeRemoveBlockDefinition = 36, CpeRemoveBlockDefinition = 36,
CpeDefineBlockExt = 37, CpeDefineBlockExt = 37,
CpeBulkBlockUpdate = 38, CpeBulkBlockUpdate = 38,
CpeSetTextColor = 39,
} }
public enum MessageType { public enum MessageType {

View File

@ -65,7 +65,7 @@ namespace ClassicalSharp {
"HackControl", "MessageTypes", "PlayerClick", "HackControl", "MessageTypes", "PlayerClick",
"FullCP437", "LongerMessages", "FullCP437", "LongerMessages",
// proposals // proposals
"BlockDefinitions", "BlockDefinitionsExt", "BlockDefinitions", "BlockDefinitionsExt", "TextColors",
}; };
void HandleCpeExtInfo() { void HandleCpeExtInfo() {
@ -513,6 +513,17 @@ namespace ClassicalSharp {
| buffer[offset + 2] << 8 | buffer[offset + 3]; | buffer[offset + 2] << 8 | buffer[offset + 3];
} }
void HandleSetTextColor() {
FastColour col = new FastColour( reader.ReadUInt8(), reader.ReadUInt8(),
reader.ReadUInt8(), reader.ReadUInt8() );
byte code = reader.ReadUInt8();
if( code <= ' ' || code > '~' ) return; // Control chars, space, extended chars cannot be used
if( (code >= '0' && code <= '9') || (code >= 'a' && code <= 'f')
|| (code >= 'A' && code <= 'F') ) return; // Standard chars cannot be used.
game.Drawer2D.Colours[code] = col;
}
static SoundType[] stepSnds, breakSnds; static SoundType[] stepSnds, breakSnds;
static NetworkProcessor() { static NetworkProcessor() {
stepSnds = new SoundType[10]; stepSnds = new SoundType[10];

View File

@ -159,7 +159,7 @@ namespace ClassicalSharp {
131, 1, 1, 1028, 7, 9, 8, 74, 10, 7, 5, 4, 2, 131, 1, 1, 1028, 7, 9, 8, 74, 10, 7, 5, 4, 2,
66, 65, 2, 67, 69, 3, 2, 3, 134, 196, 130, 3, 66, 65, 2, 67, 69, 3, 2, 3, 134, 196, 130, 3,
8, 86, 2, 4, 66, 69, 2, 8, 138, 0, 80, 2, 85, 8, 86, 2, 4, 66, 69, 2, 8, 138, 0, 80, 2, 85,
1282, 1282, 6,
}; };
NetWriter writer; NetWriter writer;
@ -209,7 +209,7 @@ namespace ClassicalSharp {
HandleCpeSetBlockPermission, HandleCpeChangeModel, HandleCpeEnvSetMapApperance, HandleCpeSetBlockPermission, HandleCpeChangeModel, HandleCpeEnvSetMapApperance,
HandleCpeEnvWeatherType, HandleCpeHackControl, HandleCpeExtAddEntity2, HandleCpeEnvWeatherType, HandleCpeHackControl, HandleCpeExtAddEntity2,
null, HandleCpeDefineBlock, HandleCpeRemoveBlockDefinition, HandleCpeDefineBlockExt, null, HandleCpeDefineBlock, HandleCpeRemoveBlockDefinition, HandleCpeDefineBlockExt,
HandleBulkBlockUpdate, HandleBulkBlockUpdate, HandleSetTextColor,
}; };
maxHandledPacket = handlers.Length; maxHandledPacket = handlers.Length;
} }

View File

@ -10,7 +10,7 @@ namespace ClassicalSharp {
wrap = new char[capacity]; wrap = new char[capacity];
} }
public void WordWrap( ref string[] lines, ref int[] lineLens, int lineSize ) { public void WordWrap( IDrawer2D drawer, ref string[] lines, ref int[] lineLens, int lineSize ) {
int len = Length; int len = Length;
for( int i = 0; i < lines.Length; i++ ) { for( int i = 0; i < lines.Length; i++ ) {
lines[i] = null; lines[i] = null;
@ -44,7 +44,7 @@ namespace ClassicalSharp {
} }
// Output the used lines // Output the used lines
OutputLines( ref lines, linesCount, lineSize, lineLens ); OutputLines( drawer, ref lines, linesCount, lineSize, lineLens );
value = realText; value = realText;
} }
@ -58,14 +58,13 @@ namespace ClassicalSharp {
value = wrap; value = wrap;
} }
void OutputLines( ref string[] lines, int linesCount, int lineSize, int[] lineLens ) { void OutputLines( IDrawer2D drawer, ref string[] lines, int linesCount, int lineSize, int[] lineLens ) {
for( int i = 0; i < capacity; i++ ) { for( int i = 0; i < capacity; i++ ) {
if( value[i] == '\0' ) value[i] = ' '; if( value[i] == '\0' ) value[i] = ' ';
} }
// convert %0-f to &0-f for colour preview. // convert %0-f to &0-f for colour preview.
for( int i = 0; i < capacity - 1; i++ ) { for( int i = 0; i < capacity - 1; i++ ) {
int hex; if( value[i] == '%' && drawer.ValidColour( value[i + 1] ) )
if( value[i] == '%' && Utils.TryParseHex( value[i + 1], out hex ) )
value[i] = '&'; value[i] = '&';
} }