Move MapPreviewWidget.HandleInputInner to the lobby delegate.

This commit is contained in:
Paul Chote
2011-01-05 22:16:01 +13:00
parent 7b5b84b1b7
commit 3e1db3e8ae
3 changed files with 23 additions and 35 deletions

View File

@@ -19,10 +19,7 @@ namespace OpenRA.Widgets
{ {
public class MapPreviewWidget : Widget public class MapPreviewWidget : Widget
{ {
public int SpawnClickRadius = 50;
public Func<MapStub> Map = () => null; public Func<MapStub> Map = () => null;
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>();
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Path ))); static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Path )));
@@ -32,37 +29,17 @@ namespace OpenRA.Widgets
{ {
lastMap = other.lastMap; lastMap = other.lastMap;
Map = other.Map; Map = other.Map;
OnSpawnClick = other.OnSpawnClick;
SpawnColors = other.SpawnColors; SpawnColors = other.SpawnColors;
} }
public override Widget Clone() { return new MapPreviewWidget(this); } public override Widget Clone() { return new MapPreviewWidget(this); }
public override bool HandleInputInner(MouseInput mi) { return true; }
public int2 ConvertToPreview(MapStub map, int2 point) public int2 ConvertToPreview(MapStub map, int2 point)
{ {
return new int2(MapRect.X + (int)(PreviewScale*(point.X - map.Bounds.Left)) , MapRect.Y + (int)(PreviewScale*(point.Y - map.Bounds.Top))); return new int2(MapRect.X + (int)(PreviewScale*(point.X - map.Bounds.Left)) , MapRect.Y + (int)(PreviewScale*(point.Y - map.Bounds.Top)));
} }
public override bool HandleInputInner(MouseInput mi)
{
var map = Map();
if (map == null)
return false;
if (mi.Event == MouseInputEvent.Down && mi.Button == MouseButton.Left)
{
var p = map.Waypoints
.Select((sp, i) => Pair.New(ConvertToPreview(map, sp.Value), i))
.Where(a => (a.First - mi.Location).LengthSquared < SpawnClickRadius)
.Select(a => a.Second + 1)
.FirstOrDefault();
OnSpawnClick(p);
return true;
}
return false;
}
Sheet mapChooserSheet; Sheet mapChooserSheet;
Sprite mapChooserSprite; Sprite mapChooserSprite;
MapStub lastMap; MapStub lastMap;

View File

@@ -194,10 +194,6 @@ namespace OpenRA.Widgets
return EventBounds.Contains(pos.ToPoint()) ? GetCursor(pos) : null; return EventBounds.Contains(pos.ToPoint()) ? GetCursor(pos) : null;
} }
// Hack: Don't eat mouse input that others want
// TODO: Solve this properly
public virtual bool HandleInputInner(MouseInput mi) { return !ClickThrough && mi.Button == MouseButton.Left && mi.Event != MouseInputEvent.Move; }
public static bool HandleInput(MouseInput mi) public static bool HandleInput(MouseInput mi)
{ {
bool handled = false; bool handled = false;
@@ -239,6 +235,9 @@ namespace OpenRA.Widgets
return true; return true;
} }
// Hack: Don't eat mouse input that others want
// TODO: Solve this properly
public virtual bool HandleInputInner(MouseInput mi) { return !ClickThrough && mi.Button == MouseButton.Left && mi.Event != MouseInputEvent.Move; }
public virtual bool HandleKeyPressInner(KeyInput e) { return false; } public virtual bool HandleKeyPressInner(KeyInput e) { return false; }
public virtual bool HandleKeyPressOuter(KeyInput e) public virtual bool HandleKeyPressOuter(KeyInput e)

View File

@@ -49,12 +49,24 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
var mapPreview = lobby.GetWidget<MapPreviewWidget>("LOBBY_MAP_PREVIEW"); var mapPreview = lobby.GetWidget<MapPreviewWidget>("LOBBY_MAP_PREVIEW");
mapPreview.Map = () => Map; mapPreview.Map = () => Map;
mapPreview.OnSpawnClick = sp => mapPreview.OnMouseDown = mi =>
{ {
if (orderManager.LocalClient.State == Session.ClientState.Ready) return; var map = mapPreview.Map();
var owned = orderManager.LobbyInfo.Clients.Any(c => c.SpawnPoint == sp); if (map == null || mi.Button != MouseButton.Left
if (sp == 0 || !owned) || orderManager.LocalClient.State == Session.ClientState.Ready)
orderManager.IssueOrder(Order.Command("spawn {0}".F(sp))); return false;
var p = map.Waypoints
.Select((sp, i) => Pair.New(mapPreview.ConvertToPreview(map, sp.Value), i))
.Where(a => (a.First - mi.Location).LengthSquared < 64)
.Select(a => a.Second + 1)
.FirstOrDefault();
var owned = orderManager.LobbyInfo.Clients.Any(c => c.SpawnPoint == p);
if (p == 0 || !owned)
orderManager.IssueOrder(Order.Command("spawn {0}".F(p)));
return true;
}; };
mapPreview.SpawnColors = () => mapPreview.SpawnColors = () =>