diff --git a/OpenRA.Mods.Cnc/Widgets/CncModBrowserLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncModBrowserLogic.cs index 10703a8a8c..889c0f7968 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncModBrowserLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncModBrowserLogic.cs @@ -29,24 +29,7 @@ namespace OpenRA.Mods.Cnc.Widgets var panel = widget.GetWidget("MODS_PANEL"); var modList = panel.GetWidget("MOD_LIST"); var loadButton = panel.GetWidget("LOAD_BUTTON"); - loadButton.OnClick = () => - { - // TODO: This is crap - var mods = new List() { currentMod.Id }; - var m = currentMod; - while (!string.IsNullOrEmpty(m.Requires)) - { - m = Mod.AllMods[currentMod.Requires]; - mods.Add(m.Id); - } - - Game.RunAfterTick(() => - { - Widget.CloseWindow(); - onSwitch(); - Game.InitializeWithMods(mods.ToArray()); - }); - }; + loadButton.OnClick = () => LoadMod(currentMod.Id, onSwitch); loadButton.IsDisabled = () => currentMod.Id == Game.CurrentMods.Keys.First(); panel.GetWidget("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); }; @@ -64,17 +47,23 @@ namespace OpenRA.Mods.Cnc.Widgets item.GetWidget("AUTHOR").GetText = () => mod.Author; modList.AddChild(item); } - - - /* - // Server info - var infoPanel = panel.GetWidget("SERVER_INFO"); - infoPanel.IsVisible = () => currentServer != null; - infoPanel.GetWidget("SERVER_IP").GetText = () => currentServer.Address; - infoPanel.GetWidget("SERVER_MODS").GetText = () => ServerBrowserDelegate.GenerateModsLabel(currentServer); - infoPanel.GetWidget("MAP_TITLE").GetText = () => (CurrentMap() != null) ? CurrentMap().Title : "Unknown"; - infoPanel.GetWidget("MAP_PLAYERS").GetText = () => GetPlayersLabel(currentServer); - */ + } + + void LoadMod(string mod, Action onSwitch) + { + var mods = new List(); + while (!string.IsNullOrEmpty(mod)) + { + mods.Add(mod); + mod = Mod.AllMods[mod].Requires; + } + + Game.RunAfterTick(() => + { + Widget.CloseWindow(); + onSwitch(); + Game.InitializeWithMods(mods.ToArray()); + }); } } } diff --git a/OpenRA.Mods.RA/Widgets/Delegates/MainMenuButtonsDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/MainMenuButtonsDelegate.cs index d4eaadf13e..704bcd830e 100755 --- a/OpenRA.Mods.RA/Widgets/Delegates/MainMenuButtonsDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/MainMenuButtonsDelegate.cs @@ -38,13 +38,12 @@ namespace OpenRA.Mods.RA.Widgets.Delegates public static void DisplayModSelector() { var selector = Game.modData.WidgetLoader.LoadWidget( new WidgetArgs(), Widget.RootWidget, "QUICKMODSWITCHER" ); - var switcher = selector.GetWidget("SWITCHER"); + var switcher = selector.GetWidget("SWITCHER"); switcher.OnMouseDown = _ => ShowModsDropDown(switcher); switcher.GetText = ActiveModTitle; selector.GetWidget("VERSION").GetText = ActiveModVersion; } - static string ActiveModTitle() { var mod = Game.modData.Manifest.Mods[0]; @@ -57,37 +56,36 @@ namespace OpenRA.Mods.RA.Widgets.Delegates return Mod.AllMods[mod].Version; } - static bool ShowModsDropDown(ButtonWidget selector) + static void LoadMod(string mod) { - var dropDownOptions = new List>(); - - foreach (var kv in Mod.AllMods) + var mods = new List(); + while (!string.IsNullOrEmpty(mod)) { - var modList = new List() { kv.Key }; - var m = kv.Key; - while (!string.IsNullOrEmpty(Mod.AllMods[m].Requires)) - { - m = Mod.AllMods[m].Requires; - modList.Add(m); - } - - dropDownOptions.Add(new Pair( kv.Value.Title, - () => - { - if (Game.CurrentMods.Keys.ToArray().SymmetricDifference(modList.ToArray()).Any()) - Game.RunAfterTick(() => Game.InitializeWithMods( modList.ToArray() ) ); - } - )); + mods.Add(mod); + mod = Mod.AllMods[mod].Requires; } - - DropDownButtonWidget.ShowDropDown( selector, - dropDownOptions, - (ac, w) => new LabelWidget - { - Bounds = new Rectangle(0, 0, w, 24), - Text = " {0}".F(ac.First), - OnMouseUp = mi => { ac.Second(); return true; }, - }); + + if (!Game.CurrentMods.Keys.ToArray().SymmetricDifference(mods.ToArray()).Any()) + return; + + Game.RunAfterTick(() => + { + Game.InitializeWithMods(mods.ToArray()); + }); + } + + static bool ShowModsDropDown(DropDownButtonWidget dropdown) + { + Func setupItem = (m, itemTemplate) => + { + var item = ScrollItemWidget.Setup(itemTemplate, + () => m == Game.CurrentMods.Keys.First(), + () => LoadMod(m)); + item.GetWidget("LABEL").GetText = () => Mod.AllMods[m].Title; + return item; + }; + + dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, Mod.AllMods.Keys.ToList(), setupItem); return true; }