Newer
Older
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
MaZiFAU
committed
using static SOMDocManager;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class PopupBehavior : MonoBehaviour
{
[SerializeField] GameObject canvas;
[SerializeField] Button CloseButton;
[SerializeField] TMP_Text message;
private Scroll activeScroll;
private List<GameObject> parameterDisplays;
public string ServerErrorMessage = "unknown server error";
public string NonTotalMessage = "Scroll application not complete";
public string UnknownErrorMessage = "Unkown error - did you apply all facts?";
public string InvalidAssignmentMessage = "Invalid Assignment";
private string errorMessage = "";
// Start is called before the first frame update
void Awake()
{
CommunicationEvents.PushoutFactFailEvent.AddListener(onFailedScrollInput);
CloseButton.onClick.RemoveAllListeners();
CloseButton.onClick.AddListener(hidePopUp);
}
public void setMessage(string errorMessage)
{
this.message.text = errorMessage;
}
public void setScroll(Scroll scroll)
{
this.activeScroll = scroll;
}
public void setParameterDisplays(List<GameObject> parameters)
{
this.parameterDisplays = parameters;
}
public void showPopUp()
{
canvas.SetActive(true);
StartCoroutine(hideAfter5sec());
IEnumerator hideAfter5sec()
{
yield return new WaitForSeconds(5);
hidePopUp();
}
}
public void hidePopUp()
{
canvas.SetActive(false);
}
/// <summary>
/// this method creates a helpful error message and shows the popup. For it to work properly, the setScroll and setParameterDisplays must have been set.
/// </summary>
/// <param name="startfact"></param>
/// <param name="errorInfo"></param>
public void onFailedScrollInput(Fact startfact, Scroll.ScrollApplicationInfo errorInfo)
{
setMessage(generateHelpfulMessageAndAnimateScrollParam(errorInfo));
private string generateHelpfulMessageAndAnimateScrollParam(Scroll.ScrollApplicationInfo errorInfo)
int invAssCount = 0;
errorMessage = "";
Scroll.ScrollApplicationCheckingError error = errorInfo.errors[i];
//check which error ocurred and set Message to a helpful error message.
if (error.kind == "nonTotal")
{
errorMessage += NonTotalMessage;
errorMessage += '\n';
MMTDeclaration fact = parseFactFromError(error);
//animate all invalidly assigned facts
if (parameterDisplays != null && fact != null)
foreach (GameObject g in parameterDisplays)
RenderedScrollFact scrollfact = g.transform.GetChild(0).GetComponent<RenderedScrollFact>();
scrollfact.ImageHintObject.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger();
{
Debug.Log("PopupBehavior: Error: scroll or parameterDisplays not set.");
{
errorMessage += UnknownErrorMessage;
errorMessage += '\n';
}
}
//invalid assignment message
{
errorMessage += invAssCount.ToString() + " " + InvalidAssignmentMessage;
{
errorMessage += 's';
}
errorMessage += '\n';
}
return errorMessage;
}
//this should be changed, the Fact Object should be parsed by JSON. This is a workaround because the MMT servers JSON serialization contains a bug
private MMTDeclaration parseFactFromError(Scroll.ScrollApplicationCheckingError error)
string message = error.msg;
//cut start of string
int indexFact = message.IndexOf('[');
string factUri = message.Substring(indexFact + 1);
// cut end of string
int indexFactEnd = factUri.IndexOf(']');
string rest = factUri.Substring(indexFactEnd);
factUri = factUri.Substring(0, indexFactEnd);
//get fact Label from the rest of the string
int factNameLength = rest.IndexOf('?') - 2;
string factLabel = rest.Substring(2, factNameLength);
//add ?factName to URI
factUri += "?" + factLabel;
//find the required fact in the active scroll thats invalidly assigned
return activeScroll?.requiredFacts
.FirstOrDefault(decl => decl.@ref.uri.Equals(factUri));