Skip to content

Commit

Permalink
Options value set - threading fix
Browse files Browse the repository at this point in the history
  • Loading branch information
krzychu124 committed Oct 11, 2022
1 parent 503421d commit e6a73b1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
16 changes: 15 additions & 1 deletion TLM/TLM/UI/Helpers/CheckboxOption.cs
Expand Up @@ -4,7 +4,9 @@ namespace TrafficManager.UI.Helpers {
using TrafficManager.State;
using CSUtil.Commons;
using System.Collections.Generic;
using ColossalFramework.Threading;
using JetBrains.Annotations;
using TrafficManager.Util;
using UnityEngine;

public class CheckboxOption : SerializableUIOptionBase<bool, UICheckBox, CheckboxOption> {
Expand Down Expand Up @@ -74,7 +76,19 @@ public CheckboxOption(string fieldName, Options.PersistTo scope = Options.Persis
if (!value && _propagatesFalseTo != null)
PropagateTo(_propagatesFalseTo, false);

if (HasUI) _ui.isChecked = value;
if (Shortcuts.IsMainThread()) {
if (HasUI) {
_ui.isChecked = value;
}
} else {
SimulationManager.instance
.m_ThreadingWrapper
.QueueMainThread(() => {
if (HasUI) {
_ui.isChecked = value;
}
});
}
}
}

Expand Down
15 changes: 13 additions & 2 deletions TLM/TLM/UI/Helpers/DropDownOption.cs
Expand Up @@ -6,6 +6,7 @@ namespace TrafficManager.UI.Helpers {
using System;
using System.Linq;
using TrafficManager.API.Traffic.Enums;
using TrafficManager.Util;

public class DropDownOption<TEnum> : SerializableUIOptionBase<TEnum, UIDropDown, DropDownOption<TEnum>>
where TEnum : struct, Enum, IConvertible {
Expand Down Expand Up @@ -50,8 +51,18 @@ public DropDownOption(string fieldName, Options.PersistTo scope = Options.Persis
set {
if (values_.Contains(value)) {
base.Value = value;
if (HasUI) {
_ui.selectedIndex = IndexOf(value);
if (Shortcuts.IsMainThread()) {
if (HasUI) {
_ui.selectedIndex = IndexOf(value);
}
} else {
SimulationManager.instance
.m_ThreadingWrapper
.QueueMainThread(() => {
if (HasUI) {
_ui.selectedIndex = IndexOf(value);
}
});
}
} else {
Log.Error($"unrecognised value:{value} for enum:{typeof(TEnum).Name}");
Expand Down
3 changes: 1 addition & 2 deletions TLM/TLM/UI/Helpers/SerializableUIOptionBase.cs
Expand Up @@ -105,8 +105,7 @@ public abstract class SerializableUIOptionBase<TVal, TUI, TComponent> : ILegacyS

/* UI: */

// TODO FIX - temporary solution!
public bool HasUI => _ui != null && Thread.CurrentThread != SimulationManager.instance.m_simulationThread;
public bool HasUI => _ui != null;
protected TUI _ui;

protected string _label;
Expand Down
18 changes: 15 additions & 3 deletions TLM/TLM/UI/Helpers/SliderOption.cs
Expand Up @@ -6,6 +6,7 @@ namespace TrafficManager.UI.Helpers {
using CSUtil.Commons;
using UnityEngine;
using System;
using TrafficManager.Util;

public class SliderOption : SerializableUIOptionBase<float, UISlider, SliderOption> {

Expand Down Expand Up @@ -70,9 +71,20 @@ public byte FloatToByte(float val)

Log.Info($"SliderOption.Value: `{FieldName}` changed to {value}");

if (HasUI) {
_ui.value = value;
UpdateTooltip();
if (Shortcuts.IsMainThread()) {
if (HasUI) {
_ui.value = value;
UpdateTooltip();
}
} else {
SimulationManager.instance
.m_ThreadingWrapper
.QueueMainThread(() => {
if (HasUI) {
_ui.value = value;
UpdateTooltip();
}
});
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions TLM/TLM/Util/Shortcuts.cs
Expand Up @@ -13,11 +13,14 @@ namespace TrafficManager.Util {
using UnityEngine;
using ColossalFramework.UI;
using System.Diagnostics;
using ColossalFramework.Threading;

internal static class Shortcuts {
internal static bool InSimulationThread() =>
System.Threading.Thread.CurrentThread == SimulationManager.instance.m_simulationThread;

internal static bool IsMainThread() => Dispatcher.currentSafe == Dispatcher.mainSafe;

/// <summary>
/// returns a new calling Clone() on all items.
/// </summary>
Expand Down

0 comments on commit e6a73b1

Please sign in to comment.