1
0
mirror of https://github.com/YellowberryHN/WACCALauncher synced 2024-12-18 02:05:58 +01:00

Finished rewriting menu logic with custom controls

This commit is contained in:
Zsolt Zitting 2023-08-25 00:56:01 -07:00
parent d62004c414
commit 18e493e9c6
2 changed files with 80 additions and 153 deletions

View File

@ -10,9 +10,7 @@ using System.Diagnostics;
using IniParser; using IniParser;
using IniParser.Model; using IniParser.Model;
using SharpDX.DirectInput; using SharpDX.DirectInput;
using System.Collections;
using System.Linq; using System.Linq;
using IniParser.Exceptions;
using System.Reflection; using System.Reflection;
namespace WACCALauncher namespace WACCALauncher
@ -211,7 +209,6 @@ namespace WACCALauncher
_loadingLabel.Hide(); _loadingLabel.Hide();
menuLabel.Show(); menuLabel.Show();
waccaListTest.Visible = waccaListTest.Enabled = true; waccaListTest.Visible = waccaListTest.Enabled = true;
waccaListTest.SelectedIndex = 0;
_autoLaunch = false; _autoLaunch = false;
} }
@ -228,17 +225,20 @@ namespace WACCALauncher
_delayTimer.Enabled = true; _delayTimer.Enabled = true;
} }
public void MenuBack()
{
// back from current menu item
Console.WriteLine("MenuBack");
if (_menuManager.GetCurrentMenu().ParentMenu == null)
MenuHide();
else _menuManager.NavigateBack();
}
private void MenuSelect() private void MenuSelect()
{ {
// select menu item // select menu item
Console.WriteLine("MenuSelect"); Console.WriteLine("MenuSelect");
(waccaListTest.SelectedItem as ConfigMenuItem).Select(this); (waccaListTest.SelectedItem as ConfigMenu).Select(this);
}
private void MenuBack()
{
// back from current menu item
Console.WriteLine("MenuBack");
} }
/* /*
@ -307,70 +307,34 @@ namespace WACCALauncher
LoadVersionsFromConfig(); LoadVersionsFromConfig();
var defVerMenu = new List<ConfigMenuItem>(); var mainMenu = new ConfigMenu("Launcher Settings", items: new List<ConfigMenu>() {
new ConfigMenu("set default version", ConfigMenuAction.Menu, items: GetDefaultVersionMenu()),
foreach (var ver in Versions) new ConfigMenu("test VFD", ConfigMenuAction.Command, method: vfd_test),
{ new ConfigMenu("exit to windows", ConfigMenuAction.Command, method: Application.Exit),
var name = ver.GameVersion == VersionType.Custom ? ver.CustomName : ver.ToString(); new ConfigMenu("launch game", ConfigMenuAction.Return)
defVerMenu.Add(new ConfigMenuItem($"({(ver == DefaultVer ? 'X' : ' ')}) {name}", ConfigMenuAction.VersionSelect, version: ver));
}
defVerMenu.Add(new ConfigMenuItem("Return to settings", ConfigMenuAction.Return));
var mainMenu = new ConfigMenu(new List<ConfigMenuItem>() {
new ConfigMenuItem("set default version", ConfigMenuAction.Submenu, submenu: defVerMenu),
new ConfigMenuItem("test VFD", ConfigMenuAction.Command, method: vfd_test),
new ConfigMenuItem("exit to windows", ConfigMenuAction.Command, method: Application.Exit),
new ConfigMenuItem("launch game", ConfigMenuAction.Return)
}); });
_menuManager = new MenuManager(mainMenu); _menuManager = new MenuManager(mainMenu, waccaListTest);
waccaListTest.AssignMenuManager(_menuManager);
_loadingLabel.Font = _menuFont; _loadingLabel.Font = _menuFont;
menuLabel.Font = _menuFont; menuLabel.Font = _menuFont;
} }
/* public List<ConfigMenu> GetDefaultVersionMenu()
{
var defVerMenu = new List<ConfigMenu>();
public void GenerateMenu(ConfigMenu menu, int selectedIndex = 0) foreach (var ver in Versions)
{ {
if (menu == null || menu == CurrentMenu) return; var name = ver.GameVersion == VersionType.Custom ? ver.CustomName : ver.ToString();
if(CurrentMenu != null) defVerMenu.Add(new ConfigMenu($"({(ver == DefaultVer ? 'X' : ' ')}) {name}", ConfigMenuAction.VersionSelect, version: ver));
{
menu.ParentMenu = CurrentMenu;
foreach (var item in CurrentMenu)
{
item.label.Dispose();
}
} }
CurrentMenu = menu; defVerMenu.Add(new ConfigMenu("Return to settings", ConfigMenuAction.Return));
_currentMenuItem = selectedIndex;
menuLabel.Text = menu.Name.ToUpper(); return defVerMenu;
var menuIndex = 0;
foreach (var item in menu)
{
item.label = new Label();
item.label.Text = item.Name.ToUpper();
item.label.ForeColor = Color.White;
item.label.TextAlign = ContentAlignment.MiddleLeft;
item.label.AutoSize = false;
item.label.Size = new Size(700, 30);
item.label.Font = _menuFont;
item.label.Location = new Point(200, 240 + (40 * menuIndex));
item.label.Parent = this;
this.Controls.Add(item.label);
menuIndex++;
} }
RefreshMenu();
}
*/
private static void KillExplorer() private static void KillExplorer()
{ {
Process.Start(@"C:\Windows\System32\taskkill.exe", @"/F /IM explorer.exe"); Process.Start(@"C:\Windows\System32\taskkill.exe", @"/F /IM explorer.exe");
@ -391,7 +355,6 @@ namespace WACCALauncher
//this.Hide(); //this.Hide();
_gameProcess.Exited += QuitLauncher; _gameProcess.Exited += QuitLauncher;
_gameProcess.Start(); _gameProcess.Start();
//Application.Exit();
} }
public void LaunchGame(VersionType type) public void LaunchGame(VersionType type)
@ -578,42 +541,24 @@ namespace WACCALauncher
public enum ConfigMenuAction public enum ConfigMenuAction
{ {
None = 0, None = 0,
Menu,
Command, Command,
Submenu,
VersionSelect, VersionSelect,
ItemSelect, ItemSelect,
Return Return
} }
public class ConfigMenuItem public class ConfigMenu
{ {
public readonly string Name; public string Name;
public List<ConfigMenu> Items;
public ConfigMenu ParentMenu;
private readonly ConfigMenuAction _action; private readonly ConfigMenuAction _action;
private readonly Action _method; private readonly Action _method;
private readonly ConfigMenu ParentMenu;
public ConfigMenu Submenu { get; private set; }
private readonly List<string> _options; private readonly List<string> _options;
private readonly Version _version; private readonly Version _version;
public ConfigMenuItem(string name, ConfigMenuAction action = ConfigMenuAction.None, Action method = null, ConfigMenu submenu = null, List<string> options = null, Version version = null) {
this.Name = name;
this._action = action;
if (action == ConfigMenuAction.Command && method == null)
throw new ArgumentException($"Menu item '{name}' was defined with Command type, but has no method associated.");
else if (action == ConfigMenuAction.Submenu && submenu == null)
throw new ArgumentException($"Menu item '{name}' was defined with Submenu type, but has no submenu associated.");
else if (action == ConfigMenuAction.ItemSelect && options == null)
throw new ArgumentException($"Menu item '{name}' was defined with ItemSelect type, but has no options associated.");
else if (action == ConfigMenuAction.VersionSelect && version == null)
throw new ArgumentException($"Menu item '{name}' was defined with VersionSelect type, but has no version associated.");
this._method = method;
this.Submenu = submenu;
this._options = options;
this._version = version;
}
public void Select(MainForm form) public void Select(MainForm form)
{ {
if (_action == ConfigMenuAction.Command) if (_action == ConfigMenuAction.Command)
@ -621,12 +566,9 @@ namespace WACCALauncher
// only works with static methods, why // only works with static methods, why
this._method(); this._method();
} }
else if (_action == ConfigMenuAction.Submenu && Submenu != null) else if (_action == ConfigMenuAction.Menu && Items != null)
{ {
Console.WriteLine("attempting submenu");
form._menuManager.NavigateToSubmenu(this); form._menuManager.NavigateToSubmenu(this);
//form.waccaListTest.Update();
//form.GenerateMenu(_submenu);
} }
else if (_action == ConfigMenuAction.ItemSelect && _options != null) else if (_action == ConfigMenuAction.ItemSelect && _options != null)
{ {
@ -637,14 +579,30 @@ namespace WACCALauncher
{ {
Console.WriteLine($"setting default version to {_version}"); Console.WriteLine($"setting default version to {_version}");
form.SetDefaultVer(_version); form.SetDefaultVer(_version);
for (int i = 0; i < form.Versions.Count; i++) // TODO: this is kinda jank, fix this
form._menuManager.UpdateCurrentMenuItems(form.GetDefaultVersionMenu());
}
else if (_action == ConfigMenuAction.Return) { form.MenuBack(); }
}
public ConfigMenu(string name, ConfigMenuAction action = ConfigMenuAction.None, Action method = null, List<ConfigMenu> items = null, List<string> options = null, Version version = null)
{ {
var name = form.Versions[i].GameVersion == VersionType.Custom ? form.Versions[i].CustomName : form.Versions[i].ToString(); this.Name = name;
//menu[i].label.Text = $"({(form.Versions[i] == form.DefaultVer ? 'X' : ' ')}) {name}".ToUpper(); this._action = action;
}
//form.RefreshMenu(); if (action == ConfigMenuAction.Menu && items == null)
} throw new ArgumentException($"Menu item '{name}' was defined with Menu type, but has no menu associated.");
else if (_action == ConfigMenuAction.Return) { form.MenuHide(); } else if (action == ConfigMenuAction.Command && method == null)
throw new ArgumentException($"Menu item '{name}' was defined with Command type, but has no method associated.");
else if (action == ConfigMenuAction.ItemSelect && options == null)
throw new ArgumentException($"Menu item '{name}' was defined with ItemSelect type, but has no options associated.");
else if (action == ConfigMenuAction.VersionSelect && version == null)
throw new ArgumentException($"Menu item '{name}' was defined with VersionSelect type, but has no version associated.");
this.Items = items;
this._method = method;
this._options = options;
this._version = version;
} }
public override string ToString() public override string ToString()
@ -653,22 +611,19 @@ namespace WACCALauncher
} }
} }
public class ConfigMenu
{
public List<ConfigMenuItem> Items;
private ConfigMenu ParentMenu;
}
public class MenuManager public class MenuManager
{ {
private ConfigMenu _rootMenu; private ConfigMenu _rootMenu;
private ConfigMenu _currentMenu; private ConfigMenu _currentMenu;
public readonly string name; private WaccaList _list;
public MenuManager(ConfigMenu root) public MenuManager(ConfigMenu root, WaccaList list)
{ {
_rootMenu = root; _rootMenu = root;
_currentMenu = _rootMenu; _currentMenu = _rootMenu;
_list = list;
_list.AssignMenuManager(this);
UpdateList();
} }
public ConfigMenu GetCurrentMenu() public ConfigMenu GetCurrentMenu()
@ -676,49 +631,31 @@ namespace WACCALauncher
return _currentMenu; return _currentMenu;
} }
public void NavigateToSubmenu(int index) public void NavigateToSubmenu(ConfigMenu menu)
{ {
if (index >= 0 && index < _currentMenu.Items.Count) menu.ParentMenu = _currentMenu;
{ _currentMenu = menu;
var selectedMenuItem = _currentMenu.Items[index]; UpdateList();
if (selectedMenuItem.Submenu != null)
{
selectedMenuItem.Submenu.ParentContainer = _currentMenu;
_currentMenu = selectedMenuItem.Submenu;
}
}
} }
public void NavigateBack() public void NavigateBack()
{ {
if (_currentContainer.ParentContainer != null) _currentMenu = _currentMenu.ParentMenu;
{ UpdateList();
_currentContainer = _currentContainer.ParentContainer;
}
} }
/* public void UpdateCurrentMenuItems(List<ConfigMenu> items)
public void NavigateToSubMenu(string optionName)
{ {
if (optionName != string.Empty && optionName != null) _currentMenu.Items = items;
{ UpdateList(preserveIndex: true);
var selectedItem = currentMenu.Find(x => x.Name == optionName);
if (selectedItem.submenu != null && selectedItem.submenu.Count > 0)
{
currentMenu = selectedItem.submenu;
}
}
} }
//public void NavigateBack() private void UpdateList(bool preserveIndex = false)
//{ {
// if (currentMenu > 0 && currentMenu[0].Parent != null) var oldIndex = _list.SelectedIndex;
// { _list.Items.Clear();
// currentMenu = currentMenu[0].Parent.SubItems; _list.Items.AddRange(_currentMenu.Items.ToArray());
// } if (_list.Items.Count > 0) _list.SelectedIndex = preserveIndex ? oldIndex : 0;
//} }
*/
} }
} }

View File

@ -10,7 +10,7 @@ using System.Windows.Forms;
namespace WACCALauncher namespace WACCALauncher
{ {
public partial class WaccaList : System.Windows.Forms.ListBox public partial class WaccaList : ListBox
{ {
private MenuManager menuManager; private MenuManager menuManager;
@ -20,35 +20,25 @@ namespace WACCALauncher
BorderStyle = BorderStyle.None; BorderStyle = BorderStyle.None;
DrawMode = DrawMode.OwnerDrawVariable; DrawMode = DrawMode.OwnerDrawVariable;
ItemHeight = 40; ItemHeight = 40;
Size = new System.Drawing.Size(680, 600); Size = new Size(680, 600);
Location = new System.Drawing.Point(200, 240); Location = new Point(200, 240);
} }
public void AssignMenuManager(MenuManager manager) public void AssignMenuManager(MenuManager manager)
{ {
menuManager = manager; menuManager = manager;
Items.Clear();
Items.AddRange(menuManager.GetCurrentMenu().ToArray());
} }
protected override void OnDrawItem(DrawItemEventArgs e) protected override void OnDrawItem(DrawItemEventArgs e)
{ {
e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds); e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds);
if (Items.Count < 1) return; if (Items.Count < 1 || e.Index < 0) return;
var text = Items[e.Index].ToString().ToUpper(); var text = Items[e.Index].ToString().ToUpper();
var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected; var selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
e.Graphics.DrawString(text, Font, selected ? Brushes.Red : Brushes.White, e.Bounds); e.Graphics.DrawString(text, Font, selected ? Brushes.Red : Brushes.White, e.Bounds);
} }
protected override void OnInvalidated(InvalidateEventArgs e)
{
Items.Clear();
Items.AddRange(menuManager.GetCurrentMenu().ToArray());
SelectedIndex = 0;
base.OnInvalidated(e);
}
} }
} }