-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathGameSessionDropDown.cs
More file actions
211 lines (178 loc) · 7.66 KB
/
GameSessionDropDown.cs
File metadata and controls
211 lines (178 loc) · 7.66 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using ClientCore.Extensions;
using ClientCore.I18N;
using ClientGUI;
using DTAClient.Domain.Multiplayer;
using DTAClient.DXGUI.Multiplayer.GameLobby;
using Rampastring.Tools;
using Rampastring.XNAUI;
using Rampastring.XNAUI.XNAControls;
namespace DTAClient.DXGUI.Generic;
/// <summary>
/// A game option drop-down for the game lobby or campaign.
/// </summary>
// TODO split the logic between descendants better and clean up
public class GameSessionDropDown : XNAClientDropDown, IGameSessionSetting
{
private const int DEFAULT_SORT_ORDER = 0;
public GameSessionDropDown(WindowManager windowManager) : base(windowManager) { }
public string OptionName { get; private set; }
public bool AffectsSpawnIni => dataWriteMode != DropDownDataWriteMode.MAPCODE;
public bool AffectsMapCode => dataWriteMode == DropDownDataWriteMode.MAPCODE;
public bool AllowScoring => true; // TODO
private DropDownDataWriteMode dataWriteMode = DropDownDataWriteMode.BOOLEAN;
private string spawnIniOption = string.Empty;
private int defaultIndex;
/// <summary>
/// Whether this dropdown should be included in the GAME broadcast.
/// </summary>
public bool BroadcastToLobby { get; private set; }
/// <summary>
/// Whether the icon/text should be shown in the game list.
/// </summary>
public bool ShowInGameList { get; private set; }
/// <summary>
/// Whether the icon should be shown on the right side of the game list.
/// Only applies if ShowInGameList is true.
/// </summary>
public bool ShowInGameListOnRight { get; private set; }
/// <summary>
/// Whether the icon/text should be shown in the game information panel.
/// </summary>
public bool ShowInGameInformationPanel { get; private set; }
/// <summary>
/// Whether to show only the icon (without text) in the game information panel.
/// Only applies if ShowInGameInformationPanel is true.
/// </summary>
public bool ShowInGameInformationPanelAsIconOnly { get; private set; }
/// <summary>
/// Whether the icon should be shown in the game lobby control itself.
/// </summary>
public bool ShowIconInGameLobby { get; private set; }
/// <summary>
/// Whether this setting should be filterable and shown in the filters panel.
/// </summary>
public bool ShowInFilters { get; private set; }
/// <summary>
/// Sort order for displaying icons in the GameInformationPanel and GameListBox.
/// Lower values appear first.
/// </summary>
public int SortOrder { get; private set; } = DEFAULT_SORT_ORDER;
protected override void ParseControlINIAttribute(IniFile iniFile, string key, string value)
{
// shorthand for localization function
static string Localize(XNAControl control, string attributeName, string defaultValue, bool notify = true)
=> Translation.Instance.LookUp(control, attributeName, defaultValue, notify);
switch (key)
{
case "Items":
Items.Clear();
string[] items = value.SplitWithCleanup();
string[] itemLabels = iniFile.GetStringListValue(Name, "ItemLabels", "");
string[] iconNames = iniFile.GetStringListValue(Name, "Icons", "");
for (int i = 0; i < items.Length; i++)
{
bool hasLabel = itemLabels.Length > i && !string.IsNullOrEmpty(itemLabels[i]);
string iconName = iconNames.Length > i ? iconNames[i] : null;
XNADropDownItem item = new()
{
Text = Localize(this, $"Item{i}",
hasLabel ? itemLabels[i] : items[i]),
Tag = items[i],
Texture = !string.IsNullOrEmpty(iconName) ? AssetLoader.LoadTexture(iconName) : null,
};
AddItem(item);
}
return;
case "DataWriteMode":
if (value.ToUpper() == "INDEX")
dataWriteMode = DropDownDataWriteMode.INDEX;
else if (value.ToUpper() == "BOOLEAN")
dataWriteMode = DropDownDataWriteMode.BOOLEAN;
else if (value.ToUpper() == "MAPCODE")
dataWriteMode = DropDownDataWriteMode.MAPCODE;
else
dataWriteMode = DropDownDataWriteMode.STRING;
return;
case "SpawnIniOption":
spawnIniOption = value;
return;
case "DefaultIndex":
SelectedIndex = int.Parse(value);
defaultIndex = SelectedIndex;
return;
case "OptionName":
OptionName = Localize(this, "OptionName", value);
return;
case "BroadcastToLobby":
BroadcastToLobby = Conversions.BooleanFromString(value, false);
return;
case "ShowInGameList":
ShowInGameList = Conversions.BooleanFromString(value, false);
return;
case "ShowInGameListOnRight":
ShowInGameListOnRight = Conversions.BooleanFromString(value, false);
return;
case "ShowInGameInformationPanel":
ShowInGameInformationPanel = Conversions.BooleanFromString(value, false);
return;
case "ShowInGameInformationPanelAsIconOnly":
ShowInGameInformationPanelAsIconOnly = Conversions.BooleanFromString(value, false);
return;
case "ShowIconInGameLobby":
ShowIconInGameLobby = Conversions.BooleanFromString(value, false);
return;
case "ShowInFilters":
ShowInFilters = Conversions.BooleanFromString(value, false);
return;
case "SortOrder":
SortOrder = int.Parse(value);
return;
}
base.ParseControlINIAttribute(iniFile, key, value);
}
public int Value
{
get => SelectedIndex;
set => SelectedIndex = value;
}
public void ApplySpawnIniCode(IniFile spawnIni)
{
if (!AffectsSpawnIni || SelectedIndex < 0 || SelectedIndex >= Items.Count)
return;
if (String.IsNullOrEmpty(spawnIniOption))
{
Logger.Log("GameLobbyDropDown.WriteSpawnIniCode: " + Name + " has no associated spawn INI option!");
return;
}
switch (dataWriteMode)
{
case DropDownDataWriteMode.BOOLEAN:
spawnIni.SetBooleanValue("Settings", spawnIniOption, SelectedIndex > 0);
break;
case DropDownDataWriteMode.INDEX:
spawnIni.SetIntValue("Settings", spawnIniOption, SelectedIndex);
break;
default:
case DropDownDataWriteMode.STRING:
spawnIni.SetStringValue("Settings", spawnIniOption, Items[SelectedIndex].Tag.ToString());
break;
}
}
public void ApplyMapCode(IniFile mapIni, GameMode gameMode)
{
if (!AffectsMapCode || SelectedIndex < 0 || SelectedIndex >= Items.Count) return;
string customIniPath;
customIniPath = Items[SelectedIndex].Tag.ToString();
MapCodeHelper.ApplyMapCode(mapIni, customIniPath, gameMode);
}
public override void OnLeftClick(InputEventArgs inputEventArgs)
{
// FIXME there's a discrepancy with how base XNAUI handles this
// it doesn't set handled if changing the setting is not allowed
inputEventArgs.Handled = true;
if (!AllowDropDown)
return;
base.OnLeftClick(inputEventArgs);
}
}