IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
MouseInputDevice.cs
1 using UnityEngine;
2 using System.Collections;
3 using System;
4 using UnityEngine.EventSystems;
5 using UI;
6 #if UNITY_EDITOR
7 using UnityEditor;
8 #endif
9 
10 public class MouseInputDevice : MonoBehaviour, InputDevice {
11 
12  public InputDeviceManager.InputDeviceType getDeviceType ()
13  {
14  return InputDeviceManager.InputDeviceType.Mouse;
15  }
16 
17  public bool developmentMode = true;
18  public Vector3 rayOriginOffset;
19 
20  private LineRenderer lineRenderer;
21 
22  public float mouseSpeed = 1.4f;
23 
24  private Vector2 texCoordDelta;
25  private Vector3 positionDelta;
26 
27  private Vector3 rayAngle = new Vector3( 0, 0, 0 );
28 
29  private ButtonInfo buttonInfo = new ButtonInfo();
30 
31  public Ray createRay()
32  {
33 
34  Ray ray;
35  if(developmentMode){
36  ray = Camera.main.ScreenPointToRay(Input.mousePosition);
37  }
38  else{
39  rayAngle = rayAngle + new Vector3 ( -Input.GetAxis("Mouse Y") * mouseSpeed, Input.GetAxis("Mouse X") * mouseSpeed, 0f );
40  Vector3 rayDir = Quaternion.Euler (rayAngle) * Vector3.forward;
41 
42  ray = new Ray(Camera.main.transform.position + rayOriginOffset, rayDir);
43  }
44  return ray;
45  }
46 
47  public Vector2 getScrollDelta()
48  {
49  return Input.mouseScrollDelta*20f;
50  }
51 
52  void Start () {
53  lineRenderer = this.GetComponent<LineRenderer> ();
54  if (lineRenderer == null) {
55  Debug.LogError ("[MouseInput.cs] Line renderer not set");
56  }
57 
58  if (InputDeviceManager.instance != null) {
59  InputDeviceManager.instance.registerInputDevice (this);
60  Debug.Log ("Mouse registered");
61  }
62  }
63 
64  public void Update() {
65  if (!developmentMode && Input.GetKey ("escape")) {
66  Cursor.lockState = CursorLockMode.None;
67  }
68  }
69 
70  public void OnEnable() {
71  if (!developmentMode) {
72  Cursor.lockState = CursorLockMode.Locked;
73  }
74  }
75 
76  public bool isLeftButtonDown()
77  {
78  return Input.GetMouseButton (0);
79  }
80  public bool isRightButtonDown()
81  {
82  return Input.GetMouseButton (1);
83  }
84  public bool isMiddleButtonDown()
85  {
86  return Input.GetMouseButton (2);
87  }
88 
89  public ButtonInfo updateButtonInfo ()
90  {
91 
92  if (Input.GetMouseButtonDown (0) && Input.GetMouseButtonUp (0)) {
93  buttonInfo.buttonStates [ButtonType.Left] = PointerEventData.FramePressState.PressedAndReleased;
94  } else if (Input.GetMouseButtonDown (0)) {
95  buttonInfo.buttonStates [ButtonType.Left] = PointerEventData.FramePressState.Pressed;
96  } else if (Input.GetMouseButtonUp (0)) {
97  buttonInfo.buttonStates [ButtonType.Left] = PointerEventData.FramePressState.Released;
98  } else {
99  buttonInfo.buttonStates [ButtonType.Left] = PointerEventData.FramePressState.NotChanged;
100  }
101 
102  if (Input.GetMouseButtonDown (1) && Input.GetMouseButtonUp (1)) {
103  buttonInfo.buttonStates [ButtonType.Right] = PointerEventData.FramePressState.PressedAndReleased;
104  } else if (Input.GetMouseButtonDown (1)) {
105  buttonInfo.buttonStates [ButtonType.Right] = PointerEventData.FramePressState.Pressed;
106  } else if (Input.GetMouseButtonUp (1)) {
107  buttonInfo.buttonStates [ButtonType.Right] = PointerEventData.FramePressState.Released;
108  } else {
109  buttonInfo.buttonStates [ButtonType.Right] = PointerEventData.FramePressState.NotChanged;
110  }
111 
112  if (Input.GetMouseButtonDown (2) && Input.GetMouseButtonUp (2)) {
113  buttonInfo.buttonStates [ButtonType.Middle] = PointerEventData.FramePressState.PressedAndReleased;
114  } else if (Input.GetMouseButtonDown (2)) {
115  buttonInfo.buttonStates [ButtonType.Middle] = PointerEventData.FramePressState.Pressed;
116  } else if (Input.GetMouseButtonUp (2)) {
117  buttonInfo.buttonStates [ButtonType.Middle] = PointerEventData.FramePressState.Released;
118  } else {
119  buttonInfo.buttonStates [ButtonType.Middle] = PointerEventData.FramePressState.NotChanged;
120  }
121 
122  return buttonInfo;
123  }
124 
125  public Camera getEventCamera()
126  {
127  return Camera.main;
128  }
129 
131  public Vector2 getTexCoordDelta() {
132  return texCoordDelta;
133  }
134  public Vector3 get3DDelta() {
135  return positionDelta;
136  }
137 
138  public void setTexCoordDelta( Vector2 delta ) {
139  texCoordDelta = delta;
140  }
141  public void set3DDelta( Vector2 delta ) {
142  positionDelta = delta;
143  }
144 }
145 
146 #if UNITY_EDITOR
147 [CustomEditor(typeof(MouseInputDevice))]
149 public class MouseInputDeviceEditor : Editor
150 {
151  public override void OnInspectorGUI()
152  {
153  MouseInputDevice mid = target as MouseInputDevice;
154 
155  mid.developmentMode = GUILayout.Toggle(mid.developmentMode, "Development Mode");
156 
157  if (!mid.developmentMode) {
158  mid.rayOriginOffset = EditorGUILayout.Vector3Field ("Ray Origin Offset", new Vector3 (0.2f, -0.3f, 0f));
159  mid.mouseSpeed = EditorGUILayout.FloatField ("Mouse Speed", 1.5f);
160  Cursor.lockState = CursorLockMode.Locked;
161  } else {
162  mid.rayOriginOffset = Vector3.zero;
163  mid.mouseSpeed = 1.5f;
164  Cursor.lockState = CursorLockMode.None;
165  }
166  }
167 }
168 #endif
Vector2 getTexCoordDelta()
Returns the difference in texture coordinates between this and the last frame: