From 8ed1fec49c613e5393671c4f481d35e2601ba9db Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Tue, 15 Dec 2009 21:17:38 +1300 Subject: [PATCH] added M-n support to center on a group --- OpenRa.Game/Controller.cs | 2 +- OpenRa.Game/Graphics/Viewport.cs | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index e899d9bdbd..954d71a098 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -164,7 +164,7 @@ namespace OpenRa.Game if (keys.HasModifier(Keys.Alt)) { - // center on this group + Game.viewport.Center(cache[group]); return; } diff --git a/OpenRa.Game/Graphics/Viewport.cs b/OpenRa.Game/Graphics/Viewport.cs index f0345a5b0e..2655c25555 100644 --- a/OpenRa.Game/Graphics/Viewport.cs +++ b/OpenRa.Game/Graphics/Viewport.cs @@ -83,15 +83,20 @@ namespace OpenRa.Game.Graphics return (1 / 24.0f) * (new float2(mi.Location.X, mi.Location.Y) + Location); } + public void Center(IEnumerable actors) + { + if (!actors.Any()) return; + + var avgPos = (1f / actors.Count()) * actors + .Select(a => a.CenterLocation) + .Aggregate((a, b) => a + b); + + scrollPosition = avgPos - .5f * new float2(Width, Height); + } + public void GoToStartLocation() { - var unit = Game.world.Actors - .FirstOrDefault(a => a.Owner == Game.LocalPlayer); - - if (unit == null) - return; - - scrollPosition = unit.CenterLocation - .5f * new float2(Width, Height); + Center(Game.world.Actors.Where(a => a.Owner == Game.LocalPlayer)); } } }