public void InputUpdate() { MouseScroll = Vector2.zero; KeyEvents.Clear(); delayedResize = new Vector2(float.NaN, float.NaN); lock (messages) { for (int i = 0; i < messages.Count; i++) { HandleMessage(messages[i]); } messages.Clear(); } if (!float.IsNaN(delayedResize.x)) { browser.Resize((int)delayedResize.x, (int)delayedResize.y); } }
private void HandleMessage(string message) { var msg = JSONNode.Parse(message); switch ((string)msg["type"]) { case "mouseDown": case "mouseUp": { int button = msg["button"]; MouseButton flag = 0; if (button == 0) { flag = MouseButton.Left; } else if (button == 1) { flag = MouseButton.Right; } else if (button == 2) { flag = MouseButton.Middle; } if (msg["type"] == "mouseDown") { MouseButtons |= flag; } else { MouseButtons &= ~flag; } break; } case "mouseMove": { var screenPos = new Vector2(msg["x"], msg["y"]); screenPos.x = screenPos.x / browser.Size.x; screenPos.y = screenPos.y / browser.Size.y; MousePosition = screenPos; //Debug.Log("mouse now at " + screenPos); break; } case "mouseFocus": MouseHasFocus = msg["focus"]; break; case "keyboardFocus": KeyboardHasFocus = msg["focus"]; break; case "mouseScroll": { const float mult = 1 / 3f; MouseScroll += new Vector2(msg["x"], msg["y"]) * mult; break; } case "keyDown": case "keyUp": { var ev = new Event(); ev.type = msg["type"] == "keyDown" ? EventType.KeyDown : EventType.KeyUp; ev.character = (char)0; ev.keyCode = KeyMappings.GetUnityKeyCode(msg["code"]); SetMods(ev); //Debug.Log("Convert wkc " + (int)msg["code"] + " to ukc " + ev.keyCode); KeyEvents.Add(ev); break; } case "keyPress": { string characters = msg["characters"]; foreach (char c in characters) { var ev = new Event(); ev.type = EventType.KeyDown; SetMods(ev); ev.character = c; ev.keyCode = 0; KeyEvents.Add(ev); } break; } case "resize": //on OS X (editor at least), resizing hangs the update loop, so we suddenly end up with a bajillion resize //messages we were unable to process. Just record it here and when we've processed everything we'll resize delayedResize.x = msg["w"]; delayedResize.y = msg["h"]; break; case "close": Destroy(gameObject); break; default: Debug.LogWarning("Unknown window event: " + msg.AsJSON); break; } }