Remove some duplication
This commit is contained in:
@@ -66,27 +66,5 @@ namespace OpenRA.FileFormats
|
|||||||
|
|
||||||
Uid = Package.GetContent("map.uid").ReadAllText();
|
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);
|
|
||||||
|
|
||||||
var size = Math.Max(Width, Height);
|
|
||||||
var dw = (int)(scale * (size - Width)) / 2;
|
|
||||||
var dh = (int)(scale * (size - Height)) / 2;
|
|
||||||
|
|
||||||
return new Rectangle(container.X + dw, container.Y + dh, (int)(Width * scale), (int)(Height * scale));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,24 +35,18 @@ namespace OpenRA.Widgets
|
|||||||
var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont;
|
var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont;
|
||||||
var pos = RenderOrigin;
|
var pos = RenderOrigin;
|
||||||
var rect = RenderBounds;
|
var rect = RenderBounds;
|
||||||
WidgetUtils.DrawPanel("dialog3", new Rectangle(rect.Location,
|
var check = new Rectangle(rect.Location,
|
||||||
new Size(Bounds.Height, Bounds.Height)));
|
new Size(Bounds.Height, Bounds.Height));
|
||||||
|
WidgetUtils.DrawPanel("dialog3", check);
|
||||||
|
|
||||||
var textSize = font.Measure(Text);
|
var textSize = font.Measure(Text);
|
||||||
font.DrawText(Text,
|
font.DrawText(Text,
|
||||||
new float2(rect.Left + rect.Height * 1.5f, pos.Y - baseLine + (Bounds.Height - textSize.Y)/2), Color.White);
|
new float2(rect.Left + rect.Height * 1.5f, pos.Y - baseLine + (Bounds.Height - textSize.Y)/2), Color.White);
|
||||||
|
|
||||||
if (Checked())
|
if (Checked())
|
||||||
{
|
{
|
||||||
Game.chrome.renderer.RgbaSpriteRenderer.Flush();
|
Game.chrome.renderer.RgbaSpriteRenderer.Flush();
|
||||||
|
WidgetUtils.FillRectWithColor(check.InflateBy(-4,-5,-4,-5),Color.White);
|
||||||
Game.chrome.lineRenderer.FillRect(
|
|
||||||
new RectangleF(
|
|
||||||
Game.viewport.Location.X + rect.Left + 4,
|
|
||||||
Game.viewport.Location.Y + rect.Top + 5,
|
|
||||||
rect.Height - 9,
|
|
||||||
rect.Height - 9),
|
|
||||||
Color.White);
|
|
||||||
Game.chrome.lineRenderer.Flush();
|
Game.chrome.lineRenderer.Flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,11 +45,8 @@ namespace OpenRA.Widgets
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override void DrawInner(World world)
|
public override void DrawInner(World world)
|
||||||
{
|
{
|
||||||
var pos = RenderOrigin;
|
WidgetUtils.FillRectWithColor(RenderBounds, GetColor());
|
||||||
var paletteRect = new RectangleF(pos.X + Game.viewport.Location.X,
|
|
||||||
pos.Y + Game.viewport.Location.Y, Bounds.Width, Bounds.Height);
|
|
||||||
Game.chrome.lineRenderer.FillRect(paletteRect, GetColor());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,19 +9,14 @@ namespace OpenRA.Widgets
|
|||||||
{
|
{
|
||||||
class MapPreviewWidget : Widget
|
class MapPreviewWidget : Widget
|
||||||
{
|
{
|
||||||
Sheet mapChooserSheet;
|
public int SpawnClickRadius = 50;
|
||||||
Sprite mapChooserSprite;
|
|
||||||
bool mapPreviewDirty = true;
|
|
||||||
MapStub lastMap;
|
|
||||||
|
|
||||||
public Func<MapStub> Map = () => null;
|
public Func<MapStub> Map = () => null;
|
||||||
public Action<int> OnSpawnClick = spawn => {};
|
public Action<int> OnSpawnClick = spawn => {};
|
||||||
public Func<Dictionary<int2, Color>> SpawnColors = () => new Dictionary<int2, Color>();
|
public Func<Dictionary<int2, Color>> SpawnColors = () => new Dictionary<int2, Color>();
|
||||||
|
|
||||||
public MapPreviewWidget() : base() { }
|
|
||||||
|
|
||||||
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview(stub));
|
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview(stub));
|
||||||
|
|
||||||
|
public MapPreviewWidget() : base() { }
|
||||||
protected MapPreviewWidget(MapPreviewWidget other)
|
protected MapPreviewWidget(MapPreviewWidget other)
|
||||||
: base(other)
|
: base(other)
|
||||||
{
|
{
|
||||||
@@ -30,11 +25,14 @@ namespace OpenRA.Widgets
|
|||||||
OnSpawnClick = other.OnSpawnClick;
|
OnSpawnClick = other.OnSpawnClick;
|
||||||
SpawnColors = other.SpawnColors;
|
SpawnColors = other.SpawnColors;
|
||||||
}
|
}
|
||||||
|
public override Widget Clone() { return new MapPreviewWidget(this); }
|
||||||
static Sprite UnownedSpawn = null;
|
|
||||||
static Sprite OwnedSpawn = null;
|
|
||||||
const int closeEnough = 50;
|
|
||||||
|
|
||||||
|
|
||||||
|
public int2 ConvertToPreview(MapStub map, int2 point)
|
||||||
|
{
|
||||||
|
return new int2(MapRect.X + (int)(PreviewScale*(point.X - map.TopLeft.X)) , MapRect.Y + (int)(PreviewScale*(point.Y - map.TopLeft.Y)));
|
||||||
|
}
|
||||||
|
|
||||||
public override bool HandleInput(MouseInput mi)
|
public override bool HandleInput(MouseInput mi)
|
||||||
{
|
{
|
||||||
var map = Map();
|
var map = Map();
|
||||||
@@ -44,8 +42,8 @@ namespace OpenRA.Widgets
|
|||||||
if (mi.Event == MouseInputEvent.Down && mi.Button == MouseButton.Left)
|
if (mi.Event == MouseInputEvent.Down && mi.Button == MouseButton.Left)
|
||||||
{
|
{
|
||||||
var p = map.Waypoints
|
var p = map.Waypoints
|
||||||
.Select((sp, i) => Pair.New(map.ConvertToPreview(sp.Value, RenderBounds), i))
|
.Select((sp, i) => Pair.New(ConvertToPreview(map, sp.Value), i))
|
||||||
.Where(a => (a.First - mi.Location).LengthSquared < closeEnough)
|
.Where(a => (a.First - mi.Location).LengthSquared < SpawnClickRadius)
|
||||||
.Select(a => a.Second + 1)
|
.Select(a => a.Second + 1)
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
OnSpawnClick(p);
|
OnSpawnClick(p);
|
||||||
@@ -55,8 +53,13 @@ namespace OpenRA.Widgets
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Widget Clone() { return new MapPreviewWidget(this); }
|
Sheet mapChooserSheet;
|
||||||
|
Sprite mapChooserSprite;
|
||||||
|
MapStub lastMap;
|
||||||
|
Rectangle MapRect;
|
||||||
|
float PreviewScale = 0;
|
||||||
|
static Sprite UnownedSpawn = null;
|
||||||
|
static Sprite OwnedSpawn = null;
|
||||||
public override void DrawInner( World world )
|
public override void DrawInner( World world )
|
||||||
{
|
{
|
||||||
if (UnownedSpawn == null)
|
if (UnownedSpawn == null)
|
||||||
@@ -66,39 +69,37 @@ namespace OpenRA.Widgets
|
|||||||
|
|
||||||
var map = Map();
|
var map = Map();
|
||||||
if( map == null ) return;
|
if( map == null ) return;
|
||||||
|
|
||||||
if (lastMap != map)
|
if (lastMap != map)
|
||||||
{
|
{
|
||||||
mapPreviewDirty = true;
|
|
||||||
lastMap = map;
|
lastMap = map;
|
||||||
}
|
|
||||||
|
|
||||||
var mapRect = map.PreviewBounds( RenderBounds );
|
|
||||||
|
|
||||||
if( mapPreviewDirty )
|
// Update image data
|
||||||
{
|
|
||||||
var preview = PreviewCache[map];
|
var preview = PreviewCache[map];
|
||||||
if( mapChooserSheet == null || mapChooserSheet.Size.Width != preview.Width || mapChooserSheet.Size.Height != preview.Height )
|
if( mapChooserSheet == null || mapChooserSheet.Size.Width != preview.Width || mapChooserSheet.Size.Height != preview.Height )
|
||||||
mapChooserSheet = new Sheet( Game.renderer, new Size( preview.Width, preview.Height ) );
|
mapChooserSheet = new Sheet( Game.renderer, new Size( preview.Width, preview.Height ) );
|
||||||
|
|
||||||
mapChooserSheet.Texture.SetData( preview );
|
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.Width, map.Height ), TextureChannel.Alpha );
|
||||||
mapPreviewDirty = false;
|
|
||||||
|
// 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
Game.chrome.renderer.RgbaSpriteRenderer.DrawSprite( mapChooserSprite,
|
Game.chrome.renderer.RgbaSpriteRenderer.DrawSprite( mapChooserSprite,
|
||||||
new float2( mapRect.Location ),
|
new float2(MapRect.Location),
|
||||||
"chrome",
|
"chrome",
|
||||||
new float2( mapRect.Size ) );
|
new float2( MapRect.Size ) );
|
||||||
|
|
||||||
DrawSpawnPoints( map, world );
|
// Overlay spawnpoints
|
||||||
}
|
|
||||||
|
|
||||||
void DrawSpawnPoints(MapStub map, World world)
|
|
||||||
{
|
|
||||||
var colors = SpawnColors();
|
var colors = SpawnColors();
|
||||||
foreach (var p in map.SpawnPoints)
|
foreach (var p in map.SpawnPoints)
|
||||||
{
|
{
|
||||||
var pos = map.ConvertToPreview(p, RenderBounds);
|
var pos = ConvertToPreview(map, p);
|
||||||
var sprite = UnownedSpawn;
|
var sprite = UnownedSpawn;
|
||||||
var offset = new int2(-UnownedSpawn.bounds.Width/2, -UnownedSpawn.bounds.Height/2);
|
var offset = new int2(-UnownedSpawn.bounds.Width/2, -UnownedSpawn.bounds.Height/2);
|
||||||
|
|
||||||
@@ -106,13 +107,8 @@ namespace OpenRA.Widgets
|
|||||||
{
|
{
|
||||||
sprite = OwnedSpawn;
|
sprite = OwnedSpawn;
|
||||||
offset = new int2(-OwnedSpawn.bounds.Width/2, -OwnedSpawn.bounds.Height/2);
|
offset = new int2(-OwnedSpawn.bounds.Width/2, -OwnedSpawn.bounds.Height/2);
|
||||||
|
WidgetUtils.FillRectWithColor(new Rectangle(pos.X + offset.X + 2, pos.Y + offset.Y + 2, 12, 12), colors[p]);
|
||||||
Game.chrome.lineRenderer.FillRect(new RectangleF(
|
|
||||||
Game.viewport.Location.X + pos.X + offset.X + 2,
|
|
||||||
Game.viewport.Location.Y + pos.Y + offset.Y + 2,
|
|
||||||
12, 12), colors[p]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Game.chrome.renderer.RgbaSpriteRenderer.DrawSprite(sprite, pos + offset, "chrome");
|
Game.chrome.renderer.RgbaSpriteRenderer.DrawSprite(sprite, pos + offset, "chrome");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,7 +63,15 @@ namespace OpenRA.Widgets
|
|||||||
DrawRGBA(ss, new float2(x, y));
|
DrawRGBA(ss, new float2(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void FillRectWithColor(Rectangle r, Color c)
|
||||||
|
{
|
||||||
|
Game.chrome.lineRenderer.FillRect(new RectangleF(
|
||||||
|
Game.viewport.Location.X + r.X,
|
||||||
|
Game.viewport.Location.Y + r.Y,
|
||||||
|
r.Width, r.Height), c);
|
||||||
|
}
|
||||||
|
|
||||||
public static int[] GetBorderSizes(string collection)
|
public static int[] GetBorderSizes(string collection)
|
||||||
{
|
{
|
||||||
var images = new[] { "border-t", "border-b", "border-l", "border-r" };
|
var images = new[] { "border-t", "border-b", "border-l", "border-r" };
|
||||||
|
|||||||
Reference in New Issue
Block a user