Merge pull request #3008 from pchote/split-tabs-logic

Split multiple-production-queue widget logic into its own file
This commit is contained in:
Matthias Mailänder
2013-04-08 08:06:34 -07:00
14 changed files with 180 additions and 154 deletions

View File

@@ -11,6 +11,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics;
namespace OpenRA.Widgets
@@ -36,6 +37,11 @@ namespace OpenRA.Widgets
public Action<MouseInput> OnMouseDown = _ => {};
public Action<MouseInput> OnMouseUp = _ => {};
public readonly string TooltipTemplate = "BUTTON_TOOLTIP";
public readonly string TooltipText;
public readonly string TooltipContainer;
Lazy<TooltipContainerWidget> tooltipContainer;
// Equivalent to OnMouseUp, but without an input arg
public Action OnClick = () => {};
public Action OnDoubleClick = () => {};
@@ -49,24 +55,32 @@ namespace OpenRA.Widgets
OnKeyPress = _ => OnClick();
IsDisabled = () => Disabled;
IsHighlighted = () => Highlighted;
tooltipContainer = Lazy.New(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
}
protected ButtonWidget(ButtonWidget widget)
: base(widget)
protected ButtonWidget(ButtonWidget other)
: base(other)
{
Text = widget.Text;
Font = widget.Font;
Depressed = widget.Depressed;
VisualHeight = widget.VisualHeight;
GetText = widget.GetText;
OnMouseDown = widget.OnMouseDown;
Disabled = widget.Disabled;
IsDisabled = widget.IsDisabled;
Highlighted = widget.Highlighted;
IsHighlighted = widget.IsHighlighted;
Text = other.Text;
Font = other.Font;
Depressed = other.Depressed;
VisualHeight = other.VisualHeight;
GetText = other.GetText;
OnMouseDown = other.OnMouseDown;
Disabled = other.Disabled;
IsDisabled = other.IsDisabled;
Highlighted = other.Highlighted;
IsHighlighted = other.IsHighlighted;
OnMouseUp = mi => OnClick();
OnKeyPress = _ => OnClick();
TooltipTemplate = other.TooltipTemplate;
TooltipText = other.TooltipText;
TooltipContainer = other.TooltipContainer;
tooltipContainer = Lazy.New(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
}
public override bool LoseFocus(MouseInput mi)
@@ -137,6 +151,19 @@ namespace OpenRA.Widgets
return Depressed;
}
public override void MouseEntered()
{
if (TooltipContainer == null) return;
tooltipContainer.Value.SetTooltip(TooltipTemplate,
new WidgetArgs() {{ "button", this }});
}
public override void MouseExited()
{
if (TooltipContainer == null) return;
tooltipContainer.Value.RemoveTooltip();
}
public override int2 ChildOrigin { get { return RenderOrigin +
((Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0)); } }
@@ -166,14 +193,13 @@ namespace OpenRA.Widgets
public static void DrawBackground(string baseName, Rectangle rect, bool disabled, bool pressed, bool hover, bool highlighted)
{
var variant = highlighted ? "-highlighted" : "";
var state = disabled ? "-disabled" :
pressed ? "-pressed" :
hover ? "-hover" :
"";
if (highlighted)
state += "-highlighted";
WidgetUtils.DrawPanel(baseName + state, rect);
WidgetUtils.DrawPanel(baseName + variant + state, rect);
}
}
}

View File

@@ -110,11 +110,12 @@
<Compile Include="Widgets\ProductionPaletteWidget.cs" />
<Compile Include="Widgets\ProductionTabsWidget.cs" />
<Compile Include="Widgets\SupportPowersWidget.cs" />
<Compile Include="Widgets\ToggleButtonWidget.cs" />
<Compile Include="WithFire.cs" />
<Compile Include="WithRoof.cs" />
<Compile Include="Widgets\ResourceBarWidget.cs" />
<Compile Include="Widgets\Logic\SpawnSelectorTooltipLogic.cs" />
<Compile Include="Widgets\Logic\ProductionTabsLogic.cs" />
<Compile Include="Widgets\ProductionTypeButtonWidget.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -15,7 +15,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
public class ButtonTooltipLogic
{
[ObjectCreator.UseCtor]
public ButtonTooltipLogic(Widget widget, ToggleButtonWidget button)
public ButtonTooltipLogic(Widget widget, ButtonWidget button)
{
var label = widget.Get<LabelWidget>("LABEL");
var hotkey = widget.Get<LabelWidget>("HOTKEY");

View File

@@ -19,11 +19,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncIngameChromeLogic
{
enum MenuType { None, Cheats }
MenuType menu = MenuType.None;
Widget ingameRoot;
ProductionTabsWidget queueTabs;
World world;
void AddChatLine(Color c, string from, string text)
@@ -35,32 +31,6 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
{
Game.AddChatLine -= AddChatLine;
Game.BeforeGameStart -= UnregisterEvents;
if (queueTabs != null)
{
world.ActorAdded += queueTabs.ActorChanged;
world.ActorRemoved += queueTabs.ActorChanged;
}
}
void SetupProductionGroupButton(ToggleButtonWidget button, string group)
{
Action<bool> selectTab = reverse =>
{
if (queueTabs.QueueGroup == group)
queueTabs.SelectNextTab(reverse);
else
queueTabs.QueueGroup = group;
};
button.IsDisabled = () => queueTabs.Groups[group].Tabs.Count == 0;
button.OnMouseUp = mi => selectTab(mi.Modifiers.HasModifier(Modifiers.Shift));
button.OnKeyPress = e => selectTab(e.Modifiers.HasModifier(Modifiers.Shift));
button.IsToggled = () => queueTabs.QueueGroup == group;
var chromeName = group.ToLowerInvariant();
var icon = button.Get<ImageWidget>("ICON");
icon.GetImageName = () => button.IsDisabled() ? chromeName+"-disabled" :
queueTabs.Groups[group].Alert ? chromeName+"-alert" : chromeName;
}
[ObjectCreator.UseCtor]
@@ -87,12 +57,6 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
{
var cachedPause = world.Paused;
if (menu != MenuType.None)
{
Ui.CloseWindow();
menu = MenuType.None;
}
ingameRoot.IsVisible = () => false;
if (world.LobbyInfo.IsSinglePlayer)
world.IssueOrder(Order.PauseGame(true));
@@ -126,25 +90,14 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
BindOrderButton<SellOrderGenerator>(world, sidebarRoot, "SELL_BUTTON", "sell");
BindOrderButton<RepairOrderGenerator>(world, sidebarRoot, "REPAIR_BUTTON", "repair");
sidebarRoot.Get<ToggleButtonWidget>("SELL_BUTTON").Key = Game.Settings.Keys.SellKey;
sidebarRoot.Get<ToggleButtonWidget>("REPAIR_BUTTON").Key = Game.Settings.Keys.RepairKey;
sidebarRoot.Get<ButtonWidget>("SELL_BUTTON").Key = Game.Settings.Keys.SellKey;
sidebarRoot.Get<ButtonWidget>("REPAIR_BUTTON").Key = Game.Settings.Keys.RepairKey;
var powerManager = world.LocalPlayer.PlayerActor.Trait<PowerManager>();
var playerResources = world.LocalPlayer.PlayerActor.Trait<PlayerResources>();
sidebarRoot.Get<LabelWidget>("CASH").GetText = () =>
"${0}".F(playerResources.DisplayCash + playerResources.DisplayOre);
queueTabs = playerWidgets.Get<ProductionTabsWidget>("PRODUCTION_TABS");
world.ActorAdded += queueTabs.ActorChanged;
world.ActorRemoved += queueTabs.ActorChanged;
var queueTypes = sidebarRoot.Get("PRODUCTION_TYPES");
SetupProductionGroupButton(queueTypes.Get<ToggleButtonWidget>("BUILDING"), "Building");
SetupProductionGroupButton(queueTypes.Get<ToggleButtonWidget>("DEFENSE"), "Defense");
SetupProductionGroupButton(queueTypes.Get<ToggleButtonWidget>("INFANTRY"), "Infantry");
SetupProductionGroupButton(queueTypes.Get<ToggleButtonWidget>("VEHICLE"), "Vehicle");
SetupProductionGroupButton(queueTypes.Get<ToggleButtonWidget>("AIRCRAFT"), "Aircraft");
playerWidgets.Get<ButtonWidget>("OPTIONS_BUTTON").OnClick = OptionsClicked;
var winLossWatcher = playerWidgets.Get<LogicTickerWidget>("WIN_LOSS_WATCHER");
@@ -186,9 +139,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
static void BindOrderButton<T>(World world, Widget parent, string button, string icon)
where T : IOrderGenerator, new()
{
var w = parent.Get<ToggleButtonWidget>(button);
var w = parent.Get<ButtonWidget>(button);
w.OnClick = () => world.ToggleInputMode<T>();
w.IsToggled = () => world.OrderGenerator is T;
w.IsHighlighted = () => world.OrderGenerator is T;
w.Get<ImageWidget>("ICON").GetImageName =
() => world.OrderGenerator is T ? icon+"-active" : icon;

View File

@@ -98,7 +98,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
// Debug / Cheats panel
var debugButton = panelParent.Get<ButtonWidget>("DEBUG_BUTTON");
debugButton.OnClick = () => Panel = PanelType.Debug;
debugButton.IsDisabled = () => Panel == PanelType.Debug;
debugButton.IsHighlighted = () => Panel == PanelType.Debug;
if (world.LocalPlayer != null && world.LobbyInfo.GlobalSettings.AllowCheats)
{
@@ -113,7 +113,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var iop = world.WorldActor.TraitsImplementing<IObjectivesPanel>().FirstOrDefault();
var objectivesButton = panelParent.Get<ButtonWidget>("OBJECTIVES_BUTTON");
objectivesButton.OnClick = () => Panel = PanelType.Objectives;
objectivesButton.IsDisabled = () => Panel == PanelType.Objectives;
objectivesButton.IsHighlighted = () => Panel == PanelType.Objectives;
if (iop != null && iop.ObjectivesPanel != null)
{

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
// General pane
var generalButton = panel.Get<ButtonWidget>("GENERAL_BUTTON");
generalButton.OnClick = () => Settings = PanelType.General;
generalButton.IsDisabled = () => Settings == PanelType.General;
generalButton.IsHighlighted = () => Settings == PanelType.General;
var generalPane = panel.Get("GENERAL_CONTROLS");
generalPane.IsVisible = () => Settings == PanelType.General;
@@ -118,7 +118,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
var inputButton = panel.Get<ButtonWidget>("INPUT_BUTTON");
inputButton.OnClick = () => Settings = PanelType.Input;
inputButton.IsDisabled = () => Settings == PanelType.Input;
inputButton.IsHighlighted = () => Settings == PanelType.Input;
var classicMouseCheckbox = inputPane.Get<CheckboxWidget>("CLASSICORDERS_CHECKBOX");
classicMouseCheckbox.IsChecked = () => gameSettings.UseClassicMouseStyle;

View File

@@ -0,0 +1,70 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using OpenRA.Mods.RA.Orders;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class ProductionTabsLogic
{
ProductionTabsWidget tabs;
World world;
void SetupProductionGroupButton(ProductionTypeButtonWidget button)
{
if (button == null)
return;
Action<bool> selectTab = reverse =>
{
if (tabs.QueueGroup == button.ProductionGroup)
tabs.SelectNextTab(reverse);
else
tabs.QueueGroup = button.ProductionGroup;
};
button.IsDisabled = () => tabs.Groups[button.ProductionGroup].Tabs.Count == 0;
button.OnMouseUp = mi => selectTab(mi.Modifiers.HasModifier(Modifiers.Shift));
button.OnKeyPress = e => selectTab(e.Modifiers.HasModifier(Modifiers.Shift));
button.IsHighlighted = () => tabs.QueueGroup == button.ProductionGroup;
var chromeName = button.ProductionGroup.ToLowerInvariant();
var icon = button.Get<ImageWidget>("ICON");
icon.GetImageName = () => button.IsDisabled() ? chromeName+"-disabled" :
tabs.Groups[button.ProductionGroup].Alert ? chromeName+"-alert" : chromeName;
}
[ObjectCreator.UseCtor]
public ProductionTabsLogic(Widget widget, World world)
{
this.world = world;
tabs = widget.Get<ProductionTabsWidget>("PRODUCTION_TABS");
world.ActorAdded += tabs.ActorChanged;
world.ActorRemoved += tabs.ActorChanged;
Game.BeforeGameStart += UnregisterEvents;
var typesContainer = Ui.Root.Get(tabs.TypesContainer);
foreach (var i in typesContainer.Children)
SetupProductionGroupButton(i as ProductionTypeButtonWidget);
}
void UnregisterEvents()
{
Game.BeforeGameStart -= UnregisterEvents;
world.ActorAdded -= tabs.ActorChanged;
world.ActorRemoved -= tabs.ActorChanged;
}
}
}

View File

@@ -61,6 +61,8 @@ namespace OpenRA.Mods.Cnc.Widgets
class ProductionTabsWidget : Widget
{
public readonly string PaletteWidget = null;
public readonly string TypesContainer = null;
public readonly float ScrollVelocity = 4f;
public readonly int TabWidth = 30;
public readonly int ArrowWidth = 20;
@@ -156,7 +158,7 @@ namespace OpenRA.Mods.Cnc.Widgets
{
var rect = new Rectangle(origin.X + ContentWidth, origin.Y, TabWidth, rb.Height);
var hover = !leftHover && !rightHover && Ui.MouseOverWidget == this && rect.Contains(Viewport.LastMousePos);
var baseName = tab.Queue == CurrentQueue ? "button-toggled" : "button";
var baseName = tab.Queue == CurrentQueue ? "button-highlighted" : "button";
ButtonWidget.DrawBackground(baseName, rect, false, false, hover, false);
ContentWidth += TabWidth - 1;

View File

@@ -0,0 +1,29 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets
{
public class ProductionTypeButtonWidget : ButtonWidget
{
public readonly string ProductionGroup;
public ProductionTypeButtonWidget() : base() {}
protected ProductionTypeButtonWidget(ProductionTypeButtonWidget other)
: base(other)
{
ProductionGroup = other.ProductionGroup;
}
}
}

View File

@@ -1,62 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets
{
public class ToggleButtonWidget : ButtonWidget
{
public readonly string TooltipTemplate = "BUTTON_TOOLTIP";
public readonly string TooltipText;
public readonly string TooltipContainer;
public Func<bool> IsToggled = () => false;
Lazy<TooltipContainerWidget> tooltipContainer;
public ToggleButtonWidget()
: base()
{
tooltipContainer = Lazy.New(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
}
protected ToggleButtonWidget(ToggleButtonWidget other)
: base(other)
{
TooltipTemplate = other.TooltipTemplate;
TooltipText = other.TooltipText;
TooltipContainer = other.TooltipContainer;
tooltipContainer = Lazy.New(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
}
public override void MouseEntered()
{
if (TooltipContainer == null) return;
tooltipContainer.Value.SetTooltip(TooltipTemplate,
new WidgetArgs() {{ "button", this }});
}
public override void MouseExited()
{
if (TooltipContainer == null) return;
tooltipContainer.Value.RemoveTooltip();
}
public override void DrawBackground(Rectangle rect, bool disabled, bool pressed, bool hover, bool highlighted)
{
var baseName = IsToggled() ? "button-toggled" : "button";
ButtonWidget.DrawBackground(baseName, rect, disabled, pressed, hover, highlighted);
}
}
}

View File

@@ -272,7 +272,7 @@ button-hover: dialog.png
corner-bl: 512,82,1,1
corner-br: 594,82,1,1
button-hover-highlighted: dialog.png
button-highlighted-hover: dialog.png
background: 513,145,126,126
border-r: 639,145,1,126
border-l: 512,145,1,126
@@ -295,7 +295,7 @@ button-disabled: dialog.png
corner-bl: 512,82,1,1
corner-br: 594,82,1,1
button-disabled-highlighted: dialog.png
button-highlighted-disabled: dialog.png
background: 513,145,126,126
border-r: 639,145,1,126
border-l: 512,145,1,126
@@ -318,7 +318,7 @@ button-pressed: dialog.png
corner-bl: 640,82,1,1
corner-br: 722,82,1,1
button-pressed-highlighted: dialog.png
button-highlighted-pressed: dialog.png
background: 641,145,126,126
border-r: 767,145,1,126
border-l: 640,145,1,126

View File

@@ -54,7 +54,7 @@ button-pressed: chrome.png
corner-bl: 64,254,2,2
corner-br: 126,254,2,2
button-toggled: chrome.png
button-highlighted: chrome.png
background: 257,129,30,30
border-r: 287,129,1,30
border-l: 256,129,1,30
@@ -65,7 +65,7 @@ button-toggled: chrome.png
corner-bl: 256,159,1,1
corner-br: 287,159,1,1
button-toggled-hover: chrome.png
button-highlighted-hover: chrome.png
background: 289,129,30,30
border-r: 319,129,1,30
border-l: 288,129,1,30
@@ -76,7 +76,7 @@ button-toggled-hover: chrome.png
corner-bl: 288,159,1,1
corner-br: 319,159,1,1
button-toggled-pressed: chrome.png
button-highlighted-pressed: chrome.png
background: 257,161,30,30
border-r: 287,161,1,30
border-l: 256,161,1,30
@@ -87,7 +87,7 @@ button-toggled-pressed: chrome.png
corner-bl: 256,191,1,1
corner-br: 287,191,1,1
button-toggled-disabled: chrome.png
button-highlighted-disabled: chrome.png
background: 289,161,30,30
border-r: 319,161,1,30
border-l: 288,161,1,30

View File

@@ -44,7 +44,7 @@ Container@INGAME_ROOT:
TooltipContainer@TOOLTIP_CONTAINER:
Container@OBSERVER_WIDGETS:
Children:
ToggleButton@OPTIONS_BUTTON:
Button@OPTIONS_BUTTON:
Key:escape
X:WINDOW_RIGHT-202
Y:5
@@ -98,7 +98,7 @@ Container@PLAYER_WIDGETS:
Height:240
Background:panel-black
Children:
ToggleButton@OPTIONS_BUTTON:
Button@OPTIONS_BUTTON:
Key:escape
X:42
Y:0-24
@@ -113,7 +113,7 @@ Container@PLAYER_WIDGETS:
Y:5
ImageCollection:order-icons
ImageName:options
ToggleButton@SELL_BUTTON:
Button@SELL_BUTTON:
X:82
Y:0-24
Width:30
@@ -126,7 +126,7 @@ Container@PLAYER_WIDGETS:
X:7
Y:5
ImageCollection:order-icons
ToggleButton@REPAIR_BUTTON:
Button@REPAIR_BUTTON:
X:122
Y:0-24
Width:30
@@ -190,67 +190,74 @@ Container@PLAYER_WIDGETS:
Width:170
Height:30
Children:
ToggleButton@BUILDING:
ProductionTypeButton@BUILDING:
Width:30
Height:30
Key: q
TooltipText: Buildings
TooltipContainer:TOOLTIP_CONTAINER
ProductionGroup:Building
Children:
Image@ICON:
X:7
Y:7
ImageCollection:production-icons
ToggleButton@DEFENSE:
ProductionTypeButton@DEFENSE:
X:35
Width:30
Height:30
Key: w
TooltipText: Defense
TooltipContainer:TOOLTIP_CONTAINER
ProductionGroup:Defense
Children:
Image@ICON:
X:7
Y:7
ImageCollection:production-icons
ToggleButton@INFANTRY:
ProductionTypeButton@INFANTRY:
X:70
Width:30
Height:30
Key: e
TooltipText: Infantry
TooltipContainer:TOOLTIP_CONTAINER
ProductionGroup:Infantry
Children:
Image@ICON:
X:7
Y:7
ImageCollection:production-icons
ToggleButton@VEHICLE:
ProductionTypeButton@VEHICLE:
X:105
Width:30
Height:30
Key: r
TooltipText: Vehicles
TooltipContainer:TOOLTIP_CONTAINER
ProductionGroup:Vehicle
Children:
Image@ICON:
X:7
Y:7
ImageCollection:production-icons
ToggleButton@AIRCRAFT:
ProductionTypeButton@AIRCRAFT:
X:140
Width:30
Height:30
Key: t
TooltipText: Aircraft
TooltipContainer:TOOLTIP_CONTAINER
ProductionGroup:Aircraft
Children:
Image@ICON:
X:7
Y:7
ImageCollection:production-icons
ProductionTabs@PRODUCTION_TABS:
Logic:ProductionTabsLogic
PaletteWidget:PRODUCTION_PALETTE
TypesContainer:PRODUCTION_TYPES
X:WINDOW_RIGHT - 204
Y:268
Width:194

View File

@@ -272,7 +272,7 @@ button-hover: dialog.png
corner-bl: 512,82,1,1
corner-br: 594,82,1,1
button-hover-highlighted: dialog.png
button-highlighted-hover: dialog.png
background: 513,145,126,126
border-r: 639,145,1,126
border-l: 512,145,1,126
@@ -295,7 +295,7 @@ button-disabled: dialog.png
corner-bl: 512,82,1,1
corner-br: 594,82,1,1
button-disabled-highlighted: dialog.png
button-highlighted-disabled: dialog.png
background: 513,145,126,126
border-r: 639,145,1,126
border-l: 512,145,1,126
@@ -318,7 +318,7 @@ button-pressed: dialog.png
corner-bl: 640,82,1,1
corner-br: 722,82,1,1
button-pressed-highlighted: dialog.png
button-highlighted-pressed: dialog.png
background: 641,145,126,126
border-r: 767,145,1,126
border-l: 640,145,1,126