/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
using UnityEngine;
using UnityEngine.UI;
using System;
namespace NRKernal
{
/// Form for viewing the nr notification.
public class NRNotificationWindow : MonoBehaviour
{
/// (Serializable) information about the notification.
[Serializable]
public struct NotificationInfo
{
/// The sprite.
public Sprite sprite;
/// The title.
public string title;
/// The message.
public string message;
}
/// Information describing the high level.
public NotificationInfo m_HighLevelInfo;
/// Information describing the middle level.
public NotificationInfo m_MiddleLevelInfo;
/// The icon.
[SerializeField] Image m_Icon;
/// The title.
[SerializeField] Text m_Title;
/// The message.
[SerializeField] Text m_Message;
/// The confirm control.
[SerializeField] Button m_ConfirmBtn;
protected NRNotificationListener.Level m_Level = NRNotificationListener.Level.Low;
protected event Action OnConfirm;
protected float m_Duration = 2f;
private string m_TitleExtra;
private string m_MessageExtra;
/// Fill data.
/// The level.
/// (Optional) The duration.
public virtual NRNotificationWindow Build()
{
NotificationInfo info;
switch (m_Level)
{
case NRNotificationListener.Level.High:
info = m_HighLevelInfo;
break;
case NRNotificationListener.Level.Middle:
info = m_MiddleLevelInfo;
m_ConfirmBtn?.gameObject.SetActive(false);
break;
case NRNotificationListener.Level.Low:
default:
GameObject.Destroy(gameObject);
return this;
}
m_Icon.sprite = info.sprite;
if (!string.IsNullOrEmpty(m_TitleExtra))
{
m_Title.text = m_TitleExtra;
}
else
{
m_Title.text = info.title;
}
if (!string.IsNullOrEmpty(m_MessageExtra))
{
m_Message.text = m_MessageExtra;
}
else
{
m_Message.text = info.message;
}
m_ConfirmBtn?.onClick.AddListener(() =>
{
OnConfirm?.Invoke();
AutoDestroy();
});
if (m_Duration > 0)
{
Invoke("AutoDestroy", m_Duration);
}
return this;
}
public NRNotificationWindow SetTitle(string title)
{
this.m_TitleExtra = title;
return this;
}
public NRNotificationWindow SetContent(string content)
{
this.m_MessageExtra = content;
return this;
}
public NRNotificationWindow SetLevle(NRNotificationListener.Level level)
{
this.m_Level = level;
return this;
}
public NRNotificationWindow SetConfirmAction(Action callback)
{
this.OnConfirm += callback;
return this;
}
public NRNotificationWindow SetDuration(float duration)
{
this.m_Duration = duration;
return this;
}
/// Automatic destroy.
private void AutoDestroy()
{
GameObject.Destroy(gameObject);
}
}
}