NRPhoneScreenProvider.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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
  10. {
  11. using System;
  12. using UnityEngine;
  13. /// <summary> A system button state. </summary>
  14. public class SystemInputState
  15. {
  16. /// <summary> TRIGGER APP HOME. </summary>
  17. public bool[] buttons = new bool[3];
  18. /// <summary> The touch. </summary>
  19. public Vector2 touch;
  20. /// <summary> The origin touch. </summary>
  21. public Vector2 originTouch;
  22. /// <summary> True to pressing. </summary>
  23. public bool pressing;
  24. /// <summary> True to press down. </summary>
  25. public bool pressDown;
  26. /// <summary> True to press up. </summary>
  27. public bool pressUp;
  28. public override string ToString()
  29. {
  30. return string.Format("buttons {0} {1} {2} touch x:{3} touch y:{4}", buttons[0], buttons[1], buttons[2], touch.x, touch.y);
  31. }
  32. }
  33. public class SystemButtonState
  34. {
  35. /// <summary> APP TRIGGER HOME. </summary>
  36. public bool[] buttons = new bool[3];
  37. public float touch_x;
  38. public float touch_y;
  39. public void Reset() {
  40. for (int i = 0; i < buttons.Length; i++)
  41. {
  42. buttons[i] = false;
  43. }
  44. touch_x = 0.0f;
  45. touch_y = 0.0f;
  46. }
  47. public void Set(bool btnApp, bool btnTouch, bool btnHome, float touchX, float touchY)
  48. {
  49. buttons[0] = btnApp;
  50. buttons[1] = btnTouch;
  51. buttons[2] = btnHome;
  52. touch_x = touchX;
  53. touch_y = touchY;
  54. }
  55. public SystemInputState TransformTo(SystemInputState unitystate)
  56. {
  57. if (unitystate == null)
  58. {
  59. unitystate = new SystemInputState();
  60. }
  61. unitystate.buttons[0] = this.buttons[1];
  62. unitystate.buttons[1] = this.buttons[0];
  63. unitystate.buttons[2] = this.buttons[2];
  64. unitystate.touch.x = this.touch_x;
  65. unitystate.touch.y = this.touch_y;
  66. return unitystate;
  67. }
  68. public SystemButtonState DeSerialize(byte[] data)
  69. {
  70. buttons[0] = Convert.ToBoolean(data[0]);
  71. buttons[1] = Convert.ToBoolean(data[1]);
  72. buttons[2] = Convert.ToBoolean(data[2]);
  73. touch_x = ConvertUtility.IntBitsToFloat(BitConverter.ToInt32(data, 3));
  74. touch_y = ConvertUtility.IntBitsToFloat(BitConverter.ToInt32(data, 7));
  75. return this;
  76. }
  77. public override string ToString()
  78. {
  79. return string.Format("buttons {0} {1} {2} touch x:{3} touch y:{4}", buttons[0], buttons[1], buttons[2], touch_x, touch_y);
  80. }
  81. }
  82. public interface ISystemButtonStateProvider
  83. {
  84. void BindReceiver(ISystemButtonStateReceiver receiver);
  85. }
  86. public interface ISystemButtonStateReceiver
  87. {
  88. void OnDataReceived(SystemButtonState state);
  89. }
  90. public interface ISystemButtonDataProxy
  91. {
  92. void OnUpdate(AndroidJavaObject state);
  93. }
  94. public class NRPhoneScreenProviderBase : ISystemButtonStateProvider
  95. {
  96. protected ISystemButtonDataProxy m_AndroidSystemButtonDataProxy;
  97. protected ISystemButtonStateReceiver m_Receiver;
  98. protected SystemInputState m_SystemButtonState = new SystemInputState();
  99. private AndroidJavaObject m_UnityActivity;
  100. public NRPhoneScreenProviderBase()
  101. {
  102. AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  103. m_UnityActivity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  104. m_AndroidSystemButtonDataProxy = CreateAndroidDataProxy();
  105. this.RegistFragment(m_UnityActivity, m_AndroidSystemButtonDataProxy);
  106. NRKernalUpdater.OnPreUpdate += OnPreUpdate;
  107. }
  108. public void BindReceiver(ISystemButtonStateReceiver receiver)
  109. {
  110. this.m_Receiver = receiver;
  111. }
  112. public virtual void OnPreUpdate() {}
  113. public virtual void RegistFragment(AndroidJavaObject unityActivity, ISystemButtonDataProxy proxy) { }
  114. public virtual ISystemButtonDataProxy CreateAndroidDataProxy() { return null; }
  115. public virtual void OnSystemButtonDataChanged(SystemButtonState state)
  116. {
  117. this.m_Receiver?.OnDataReceived(state);
  118. }
  119. public virtual void ResizeView(int w, int h)
  120. {
  121. this.RegistFragment(m_UnityActivity, m_AndroidSystemButtonDataProxy);
  122. }
  123. }
  124. }