/
InitialControlSet.cs
172 lines (149 loc) · 6.99 KB
/
InitialControlSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EOLib;
using EOLib.Config;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Threading.Tasks;
using XNAControls;
namespace EndlessClient.ControlSets
{
public class InitialControlSet : BaseControlSet
{
private readonly IConfigurationProvider _configProvider;
private readonly IMainButtonController _mainButtonController;
private IXNAButton _createAccount,
_login,
_viewCredits,
_exitGame;
private IXNALabel _versionInfo;
protected IXNAPictureBox _personPicture;
private readonly Texture2D[] _personSet1;
private readonly Random _randomGen;
private Task _mainButtonClickTask;
public override GameStates GameState => GameStates.Initial;
public InitialControlSet(IConfigurationProvider configProvider,
IMainButtonController mainButtonController)
{
_configProvider = configProvider;
_mainButtonController = mainButtonController;
_personSet1 = new Texture2D[4];
_randomGen = new Random();
}
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, contentProvider);
for (int i = 0; i < _personSet1.Length; ++i)
_personSet1[i] = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 41 + i, true);
}
protected override void InitializeControlsHelper(IControlSet currentControlSet)
{
_createAccount = GetControl(currentControlSet, GameControlIdentifier.InitialCreateAccount, GetMainCreateAccountButton);
_login = GetControl(currentControlSet, GameControlIdentifier.InitialLogin, GetMainLoginButton);
_viewCredits = GetControl(currentControlSet, GameControlIdentifier.InitialViewCredits, GetViewCreditsButton);
_exitGame = GetControl(currentControlSet, GameControlIdentifier.InitialExitGame, GetExitButton);
_versionInfo = GetControl(currentControlSet, GameControlIdentifier.InitialVersionLabel, GetVersionInfoLabel);
_personPicture = GetControl(currentControlSet, GameControlIdentifier.PersonDisplay1, GetPersonPicture1);
_allComponents.Add(_createAccount);
_allComponents.Add(_login);
_allComponents.Add(_viewCredits);
_allComponents.Add(_exitGame);
_allComponents.Add(_versionInfo);
_allComponents.Add(_personPicture);
}
public override IGameComponent FindComponentByControlIdentifier(GameControlIdentifier control)
{
switch (control)
{
case GameControlIdentifier.InitialCreateAccount: return _createAccount;
case GameControlIdentifier.InitialLogin: return _login;
case GameControlIdentifier.InitialViewCredits: return _viewCredits;
case GameControlIdentifier.InitialExitGame: return _exitGame;
case GameControlIdentifier.InitialVersionLabel: return _versionInfo;
case GameControlIdentifier.PersonDisplay1: return _personPicture;
default: return base.FindComponentByControlIdentifier(control);
}
}
private IXNAButton GetMainCreateAccountButton()
{
var button = MainButtonCreationHelper(GameControlIdentifier.InitialCreateAccount);
button.OnClick += (o, e) => AsyncMainButtonClick(_mainButtonController.ClickCreateAccount);
return button;
}
private IXNAButton GetMainLoginButton()
{
var button = MainButtonCreationHelper(GameControlIdentifier.InitialLogin);
button.OnClick += (o, e) => AsyncMainButtonClick(_mainButtonController.ClickLogin);
return button;
}
private IXNAButton GetViewCreditsButton()
{
var button = MainButtonCreationHelper(GameControlIdentifier.InitialViewCredits);
button.OnClick += (o, e) => _mainButtonController.ClickViewCredits();
return button;
}
private IXNAButton GetExitButton()
{
var button = MainButtonCreationHelper(GameControlIdentifier.InitialExitGame);
button.OnClick += (o, e) => _mainButtonController.ClickExit();
return button;
}
private void AsyncMainButtonClick(Func<Task> clickHandler)
{
if (_mainButtonClickTask == null)
{
_mainButtonClickTask = clickHandler();
_mainButtonClickTask.ContinueWith(_ => _mainButtonClickTask = null);
}
}
private IXNAButton MainButtonCreationHelper(GameControlIdentifier whichControl)
{
int i;
switch (whichControl)
{
case GameControlIdentifier.InitialCreateAccount: i = 0; break;
case GameControlIdentifier.InitialLogin: i = 1; break;
case GameControlIdentifier.InitialViewCredits: i = 2; break;
case GameControlIdentifier.InitialExitGame: i = 3; break;
default: throw new ArgumentException("Invalid control specified for helper", nameof(whichControl));
}
var widthFactor = _mainButtonTexture.Width / 2;
var heightFactor = _mainButtonTexture.Height / 4;
var outSource = new Rectangle(0, i * heightFactor, widthFactor, heightFactor);
var overSource = new Rectangle(widthFactor, i * heightFactor, widthFactor, heightFactor);
return new XNAButton(_mainButtonTexture, new Vector2(25, 280 + i * 40), outSource, overSource);
}
private IXNALabel GetVersionInfoLabel()
{
return new XNALabel(Constants.FontSize07)
{
AutoSize = true,
Text = string.Format(Constants.VersionInfoFormat,
_configProvider.VersionMajor,
_configProvider.VersionMinor,
_configProvider.VersionBuild,
_configProvider.Host,
_configProvider.Port),
ForeColor = Color.FromNonPremultiplied(190, 170, 150, 255),
DrawArea = new Rectangle(28, 457, 1, 1)
};
}
private IXNAPictureBox GetPersonPicture1()
{
var texture = _personSet1[_randomGen.Next(4)];
return new XNAPictureBox
{
Texture = texture,
DrawArea = new Rectangle(229, 70, texture.Width, texture.Height)
};
}
protected void ExcludePersonPicture1()
{
_personPicture.Dispose();
_allComponents.Remove(_personPicture);
}
}
}