VirtualInput.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityStandardAssets.CrossPlatformInput
  5. {
  6. public abstract class VirtualInput
  7. {
  8. public Vector3 virtualMousePosition { get; private set; }
  9. protected Dictionary<string, CrossPlatformInputManager.VirtualAxis> m_VirtualAxes =
  10. new Dictionary<string, CrossPlatformInputManager.VirtualAxis>();
  11. // Dictionary to store the name relating to the virtual axes
  12. protected Dictionary<string, CrossPlatformInputManager.VirtualButton> m_VirtualButtons =
  13. new Dictionary<string, CrossPlatformInputManager.VirtualButton>();
  14. protected List<string> m_AlwaysUseVirtual = new List<string>();
  15. // list of the axis and button names that have been flagged to always use a virtual axis or button
  16. public bool AxisExists(string name)
  17. {
  18. return m_VirtualAxes.ContainsKey(name);
  19. }
  20. public bool ButtonExists(string name)
  21. {
  22. return m_VirtualButtons.ContainsKey(name);
  23. }
  24. public void RegisterVirtualAxis(CrossPlatformInputManager.VirtualAxis axis)
  25. {
  26. // check if we already have an axis with that name and log and error if we do
  27. if (m_VirtualAxes.ContainsKey(axis.name))
  28. {
  29. Debug.LogError("There is already a virtual axis named " + axis.name + " registered.");
  30. }
  31. else
  32. {
  33. // add any new axes
  34. m_VirtualAxes.Add(axis.name, axis);
  35. // if we dont want to match with the input manager setting then revert to always using virtual
  36. if (!axis.matchWithInputManager)
  37. {
  38. m_AlwaysUseVirtual.Add(axis.name);
  39. }
  40. }
  41. }
  42. public void RegisterVirtualButton(CrossPlatformInputManager.VirtualButton button)
  43. {
  44. // check if already have a buttin with that name and log an error if we do
  45. if (m_VirtualButtons.ContainsKey(button.name))
  46. {
  47. Debug.LogError("There is already a virtual button named " + button.name + " registered.");
  48. }
  49. else
  50. {
  51. // add any new buttons
  52. m_VirtualButtons.Add(button.name, button);
  53. // if we dont want to match to the input manager then always use a virtual axis
  54. if (!button.matchWithInputManager)
  55. {
  56. m_AlwaysUseVirtual.Add(button.name);
  57. }
  58. }
  59. }
  60. public void UnRegisterVirtualAxis(string name)
  61. {
  62. // if we have an axis with that name then remove it from our dictionary of registered axes
  63. if (m_VirtualAxes.ContainsKey(name))
  64. {
  65. m_VirtualAxes.Remove(name);
  66. }
  67. }
  68. public void UnRegisterVirtualButton(string name)
  69. {
  70. // if we have a button with this name then remove it from our dictionary of registered buttons
  71. if (m_VirtualButtons.ContainsKey(name))
  72. {
  73. m_VirtualButtons.Remove(name);
  74. }
  75. }
  76. // returns a reference to a named virtual axis if it exists otherwise null
  77. public CrossPlatformInputManager.VirtualAxis VirtualAxisReference(string name)
  78. {
  79. return m_VirtualAxes.ContainsKey(name) ? m_VirtualAxes[name] : null;
  80. }
  81. public void SetVirtualMousePositionX(float f)
  82. {
  83. virtualMousePosition = new Vector3(f, virtualMousePosition.y, virtualMousePosition.z);
  84. }
  85. public void SetVirtualMousePositionY(float f)
  86. {
  87. virtualMousePosition = new Vector3(virtualMousePosition.x, f, virtualMousePosition.z);
  88. }
  89. public void SetVirtualMousePositionZ(float f)
  90. {
  91. virtualMousePosition = new Vector3(virtualMousePosition.x, virtualMousePosition.y, f);
  92. }
  93. public abstract float GetAxis(string name, bool raw);
  94. public abstract bool GetButton(string name);
  95. public abstract bool GetButtonDown(string name);
  96. public abstract bool GetButtonUp(string name);
  97. public abstract void SetButtonDown(string name);
  98. public abstract void SetButtonUp(string name);
  99. public abstract void SetAxisPositive(string name);
  100. public abstract void SetAxisNegative(string name);
  101. public abstract void SetAxisZero(string name);
  102. public abstract void SetAxis(string name, float value);
  103. public abstract Vector3 MousePosition();
  104. }
  105. }