Added int2.Clamp(Rectangle). All queries for map geometry use map.Bounds.

This commit is contained in:
Paul Chote
2010-11-24 12:24:48 +13:00
parent 1dfe437641
commit fc5830a687
14 changed files with 63 additions and 52 deletions

View File

@@ -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)
{

View File

@@ -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],

View File

@@ -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();
}

View File

@@ -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)));
}
}
}

View File

@@ -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;

View File

@@ -34,8 +34,8 @@ namespace OpenRA.Graphics
var tileMapping = new Cache<TileReference<ushort,byte>, 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)
{

View File

@@ -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()

View File

@@ -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);
}
}
}

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Widgets.Delegates
var ml = bg.GetWidget<ListBoxWidget>("MAP_LIST");
bg.GetWidget<MapPreviewWidget>("MAPCHOOSER_MAP_PREVIEW").Map = () => Map;
bg.GetWidget<LabelWidget>("CURMAP_TITLE").GetText = () => Map.Title;
bg.GetWidget<LabelWidget>("CURMAP_SIZE").GetText = () => "{0}x{1}".F(Map.Width, Map.Height);
bg.GetWidget<LabelWidget>("CURMAP_SIZE").GetText = () => "{0}x{1}".F(Map.Bounds.Width, Map.Bounds.Height);
bg.GetWidget<LabelWidget>("CURMAP_THEATER").GetText = () => Rules.TileSets[Map.Tileset].Name;
bg.GetWidget<LabelWidget>("CURMAP_PLAYERS").GetText = () => Map.PlayerCount.ToString();

View File

@@ -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,

View File

@@ -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)

View File

@@ -35,7 +35,8 @@ namespace OpenRA.Mods.Cnc
Map = w.Map;
Players = w.players.Values.ToDictionary(p => p.InternalName);
Actors = w.WorldActor.Trait<SpawnMapActors>().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", () =>

View File

@@ -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

View File

@@ -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);