diff --git a/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.cpp b/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.cpp index ea025a3d917bd9ae226a6287360aa54c7fe03a42..fe460990a00656a687a5e972fa78855146acdb04 100644 --- a/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.cpp +++ b/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.cpp @@ -30,7 +30,6 @@ namespace { constexpr int32_t MAX_ROTATION_FINGERS = 5; constexpr int32_t DEFAULT_ROTATION_FINGERS = 2; constexpr double ONE_CIRCLE = 360.0; -constexpr double QUARTER_CIRCLE = 90.0; constexpr double RANGE_MIN = -180.0; constexpr double RANGE_MAX = 180.0; @@ -104,6 +103,7 @@ void RotationRecognizer::HandleTouchDownEvent(const TouchEvent& event) if (static_cast(activeFingers_.size()) >= DEFAULT_ROTATION_FINGERS) { initialAngle_ = ComputeAngle(); + currentAngle_ = initialAngle_; lastRefereeState_ = refereeState_; refereeState_ = RefereeState::DETECTING; } @@ -203,38 +203,32 @@ void RotationRecognizer::HandleTouchUpEvent(const AxisEvent& event) void RotationRecognizer::HandleTouchMoveEvent(const TouchEvent& event) { + touchPoints_[event.id] = event; + lastAngle_ = currentAngle_; + currentAngle_ = ComputeAngle(); + time_ = event.time; if (!IsActiveFinger(event.id) || currentFingers_ < fingers_) { touchPoints_[event.id] = event; - lastAngle_ = 0.0; - angleSignChanged_ = false; return; } - touchPoints_[event.id] = event; - currentAngle_ = ComputeAngle(); - time_ = event.time; + if (static_cast(activeFingers_.size()) < DEFAULT_ROTATION_FINGERS) { lastAngle_ = 0.0; - angleSignChanged_ = false; + cumulativeAngle_ = 0.0; return; } if (refereeState_ == RefereeState::DETECTING) { - auto trueAngle = currentAngle_; - if (currentAngle_ * lastAngle_ < 0 && fabs(currentAngle_) > QUARTER_CIRCLE) { - angleSignChanged_ = !angleSignChanged_; - } - if (angleSignChanged_) { - if (initialAngle_ > 0.0) { - trueAngle += ONE_CIRCLE; - } else { - trueAngle -= ONE_CIRCLE; - } + double trueAngle = currentAngle_ - lastAngle_; + if (trueAngle > RANGE_MAX) { + trueAngle -= ONE_CIRCLE; + } else if (trueAngle < RANGE_MIN) { + trueAngle += ONE_CIRCLE; } - lastAngle_ = currentAngle_; - double diffAngle = fabs((trueAngle - initialAngle_)); - if (GreatOrEqual(diffAngle, angle_)) { + cumulativeAngle_ += trueAngle; + if (GreatOrEqual(fabs(cumulativeAngle_), angle_)) { lastAngle_ = 0.0; - angleSignChanged_ = false; + cumulativeAngle_ = 0.0; resultAngle_ = ChangeValueRange(currentAngle_ - initialAngle_); if (CheckLimitFinger()) { extraInfo_ += " isLFC: " + std::to_string(isLimitFingerCount_); @@ -249,7 +243,7 @@ void RotationRecognizer::HandleTouchMoveEvent(const TouchEvent& event) } } else if (refereeState_ == RefereeState::SUCCEED) { lastAngle_ = 0.0; - angleSignChanged_ = false; + cumulativeAngle_ = 0.0; resultAngle_ = ChangeValueRange(currentAngle_ - initialAngle_); if (static_cast(touchPoints_.size()) > fingers_ && isLimitFingerCount_) { return; @@ -356,7 +350,7 @@ void RotationRecognizer::OnResetStatus() currentAngle_ = 0.0; resultAngle_ = 0.0; lastAngle_ = 0.0; - angleSignChanged_ = false; + cumulativeAngle_ = 0.0; localMatrix_.clear(); } diff --git a/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.h b/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.h index da49af1af7fc9750d03bbdd4d43704e739d99fa1..0de68abbba9f4522c3467ac1140a0f124ad39b53 100644 --- a/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.h +++ b/frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.h @@ -71,7 +71,7 @@ private: double currentAngle_ = 0.0; double resultAngle_ = 0.0; double lastAngle_ = 0.0; - bool angleSignChanged_ = false; + double cumulativeAngle_ = 0.0; TimeStamp time_; AxisEvent lastAxisEvent_; }; diff --git a/test/unittest/core/gestures/rotation_recognizer_test_ng.cpp b/test/unittest/core/gestures/rotation_recognizer_test_ng.cpp index 865238673467fdcd6cce8dc8811509b590214c61..38c6814d65999f390dc79d2941354f623244f0bc 100644 --- a/test/unittest/core/gestures/rotation_recognizer_test_ng.cpp +++ b/test/unittest/core/gestures/rotation_recognizer_test_ng.cpp @@ -1462,7 +1462,7 @@ HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest00 rotationRecognizerPtr->angle_ = 0; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); EXPECT_EQ(rotationRecognizerPtr->lastAngle_, 0.0); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); } /** @@ -1499,13 +1499,13 @@ HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest00 rotationRecognizerPtr->touchPoints_[touchEvent2.id] = touchEvent2; rotationRecognizerPtr->touchPoints_[touchEvent.id] = touchEvent; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); rotationRecognizerPtr->initialAngle_ = 1; rotationRecognizerPtr->refereeState_ = RefereeState::DETECTING; rotationRecognizerPtr->lastAngle_ = -10; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); touchEvent.SetX(100); rotationRecognizerPtr->lastAngle_ = 10; @@ -1517,7 +1517,7 @@ HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest00 rotationRecognizerPtr->touchPoints_[touchEvent.id] = touchEvent; rotationRecognizerPtr->refereeState_ = RefereeState::DETECTING; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); } /** @@ -1554,7 +1554,7 @@ HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest00 rotationRecognizerPtr->touchPoints_[touchEvent2.id] = touchEvent2; rotationRecognizerPtr->touchPoints_[touchEvent.id] = touchEvent; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); touchEvent.SetX(100); rotationRecognizerPtr->lastAngle_ = -10; @@ -1566,7 +1566,7 @@ HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest00 rotationRecognizerPtr->touchPoints_[touchEvent.id] = touchEvent; rotationRecognizerPtr->refereeState_ = RefereeState::DETECTING; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); } /** @@ -1603,24 +1603,24 @@ HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest00 rotationRecognizerPtr->touchPoints_[touchEvent2.id] = touchEvent2; rotationRecognizerPtr->touchPoints_[touchEvent.id] = touchEvent; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); rotationRecognizerPtr->fingers_ = 2; rotationRecognizerPtr->refereeState_ = RefereeState::SUCCEED; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); rotationRecognizerPtr->isLimitFingerCount_ = false; rotationRecognizerPtr->fingers_ = 1; rotationRecognizerPtr->refereeState_ = RefereeState::SUCCEED; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); rotationRecognizerPtr->isLimitFingerCount_ = false; rotationRecognizerPtr->fingers_ = 2; rotationRecognizerPtr->refereeState_ = RefereeState::SUCCEED; rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); } /** @@ -1648,12 +1648,12 @@ HWTEST_F(RotationRecognizerTestNg, HandleTouchCancelEvent001, TestSize.Level1) rotationRecognizerPtr->refereeState_ = RefereeState::SUCCEED; rotationRecognizerPtr->activeFingers_.push_back(touchEvent.id); rotationRecognizerPtr->HandleTouchCancelEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); rotationRecognizerPtr->refereeState_ = RefereeState::FAIL; rotationRecognizerPtr->activeFingers_.push_back(touchEvent.id); rotationRecognizerPtr->HandleTouchCancelEvent(touchEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); } /** @@ -1673,7 +1673,7 @@ HWTEST_F(RotationRecognizerTestNg, HandleTouchCancelEvent002, TestSize.Level1) rotationRecognizerPtr->targetComponent_ = targetComponent; rotationRecognizerPtr->refereeState_ = RefereeState::SUCCEED; rotationRecognizerPtr->HandleTouchCancelEvent(axisEvent); - EXPECT_EQ(rotationRecognizerPtr->angleSignChanged_, false); + EXPECT_EQ(rotationRecognizerPtr->cumulativeAngle_, 0.0); } /** @@ -1870,4 +1870,96 @@ HWTEST_F(RotationRecognizerTestNg, GetGestureEventInfoTest001, TestSize.Level1) rotationRecognizerPtr->GetGestureEventInfo(info); EXPECT_EQ(info.GetSourceTool(), SourceTool::MOUSE); } + +/** + * @tc.name: RotationRecognizerPtrHandleTouchUpEventTest006 + * @tc.desc: Test RotationRecognizer function: HandleTouchMoveEvent with angle transition from 178 to -178 degrees + * @tc.type: FUNC + */ +HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest006, TestSize.Level0) +{ + RefPtr rotationRecognizerPtr = + AceType::MakeRefPtr(2, ROTATION_GESTURE_ANGLE); + RefPtr targetComponent = AceType::MakeRefPtr(); + auto gestureJudgeFunc = [](const RefPtr& gestureInfo, const std::shared_ptr& info) { + return GestureJudgeResult::REJECT;}; + targetComponent->SetOnGestureJudgeBegin(gestureJudgeFunc); + + rotationRecognizerPtr->targetComponent_ = targetComponent; + rotationRecognizerPtr->refereeState_ = RefereeState::DETECTING; + rotationRecognizerPtr->fingers_ = 2; + rotationRecognizerPtr->currentFingers_ = 2; + rotationRecognizerPtr->activeFingers_.push_back(1); + rotationRecognizerPtr->activeFingers_.push_back(2); + + rotationRecognizerPtr->initialAngle_ = 178.0; + rotationRecognizerPtr->currentAngle_ = 178.0; + rotationRecognizerPtr->lastAngle_ = 178.0; + + TouchEvent touchEvent1; + touchEvent1.id = 1; + touchEvent1.SetX(-50); + touchEvent1.SetY(-5); + + TouchEvent touchEvent2; + touchEvent2.id = 2; + touchEvent2.SetX(50); + touchEvent2.SetY(0); + + rotationRecognizerPtr->touchPoints_[1] = touchEvent1; + rotationRecognizerPtr->touchPoints_[2] = touchEvent2; + + touchEvent1.SetX(-50); + touchEvent1.SetY(-10); + rotationRecognizerPtr->touchPoints_[1] = touchEvent1; + + rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent1); + EXPECT_NE(rotationRecognizerPtr, nullptr); +} + +/** + * @tc.name: RotationRecognizerPtrHandleTouchUpEventTest007 + * @tc.desc: Test RotationRecognizer function: HandleTouchMoveEvent with angle transition from -178 to 178 degrees + * @tc.type: FUNC + */ +HWTEST_F(RotationRecognizerTestNg, RotationRecognizerPtrHandleTouchUpEventTest007, TestSize.Level0) +{ + RefPtr rotationRecognizerPtr = + AceType::MakeRefPtr(2, ROTATION_GESTURE_ANGLE); + RefPtr targetComponent = AceType::MakeRefPtr(); + auto gestureJudgeFunc = [](const RefPtr& gestureInfo, const std::shared_ptr& info) { + return GestureJudgeResult::REJECT;}; + targetComponent->SetOnGestureJudgeBegin(gestureJudgeFunc); + + rotationRecognizerPtr->targetComponent_ = targetComponent; + rotationRecognizerPtr->refereeState_ = RefereeState::DETECTING; + rotationRecognizerPtr->fingers_ = 2; + rotationRecognizerPtr->currentFingers_ = 2; + rotationRecognizerPtr->activeFingers_.push_back(1); + rotationRecognizerPtr->activeFingers_.push_back(2); + + rotationRecognizerPtr->initialAngle_ = -178.0; + rotationRecognizerPtr->currentAngle_ = -178.0; + rotationRecognizerPtr->lastAngle_ = -178.0; + + TouchEvent touchEvent1; + touchEvent1.id = 1; + touchEvent1.SetX(-50); + touchEvent1.SetY(1); + + TouchEvent touchEvent2; + touchEvent2.id = 2; + touchEvent2.SetX(50); + touchEvent2.SetY(0); + + rotationRecognizerPtr->touchPoints_[1] = touchEvent1; + rotationRecognizerPtr->touchPoints_[2] = touchEvent2; + + touchEvent1.SetX(50); + touchEvent1.SetY(1); + rotationRecognizerPtr->touchPoints_[1] = touchEvent1; + + rotationRecognizerPtr->HandleTouchMoveEvent(touchEvent1); + EXPECT_NE(rotationRecognizerPtr, nullptr); +} } // namespace OHOS::Ace::NG \ No newline at end of file