// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void HandleEvents() { // make sure all hotpoints de-active at first for (int i = 0; i < touchPoints.Length; ++i) { exHotPoint hotPoint = touchPoints[i]; hotPoint.active = false; hotPoint.pressDown = false; hotPoint.pressUp = false; } for (int i = 0; i < mousePoints.Length; ++i) { exHotPoint hotPoint = mousePoints[i]; hotPoint.active = false; hotPoint.pressDown = false; hotPoint.pressUp = false; } // if (hasMouse) { HandleMouse(); } if (hasTouch) { HandleTouches(); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void HandleTouches() { for (int i = 0; i < Input.touchCount; ++i) { Touch touch = Input.GetTouch(i); if (touch.fingerId >= 10) { continue; } exHotPoint hotPoint = touchPoints[touch.fingerId]; hotPoint.active = true; // we need clear all internal state when hotpoint is de-active if (hotPoint.active == false) { hotPoint.Reset(); } else { hotPoint.pos = touch.position; hotPoint.delta = touch.deltaPosition; Vector3 lastWorldPos = camera.ScreenToWorldPoint(touch.position - touch.deltaPosition); hotPoint.worldPos = camera.ScreenToWorldPoint(touch.position); hotPoint.worldDelta = hotPoint.worldPos - lastWorldPos; hotPoint.pressDown = (touch.phase == TouchPhase.Began); hotPoint.pressUp = (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended); } } HandleHotPoints(touchPoints, false); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void HandleHotPoints(exHotPoint[] _hotPoints, bool _isMouse) { // ======================================================== // handle hover event // ======================================================== int hotPointCountForMouse = _isMouse ? 1 : _hotPoints.Length; for (int i = 0; i < hotPointCountForMouse; ++i) { exHotPoint hotPoint = _hotPoints[i]; if (hotPoint.active == false) { continue; } if (hotPoint.pressed != null && hotPoint.pressed.grabMouseOrTouch) { continue; } // get hot control exUIControl lastCtrl = hotPoint.hover; exUIControl curCtrl = PickControl(hotPoint.pos); hotPoint.hover = curCtrl; if (lastCtrl != curCtrl) { exUIPointInfo pointInfo = new exUIPointInfo(); pointInfo.id = hotPoint.id; pointInfo.pos = hotPoint.pos; pointInfo.delta = hotPoint.delta; pointInfo.worldPos = hotPoint.worldPos; pointInfo.worldDelta = hotPoint.worldDelta; exUIPointEvent pointEvent = new exUIPointEvent(); pointEvent.isMouse = hotPoint.isMouse; pointEvent.pointInfos = new exUIPointInfo [] { pointInfo }; // on hover out if (lastCtrl != null) { lastCtrl.OnHoverOut(pointEvent); } // on hover in if (curCtrl != null) { pointEvent.Reset(); curCtrl.OnHoverIn(pointEvent); if (hotPoint.isTouch) { hotPoint.pressDown = true; } } } } if (_isMouse) { for (int i = 1; i < _hotPoints.Length; ++i) { _hotPoints[i].hover = _hotPoints[0].hover; } // ======================================================== // send scroll wheel event // ======================================================== float scroll = Input.GetAxis("Mouse ScrollWheel"); if (scroll != 0.0f && _hotPoints[0].hover != null) { exUIWheelEvent wheelEvent = new exUIWheelEvent(); wheelEvent.delta = scroll; _hotPoints[0].hover.OnEXMouseWheel(wheelEvent); } } // ======================================================== // handle press down event // ======================================================== for (int i = 0; i < _hotPoints.Length; ++i) { exHotPoint hotPoint = _hotPoints[i]; if (hotPoint.active == false) { continue; } if (hotPoint.pressDown) { exUIControl curCtrl = hotPoint.hover; if (hotPoint.pressed != null && hotPoint.pressed.grabMouseOrTouch) { curCtrl = hotPoint.pressed; } // send press down event if (curCtrl != null) { exUIPointInfo pointInfo = new exUIPointInfo(); pointInfo.id = hotPoint.id; pointInfo.pos = hotPoint.pos; pointInfo.delta = hotPoint.delta; pointInfo.worldPos = hotPoint.worldPos; pointInfo.worldDelta = hotPoint.worldDelta; exUIPointEvent pointEvent = new exUIPointEvent(); pointEvent.isMouse = hotPoint.isMouse; pointEvent.pointInfos = new exUIPointInfo [] { pointInfo }; curCtrl.OnPressDown(pointEvent); } hotPoint.pressed = curCtrl; } } // ======================================================== // handle moving before press-up // ======================================================== Dictionary <exUIControl, List <exHotPoint> > moveEvents = new Dictionary <exUIControl, List <exHotPoint> >(); // collect press move event for (int i = 0; i < _hotPoints.Length; ++i) { exHotPoint hotPoint = _hotPoints[i]; if (hotPoint.active == false) { continue; } if (hotPoint.delta != Vector2.zero) { exUIControl curCtrl = hotPoint.hover; if (hotPoint.pressed != null && hotPoint.pressed.grabMouseOrTouch) { curCtrl = hotPoint.pressed; } if (curCtrl != null) { List <exHotPoint> hotPointList = null; if (moveEvents.ContainsKey(curCtrl)) { hotPointList = moveEvents[curCtrl]; } else { hotPointList = new List <exHotPoint>(); moveEvents.Add(curCtrl, hotPointList); } hotPointList.Add(hotPoint); } } } // send hot-point move event foreach (KeyValuePair <exUIControl, List <exHotPoint> > iter in moveEvents) { exUIPointEvent pointEvent = new exUIPointEvent(); pointEvent.pointInfos = new exUIPointInfo [iter.Value.Count]; for (int i = 0; i < iter.Value.Count; ++i) { exHotPoint hotPoint = iter.Value[i]; exUIPointInfo pointInfo = new exUIPointInfo(); pointInfo.id = hotPoint.id; pointInfo.pos = hotPoint.pos; pointInfo.delta = hotPoint.delta; pointInfo.worldPos = hotPoint.worldPos; pointInfo.worldDelta = hotPoint.worldDelta; pointEvent.pointInfos[i] = pointInfo; pointEvent.isMouse = hotPoint.isMouse; } iter.Key.OnHoverMove(pointEvent); } // ======================================================== // handle press up event // ======================================================== for (int i = 0; i < _hotPoints.Length; ++i) { exHotPoint hotPoint = _hotPoints[i]; if (hotPoint.active == false) { continue; } // if (hotPoint.pressUp) { exUIPointInfo pointInfo = new exUIPointInfo(); pointInfo.id = hotPoint.id; pointInfo.pos = hotPoint.pos; pointInfo.delta = hotPoint.delta; pointInfo.worldPos = hotPoint.worldPos; pointInfo.worldDelta = hotPoint.worldDelta; exUIPointEvent pointEvent = new exUIPointEvent(); pointEvent.isMouse = hotPoint.isMouse; pointEvent.pointInfos = new exUIPointInfo [] { pointInfo }; exUIControl curCtrl = hotPoint.hover; if (hotPoint.pressed != null && hotPoint.pressed.grabMouseOrTouch) { curCtrl = hotPoint.pressed; } // send press down event if (curCtrl != null) { curCtrl.OnPressUp(pointEvent); if (hotPoint.isTouch) { curCtrl.OnHoverOut(pointEvent); } } hotPoint.pressed = null; } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void HoverOut(exUIControl _ctrl, int _id) { exHotPoint[] hotPoints = null; if (hasTouch || simulateMouseAsTouch) { hotPoints = touchPoints; } else { hotPoints = mousePoints; } // for (int i = 0; i < hotPoints.Length; ++i) { exHotPoint hotPoint = hotPoints[i]; if (hotPoint.active == false) { continue; } if (hotPoint.id != _id) { continue; } exUIPointInfo pointInfo = new exUIPointInfo(); pointInfo.id = hotPoint.id; pointInfo.pos = hotPoint.pos; pointInfo.delta = hotPoint.delta; pointInfo.worldPos = hotPoint.worldPos; pointInfo.worldDelta = hotPoint.worldDelta; exUIPointEvent pointEvent = new exUIPointEvent(); pointEvent.isMouse = hotPoint.isMouse; pointEvent.pointInfos = new exUIPointInfo [] { pointInfo }; // on hover out if (_ctrl != null) { _ctrl.OnHoverOut(pointEvent); } hotPoint.hover = null; // on hover in if (_ctrl.parent != null) { pointEvent.Reset(); hotPoint.hover = _ctrl.parent; _ctrl.parent.OnHoverIn(pointEvent); if (hotPoint.isTouch) { pointEvent.Reset(); hotPoint.pressed = _ctrl.parent; _ctrl.parent.OnPressDown(pointEvent); } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ // public void DispatchEvent ( exUIControl _sender, List<exUIEventListener> _eventListeners, exUIEvent _event ) { // if ( _eventListeners.Count <= 0 ) // return; // if ( _event.bubbles ) { // List<exUIControl> routine = GetRoutine( _sender ); // _event.target = _sender; // for ( int i = 0; i < _eventListeners.Count; ++i ) { // exUIEventListener listener = _eventListeners[i]; // // capture phase // if ( listener.capturePhase ) { // _event.eventPhase = exUIEventPhase.Capture; // for ( int j = routine.Count-1; j >= 0; --j ) { // _event.currentTarget = routine[j]; // listener.func ( _event ); // if ( _event.isPropagationStopped ) // break; // } // if ( _event.isPropagationStopped ) // continue; // } // // target phase // _event.eventPhase = exUIEventPhase.Target; // _event.currentTarget = _sender; // listener.func ( _event ); // if ( _event.isPropagationStopped ) // continue; // // bubble phase // _event.eventPhase = exUIEventPhase.Bubble; // for ( int j = 0; j < routine.Count; ++j ) { // _event.currentTarget = routine[j]; // listener.func ( _event ); // if ( _event.isPropagationStopped ) // break; // } // } // } // else { // _event.target = _sender; // for ( int i = 0; i < _eventListeners.Count; ++i ) { // exUIEventListener listener = _eventListeners[i]; // // target phase // _event.eventPhase = exUIEventPhase.Target; // _event.currentTarget = _sender; // listener.func ( _event ); // if ( _event.isPropagationStopped ) // break; // } // } // } // ------------------------------------------------------------------ // Desc: // NOTE: FindObjectsOfType() will not find deactived GameObjects, // so you need to manually add them to exUIMng // ------------------------------------------------------------------ void Init() { if (initialized) { return; } // if (camera == null) { Debug.LogError("The exUIMng should attach to a camera"); return; } // if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer #if UNITY_4_2 || Application.platform == RuntimePlatform.WP8Player || Application.platform == RuntimePlatform.BB10Player #endif ) { hasMouse = false; hasTouch = true; // hasKeyboard = false; // hasController = true; } else if (Application.platform == RuntimePlatform.PS3 || Application.platform == RuntimePlatform.XBOX360 ) { hasMouse = false; hasTouch = false; // hasKeyboard = false; // hasController = true; } else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor ) { hasMouse = true; hasTouch = false; // hasKeyboard = true; // hasController = true; } // for (int i = 0; i < 10; ++i) { touchPoints[i] = new exHotPoint(); touchPoints[i].Reset(); touchPoints[i].id = i; } for (int i = 0; i < 3; ++i) { mousePoints[i] = new exHotPoint(); mousePoints[i].Reset(); mousePoints[i].id = i; mousePoints[i].isMouse = true; } // find all controls in the scene, and add root controls to UIMng exUIControl[] allControls = FindObjectsOfType(typeof(exUIControl)) as exUIControl[]; for (int i = 0; i < allControls.Length; ++i) { exUIControl ctrl = allControls[i]; exUIControl parent_ctrl = exUIMng.FindParent(ctrl); if (parent_ctrl == null) { AddControl(ctrl); } } // initialized = true; }
/////////////////////////////////////////////////////////////////////////////// // Debug /////////////////////////////////////////////////////////////////////////////// public void ShowDebugInfo(Rect _pos) { GUILayout.BeginArea(new Rect(_pos.x, _pos.y, _pos.width, _pos.height), "Debug", GUI.skin.window); // Keyboard GUILayout.Label("Keyboard Focus: " + (focus ? focus.name : "None")); // Touch-Point State if (hasTouch || simulateMouseAsTouch) { for (int i = 0; i < touchPoints.Length; ++i) { exHotPoint hotPoint = touchPoints[i]; if (hotPoint.active == false) { continue; } GUILayout.Label("Touch[" + i + "]"); GUILayout.BeginHorizontal(); GUILayout.Space(15); GUILayout.BeginVertical(); GUILayout.Label("pos: " + hotPoint.pos.ToString()); GUILayout.Label("hover: " + (hotPoint.hover ? hotPoint.hover.name : "None")); GUILayout.Label("pressed: " + (hotPoint.pressed ? hotPoint.pressed.name : "None")); GUILayout.EndVertical(); GUILayout.EndHorizontal(); } } // Mouse-Point State if (hasMouse && simulateMouseAsTouch == false) { GUILayout.Label("Mouse"); GUILayout.BeginHorizontal(); GUILayout.Space(15); GUILayout.BeginVertical(); GUILayout.Label("pos: " + mousePoints[0].pos.ToString()); GUILayout.Label("hover: " + (mousePoints[0].hover ? mousePoints[0].hover.name : "None")); GUILayout.Label("left-pressed: " + (mousePoints[0].pressed ? mousePoints[0].pressed.name : "None")); GUILayout.Label("right-pressed: " + (mousePoints[1].pressed ? mousePoints[1].pressed.name : "None")); GUILayout.Label("middle-pressed: " + (mousePoints[2].pressed ? mousePoints[2].pressed.name : "None")); GUILayout.EndVertical(); GUILayout.EndHorizontal(); } // Root Controls GUILayout.Label("Root Controls"); GUILayout.BeginHorizontal(); GUILayout.Space(15); GUILayout.BeginVertical(); for (int i = 0; i < controls.Count; ++i) { GUILayout.Label("[" + i + "] " + controls[i].name); } GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUILayout.EndArea(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void HandleMouse() { if (simulateMouseAsTouch) { for (int i = 0; i < 3; ++i) { exHotPoint hotPoint = touchPoints[i]; hotPoint.id = i; // check if active the hotPoint hotPoint.active = Input.GetMouseButtonDown(i) || Input.GetMouseButton(i) || Input.GetMouseButtonUp(i); // we need clear all internal state when hotpoint is de-active if (hotPoint.active == false) { hotPoint.Reset(); } else { Vector2 lastMousePos = hotPoint.pos; hotPoint.pos = Input.mousePosition; if (Input.GetMouseButtonDown(i)) { hotPoint.delta = Vector2.zero; } else { hotPoint.delta = hotPoint.pos - lastMousePos; } Vector3 lastMouseWorldPos = hotPoint.worldPos; hotPoint.worldPos = camera.ScreenToWorldPoint(Input.mousePosition); hotPoint.worldDelta = hotPoint.worldPos - lastMouseWorldPos; hotPoint.pressDown = Input.GetMouseButtonDown(i); hotPoint.pressUp = Input.GetMouseButtonUp(i); } } HandleHotPoints(touchPoints, false); } else { for (int i = 0; i < 3; ++i) { exHotPoint hotPoint = mousePoints[i]; // check if active the hotPoint hotPoint.active = true; // we need clear all internal state when hotpoint is de-active if (hotPoint.active == false) { hotPoint.Reset(); } else { Vector2 lastMousePos = hotPoint.pos; hotPoint.pos = Input.mousePosition; hotPoint.delta = hotPoint.pos - lastMousePos; Vector3 lastMouseWorldPos = hotPoint.worldPos; hotPoint.worldPos = camera.ScreenToWorldPoint(Input.mousePosition); hotPoint.worldDelta = hotPoint.worldPos - lastMouseWorldPos; hotPoint.pressDown = Input.GetMouseButtonDown(i); hotPoint.pressUp = Input.GetMouseButtonUp(i); } } HandleHotPoints(mousePoints, true); } }