ObserverConfigView.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental.StreammingCast
  10. {
  11. using System.IO;
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. using UnityEngine.Events;
  15. using NRKernal.NRExamples;
  16. /// <summary> A configuration view. </summary>
  17. public class ObserverConfigView : MonoBehaviour
  18. {
  19. /// <summary> Back, called when the configuration view. </summary>
  20. /// <param name="config"> The configuration.</param>
  21. public delegate void ConfigViewCallBack(ObserverViewConfig config);
  22. /// <summary> Event queue for all listeners interested in OnConfigrationChanged events. </summary>
  23. public event ConfigViewCallBack OnConfigrationChanged;
  24. /// <summary> The IP field. </summary>
  25. public InputField m_IPField;
  26. /// <summary> The use debug. </summary>
  27. public Toggle m_UseDebug;
  28. public NRInteractiveItem m_CloseBtn;
  29. public Transform m_Root;
  30. /// <summary> The current configuration. </summary>
  31. private ObserverViewConfig currentConfig;
  32. private const string DefaultServerIP = "192.168.0.100";
  33. private const float CLICK_INTERVAL = 0.2f;
  34. private int CLICK_TRIGGER_TIMES = 3;
  35. private float lastClickTime = 0f;
  36. private int clickTimes = 0;
  37. /// <summary> Gets the full pathname of the configuration file. </summary>
  38. /// <value> The full pathname of the configuration file. </value>
  39. public static string ConfigPath
  40. {
  41. get
  42. {
  43. return Path.Combine(Application.persistentDataPath, "ObserverViewConfig.json");
  44. }
  45. }
  46. void Start()
  47. {
  48. m_IPField.onValueChanged.AddListener((value) =>
  49. {
  50. if (!value.Equals(currentConfig.serverIP))
  51. {
  52. currentConfig.serverIP = value;
  53. UpdateConfig();
  54. }
  55. });
  56. m_UseDebug.onValueChanged.AddListener((value) =>
  57. {
  58. if (value != currentConfig.useDebugUI)
  59. {
  60. currentConfig.useDebugUI = value;
  61. UpdateConfig();
  62. }
  63. });
  64. m_CloseBtn.onPointerClick.AddListener(new UnityAction(() =>
  65. {
  66. SwitchView(false);
  67. }));
  68. NRInput.AddClickListener(ControllerHandEnum.Left, ControllerButton.HOME, OnHomeButtonClick);
  69. NRInput.AddClickListener(ControllerHandEnum.Right, ControllerButton.HOME, OnHomeButtonClick);
  70. this.LoadConfig();
  71. SwitchView(false);
  72. }
  73. private void OnHomeButtonClick()
  74. {
  75. if (clickTimes == 0)
  76. {
  77. clickTimes++;
  78. lastClickTime = Time.realtimeSinceStartup;
  79. }
  80. else if (Time.realtimeSinceStartup - lastClickTime < CLICK_INTERVAL)
  81. {
  82. clickTimes++;
  83. lastClickTime = Time.realtimeSinceStartup;
  84. }
  85. else
  86. {
  87. clickTimes = 0;
  88. }
  89. if (clickTimes == CLICK_TRIGGER_TIMES)
  90. {
  91. SwitchView(!m_Root.gameObject.activeInHierarchy);
  92. }
  93. }
  94. private void SwitchView(bool flag)
  95. {
  96. m_Root.gameObject.SetActive(flag);
  97. }
  98. /// <summary> Loads the configuration. </summary>
  99. private void LoadConfig()
  100. {
  101. if (!File.Exists(ConfigPath))
  102. {
  103. NRDebugger.Info("[ConfigView] File is not exist :" + ConfigPath);
  104. currentConfig = new ObserverViewConfig(DefaultServerIP, Vector3.zero);
  105. }
  106. else
  107. {
  108. NRDebugger.Info("[ConfigView] Load config from:" + ConfigPath);
  109. currentConfig = JsonUtility.FromJson<ObserverViewConfig>(File.ReadAllText(ConfigPath));
  110. }
  111. m_IPField.text = currentConfig.serverIP;
  112. m_UseDebug.isOn = currentConfig.useDebugUI;
  113. OnConfigrationChanged?.Invoke(currentConfig);
  114. }
  115. /// <summary> Config will Works at next run time. </summary>
  116. private void UpdateConfig()
  117. {
  118. var json = JsonUtility.ToJson(currentConfig);
  119. File.WriteAllText(ConfigPath, json);
  120. NRDebugger.Info("[ConfigView] Save config :" + json);
  121. OnConfigrationChanged?.Invoke(currentConfig);
  122. }
  123. }
  124. }