Optimise text input bar - don't recreate caret texture unless necessary.

This commit is contained in:
UnknownShadow200 2015-11-16 17:00:50 +11:00
parent 56b0842de8
commit 4431720311
4 changed files with 16 additions and 13 deletions

View File

@ -65,7 +65,7 @@ namespace ClassicalSharp {
public override Size MeasureSize( ref DrawTextArgs args ) { public override Size MeasureSize( ref DrawTextArgs args ) {
GetTextParts( args.Text ); GetTextParts( args.Text );
if( parts.Count == 0 ) if( parts.Count == 0 )
return Size.Empty; return Size.Empty;
SizeF total = SizeF.Empty; SizeF total = SizeF.Empty;

View File

@ -135,7 +135,7 @@ namespace ClassicalSharp {
public abstract void DisposeInstance(); public abstract void DisposeInstance();
/// <summary> Creates a 2D texture with origin at the specified window coordinates. </summary> /// <summary> Creates a 2D texture with origin at the specified window coordinates. </summary>
public Texture Make2DTexture( Bitmap bmp, Size used, int windowX, int windowY ) { public Texture Make2DTexture( Bitmap bmp, Size used, int windowX, int windowY ) {
int texId = graphics.CreateTexture( bmp ); int texId = graphics.CreateTexture( bmp );
return new Texture( texId, windowX, windowY, used.Width, used.Height, return new Texture( texId, windowX, windowY, used.Width, used.Height,
(float)used.Width / bmp.Width, (float)used.Height / bmp.Height ); (float)used.Width / bmp.Width, (float)used.Height / bmp.Height );
@ -184,11 +184,9 @@ namespace ClassicalSharp {
i += partLength + 1; i += partLength + 1;
if( nextAnd >= 0 && nextAnd + 1 < value.Length ) { if( nextAnd >= 0 && nextAnd + 1 < value.Length ) {
try { if( !Utils.TryParseHex( value[nextAnd + 1], out code ) ) {
code = Utils.ParseHex( value[nextAnd + 1] );
} catch( FormatException ) {
code = 0xF; code = 0xF;
i--; // include the character that isn't a colour code. i--;// include the character that isn't a colour code.
} }
} }
} }

View File

@ -163,7 +163,7 @@ namespace ClassicalSharp {
announcementFont.Dispose(); announcementFont.Dispose();
normalChat.Dispose(); normalChat.Dispose();
textInput.Dispose(); textInput.DisposeFully();
status.Dispose(); status.Dispose();
bottomRight.Dispose(); bottomRight.Dispose();
graphicsApi.DeleteTexture( ref announcementTex ); graphicsApi.DeleteTexture( ref announcementTex );

View File

@ -21,6 +21,9 @@ namespace ClassicalSharp {
defaultWidth = defSize.Width; defaultHeight = defSize.Height; defaultWidth = defSize.Width; defaultHeight = defSize.Height;
Width = defaultWidth; Height = defaultHeight; Width = defaultWidth; Height = defaultHeight;
args = new DrawTextArgs( "_", boldFont, true );
caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
this.font = font; this.font = font;
this.boldFont = boldFont; this.boldFont = boldFont;
altText = new AltTextInputWidget( game, font, boldFont, this ); altText = new AltTextInputWidget( game, font, boldFont, this );
@ -65,12 +68,10 @@ namespace ClassicalSharp {
public override void Init() { public override void Init() {
X = 5; X = 5;
DrawTextArgs args = new DrawTextArgs( "_", boldFont, true );
caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
chatInputText.WordWrap( ref parts, ref partLens, 64 );
chatInputText.WordWrap( ref parts, ref partLens, 64 );
maxWidth = 0; maxWidth = 0;
args = new DrawTextArgs( null, font, true ); DrawTextArgs args = new DrawTextArgs( null, font, true );
for( int i = 0; i < lines; i++ ) { for( int i = 0; i < lines; i++ ) {
args.Text = parts[i]; args.Text = parts[i];
sizes[i] = game.Drawer2D.MeasureChatSize( ref args ); sizes[i] = game.Drawer2D.MeasureChatSize( ref args );
@ -150,10 +151,14 @@ namespace ClassicalSharp {
Width = size.Width; Width = size.Width;
} }
public override void Dispose() { public override void Dispose() {
graphicsApi.DeleteTexture( ref caretTex );
graphicsApi.DeleteTexture( ref inputTex ); graphicsApi.DeleteTexture( ref inputTex );
} }
public void DisposeFully() {
Dispose();
graphicsApi.DeleteTexture( ref caretTex );
}
public override void MoveTo( int newX, int newY ) { public override void MoveTo( int newX, int newY ) {
int diffX = newX - X, diffY = newY - Y; int diffX = newX - X, diffY = newY - Y;