Newer
Older
Björn Eßwein
committed
using REST_JSON_API;
using System.Collections.Generic;
using System.Text.RegularExpressions;
Björn Eßwein
committed
using TMPro;
using UnityEngine;
public class ScrollDetails : ScrollView
{
public static ScrollDetails Instance
{
get => _Instance;
set
{
if (_Instance != value)
Destroy(_Instance);
_Instance = value;
}
}
private static ScrollDetails _Instance;
public WorldCursor cursor;
public GameObject parameterDisplayPrefab;
public GameObject mmtAnswerPopUp;
private PopupBehavior Popup;
public static List<RenderedScrollFact> ParameterDisplays { get; private set; }
void Awake()
{
Instance = this;
Björn Eßwein
committed
if (cursor == null) cursor = FindObjectOfType<WorldCursor>();
Björn Eßwein
committed
Popup = mmtAnswerPopUp.GetComponent<PopupBehavior>();
Popup.gameObject.SetActive(true); // force Awake
}
private void OnEnable()
{
Björn Eßwein
committed
SwitchScrollUI.activeScrollData.OnScrollChanged.AddListener(ShowScroll);
SwitchScrollUI.activeScrollData.OnFactAssignmentUpdated.AddListener(OnFactAssignmentUpdated);
Björn Eßwein
committed
SwitchScrollUI.activeScrollData.OnScrollDynamicInfoUpdated.AddListener(DisplayScrollDescription);
Björn Eßwein
committed
}
private void OnDisable()
{
Björn Eßwein
committed
SwitchScrollUI.activeScrollData.OnScrollChanged.RemoveListener(ShowScroll);
SwitchScrollUI.activeScrollData.OnFactAssignmentUpdated.RemoveListener(OnFactAssignmentUpdated);
Björn Eßwein
committed
SwitchScrollUI.activeScrollData.OnScrollDynamicInfoUpdated.RemoveListener(DisplayScrollDescription);
Björn Eßwein
committed
}
private void OnDestroy()
{
_Instance = null;
}
/// <summary>
/// Update the scroll details screen to display the currently active scroll
/// </summary>
/// <param name="activeScroll"></param>
Björn Eßwein
committed
private void ShowScroll(Scroll newScroll)
Björn Eßwein
committed
{
Transform originalScroll = gameObject.transform.GetChild(1);
Transform originalScrollView = originalScroll.GetChild(1);
Transform originalViewport = originalScrollView.GetChild(0);
//Clear all current ScrollFacts
originalViewport.GetChild(0).gameObject.DestroyAllChildren();
Björn Eßwein
committed
DisplayScrollDescription(newScroll);
Björn Eßwein
committed
//ParameterDisplays.ForEach(gameObj => Destroy(gameObj));
ParameterDisplays = new();
// generate parameter display slots
Björn Eßwein
committed
foreach (var slot in SwitchScrollUI.activeScrollData.Assignments)
Björn Eßwein
committed
{
GameObject originalObj =
Instantiate(parameterDisplayPrefab, originalViewport.GetChild(0));
RenderedScrollFact originalRSF =
originalObj.GetComponentInChildren<RenderedScrollFact>();
ParameterDisplays.Add(originalRSF);
Björn Eßwein
committed
originalRSF.Populate(newScroll, slot.Key);
Björn Eßwein
committed
originalRSF.transform.parent.gameObject.SetActive(slot.Value.IsVisible);
}
}
Björn Eßwein
committed
private void DisplayScrollDescription(Scroll scroll)
Björn Eßwein
committed
{
Björn Eßwein
committed
var description = scroll.description;
// if description is a html description, extract the alternative text representation
if (Regex.IsMatch(scroll.description, ".*<scroll-description.*"))
{
description = Regex.Match(scroll.description, "<scroll-description [^>]* alt=((?>\\\")|(?>\\'))([^>]*?)\\1[^>]*>").Groups[2].Value;
Björn Eßwein
committed
}
Björn Eßwein
committed
Transform scrollComponent = gameObject.transform.GetChild(1).transform;
scrollComponent.GetChild(0).GetComponent<TextMeshProUGUI>().text = description;
Björn Eßwein
committed
}
private void OnFactAssignmentUpdated(string slotUri, ActiveScroll.SlotAssignment slotAssignment)
{
RenderedScrollFact changed = ParameterDisplays.Find(RSF => RSF.ScrollFactURI == slotUri);
// check if the RenderedScrollFact already contains the assigned fact
Björn Eßwein
committed
// this is to prevent an endless loop because updating the Fact property will trigger the NewAssignmentEvent
// TODO: BE: find an other solution because this results in requesting the scroll dynamic update twice from the server
Björn Eßwein
committed
if (changed != null && (
(!slotAssignment.IsSet && changed.IsSet) ||
(slotAssignment.IsSet && changed.URI != slotAssignment.fact?.Id)
))
Björn Eßwein
committed
{
changed.Fact = slotAssignment.fact;
}
}
[ContextMenu("MagicButtonClicked")]
public void MagicButtonClicked()
{
SwitchScrollUI.activeScrollData.ButtonClicked(new MagicScrollButton());
}