// ----------------- override protected void OnPress(Vector2 startPos, Vector2 pos, float delay, bool startedByMouse, bool isPressureSensitive, float pressure) { base.OnPress(startPos, pos, delay, startedByMouse, isPressureSensitive, pressure); this.blockDrag = false; this.moved = false; this.justMoved = false; this.nonStaticFlag = false; this.constrainedVecCur = this.constrainedVecPrev = Vector2.zero; this.scrollVecCur = this.scrollVecPrev = Vector2.zero; this.segmentOrigin = this.posCurSmooth; this.scrollConstraint = TouchGestureConfig.DirConstraint.None; this.swipeConstraint = TouchGestureConfig.DirConstraint.None; this.swipeDirConstraint = TouchGestureConfig.DirConstraint.None; if (this.config != null) { this.scrollConstraint = this.config.scrollConstraint; this.swipeConstraint = this.config.swipeConstraint; this.swipeDirConstraint = this.config.swipeDirConstraint; } this.swipeDirState.Reset(); this.swipeDirState4.Reset(); this.swipeDirState8.Reset(); }
// ---------------- override public void Reset() { base.Reset(); this.swipeConstraint = TouchGestureConfig.DirConstraint.None; this.scrollConstraint = TouchGestureConfig.DirConstraint.None; this.swipeDirConstraint = TouchGestureConfig.DirConstraint.None; this.constrainedVecCur = Vector2.zero; this.constrainedVecPrev = Vector2.zero; this.tapCount = 0; this.tapCandidateCount = 0; this.elapsedSincePress = 0; this.elapsedSinceRelease = 0; this.tapCandidateReleased = false; this.moved = false; this.justMoved = false; this.justTapped = false; this.justLongTapped = false; this.cleanPressCur = false; this.cleanPressPrev = false; this.holdPressCur = false; this.holdPressPrev = false; this.flushTapsFlag = false; this.nonStaticFlag = false; this.cancelTapFlag = false; this.disableTapUntilReleaseFlag = false; this.swipeDirState.Reset(); this.swipeDirState4.Reset(); this.swipeDirState8.Reset(); }
// ----------------- override protected void CheckMovement(bool itsFinalUpdate) { base.CheckMovement(itsFinalUpdate); if ((this.thresh == null) || (this.config == null)) { return; } if (!this.PressedRaw()) { this.cleanPressCur = false; this.holdPressCur = false; } else { // Detect long-press... bool longPressStillPossible = false; if (this.holdPressCur) { // End long-press on move? if (this.config.endLongPressWhenMoved && (this.nonStaticFlag || this.Moved(this.thresh.tapMoveThreshPxSq))) { this.holdPressCur = false; } // End long-press on swipe? else if (this.config.endLongPressWhenSwiped && this.Swiped()) { this.holdPressCur = false; } else { // TODO check for long tap } } else if (this.config.detectLongPress) { if (!this.nonStaticFlag && !this.Moved(this.thresh.tapMoveThreshPxSq)) { if (this.elapsedSincePress > this.thresh.longPressMinTime) { this.holdPressCur = true; } else { longPressStillPossible = true; } } } // Detect normal press... if (this.cleanPressCur) { } else if (!this.holdPressCur && !longPressStillPossible && !this.IsPotentialTap()) { this.cleanPressCur = true; } } // Calculate scroll vector... Vector2 dragVecCur = this.GetSwipeVecRaw(); if (this.scrollConstraint != TouchGestureConfig.DirConstraint.None) { if (this.scrollConstraint == TouchGestureConfig.DirConstraint.Auto) { if (Mathf.Abs(dragVecCur.x) > this.thresh.scrollThreshPx) { this.scrollConstraint = TouchGestureConfig.DirConstraint.Horizontal; } if ((Mathf.Abs(dragVecCur.y) > this.thresh.scrollThreshPx) && (Mathf.Abs(dragVecCur.y) > Mathf.Abs(dragVecCur.x))) { this.scrollConstraint = TouchGestureConfig.DirConstraint.Vertical; } } if (this.scrollConstraint == TouchGestureConfig.DirConstraint.Horizontal) { dragVecCur.y = 0; } else if (this.scrollConstraint == TouchGestureConfig.DirConstraint.Vertical) { dragVecCur.x = 0; } } for (int axisi = 0; axisi < 2; ++axisi) { this.scrollVecCur[axisi] = CFUtils.GetScrollValue(dragVecCur[axisi], (int)this.scrollVecCur[axisi], this.thresh.scrollThreshPx, this.thresh.scrollMagnetFactor); } // Determinate swipe constraint... this.constrainedVecCur = this.GetSwipeVecSmooth(); if (this.swipeConstraint != TouchGestureConfig.DirConstraint.None) { if (this.swipeConstraint == TouchGestureConfig.DirConstraint.Auto) { if (Mathf.Abs(this.extremeDistPerAxisCur.x) > this.thresh.dragThreshPx) { this.swipeConstraint = TouchGestureConfig.DirConstraint.Horizontal; } if ((Mathf.Abs(this.extremeDistPerAxisCur.y) > this.thresh.dragThreshPx) && (Mathf.Abs(this.extremeDistPerAxisCur.y) > Mathf.Abs(this.extremeDistPerAxisCur.x))) { this.swipeConstraint = TouchGestureConfig.DirConstraint.Vertical; } } if (this.swipeConstraint == TouchGestureConfig.DirConstraint.Horizontal) { this.constrainedVecCur.y = 0; } else if (this.swipeConstraint == TouchGestureConfig.DirConstraint.Vertical) { this.constrainedVecCur.x = 0; } else { this.constrainedVecCur = Vector2.zero; } } // Swipe check... if (!this.moved && !this.blockDrag) { if ((this.constrainedVecCur.sqrMagnitude > this.thresh.dragThreshPxSq)) { this.OnSwipeStart(); } } // Swipe direction... Vector2 swipeSegVec = (this.posCurSmooth - this.segmentOrigin); if (this.swipeDirConstraint != TouchGestureConfig.DirConstraint.None) { if (this.swipeDirConstraint == TouchGestureConfig.DirConstraint.Auto) { if (Mathf.Abs(swipeSegVec.x) > this.thresh.swipeSegLenPx) { this.swipeDirConstraint = TouchGestureConfig.DirConstraint.Horizontal; } if ((Mathf.Abs(swipeSegVec.y) > this.thresh.swipeSegLenPx) && (Mathf.Abs(swipeSegVec.y) > Mathf.Abs(swipeSegVec.x))) { this.swipeDirConstraint = TouchGestureConfig.DirConstraint.Vertical; } } if (this.swipeDirConstraint == TouchGestureConfig.DirConstraint.Horizontal) { swipeSegVec.y = 0; } else if (this.swipeDirConstraint == TouchGestureConfig.DirConstraint.Vertical) { swipeSegVec.x = 0; } else { swipeSegVec = Vector2.zero; } } float swipeSegVecLenSq = swipeSegVec.sqrMagnitude; if (swipeSegVecLenSq > this.thresh.swipeSegLenPxSq) { swipeSegVec.Normalize(); this.swipeDirState4.SetDir(CFUtils.VecToDir(swipeSegVec, this.swipeDirState4.GetCur(), 0.1f, false), config.swipeOriginalDirResetMode); this.swipeDirState8.SetDir(CFUtils.VecToDir(swipeSegVec, this.swipeDirState8.GetCur(), 0.1f, true), config.swipeOriginalDirResetMode); this.swipeDirState.SetDir(((config.dirMode == TouchGestureConfig.DirMode.EightWay) ? this.swipeDirState8 : this.swipeDirState4).GetCur(), config.swipeOriginalDirResetMode); this.segmentOrigin = this.posCurSmooth; } }
// --------------- public void DrawGUI(TouchGestureConfig config) { int maxTapCount = config.maxTapCount; bool cleanTapsOnly = config.cleanTapsOnly, detectLongTap = config.detectLongTap, detectLongPress = config.detectLongPress, endLongPressWhenMoved = config.endLongPressWhenMoved, endLongPressWhenSwiped = config.endLongPressWhenSwiped; TouchGestureConfig.DirMode dirMode = config.dirMode; TouchGestureConfig.DirConstraint swipeConstraint = config.swipeConstraint, swipeDirConstraint = config.swipeDirConstraint, scrollConstraint = config.scrollConstraint; DirectionState.OriginalDirResetMode swipeOriginalDirResetMode = config.swipeOriginalDirResetMode; // GUI... InspectorUtils.BeginIndentedSection(this.titleContent); maxTapCount = CFGUI.IntSlider(new GUIContent("Max Number of Taps", "Maximal number of consecutive taps to detect.\nWhen \'Report All Taps\' option is turned off, system will wait for potential follow-up taps so they may be reported with slight delay."), maxTapCount, 0, 5, LABEL_WIDTH); dirMode = (TouchGestureConfig.DirMode)CFGUI.EnumPopup(new GUIContent("Swipe Dir. Mode", "Swipe Segmwnt Direction Mode"), dirMode, LABEL_WIDTH); swipeOriginalDirResetMode = (DirectionState.OriginalDirResetMode)CFGUI.EnumPopup(new GUIContent("Swipe Dir. Reset Mode", "Swipe Segment's Original Direction Reset Mode - choose when original direction is reset. This option is used by Direction Binding in ORIGINAL group modes..."), swipeOriginalDirResetMode, LABEL_WIDTH); swipeDirConstraint = (TouchGestureConfig.DirConstraint)CFGUI.EnumPopup(new GUIContent("Scroll Dir Constraint", "Scroll Direction Constraint Mode"), swipeDirConstraint, LABEL_WIDTH); swipeConstraint = (TouchGestureConfig.DirConstraint)CFGUI.EnumPopup(new GUIContent("Swipe Constraint", "Swipe Constraint Mode"), swipeConstraint, LABEL_WIDTH); scrollConstraint = (TouchGestureConfig.DirConstraint)CFGUI.EnumPopup(new GUIContent("Scroll Constraint", "Scroll Constraint Mode"), scrollConstraint, LABEL_WIDTH); cleanTapsOnly = EditorGUILayout.ToggleLeft(new GUIContent("Clean Taps Only", "When turned on, only clean taps will be reported."), cleanTapsOnly); detectLongPress = EditorGUILayout.ToggleLeft(new GUIContent("Detect Long Press", "Long Press is a static touch (tap threshold) pressed for some time (Long Press Min. Duration)."), detectLongPress); if (detectLongPress) { detectLongTap = EditorGUILayout.ToggleLeft(new GUIContent("Detect Long Tap", ""), detectLongTap); endLongPressWhenMoved = EditorGUILayout.ToggleLeft(new GUIContent("End Long Press When Moved", "End Long Press (and start Normal Press) when touch moved past the Tap Threshold."), endLongPressWhenMoved); endLongPressWhenSwiped = EditorGUILayout.ToggleLeft(new GUIContent("End Long Press When Swiped", "End Long Press (and start Normal Press) when touch moved past the Swipe Threshold."), endLongPressWhenSwiped); } InspectorUtils.EndIndentedSection(); // Register Undo... if ((maxTapCount != config.maxTapCount) || (cleanTapsOnly != config.cleanTapsOnly) || (detectLongTap != config.detectLongTap) || (detectLongPress != config.detectLongPress) || (dirMode != config.dirMode) || (swipeConstraint != config.swipeConstraint) || (swipeDirConstraint != config.swipeDirConstraint) || (scrollConstraint != config.scrollConstraint) || (swipeOriginalDirResetMode != config.swipeOriginalDirResetMode) || (endLongPressWhenSwiped != config.endLongPressWhenSwiped) || (endLongPressWhenMoved != config.endLongPressWhenMoved)) { CFGUI.CreateUndo("Gesture Config modification", this.undoObject); config.dirMode = dirMode; config.swipeConstraint = swipeConstraint; config.swipeDirConstraint = swipeDirConstraint; config.scrollConstraint = scrollConstraint; config.maxTapCount = maxTapCount; config.cleanTapsOnly = cleanTapsOnly; config.detectLongPress = detectLongPress; config.detectLongTap = detectLongTap; config.endLongPressWhenMoved = endLongPressWhenMoved; config.endLongPressWhenSwiped = endLongPressWhenSwiped; config.swipeOriginalDirResetMode = swipeOriginalDirResetMode; CFGUI.EndUndo(this.undoObject); } }