Skip to content

Commit

Permalink
Fix #45: Add options to displace button
Browse files Browse the repository at this point in the history
  • Loading branch information
Craxy committed May 26, 2019
1 parent 9c9d3d6 commit e809b85
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/ToggleTrafficLights/Game/Behaviours/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void Setup()

private void SetupTtlButton()
{
ToggleTrafficLightsButton.InitialAdd();
ToggleTrafficLightsButton.InitialAdd(Options);
}

private void SetupTool()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<ToggleTrafficLightsButton>();
var btn = pnl.AddUIComponent<ToggleTrafficLightsButton>();
btn.UpdateOffset(options.TTLButtonHorizontalOffset.value, options.TTLButtonVerticalOffset.value);
}

public static void DestroyAll()
Expand Down
6 changes: 6 additions & 0 deletions src/ToggleTrafficLights/Game/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ 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;
ShortcutActivateTTLWithoutMenu.value = DefaultShortcutActivateTTLWithoutMenu;
ShortcutActivateTrafficRoutesJunctions.value = DefaultShortcutActivateTrafficRoutesJunctions;
}


internal static string GetShortcutName(string name)
{
switch (name)
Expand Down
114 changes: 94 additions & 20 deletions src/ToggleTrafficLights/Game/UI/SettingsPanel.cs
Original file line number Diff line number Diff line change
@@ -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<UILabel>("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<ToggleTrafficLightsButton>();
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<UILabel>("Label");
uiLabel.text = FormatText(defaultValue);
var uiSlider = uiPanel.Find<UISlider>("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<UISettingsPanel>().Setup(options);
return parent.AddUIComponent<UIShortcutSettingsPanel>().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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)))
{
Expand All @@ -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;
}
Expand All @@ -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" +
Expand Down Expand Up @@ -337,7 +411,7 @@ private void RefreshKeyMapping()
var btn = components[i].Find<UITextComponent>("Binding");
if (btn != null && btn.objectUserData is SavedInputKey)
{
var shortcut = (SavedInputKey) btn.objectUserData;
var shortcut = (SavedInputKey)btn.objectUserData;
btn.text = shortcut.ToLocalizedString("KEYNAME");
}
}
Expand Down

0 comments on commit e809b85

Please sign in to comment.