Launcher: Reduce code duplication.

This commit is contained in:
UnknownShadow200 2016-10-06 16:23:18 +11:00
parent 994f1cf31b
commit 1511fb4f4f
6 changed files with 47 additions and 77 deletions

View File

@ -10,21 +10,17 @@ namespace Launcher.Gui.Screens {
ChooseModeView view; ChooseModeView view;
public ChooseModeScreen( LauncherWindow game, bool firstTime ) : base( game ) { public ChooseModeScreen( LauncherWindow game, bool firstTime ) : base( game ) {
game.Window.Mouse.Move += MouseMove;
game.Window.Mouse.ButtonDown += MouseButtonDown;
view = new ChooseModeView( game ); view = new ChooseModeView( game );
view.FirstTime = firstTime; view.FirstTime = firstTime;
widgets = view.widgets; widgets = view.widgets;
} }
public override void Init() { public override void Init() {
game.Window.Keyboard.KeyDown += KeyDown; base.Init();
game.Window.Keyboard.KeyUp += KeyUp;
view.Init(); view.Init();
widgets[view.nIndex].OnClick = (x, y) => ModeClick( false, false ); widgets[view.nIndex].OnClick = (x, y) => ModeClick( false, false );
widgets[view.clIndex ].OnClick = (x, y) => ModeClick( true, false ); widgets[view.clIndex].OnClick = (x, y) => ModeClick( true, false );
widgets[view.clHaxIndex].OnClick = (x, y) => ModeClick( true, true ); widgets[view.clHaxIndex].OnClick = (x, y) => ModeClick( true, true );
if( view.backIndex >= 0 ) { if( view.backIndex >= 0 ) {
@ -36,21 +32,6 @@ namespace Launcher.Gui.Screens {
public override void Tick() { } public override void Tick() { }
void KeyDown( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Tab ) {
HandleTab();
} else if( e.Key == Key.Enter ) {
Widget widget = selectedWidget;
if( widget != null && widget.OnClick != null )
widget.OnClick( 0, 0 );
}
}
void KeyUp( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Tab )
tabDown = false;
}
public override void Resize() { public override void Resize() {
view.DrawAll(); view.DrawAll();
game.Dirty = true; game.Dirty = true;
@ -75,10 +56,7 @@ namespace Launcher.Gui.Screens {
} }
public override void Dispose() { public override void Dispose() {
game.Window.Keyboard.KeyDown -= KeyDown; base.Dispose();
game.Window.Keyboard.KeyUp -= KeyUp;
game.Window.Mouse.Move -= MouseMove;
game.Window.Mouse.ButtonDown -= MouseButtonDown;
view.Dispose(); view.Dispose();
} }
} }

View File

@ -13,13 +13,9 @@ namespace Launcher.Gui.Screens {
public InputScreen( LauncherWindow game ) : base( game ) { } public InputScreen( LauncherWindow game ) : base( game ) { }
public override void Init() { public override void Init() {
game.Window.Mouse.Move += MouseMove; base.Init();
game.Window.Mouse.ButtonDown += MouseButtonDown;
game.Window.Mouse.WheelChanged += MouseWheelChanged; game.Window.Mouse.WheelChanged += MouseWheelChanged;
game.Window.KeyPress += KeyPress; game.Window.KeyPress += KeyPress;
game.Window.Keyboard.KeyDown += KeyDown;
game.Window.Keyboard.KeyUp += KeyUp;
game.Window.Keyboard.KeyRepeat = true; game.Window.Keyboard.KeyRepeat = true;
} }
@ -49,7 +45,7 @@ namespace Launcher.Gui.Screens {
lastCaretFlash = caretShow; lastCaretFlash = caretShow;
} }
protected virtual void KeyDown( object sender, KeyboardKeyEventArgs e ) { protected override void KeyDown( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Enter && enterIndex >= 0 ) { if( e.Key == Key.Enter && enterIndex >= 0 ) {
Widget widget = (selectedWidget != null && mouseMoved) ? Widget widget = (selectedWidget != null && mouseMoved) ?
selectedWidget : widgets[enterIndex]; selectedWidget : widgets[enterIndex];
@ -94,11 +90,6 @@ namespace Launcher.Gui.Screens {
} }
} }
protected void KeyUp( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Tab )
tabDown = false;
}
protected void KeyPress( object sender, KeyPressEventArgs e ) { protected void KeyPress( object sender, KeyPressEventArgs e ) {
if( curInput != null && curInput.Chars.Append( e.KeyChar ) ) { if( curInput != null && curInput.Chars.Append( e.KeyChar ) ) {
RedrawLastInput(); RedrawLastInput();
@ -171,13 +162,9 @@ namespace Launcher.Gui.Screens {
} }
public override void Dispose() { public override void Dispose() {
game.Window.Mouse.Move -= MouseMove; base.Dispose();
game.Window.Mouse.ButtonDown -= MouseButtonDown;
game.Window.Mouse.WheelChanged -= MouseWheelChanged; game.Window.Mouse.WheelChanged -= MouseWheelChanged;
game.Window.KeyPress -= KeyPress; game.Window.KeyPress -= KeyPress;
game.Window.Keyboard.KeyDown -= KeyDown;
game.Window.Keyboard.KeyUp -= KeyUp;
game.Window.Keyboard.KeyRepeat = false; game.Window.Keyboard.KeyRepeat = false;
} }
} }

View File

@ -11,14 +11,14 @@ namespace Launcher.Gui.Screens {
ResourceFetcher fetcher; ResourceFetcher fetcher;
ResourcesView view; ResourcesView view;
public ResourcesScreen( LauncherWindow game ) : base( game ) { public ResourcesScreen( LauncherWindow game ) : base( game ) {
game.Window.Mouse.Move += MouseMove;
game.Window.Mouse.ButtonDown += MouseButtonDown;
view = new ResourcesView( game ); view = new ResourcesView( game );
widgets = view.widgets; widgets = view.widgets;
} }
public override void Init() { public override void Init() {
base.Init();
view.Init(); view.Init();
SetWidgetHandlers(); SetWidgetHandlers();
Resize(); Resize();
} }
@ -103,8 +103,7 @@ namespace Launcher.Gui.Screens {
} }
public override void Dispose() { public override void Dispose() {
game.Window.Mouse.Move -= MouseMove; base.Dispose();
game.Window.Mouse.ButtonDown -= MouseButtonDown;
view.Dispose(); view.Dispose();
} }
} }

View File

@ -19,7 +19,12 @@ namespace Launcher.Gui.Screens {
} }
/// <summary> Function called to setup the widgets and other data for this screen. </summary> /// <summary> Function called to setup the widgets and other data for this screen. </summary>
public abstract void Init(); public virtual void Init() {
game.Window.Mouse.Move += MouseMove;
game.Window.Mouse.ButtonDown += MouseButtonDown;
game.Window.Keyboard.KeyDown += KeyDown;
game.Window.Keyboard.KeyUp += KeyUp;
}
/// <summary> Function that is repeatedly called multiple times every second. </summary> /// <summary> Function that is repeatedly called multiple times every second. </summary>
public abstract void Tick(); public abstract void Tick();
@ -28,7 +33,12 @@ namespace Launcher.Gui.Screens {
public abstract void Resize(); public abstract void Resize();
/// <summary> Cleans up all native resources held by this screen. </summary> /// <summary> Cleans up all native resources held by this screen. </summary>
public abstract void Dispose(); public virtual void Dispose() {
game.Window.Mouse.Move -= MouseMove;
game.Window.Mouse.ButtonDown -= MouseButtonDown;
game.Window.Keyboard.KeyDown -= KeyDown;
game.Window.Keyboard.KeyUp -= KeyUp;
}
/// <summary> Function called when the pixels from the framebuffer /// <summary> Function called when the pixels from the framebuffer
/// are about to be transferred to the window. </summary> /// are about to be transferred to the window. </summary>
@ -75,6 +85,7 @@ namespace Launcher.Gui.Screens {
game.Dirty = true; game.Dirty = true;
} }
protected Widget lastClicked; protected Widget lastClicked;
protected void MouseButtonDown( object sender, MouseButtonEventArgs e ) { protected void MouseButtonDown( object sender, MouseButtonEventArgs e ) {
if( e.Button != MouseButton.Left ) return; if( e.Button != MouseButton.Left ) return;
@ -86,8 +97,7 @@ namespace Launcher.Gui.Screens {
lastClicked = selectedWidget; lastClicked = selectedWidget;
} }
protected virtual void WidgetUnclicked( Widget widget ) { protected virtual void WidgetUnclicked( Widget widget ) { }
}
protected virtual void MouseMove( object sender, MouseMoveEventArgs e ) { protected virtual void MouseMove( object sender, MouseMoveEventArgs e ) {
if( supressMove ) { supressMove = false; return; } if( supressMove ) { supressMove = false; return; }
@ -117,6 +127,22 @@ namespace Launcher.Gui.Screens {
selectedWidget = null; selectedWidget = null;
} }
protected virtual void KeyDown( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Tab ) {
HandleTab();
} else if( e.Key == Key.Enter ) {
Widget widget = selectedWidget;
if( widget != null && widget.OnClick != null )
widget.OnClick( 0, 0 );
}
}
protected virtual void KeyUp( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Tab )
tabDown = false;
}
protected bool tabDown = false; protected bool tabDown = false;
MouseMoveEventArgs moveArgs = new MouseMoveEventArgs(); MouseMoveEventArgs moveArgs = new MouseMoveEventArgs();
MouseButtonEventArgs pressArgs = new MouseButtonEventArgs(); MouseButtonEventArgs pressArgs = new MouseButtonEventArgs();
@ -131,7 +157,7 @@ namespace Launcher.Gui.Screens {
Utils.Clamp( ref index, 0, widgets.Length - 1 ); Utils.Clamp( ref index, 0, widgets.Length - 1 );
for( int j = 0; j < widgets.Length * 2; j++ ) { for( int j = 0; j < widgets.Length * 2; j++ ) {
int i = (j * dir + index) % widgets.Length; int i = (index + j * dir) % widgets.Length;
if( i < 0 ) i += widgets.Length; if( i < 0 ) i += widgets.Length;
if( !widgets[i].Visible || !widgets[i].TabSelectable ) continue; if( !widgets[i].Visible || !widgets[i].TabSelectable ) continue;

View File

@ -14,43 +14,25 @@ namespace Launcher.Gui.Screens {
UpdatesView view; UpdatesView view;
public UpdatesScreen( LauncherWindow game ) : base( game ) { public UpdatesScreen( LauncherWindow game ) : base( game ) {
game.Window.Mouse.Move += MouseMove;
game.Window.Mouse.ButtonDown += MouseButtonDown;
view = new UpdatesView( game ); view = new UpdatesView( game );
widgets = view.widgets; widgets = view.widgets;
} }
UpdateCheckTask checkTask; UpdateCheckTask checkTask;
public override void Init() { public override void Init() {
base.Init();
view.Init(); view.Init();
if( game.checkTask != null && game.checkTask.Done && game.checkTask.Success ) if( game.checkTask != null && game.checkTask.Done && game.checkTask.Success )
SuccessfulUpdateCheck( game.checkTask ); SuccessfulUpdateCheck( game.checkTask );
checkTask = new UpdateCheckTask(); checkTask = new UpdateCheckTask();
checkTask.CheckForUpdatesAsync(); checkTask.CheckForUpdatesAsync();
game.Window.Keyboard.KeyDown += KeyDown;
game.Window.Keyboard.KeyUp += KeyUp;
SetWidgetHandlers(); SetWidgetHandlers();
Resize(); Resize();
} }
void KeyDown( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Tab ) {
HandleTab();
} else if( e.Key == Key.Enter ) {
Widget widget = selectedWidget;
if( widget != null && widget.OnClick != null )
widget.OnClick( 0, 0 );
}
}
void KeyUp( object sender, KeyboardKeyEventArgs e ) {
if( e.Key == Key.Tab )
tabDown = false;
}
Build dev, stable; Build dev, stable;
public override void Tick() { public override void Tick() {
if( checkTask != null && checkTask.Done ) { if( checkTask != null && checkTask.Done ) {
@ -136,10 +118,7 @@ namespace Launcher.Gui.Screens {
} }
public override void Dispose() { public override void Dispose() {
game.Window.Keyboard.KeyDown -= KeyDown; base.Dispose();
game.Window.Keyboard.KeyUp -= KeyUp;
game.Window.Mouse.Move -= MouseMove;
game.Window.Mouse.ButtonDown -= MouseButtonDown;
view.Dispose(); view.Dispose();
} }
} }

View File

@ -9,7 +9,7 @@ namespace Launcher.Gui.Views {
Font buttonFont, updateFont; Font buttonFont, updateFont;
internal int loginIndex, resIndex, dcIndex, spIndex, colIndex, statusIndex; internal int loginIndex, resIndex, dcIndex, spIndex, colIndex, statusIndex;
internal int updatesIndex, modeIndex, sslIndex; internal int updatesIndex, modeIndex, sslIndex, settingsIndex;
const int buttonWidth = 220, buttonHeight = 35, sideButtonWidth = 150; const int buttonWidth = 220, buttonHeight = 35, sideButtonWidth = 150;
public MainView( LauncherWindow game ) : base( game ) { public MainView( LauncherWindow game ) : base( game ) {
@ -91,6 +91,7 @@ namespace Launcher.Gui.Views {
widgets[sslIndex].Visible = sslVisible; widgets[sslIndex].Visible = sslVisible;
widgets[sslIndex + 1].Visible = sslVisible; widgets[sslIndex + 1].Visible = sslVisible;
settingsIndex = widgetIndex;
Makers.Bitmap( this, Bitmaps.OptionsIndices, Makers.Bitmap( this, Bitmaps.OptionsIndices,
Bitmaps.OptionsPalette, Bitmaps.OptionsSize ) Bitmaps.OptionsPalette, Bitmaps.OptionsSize )
.SetLocation( Anchor.BottomOrRight, Anchor.LeftOrTop, -5, 5 ); .SetLocation( Anchor.BottomOrRight, Anchor.LeftOrTop, -5, 5 );