NRPhoneControllerStateParser.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 UnityEngine;
  12. /// <summary>
  13. /// The class parses the touch position of smart phone to usable states by invoking parsing
  14. /// method every frame. </summary>
  15. public class NRPhoneControllerStateParser : IControllerStateParser
  16. {
  17. /// <summary> The buttons down. </summary>
  18. private bool[] _buttons_down = new bool[3];
  19. /// <summary> The buttons up. </summary>
  20. private bool[] _buttons_up = new bool[3];
  21. /// <summary> The buttons. </summary>
  22. private bool[] _buttons = new bool[3];
  23. private bool[] _lastButtons = new bool[3];
  24. /// <summary> The down. </summary>
  25. private bool[] _down = new bool[3];
  26. /// <summary> The touch. </summary>
  27. private Vector2 _touch;
  28. /// <summary> True to physical button. </summary>
  29. private bool _physical_button;
  30. /// <summary> The current down button. </summary>
  31. private int _current_down_btn = -1;
  32. /// <summary> True to last physical button. </summary>
  33. private bool _last_physical_button;
  34. /// <summary> The precision. </summary>
  35. private const float PRECISION = 0.000001f;
  36. /// <summary> Parser controller state. </summary>
  37. /// <param name="state"> The state.</param>
  38. public void ParserControllerState(ControllerState state)
  39. {
  40. _last_physical_button = _physical_button;
  41. _physical_button = (Mathf.Abs(state.touchPos.x) > PRECISION || Mathf.Abs(state.touchPos.y) > PRECISION);
  42. var sysbtnState = NRVirtualDisplayer.SystemButtonState;
  43. if (NRVirtualDisplayer.displayMode == NRVirtualDisplayer.DisplayMode.AndroidNative)
  44. {
  45. lock (sysbtnState)
  46. {
  47. lock (_buttons)
  48. {
  49. for (int i = 0; i < sysbtnState.buttons.Length; i++)
  50. {
  51. _buttons[i] = sysbtnState.buttons[i];
  52. }
  53. }
  54. lock (_buttons_up)
  55. {
  56. lock (_buttons_down)
  57. {
  58. for (int i = 0; i < _buttons.Length; ++i)
  59. {
  60. _buttons_up[i] = (_lastButtons[i] & !_buttons[i]);
  61. _buttons_down[i] = (!_lastButtons[i] & _buttons[i]);
  62. }
  63. }
  64. }
  65. lock (_lastButtons)
  66. {
  67. for (int i = 0; i < _lastButtons.Length; i++)
  68. {
  69. _lastButtons[i] = _buttons[i];
  70. }
  71. }
  72. state.touchPos = NRVirtualDisplayer.SystemButtonState.touch;
  73. }
  74. }
  75. else
  76. {
  77. lock (sysbtnState)
  78. {
  79. lock (_buttons)
  80. {
  81. lock (_down)
  82. {
  83. for (int i = 0; i < _buttons.Length; ++i)
  84. {
  85. _down[i] = _buttons[i];
  86. }
  87. }
  88. if (_current_down_btn != -1)
  89. {
  90. _buttons[_current_down_btn] = _physical_button;
  91. if (!_buttons[_current_down_btn])
  92. _current_down_btn = -1;
  93. }
  94. else
  95. {
  96. _buttons[0] = false; //Trigger
  97. _buttons[1] = false; //App
  98. _buttons[2] = false; //Home
  99. for (int i = 0; i < sysbtnState.buttons.Length; i++)
  100. {
  101. _buttons[i] = sysbtnState.buttons[i];
  102. }
  103. _current_down_btn = -1;
  104. for (int i = 0; i < 3; i++)
  105. {
  106. if (_buttons[i])
  107. {
  108. _current_down_btn = i;
  109. break;
  110. }
  111. }
  112. }
  113. lock (_buttons_up)
  114. {
  115. lock (_buttons_down)
  116. {
  117. for (int i = 0; i < _buttons.Length; ++i)
  118. {
  119. _buttons_up[i] = (_down[i] & !_buttons[i]);
  120. _buttons_down[i] = (!_down[i] & _buttons[i]);
  121. }
  122. }
  123. }
  124. NRVirtualDisplayer.SystemButtonState.originTouch = state.touchPos;
  125. NRVirtualDisplayer.SystemButtonState.pressing = _physical_button;
  126. NRVirtualDisplayer.SystemButtonState.pressDown = (_physical_button && !_last_physical_button);
  127. NRVirtualDisplayer.SystemButtonState.pressUp = (!_physical_button && _last_physical_button);
  128. state.touchPos = NRVirtualDisplayer.SystemButtonState.touch;
  129. }
  130. }
  131. }
  132. state.isTouching = _buttons[0];
  133. if (!state.isTouching && !_buttons_up[0])
  134. {
  135. state.touchPos = Vector2.zero;
  136. }
  137. state.buttonsState =
  138. (_buttons[0] ? ControllerButton.TRIGGER : 0)
  139. | (_buttons[1] ? ControllerButton.APP : 0)
  140. | (_buttons[2] ? ControllerButton.HOME : 0);
  141. state.buttonsDown =
  142. (_buttons_down[0] ? ControllerButton.TRIGGER : 0)
  143. | (_buttons_down[1] ? ControllerButton.APP : 0)
  144. | (_buttons_down[2] ? ControllerButton.HOME : 0);
  145. state.buttonsUp =
  146. (_buttons_up[0] ? ControllerButton.TRIGGER : 0)
  147. | (_buttons_up[1] ? ControllerButton.APP : 0)
  148. | (_buttons_up[2] ? ControllerButton.HOME : 0);
  149. }
  150. }
  151. }