Feature: Axis event enhancement

Signed-off-by: bixuefeng <bixuefeng@huawei.com>
Change-Id: I05508fbacea166adf2a5a094c89f6be29b9273f7
This commit is contained in:
bixuefeng 2023-11-08 11:33:04 +08:00
parent 860374a420
commit e353fc86f4
14 changed files with 219 additions and 57 deletions

View File

@ -130,6 +130,33 @@ void EventManager::TouchTest(
axisTouchTestResults_[event.id] = std::move(hitTestResult);
}
bool EventManager::HasDifferentDirectionGesture()
{
uint8_t verticalFlag = 0;
uint8_t horizontalFlag = 0;
for (const auto& axisResult : axisTouchTestResults_) {
auto axisRecognizerList = axisResult.second;
for (const auto& axisRecognizer : axisRecognizerList) {
if (!axisRecognizer) {
continue;
}
auto axisDirection = axisRecognizer->GetAxisDirection();
if (axisDirection == Axis::FREE) {
return true;
}
if (axisDirection == Axis::VERTICAL) {
verticalFlag = 0x1;
} else if (axisDirection == Axis::HORIZONTAL) {
horizontalFlag = 0x2;
}
if ((verticalFlag | horizontalFlag) == 0x3) {
return true;
}
}
}
return (verticalFlag | horizontalFlag) == 0x3;
}
void EventManager::HandleGlobalEvent(const TouchEvent& touchPoint, const RefPtr<TextOverlayManager>& textOverlayManager)
{
if (touchPoint.type != TouchType::DOWN) {

View File

@ -75,6 +75,8 @@ public:
void TouchTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode, const TouchRestrict& touchRestrict);
bool HasDifferentDirectionGesture();
bool DispatchTouchEvent(const TouchEvent& point);
bool DispatchTouchEvent(const AxisEvent& event);
void FlushTouchEventsBegin(const std::list<TouchEvent>& touchEvents);

View File

@ -14,7 +14,6 @@
*/
#include "core/components_ng/gestures/recognizers/pan_recognizer.h"
#include <map>
#include "base/geometry/offset.h"
#include "base/log/log.h"
@ -33,7 +32,6 @@ namespace {
constexpr int32_t MAX_PAN_FINGERS = 10;
constexpr int32_t DEFAULT_PAN_FINGERS = 1;
constexpr Dimension DISTANCE_PER_MOUSE_DEGREE = LINE_HEIGHT_DESKTOP * LINE_NUMBER_DESKTOP / MOUSE_WHEEL_DEGREES;
constexpr int32_t AXIS_PAN_FINGERS = 1;
} // namespace
@ -130,8 +128,8 @@ void PanRecognizer::UpdateTouchPointInVelocityTracker(const TouchEvent& event, b
void PanRecognizer::HandleTouchDownEvent(const TouchEvent& event)
{
TAG_LOGI(AceLogTag::ACE_GESTURE,
"Pan recognizer receives %{public}d touch down event, begin to detect pan event", event.id);
TAG_LOGI(AceLogTag::ACE_GESTURE, "Pan recognizer receives %{public}d touch down event, begin to detect pan event",
event.id);
fingers_ = newFingers_;
distance_ = newDistance_;
direction_ = newDirection_;
@ -210,7 +208,7 @@ void PanRecognizer::HandleTouchUpEvent(const TouchEvent& event)
}
globalPoint_ = Point(event.x, event.y);
lastTouchEvent_ = event;
if (static_cast<int32_t>(touchPoints_.size()) == fingers_) {
UpdateTouchPointInVelocityTracker(event, true);
} else if (static_cast<int32_t>(touchPoints_.size()) > fingers_) {
@ -322,25 +320,28 @@ void PanRecognizer::HandleTouchMoveEvent(const AxisEvent& event)
if (fingers_ != AXIS_PAN_FINGERS) {
return;
}
globalPoint_ = Point(event.x, event.y);
auto distancePerMouseDegreePx = DISTANCE_PER_MOUSE_DEGREE.ConvertToPx();
if (direction_.type == PanDirection::ALL || (direction_.type & PanDirection::HORIZONTAL) == 0) {
// PanRecognizer Direction: Vertical or ALL
delta_ =
Offset(-event.horizontalAxis * distancePerMouseDegreePx, -event.verticalAxis * distancePerMouseDegreePx);
} else if ((direction_.type & PanDirection::VERTICAL) == 0) {
// PanRecognizer Direction: Horizontal
if (NearZero(event.horizontalAxis)) {
delta_ = Offset(-event.verticalAxis * distancePerMouseDegreePx, 0);
} else {
delta_ = Offset(
-event.horizontalAxis * distancePerMouseDegreePx, -event.verticalAxis * distancePerMouseDegreePx);
auto pipeline = PipelineContext::GetCurrentContext();
bool isShiftKeyPressed = false;
bool hasDifferentDirectionGesture = false;
if (pipeline) {
isShiftKeyPressed =
pipeline->IsKeyInPressed(KeyCode::KEY_SHIFT_LEFT) || pipeline->IsKeyInPressed(KeyCode::KEY_SHIFT_RIGHT);
hasDifferentDirectionGesture = pipeline->HasDifferentDirectionGesture();
}
delta_ = event.ConvertToOffset(isShiftKeyPressed, hasDifferentDirectionGesture);
if (event.sourceTool == SourceTool::MOUSE) {
if ((direction_.type & PanDirection::HORIZONTAL) == 0) { // Direction is vertical
delta_.SetX(0.0);
} else if ((direction_.type & PanDirection::VERTICAL) == 0) { // Direction is horizontal
delta_.SetY(0.0);
}
}
globalPoint_ = Point(event.x, event.y);
mainDelta_ = GetMainAxisDelta();
averageDistance_ += delta_;
auto pesudoTouchEvent = TouchEvent();
pesudoTouchEvent.time = event.time;
pesudoTouchEvent.x = lastAxisEvent_.horizontalAxis + event.horizontalAxis;

View File

@ -45,6 +45,20 @@ public:
void OnFlushTouchEventsBegin() override;
void OnFlushTouchEventsEnd() override;
Axis GetAxisDirection() override
{
if (direction_.type == PanDirection::ALL) {
return Axis::FREE;
}
if ((direction_.type & PanDirection::VERTICAL) == 0) {
return Axis::HORIZONTAL;
}
if ((direction_.type & PanDirection::HORIZONTAL) == 0) {
return Axis::VERTICAL;
}
return Axis::NONE;
}
void SetDirection(const PanDirection& direction);
void SetIsForDrag(bool isForDrag)

View File

@ -85,6 +85,44 @@ public:
}
const std::list<RefPtr<NGGestureRecognizer>>& GetGroupRecognizer();
Axis GetAxisDirection() override
{
uint8_t horizontalFlag = 0;
uint8_t verticalFlag = 0;
uint8_t freeFlag = 0;
for (const auto& recognizer : recognizers_) {
if (!recognizer) {
continue;
}
auto direction = recognizer->GetAxisDirection();
switch (direction) {
case Axis::HORIZONTAL:
horizontalFlag = 0x1;
break;
case Axis::VERTICAL:
verticalFlag = 0x2;
break;
case Axis::FREE:
freeFlag = 0x3;
break;
default:
break;
}
}
uint8_t directionFlag = horizontalFlag | verticalFlag | freeFlag;
switch (directionFlag) {
case 0x1:
return Axis::HORIZONTAL;
case 0x2:
return Axis::VERTICAL;
case 0x3:
return Axis::FREE;
default:
return Axis::NONE;
}
}
protected:
void OnBeginGestureReferee(int32_t touchId, bool needUpdateChild = false) override;
void OnFinishGestureReferee(int32_t touchId, bool isBlocked = false) override;

View File

@ -26,6 +26,7 @@
#include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
#include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h"
#include "core/event/touch_event.h"
#include "core/pipeline_ng/pipeline_context.h"
namespace OHOS::Ace::NG {
namespace {
@ -98,16 +99,14 @@ void SwipeRecognizer::HandleTouchDownEvent(const TouchEvent& event)
void SwipeRecognizer::HandleTouchDownEvent(const AxisEvent& event)
{
TAG_LOGI(AceLogTag::ACE_GESTURE,
"Swipe recognizer receives axis down event, begin to detect swipe event");
TAG_LOGI(AceLogTag::ACE_GESTURE, "Swipe recognizer receives axis down event, begin to detect swipe event");
if (direction_.type == SwipeDirection::NONE) {
Adjudicate(Claim(this), GestureDisposal::REJECT);
return;
}
axisEventStart_ = event;
axisVerticalTotal_ = 0.0;
axisHorizontalTotal_ = 0.0;
axisOffset_.Reset();
touchDownTime_ = event.time;
time_ = event.time;
refereeState_ = RefereeState::DETECTING;
@ -119,8 +118,8 @@ void SwipeRecognizer::HandleTouchUpEvent(const TouchEvent& event)
globalPoint_ = Point(event.x, event.y);
time_ = event.time;
lastTouchEvent_ = event;
if ((refereeState_ != RefereeState::DETECTING) && (refereeState_ != RefereeState::FAIL)
&& (refereeState_ != RefereeState::PENDING)) {
if ((refereeState_ != RefereeState::DETECTING) && (refereeState_ != RefereeState::FAIL) &&
(refereeState_ != RefereeState::PENDING)) {
Adjudicate(AceType::Claim(this), GestureDisposal::REJECT);
return;
}
@ -164,14 +163,19 @@ void SwipeRecognizer::HandleTouchUpEvent(const AxisEvent& event)
}
if (refereeState_ == RefereeState::DETECTING) {
auto slidingTime = event.time - touchDownTime_;
if (NearZero(axisOffset_.GetX()) && NearZero(axisOffset_.GetY())) {
Adjudicate(AceType::Claim(this), GestureDisposal::REJECT);
return;
}
if (event.sourceTool == SourceTool::MOUSE) {
resultSpeed_ = 0.0;
Adjudicate(AceType::Claim(this), GestureDisposal::ACCEPT);
return;
}
auto duration = event.time - touchDownTime_;
auto duration_ms =
std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, RATIO_US_TO_MS>>>(slidingTime);
double verticalMoveTotal = axisVerticalTotal_ * DP_PER_LINE_DESKTOP * LINE_NUMBER_DESKTOP / MOUSE_WHEEL_DEGREES;
double horizontalMoveTotal =
axisHorizontalTotal_ * DP_PER_LINE_DESKTOP * LINE_NUMBER_DESKTOP / MOUSE_WHEEL_DEGREES;
resultSpeed_ =
Offset(horizontalMoveTotal, verticalMoveTotal).GetDistance() / duration_ms.count() * RATIO_MS_TO_S;
std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, RATIO_US_TO_MS>>>(duration);
resultSpeed_ = axisOffset_.GetDistance() / duration_ms.count() * RATIO_MS_TO_S;
if (resultSpeed_ < speed_) {
Adjudicate(AceType::Claim(this), GestureDisposal::REJECT);
} else {
@ -215,8 +219,32 @@ void SwipeRecognizer::HandleTouchMoveEvent(const AxisEvent& event)
}
globalPoint_ = Point(event.x, event.y);
time_ = event.time;
axisVerticalTotal_ += fabs(event.verticalAxis);
axisHorizontalTotal_ += fabs(event.horizontalAxis);
auto pipeline = PipelineContext::GetCurrentContext();
bool isShiftKeyPressed = false;
bool hasDifferentDirectionGesture = false;
if (pipeline) {
isShiftKeyPressed =
pipeline->IsKeyInPressed(KeyCode::KEY_SHIFT_LEFT) || pipeline->IsKeyInPressed(KeyCode::KEY_SHIFT_RIGHT);
hasDifferentDirectionGesture = pipeline->HasDifferentDirectionGesture();
}
auto currentOffset = event.ConvertToOffset(isShiftKeyPressed, hasDifferentDirectionGesture);
if (event.sourceTool == SourceTool::MOUSE) {
if (direction_.type == SwipeDirection::VERTICAL) {
currentOffset.SetX(0.0);
} else if (direction_.type == SwipeDirection::HORIZONTAL) {
currentOffset.SetY(0.0);
}
}
if (NearZero(currentOffset.GetX()) && NearZero(currentOffset.GetY())) {
return;
}
axisOffset_ += currentOffset;
double newAngle = ComputeAngle(currentOffset.GetX(), currentOffset.GetY());
if (CheckAngle(newAngle)) {
prevAngle_ = newAngle;
return;
}
Adjudicate(Claim(this), GestureDisposal::REJECT);
}
void SwipeRecognizer::HandleTouchCancelEvent(const TouchEvent& event)
@ -265,8 +293,7 @@ bool SwipeRecognizer::CheckAngle(double angle)
void SwipeRecognizer::OnResetStatus()
{
MultiFingersRecognizer::OnResetStatus();
axisHorizontalTotal_ = 0.0;
axisVerticalTotal_ = 0.0;
axisOffset_.Reset();
resultSpeed_ = 0.0;
lastTouchEvent_ = TouchEvent();
downEvents_.clear();
@ -285,11 +312,7 @@ void SwipeRecognizer::SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& c
UpdateFingerListInfo();
info.SetFingerList(fingerList_);
info.SetGlobalPoint(globalPoint_);
if (deviceType_ == SourceType::MOUSE) {
info.SetSpeed(0.0);
} else {
info.SetSpeed(resultSpeed_);
}
info.SetSpeed(resultSpeed_);
info.SetSourceDevice(deviceType_);
info.SetDeviceId(deviceId_);
info.SetTarget(GetEventTarget().value_or(EventTarget()));

View File

@ -38,6 +38,20 @@ public:
void OnAccepted() override;
void OnRejected() override;
Axis GetAxisDirection() override
{
switch (direction_.type) {
case SwipeDirection::HORIZONTAL:
return Axis::HORIZONTAL;
case SwipeDirection::VERTICAL:
return Axis::VERTICAL;
case SwipeDirection::ALL:
return Axis::FREE;
default:
return Axis::NONE;
}
}
private:
void HandleTouchDownEvent(const TouchEvent& event) override;
void HandleTouchUpEvent(const TouchEvent& event) override;
@ -61,8 +75,7 @@ private:
std::map<int32_t, TouchEvent> downEvents_;
AxisEvent axisEventStart_;
double axisVerticalTotal_ = 0.0;
double axisHorizontalTotal_ = 0.0;
Offset axisOffset_;
TimeStamp time_;
TimeStamp touchDownTime_;

View File

@ -147,6 +147,27 @@ struct AxisEvent final {
{
return (static_cast<uint32_t>(direction) & static_cast<uint32_t>(AxisDirection::RIGHT));
}
Offset ConvertToOffset(bool isShiftKeyPressed = false, bool hasDifferentDirectionGesture = false) const
{
Offset result;
if (sourceTool == SourceTool::MOUSE) {
// Axis event is made by mouse.
if (isShiftKeyPressed) {
result = Offset(-verticalAxis, -horizontalAxis);
} else {
if (hasDifferentDirectionGesture) {
result = Offset(-horizontalAxis, -verticalAxis);
} else {
result = Offset(-verticalAxis, -verticalAxis);
}
}
} else {
// Axis event is made by others. Include touch-pad.
result = Offset(-horizontalAxis, -verticalAxis);
}
return result * (LINE_HEIGHT_DESKTOP * LINE_NUMBER_DESKTOP / MOUSE_WHEEL_DEGREES).ConvertToPx();
}
};
class AxisInfo : public BaseEventInfo {

View File

@ -449,6 +449,10 @@ public:
}
virtual void OnFlushTouchEventsBegin() {}
virtual void OnFlushTouchEventsEnd() {}
virtual Axis GetAxisDirection()
{
return direction_;
}
void SetTouchRestrict(const TouchRestrict& touchRestrict)
{
@ -550,6 +554,7 @@ protected:
std::string nodeName_ = "NULL";
int32_t nodeId_ = -1;
WeakPtr<NG::FrameNode> node_ = nullptr;
Axis direction_ = Axis::NONE;
};
using TouchTestResult = std::list<RefPtr<TouchEventTarget>>;

View File

@ -1577,6 +1577,12 @@ void PipelineContext::OnAxisEvent(const AxisEvent& event)
OnMouseEvent(mouseEvent);
}
bool PipelineContext::HasDifferentDirectionGesture() const
{
CHECK_NULL_RETURN(eventManager_, false);
return eventManager_->HasDifferentDirectionGesture();
}
void PipelineContext::AddVisibleAreaChangeNode(
const RefPtr<FrameNode>& node, double ratio, const VisibleRatioCallback& callback, bool isUserCallback)
{

View File

@ -262,6 +262,8 @@ public:
void RemoveWindowSizeChangeCallback(int32_t nodeId);
bool HasDifferentDirectionGesture() const;
bool IsKeyInPressed(KeyCode tarCode) const
{
CHECK_NULL_RETURN(eventManager_, false);

View File

@ -347,6 +347,11 @@ void PipelineContext::AddIsFocusActiveUpdateEvent(
{}
void PipelineContext::RemoveIsFocusActiveUpdateEvent(const RefPtr<FrameNode>& node) {}
bool PipelineContext::HasDifferentDirectionGesture() const
{
return false;
}
} // namespace OHOS::Ace::NG
// pipeline base

View File

@ -6689,8 +6689,8 @@ HWTEST_F(GesturesTestNg, SwipeRecognizerTest002, TestSize.Level1)
AxisEvent axisEvent;
swipeRecognizer.HandleTouchDownEvent(axisEvent);
EXPECT_EQ(swipeRecognizer.axisVerticalTotal_, 0.0);
EXPECT_EQ(swipeRecognizer.axisHorizontalTotal_, 0.0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetX(), 0.0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetY(), 0.0);
EXPECT_EQ(swipeRecognizer.refereeState_, RefereeState::DETECTING);
}
@ -7207,8 +7207,8 @@ HWTEST_F(GesturesTestNg, SwipeRecognizerHandleTouchMoveEventTest001, TestSize.Le
swipeRecognizer.refereeState_ = RefereeState::FAIL;
swipeRecognizer.currentFingers_ = swipeRecognizer.fingers_;
swipeRecognizer.HandleTouchMoveEvent(axisEvent);
EXPECT_EQ(swipeRecognizer.axisVerticalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisHorizontalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetX(), 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetY(), 0);
}
/**
@ -7250,8 +7250,8 @@ HWTEST_F(GesturesTestNg, SwipeRecognizerTest004, TestSize.Level1)
swipeRecognizer.HandleTouchMoveEvent(axisEvent);
EXPECT_EQ(swipeRecognizer.globalPoint_.GetX(), axisEvent.x);
EXPECT_EQ(swipeRecognizer.globalPoint_.GetY(), axisEvent.y);
EXPECT_EQ(swipeRecognizer.axisVerticalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisHorizontalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetX(), 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetY(), 0);
}
/**
@ -7364,8 +7364,8 @@ HWTEST_F(GesturesTestNg, SwipeRecognizerHandleTouchMoveEventTest002, TestSize.Le
swipeRecognizer.refereeState_ = RefereeState::FAIL;
swipeRecognizer.currentFingers_ = swipeRecognizer.fingers_;
swipeRecognizer.HandleTouchMoveEvent(axisEvent);
EXPECT_EQ(swipeRecognizer.axisVerticalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisHorizontalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetX(), 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetY(), 0);
}
/**
@ -7406,8 +7406,8 @@ HWTEST_F(GesturesTestNg, SwipeRecognizerHandleTouchMoveEventTest003, TestSize.Le
swipeRecognizer.refereeState_ = RefereeState::FAIL;
swipeRecognizer.currentFingers_ = swipeRecognizer.fingers_;
swipeRecognizer.HandleTouchMoveEvent(axisEvent);
EXPECT_EQ(swipeRecognizer.axisVerticalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisHorizontalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetX(), 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetY(), 0);
}
/**
@ -7446,8 +7446,8 @@ HWTEST_F(GesturesTestNg, SwipeRecognizerHandleTouchMoveEventTest004, TestSize.Le
swipeRecognizer.refereeState_ = RefereeState::FAIL;
swipeRecognizer.currentFingers_ = swipeRecognizer.fingers_;
swipeRecognizer.HandleTouchMoveEvent(axisEvent);
EXPECT_EQ(swipeRecognizer.axisVerticalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisHorizontalTotal_, 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetX(), 0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetY(), 0);
}
/**
@ -7468,8 +7468,8 @@ HWTEST_F(GesturesTestNg, SwipeRecognizerTest006, TestSize.Level1)
* @tc.expected: step2. result equals.
*/
swipeRecognizer.OnResetStatus();
EXPECT_EQ(swipeRecognizer.axisHorizontalTotal_, 0.0);
EXPECT_EQ(swipeRecognizer.axisVerticalTotal_, 0.0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetX(), 0.0);
EXPECT_EQ(swipeRecognizer.axisOffset_.GetY(), 0.0);
EXPECT_EQ(swipeRecognizer.resultSpeed_, 0.0);
EXPECT_EQ(swipeRecognizer.globalPoint_.GetX(), 0.0);
EXPECT_EQ(swipeRecognizer.globalPoint_.GetY(), 0.0);

View File

@ -149,5 +149,10 @@ void EventManager::DispatchKeyboardShortcut(const KeyEvent& event) {}
void EventManager::ClearResults() {}
bool EventManager::HasDifferentDirectionGesture()
{
return false;
}
EventManager::EventManager() {}
} // namespace OHOS::Ace