Skip to content

Commit

Permalink
Implement CampaignTagSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
SadPencil committed Sep 14, 2022
1 parent f02dd02 commit 27a2678
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 31 deletions.
2 changes: 2 additions & 0 deletions ClientCore/ClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ public string GetThemePath(string themeName)

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

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

public string GetGameExecutableName()
{
string[] exeNames = clientDefinitionsIni.GetStringValue(SETTINGS, "GameExecutableNames", "Game.exe").Split(',');
Expand Down
31 changes: 23 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,17 @@ 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 ParseMissionWithFillter(ISet<string> selectedTags = null)
{
Missions.Clear();
lbCampaignList.Items.Clear();
ParseBattleIni("INI/Battle.ini", selectedTags);
ParseBattleIni("INI/" + ClientConfiguration.Instance.BattleFSFileName, selectedTags);
}
public override void Initialize()
{
BackgroundTexture = AssetLoader.LoadTexture("missionselectorbg.png");
Expand Down Expand Up @@ -178,8 +190,7 @@ public override void Initialize()

trbDifficultySelector.Value = UserINISettings.Instance.Difficulty;

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

cheaterWindow = new CheaterWindow(WindowManager);
DarkeningPanel dp = new DarkeningPanel(WindowManager);
Expand Down Expand Up @@ -222,7 +233,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 @@ -316,7 +327,8 @@ private void LaunchMission(Mission mission)
UserINISettings.Instance.Difficulty.Value = trbDifficultySelector.Value;
UserINISettings.Instance.SaveSettings();

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

discordHandler?.UpdatePresence(mission.GUIName, difficultyName, mission.IconPath, true);
GameProcessLogic.GameProcessExited += GameProcessExited_Callback;
Expand All @@ -343,8 +355,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 @@ -371,8 +384,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 @@ -394,7 +405,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
91 changes: 91 additions & 0 deletions DXMainClient/DXGUI/Generic/CampaignTagSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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.CampaignGroupSelectorEnabled) return;

ClientRectangle = new Rectangle(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
WindowManager.CenterControlOnScreen(this);

BorderColor = UISettings.ActiveSettings.PanelBorderColor;

base.Initialize();

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

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.ParseMissionWithFillter(new HashSet<string>() { tagName });
CampaignSelector.Enable();
};
}
}

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

private void Hide()
{
if (ClientConfiguration.Instance.CampaignGroupSelectorEnabled)
Disable();
else
CampaignSelector.Disable();
}

public void Show()
{
if (ClientConfiguration.Instance.CampaignGroupSelectorEnabled)
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 @@ -761,7 +765,7 @@ private void BtnOptions_LeftClick(object sender, EventArgs e)
=> optionsWindow.Open();

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

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
1 change: 1 addition & 0 deletions DXMainClient/DXMainClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@
<Compile Include="Domain\Multiplayer\LAN\HostedLANGame.cs" />
<Compile Include="Domain\Multiplayer\TeamStartMappingPreset.cs" />
<Compile Include="Domain\StatisticsSender.cs" />
<Compile Include="DXGUI\Generic\CampaignTagSelector.cs" />
<Compile Include="DXGUI\Generic\CheaterWindow.cs" />
<Compile Include="Domain\Multiplayer\Map.cs" />
<Compile Include="Domain\Multiplayer\MapLoader.cs" />
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 27a2678

Please sign in to comment.