Skip to content

Commit

Permalink
Merge pull request #76 from Netherlands3D/feature/enable-disable-func…
Browse files Browse the repository at this point in the history
…tions

Feature/enable disable functions
  • Loading branch information
sambaas authored Feb 5, 2024
2 parents 94eda8b + f623557 commit 0a6a96f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 59 deletions.
32 changes: 17 additions & 15 deletions Assets/Scripts/Configuration/SetupWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace Netherlands3D.Twin.Configuration
Expand All @@ -24,9 +25,11 @@ public class SetupWizard : MonoBehaviour
[SerializeField] private TMP_InputField originYField;
[SerializeField] private TMP_InputField shareUrlField;

[SerializeField] private GameObject featureList;
[FormerlySerializedAs("featureList")]
[SerializeField] private GameObject functionalitiesList;

[Header("Prefab")] [SerializeField] private FunctionalitySelection featureSelectionPrefab;
[FormerlySerializedAs("featureSelectionPrefab")]
[Header("Prefab")] [SerializeField] private FunctionalitySelection functionalitySelectionPrefab;

[Header("Events")]
public UnityEvent OnSettingsChanged = new UnityEvent();
Expand All @@ -39,13 +42,13 @@ private void Start()
originYField.text = configuration.Origin.Points[1].ToString(CultureInfo.InvariantCulture);
originYField.onValueChanged.AddListener(OnOriginYChanged);

foreach (var availableFeature in configuration.Functionalities)
foreach (var availableFunctionality in configuration.Functionalities)
{
FunctionalitySelection functionalitySelection = Instantiate(featureSelectionPrefab, featureList.transform);
functionalitySelection.Init(availableFeature);
FunctionalitySelection functionalitySelection = Instantiate(functionalitySelectionPrefab, functionalitiesList.transform);
functionalitySelection.Init(availableFunctionality);

functionalitySelection.Toggle.onValueChanged.AddListener(value => OnFunctionalityChanged(availableFeature, value));
functionalitySelection.Button.onClick.AddListener(() => OnFunctionalitySelected(availableFeature));
functionalitySelection.Toggle.onValueChanged.AddListener(value => OnFunctionalityChanged(availableFunctionality, value));
functionalitySelection.Button.onClick.AddListener(() => OnFunctionalitySelected(availableFunctionality));
}
}

Expand All @@ -58,10 +61,10 @@ private void OnEnable()
configuration.OnOriginChanged.AddListener(ValidateRdCoordinates);
configuration.OnOriginChanged.AddListener(UpdateShareUrlWhenOriginChanges);
configuration.OnTitleChanged.AddListener(UpdateShareUrlWhenTitleChanges);
foreach (var availableFeature in configuration.Functionalities)
foreach (var availableFunctionality in configuration.Functionalities)
{
availableFeature.OnEnable.AddListener(UpdateShareUrlWhenFeatureChanges);
availableFeature.OnDisable.AddListener(UpdateShareUrlWhenFeatureChanges);
availableFunctionality.OnEnable.AddListener(UpdateShareUrlWhenFunctionalityChanges);
availableFunctionality.OnDisable.AddListener(UpdateShareUrlWhenFunctionalityChanges);
}
}

Expand All @@ -83,10 +86,10 @@ private void OnDisable()
configuration.OnOriginChanged.RemoveListener(ValidateRdCoordinates);
configuration.OnOriginChanged.RemoveListener(UpdateShareUrlWhenOriginChanges);
configuration.OnTitleChanged.RemoveListener(UpdateShareUrlWhenTitleChanges);
foreach (var availableFeature in configuration.Functionalities)
foreach (var availableFunctionality in configuration.Functionalities)
{
availableFeature.OnEnable.RemoveListener(UpdateShareUrlWhenFeatureChanges);
availableFeature.OnDisable.RemoveListener(UpdateShareUrlWhenFeatureChanges);
availableFunctionality.OnEnable.RemoveListener(UpdateShareUrlWhenFunctionalityChanges);
availableFunctionality.OnDisable.RemoveListener(UpdateShareUrlWhenFunctionalityChanges);
}
}

Expand All @@ -109,7 +112,7 @@ public void UpdateStartingPositionWithoutNotify(Coordinate coordinate)
configuration.Origin = coordinate;
}

private void UpdateShareUrlWhenFeatureChanges()
private void UpdateShareUrlWhenFunctionalityChanges()
{
UpdateShareUrl();
}
Expand Down Expand Up @@ -142,7 +145,6 @@ private void OnFunctionalityChanged(Functionality availableFunctionality, bool v
private void OnFunctionalitySelected(Functionality functionality)
{
//TODO: Display information about the functionality
Debug.Log("Show information about " + functionality.Title);
}

private void OnOriginYChanged(string value)
Expand Down
14 changes: 8 additions & 6 deletions Assets/Scripts/UI/FunctionalitySelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Netherlands3D.Twin.Functionalities;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace Netherlands3D.Twin
Expand All @@ -13,22 +14,23 @@ public class FunctionalitySelection : MonoBehaviour
[SerializeField] private TMP_Text title;
[SerializeField] private TMP_Text caption;

private Functionality feature;
[FormerlySerializedAs("feature")]
private Functionality functionality;
private Toggle toggle;
public Toggle Toggle { get => toggle; private set => toggle = value; }

private Button button;
public Button Button { get => button; private set => button = value; }

public void Init(Functionality feature)
public void Init(Functionality functionality)
{
this.feature = feature;
this.functionality = functionality;

title.text = feature.Title;
caption.text = feature.Caption;
title.text = functionality.Title;
caption.text = functionality.Caption;

Toggle = GetComponent<Toggle>();
Toggle.isOn = this.feature.IsEnabled;
Toggle.isOn = this.functionality.IsEnabled;

Button = GetComponentInChildren<Button>();
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/UI/MinMaxWidth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Netherlands3D.Twin
{
[ExecuteInEditMode]
public class MinMaxWidth : MonoBehaviour
{
[SerializeField] float minWidth = 800.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,53 @@ public class FunctionalityLink
private void Awake()
{
gameObject.SetActive(false);
try
{
foreach (var functionalityLink in FunctionalityLinks)
{
AddFeatureListenerForLink(functionalityLink);
}
}
finally

foreach (var functionalityLink in FunctionalityLinks)
{
gameObject.SetActive(true);
AddFunctionalityListenerForLink(functionalityLink);
}

gameObject.SetActive(true);
}

#if UNITY_EDITOR
private void OnValidate()
{
foreach (var featureLink in FunctionalityLinks)
foreach (var functionalityLink in FunctionalityLinks)
{
var linkFeature = featureLink.functionality != null ? featureLink.functionality : null;
featureLink.name = linkFeature?.Caption + " -> ";
var linkFunctionality = functionalityLink.functionality != null ? functionalityLink.functionality : null;
functionalityLink.name = linkFunctionality?.Caption + " -> ";

int listenerCount = featureLink.onFunctionalityToggle?.GetPersistentEventCount() ?? 0;
int listenerCount = functionalityLink.onFunctionalityToggle?.GetPersistentEventCount() ?? 0;

if (listenerCount == 1)
{
var type = featureLink.onFunctionalityToggle.GetPersistentTarget(0).ToString().Replace("(UnityEngine.", "(");
var methodName = featureLink.onFunctionalityToggle.GetPersistentMethodName(0);
featureLink.name += type + " -> " + methodName;
var type = functionalityLink.onFunctionalityToggle.GetPersistentTarget(0).ToString().Replace("(UnityEngine.", "(");
var methodName = functionalityLink.onFunctionalityToggle.GetPersistentMethodName(0);
functionalityLink.name += type + " -> " + methodName;
}
else if (listenerCount > 1)
{
featureLink.name += " (" + listenerCount + ")";
functionalityLink.name += " (" + listenerCount + ")";
}
else{
featureLink.name += " No listeners";
functionalityLink.name += " No listeners";
}
}
}
#endif


private void AddFeatureListenerForLink(FunctionalityLink featureLink)
private void AddFunctionalityListenerForLink(FunctionalityLink functionalityLink)
{
FunctionalityListener listener = gameObject.AddComponent<FunctionalityListener>();
listener.functionality = featureLink.functionality;
listener.OnEnableFeature.AddListener((Functionality feature) =>
listener.functionality = functionalityLink.functionality;
listener.OnEnableFunctionality.AddListener((Functionality functionality) =>
{
featureLink.onFunctionalityToggle?.Invoke(true);
functionalityLink.onFunctionalityToggle?.Invoke(true);
});
listener.OnDisableFeature.AddListener((Functionality feature) =>
listener.OnDisableFunctionality.AddListener((Functionality functionality) =>
{
featureLink.onFunctionalityToggle?.Invoke(false);
functionalityLink.onFunctionalityToggle?.Invoke(false);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class FunctionalityListener : MonoBehaviour
[FormerlySerializedAs("feature")]
public Functionality functionality;

[HideInInspector] public UnityEvent<Functionality> OnEnableFeature = new ();
[HideInInspector] public UnityEvent<Functionality> OnDisableFeature = new ();
[HideInInspector] public UnityEvent<Functionality> OnEnableFunctionality = new ();
[HideInInspector] public UnityEvent<Functionality> OnDisableFunctionality = new ();

private void Awake() {
functionality.OnEnable.AddListener(EnableFeature);
functionality.OnDisable.AddListener(DisableFeature);
functionality.OnEnable.AddListener(EnableFunctionality);
functionality.OnDisable.AddListener(DisableFunctionality);
}

private void OnValidate() {
Expand All @@ -31,28 +31,28 @@ private void OnEnable()
{
if (functionality.IsEnabled)
{
EnableFeature();
EnableFunctionality();
}
else
{
DisableFeature();
DisableFunctionality();
}
}

private void EnableFeature()
private void EnableFunctionality()
{
OnEnableFeature.Invoke(functionality);
OnEnableFunctionality.Invoke(functionality);
}

private void DisableFeature()
private void DisableFunctionality()
{
OnDisableFeature.Invoke(functionality);
OnDisableFunctionality.Invoke(functionality);
}

private void OnDestroy()
{
functionality.OnEnable.RemoveListener(EnableFeature);
functionality.OnDisable.RemoveListener(DisableFeature);
functionality.OnEnable.RemoveListener(EnableFunctionality);
functionality.OnDisable.RemoveListener(DisableFunctionality);
}
}
}

0 comments on commit 0a6a96f

Please sign in to comment.