Show prefix character in chat input (Thanks FrostFox), partially addresses #167.

This commit is contained in:
UnknownShadow200 2016-04-16 22:08:15 +10:00
parent 97128d295a
commit 24b5db5bf6
2 changed files with 44 additions and 30 deletions

View File

@ -289,11 +289,12 @@ namespace ClassicalSharp.Gui {
for( int y = 0; y < lines; y++ ) { for( int y = 0; y < lines; y++ ) {
string line = parts[y]; string line = parts[y];
int xOffset = y == 0 ? defaultWidth : 0;
if( line == null ) continue; if( line == null ) continue;
for( int x = 0; x < line.Length; x++ ) { for( int x = 0; x < line.Length; x++ ) {
args.Text = line.Substring( 0, x ); args.Text = line.Substring( 0, x );
int trimmedWidth = drawer.MeasureChatSize( ref args ).Width; int trimmedWidth = drawer.MeasureChatSize( ref args ).Width + xOffset;
// avoid allocating an unnecessary string // avoid allocating an unnecessary string
fixed( char* ptr = oneChar ) fixed( char* ptr = oneChar )
ptr[0] = line[x]; ptr[0] = line[x];

View File

@ -12,15 +12,17 @@ namespace ClassicalSharp.Gui {
HorizontalAnchor = Anchor.LeftOrTop; HorizontalAnchor = Anchor.LeftOrTop;
VerticalAnchor = Anchor.BottomOrRight; VerticalAnchor = Anchor.BottomOrRight;
typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1. typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1.
chatInputText = new WrappableStringBuffer( TotalChars ); chatInputText = new WrappableStringBuffer( TotalChars );
DrawTextArgs args = new DrawTextArgs( "A", boldFont, true );
Size defSize = game.Drawer2D.MeasureChatSize( ref args );
defaultWidth = defSize.Width; defaultHeight = defSize.Height;
Width = defaultWidth; Height = defaultHeight;
args = new DrawTextArgs( "_", boldFont, true ); DrawTextArgs args = new DrawTextArgs( "_", boldFont, true );
caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 ); caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
caretTex.Width = (caretTex.Width * 3) / 4;
defaultCaretWidth = caretTex.Width;
args = new DrawTextArgs( "> ", font, true );
Size defSize = game.Drawer2D.MeasureChatSize( ref args );
defaultWidth = Width = defSize.Width;
defaultHeight = Height = defSize.Height;
this.font = font; this.font = font;
altText = new AltTextInputWidget( game, font, this ); altText = new AltTextInputWidget( game, font, this );
@ -29,10 +31,10 @@ namespace ClassicalSharp.Gui {
public int RealHeight { get { return Height + altText.Height; } } public int RealHeight { get { return Height + altText.Height; } }
Texture inputTex, caretTex; Texture inputTex, caretTex, prefixTex;
int caretPos = -1, typingLogPos = 0; int caretPos = -1, typingLogPos = 0;
public int YOffset; public int YOffset;
int defaultWidth, defaultHeight; int defaultCaretWidth, defaultWidth, defaultHeight;
internal WrappableStringBuffer chatInputText; internal WrappableStringBuffer chatInputText;
readonly Font font; readonly Font font;
@ -41,14 +43,15 @@ namespace ClassicalSharp.Gui {
public override void Render( double delta ) { public override void Render( double delta ) {
api.Texturing = false; api.Texturing = false;
int y = Y, x = X; int y = Y, x = X;
for( int i = 0; i < sizes.Length; i++ ) { for( int i = 0; i < sizes.Length; i++ ) {
if( i > 0 && sizes[i].Height == 0 ) break;
bool caretAtEnd = caretTex.Y1 == y && (indexX == LineLength || caretPos == -1); bool caretAtEnd = caretTex.Y1 == y && (indexX == LineLength || caretPos == -1);
int offset = caretAtEnd ? defaultWidth : 0; int drawWidth = sizes[i].Width + (caretAtEnd ? caretTex.Width : 0);
api.Draw2DQuad( x + 5, y, sizes[i].Width + offset, sizes[i].Height, backColour );
api.Draw2DQuad( x, y, drawWidth + 10, defaultHeight, backColour );
y += sizes[i].Height; y += sizes[i].Height;
} }
if( sizes.Length == 0 || sizes[0] == Size.Empty )
api.Draw2DQuad( x + 5, y, defaultWidth, defaultHeight, backColour );
api.Texturing = true; api.Texturing = true;
inputTex.Render( api ); inputTex.Render( api );
@ -71,13 +74,18 @@ namespace ClassicalSharp.Gui {
X = 5; X = 5;
chatInputText.WordWrap( game.Drawer2D, ref parts, ref partLens, LineLength ); chatInputText.WordWrap( game.Drawer2D, ref parts, ref partLens, LineLength );
maxWidth = 0; for( int y = 0; y < sizes.Length; y++ )
sizes[y] = Size.Empty;
sizes[0].Width = defaultWidth; maxWidth = defaultWidth;
DrawTextArgs args = new DrawTextArgs( null, font, true ); DrawTextArgs args = new DrawTextArgs( null, font, true );
for( int i = 0; i < lines; i++ ) { for( int y = 0; y < lines; y++ ) {
args.Text = parts[i]; int offset = y == 0 ? defaultWidth : 0;
sizes[i] = game.Drawer2D.MeasureChatSize( ref args ); args.Text = parts[y];
maxWidth = Math.Max( maxWidth, sizes[i].Width ); sizes[y] += game.Drawer2D.MeasureChatSize( ref args );
maxWidth = Math.Max( maxWidth, sizes[y].Width );
} }
if( sizes[0].Height == 0 ) sizes[0].Height = defaultHeight;
bool supports = game.Network.ServerSupportsPartialMessages; bool supports = game.Network.ServerSupportsPartialMessages;
if( chatInputText.Length > LineLength && !shownWarning && !supports ) { if( chatInputText.Length > LineLength && !shownWarning && !supports ) {
@ -102,16 +110,18 @@ namespace ClassicalSharp.Gui {
if( indexX == LineLength ) { if( indexX == LineLength ) {
caretTex.X1 = 10 + sizes[indexY].Width; caretTex.X1 = 10 + sizes[indexY].Width;
caretCol = FastColour.Yellow; caretCol = FastColour.Yellow;
caretTex.Width = defaultCaretWidth;
} else { } else {
args.Text = parts[indexY].Substring( 0, indexX ); args.Text = parts[indexY].Substring( 0, indexX );
Size trimmedSize = game.Drawer2D.MeasureChatSize( ref args ); Size trimmedSize = game.Drawer2D.MeasureChatSize( ref args );
if( indexY == 0 ) trimmedSize.Width += defaultWidth;
caretTex.X1 = 10 + trimmedSize.Width; caretTex.X1 = 10 + trimmedSize.Width;
caretCol = FastColour.Scale( FastColour.White, 0.8f );
string line = parts[indexY]; string line = parts[indexY];
args.Text = indexX < line.Length ? new String( line[indexX], 1 ) : " "; args.Text = indexX < line.Length ? new String( line[indexX], 1 ) : "";
Size charSize = game.Drawer2D.MeasureChatSize( ref args ); caretTex.Width = indexX < line.Length ?
caretTex.Width = charSize.Width; game.Drawer2D.MeasureChatSize( ref args ).Width : defaultCaretWidth;
caretCol = FastColour.Scale( FastColour.White, 0.8f );
} }
caretTex.Y1 = sizes[0].Height * indexY + Y; caretTex.Y1 = sizes[0].Height * indexY + Y;
CalcCaretColour(); CalcCaretColour();
@ -128,7 +138,8 @@ namespace ClassicalSharp.Gui {
using( IDrawer2D drawer = game.Drawer2D ) using( IDrawer2D drawer = game.Drawer2D )
{ {
drawer.SetBitmap( bmp ); drawer.SetBitmap( bmp );
DrawTextArgs args = new DrawTextArgs( null, font, true ); DrawTextArgs args = new DrawTextArgs( "> ", font, true );
drawer.DrawChatText( ref args, 0, 0 );
for( int i = 0; i < parts.Length; i++ ) { for( int i = 0; i < parts.Length; i++ ) {
if( parts[i] == null ) break; if( parts[i] == null ) break;
@ -137,7 +148,8 @@ namespace ClassicalSharp.Gui {
if( !IDrawer2D.IsWhiteColour( lastCol ) ) if( !IDrawer2D.IsWhiteColour( lastCol ) )
args.Text = "&" + lastCol + args.Text; args.Text = "&" + lastCol + args.Text;
drawer.DrawChatText( ref args, 0, realHeight ); int offset = i == 0 ? defaultWidth : 0;
drawer.DrawChatText( ref args, offset, realHeight );
realHeight += sizes[i].Height; realHeight += sizes[i].Height;
} }
inputTex = drawer.Make2DTexture( bmp, size, 10, 0 ); inputTex = drawer.Make2DTexture( bmp, size, 10, 0 );
@ -175,6 +187,7 @@ namespace ClassicalSharp.Gui {
public void DisposeFully() { public void DisposeFully() {
Dispose(); Dispose();
api.DeleteTexture( ref caretTex ); api.DeleteTexture( ref caretTex );
api.DeleteTexture( ref prefixTex );
} }
public override void MoveTo( int newX, int newY ) { public override void MoveTo( int newX, int newY ) {