From e809b85df87befe646946d651d4968c8a4447aaa Mon Sep 17 00:00:00 2001 From: Craxy Date: Sun, 26 May 2019 20:37:03 +0200 Subject: [PATCH] Fix #45: Add options to displace button --- .../Game/Behaviours/Level.cs | 2 +- .../Behaviours/ToggleTrafficLightsButton.cs | 15 ++- src/ToggleTrafficLights/Game/Options.cs | 6 + .../Game/UI/SettingsPanel.cs | 114 +++++++++++++++--- 4 files changed, 113 insertions(+), 24 deletions(-) diff --git a/src/ToggleTrafficLights/Game/Behaviours/Level.cs b/src/ToggleTrafficLights/Game/Behaviours/Level.cs index 2c6e315..c45605b 100644 --- a/src/ToggleTrafficLights/Game/Behaviours/Level.cs +++ b/src/ToggleTrafficLights/Game/Behaviours/Level.cs @@ -165,7 +165,7 @@ private void Setup() private void SetupTtlButton() { - ToggleTrafficLightsButton.InitialAdd(); + ToggleTrafficLightsButton.InitialAdd(Options); } private void SetupTool() diff --git a/src/ToggleTrafficLights/Game/Behaviours/ToggleTrafficLightsButton.cs b/src/ToggleTrafficLights/Game/Behaviours/ToggleTrafficLightsButton.cs index 1406e7f..77342f9 100644 --- a/src/ToggleTrafficLights/Game/Behaviours/ToggleTrafficLightsButton.cs +++ b/src/ToggleTrafficLights/Game/Behaviours/ToggleTrafficLightsButton.cs @@ -119,7 +119,8 @@ private static void Setup(ToggleTrafficLightsButton button) "OptionBaseFocusedRed", }); button.playAudioEvents = true; - button.relativePosition = new Vector3(131, 38); + // set by UpdateOffset + // button.relativePosition = new Vector3(DefaultButtonPositionX, DefaultButtonPositionY); SetDeactivatedStateSprites(button); } @@ -170,6 +171,13 @@ private static UITextureAtlas CreateAtlas(string file, string name, Material bas return atlas; } + private const float DefaultButtonPositionX = 131.0f; + private const float DefaultButtonPositionY = 38.0f; + public void UpdateOffset(float horizontal, float vertical) + { + this.relativePosition = new Vector3(DefaultButtonPositionX + horizontal, DefaultButtonPositionY + vertical); + } + #endregion Button #region Sprites @@ -251,10 +259,11 @@ private static UIPanel GetCurrentRoadsOptionPanel() return GetRoadsOptionPanelInRoadsPanel() ?? GetRoadsOptionPanel(); } - public static void InitialAdd() + public static void InitialAdd(Options options) { var pnl = GetCurrentRoadsOptionPanel(); - pnl.AddUIComponent(); + var btn = pnl.AddUIComponent(); + btn.UpdateOffset(options.TTLButtonHorizontalOffset.value, options.TTLButtonVerticalOffset.value); } public static void DestroyAll() diff --git a/src/ToggleTrafficLights/Game/Options.cs b/src/ToggleTrafficLights/Game/Options.cs index 9c34d1d..fc7eae4 100644 --- a/src/ToggleTrafficLights/Game/Options.cs +++ b/src/ToggleTrafficLights/Game/Options.cs @@ -38,6 +38,11 @@ static Options() public readonly SavedBool KeepInfoMode = new SavedBool(nameof(KeepInfoMode), SettingsFile, DefaultKeepInfoMode, true); + public readonly SavedFloat TTLButtonHorizontalOffset = + new SavedFloat(nameof(TTLButtonHorizontalOffset), SettingsFile, 0.0f, true); + public readonly SavedFloat TTLButtonVerticalOffset = + new SavedFloat(nameof(TTLButtonVerticalOffset), SettingsFile, 0.0f, true); + public void ResetShortcuts() { ShortcutActivateTTLWithMenu.value = DefaultShortcutActivateTTLWithMenu; @@ -45,6 +50,7 @@ public void ResetShortcuts() ShortcutActivateTrafficRoutesJunctions.value = DefaultShortcutActivateTrafficRoutesJunctions; } + internal static string GetShortcutName(string name) { switch (name) diff --git a/src/ToggleTrafficLights/Game/UI/SettingsPanel.cs b/src/ToggleTrafficLights/Game/UI/SettingsPanel.cs index 1667cf6..567c6dd 100644 --- a/src/ToggleTrafficLights/Game/UI/SettingsPanel.cs +++ b/src/ToggleTrafficLights/Game/UI/SettingsPanel.cs @@ -1,48 +1,122 @@ +using System; using System.Collections.Generic; using System.Reflection; using ColossalFramework; using ColossalFramework.Globalization; using ColossalFramework.UI; +using Craxy.CitiesSkylines.ToggleTrafficLights.Game.Behaviours; using Craxy.CitiesSkylines.ToggleTrafficLights.Utils; using ICities; using UnityEngine; namespace Craxy.CitiesSkylines.ToggleTrafficLights.Game.UI { - public sealed class SettingsBuilder + public static class SettingsBuilder { public static void MakeSettings(UIHelper helper, Options options) { { var group = helper.AddGroup("Settings"); { - var cb = (UICheckBox) group.AddCheckbox("Keep Info Mode", options.KeepInfoMode.value, c => options.KeepInfoMode.value = c); + var cb = (UICheckBox)group.AddCheckbox("Keep Info Mode", options.KeepInfoMode.value, c => options.KeepInfoMode.value = c); cb.tooltip = "If enabled, activating the junction tool keeps the current view (underground or overground). Otherwise the last used view is used."; } } { var group = helper.AddGroup("Keymapping"); - var pnl = UISettingsPanel.AddTo((UIComponent) ((UIHelper) group).self, options); + var pnl = UIShortcutSettingsPanel.AddTo((UIComponent)((UIHelper)group).self, options); } + { + var group = helper.AddGroup("Advanced Settings"); + { + var groupBtn = group.AddGroup("Displace TTL Button"); + { + { + var c = (UIComponent)(((UIHelper)groupBtn).self); + var lbl = c.parent.Find("Label"); + if (lbl != null) + { + lbl.tooltip = + "To see the button moving:\n" + + "* Ingame, open the Roads Building Menu\n" + + "* open the Pause Menu by clicking on the gear icon in the upper right corner\n" + + "* Open options and move into the TTL section\n" + + "* The TTL button is visible (although blured) in the background and changing its offset is directly reflected"; + } + } + + var horizontalOffset = AddSlider((UIHelper)groupBtn, "Horizontal offset", -210.0f, +180.0f, 1.0f, options.TTLButtonHorizontalOffset.value, o => OnOffsetChanged(o, null, options)); + var verticalOffset = AddSlider((UIHelper)groupBtn, "Vertical offset", -80.0f, +35.0f, 1.0f, options.TTLButtonVerticalOffset.value, o => OnOffsetChanged(null, o, options)); + + void OnOffsetReset() + { + horizontalOffset.value = 0.0f; + verticalOffset.value = 0.0f; + } + groupBtn.AddButton("Reset", OnOffsetReset); + } + } + } + } + + public static void OnOffsetChanged(float? horizontal, float? vertical, Options options) + { + if (horizontal.HasValue) + { + options.TTLButtonHorizontalOffset.value = horizontal.Value; + } + if (vertical.HasValue) + { + options.TTLButtonVerticalOffset.value = vertical.Value; + } + + // try update button + var btn = GameObject.FindObjectOfType(); + if (btn != null) + { + btn.UpdateOffset(options.TTLButtonHorizontalOffset.value, options.TTLButtonVerticalOffset.value); + } + } + + private static UISlider AddSlider(UIHelper parent, string text, float min, float max, float step, float defaultValue, OnValueChanged onValueChanged) + { + string FormatText(float value) + => $"{text}: {(int)value}"; + + var uiPanel = ((UIComponent)parent.self).AttachUIComponent(UITemplateManager.GetAsGameObject("OptionsSliderTemplate")) as UIPanel; + var uiLabel = uiPanel.Find("Label"); + uiLabel.text = FormatText(defaultValue); + var uiSlider = uiPanel.Find("Slider"); + uiSlider.minValue = min; + uiSlider.maxValue = max; + uiSlider.stepSize = step; + uiSlider.value = defaultValue; + uiSlider.eventValueChanged += (_, v) => + { + uiLabel.text = FormatText(v); + onValueChanged?.Invoke(v); + }; + + return uiSlider; } } - public class UISettingsPanel : UIPanel + public sealed class UIShortcutSettingsPanel : UIPanel { public LocaleManager LocaleManager = LocaleManager.instance; - public static UISettingsPanel AddTo(UIComponent parent, Options options) + public static UIShortcutSettingsPanel AddTo(UIComponent parent, Options options) { - return parent.AddUIComponent().Setup(options); + return parent.AddUIComponent().Setup(options); } private Options _options; - private UISettingsPanel Setup(Options options) + private UIShortcutSettingsPanel Setup(Options options) { _options = options; - var pnt = (UIPanel) parent; + var pnt = (UIPanel)parent; this.width = parent.width - pnt.padding.left - pnt.padding.right; this.autoLayout = true; @@ -95,7 +169,7 @@ private void AddRightButton(string text, string tooltip, MouseEventHandler onCli pnl.autoLayoutStart = LayoutStart.TopRight; pnl.width = this.width - this.padding.left - this.padding.right - 50.0f; - UIButton btn = (UIButton) pnl.AttachUIComponent(UITemplateManager.GetAsGameObject("OptionsButtonTemplate")); + UIButton btn = (UIButton)pnl.AttachUIComponent(UITemplateManager.GetAsGameObject("OptionsButtonTemplate")); btn.text = text; btn.tooltip = tooltip; btn.eventClick += onClick; @@ -106,7 +180,7 @@ private void AddShortcut(int i, string name, SavedInputKey shortcut) { //Source: OptionsKeymappingPanel.CreateBindableInputs const string keyBindingTemplate = "KeyBindingTemplate"; - var pnl = (UIPanel) this.AttachUIComponent(UITemplateManager.GetAsGameObject(keyBindingTemplate)); + var pnl = (UIPanel)this.AttachUIComponent(UITemplateManager.GetAsGameObject(keyBindingTemplate)); if (i % 2 == 0) { pnl.backgroundSprite = string.Empty; @@ -124,7 +198,7 @@ private void AddShortcut(int i, string name, SavedInputKey shortcut) private void AddShortcutDisplay(int i, string name, SavedInputKey shortcut) { const string keyBindingTemplate = "KeyBindingTemplate"; - var pnl = (UIPanel) this.AttachUIComponent(UITemplateManager.GetAsGameObject(keyBindingTemplate)); + var pnl = (UIPanel)this.AttachUIComponent(UITemplateManager.GetAsGameObject(keyBindingTemplate)); if (i % 2 == 0) { pnl.backgroundSprite = string.Empty; @@ -150,8 +224,8 @@ private void OnBindingMouseDown(UIComponent comp, UIMouseEventParameter e) { if (_editingBinding == null) { - var btn = (UIButton) comp; - _editingBinding = (SavedInputKey) btn.objectUserData; + var btn = (UIButton)comp; + _editingBinding = (SavedInputKey)btn.objectUserData; btn.buttonsMask = UIMouseButton.Left | UIMouseButton.Right | UIMouseButton.Middle | UIMouseButton.Special0 | UIMouseButton.Special1 | UIMouseButton.Special2 | UIMouseButton.Special3; btn.text = Locale.Get("KEYMAPPING_PRESSANYKEY"); @@ -200,7 +274,7 @@ private void OnBindingKeyDown(UIComponent comp, UIKeyEventParameter e) ConfirmPanel.ShowModal(Locale.Get("CONFIRM_REBINDKEY", "Title"), message, (c, ret) => { - var btn = (UIButton) comp; + var btn = (UIButton)comp; if (ret == 1) { _editingBinding.value = newKey; @@ -212,12 +286,12 @@ private void OnBindingKeyDown(UIComponent comp, UIKeyEventParameter e) RefreshKeyMapping(); } _editingBinding = null; - btn.text = ((SavedInputKey) btn.objectUserData).ToLocalizedString("KEYNAME"); + btn.text = ((SavedInputKey)btn.objectUserData).ToLocalizedString("KEYNAME"); }); } else { - UpdateKeyBinding(newKey, (UIButton) comp, false); + UpdateKeyBinding(newKey, (UIButton)comp, false); } } @@ -261,7 +335,7 @@ private static bool IsAlreadyBoundCitiesSkylines(SavedInputKey target, InputKey foreach (var field in typeof(Settings).GetFields(BindingFlags.Static | BindingFlags.Public)) { var customAttributes = - (RebindableKeyAttribute[]) field.GetCustomAttributes(typeof(RebindableKeyAttribute), false); + (RebindableKeyAttribute[])field.GetCustomAttributes(typeof(RebindableKeyAttribute), false); if (customAttributes.Length > 0 && (category == customAttributes[0].category || string.IsNullOrEmpty(customAttributes[0].category))) { @@ -286,7 +360,7 @@ private static InputKey GetDefaultEntry(string entryName) var obj = field.GetValue(null); if (obj is InputKey) { - return (InputKey) obj; + return (InputKey)obj; } return 0; } @@ -304,7 +378,7 @@ private static bool IsAlreadyBoundTtl(SavedInputKey target, InputKey inputKey, O { if (field.FieldType == typeof(SavedInputKey)) { - var savedInputKey = (SavedInputKey) field.GetValue(options); + var savedInputKey = (SavedInputKey)field.GetValue(options); if (target != savedInputKey && inputKey == savedInputKey.value) { DebugLog.Info($"Already assigned: \n" + @@ -337,7 +411,7 @@ private void RefreshKeyMapping() var btn = components[i].Find("Binding"); if (btn != null && btn.objectUserData is SavedInputKey) { - var shortcut = (SavedInputKey) btn.objectUserData; + var shortcut = (SavedInputKey)btn.objectUserData; btn.text = shortcut.ToLocalizedString("KEYNAME"); } }