Skip to content

Commit

Permalink
Add CampaignTagSelector with default off
Browse files Browse the repository at this point in the history
  • Loading branch information
SadPencil committed Sep 30, 2022
1 parent 6ade1e4 commit d377097
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 46 deletions.
2 changes: 2 additions & 0 deletions ClientCore/ClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ public string GetThemePath(string themeName)

public string AllowedCustomGameModes => clientDefinitionsIni.GetStringValue(SETTINGS, "AllowedCustomGameModes", "Standard,Custom Map");

public bool CampaignTagSelectorEnabled => clientDefinitionsIni.GetBooleanValue(SETTINGS, "CampaignTagSelectorEnabled", false);

public string GetGameExecutableName()
{
string[] exeNames = clientDefinitionsIni.GetStringValue(SETTINGS, "GameExecutableNames", "Game.exe").Split(',');
Expand Down
45 changes: 30 additions & 15 deletions ClientGUI/INItializableWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Rampastring.XNAUI.XNAControls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

Expand All @@ -26,29 +27,43 @@ public INItializableWindow(WindowManager windowManager) : base(windowManager)
/// instead of the window's name.
/// </summary>
protected string IniNameOverride { get; set; }
private bool VisitChild(IEnumerable<XNAControl> list, Func<XNAControl, bool> judge)
{
foreach (XNAControl child in list)
{
bool stop = judge(child);
if (stop) return true;
stop = VisitChild(child.Children, judge);
if (stop) return true;
}
return false;
}

public T FindChild<T>(string childName, bool optional = false) where T : XNAControl
{
T child = FindChild<T>(Children, childName);
if (child == null && !optional)
XNAControl result = null;
VisitChild(new List<XNAControl>() { this }, control =>
{
if (control.Name != childName) return false;
result = control;
return true;
});
if (result == null && !optional)
throw new KeyNotFoundException("Could not find required child control: " + childName);

return child;
return (T)result;
}

private T FindChild<T>(IEnumerable<XNAControl> list, string controlName) where T : XNAControl
public List<T> FindChildrenStartWith<T>(string prefix) where T : XNAControl
{
foreach (XNAControl child in list)
List<T> result = new List<T>();
VisitChild(new List<XNAControl>() { this }, (control) =>
{
if (child.Name == controlName)
return (T)child;

T childOfChild = FindChild<T>(child.Children, controlName);
if (childOfChild != null)
return childOfChild;
}

return null;
if (string.IsNullOrEmpty(prefix) ||
!string.IsNullOrEmpty(control.Name) && control.Name.StartsWith(prefix))
result.Add((T)control);
return false;
});
return result;
}

/// <summary>
Expand Down
42 changes: 34 additions & 8 deletions DXMainClient/DXGUI/Generic/CampaignSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using DTAClient.Domain;
using System.IO;
using ClientGUI;
Expand Down Expand Up @@ -57,6 +58,29 @@ public CampaignSelector(WindowManager windowManager, DiscordHandler discordHandl

private Mission missionToLaunch;

/// <summary>
/// Show missons with selected tags.
/// </summary>
/// <param name="selectedTags">Missions with at lease one of which tags to be shown. As an exception, null means show all missions.</param>
public void ParseMissionWithFilter(ISet<string> selectedTags = null)
{
Missions.Clear();

lbCampaignList.IsChangingSize = true;

lbCampaignList.Items.Clear();
lbCampaignList.SelectedIndex = -1;
// The following two operations are handled by LbCampaignList_SelectedIndexChanged
// tbMissionDescription.Text = string.Empty;
// btnLaunch.AllowClick = false;

ParseBattleIni("INI/Battle.ini", selectedTags);
ParseBattleIni("INI/" + ClientConfiguration.Instance.BattleFSFileName, selectedTags);

lbCampaignList.IsChangingSize = false;

lbCampaignList.TopIndex = 0; // reset the scroll bar
}
public override void Initialize()
{
BackgroundTexture = AssetLoader.LoadTexture("missionselectorbg.png");
Expand Down Expand Up @@ -178,8 +202,7 @@ public override void Initialize()

trbDifficultySelector.Value = UserINISettings.Instance.Difficulty;

ParseBattleIni("INI/Battle.ini");
ParseBattleIni("INI/" + ClientConfiguration.Instance.BattleFSFileName);
ParseMissionWithFilter(null);

cheaterWindow = new CheaterWindow(WindowManager);
DarkeningPanel dp = new DarkeningPanel(WindowManager);
Expand Down Expand Up @@ -222,7 +245,7 @@ private void LbCampaignList_SelectedIndexChanged(object sender, EventArgs e)

private void BtnCancel_LeftClick(object sender, EventArgs e)
{
Enabled = false;
Disable();
}

private void BtnLaunch_LeftClick(object sender, EventArgs e)
Expand Down Expand Up @@ -315,7 +338,7 @@ private void LaunchMission(Mission mission)
UserINISettings.Instance.Difficulty.Value = trbDifficultySelector.Value;
UserINISettings.Instance.SaveSettings();

((MainMenuDarkeningPanel)Parent).Hide();
Disable();

discordHandler?.UpdatePresence(mission.GUIName, difficultyName, mission.IconPath, true);
GameProcessLogic.GameProcessExited += GameProcessExited_Callback;
Expand All @@ -342,8 +365,9 @@ protected virtual void GameProcessExited()
/// Parses a Battle(E).ini file. Returns true if succesful (file found), otherwise false.
/// </summary>
/// <param name="path">The path of the file, relative to the game directory.</param>
/// <param name="selectedTags">Missions with at lease one of which tags to be shown. As an exception, null means show all missions.</param>
/// <returns>True if succesful, otherwise false.</returns>
private bool ParseBattleIni(string path)
private bool ParseBattleIni(string path, ISet<string> selectedTags = null)
{
Logger.Log("Attempting to parse " + path + " to populate mission list.");

Expand All @@ -370,8 +394,6 @@ private bool ParseBattleIni(string path)

var mission = new Mission(battleIni, battleSection);

Missions.Add(mission);

XNAListBoxItem item = new XNAListBoxItem();
item.Text = mission.GUIName;
if (!mission.Enabled)
Expand All @@ -393,7 +415,11 @@ private bool ParseBattleIni(string path)
if (!string.IsNullOrEmpty(mission.IconPath))
item.Texture = AssetLoader.LoadTexture(mission.IconPath + "icon.png");

lbCampaignList.AddItem(item);
if (selectedTags == null || mission.Tags.Intersect(selectedTags).Count() >= 1)
{
Missions.Add(mission);
lbCampaignList.AddItem(item);
}
}

Logger.Log("Finished parsing " + path + ".");
Expand Down
85 changes: 85 additions & 0 deletions DXMainClient/DXGUI/Generic/CampaignTagSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using ClientCore;
using ClientGUI;
using DTAClient.Domain;
using Microsoft.Xna.Framework;
using Rampastring.Tools;
using Rampastring.XNAUI;
using Rampastring.XNAUI.XNAControls;
using System;
using System.Collections.Generic;
using System.Linq;
using Localization;

namespace DTAClient.DXGUI.Generic
{
public class CampaignTagSelector : INItializableWindow
{
private const int DEFAULT_WIDTH = 576;
private const int DEFAULT_HEIGHT = 475;
private string _iniSectionName;
private DiscordHandler discordHandler;

public CampaignTagSelector(WindowManager windowManager, string iniName, DiscordHandler discordHandler) : base(windowManager)
{
_iniSectionName = iniName;
this.discordHandler = discordHandler;
}
protected XNAClientButton btnCancel;
protected XNAClientButton btnShowAllMission;
public override void Initialize()
{
CampaignSelector = new CampaignSelector(WindowManager, discordHandler);
DarkeningPanel.AddAndInitializeWithControl(WindowManager, CampaignSelector);
CampaignSelector.Disable();

Name = _iniSectionName;

if (!ClientConfiguration.Instance.CampaignTagSelectorEnabled) return;

ClientRectangle = new Rectangle(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
BorderColor = UISettings.ActiveSettings.PanelBorderColor;

base.Initialize();

WindowManager.CenterControlOnScreen(this);

btnCancel = FindChild<XNAClientButton>(nameof(btnCancel));
btnCancel.LeftClick += BtnCancel_LeftClick;
btnShowAllMission = FindChild<XNAClientButton>(nameof(btnShowAllMission));
btnShowAllMission.LeftClick += (sender, e) =>
{
CampaignSelector.ParseMissionWithFilter(null);
CampaignSelector.Enable();
Disable();
};

const string TagButtonsPrefix = "ButtonTag_";
var tagButtons = FindChildrenStartWith<XNAClientButton>(TagButtonsPrefix);
foreach (var tagButton in tagButtons)
{
string tagName = tagButton.Name.Substring(TagButtonsPrefix.Length);
tagButton.LeftClick += (sender, e) =>
{
CampaignSelector.ParseMissionWithFilter(new HashSet<string>() { tagName });
CampaignSelector.Enable();
Disable();
};
}
}

private void BtnCancel_LeftClick(object sender, EventArgs e)
{
Disable();
}

public void Open()
{
if (ClientConfiguration.Instance.CampaignTagSelectorEnabled)
Enable();
else
CampaignSelector.Enable();
}

CampaignSelector CampaignSelector;
}
}
31 changes: 14 additions & 17 deletions DXMainClient/DXGUI/Generic/LoadingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Rampastring.Tools;
using ClientUpdater;
using SkirmishLobby = DTAClient.DXGUI.Multiplayer.GameLobby.SkirmishLobby;
using System.Collections.Generic;
using Rampastring.XNAUI.XNAControls;

namespace DTAClient.DXGUI.Generic
{
Expand Down Expand Up @@ -131,21 +133,22 @@ private void Finish()

topBar.SetSecondarySwitch(cncnetLobby);

var campaignTagSelector = new CampaignTagSelector(WindowManager, nameof(CampaignTagSelector), discordHandler);

var mainMenu = new MainMenu(WindowManager, skirmishLobby, lanLobby,
topBar, optionsWindow, cncnetLobby, cncnetManager, discordHandler);
topBar, optionsWindow, cncnetLobby, campaignTagSelector, cncnetManager, discordHandler);
WindowManager.AddAndInitializeControl(mainMenu);

DarkeningPanel.AddAndInitializeWithControl(WindowManager, skirmishLobby);

DarkeningPanel.AddAndInitializeWithControl(WindowManager, cncnetGameLoadingLobby);

DarkeningPanel.AddAndInitializeWithControl(WindowManager, cncnetGameLobby);

DarkeningPanel.AddAndInitializeWithControl(WindowManager, cncnetLobby);

DarkeningPanel.AddAndInitializeWithControl(WindowManager, lanLobby);
List<XNAControl> controlsToBeInitialized = new List<XNAControl>()
{
skirmishLobby, cncnetGameLoadingLobby, cncnetGameLobby, cncnetLobby, lanLobby, optionsWindow, campaignTagSelector,
};

DarkeningPanel.AddAndInitializeWithControl(WindowManager, optionsWindow);
foreach (var control in controlsToBeInitialized)
{
DarkeningPanel.AddAndInitializeWithControl(WindowManager, control);
control.Disable();
}

WindowManager.AddAndInitializeControl(privateMessagingPanel);
privateMessagingPanel.AddChild(pmWindow);
Expand All @@ -154,13 +157,7 @@ private void Finish()
topBar.SetOptionsWindow(optionsWindow);

WindowManager.AddAndInitializeControl(gipw);
skirmishLobby.Disable();
cncnetLobby.Disable();
cncnetGameLobby.Disable();
cncnetGameLoadingLobby.Disable();
lanLobby.Disable();
pmWindow.Disable();
optionsWindow.Disable();

WindowManager.AddAndInitializeControl(topBar);
topBar.AddPrimarySwitchable(mainMenu);
Expand Down
6 changes: 5 additions & 1 deletion DXMainClient/DXGUI/Generic/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MainMenu : XNAWindow, ISwitchable
public MainMenu(WindowManager windowManager, SkirmishLobby skirmishLobby,
LANLobby lanLobby, TopBar topBar, OptionsWindow optionsWindow,
CnCNetLobby cncnetLobby,
CampaignTagSelector campaignTagSelector,
CnCNetManager connectionManager, DiscordHandler discordHandler) : base(windowManager)
{
this.skirmishLobby = skirmishLobby;
Expand All @@ -47,6 +48,7 @@ public MainMenu(WindowManager windowManager, SkirmishLobby skirmishLobby,
this.connectionManager = connectionManager;
this.optionsWindow = optionsWindow;
this.cncnetLobby = cncnetLobby;
this.campaignTagSelector = campaignTagSelector;
this.discordHandler = discordHandler;
cncnetLobby.UpdateCheck += CncnetLobby_UpdateCheck;
isMediaPlayerAvailable = IsMediaPlayerAvailable();
Expand All @@ -62,6 +64,8 @@ public MainMenu(WindowManager windowManager, SkirmishLobby skirmishLobby,

private SkirmishLobby skirmishLobby;

private CampaignTagSelector campaignTagSelector;

private LANLobby lanLobby;

private CnCNetManager connectionManager;
Expand Down Expand Up @@ -783,7 +787,7 @@ private void BtnOptions_LeftClick(object sender, EventArgs e)
=> optionsWindow.Open();

private void BtnNewCampaign_LeftClick(object sender, EventArgs e)
=> innerPanel.Show(innerPanel.CampaignSelector);
=> campaignTagSelector.Open();

private void BtnLoadGame_LeftClick(object sender, EventArgs e)
=> innerPanel.Show(innerPanel.GameLoadingWindow);
Expand Down
4 changes: 0 additions & 4 deletions DXMainClient/DXGUI/Generic/MainMenuDarkeningPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public MainMenuDarkeningPanel(WindowManager windowManager, DiscordHandler discor

private DiscordHandler discordHandler;

public CampaignSelector CampaignSelector;
public GameLoadingWindow GameLoadingWindow;
public StatisticsWindow StatisticsWindow;
public UpdateQueryWindow UpdateQueryWindow;
Expand All @@ -40,9 +39,6 @@ public override void Initialize()
PanelBackgroundDrawMode = PanelBackgroundImageDrawMode.STRETCHED;
Alpha = 1.0f;

CampaignSelector = new CampaignSelector(WindowManager, discordHandler);
AddChild(CampaignSelector);

GameLoadingWindow = new GameLoadingWindow(WindowManager, discordHandler);
AddChild(GameLoadingWindow);

Expand Down
4 changes: 3 additions & 1 deletion DXMainClient/Domain/Mission.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Rampastring.Tools;
using System;
using System.Collections.Generic;

namespace DTAClient.Domain
{
Expand All @@ -21,7 +22,7 @@ public Mission(IniFile iniFile, string sectionName)
Enabled = iniFile.GetBooleanValue(sectionName, nameof(Enabled), true);
BuildOffAlly = iniFile.GetBooleanValue(sectionName, nameof(BuildOffAlly), false);
PlayerAlwaysOnNormalDifficulty = iniFile.GetBooleanValue(sectionName, nameof(PlayerAlwaysOnNormalDifficulty), false);

Tags = iniFile.GetStringValue(sectionName, nameof(Tags), string.Empty).Split(',');
GUIDescription = GUIDescription.Replace("@", Environment.NewLine);
}

Expand All @@ -36,5 +37,6 @@ public Mission(IniFile iniFile, string sectionName)
public bool Enabled { get; }
public bool BuildOffAlly { get; }
public bool PlayerAlwaysOnNormalDifficulty { get; }
public string[] Tags { get; }
}
}

0 comments on commit d377097

Please sign in to comment.