// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void StartDrag(exUIEvent _event) { exUIPointEvent pointEvent = _event as exUIPointEvent; if (draggable && (pointEvent.isTouch || pointEvent.GetMouseButton(0))) { dragging = true; draggingID = pointEvent.mainPoint.id; damping = false; spring = false; velocity = Vector2.zero; exUIMng.inst.SetFocus(this); _event.StopPropagation(); } }
/////////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void Awake() { base.Awake(); grabMouseOrTouch = true; if (contentAnchor != null) { originalAnchorPos = contentAnchor.localPosition; } AddEventListener("onPressDown", delegate(exUIEvent _event) { if (dragging) { _event.StopPropagation(); return; } StartDrag(_event); }); AddEventListener("onPressUp", delegate(exUIEvent _event) { exUIPointEvent pointEvent = _event as exUIPointEvent; if (draggable && (pointEvent.isTouch || pointEvent.GetMouseButton(0)) && pointEvent.pointInfos[0].id == draggingID) { if (dragging) { dragging = false; draggingID = -1; StartScroll(); } _event.StopPropagation(); } }); AddEventListener("onHoverIn", delegate(exUIEvent _event) { if (dragging) { _event.StopPropagation(); return; } StartDrag(_event); }); AddEventListener("onHoverMove", delegate(exUIEvent _event) { if (dragging == false) { StartDrag(_event); return; } float contentX = (horizontalContentDir == ContentDirection.LeftToRight) ? 0.0f : -(contentSize_.x - width); float contentY = (verticalContentDir == ContentDirection.TopToBottom) ? 0.0f : -(contentSize_.y - height); Rect scrollRect = new Rect(scrollOffset_.x, scrollOffset_.y, width, height); Rect contentRect = new Rect(contentX, contentY, Mathf.Max(contentSize_.x, width), Mathf.Max(contentSize_.y, height)); Vector2 constrainOffset = exGeometryUtility.GetConstrainOffset(scrollRect, contentRect); exUIPointEvent pointEvent = _event as exUIPointEvent; for (int i = 0; i < pointEvent.pointInfos.Length; ++i) { exUIPointInfo point = pointEvent.pointInfos[i]; if (draggable && (pointEvent.isTouch || pointEvent.GetMouseButton(0)) && point.id == draggingID) { Vector2 delta = point.worldDelta; delta.x = -delta.x; if (Mathf.Abs(constrainOffset.x) > 0.001f) { delta.x *= 0.5f; } if (Mathf.Abs(constrainOffset.y) > 0.001f) { delta.y *= 0.5f; } // velocity = Vector2.Lerp(velocity, velocity + (delta / Time.deltaTime) * scrollSpeed, 0.67f); if (Mathf.Sign(velocity.x) != Mathf.Sign(delta.x)) { velocity.x = 0.0f; } if (Mathf.Sign(velocity.y) != Mathf.Sign(delta.y)) { velocity.y = 0.0f; } _event.StopPropagation(); Scroll(delta); break; } } }); AddEventListener("onMouseWheel", delegate(exUIEvent _event) { // TODO: if ( mouseWheelByHorizontal ) float contentX = (horizontalContentDir == ContentDirection.LeftToRight) ? 0.0f : -(contentSize_.x - width); float contentY = (verticalContentDir == ContentDirection.TopToBottom) ? 0.0f : -(contentSize_.y - height); exUIWheelEvent wheelEvent = _event as exUIWheelEvent; Vector2 delta = new Vector2(0.0f, -wheelEvent.delta * 100.0f); Vector2 constrainOffset = exGeometryUtility.GetConstrainOffset(new Rect(scrollOffset_.x, scrollOffset_.y, width, height), new Rect(contentX, contentY, contentSize_.x, contentSize_.y)); if (Mathf.Abs(constrainOffset.y) > 0.001f) { delta.y *= 0.5f; } velocity = Vector2.Lerp(velocity, velocity + (delta / Time.deltaTime) * scrollSpeed, 0.67f); if (Mathf.Sign(velocity.x) != Mathf.Sign(delta.x)) { velocity.x = 0.0f; } if (Mathf.Sign(velocity.y) != Mathf.Sign(delta.y)) { velocity.y = 0.0f; } _event.StopPropagation(); Scroll(delta); StartScroll(); }); }
/////////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void Awake() { base.Awake(); // handle scroll bar Transform transBar = transform.Find("__bar"); if (transBar) { bar = transBar.GetComponent <exSprite>(); if (bar) { bar.customSize = true; bar.anchor = Anchor.TopLeft; } // exUIButton btnBar = transBar.GetComponent <exUIButton>(); if (btnBar) { btnBar.grabMouseOrTouch = true; btnBar.AddEventListener("onPressDown", delegate(exUIEvent _event) { if (dragging) { return; } exUIPointEvent pointEvent = _event as exUIPointEvent; if (pointEvent.isTouch || pointEvent.GetMouseButton(0)) { dragging = true; draggingID = pointEvent.mainPoint.id; exUIMng.inst.SetFocus(this); } }); btnBar.AddEventListener("onPressUp", delegate(exUIEvent _event) { exUIPointEvent pointEvent = _event as exUIPointEvent; if ((pointEvent.isTouch || pointEvent.GetMouseButton(0)) && pointEvent.pointInfos[0].id == draggingID) { if (dragging) { dragging = false; draggingID = -1; } } }); btnBar.AddEventListener("onHoverMove", delegate(exUIEvent _event) { if (scrollView) { exUIPointEvent pointEvent = _event as exUIPointEvent; for (int i = 0; i < pointEvent.pointInfos.Length; ++i) { exUIPointInfo point = pointEvent.pointInfos[i]; if (dragging && (pointEvent.isTouch || pointEvent.GetMouseButton(0)) && point.id == draggingID) { Vector2 delta = point.worldDelta; delta.y = -delta.y; scrollView.Scroll(delta / ratio); } } } }); } } // handle background background = GetComponent <exSprite>(); if (background) { scrollStart = transBar ? transBar.localPosition : Vector3.zero; if (background.spriteType == exSpriteType.Sliced) { if (direction == Direction.Horizontal) { scrollStart.x = background.leftBorderSize; } else { scrollStart.y = background.topBorderSize; } } } // handle scroll view if (scrollView) { scrollView.AddEventListener("onContentResized", delegate(exUIEvent _event) { UpdateScrollBarRatio(); UpdateScrollBar(); }); scrollView.AddEventListener("onScroll", delegate(exUIEvent _event) { if (direction == Direction.Horizontal) { scrollOffset = scrollView.scrollOffset.x * ratio; } else { scrollOffset = scrollView.scrollOffset.y * ratio; } UpdateScrollBar(); // if (scrollView.showCondition == exUIScrollView.ShowCondition.WhenDragging) { activeSelf = true; } }); scrollView.AddEventListener("onScrollFinished", delegate(exUIEvent _event) { if (scrollView.showCondition == exUIScrollView.ShowCondition.WhenDragging) { cooldownTimer = cooldown; isCoolingDown = true; } }); UpdateScrollBarRatio(); UpdateScrollBar(); // handle scrollbar effect if (scrollView.showCondition == exUIScrollView.ShowCondition.WhenDragging) { // exUIEffect effect = GetComponent <exUIEffect>(); if (effect == null) { effect = gameObject.AddComponent <exUIEffect>(); if (background != null) { effect.AddEffect_Color(background, EffectEventType.Deactive, exEase.Type.Linear, new Color(background.color.r, background.color.g, background.color.b, 0.0f), 0.5f); } if (bar != null) { effect.AddEffect_Color(bar, EffectEventType.Deactive, exEase.Type.Linear, new Color(bar.color.r, bar.color.g, bar.color.b, 0.0f), 0.5f); } } // if (background) { Color tmpColor = background.color; tmpColor.a = 0.0f; background.color = tmpColor; } // if (bar) { Color tmpColor = bar.color; tmpColor.a = 0.0f; bar.color = tmpColor; } // active_ = false; } } }
/////////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected new void Awake() { base.Awake(); AddEventListener("onPressDown", delegate(exUIEvent _event) { if (pressing) { return; } exUIPointEvent pointEvent = _event as exUIPointEvent; if (pointEvent.isTouch || pointEvent.GetMouseButton(0)) { pressing = true; pressDownAt = pointEvent.mainPoint.pos; pressingID = pointEvent.mainPoint.id; exUIMng.inst.SetFocus(this); exUIEvent evtButtonDown = new exUIEvent(); evtButtonDown.bubbles = false; OnButtonDown(evtButtonDown); _event.StopPropagation(); } }); AddEventListener("onPressUp", delegate(exUIEvent _event) { exUIPointEvent pointEvent = _event as exUIPointEvent; if (pointEvent.isTouch || pointEvent.GetMouseButton(0)) { exUIEvent evtButtonUp = new exUIEvent(); evtButtonUp.bubbles = false; OnButtonUp(evtButtonUp); if (pressing) { pressing = false; exUIEvent evtClick = new exUIEvent(); evtClick.bubbles = false; OnClick(evtClick); _event.StopPropagation(); } } }); AddEventListener("onHoverOut", delegate(exUIEvent _event) { if (pressing) { pressing = false; pressingID = -1; _event.StopPropagation(); } }); AddEventListener("onHoverMove", delegate(exUIEvent _event) { if (pressing) { exUIPointEvent pointEvent = _event as exUIPointEvent; for (int i = 0; i < pointEvent.pointInfos.Length; ++i) { exUIPointInfo point = pointEvent.pointInfos[i]; if (point.id == pressingID) { Vector2 delta = pointEvent.mainPoint.pos - pressDownAt; if (allowDrag == false && delta.sqrMagnitude >= dragThreshold * dragThreshold) { exUIMng.inst.HoverOut(this, point.id); } else { _event.StopPropagation(); } } } } }); }
// ------------------------------------------------------------------ // 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); } } } }