More work on caret in text fields in launcher.

This commit is contained in:
UnknownShadow200 2015-12-29 23:20:16 +11:00
parent e3ce1d41f8
commit 744e2882d7
7 changed files with 91 additions and 46 deletions

View file

@ -17,6 +17,7 @@ namespace Launcher2 {
}
public override void Tick() {
base.Tick();
LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex];
if( !game.Window.Mouse[MouseButton.Left] ) {
table.DraggingColumn = -1;

View file

@ -15,10 +15,7 @@ namespace Launcher2 {
base.Init();
Resize();
}
public override void Tick() {
}
public override void Resize() {
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );

View file

@ -26,9 +26,6 @@ namespace Launcher2 {
}
}
public override void Tick() {
}
public override void Resize() {
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );

View file

@ -28,6 +28,24 @@ namespace Launcher2 {
game.Window.Keyboard.KeyRepeat = true;
}
DateTime widgetOpenTime;
bool lastCaretFlash = false;
public override void Tick() {
double elapsed = (DateTime.UtcNow - widgetOpenTime).TotalSeconds;
bool caretShow = (elapsed % 1) < 0.5;
if( caretShow == lastCaretFlash || lastInput == null )
return;
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );
lastInput.Redraw( drawer, lastInput.Text, inputFont, inputHintFont );
if( caretShow )
lastInput.DrawCaret( drawer, inputFont );
Dirty = true;
}
lastCaretFlash = caretShow;
}
protected virtual void KeyDown( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Enter && enterIndex >= 0 ) {
LauncherWidget widget = (selectedWidget != null && mouseMoved) ?
@ -39,7 +57,6 @@ namespace Launcher2 {
}
if( lastInput == null ) return;
if( e.Key == Key.BackSpace && lastInput.BackspaceChar() ) {
RedrawLastInput();
OnRemovedChar();
@ -55,10 +72,10 @@ namespace Launcher2 {
if( lastInput.ClearText() )
RedrawLastInput();
} else if( e.Key == Key.Left ) {
lastInput.ChangeCursorPos( -1 );
lastInput.AdvanceCursorPos( -1 );
RedrawLastInput();
} else if( e.Key == Key.Right ) {
lastInput.ChangeCursorPos( +1 );
lastInput.AdvanceCursorPos( +1 );
RedrawLastInput();
}
}
@ -82,6 +99,14 @@ namespace Launcher2 {
}
}
protected override void SelectWidget( LauncherWidget widget ) {
base.SelectWidget( widget );
if( widget is LauncherInputWidget ) {
widgetOpenTime = DateTime.UtcNow;
lastCaretFlash = false;
}
}
protected virtual void RedrawLastInput() {
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );
@ -89,7 +114,6 @@ namespace Launcher2 {
game.ClearArea( lastInput.X, lastInput.Y,
lastInput.Width + 1, lastInput.Height + 1 );
lastInput.Redraw( drawer, lastInput.Text, inputFont, inputHintFont );
lastInput.DrawCursor( inputFont, drawer );
Dirty = true;
}
}
@ -121,6 +145,9 @@ namespace Launcher2 {
}
input.Active = true;
widgetOpenTime = DateTime.UtcNow;
lastCaretFlash = false;
input.SetCaretToCursor( mouseX, mouseY, drawer, inputFont );
input.Redraw( drawer, input.Text, inputFont, inputHintFont );
}
lastInput = input;

View file

@ -19,6 +19,7 @@ namespace Launcher2 {
}
public override void Tick() {
base.Tick();
if( !signingIn ) return;
ClassicubeSession session = game.Session;

View file

@ -63,7 +63,6 @@ namespace Launcher2 {
if( Text.Length != 0 || HintText == null ) {
int y = Y + 2 + (Height - size.Height) / 2;
drawer.DrawText( ref args, X + 5, y );
DrawCursor( font, drawer );
} else {
args.SkipPartsCheck = false;
args.Text = HintText;
@ -76,38 +75,6 @@ namespace Launcher2 {
}
}
public void DrawCursor( Font font, IDrawer2D drawer ) {
string text = Text;
if( Password )
text = new String( '*', text.Length );
DrawTextArgs args = new DrawTextArgs( text, font, true );
if( CaretPos == -1 ) {
Size size = drawer.MeasureSize( ref args );
drawer.Clear( FastColour.White, X + 5 + size.Width,
Y + Height - 5, 10, 2 );
} else {
args.Text = text.Substring( 0, CaretPos );
Size trimmedSize = drawer.MeasureChatSize( ref args );
args.Text = new String( text[CaretPos], 1 );
Size charSize = drawer.MeasureChatSize( ref args );
drawer.Clear( FastColour.White, X + 5 + trimmedSize.Width,
Y + Height - 5, charSize.Width, 2 );
}
}
public void ChangeCursorPos( int dir ) {
if( CaretPos == 0 && dir == -1 )
return;
if( CaretPos == -1 && dir == -1 )
CaretPos = Text.Length;
CaretPos += dir;
if( CaretPos < 0 || CaretPos >= Text.Length )
CaretPos = -1;
}
/// <summary> Appends a character to the end of the currently entered text. </summary>
/// <returns> true if a redraw is necessary, false otherwise. </returns>
public bool AppendChar( char c ) {
@ -192,5 +159,60 @@ namespace Launcher2 {
if( TextChanged != null ) TextChanged( this );
return true;
}
public void DrawCaret( IDrawer2D drawer, Font font ) {
string text = Text;
if( Password )
text = new String( '*', text.Length );
DrawTextArgs args = new DrawTextArgs( text, font, true );
if( CaretPos == -1 ) {
Size size = drawer.MeasureSize( ref args );
drawer.Clear( FastColour.White, X + 5 + size.Width,
Y + Height - 5, 10, 2 );
} else {
args.Text = text.Substring( 0, CaretPos );
Size trimmedSize = drawer.MeasureChatSize( ref args );
args.Text = new String( text[CaretPos], 1 );
Size charSize = drawer.MeasureChatSize( ref args );
drawer.Clear( FastColour.White, X + 5 + trimmedSize.Width,
Y + Height - 5, charSize.Width, 2 );
}
}
public void AdvanceCursorPos( int dir ) {
if( (CaretPos == 0 && dir == -1) || (CaretPos == -1 && dir == 1) )
return;
if( CaretPos == -1 && dir == -1 )
CaretPos = Text.Length;
CaretPos += dir;
if( CaretPos < 0 || CaretPos >= Text.Length )
CaretPos = -1;
}
public void SetCaretToCursor( int mouseX, int mouseY, IDrawer2D drawer, Font font ) {
string text = Text;
if( Password )
text = new String( '*', text.Length );
mouseX -= X; mouseY -= Y;
DrawTextArgs args = new DrawTextArgs( text, font, true );
Size size = drawer.MeasureSize( ref args );
if( mouseX >= size.Width ) {
CaretPos = -1; return;
}
for( int i = 0; i < Text.Length; i++ ) {
args.Text = text.Substring( 0, i );
int trimmedWidth = drawer.MeasureChatSize( ref args ).Width;
args.Text = new String( text[i], 1 );
int charWidth = drawer.MeasureChatSize( ref args ).Width;
if( mouseX >= trimmedWidth && mouseX < trimmedWidth + charWidth ) {
CaretPos = i; return;
}
}
}
}
}

View file

@ -4,7 +4,7 @@
<ProjectGuid>{3E84ACC1-27B4-401B-A359-6AAE4DF6C9B5}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<RootNamespace>Launcher2</RootNamespace>
<AssemblyName>Launcher2</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
@ -20,7 +20,7 @@
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<BaseAddress>4194304</BaseAddress>
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>