1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 12:33:01 +08:00

Add basic login flow.

This commit is contained in:
Dean Herbert 2016-11-30 16:54:15 +09:00
parent 88748499fa
commit 11f726ad45
2 changed files with 76 additions and 10 deletions

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading;
using osu.Framework;
@ -71,6 +72,7 @@ namespace osu.Game.Online.API
public void Register(IOnlineComponent component)
{
components.Add(component);
component.APIStateChanged(this, state);
}
public void Unregister(IOnlineComponent component)
@ -158,6 +160,16 @@ namespace osu.Game.Online.API
password = null;
}
public void Login(string username, string password)
{
Debug.Assert(State == APIState.Offline);
Username = username;
Password = password;
State = APIState.Connecting;
}
/// <summary>
/// Handle a single API request.
/// </summary>
@ -167,6 +179,7 @@ namespace osu.Game.Online.API
{
try
{
Logger.Log($@"Performing request {req}", LoggingTarget.Network);
req.Perform(this);
State = APIState.Online;

View File

@ -1,4 +1,5 @@
using OpenTK;
using System;
using OpenTK;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
@ -10,7 +11,7 @@ using osu.Game.Online.API;
namespace osu.Game.Overlays.Options.General
{
public class LoginOptions : OptionsSubsection
public class LoginOptions : OptionsSubsection, IOnlineComponent
{
private Container loginForm;
protected override string Header => "Sign In";
@ -31,16 +32,56 @@ namespace osu.Game.Overlays.Options.General
[BackgroundDependencyLoader(permitNulls: true)]
private void load(APIAccess api)
{
if (api == null)
return;
loginForm.Children = new Drawable[]
{
new LoginForm(api)
};
api?.Register(this);
}
public void APIStateChanged(APIAccess api, APIState state)
{
switch (state)
{
case APIState.Offline:
loginForm.Children = new Drawable[]
{
new LoginForm(api)
};
break;
case APIState.Failing:
loginForm.Children = new Drawable[]
{
new SpriteText
{
Text = @"Connection failing :(",
},
};
break;
case APIState.Connecting:
loginForm.Children = new Drawable[]
{
new SpriteText
{
Text = @"Connecting...",
},
};
break;
case APIState.Online:
loginForm.Children = new Drawable[]
{
new SpriteText
{
Text = $@"Connected as {api.Username}!",
},
};
break;
}
}
class LoginForm : FlowContainer
{
private APIAccess api;
private TextBox username;
private TextBox password;
public LoginForm(APIAccess api)
{
Direction = FlowDirection.VerticalOnly;
@ -51,16 +92,28 @@ namespace osu.Game.Overlays.Options.General
Children = new Drawable[]
{
new SpriteText { Text = "Username" },
new TextBox { Height = 20, RelativeSizeAxes = Axes.X, Text = api?.Username ?? string.Empty },
username = new TextBox { Height = 20, RelativeSizeAxes = Axes.X, Text = api?.Username ?? string.Empty },
new SpriteText { Text = "Password" },
new TextBox { Height = 20, RelativeSizeAxes = Axes.X },
password = new PasswordTextBox { Height = 20, RelativeSizeAxes = Axes.X },
new OsuButton
{
RelativeSizeAxes = Axes.X,
Text = "Log in",
Action = performLogin
}
};
}
private void performLogin()
{
api.Login(username.Text, password.Text);
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(APIAccess api)
{
this.api = api;
}
}
}
}