diff --git a/OpenRA.Game/Chrome.cs b/OpenRA.Game/Chrome.cs index 9dfa0b919d..c4ae596462 100644 --- a/OpenRA.Game/Chrome.cs +++ b/OpenRA.Game/Chrome.cs @@ -15,7 +15,7 @@ using OpenRA.Widgets; namespace OpenRA { - class Chrome : IHandleInput + class Chrome { public readonly Renderer renderer; public readonly LineRenderer lineRenderer; @@ -44,37 +44,10 @@ namespace OpenRA if (!world.GameHasStarted) return; if (world.LocalPlayer == null) return; - ++ticksSinceLastMove; + ++Widget.ticksSinceLastMove; } public void Draw(World world) { Widget.RootWidget.Draw(world); shpRenderer.Flush(); rgbaRenderer.Flush(); lineRenderer.Flush(); } - - public int ticksSinceLastMove = 0; - public int2 lastMousePos; - public bool HandleInput(World world, MouseInput mi) - { - if (Widget.SelectedWidget != null && Widget.SelectedWidget.HandleMouseInputOuter(mi)) - return true; - - if (Widget.RootWidget.HandleMouseInputOuter(mi)) - return true; - if (mi.Event == MouseInputEvent.Move) - { - lastMousePos = mi.Location; - ticksSinceLastMove = 0; - } - return false; - } - - public bool HandleKeyPress(KeyInput e) - { - if (Widget.SelectedWidget != null) - return Widget.SelectedWidget.HandleKeyPressOuter(e); - - if (Widget.RootWidget.HandleKeyPressOuter(e)) - return true; - return false; - } } } diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 8febdd7df6..4cb308eb13 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -478,7 +478,7 @@ namespace OpenRA { int sync = world.SyncHash(); - if (chrome.HandleKeyPress(e)) + if (Widget.HandleKeyPress(e)) return; switch (e.KeyName) diff --git a/OpenRA.Game/Graphics/Viewport.cs b/OpenRA.Game/Graphics/Viewport.cs index 68a1a2045a..8d77b3fa9f 100644 --- a/OpenRA.Game/Graphics/Viewport.cs +++ b/OpenRA.Game/Graphics/Viewport.cs @@ -42,7 +42,7 @@ namespace OpenRA.Graphics scrollPosition = scrollPosition + delta; } - public IEnumerable regions { get { return new IHandleInput[] { Game.chrome, Game.controller }; } } + public IEnumerable regions { get { return new IHandleInput[] { Widget.RootWidget, Game.controller }; } } public Viewport(float2 screenSize, int2 mapStart, int2 mapEnd, Renderer renderer) { diff --git a/OpenRA.Game/Widgets/BuildPaletteWidget.cs b/OpenRA.Game/Widgets/BuildPaletteWidget.cs index 04cf47c9a3..641daaad60 100644 --- a/OpenRA.Game/Widgets/BuildPaletteWidget.cs +++ b/OpenRA.Game/Widgets/BuildPaletteWidget.cs @@ -137,7 +137,7 @@ namespace OpenRA.Widgets currentTab = produces; } - public override bool HandleKeyPress(KeyInput e) + public override bool HandleKeyPressInner(KeyInput e) { if (e.KeyChar == '\t') { @@ -148,7 +148,7 @@ namespace OpenRA.Widgets return DoBuildingHotkey(Char.ToLowerInvariant(e.KeyChar), Game.world); } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (mi.Event != MouseInputEvent.Down) return false; @@ -223,7 +223,7 @@ namespace OpenRA.Widgets var firstOfThis = queue.AllItems(queueName).FirstOrDefault(a => a.Item == item.Name); - if (rect.Contains(Game.chrome.lastMousePos.ToPoint())) + if (rect.Contains(Widget.lastMousePos.ToPoint())) tooltipItem = item.Name; var overlayPos = drawPos + new float2((64 - ready.Image.size.X) / 2, 2); @@ -421,7 +421,7 @@ namespace OpenRA.Widgets var rect = new Rectangle((int)x,(int)y,(int)tabWidth,(int)tabHeight); tabs.Add(Pair.New(rect, HandleTabClick(groupName, world))); - if (rect.Contains(Game.chrome.lastMousePos.ToPoint())) + if (rect.Contains(Widget.lastMousePos.ToPoint())) { var text = CategoryNameRemaps.ContainsKey(groupName) ? CategoryNameRemaps[groupName] : groupName; var sz = Game.chrome.renderer.BoldFont.Measure(text); diff --git a/OpenRA.Game/Widgets/ButtonWidget.cs b/OpenRA.Game/Widgets/ButtonWidget.cs index 8949d14c23..252bf3b45a 100644 --- a/OpenRA.Game/Widgets/ButtonWidget.cs +++ b/OpenRA.Game/Widgets/ButtonWidget.cs @@ -42,7 +42,7 @@ namespace OpenRA.Widgets return base.LoseFocus(mi); } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (mi.Event == MouseInputEvent.Down && !TakeFocus(mi)) return false; diff --git a/OpenRA.Game/Widgets/ChatEntryWidget.cs b/OpenRA.Game/Widgets/ChatEntryWidget.cs index 68924a170b..3842bbbc72 100644 --- a/OpenRA.Game/Widgets/ChatEntryWidget.cs +++ b/OpenRA.Game/Widgets/ChatEntryWidget.cs @@ -44,9 +44,9 @@ namespace OpenRA.Widgets return composing ? false : base.LoseFocus(mi); } - public override bool HandleInput(MouseInput mi) { return false; } + public override bool HandleInputInner(MouseInput mi) { return false; } - public override bool HandleKeyPress(KeyInput e) + public override bool HandleKeyPressInner(KeyInput e) { if (e.KeyChar == '\r') { @@ -94,7 +94,7 @@ namespace OpenRA.Widgets return false; } - return base.HandleKeyPress(e); + return base.HandleKeyPressInner(e); } } } diff --git a/OpenRA.Game/Widgets/CheckboxWidget.cs b/OpenRA.Game/Widgets/CheckboxWidget.cs index 8ee8871d62..5a5ec78890 100644 --- a/OpenRA.Game/Widgets/CheckboxWidget.cs +++ b/OpenRA.Game/Widgets/CheckboxWidget.cs @@ -41,7 +41,7 @@ namespace OpenRA.Widgets } } - public override bool HandleInput(MouseInput mi) { return true; } + public override bool HandleInputInner(MouseInput mi) { return true; } public CheckboxWidget() : base() { } diff --git a/OpenRA.Game/Widgets/ListBoxWidget.cs b/OpenRA.Game/Widgets/ListBoxWidget.cs index c39b6ecdf6..c4f0b303fa 100644 --- a/OpenRA.Game/Widgets/ListBoxWidget.cs +++ b/OpenRA.Game/Widgets/ListBoxWidget.cs @@ -86,7 +86,7 @@ namespace OpenRA.Widgets return base.LoseFocus(mi); } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (mi.Event == MouseInputEvent.Down && !TakeFocus(mi)) return false; diff --git a/OpenRA.Game/Widgets/MapPreviewWidget.cs b/OpenRA.Game/Widgets/MapPreviewWidget.cs old mode 100755 new mode 100644 index 127967b7f0..2f883cf386 --- a/OpenRA.Game/Widgets/MapPreviewWidget.cs +++ b/OpenRA.Game/Widgets/MapPreviewWidget.cs @@ -43,7 +43,7 @@ namespace OpenRA.Widgets 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 HandleInputInner(MouseInput mi) { var map = Map(); if (map == null) diff --git a/OpenRA.Game/Widgets/MoneyBinWidget.cs b/OpenRA.Game/Widgets/MoneyBinWidget.cs index 34bd83a306..c4a7839c42 100644 --- a/OpenRA.Game/Widgets/MoneyBinWidget.cs +++ b/OpenRA.Game/Widgets/MoneyBinWidget.cs @@ -72,7 +72,7 @@ namespace OpenRA.Widgets } } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (mi.Event == MouseInputEvent.Down) { diff --git a/OpenRA.Game/Widgets/OrderButtonWidget.cs b/OpenRA.Game/Widgets/OrderButtonWidget.cs index 702e24e6b5..b84a77bb8b 100644 --- a/OpenRA.Game/Widgets/OrderButtonWidget.cs +++ b/OpenRA.Game/Widgets/OrderButtonWidget.cs @@ -24,7 +24,7 @@ namespace OpenRA.Widgets var image = ChromeProvider.GetImage(Game.chrome.renderer, Image + "-button", GetImage()); var rect = new Rectangle(RenderBounds.X, RenderBounds.Y, (int)image.size.X, (int)image.size.Y); - if (rect.Contains(Game.chrome.lastMousePos.ToPoint())) + if (rect.Contains(Widget.lastMousePos.ToPoint())) { rect = rect.InflateBy(3, 3, 3, 3); var pos = new int2(rect.Left, rect.Top); diff --git a/OpenRA.Game/Widgets/RadarBinWidget.cs b/OpenRA.Game/Widgets/RadarBinWidget.cs index 385aa05f7f..9e0a4828c0 100644 --- a/OpenRA.Game/Widgets/RadarBinWidget.cs +++ b/OpenRA.Game/Widgets/RadarBinWidget.cs @@ -50,7 +50,7 @@ namespace OpenRA.Widgets return SequenceProvider.HasCursorSequence(cursor+"-minimap") ? cursor+"-minimap" : cursor; } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (!hasRadar || radarAnimating) return false; // we're not set up for this. diff --git a/OpenRA.Game/Widgets/SliderWidget.cs b/OpenRA.Game/Widgets/SliderWidget.cs index 27272d7045..6181f311e8 100644 --- a/OpenRA.Game/Widgets/SliderWidget.cs +++ b/OpenRA.Game/Widgets/SliderWidget.cs @@ -43,7 +43,7 @@ namespace OpenRA.Widgets isMoving = other.isMoving; } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (mi.Event == MouseInputEvent.Down && !TakeFocus(mi)) return false; diff --git a/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs b/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs index b8edfd092f..35a2139583 100644 --- a/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs +++ b/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs @@ -47,7 +47,7 @@ namespace OpenRA.Widgets get { return buttons.Any() ? buttons.Select(b => b.First).Aggregate(Rectangle.Union) : Bounds; } } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (mi.Event == MouseInputEvent.Down) { @@ -92,7 +92,7 @@ namespace OpenRA.Widgets var drawPos = new float2(rectBounds.X + 5, y); var rect = new Rectangle(rectBounds.X + 5, y, 64, 48); - if (rect.Contains(Game.chrome.lastMousePos.ToPoint())) + if (rect.Contains(Widget.lastMousePos.ToPoint())) { var pos = drawPos.ToInt2(); var tl = new int2(pos.X-3,pos.Y-3); diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs index 141689596a..1284078222 100644 --- a/OpenRA.Game/Widgets/TextFieldWidget.cs +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -44,7 +44,7 @@ namespace OpenRA.Widgets return lose; } - public override bool HandleInput(MouseInput mi) + public override bool HandleInputInner(MouseInput mi) { if (mi.Event == MouseInputEvent.Move) return false; @@ -61,7 +61,7 @@ namespace OpenRA.Widgets return true; } - public override bool HandleKeyPress(KeyInput e) + public override bool HandleKeyPressInner(KeyInput e) { // Only take input if we are focused if (!Focused) diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index dafdb387b6..78f9c7e709 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -13,10 +13,11 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; using OpenRA.FileFormats; +using OpenRA.Graphics; namespace OpenRA.Widgets { - public abstract class Widget + public abstract class Widget : IHandleInput { // Info defined in YAML public string Id = null; @@ -186,7 +187,26 @@ namespace OpenRA.Widgets return EventBounds.Contains(pos.ToPoint()) ? GetCursor(pos) : null; } - public virtual bool HandleInput(MouseInput mi) { return !ClickThrough; } + public virtual bool HandleInputInner(MouseInput mi) { return !ClickThrough; } + + public static int ticksSinceLastMove = 0; + public static int2 lastMousePos; + public bool HandleInput(World world, MouseInput mi) + { + if (SelectedWidget != null && SelectedWidget.HandleMouseInputOuter(mi)) + return true; + + if (Widget.RootWidget.HandleMouseInputOuter(mi)) + return true; + + if (mi.Event == MouseInputEvent.Move) + { + lastMousePos = mi.Location; + ticksSinceLastMove = 0; + } + return false; + } + public bool HandleMouseInputOuter(MouseInput mi) { // Are we able to handle this event? @@ -200,7 +220,7 @@ namespace OpenRA.Widgets // Do any widgety behavior (button click etc) // Return false if it can't handle any user actions - if (!HandleInput(mi)) + if (!HandleInputInner(mi)) return false; // Apply any special logic added by delegates; they return true if they caught the input @@ -212,7 +232,7 @@ namespace OpenRA.Widgets } - public virtual bool HandleKeyPress(KeyInput e) { return false; } + public virtual bool HandleKeyPressInner(KeyInput e) { return false; } public virtual bool HandleKeyPressOuter(KeyInput e) { if (!IsVisible()) @@ -224,7 +244,7 @@ namespace OpenRA.Widgets return true; // Do any widgety behavior (enter text etc) - var handled = HandleKeyPress(e); + var handled = HandleKeyPressInner(e); // Apply any special logic added by delegates; they return true if they caught the input if (OnKeyPress(e)) return true; @@ -232,6 +252,16 @@ namespace OpenRA.Widgets return handled; } + public static bool HandleKeyPress(KeyInput e) + { + if (SelectedWidget != null) + return SelectedWidget.HandleKeyPressOuter(e); + + if (RootWidget.HandleKeyPressOuter(e)) + return true; + return false; + } + public abstract void DrawInner( World world ); public virtual void Draw(World world) diff --git a/OpenRA.Game/Widgets/WorldTooltipWidget.cs b/OpenRA.Game/Widgets/WorldTooltipWidget.cs index ecf82d39d3..6fde37ffcb 100644 --- a/OpenRA.Game/Widgets/WorldTooltipWidget.cs +++ b/OpenRA.Game/Widgets/WorldTooltipWidget.cs @@ -24,10 +24,10 @@ namespace OpenRA.Widgets public override void DrawInner(World world) { - if (Game.chrome.ticksSinceLastMove < worldTooltipDelay || world == null || world.LocalPlayer == null) + if (Widget.ticksSinceLastMove < worldTooltipDelay || world == null || world.LocalPlayer == null) return; - var actor = world.FindUnitsAtMouse(Game.chrome.lastMousePos).FirstOrDefault(); + var actor = world.FindUnitsAtMouse(Widget.lastMousePos).FirstOrDefault(); if (actor == null) return; var text = actor.Info.Traits.Contains() @@ -51,23 +51,23 @@ namespace OpenRA.Widgets sz.Y += 24; WidgetUtils.DrawPanel("dialog4", Rectangle.FromLTRB( - Game.chrome.lastMousePos.X + 20, Game.chrome.lastMousePos.Y + 20, - Game.chrome.lastMousePos.X + sz.X + 20, Game.chrome.lastMousePos.Y + sz.Y + 20)); + Widget.lastMousePos.X + 20, Widget.lastMousePos.Y + 20, + Widget.lastMousePos.X + sz.X + 20, Widget.lastMousePos.Y + sz.Y + 20)); renderer.BoldFont.DrawText(text, - new float2(Game.chrome.lastMousePos.X + 30, Game.chrome.lastMousePos.Y + 30), Color.White); + new float2(Widget.lastMousePos.X + 30, Widget.lastMousePos.Y + 30), Color.White); if (text2 != "") { renderer.RegularFont.DrawText(text2, - new float2(Game.chrome.lastMousePos.X + 65, Game.chrome.lastMousePos.Y + 50), actor.Owner.Color); + new float2(Widget.lastMousePos.X + 65, Widget.lastMousePos.Y + 50), actor.Owner.Color); renderer.RegularFont.DrawText(text3, - new float2(Game.chrome.lastMousePos.X + 65 + sz2.X, Game.chrome.lastMousePos.Y + 50), Color.White); + new float2(Widget.lastMousePos.X + 65 + sz2.X, Widget.lastMousePos.Y + 50), Color.White); WidgetUtils.DrawRGBA( ChromeProvider.GetImage(Game.chrome.renderer, "flags", actor.Owner.Country.Race), - new float2(Game.chrome.lastMousePos.X + 30, Game.chrome.lastMousePos.Y + 50)); + new float2(Widget.lastMousePos.X + 30, Widget.lastMousePos.Y + 50)); } renderer.RgbaSpriteRenderer.Flush();