diff --git a/OpenRA.Editor/Form1.cs b/OpenRA.Editor/Form1.cs index bc6340e502..9f4a62c15b 100755 --- a/OpenRA.Editor/Form1.cs +++ b/OpenRA.Editor/Form1.cs @@ -221,8 +221,10 @@ namespace OpenRA.Editor if (DialogResult.OK != rd.ShowDialog()) return; - surface1.Map.TopLeft = new int2((int)rd.cordonLeft.Value, (int)rd.cordonTop.Value); - surface1.Map.BottomRight = new int2((int)rd.cordonRight.Value, (int)rd.cordonBottom.Value); + surface1.Map.ResizeCordon((int)rd.cordonLeft.Value, + (int)rd.cordonTop.Value, + (int)rd.cordonRight.Value, + (int)rd.cordonBottom.Value); if ((int)rd.width.Value != surface1.Map.MapSize.X || (int)rd.height.Value != surface1.Map.MapSize.Y) { diff --git a/OpenRA.Editor/Surface.cs b/OpenRA.Editor/Surface.cs index 4ab8c74aff..ae802a3ef5 100755 --- a/OpenRA.Editor/Surface.cs +++ b/OpenRA.Editor/Surface.cs @@ -507,8 +507,8 @@ namespace OpenRA.Editor e.Graphics.DrawRectangle(CordonPen, Map.Bounds.Left * TileSet.TileSize * Zoom + Offset.X, Map.Bounds.Top * TileSet.TileSize * Zoom + Offset.Y, - Map.Width * TileSet.TileSize * Zoom, - Map.Height * TileSet.TileSize * Zoom); + Map.Bounds.Width * TileSet.TileSize * Zoom, + Map.Bounds.Height * TileSet.TileSize * Zoom); foreach (var ar in Map.Actors) DrawActor(e.Graphics, ar.Value.Location(), ActorTemplates[ar.Value.Type], diff --git a/OpenRA.FileFormats/Map/MapStub.cs b/OpenRA.FileFormats/Map/MapStub.cs index 5992adea9a..0458e72f94 100644 --- a/OpenRA.FileFormats/Map/MapStub.cs +++ b/OpenRA.FileFormats/Map/MapStub.cs @@ -35,17 +35,16 @@ namespace OpenRA.FileFormats [FieldLoader.Load] public int2 TopLeft; [FieldLoader.Load] public int2 BottomRight; - public int Width { get { return BottomRight.X - TopLeft.X; } } - public int Height { get { return BottomRight.Y - TopLeft.Y; } } - public Rectangle Bounds { get { return Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); } } - + public Rectangle Bounds; + public MapStub() {} // Hack for the editor - not used for anything important public MapStub(IFolder container) { Container = container; var yaml = MiniYaml.FromStream(Container.GetContent("map.yaml")); FieldLoader.Load( this, new MiniYaml( null, yaml ) ); - + + Bounds = Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); Uid = Container.GetContent("map.uid").ReadAllText(); } diff --git a/OpenRA.FileFormats/Primitives/int2.cs b/OpenRA.FileFormats/Primitives/int2.cs index d669521fb1..715c5a3cbf 100644 --- a/OpenRA.FileFormats/Primitives/int2.cs +++ b/OpenRA.FileFormats/Primitives/int2.cs @@ -72,5 +72,11 @@ namespace OpenRA { return a + ( b - a ) * mul / div; } + + public int2 Clamp(Rectangle r) + { + return new int2(Math.Min(r.Right, Math.Max(X, r.Left)), + Math.Min(r.Bottom, Math.Max(Y, r.Top))); + } } } diff --git a/OpenRA.Game/Graphics/Minimap.cs b/OpenRA.Game/Graphics/Minimap.cs index 18701eef63..0b80e8f4e2 100644 --- a/OpenRA.Game/Graphics/Minimap.cs +++ b/OpenRA.Game/Graphics/Minimap.cs @@ -28,12 +28,12 @@ namespace OpenRA.Graphics public static Bitmap TerrainBitmap(Map map, bool actualSize) { var tileset = Rules.TileSets[map.Tileset]; - var width = map.Width; - var height = map.Height; + var width = map.Bounds.Width; + var height = map.Bounds.Height; if (!actualSize) { - width = height = Util.NextPowerOf2(Math.Max(map.Width, map.Height)); + width = height = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); } Bitmap terrain = new Bitmap(width, height); @@ -45,8 +45,8 @@ namespace OpenRA.Graphics { int* c = (int*)bitmapData.Scan0; - for (var x = 0; x < map.Width; x++) - for (var y = 0; y < map.Height; y++) + for (var x = 0; x < map.Bounds.Width; x++) + for (var y = 0; y < map.Bounds.Height; y++) { var mapX = x + map.Bounds.Left; var mapY = y + map.Bounds.Top; @@ -75,8 +75,8 @@ namespace OpenRA.Graphics { int* c = (int*)bitmapData.Scan0; - for (var x = 0; x < map.Width; x++) - for (var y = 0; y < map.Height; y++) + for (var x = 0; x < map.Bounds.Width; x++) + for (var y = 0; y < map.Bounds.Height; y++) { var mapX = x + map.Bounds.Left; var mapY = y + map.Bounds.Top; @@ -100,7 +100,7 @@ namespace OpenRA.Graphics public static Bitmap CustomTerrainBitmap(World world) { var map = world.Map; - var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height)); + var size = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); Bitmap bitmap = new Bitmap(size, size); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); @@ -109,8 +109,8 @@ namespace OpenRA.Graphics { int* c = (int*)bitmapData.Scan0; - for (var x = 0; x < map.Width; x++) - for (var y = 0; y < map.Height; y++) + for (var x = 0; x < map.Bounds.Width; x++) + for (var y = 0; y < map.Bounds.Height; y++) { var mapX = x + map.Bounds.Left; var mapY = y + map.Bounds.Top; @@ -127,7 +127,7 @@ namespace OpenRA.Graphics public static Bitmap ActorsBitmap(World world) { var map = world.Map; - var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height)); + var size = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); Bitmap bitmap = new Bitmap(size, size); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); @@ -157,7 +157,7 @@ namespace OpenRA.Graphics public static Bitmap ShroudBitmap(World world) { var map = world.Map; - var size = Util.NextPowerOf2(Math.Max(map.Width, map.Height)); + var size = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height)); Bitmap bitmap = new Bitmap(size, size); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); @@ -171,8 +171,8 @@ namespace OpenRA.Graphics { int* c = (int*)bitmapData.Scan0; - for (var x = 0; x < map.Width; x++) - for (var y = 0; y < map.Height; y++) + for (var x = 0; x < map.Bounds.Width; x++) + for (var y = 0; y < map.Bounds.Height; y++) { var mapX = x + map.Bounds.Left; var mapY = y + map.Bounds.Top; diff --git a/OpenRA.Game/Graphics/TerrainRenderer.cs b/OpenRA.Game/Graphics/TerrainRenderer.cs index c1049a857b..3ae71cda6a 100644 --- a/OpenRA.Game/Graphics/TerrainRenderer.cs +++ b/OpenRA.Game/Graphics/TerrainRenderer.cs @@ -34,8 +34,8 @@ namespace OpenRA.Graphics var tileMapping = new Cache, Sprite>( x => Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize)); - Vertex[] vertices = new Vertex[4 * map.Height * map.Width]; - ushort[] indices = new ushort[6 * map.Height * map.Width]; + Vertex[] vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width]; + ushort[] indices = new ushort[6 * map.Bounds.Height * map.Bounds.Width]; terrainSheet = tileMapping[map.MapTiles[map.Bounds.Left, map.Bounds.Top]].sheet; @@ -65,19 +65,19 @@ namespace OpenRA.Graphics public void Draw( WorldRenderer wr, Viewport viewport ) { - int indicesPerRow = map.Width * 6; - int verticesPerRow = map.Width * 4; + int indicesPerRow = map.Bounds.Width * 6; + int verticesPerRow = map.Bounds.Width * 4; int visibleRows = (int)(viewport.Height * 1f / Game.CellSize + 2); int firstRow = (int)(viewport.Location.Y * 1f / Game.CellSize - map.Bounds.Top); int lastRow = firstRow + visibleRows; - if (lastRow < 0 || firstRow > map.Height) + if (lastRow < 0 || firstRow > map.Bounds.Height) return; if (firstRow < 0) firstRow = 0; - if (lastRow > map.Height) lastRow = map.Height; + if (lastRow > map.Bounds.Height) lastRow = map.Bounds.Height; if (world.LocalPlayer != null && !world.LocalPlayer.Shroud.Disabled && world.LocalPlayer.Shroud.Bounds.HasValue) { diff --git a/OpenRA.Game/Graphics/Viewport.cs b/OpenRA.Game/Graphics/Viewport.cs index d984efedfd..d8b5668a25 100755 --- a/OpenRA.Game/Graphics/Viewport.cs +++ b/OpenRA.Game/Graphics/Viewport.cs @@ -52,8 +52,7 @@ namespace OpenRA.Graphics private int2 NormalizeScrollPosition(int2 newScrollPosition) { - return new int2(Math.Min(adjustedMapBounds.Right, Math.Max(newScrollPosition.X, adjustedMapBounds.Left)), - Math.Min(adjustedMapBounds.Bottom, Math.Max(newScrollPosition.Y, adjustedMapBounds.Top))); + return newScrollPosition.Clamp(adjustedMapBounds); } public ScrollDirection GetBlockedDirections() diff --git a/OpenRA.Game/Map.cs b/OpenRA.Game/Map.cs index d47e71fd00..ff47fe1963 100644 --- a/OpenRA.Game/Map.cs +++ b/OpenRA.Game/Map.cs @@ -67,8 +67,7 @@ namespace OpenRA index = (byte)(tile.Value.PickAny ? 0xffu : 0) } } }; PlayerCount = 0; - TopLeft = new int2(0, 0); - BottomRight = new int2(0, 0); + ResizeCordon(0,0,0,0); Title = "Name your map here"; Description = "Describe your map here"; @@ -349,5 +348,12 @@ namespace OpenRA MapResources = ResizeArray(MapResources, MapResources[0, 0], width, height); MapSize = new int2(width, height); } + + public void ResizeCordon(int left, int top, int right, int bottom) + { + TopLeft = new int2(left, top); + BottomRight = new int2(right, bottom); + Bounds = Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); + } } } diff --git a/OpenRA.Game/Widgets/Delegates/MapChooserDelegate.cs b/OpenRA.Game/Widgets/Delegates/MapChooserDelegate.cs index 67d2db62db..8fef6546b0 100644 --- a/OpenRA.Game/Widgets/Delegates/MapChooserDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/MapChooserDelegate.cs @@ -33,7 +33,7 @@ namespace OpenRA.Widgets.Delegates var ml = bg.GetWidget("MAP_LIST"); bg.GetWidget("MAPCHOOSER_MAP_PREVIEW").Map = () => Map; bg.GetWidget("CURMAP_TITLE").GetText = () => Map.Title; - bg.GetWidget("CURMAP_SIZE").GetText = () => "{0}x{1}".F(Map.Width, Map.Height); + bg.GetWidget("CURMAP_SIZE").GetText = () => "{0}x{1}".F(Map.Bounds.Width, Map.Bounds.Height); bg.GetWidget("CURMAP_THEATER").GetText = () => Rules.TileSets[Map.Tileset].Name; bg.GetWidget("CURMAP_PLAYERS").GetText = () => Map.PlayerCount.ToString(); diff --git a/OpenRA.Game/Widgets/MapPreviewWidget.cs b/OpenRA.Game/Widgets/MapPreviewWidget.cs index 64999af777..b3b24342ac 100644 --- a/OpenRA.Game/Widgets/MapPreviewWidget.cs +++ b/OpenRA.Game/Widgets/MapPreviewWidget.cs @@ -91,14 +91,14 @@ namespace OpenRA.Widgets mapChooserSheet = new Sheet(new Size( preview.Width, preview.Height ) ); mapChooserSheet.Texture.SetData( preview ); - mapChooserSprite = new Sprite( mapChooserSheet, new Rectangle( 0, 0, map.Width, map.Height ), TextureChannel.Alpha ); + 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.Width, RenderBounds.Height * 1.0f / map.Height); - var size = Math.Max(map.Width, map.Height); - var dw = (int)(PreviewScale * (size - map.Width)) / 2; - var dh = (int)(PreviewScale * (size - map.Height)) / 2; - MapRect = new Rectangle(RenderBounds.X + dw, RenderBounds.Y + dh, (int)(map.Width * PreviewScale), (int)(map.Height * PreviewScale)); + 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, diff --git a/OpenRA.Game/WorldUtils.cs b/OpenRA.Game/WorldUtils.cs index 4b769a9b6e..b3747912c7 100755 --- a/OpenRA.Game/WorldUtils.cs +++ b/OpenRA.Game/WorldUtils.cs @@ -90,9 +90,7 @@ namespace OpenRA public static int2 ClampToWorld( this World world, int2 xy ) { - var b = world.Map.Bounds; - return new int2(Math.Min(b.Right, Math.Max(xy.X, b.Left)), - Math.Min(b.Bottom, Math.Max(xy.Y, b.Top))); + return xy.Clamp(world.Map.Bounds); } public static int2 ChooseRandomEdgeCell(this World w) diff --git a/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs b/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs index d837ef8726..26cc32ad86 100644 --- a/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs +++ b/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs @@ -35,7 +35,8 @@ namespace OpenRA.Mods.Cnc Map = w.Map; Players = w.players.Values.ToDictionary(p => p.InternalName); Actors = w.WorldActor.Trait().Actors; - Game.MoveViewport((.5f * (w.Map.TopLeft + w.Map.BottomRight).ToFloat2()).ToInt2()); + var b = w.Map.Bounds; + Game.MoveViewport(new int2(b.Left + b.Width/2, b.Top + b.Height/2)); Scripting.Media.PlayFMVFullscreen(w, "gdi1.vqa", () => Scripting.Media.PlayFMVFullscreen(w, "landing.vqa", () => diff --git a/OpenRA.Mods.RA/Effects/NukeLaunch.cs b/OpenRA.Mods.RA/Effects/NukeLaunch.cs index 2f40b5969a..a65164a256 100755 --- a/OpenRA.Mods.RA/Effects/NukeLaunch.cs +++ b/OpenRA.Mods.RA/Effects/NukeLaunch.cs @@ -35,7 +35,7 @@ namespace OpenRA.Mods.RA.Effects if (silo == null) { - altitude = silo.World.Map.Height*Game.CellSize; + altitude = silo.World.Map.Bounds.Height*Game.CellSize; StartDescent(silo.World); } else @@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA.Effects if (goingUp) { altitude += 10; - if (altitude >= world.Map.Height*Game.CellSize) + if (altitude >= world.Map.Bounds.Height*Game.CellSize) StartDescent(world); } else diff --git a/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs b/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs index f2f4566c03..c4894ae43a 100755 --- a/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs +++ b/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs @@ -45,14 +45,14 @@ namespace OpenRA.Mods.RA.Widgets public RadarBinWidget( [ObjectCreator.Param] World world ) { this.world = world; - var size = Math.Max(world.Map.Width, world.Map.Height); - previewScale = Math.Min(192f / world.Map.Width, 192f / world.Map.Height); - previewOrigin = new int2(9 + (int)(radarOpenOrigin.X + previewScale * (size - world.Map.Width)/2), (int)(radarOpenOrigin.Y + previewScale * (size - world.Map.Height)/2)); - mapRect = new RectangleF(previewOrigin.X, previewOrigin.Y, (int)(world.Map.Width * previewScale), (int)(world.Map.Height * previewScale)); + var size = Math.Max(world.Map.Bounds.Width, world.Map.Bounds.Height); + previewScale = Math.Min(192f / world.Map.Bounds.Width, 192f / world.Map.Bounds.Height); + previewOrigin = new int2(9 + (int)(radarOpenOrigin.X + previewScale * (size - world.Map.Bounds.Width)/2), (int)(radarOpenOrigin.Y + previewScale * (size - world.Map.Bounds.Height)/2)); + mapRect = new RectangleF(previewOrigin.X, previewOrigin.Y, (int)(world.Map.Bounds.Width * previewScale), (int)(world.Map.Bounds.Height * previewScale)); // Only needs to be done once var terrainBitmap = Minimap.TerrainBitmap(world.Map); - var r = new Rectangle( 0, 0, world.Map.Width, world.Map.Height ); + var r = new Rectangle( 0, 0, world.Map.Bounds.Width, world.Map.Bounds.Height ); var s = new Size( terrainBitmap.Width, terrainBitmap.Height ); terrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha); terrainSprite.sheet.Texture.SetData(terrainBitmap); @@ -138,7 +138,7 @@ namespace OpenRA.Mods.RA.Widgets // Don't draw the radar if the tray is moving if (radarAnimationFrame >= radarSlideAnimationLength) { - var o = new float2(mapRect.Location.X, mapRect.Location.Y + world.Map.Height * previewScale * (1 - radarMinimapHeight)/2); + var o = new float2(mapRect.Location.X, mapRect.Location.Y + world.Map.Bounds.Height * previewScale * (1 - radarMinimapHeight)/2); var s = new float2(mapRect.Size.Width, mapRect.Size.Height*radarMinimapHeight); Game.Renderer.RgbaSpriteRenderer.DrawSprite(terrainSprite, o, s); Game.Renderer.RgbaSpriteRenderer.DrawSprite(customTerrainSprite, o, s);