Add Statistics options hotkeys

This commit is contained in:
rob-v
2017-09-02 08:14:20 +02:00
committed by reaperrr
parent b86d632c8c
commit c9b4568117
11 changed files with 170 additions and 93 deletions

View File

@@ -10,13 +10,15 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Lint;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
[ChromeLogicArgsHotkeys("StatisticsBasicKey", "StatisticsEconomyKey", "StatisticsProductionKey", "StatisticsCombatKey", "StatisticsGraphKey")]
public class MenuButtonsChromeLogic : ChromeLogic
{
readonly World world;
@@ -26,13 +28,20 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Widget currentWidget;
[ObjectCreator.UseCtor]
public MenuButtonsChromeLogic(Widget widget, World world)
public MenuButtonsChromeLogic(Widget widget, World world, Dictionary<string, MiniYaml> logicArgs)
{
this.world = world;
worldRoot = Ui.Root.Get("WORLD_ROOT");
menuRoot = Ui.Root.Get("MENU_ROOT");
MiniYaml yaml;
var ks = Game.Settings.Keys;
string[] keyNames = Enum.GetNames(typeof(ObserverStatsPanel));
var statsHotkeys = new NamedHotkey[keyNames.Length];
for (var i = 0; i < keyNames.Length; i++)
statsHotkeys[i] = logicArgs.TryGetValue("Statistics" + keyNames[i] + "Key", out yaml) ? new NamedHotkey(yaml.Value, ks) : new NamedHotkey();
// System buttons
var options = widget.GetOrNull<MenuButtonWidget>("OPTIONS_BUTTON");
if (options != null)
@@ -85,7 +94,29 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (stats != null)
{
stats.IsDisabled = () => disableSystemButtons || world.Map.Visibility.HasFlag(MapVisibility.MissionSelector);
stats.OnClick = () => OpenMenuPanel(stats);
stats.OnClick = () => OpenMenuPanel(stats, new WidgetArgs() { { "activePanel", ObserverStatsPanel.Basic } });
}
var keyListener = widget.GetOrNull<LogicKeyListenerWidget>("OBSERVER_KEY_LISTENER");
if (keyListener != null)
{
keyListener.AddHandler(e =>
{
if (e.Event == KeyInputEvent.Down && !e.IsRepeat)
{
var key = Hotkey.FromKeyInput(e);
for (var i = 0; i < statsHotkeys.Length; i++)
{
if (key == statsHotkeys[i].GetValue())
{
OpenMenuPanel(stats, new WidgetArgs() { { "activePanel", i } });
return true;
}
}
}
return false;
});
}
}

View File

@@ -14,6 +14,7 @@ using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Lint;
using OpenRA.Mods.Common.Traits;
using OpenRA.Network;
using OpenRA.Traits;
@@ -21,30 +22,40 @@ using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public enum ObserverStatsPanel { Basic, Economy, Production, Combat, Graph }
[ChromeLogicArgsHotkeys("StatisticsBasicKey", "StatisticsEconomyKey", "StatisticsProductionKey", "StatisticsCombatKey", "StatisticsGraphKey")]
public class ObserverStatsLogic : ChromeLogic
{
ContainerWidget basicStatsHeaders;
ContainerWidget economyStatsHeaders;
ContainerWidget productionStatsHeaders;
ContainerWidget combatStatsHeaders;
ContainerWidget earnedThisMinuteGraphHeaders;
ScrollPanelWidget playerStatsPanel;
ScrollItemWidget basicPlayerTemplate;
ScrollItemWidget economyPlayerTemplate;
ScrollItemWidget productionPlayerTemplate;
ScrollItemWidget combatPlayerTemplate;
ContainerWidget earnedThisMinuteGraphTemplate;
ScrollItemWidget teamTemplate;
DropDownButtonWidget statsDropDown;
IEnumerable<Player> players;
World world;
WorldRenderer worldRenderer;
readonly ContainerWidget basicStatsHeaders;
readonly ContainerWidget economyStatsHeaders;
readonly ContainerWidget productionStatsHeaders;
readonly ContainerWidget combatStatsHeaders;
readonly ContainerWidget earnedThisMinuteGraphHeaders;
readonly ScrollPanelWidget playerStatsPanel;
readonly ScrollItemWidget basicPlayerTemplate;
readonly ScrollItemWidget economyPlayerTemplate;
readonly ScrollItemWidget productionPlayerTemplate;
readonly ScrollItemWidget combatPlayerTemplate;
readonly ContainerWidget earnedThisMinuteGraphTemplate;
readonly ScrollItemWidget teamTemplate;
readonly IEnumerable<Player> players;
readonly World world;
readonly WorldRenderer worldRenderer;
[ObjectCreator.UseCtor]
public ObserverStatsLogic(World world, WorldRenderer worldRenderer, Widget widget, Action onExit)
public ObserverStatsLogic(World world, WorldRenderer worldRenderer, Widget widget, Action onExit, ObserverStatsPanel activePanel, Dictionary<string, MiniYaml> logicArgs)
{
this.world = world;
this.worldRenderer = worldRenderer;
MiniYaml yaml;
var ks = Game.Settings.Keys;
string[] keyNames = Enum.GetNames(typeof(ObserverStatsPanel));
var statsHotkeys = new NamedHotkey[keyNames.Length];
for (var i = 0; i < keyNames.Length; i++)
statsHotkeys[i] = logicArgs.TryGetValue("Statistics" + keyNames[i] + "Key", out yaml) ? new NamedHotkey(yaml.Value, ks) : new NamedHotkey();
players = world.Players.Where(p => !p.NonCombatant);
basicStatsHeaders = widget.Get<ContainerWidget>("BASIC_STATS_HEADERS");
@@ -64,79 +75,40 @@ namespace OpenRA.Mods.Common.Widgets.Logic
teamTemplate = playerStatsPanel.Get<ScrollItemWidget>("TEAM_TEMPLATE");
statsDropDown = widget.Get<DropDownButtonWidget>("STATS_DROPDOWN");
statsDropDown.GetText = () => "Basic";
statsDropDown.OnMouseDown = _ =>
var statsDropDown = widget.Get<DropDownButtonWidget>("STATS_DROPDOWN");
Func<string, ContainerWidget, Action, StatsDropDownOption> createStatsOption = (title, headers, a) =>
{
var options = new List<StatsDropDownOption>
return new StatsDropDownOption
{
new StatsDropDownOption
Title = title,
IsSelected = () => headers.Visible,
OnClick = () =>
{
Title = "Basic",
IsSelected = () => basicStatsHeaders.Visible,
OnClick = () =>
{
ClearStats();
statsDropDown.GetText = () => "Basic";
DisplayStats(BasicStats);
}
},
new StatsDropDownOption
{
Title = "Economy",
IsSelected = () => economyStatsHeaders.Visible,
OnClick = () =>
{
ClearStats();
statsDropDown.GetText = () => "Economy";
DisplayStats(EconomyStats);
}
},
new StatsDropDownOption
{
Title = "Production",
IsSelected = () => productionStatsHeaders.Visible,
OnClick = () =>
{
ClearStats();
statsDropDown.GetText = () => "Production";
DisplayStats(ProductionStats);
}
},
new StatsDropDownOption
{
Title = "Combat",
IsSelected = () => combatStatsHeaders.Visible,
OnClick = () =>
{
ClearStats();
statsDropDown.GetText = () => "Combat";
DisplayStats(CombatStats);
}
},
new StatsDropDownOption
{
Title = "Earnings (graph)",
IsSelected = () => earnedThisMinuteGraphHeaders.Visible,
OnClick = () =>
{
ClearStats();
statsDropDown.GetText = () => "Earnings (graph)";
EarnedThisMinuteGraph();
}
ClearStats();
statsDropDown.GetText = () => title;
a();
}
};
Func<StatsDropDownOption, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
{
var item = ScrollItemWidget.Setup(template, option.IsSelected, option.OnClick);
item.Get<LabelWidget>("LABEL").GetText = () => option.Title;
return item;
};
statsDropDown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, options, setupItem);
};
ClearStats();
DisplayStats(BasicStats);
var statsDropDownOptions = new StatsDropDownOption[]
{
createStatsOption("Basic", basicStatsHeaders, () => DisplayStats(BasicStats)),
createStatsOption("Economy", economyStatsHeaders, () => DisplayStats(EconomyStats)),
createStatsOption("Production", productionStatsHeaders, () => DisplayStats(ProductionStats)),
createStatsOption("Combat", combatStatsHeaders, () => DisplayStats(CombatStats)),
createStatsOption("Earnings (graph)", earnedThisMinuteGraphHeaders, () => EarnedThisMinuteGraph())
};
Func<StatsDropDownOption, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
{
var item = ScrollItemWidget.Setup(template, option.IsSelected, option.OnClick);
item.Get<LabelWidget>("LABEL").GetText = () => option.Title;
return item;
};
statsDropDown.OnMouseDown = _ => statsDropDown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, statsDropDownOptions, setupItem);
statsDropDownOptions[(int)activePanel].OnClick();
var close = widget.GetOrNull<ButtonWidget>("CLOSE");
if (close != null)
@@ -146,6 +118,25 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Ui.Root.RemoveChild(widget);
onExit();
};
var keyListener = statsDropDown.Get<LogicKeyListenerWidget>("STATS_DROPDOWN_KEYHANDLER");
keyListener.AddHandler(e =>
{
if (e.Event == KeyInputEvent.Down && !e.IsRepeat)
{
var key = Hotkey.FromKeyInput(e);
for (var i = 0; i < statsHotkeys.Length; i++)
{
if (key == statsHotkeys[i].GetValue())
{
statsDropDownOptions[i].OnClick();
return true;
}
}
}
return false;
});
}
void ClearStats()

View File

@@ -502,7 +502,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ "ReplaySpeedSlowKey", "Slow speed" },
{ "ReplaySpeedRegularKey", "Regular speed" },
{ "ReplaySpeedFastKey", "Fast speed" },
{ "ReplaySpeedMaxKey", "Maximum speed" }
{ "ReplaySpeedMaxKey", "Maximum speed" },
{ "StatisticsBasicKey", "Basic statistics" },
{ "StatisticsEconomyKey", "Economy statistics" },
{ "StatisticsProductionKey", "Production statistics" },
{ "StatisticsCombatKey", "Combat statistics" },
{ "StatisticsGraphKey", "Statistics graph" }
};
var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing);