From 74c390d1d0e4f85c0939e2d11e85e117953bb462 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 5 Nov 2017 21:11:14 +0000 Subject: [PATCH] Migrate input settings to HotkeyManager. --- .../Widgets/Logic/SettingsLogic.cs | 234 +++--------------- mods/cnc/chrome/settings.yaml | 105 ++++---- mods/common/chrome/settings.yaml | 121 ++++----- 3 files changed, 147 insertions(+), 313 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs index bff52773ee..c1c6542281 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs @@ -34,6 +34,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic readonly ModData modData; readonly WorldRenderer worldRenderer; + readonly Dictionary logicArgs; SoundDevice soundDevice; PanelType settingsPanel = PanelType.Display; @@ -49,10 +50,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic } [ObjectCreator.UseCtor] - public SettingsLogic(Widget widget, Action onExit, ModData modData, WorldRenderer worldRenderer) + public SettingsLogic(Widget widget, Action onExit, ModData modData, WorldRenderer worldRenderer, Dictionary logicArgs) { this.worldRenderer = worldRenderer; this.modData = modData; + this.logicArgs = logicArgs; panelContainer = widget.Get("SETTINGS_PANEL"); tabContainer = widget.Get("TAB_CONTAINER"); @@ -122,21 +124,17 @@ namespace OpenRA.Mods.Common.Widgets.Logic ss.OnChange += x => field.SetValue(group, x); } - static void BindHotkeyPref(KeyValuePair kv, KeySettings ks, Widget template, Widget parent) + static void BindHotkeyPref(HotkeyDefinition hd, HotkeyManager manager, Widget template, Widget parent) { var key = template.Clone() as Widget; - key.Id = kv.Key; + key.Id = hd.Name; key.IsVisible = () => true; - var field = ks.GetType().GetField(kv.Key); - if (field == null) - throw new InvalidOperationException("Game.Settings.Keys does not contain {1}".F(kv.Key)); - - key.Get("FUNCTION").GetText = () => kv.Value + ":"; + key.Get("FUNCTION").GetText = () => hd.Description + ":"; var textBox = key.Get("HOTKEY"); - textBox.Key = (Hotkey)field.GetValue(ks); - textBox.OnLoseFocus = () => field.SetValue(ks, textBox.Key); + textBox.Key = manager[hd.Name].GetValue(); + textBox.OnLoseFocus = () => manager.Set(hd.Name, textBox.Key); parent.AddChild(key); } @@ -379,7 +377,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic Action InitInputPanel(Widget panel) { var gs = Game.Settings.Game; - var ks = Game.Settings.Keys; BindCheckboxPref(panel, "CLASSICORDERS_CHECKBOX", gs, "UseClassicMouseStyle"); BindCheckboxPref(panel, "EDGESCROLL_CHECKBOX", gs, "ViewportEdgeScroll"); @@ -415,202 +412,34 @@ namespace OpenRA.Mods.Common.Widgets.Logic var hotkeyList = panel.Get("HOTKEY_LIST"); hotkeyList.Layout = new GridLayout(hotkeyList); var hotkeyHeader = hotkeyList.Get("HEADER"); - var globalTemplate = hotkeyList.Get("GLOBAL_TEMPLATE"); - var unitTemplate = hotkeyList.Get("UNIT_TEMPLATE"); - var productionTemplate = hotkeyList.Get("PRODUCTION_TEMPLATE"); - var developerTemplate = hotkeyList.Get("DEVELOPER_TEMPLATE"); + var templates = hotkeyList.Get("TEMPLATES"); hotkeyList.RemoveChildren(); Func returnTrue = () => true; Action doNothing = () => { }; - // Game + MiniYaml hotkeyGroups; + if (logicArgs.TryGetValue("HotkeyGroups", out hotkeyGroups)) { - var hotkeys = new Dictionary() + foreach (var hg in hotkeyGroups.Nodes) { - { "CycleBaseKey", "Jump to base" }, - { "ToLastEventKey", "Jump to last radar event" }, - { "ToSelectionKey", "Jump to selection" }, - { "SelectAllUnitsKey", "Select all combat units" }, - { "SelectUnitsByTypeKey", "Select units by type" }, + var templateNode = hg.Value.Nodes.FirstOrDefault(n => n.Key == "Template"); + var typesNode = hg.Value.Nodes.FirstOrDefault(n => n.Key == "Types"); + if (templateNode == null || typesNode == null) + continue; - { "PlaceBeaconKey", "Place beacon" }, + var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); + header.Get("LABEL").GetText = () => hg.Key; + hotkeyList.AddChild(header); - { "PauseKey", "Pause / Unpause" }, - { "SellKey", "Sell mode" }, - { "PowerDownKey", "Power-down mode" }, - { "RepairKey", "Repair mode" }, - - { "NextProductionTabKey", "Next production tab" }, - { "PreviousProductionTabKey", "Previous production tab" }, - { "CycleProductionBuildingsKey", "Cycle production facilities" }, - - { "CycleStatusBarsKey", "Cycle status bars display" }, - { "TogglePixelDoubleKey", "Toggle pixel doubling" }, - { "ToggleMuteKey", "Toggle audio mute" }, - { "TogglePlayerStanceColorKey", "Toggle player stance colors" }, - { "TakeScreenshotKey", "Take screenshot" } - }; - - var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); - header.Get("LABEL").GetText = () => "Game Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, globalTemplate, hotkeyList); - } - - // Viewport - { - var hotkeys = new Dictionary() - { - { "MapScrollUpKey", "Scroll up" }, - { "MapScrollDownKey", "Scroll down" }, - { "MapScrollLeftKey", "Scroll left" }, - { "MapScrollRightKey", "Scroll right" }, - - { "MapJumpToTopEdgeKey", "Jump to top edge" }, - { "MapJumpToBottomEdgeKey", "Jump to bottom edge" }, - { "MapJumpToLeftEdgeKey", "Jump to left edge" }, - { "MapJumpToRightEdgeKey", "Jump to right edge" }, - - { "MapBookmarkSave01Key", "Record bookmark #1" }, - { "MapBookmarkRestore01Key", "Jump to bookmark #1" }, - { "MapBookmarkSave02Key", "Record bookmark #2" }, - { "MapBookmarkRestore02Key", "Jump to bookmark #2" }, - { "MapBookmarkSave03Key", "Record bookmark #3" }, - { "MapBookmarkRestore03Key", "Jump to bookmark #3" }, - { "MapBookmarkSave04Key", "Record bookmark #4" }, - { "MapBookmarkRestore04Key", "Jump to bookmark #4" } - }; - - var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); - header.Get("LABEL").GetText = () => "Viewport Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, globalTemplate, hotkeyList); - } - - // Observer - { - var hotkeys = new Dictionary() - { - { "ObserverCombinedViewKey", "All Players" }, - { "ObserverWorldViewKey", "Disable Shroud" }, - { "PauseKey", "Pause/Play" }, - { "ReplaySpeedSlowKey", "Slow speed" }, - { "ReplaySpeedRegularKey", "Regular speed" }, - { "ReplaySpeedFastKey", "Fast 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); - header.Get("LABEL").GetText = () => "Observer Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, globalTemplate, hotkeyList); - } - - // Unit - { - var hotkeys = new Dictionary() - { - { "AttackMoveKey", "Attack Move" }, - { "StopKey", "Stop" }, - { "ScatterKey", "Scatter" }, - { "DeployKey", "Deploy" }, - { "GuardKey", "Guard" } - }; - - var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); - header.Get("LABEL").GetText = () => "Unit Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, unitTemplate, hotkeyList); - } - - // Unit stance - { - var hotkeys = new Dictionary() - { - { "StanceHoldFireKey", "Hold fire" }, - { "StanceReturnFireKey", "Return fire" }, - { "StanceDefendKey", "Defend" }, - { "StanceAttackAnythingKey", "Attack anything" } - }; - - var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); - header.Get("LABEL").GetText = () => "Unit Stance Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, globalTemplate, hotkeyList); - } - - // Production - { - var hotkeys = new Dictionary() - { - { "ProductionTypeBuildingKey", "Building Tab" }, - { "ProductionTypeDefenseKey", "Defense Tab" }, - { "ProductionTypeInfantryKey", "Infantry Tab" }, - { "ProductionTypeVehicleKey", "Vehicle Tab" }, - { "ProductionTypeAircraftKey", "Aircraft Tab" }, - { "ProductionTypeNavalKey", "Naval Tab" }, - { "ProductionTypeTankKey", "Tank Tab" }, - { "ProductionTypeMerchantKey", "Starport Tab" }, - { "ProductionTypeUpgradeKey", "Upgrade Tab" } - }; - - for (var i = 1; i <= 24; i++) - hotkeys.Add("Production{0:D2}Key".F(i), "Slot {0}".F(i)); - - var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); - header.Get("LABEL").GetText = () => "Production Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, productionTemplate, hotkeyList); - } - - // Support powers - { - var hotkeys = new Dictionary(); - for (var i = 1; i <= 6; i++) - hotkeys.Add("SupportPower{0:D2}Key".F(i), "Slot {0}".F(i)); - - var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); - header.Get("LABEL").GetText = () => "Support Power Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, productionTemplate, hotkeyList); - } - - // Music - { - var hotkeys = new Dictionary() - { - { "StopMusicKey", "Stop" }, - { "PauseMusicKey", "Pause or Resume" }, - { "PrevMusicKey", "Previous" }, - { "NextMusicKey", "Next" } - }; - - var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); - header.Get("LABEL").GetText = () => "Music Commands"; - hotkeyList.AddChild(header); - - foreach (var kv in hotkeys) - BindHotkeyPref(kv, ks, developerTemplate, hotkeyList); + var types = FieldLoader.GetValue("Types", typesNode.Value.Value); + var added = new HashSet(); + var template = templates.Get(templateNode.Value.Value); + foreach (var t in types) + foreach (var hd in modData.Hotkeys.Definitions.Where(k => k.Types.Contains(t))) + if (added.Add(hd)) + BindHotkeyPref(hd, modData.Hotkeys, template, hotkeyList); + } } return () => { }; @@ -619,9 +448,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic Action ResetInputPanel(Widget panel) { var gs = Game.Settings.Game; - var ks = Game.Settings.Keys; var dgs = new GameSettings(); - var dks = new KeySettings(); return () => { @@ -635,11 +462,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic gs.AllowZoom = dgs.AllowZoom; gs.ZoomModifier = dgs.ZoomModifier; - foreach (var f in ks.GetType().GetFields()) + foreach (var hd in modData.Hotkeys.Definitions) { - var value = (Hotkey)f.GetValue(dks); - f.SetValue(ks, value); - panel.Get(f.Name).Get("HOTKEY").Key = value; + modData.Hotkeys.Set(hd.Name, hd.Default); + panel.Get(hd.Name).Get("HOTKEY").Key = hd.Default; } panel.Get("SCROLLSPEED_SLIDER").Value = gs.ViewportEdgeScrollStep; diff --git a/mods/cnc/chrome/settings.yaml b/mods/cnc/chrome/settings.yaml index b834242a3b..c9f4e9fe32 100644 --- a/mods/cnc/chrome/settings.yaml +++ b/mods/cnc/chrome/settings.yaml @@ -1,5 +1,30 @@ Container@SETTINGS_PANEL: Logic: SettingsLogic + HotkeyGroups: + Game Commands: + Template: TWO_COLUMN + Types: World, Menu + Viewport Commands: + Template: TWO_COLUMN + Types: Viewport + Observer / Replay Commands: + Template: TWO_COLUMN + Types: Observer, Replay + Unit Commands: + Template: THREE_COLUMN + Types: Unit + Unit Stance Commands: + Template: TWO_COLUMN + Types: Stance + Production Commands: + Template: THREE_COLUMN + Types: Production + Support Power Commands: + Template: THREE_COLUMN + Types: SupportPower + Music Commands: + Template: TWO_COLUMN + Types: Music X: (WINDOW_RIGHT - WIDTH) / 2 Y: (WINDOW_BOTTOM - HEIGHT) / 2 Width: 590 @@ -449,62 +474,36 @@ Container@SETTINGS_PANEL: Width: PARENT_RIGHT Height: 10 Align: Center - Container@GLOBAL_TEMPLATE: - Width: 262 - Height: 25 - Visible: false + Container@TEMPLATES: Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 85 + Container@TWO_COLUMN: + Width: 262 Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH - Width: 80 - Height: 25 - Container@UNIT_TEMPLATE: - Width: 173 - Height: 25 - Visible: false - Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 84 - Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH + 1 - Width: 80 - Height: 25 - Container@PRODUCTION_TEMPLATE: - Width: 173 - Height: 25 - Visible: false - Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 84 - Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH + 1 - Width: 80 - Height: 25 - Container@DEVELOPER_TEMPLATE: - Width: 262 - Height: 25 - Visible: false - Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 115 - Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH - Width: 110 + Visible: false + Children: + Label@FUNCTION: + Y: 0 - 1 + Width: PARENT_RIGHT - 85 + Height: 25 + Align: Right + HotkeyEntry@HOTKEY: + X: PARENT_RIGHT - WIDTH + Width: 80 + Height: 25 + Container@THREE_COLUMN: + Width: 173 Height: 25 + Visible: false + Children: + Label@FUNCTION: + Y: 0 - 1 + Width: PARENT_RIGHT - 84 + Height: 25 + Align: Right + HotkeyEntry@HOTKEY: + X: PARENT_RIGHT - WIDTH + 1 + Width: 80 + Height: 25 Container@ADVANCED_PANEL: Width: PARENT_RIGHT Height: PARENT_BOTTOM diff --git a/mods/common/chrome/settings.yaml b/mods/common/chrome/settings.yaml index fe91668e25..c295381934 100644 --- a/mods/common/chrome/settings.yaml +++ b/mods/common/chrome/settings.yaml @@ -1,5 +1,30 @@ Background@SETTINGS_PANEL: Logic: SettingsLogic + HotkeyGroups: + Game Commands: + Template: TWO_COLUMN + Types: World, Menu + Viewport Commands: + Template: TWO_COLUMN + Types: Viewport + Observer / Replay Commands: + Template: TWO_COLUMN + Types: Observer, Replay + Unit Commands: + Template: THREE_COLUMN + Types: Unit + Unit Stance Commands: + Template: TWO_COLUMN + Types: Stance + Production Commands: + Template: THREE_COLUMN + Types: Production + Support Power Commands: + Template: THREE_COLUMN + Types: SupportPower + Music Commands: + Template: TWO_COLUMN + Types: Music X: (WINDOW_RIGHT - WIDTH) / 2 Y: (WINDOW_BOTTOM - HEIGHT) / 2 Width: 600 @@ -454,62 +479,46 @@ Background@SETTINGS_PANEL: Width: PARENT_RIGHT Height: 10 Align: Center - Container@GLOBAL_TEMPLATE: - Width: 262 - Height: 25 - Visible: false - Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 85 - Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH - Width: 80 - Height: 25 - Container@UNIT_TEMPLATE: - Width: 173 - Height: 25 - Visible: false - Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 84 - Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH + 1 - Width: 80 - Height: 25 - Container@PRODUCTION_TEMPLATE: - Width: 173 - Height: 25 - Visible: false - Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 84 - Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH + 1 - Width: 80 - Height: 25 - Container@DEVELOPER_TEMPLATE: - Width: 262 - Height: 25 - Visible: false - Children: - Label@FUNCTION: - Y: 0 - 1 - Width: PARENT_RIGHT - 115 - Height: 25 - Align: Right - HotkeyEntry@HOTKEY: - X: PARENT_RIGHT - WIDTH - Width: 110 - Height: 25 + ScrollItem@HEADER: + Width: 528 + Height: 13 + Visible: false + Children: + Label@LABEL: + Font: TinyBold + Width: PARENT_RIGHT + Height: 10 + Align: Center + Container@TEMPLATES: + Children: + Container@TWO_COLUMN: + Width: 262 + Height: 25 + Visible: false + Children: + Label@FUNCTION: + Y: 0 - 1 + Width: PARENT_RIGHT - 85 + Height: 25 + Align: Right + HotkeyEntry@HOTKEY: + X: PARENT_RIGHT - WIDTH + Width: 80 + Height: 25 + Container@THREE_COLUMN: + Width: 173 + Height: 25 + Visible: false + Children: + Label@FUNCTION: + Y: 0 - 1 + Width: PARENT_RIGHT - 84 + Height: 25 + Align: Right + HotkeyEntry@HOTKEY: + X: PARENT_RIGHT - WIDTH + 1 + Width: 80 + Height: 25 Container@ADVANCED_PANEL: X: 5 Y: 50