Also backspace in input should backspace colour code before the character.

This commit is contained in:
UnknownShadow200 2016-05-08 22:06:26 +10:00
parent 36e727beae
commit d837b31f84
8 changed files with 93 additions and 86 deletions

View file

@ -246,7 +246,7 @@ namespace ClassicalSharp.Gui {
public override void Dispose() {
if( HandlesAllInput ) {
game.chatInInputBuffer = textInput.chatInputText.ToString();
game.chatInInputBuffer = textInput.buffer.ToString();
game.CursorVisible = false;
} else {
game.chatInInputBuffer = null;
@ -321,8 +321,8 @@ namespace ClassicalSharp.Gui {
HandlesAllInput = true;
game.Keyboard.KeyRepeat = true;
textInput.chatInputText.Clear();
textInput.chatInputText.Append( 0, initialText );
textInput.buffer.Clear();
textInput.buffer.Append( 0, initialText );
textInput.Init();
}

View file

@ -44,9 +44,9 @@ namespace ClassicalSharp.Gui {
}
void TabKey() {
int pos = caretPos == -1 ? chatInputText.Length - 1 : caretPos;
int pos = caretPos == -1 ? buffer.Length - 1 : caretPos;
int start = pos;
char[] value = chatInputText.value;
char[] value = buffer.value;
while( start >= 0 && IsNameChar( value[start] ) )
start--;
@ -74,63 +74,76 @@ namespace ClassicalSharp.Gui {
if( caretPos == -1 ) pos++;
int len = pos - start;
for( int i = 0; i < len; i++ )
chatInputText.DeleteAt( start );
buffer.DeleteAt( start );
if( caretPos != -1 ) caretPos -= len;
AppendText( matches[0] );
} else if( matches.Count > 1 ) {
StringBuffer buffer = new StringBuffer( 64 );
StringBuffer sb = new StringBuffer( 64 );
int index = 0;
buffer.Append( ref index, "&e" );
buffer.AppendNum( ref index, matches.Count );
buffer.Append( ref index, " matching names: " );
sb.Append( ref index, "&e" );
sb.AppendNum( ref index, matches.Count );
sb.Append( ref index, " matching names: " );
foreach( string match in matches ) {
if( (match.Length + 1 + buffer.Length) > LineLength ) break;
buffer.Append( ref index, match );
buffer.Append( ref index, ' ' );
if( (match.Length + 1 + sb.Length) > LineLength ) break;
sb.Append( ref index, match );
sb.Append( ref index, ' ' );
}
game.Chat.Add( buffer.ToString(), MessageType.ClientStatus5 );
game.Chat.Add( sb.ToString(), MessageType.ClientStatus5 );
}
}
bool IsNameChar( char c ) {
return c == '_' || c == '.' || (c >= '0' && c <= '9')
return c == '_' || c == '.' || (c >= '0' && c <= '9')
|| (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
void BackspaceKey( bool controlDown ) {
if( controlDown ) {
if( caretPos == -1 )
caretPos = chatInputText.Length - 1;
int len = chatInputText.GetBackLength( caretPos );
caretPos = buffer.Length - 1;
int len = buffer.GetBackLength( caretPos );
caretPos -= len;
if( caretPos < 0 ) caretPos = 0;
if( caretPos != 0 ) caretPos++; // Don't remove space.
for( int i = 0; i <= len; i++ )
chatInputText.DeleteAt( caretPos );
buffer.DeleteAt( caretPos );
Dispose();
Init();
return;
}
if( !chatInputText.Empty && caretPos != 0 ) {
if( caretPos == -1 ) {
chatInputText.DeleteAt( chatInputText.Length - 1 );
} else {
caretPos--;
chatInputText.DeleteAt( caretPos );
}
} else if( !buffer.Empty && caretPos != 0 ) {
DeleteChar();
BackspaceColourCode();
Dispose();
Init();
}
}
void BackspaceColourCode() {
// If text is XYZ%eH, backspaces to XYZ.
int index = caretPos == -1 ? buffer.Length - 1 : caretPos;
if( index <= 0 ) return;
if( index == 0 || buffer.value[index - 1] != '%'
|| !game.Drawer2D.ValidColour( buffer.value[index] ) )
return;
DeleteChar(); DeleteChar();
}
void DeleteChar() {
if( caretPos == -1 ) {
buffer.DeleteAt( buffer.Length - 1 );
} else {
caretPos--;
buffer.DeleteAt( caretPos );
}
}
void DeleteKey() {
if( !chatInputText.Empty && caretPos != -1 ) {
chatInputText.DeleteAt( caretPos );
if( caretPos >= chatInputText.Length ) caretPos = -1;
if( !buffer.Empty && caretPos != -1 ) {
buffer.DeleteAt( caretPos );
if( caretPos >= buffer.Length ) caretPos = -1;
Dispose();
Init();
}
@ -139,14 +152,14 @@ namespace ClassicalSharp.Gui {
void LeftKey( bool controlDown ) {
if( controlDown ) {
if( caretPos == -1 )
caretPos = chatInputText.Length - 1;
caretPos -= chatInputText.GetBackLength( caretPos );
caretPos = buffer.Length - 1;
caretPos -= buffer.GetBackLength( caretPos );
CalculateCaretData();
return;
}
if( !chatInputText.Empty ) {
if( caretPos == -1 ) caretPos = chatInputText.Length;
if( !buffer.Empty ) {
if( caretPos == -1 ) caretPos = buffer.Length;
caretPos--;
if( caretPos < 0 ) caretPos = 0;
CalculateCaretData();
@ -155,15 +168,15 @@ namespace ClassicalSharp.Gui {
void RightKey( bool controlDown ) {
if( controlDown ) {
caretPos += chatInputText.GetForwardLength( caretPos );
if( caretPos >= chatInputText.Length ) caretPos = -1;
caretPos += buffer.GetForwardLength( caretPos );
if( caretPos >= buffer.Length ) caretPos = -1;
CalculateCaretData();
return;
}
if( !chatInputText.Empty && caretPos != -1 ) {
if( !buffer.Empty && caretPos != -1 ) {
caretPos++;
if( caretPos >= chatInputText.Length ) caretPos = -1;
if( caretPos >= buffer.Length ) caretPos = -1;
CalculateCaretData();
}
}
@ -171,7 +184,7 @@ namespace ClassicalSharp.Gui {
string originalText;
void UpKey( bool controlDown ) {
if( controlDown ) {
int pos = caretPos == -1 ? chatInputText.Length : caretPos;
int pos = caretPos == -1 ? buffer.Length : caretPos;
if( pos < LineLength ) return;
caretPos = pos - LineLength;
@ -180,12 +193,12 @@ namespace ClassicalSharp.Gui {
}
if( typingLogPos == game.Chat.InputLog.Count )
originalText = chatInputText.ToString();
originalText = buffer.ToString();
if( game.Chat.InputLog.Count > 0 ) {
typingLogPos--;
if( typingLogPos < 0 ) typingLogPos = 0;
chatInputText.Clear();
chatInputText.Append( 0, game.Chat.InputLog[typingLogPos] );
buffer.Clear();
buffer.Append( 0, game.Chat.InputLog[typingLogPos] );
caretPos = -1;
Dispose();
Init();
@ -202,13 +215,13 @@ namespace ClassicalSharp.Gui {
if( game.Chat.InputLog.Count > 0 ) {
typingLogPos++;
chatInputText.Clear();
buffer.Clear();
if( typingLogPos >= game.Chat.InputLog.Count ) {
typingLogPos = game.Chat.InputLog.Count;
if( originalText != null )
chatInputText.Append( 0, originalText );
buffer.Append( 0, originalText );
} else {
chatInputText.Append( 0, game.Chat.InputLog[typingLogPos] );
buffer.Append( 0, game.Chat.InputLog[typingLogPos] );
}
caretPos = -1;
Dispose();
@ -217,7 +230,7 @@ namespace ClassicalSharp.Gui {
}
void HomeKey() {
if( chatInputText.Empty ) return;
if( buffer.Empty ) return;
caretPos = 0;
CalculateCaretData();
}
@ -228,7 +241,7 @@ namespace ClassicalSharp.Gui {
}
bool OtherKey( Key key ) {
if( key == Key.V && chatInputText.Length < TotalChars ) {
if( key == Key.V && buffer.Length < TotalChars ) {
string text = null;
try {
text = game.window.ClipboardText;
@ -252,9 +265,9 @@ namespace ClassicalSharp.Gui {
AppendText( text );
return true;
} else if( key == Key.C ) {
if( chatInputText.Empty ) return true;
if( buffer.Empty ) return true;
try {
game.window.ClipboardText = chatInputText.ToString();
game.window.ClipboardText = buffer.ToString();
} catch( Exception ex ) {
ErrorHandler.LogError( "Copy to clipboard", ex );
const string warning = "&cError while trying to copy to clipboard.";

View file

@ -12,7 +12,7 @@ namespace ClassicalSharp.Gui {
HorizontalAnchor = Anchor.LeftOrTop;
VerticalAnchor = Anchor.BottomOrRight;
typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1.
chatInputText = new WrappableStringBuffer( 64 * lines );
buffer = new WrappableStringBuffer( 64 * lines );
DrawTextArgs args = new DrawTextArgs( "_", boldFont, true );
caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
@ -35,7 +35,7 @@ namespace ClassicalSharp.Gui {
int caretPos = -1, typingLogPos = 0;
public int YOffset;
int defaultCaretWidth, defaultWidth, defaultHeight;
internal WrappableStringBuffer chatInputText;
internal WrappableStringBuffer buffer;
readonly Font font;
FastColour caretCol;
@ -72,7 +72,7 @@ namespace ClassicalSharp.Gui {
public override void Init() {
X = 5;
chatInputText.WordWrap( game.Drawer2D, ref parts, ref partLens,
buffer.WordWrap( game.Drawer2D, ref parts, ref partLens,
LineLength, TotalChars );
for( int y = 0; y < sizes.Length; y++ )
@ -89,10 +89,10 @@ namespace ClassicalSharp.Gui {
if( sizes[0].Height == 0 ) sizes[0].Height = defaultHeight;
bool supports = game.Network.ServerSupportsPartialMessages;
if( chatInputText.Length > LineLength && !shownWarning && !supports ) {
if( buffer.Length > LineLength && !shownWarning && !supports ) {
game.Chat.Add( "&eNote: Each line will be sent as a separate packet.", MessageType.ClientStatus6 );
shownWarning = true;
} else if( chatInputText.Length <= LineLength && shownWarning ) {
} else if( buffer.Length <= LineLength && shownWarning ) {
game.Chat.Add( null, MessageType.ClientStatus6 );
shownWarning = false;
}
@ -104,8 +104,8 @@ namespace ClassicalSharp.Gui {
}
void CalculateCaretData() {
if( caretPos >= chatInputText.Length ) caretPos = -1;
chatInputText.MakeCoords( caretPos, partLens, out indexX, out indexY );
if( caretPos >= buffer.Length ) caretPos = -1;
buffer.MakeCoords( caretPos, partLens, out indexX, out indexY );
DrawTextArgs args = new DrawTextArgs( null, font, true );
if( indexX == LineLength ) {
@ -204,7 +204,7 @@ namespace ClassicalSharp.Gui {
public void SendTextInBufferAndReset() {
SendInBuffer();
typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1.
chatInputText.Clear();
buffer.Clear();
caretPos = -1;
Dispose();
Height = defaultHeight;
@ -216,8 +216,8 @@ namespace ClassicalSharp.Gui {
}
void SendInBuffer() {
if( chatInputText.Empty ) return;
string allText = chatInputText.GetString();
if( buffer.Empty ) return;
string allText = buffer.GetString();
game.Chat.InputLog.Add( allText );
if( game.Network.ServerSupportsPartialMessages )
@ -257,38 +257,38 @@ namespace ClassicalSharp.Gui {
}
public void Clear() {
chatInputText.Clear();
buffer.Clear();
for( int i = 0; i < parts.Length; i++ ) {
parts[i] = null;
}
}
public void AppendText( string text ) {
if( chatInputText.Length + text.Length > TotalChars ) {
text = text.Substring( 0, TotalChars - chatInputText.Length );
if( buffer.Length + text.Length > TotalChars ) {
text = text.Substring( 0, TotalChars - buffer.Length );
}
if( text == "" ) return;
if( caretPos == -1 ) {
chatInputText.InsertAt( chatInputText.Length, text );
buffer.InsertAt( buffer.Length, text );
} else {
chatInputText.InsertAt( caretPos, text );
buffer.InsertAt( caretPos, text );
caretPos += text.Length;
if( caretPos >= chatInputText.Length ) caretPos = -1;
if( caretPos >= buffer.Length ) caretPos = -1;
}
Dispose();
Init();
}
public void AppendChar( char c ) {
if( chatInputText.Length == TotalChars ) return;
if( buffer.Length == TotalChars ) return;
if( caretPos == -1 ) {
chatInputText.InsertAt( chatInputText.Length, c );
buffer.InsertAt( buffer.Length, c );
} else {
chatInputText.InsertAt( caretPos, c );
buffer.InsertAt( caretPos, c );
caretPos++;
if( caretPos >= chatInputText.Length ) caretPos = -1;
if( caretPos >= buffer.Length ) caretPos = -1;
}
Dispose();
Init();

View file

@ -4,7 +4,7 @@
<ProjectGuid>{BEB1C785-5CAD-48FF-A886-876BF0A318D4}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<RootNamespace>ClassicalSharp</RootNamespace>
<AssemblyName>ClassicalSharp</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>

View file

@ -98,7 +98,7 @@ namespace ClassicalSharp.Entities {
}
void TextureChanged( object sender, TextureEventArgs e ) {
if( e.Texture != "char" ) return;
if( e.Texture != "char.png" ) return;
for( int i = 0; i < Players.Length; i++ ) {
if( Players[i] == null || Players[i].TextureId != -1 ) continue;
Players[i].SkinType = game.DefaultPlayerSkinType;

View file

@ -43,25 +43,21 @@ namespace ClassicalSharp.Map {
}
}
const int chunkSize = 64 * 1024;
void WriteBlocks( NbtFile nbt, byte[] blocks ) {
byte[] chunk = new byte[chunkSize];
const int chunkSize = 64 * 1024 * 32;
nbt.Write( NbtTagType.Int8Array );
nbt.Write( "Blocks" );
nbt.WriteInt32( blocks.Length );
for( int i = 0; i < blocks.Length; i += chunkSize ) {
int count = Math.Min( chunkSize, blocks.Length - i );
for( int j = 0; j < count; j++ ) {
byte block = blocks[i + j];
//if( block > BlockInfo.CpeBlocksCount ) block = 0;
chunk[j] = block;
}
nbt.WriteBytes( chunk, count );
nbt.WriteBytes( blocks, i, count );
}
}
void WriteBlockData( NbtFile nbt, byte[] blocks ) {
void WriteBlockData( NbtFile nbt, byte[] blocks ) {
const int chunkSize = 64 * 1024;
byte[] chunk = new byte[chunkSize];
nbt.Write( NbtTagType.Int8Array );
nbt.Write( "Data" );
@ -70,7 +66,7 @@ namespace ClassicalSharp.Map {
for( int i = 0; i < blocks.Length; i += chunkSize ) {
// All 0 so we can skip this.
int count = Math.Min( chunkSize, blocks.Length - i );
nbt.WriteBytes( chunk, count );
nbt.WriteBytes( chunk, 0, count );
}
}
}

View file

@ -55,7 +55,7 @@ namespace ClassicalSharp.Map {
public void WriteBytes( byte[] v ) { writer.Write( v ); }
public void WriteBytes( byte[] v, int count ) { writer.Write( v, 0, count ); }
public void WriteBytes( byte[] v, int index, int count ) { writer.Write( v, index, count ); }
public void Write( string value ) {
ushort len = (ushort)value.Length;

View file

@ -94,9 +94,7 @@ namespace ClassicalSharp.TexturePack {
SetFontBitmap( game, stream ); break;
}
if( !name.EndsWith( ".png" ) ) return;
string tex = name.Substring( 0, name.Length - 4 );
game.Events.RaiseTextureChanged( tex );
game.Events.RaiseTextureChanged( name );
}
void SetFontBitmap( Game game, Stream stream ) {