From 872344d8e73d21c02dd987e9925c7e13bf13dc7e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 11 Apr 2010 03:09:52 +1200 Subject: [PATCH] Temp lobby map preview (will be fixed properly when we widgetize) --- OpenRA.FileFormats/Map/MapStub.cs | 26 ++++++++++-- OpenRA.Game/Chrome.cs | 70 +++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 16 deletions(-) diff --git a/OpenRA.FileFormats/Map/MapStub.cs b/OpenRA.FileFormats/Map/MapStub.cs index 17773c45c2..614d4fa135 100644 --- a/OpenRA.FileFormats/Map/MapStub.cs +++ b/OpenRA.FileFormats/Map/MapStub.cs @@ -19,7 +19,8 @@ #endregion using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Linq; using System.Drawing; namespace OpenRA.FileFormats @@ -37,6 +38,7 @@ namespace OpenRA.FileFormats public string Author; public int PlayerCount; public string Tileset; + public Dictionary Waypoints = new Dictionary(); public int2 TopLeft; public int2 BottomRight; @@ -55,14 +57,32 @@ namespace OpenRA.FileFormats Package = package; var yaml = MiniYaml.FromStream(Package.GetContent("map.yaml")); FieldLoader.LoadFields(this, yaml, Fields); - + + // Waypoints + foreach (var wp in yaml["Waypoints"].Nodes) + { + string[] loc = wp.Value.Value.Split(','); + Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]), int.Parse(loc[1]))); + } + Preview = Lazy.New( () => { return new Bitmap(Package.GetContent("preview.png")); } ); Uid = Package.GetContent("map.uid").ReadAllText(); } - + + public int2 ConvertToPreview(int2 point, Rectangle container) + { + float scale = Math.Min(container.Width * 1.0f / Width, container.Height * 1.0f / Height); + + var size = Math.Max(Width, Height); + var dw = (int)(scale * (size - Width)) / 2; + var dh = (int)(scale * (size - Height)) / 2; + + return new int2(container.X + dw + (int)(scale*(point.X - TopLeft.X)) , container.Y + dh + (int)(scale*(point.Y - TopLeft.Y))); + } + public Rectangle PreviewBounds(Rectangle container) { float scale = Math.Min(container.Width * 1.0f / Width, container.Height * 1.0f / Height); diff --git a/OpenRA.Game/Chrome.cs b/OpenRA.Game/Chrome.cs index c3958ec53a..935aeae38e 100644 --- a/OpenRA.Game/Chrome.cs +++ b/OpenRA.Game/Chrome.cs @@ -288,15 +288,16 @@ namespace OpenRA mapChooserSprite = new Sprite(mapChooserSheet, new Rectangle(0,0,currentMap.Width, currentMap.Height), TextureChannel.Alpha); mapPreviewDirty = false; } - + var mapBackground = new Rectangle(r.Right - 284, r.Top + 26, 264, 264); var mapContainer = new Rectangle(r.Right - 280, r.Top + 30, 256, 256); - var mapRect = currentMap.PreviewBounds(new Rectangle(mapContainer.X+4,mapContainer.Y+4,mapContainer.Width-8,mapContainer.Height-8)); + var mapRect = currentMap.PreviewBounds(new Rectangle(mapContainer.X,mapContainer.Y,mapContainer.Width,mapContainer.Height)); - DrawDialogBackground(mapContainer, "dialog2"); + DrawDialogBackground(mapBackground, "dialog3"); rgbaRenderer.DrawSprite(mapChooserSprite, new float2(mapRect.Location), "chrome", new float2(mapRect.Size)); + DrawSpawnPoints(currentMap,mapContainer); rgbaRenderer.Flush(); var y = r.Top + 50; @@ -391,7 +392,34 @@ namespace OpenRA } public void DrawWidgets(World world) { rootWidget.Draw(world); shpRenderer.Flush(); rgbaRenderer.Flush(); } + public void DrawSpawnPoints(MapStub map, Rectangle container) + { + var points = map.Waypoints; + // .Select( (sp,i) => Pair.New(sp, Game.LobbyInfo.Clients.FirstOrDefault( + // c => c.SpawnPoint == i + 1 ) )) + // .ToList(); + + foreach (var p in points) + { + var pos = map.ConvertToPreview(p.Value,container); + //if (p.Second == null) + rgbaRenderer.DrawSprite(ChromeProvider.GetImage(renderer, "spawnpoints", "unowned"), pos, "chrome"); + //else + //{ + // lineRenderer.FillRect(new RectangleF( + // Game.viewport.Location.X + pos.X + 2, + // Game.viewport.Location.Y + pos.Y + 2, + // 12, 12), Player.PlayerColors[ p.Second.PaletteIndex % Player.PlayerColors.Count() ].c); + // + // rgbaRenderer.DrawSprite(ownedSpawnPoint, pos, "chrome"); + //} + } + + lineRenderer.Flush(); + rgbaRenderer.Flush(); + } + public void DrawLobby() { buttons.Clear(); @@ -409,16 +437,32 @@ namespace OpenRA DrawDialogBackground(r, "dialog"); DrawCentered("OpenRA Multiplayer Lobby", new int2(r.Left + w / 2, r.Top + 20), Color.White); rgbaRenderer.Flush(); - - DrawDialogBackground(new Rectangle(r.Right - 264, r.Top + 43, 244, 244),"dialog3"); - var minimapRect = new Rectangle(r.Right - 262, r.Top + 45, 240, 240); - - /* - world.Minimap.Update(); - world.Minimap.Draw(minimapRect, true); - world.Minimap.DrawSpawnPoints(minimapRect); - */ + if (Game.LobbyInfo.GlobalSettings.Map != null) + { + var mapBackground = new Rectangle(r.Right - 268, r.Top + 39, 252, 252); + var mapContainer = new Rectangle(r.Right - 264, r.Top + 43, 244, 244); + var mapRect = Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].PreviewBounds(new Rectangle(mapContainer.X,mapContainer.Y,mapContainer.Width,mapContainer.Height)); + DrawDialogBackground(mapBackground,"dialog3"); + + if (mapPreviewDirty) + { + if (mapChooserSheet == null || mapChooserSheet.Size.Width != Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].Width || mapChooserSheet.Size.Height != Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].Height) + mapChooserSheet = new Sheet(renderer, new Size(Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].Width, Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].Height)); + + mapChooserSheet.Texture.SetData(Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].Preview.Value); + mapChooserSprite = new Sprite(mapChooserSheet, new Rectangle(0,0,Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].Width, Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map].Height), TextureChannel.Alpha); + mapPreviewDirty = false; + } + + rgbaRenderer.DrawSprite(mapChooserSprite, + new float2(mapRect.Location), + "chrome", + new float2(mapRect.Size)); + + DrawSpawnPoints(Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map],mapContainer); + rgbaRenderer.Flush(); + } if (Game.IsHost) { @@ -426,7 +470,7 @@ namespace OpenRA _ => { showMapChooser = true; - currentMap = Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map.ToLowerInvariant()]; + currentMap = Game.AvailableMaps[Game.LobbyInfo.GlobalSettings.Map]; mapPreviewDirty = true; }); }