diff --git a/OpenRA.Game/Chrome/DefaultWidgetDelegates.cs b/OpenRA.Game/Chrome/DefaultWidgetDelegates.cs index b6bdbbb684..7921019df5 100644 --- a/OpenRA.Game/Chrome/DefaultWidgetDelegates.cs +++ b/OpenRA.Game/Chrome/DefaultWidgetDelegates.cs @@ -1,14 +1,16 @@ using System.Collections.Generic; +using System.Linq; using System.Drawing; using OpenRA.FileFormats; using OpenRA.Graphics; +using OpenRA.Server; namespace OpenRA.Widgets.Delegates { public interface IWidgetDelegate { bool OnClick(Widget w, MouseInput mi); } public class MainMenuButtonsDelegate : IWidgetDelegate - { + { public bool OnClick(Widget w, MouseInput mi) { // Main Menu root @@ -18,12 +20,6 @@ namespace OpenRA.Widgets.Delegates return true; } - if (w.Id == "MAINMENU_BUTTON_JOIN") - { - Game.JoinServer(Game.Settings.NetworkHost, Game.Settings.NetworkPort); - return true; - } - if (w.Id == "MAINMENU_BUTTON_CREATE") { WidgetLoader.rootWidget.GetWidget("MAINMENU_BG").Visible = false; @@ -45,6 +41,60 @@ namespace OpenRA.Widgets.Delegates return true; } + if (w.Id == "JOINSERVER_BUTTON_CANCEL") + { + WidgetLoader.rootWidget.GetWidget("JOINSERVER_BG").Visible = false; + WidgetLoader.rootWidget.GetWidget("MAINMENU_BG").Visible = true; + return true; + } + return false; + } + } + + public class ServerBrowserDelegate : IWidgetDelegate + { + IEnumerable GameList; + + public bool OnClick(Widget w, MouseInput mi) + { + // Main Menu root + if (w.Id == "MAINMENU_BUTTON_JOIN") + { + WidgetLoader.rootWidget.GetWidget("MAINMENU_BG").Visible = false; + Widget bg = WidgetLoader.rootWidget.GetWidget("JOINSERVER_BG"); + bg.Visible = true; + + int height = 50; + int width = 300; + int i = 0; + GameList = MasterServerQuery.GetGameList(Game.Settings.MasterServer); + + foreach (var game in GameList) + { + ButtonWidget b = new ButtonWidget(); + b.Bounds = new Rectangle(bg.Bounds.X + 10, bg.Bounds.Y + height, width, 25); + b.GetType().GetField("Id").SetValue( b, "JOIN_GAME_{0}".F(i)); + b.GetType().GetField("Text").SetValue( b, "{0} ({1})".F(game.Name, game.Address)); + b.GetType().GetField("Delegate").SetValue( b, "ServerBrowserDelegate"); + + b.EventBounds = b.Bounds; + bg.AddChild(b); + + height += 35; + } + + return true; + } + + if (w.Id.Substring(0,10) == "JOIN_GAME_") + { + int index = int.Parse(w.Id.Substring(10)); + GameList = MasterServerQuery.GetGameList(Game.Settings.MasterServer); + var game = GameList.ElementAt(index); + Game.JoinServer(game.Address.Split(':')[0], int.Parse(game.Address.Split(':')[1])); + return true; + } + return false; } } diff --git a/mods/cnc/menus.yaml b/mods/cnc/menus.yaml index 2a1774cd32..60bbd08767 100644 --- a/mods/cnc/menus.yaml +++ b/mods/cnc/menus.yaml @@ -22,7 +22,7 @@ Container: Width:160 Height:25 Text:Join Game - Delegate:MainMenuButtonsDelegate + Delegate:ServerBrowserDelegate Button@MAINMENU_BUTTON_CREATE: Id:MAINMENU_BUTTON_CREATE X:45 @@ -83,4 +83,28 @@ Container: Width:160 Height:25 Text:Create + Delegate:MainMenuButtonsDelegate + Background@JOINSERVER_BG: + Id:JOINSERVER_BG + X:WINDOW_RIGHT/2 - 225 + Y:200 + Width:450 + Height:400 + Visible:false + Children: + Label@JOINSERVER_LABEL_TITLE: + Id:JOINSERVER_LABEL_TITLE + X:0 + Y:20 + Width:250 + Height:25 + Text:Quick'n'dirty Server Browser + Align:Center + Button@JOINSERVER_BUTTON_CANCEL: + Id:JOINSERVER_BUTTON_CANCEL + X:PARENT_RIGHT - 170 + Y:PARENT_BOTTOM - 35 + Width:160 + Height:25 + Text:Cancel Delegate:MainMenuButtonsDelegate \ No newline at end of file