Remove OnMouseDown from Widget. Define it on just the widgets that want it.

This commit is contained in:
Paul Chote
2011-07-04 02:09:31 +12:00
parent 7a69daa99c
commit bbeaf2047b
6 changed files with 46 additions and 18 deletions

View File

@@ -24,6 +24,8 @@ namespace OpenRA.Widgets
public string Font = ChromeMetrics.Get<string>("ButtonFont");
public Func<string> GetText;
public Func<bool> IsDisabled = () => false;
public Func<MouseInput, bool> OnMouseDown = mi => false;
public Action OnClick = () => {};
public Action<KeyInput> OnKeyPress = _ => {};
@@ -43,6 +45,8 @@ namespace OpenRA.Widgets
Depressed = widget.Depressed;
VisualHeight = widget.VisualHeight;
GetText = widget.GetText;
OnMouseDown = widget.OnMouseDown;
OnMouseUp = mi => { if (!IsDisabled()) OnClick(); return true; };
OnKeyPress = _ => OnClick();
}

View File

@@ -18,7 +18,7 @@ namespace OpenRA.Widgets
public class DropDownButtonWidget : ButtonWidget
{
Widget panel;
Widget fullscreenMask;
MaskWidget fullscreenMask;
public DropDownButtonWidget()
: base()
@@ -75,17 +75,11 @@ namespace OpenRA.Widgets
panel = p;
// Mask to prevent any clicks from being sent to other widgets
fullscreenMask = new ContainerWidget();
fullscreenMask = new MaskWidget();
fullscreenMask.Bounds = new Rectangle(0, 0, Game.viewport.Width, Game.viewport.Height);
fullscreenMask.OnMouseDown = mi => RemovePanel();
Widget.RootWidget.AddChild(fullscreenMask);
fullscreenMask.OnMouseDown = mi =>
{
RemovePanel();
return true;
};
fullscreenMask.OnMouseUp = mi => true;
var oldBounds = panel.Bounds;
panel.Bounds = new Rectangle(RenderOrigin.X, RenderOrigin.Y + Bounds.Height, oldBounds.Width, oldBounds.Height);
Widget.RootWidget.AddChild(panel);
@@ -114,4 +108,30 @@ namespace OpenRA.Widgets
AttachPanel(panel);
}
}
public class MaskWidget : Widget
{
public Action<MouseInput> OnMouseDown = _ => {};
public MaskWidget() : base() { }
public MaskWidget(MaskWidget other)
: base(other)
{
OnMouseDown = other.OnMouseDown;
}
public override bool HandleMouseInput(MouseInput mi)
{
if (mi.Event != MouseInputEvent.Down && mi.Event != MouseInputEvent.Up)
return false;
if (mi.Event == MouseInputEvent.Down)
OnMouseDown(mi);
return true;
}
public override void DrawInner() { }
public override string GetCursor(int2 pos) { return null; }
public override Widget Clone() { return new MaskWidget(this); }
}
}

View File

@@ -20,6 +20,8 @@ namespace OpenRA.Widgets
{
public Func<Map> Map = () => null;
public Func<Dictionary<int2, Color>> SpawnColors = () => new Dictionary<int2, Color>();
public Action<MouseInput> OnMouseDown = _ => {};
Cache<Map,Bitmap> PreviewCache = new Cache<Map, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Path )));
public MapPreviewWidget() : base() { }
@@ -32,6 +34,15 @@ namespace OpenRA.Widgets
}
public override Widget Clone() { return new MapPreviewWidget(this); }
public override bool HandleMouseInput(MouseInput mi)
{
if (mi.Event != MouseInputEvent.Down)
return false;
OnMouseDown(mi);
return true;
}
public int2 ConvertToPreview(Map map, int2 point)
{
return new int2(MapRect.X + (int)(PreviewScale*(point.X - map.Bounds.Left)) , MapRect.Y + (int)(PreviewScale*(point.Y - map.Bounds.Top)));

View File

@@ -38,7 +38,6 @@ namespace OpenRA.Widgets
static Stack<Widget> WindowList = new Stack<Widget>();
// Common Funcs that most widgets will want
public Func<MouseInput, bool> OnMouseDown = mi => false;
public Func<MouseInput, bool> OnMouseUp = mi => false;
public Func<bool> IsVisible;
@@ -65,7 +64,6 @@ namespace OpenRA.Widgets
Bounds = widget.Bounds;
Parent = widget.Parent;
OnMouseDown = widget.OnMouseDown;
OnMouseUp = widget.OnMouseUp;
IsVisible = widget.IsVisible;
@@ -226,7 +224,6 @@ namespace OpenRA.Widgets
public virtual bool HandleMouseInput(MouseInput mi)
{
// Apply any special logic added by event handlers; they return true if they caught the input
if (mi.Event == MouseInputEvent.Down && OnMouseDown(mi)) return true;
if (mi.Event == MouseInputEvent.Up && OnMouseUp(mi)) return true;
return false;

View File

@@ -120,7 +120,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var map = mapPreview.Map();
if (map == null || mi.Button != MouseButton.Left
|| orderManager.LocalClient.State == Session.ClientState.Ready)
return false;
return;
var p = map.SpawnPoints
.Select((sp, i) => Pair.New(mapPreview.ConvertToPreview(map, sp), i))
@@ -131,8 +131,6 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var owned = orderManager.LobbyInfo.Clients.Any(c => c.SpawnPoint == p);
if (p == 0 || !owned)
orderManager.IssueOrder(Order.Command("spawn {0} {1}".F(orderManager.LocalClient.Index, p)));
return true;
};
var mapTitle = lobby.GetWidget<LabelWidget>("MAP_TITLE");

View File

@@ -65,7 +65,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var map = mapPreview.Map();
if (map == null || mi.Button != MouseButton.Left
|| orderManager.LocalClient.State == Session.ClientState.Ready)
return false;
return;
var p = map.SpawnPoints
.Select((sp, i) => Pair.New(mapPreview.ConvertToPreview(map, sp), i))
@@ -76,8 +76,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var owned = orderManager.LobbyInfo.Clients.Any(c => c.SpawnPoint == p);
if (p == 0 || !owned)
orderManager.IssueOrder(Order.Command("spawn {0} {1}".F(orderManager.LocalClient.Index, p)));
return true;
};
mapPreview.SpawnColors = () =>