From a193eeb2022a0cc696884588596ae5f8b5ecca35 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sat, 8 Oct 2011 18:48:52 +1300 Subject: [PATCH] Convert RA mapchooser to use a grid of minimap images --- OpenRA.Game/OpenRA.Game.csproj | 2 +- OpenRA.Game/Widgets/GridLayout.cs | 8 +- OpenRA.Game/Widgets/MapPreviewWidget.cs | 22 ++- .../{ScrollItem.cs => ScrollItemWidget.cs} | 0 .../Widgets/Logic/MapChooserLogic.cs | 60 ++++-- mods/ra/chrome/map-chooser.yaml | 180 ++++-------------- 6 files changed, 96 insertions(+), 176 deletions(-) rename OpenRA.Game/Widgets/{ScrollItem.cs => ScrollItemWidget.cs} (100%) diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index cfecd37945..865b5e0642 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -188,7 +188,6 @@ - @@ -202,6 +201,7 @@ + diff --git a/OpenRA.Game/Widgets/GridLayout.cs b/OpenRA.Game/Widgets/GridLayout.cs index a9f63116d5..fcb8efca20 100644 --- a/OpenRA.Game/Widgets/GridLayout.cs +++ b/OpenRA.Game/Widgets/GridLayout.cs @@ -26,18 +26,18 @@ namespace OpenRA.Widgets widget.ContentHeight = widget.ItemSpacing; pos = new int2(widget.ItemSpacing, widget.ItemSpacing); } - + if (pos.X + widget.ItemSpacing + w.Bounds.Width > widget.Bounds.Width - widget.ScrollbarWidth) { /* start a new row */ pos.X = widget.ItemSpacing; pos.Y = widget.ContentHeight; } - + w.Bounds.X += pos.X; w.Bounds.Y += pos.Y; - - pos.X += widget.Bounds.Width + widget.ItemSpacing; + + pos.X += w.Bounds.Width + widget.ItemSpacing; widget.ContentHeight = Math.Max(widget.ContentHeight, pos.Y + widget.ItemSpacing + w.Bounds.Height); } diff --git a/OpenRA.Game/Widgets/MapPreviewWidget.cs b/OpenRA.Game/Widgets/MapPreviewWidget.cs index a28a1f57b5..aab4d9330f 100644 --- a/OpenRA.Game/Widgets/MapPreviewWidget.cs +++ b/OpenRA.Game/Widgets/MapPreviewWidget.cs @@ -21,10 +21,12 @@ namespace OpenRA.Widgets public Func Map = () => null; public Func> SpawnColors = () => new Dictionary(); public Action OnMouseDown = _ => {}; + public bool IgnoreMouseInput = false; - Cache PreviewCache = new Cache(stub => Minimap.RenderMapPreview( new Map( stub.Path ))); + static Cache PreviewCache = new Cache(stub => Minimap.RenderMapPreview( new Map( stub.Path ))); public MapPreviewWidget() : base() { } + protected MapPreviewWidget(MapPreviewWidget other) : base(other) { @@ -32,10 +34,14 @@ namespace OpenRA.Widgets Map = other.Map; SpawnColors = other.SpawnColors; } + public override Widget Clone() { return new MapPreviewWidget(this); } public override bool HandleMouseInput(MouseInput mi) { + if (IgnoreMouseInput) + return base.HandleMouseInput(mi); + if (mi.Event != MouseInputEvent.Down) return false; @@ -70,15 +76,15 @@ namespace OpenRA.Widgets mapChooserSheet.Texture.SetData( preview ); mapChooserSprite = new Sprite( mapChooserSheet, new Rectangle( 0, 0, map.Bounds.Width, map.Bounds.Height ), TextureChannel.Alpha ); - - // Update map rect - PreviewScale = Math.Min(RenderBounds.Width * 1.0f / map.Bounds.Width, RenderBounds.Height * 1.0f / map.Bounds.Height); - var size = Math.Max(map.Bounds.Width, map.Bounds.Height); - var dw = (int)(PreviewScale * (size - map.Bounds.Width)) / 2; - var dh = (int)(PreviewScale * (size - map.Bounds.Height)) / 2; - MapRect = new Rectangle(RenderBounds.X + dw, RenderBounds.Y + dh, (int)(map.Bounds.Width * PreviewScale), (int)(map.Bounds.Height * PreviewScale)); } + // Update map rect + PreviewScale = Math.Min(RenderBounds.Width * 1.0f / map.Bounds.Width, RenderBounds.Height * 1.0f / map.Bounds.Height); + var size = Math.Max(map.Bounds.Width, map.Bounds.Height); + var dw = (int)(PreviewScale * (size - map.Bounds.Width)) / 2; + var dh = (int)(PreviewScale * (size - map.Bounds.Height)) / 2; + MapRect = new Rectangle(RenderBounds.X + dw, RenderBounds.Y + dh, (int)(map.Bounds.Width * PreviewScale), (int)(map.Bounds.Height * PreviewScale)); + Game.Renderer.RgbaSpriteRenderer.DrawSprite( mapChooserSprite, new float2(MapRect.Location), new float2( MapRect.Size ) ); diff --git a/OpenRA.Game/Widgets/ScrollItem.cs b/OpenRA.Game/Widgets/ScrollItemWidget.cs similarity index 100% rename from OpenRA.Game/Widgets/ScrollItem.cs rename to OpenRA.Game/Widgets/ScrollItemWidget.cs diff --git a/OpenRA.Mods.RA/Widgets/Logic/MapChooserLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/MapChooserLogic.cs index 49580f15b2..1ff5ce517c 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/MapChooserLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/MapChooserLogic.cs @@ -18,7 +18,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic public class MapChooserLogic { Map map; - Widget scrollpanel; + ScrollPanelWidget scrollpanel; ScrollItemWidget itemTemplate; string gameMode; @@ -30,14 +30,20 @@ namespace OpenRA.Mods.RA.Widgets.Logic { map = Game.modData.AvailableMaps[WidgetUtils.ChooseInitialMap(initialMap)]; - widget.GetWidget("MAP_PREVIEW").Map = () => map; - widget.GetWidget("CURMAP_TITLE").GetText = () => map.Title; - widget.GetWidget("CURMAP_AUTHOR").GetText = () => map.Author; - widget.GetWidget("CURMAP_DESC").GetText = () => map.Description; - widget.GetWidget("CURMAP_DESC_LABEL").IsVisible = () => map.Description != null; - widget.GetWidget("CURMAP_SIZE").GetText = () => "{0}x{1}".F(map.Bounds.Width, map.Bounds.Height); - widget.GetWidget("CURMAP_THEATER").GetText = () => Rules.TileSets[map.Tileset].Name; - widget.GetWidget("CURMAP_PLAYERS").GetText = () => map.PlayerCount.ToString(); + var mapPreview = widget.GetWidget("MAP_PREVIEW"); + if (mapPreview != null) + mapPreview.Map = () => map; + + if (WidgetUtils.ActiveModTitle() != "Red Alert") // hack + { + widget.GetWidget("CURMAP_TITLE").GetText = () => map.Title; + widget.GetWidget("CURMAP_AUTHOR").GetText = () => map.Author; + widget.GetWidget("CURMAP_DESC").GetText = () => map.Description; + widget.GetWidget("CURMAP_DESC_LABEL").IsVisible = () => map.Description != null; + widget.GetWidget("CURMAP_SIZE").GetText = () => "{0}x{1}".F(map.Bounds.Width, map.Bounds.Height); + widget.GetWidget("CURMAP_THEATER").GetText = () => Rules.TileSets[map.Tileset].Name; + widget.GetWidget("CURMAP_PLAYERS").GetText = () => map.PlayerCount.ToString(); + } widget.GetWidget("BUTTON_OK").OnClick = () => { Widget.CloseWindow(); onSelect(map); }; widget.GetWidget("BUTTON_CANCEL").OnClick = () => { Widget.CloseWindow(); onExit(); }; @@ -80,7 +86,12 @@ namespace OpenRA.Mods.RA.Widgets.Logic void EnumerateMaps() { scrollpanel.RemoveChildren(); - (scrollpanel as ScrollPanelWidget).ScrollToTop(); + + // hack for RA's new 2d mapchooser + if (WidgetUtils.ActiveModTitle() == "Red Alert") + scrollpanel.Layout = new GridLayout(scrollpanel); + + scrollpanel.ScrollToTop(); var maps = Game.modData.AvailableMaps .Where(kv => kv.Value.Selectable) @@ -94,17 +105,32 @@ namespace OpenRA.Mods.RA.Widgets.Logic var item = ScrollItemWidget.Setup(itemTemplate, () => m == map, () => map = m); var titleLabel = item.GetWidget("TITLE"); + titleLabel.GetText = () => m.Title; + var playersLabel = item.GetWidget("PLAYERS"); - if (playersLabel != null) - { playersLabel.GetText = () => "{0}".F(m.PlayerCount); - titleLabel.GetText = () => m.Title; - } - else - titleLabel.GetText = () => "{0} ({1})".F(m.Title, m.PlayerCount); - item.GetWidget("TYPE").GetText = () => m.Type; + var previewWidget = item.GetWidget("PREVIEW"); + if (previewWidget != null) + { + previewWidget.IgnoreMouseOver = true; + previewWidget.IgnoreMouseInput = true; + previewWidget.Map = () => m; + } + + var typeWidget = item.GetWidget("TYPE"); + if (typeWidget != null) + typeWidget.GetText = () => m.Type; + + var detailsWidget = item.GetWidget("DETAILS"); + if (detailsWidget != null) + detailsWidget.GetText = () => "{0} ({1})".F(m.Type, m.PlayerCount); + + var authorWidget = item.GetWidget("AUTHOR"); + if (authorWidget != null) + authorWidget.GetText = () => m.Author; + scrollpanel.AddChild(item); } } diff --git a/mods/ra/chrome/map-chooser.yaml b/mods/ra/chrome/map-chooser.yaml index c6badcd798..715d987b96 100644 --- a/mods/ra/chrome/map-chooser.yaml +++ b/mods/ra/chrome/map-chooser.yaml @@ -17,165 +17,53 @@ Background@MAP_CHOOSER: ScrollPanel@MAP_LIST: Id:MAP_LIST X:20 - Y:67 - Width:504 + Y:47 + Width:PARENT_RIGHT - 40 Height:474 Children: ScrollItem@MAP_TEMPLATE: Id:MAP_TEMPLATE - Width:PARENT_RIGHT-27 - Height:25 + Width:180 + Height:208 X:2 Y:0 Visible:false Children: Label@TITLE: - X:10 + X:2 + Y:PARENT_BOTTOM-47 Id:TITLE - Width:PARENT_RIGHT-100 + Width:PARENT_RIGHT-4 Height:25 - Label@TYPE: - Id:TYPE - Width:90 - X:PARENT_RIGHT-100 - Align:Right + Align:Center + Label@DETAILS: + Id:DETAILS + Width:PARENT_RIGHT-4 + X:2 + Y:PARENT_BOTTOM-35 + Align:Center Height:25 - Container@MAP_LABELS: - Width:494 + Font:Tiny + Label@AUTHOR: + Id:AUTHOR + Width:PARENT_RIGHT-4 + X:2 + Y:PARENT_BOTTOM-26 + Align:Center + Height:25 + Font:Tiny + MapPreview@PREVIEW: + Id:PREVIEW + X:(PARENT_RIGHT - WIDTH)/2 + Y:4 + Width:160 + Height:160 + DropDownButton@GAMEMODE_FILTER: + Id:GAMEMODE_FILTER + X:PARENT_RIGHT - 220 + Y:17 + Width:200 Height:25 - X:25 - Y:40 - Children: - Label@TITLE: - Width:125 - Height:25 - X:0 - Y:0 - Text:Title - Align:Center - Font:Bold - DropDownButton@GAMEMODE_FILTER: - Id:GAMEMODE_FILTER - X:300 - Width:200 - Height:25 - Background@MAPCHOOSER_MAP_BG: - X:PARENT_RIGHT-268 - Y:50 - Width:252 - Height:252 - Background:dialog3 - Children: - MapPreview@MAP_PREVIEW: - Id:MAP_PREVIEW - X:4 - Y:4 - Width:244 - Height:244 - Container@MAP_LABEL_CONTAINER: - Width:70 - Height:200 - X:PARENT_RIGHT - 255 - Y:311 - Children: - Label@CURMAP_TITLE_LABEL: - Id:CURMAP_TITLE_LABEL - X:0 - Y:0 - Align:Right - Width:70 - Height:20 - Text:Title: - Font:Bold - Label@CURMAP_TITLE: - Id:CURMAP_TITLE - X:75 - Y:0 - Align:Left - Width:70 - Height:20 - Label@CURMAP_AUTHOR_LABEL: - Id:CURMAP_AUTHOR_LABEL - X:0 - Y:20 - Align:Right - Width:70 - Height:20 - Text:Author: - Font:Bold - Label@CURMAP_AUTHOR: - Id:CURMAP_AUTHOR - X:75 - Y:20 - Align:Left - Width:175 - Height:20 - WordWrap:True - Label@CURMAP_SIZE_LABEL: - Id:CURMAP_SIZE_LABEL - X:0 - Y:40 - Align:Right - Width:70 - Height:20 - Text:Size: - Font:Bold - Label@CURMAP_SIZE: - Id:CURMAP_SIZE - X:75 - Y:40 - Align:Left - Width:70 - Height:20 - Label@CURMAP_THEATER_LABEL: - Id:CURMAP_THEATER_LABEL - X:0 - Y:60 - Align:Right - Width:70 - Height:20 - Text:Theater: - Font:Bold - Label@CURMAP_THEATER: - Id:CURMAP_THEATER - X:75 - Y:60 - Align:Left - Width:70 - Height:20 - Label@CURMAP_PLAYERS_LABEL: - Id:CURMAP_PLAYERS_LABEL - X:0 - Y:80 - Align:Right - Width:70 - Height:20 - Text:Players: - Font:Bold - Label@CURMAP_PLAYERS: - Id:CURMAP_PLAYERS - X:75 - Y:80 - Align:Left - Width:70 - Height:20 - Label@CURMAP_DESC_LABEL: - Id:CURMAP_DESC_LABEL - X:0 - Y:100 - Align:Right - Width:70 - Height:20 - Text:Desc: - Font:Bold - Label@CURMAP_DESC: - Id:CURMAP_DESC - X:75 - Y:100 - Align:Left - Width:170 - Height:20 - WordWrap:True Button@BUTTON_OK: Id:BUTTON_OK X:PARENT_RIGHT - 295