Minimap sanity; part 1: rewrite the core radar logic
This commit is contained in:
@@ -19,96 +19,6 @@ namespace OpenRA.Graphics
|
||||
{
|
||||
class Minimap
|
||||
{
|
||||
readonly World world;
|
||||
Sheet sheet;
|
||||
Sprite sprite;
|
||||
Bitmap terrain, customLayer;
|
||||
Rectangle bounds;
|
||||
|
||||
const int alpha = 230;
|
||||
|
||||
public Minimap(World world)
|
||||
{
|
||||
this.world = world;
|
||||
sheet = new Sheet( new Size(world.Map.MapSize.X, world.Map.MapSize.Y));
|
||||
var size = Math.Max(world.Map.Width, world.Map.Height);
|
||||
var dw = (size - world.Map.Width) / 2;
|
||||
var dh = (size - world.Map.Height) / 2;
|
||||
|
||||
bounds = new Rectangle(world.Map.TopLeft.X - dw, world.Map.TopLeft.Y - dh, size, size);
|
||||
|
||||
sprite = new Sprite(sheet, bounds, TextureChannel.Alpha);
|
||||
|
||||
shroudColor = Color.FromArgb(alpha, Color.Black);
|
||||
}
|
||||
|
||||
public static Rectangle MakeMinimapBounds(Map m)
|
||||
{
|
||||
var size = Math.Max(m.Width, m.Height);
|
||||
var dw = (size - m.Width) / 2;
|
||||
var dh = (size - m.Height) / 2;
|
||||
|
||||
return new Rectangle(m.TopLeft.X - dw, m.TopLeft.Y - dh, size, size);
|
||||
}
|
||||
|
||||
static Color shroudColor;
|
||||
|
||||
public void InvalidateCustom() { customLayer = null; }
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (terrain == null)
|
||||
terrain = RenderTerrainBitmap(world.Map);
|
||||
|
||||
|
||||
// Custom terrain layer
|
||||
if (customLayer == null)
|
||||
customLayer = AddCustomTerrain(world,terrain);
|
||||
|
||||
if (!world.GameHasStarted || !world.Queries.OwnedBy[world.LocalPlayer].WithTrait<ProvidesRadar>().Any())
|
||||
return;
|
||||
|
||||
sheet.Texture.SetData(AddActors(world, customLayer));
|
||||
}
|
||||
|
||||
public void Draw(RectangleF rect)
|
||||
{
|
||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(sprite,
|
||||
new float2(rect.X, rect.Y), "chrome", new float2(rect.Width, rect.Height));
|
||||
Game.Renderer.RgbaSpriteRenderer.Flush();
|
||||
}
|
||||
|
||||
public static int2 CellToMinimapPixel(Map map, RectangleF viewRect, int2 p)
|
||||
{
|
||||
var size = Math.Max(map.Width, map.Height);
|
||||
var dw = (size - map.Width) / 2;
|
||||
var dh = (size - map.Height) / 2;
|
||||
var bounds = new Rectangle(map.TopLeft.X - dw, map.TopLeft.Y - dh, size, size);
|
||||
|
||||
var fx = (float)(p.X - bounds.X) / bounds.Width;
|
||||
var fy = (float)(p.Y - bounds.Y) / bounds.Height;
|
||||
|
||||
return new int2(
|
||||
(int)(viewRect.Width * fx + viewRect.Left),
|
||||
(int)(viewRect.Height * fy + viewRect.Top));
|
||||
}
|
||||
|
||||
public static int2 MinimapPixelToCell(Map map, RectangleF viewRect, int2 p)
|
||||
{
|
||||
var size = Math.Max(map.Width, map.Height);
|
||||
var dw = (size - map.Width) / 2;
|
||||
var dh = (size - map.Height) / 2;
|
||||
var bounds = new Rectangle(map.TopLeft.X - dw, map.TopLeft.Y - dh, size, size);
|
||||
|
||||
var fx = (float)(p.X - viewRect.Left) / viewRect.Width;
|
||||
var fy = (float)(p.Y - viewRect.Top) / viewRect.Height;
|
||||
|
||||
return new int2(
|
||||
(int)(bounds.Width * fx + bounds.Left),
|
||||
(int)(bounds.Height * fy + bounds.Top));
|
||||
}
|
||||
|
||||
|
||||
static int NextPowerOf2(int v)
|
||||
{
|
||||
--v;
|
||||
@@ -139,9 +49,7 @@ namespace OpenRA.Graphics
|
||||
var mapX = x + map.TopLeft.X;
|
||||
var mapY = y + map.TopLeft.Y;
|
||||
var type = tileset.GetTerrainType(map.MapTiles[mapX, mapY]);
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = map.IsInMap(mapX, mapY)
|
||||
? Color.FromArgb(alpha, tileset.Terrain[type].Color).ToArgb()
|
||||
: shroudColor.ToArgb();
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = tileset.Terrain[type].Color.ToArgb();
|
||||
}
|
||||
}
|
||||
terrain.UnlockBits(bitmapData);
|
||||
@@ -220,6 +128,11 @@ namespace OpenRA.Graphics
|
||||
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
|
||||
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
|
||||
var shroud = Color.Black.ToArgb();
|
||||
var fogOpacity = 0.5f;
|
||||
|
||||
|
||||
|
||||
unsafe
|
||||
{
|
||||
int* c = (int*)bitmapData.Scan0;
|
||||
@@ -234,11 +147,17 @@ namespace OpenRA.Graphics
|
||||
var mapX = x + map.TopLeft.X;
|
||||
var mapY = y + map.TopLeft.Y;
|
||||
|
||||
if (!world.LocalPlayer.Shroud.DisplayOnRadar(mapX, mapY))
|
||||
if (!world.LocalPlayer.Shroud.IsExplored(mapX, mapY))
|
||||
{
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = shroudColor.ToArgb();
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = shroud;
|
||||
continue;
|
||||
}
|
||||
if (!world.LocalPlayer.Shroud.IsVisible(mapX,mapY))
|
||||
{
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = Util.Lerp(fogOpacity, Color.FromArgb(*(c + (y * bitmapData.Stride >> 2) + x)), Color.Black).ToArgb();
|
||||
continue;
|
||||
}
|
||||
|
||||
var b = world.WorldActor.traits.Get<BuildingInfluence>().GetBuildingAt(new int2(mapX, mapY));
|
||||
|
||||
if (b != null)
|
||||
|
||||
@@ -44,14 +44,20 @@ namespace OpenRA
|
||||
}
|
||||
|
||||
public bool IsExplored(int2 xy) { return IsExplored(xy.X, xy.Y); }
|
||||
bool IsExplored(int x, int y)
|
||||
public bool IsExplored(int x, int y)
|
||||
{
|
||||
if (disabled)
|
||||
return true;
|
||||
return shroud.exploredCells[x,y];
|
||||
}
|
||||
|
||||
public bool DisplayOnRadar(int x, int y) { return IsExplored(x, y); }
|
||||
public bool IsVisible(int2 xy) { return IsVisible(xy.X, xy.Y); }
|
||||
public bool IsVisible(int x, int y)
|
||||
{
|
||||
if (disabled)
|
||||
return true;
|
||||
return shroud.visibleCells[x,y] != 0;
|
||||
}
|
||||
|
||||
static readonly byte[][] SpecialShroudTiles =
|
||||
{
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace OpenRA.Widgets.Delegates
|
||||
var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG");
|
||||
|
||||
Game.OnGameStart += () => r.OpenWindow("INGAME_ROOT");
|
||||
Game.OnGameStart += () => gameRoot.GetWidget<RadarBinWidget>("INGAME_RADAR_BIN").SetWorld(Game.world);
|
||||
|
||||
r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => {
|
||||
optionsBG.Visible = !optionsBG.Visible;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
@@ -27,16 +27,38 @@ namespace OpenRA.Widgets
|
||||
bool radarAnimating = false;
|
||||
bool hasRadar = false;
|
||||
|
||||
Sheet radarSheet;
|
||||
Sprite radarSprite;
|
||||
|
||||
string radarCollection;
|
||||
|
||||
|
||||
World world;
|
||||
float previewScale = 0;
|
||||
RectangleF mapRect = Rectangle.Empty;
|
||||
int2 previewOrigin;
|
||||
Bitmap terrainBitmap = null;
|
||||
public void SetWorld(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)(previewScale * (size - world.Map.Width)) / 2, (int)(previewScale * (size - world.Map.Height)) / 2);
|
||||
mapRect = new RectangleF(radarOrigin.X + previewOrigin.X, radarOrigin.Y + previewOrigin.Y, (int)(world.Map.Width * previewScale), (int)(world.Map.Height * previewScale));
|
||||
|
||||
terrainBitmap = Minimap.RenderTerrainBitmap(world.Map);
|
||||
radarSheet = new Sheet(new Size( terrainBitmap.Width, terrainBitmap.Height ) );
|
||||
radarSprite = new Sprite( radarSheet, new Rectangle( 0, 0, world.Map.Width, world.Map.Height ), TextureChannel.Alpha );
|
||||
}
|
||||
|
||||
public override string GetCursor(int2 pos)
|
||||
{
|
||||
if (minimap == null)
|
||||
if (world == null)
|
||||
return "default";
|
||||
|
||||
var mapRect = new RectangleF(radarOrigin.X + 9, radarOrigin.Y + (192 - radarMinimapHeight) / 2,
|
||||
192, radarMinimapHeight);
|
||||
|
||||
var loc = Minimap.MinimapPixelToCell(Game.world.Map, mapRect, pos);
|
||||
var loc = MinimapPixelToCell(pos);
|
||||
|
||||
var mi = new MouseInput
|
||||
{
|
||||
@@ -45,7 +67,7 @@ namespace OpenRA.Widgets
|
||||
Modifiers = Game.controller.GetModifiers()
|
||||
};
|
||||
|
||||
var cursor = Game.controller.orderGenerator.GetCursor( Game.world, loc, mi );
|
||||
var cursor = Game.controller.orderGenerator.GetCursor( world, loc, mi );
|
||||
if (cursor == null)
|
||||
return "default";
|
||||
|
||||
@@ -56,14 +78,10 @@ namespace OpenRA.Widgets
|
||||
{
|
||||
if (!hasRadar || radarAnimating) return false; // we're not set up for this.
|
||||
|
||||
var mapRect = new RectangleF(radarOrigin.X + 9, radarOrigin.Y + (192 - radarMinimapHeight) / 2,
|
||||
192, radarMinimapHeight);
|
||||
|
||||
if (!mapRect.Contains(mi.Location.ToPointF()) || minimap == null)
|
||||
if (!mapRect.Contains(mi.Location.ToPointF()))
|
||||
return false;
|
||||
|
||||
var loc = Minimap.MinimapPixelToCell(Game.world.Map, mapRect, mi.Location);
|
||||
|
||||
var loc = MinimapPixelToCell(mi.Location);
|
||||
if ((mi.Event == MouseInputEvent.Down || mi.Event == MouseInputEvent.Move) && mi.Button == MouseButton.Left)
|
||||
Game.viewport.Center(loc);
|
||||
|
||||
@@ -90,11 +108,9 @@ namespace OpenRA.Widgets
|
||||
|
||||
public override Rectangle EventBounds
|
||||
{
|
||||
get { return new Rectangle((int)radarOrigin.X + 9, (int)(radarOrigin.Y + (192 - radarMinimapHeight) / 2),
|
||||
192, (int)radarMinimapHeight);}
|
||||
get { return new Rectangle((int)mapRect.X, (int)mapRect.Y, (int)mapRect.Width, (int)mapRect.Height);}
|
||||
}
|
||||
|
||||
Minimap minimap = null;
|
||||
public override void DrawInner(World world)
|
||||
{
|
||||
radarCollection = "radar-" + world.LocalPlayer.Country.Race;
|
||||
@@ -111,30 +127,28 @@ namespace OpenRA.Widgets
|
||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "left"), radarOrigin, "chrome");
|
||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "right"), radarOrigin + new float2(201, 0), "chrome");
|
||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "bottom"), radarOrigin + new float2(0, 192), "chrome");
|
||||
|
||||
if (radarAnimating)
|
||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(ChromeProvider.GetImage(radarCollection, "bg"), radarOrigin + new float2(9, 0), "chrome");
|
||||
|
||||
Game.Renderer.RgbaSpriteRenderer.Flush();
|
||||
|
||||
if (minimap == null)
|
||||
minimap = new Minimap(world);
|
||||
// Custom terrain layer
|
||||
var custom = Minimap.AddCustomTerrain(world,terrainBitmap);
|
||||
var final = Minimap.AddActors(world, custom);
|
||||
radarSheet.Texture.SetData(final);
|
||||
|
||||
if (radarAnimationFrame >= radarSlideAnimationLength)
|
||||
{
|
||||
var mapRect = new RectangleF(radarOrigin.X + 9, radarOrigin.Y + (192 - radarMinimapHeight) / 2, 192, radarMinimapHeight);
|
||||
minimap.Draw(mapRect);
|
||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite( radarSprite,
|
||||
new float2(mapRect.Location), "chrome", new float2(mapRect.Size) );
|
||||
}
|
||||
}
|
||||
|
||||
public override void Tick(World world)
|
||||
public override void Tick(World w)
|
||||
{
|
||||
if (world.LocalPlayer != null && minimap != null)
|
||||
minimap.Update();
|
||||
|
||||
if (!radarAnimating)
|
||||
if (world == null)
|
||||
return;
|
||||
|
||||
if (radarAnimating)
|
||||
{
|
||||
// Increment frame
|
||||
if (hasRadar)
|
||||
radarAnimationFrame++;
|
||||
@@ -157,11 +171,24 @@ namespace OpenRA.Widgets
|
||||
|
||||
// Minimap height
|
||||
if (radarAnimationFrame >= radarSlideAnimationLength)
|
||||
radarMinimapHeight = float2.Lerp(0, 192, (radarAnimationFrame - radarSlideAnimationLength) * 1.0f / radarActivateAnimationLength);
|
||||
radarMinimapHeight = float2.Lerp(0, 1, (radarAnimationFrame - radarSlideAnimationLength) * 1.0f / radarActivateAnimationLength);
|
||||
|
||||
// Animation is complete
|
||||
if (radarAnimationFrame == (hasRadar ? radarSlideAnimationLength + radarActivateAnimationLength : 0))
|
||||
radarAnimating = false;
|
||||
}
|
||||
|
||||
mapRect = new RectangleF(radarOrigin.X + previewOrigin.X, radarOrigin.Y + previewOrigin.Y + (int)(world.Map.Height * previewScale * (1 - radarMinimapHeight)/2), (int)(world.Map.Width * previewScale), (int)(world.Map.Height * previewScale * radarMinimapHeight));
|
||||
}
|
||||
|
||||
int2 CellToMinimapPixel(int2 p)
|
||||
{
|
||||
return new int2((int)(mapRect.X +previewScale*(p.X - world.Map.TopLeft.X)), (int)(mapRect.Y + previewScale*(p.Y - world.Map.TopLeft.Y)));
|
||||
}
|
||||
|
||||
int2 MinimapPixelToCell(int2 p)
|
||||
{
|
||||
return new int2(world.Map.TopLeft.X + (int)((p.X - mapRect.X)/previewScale), world.Map.TopLeft.Y + (int)((p.Y - mapRect.Y)/previewScale));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user