• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_MOUSE_EVENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_MOUSE_EVENT_H
18 
19 #include "core/event/touch_event.h"
20 
21 namespace OHOS::Ace {
22 
23 static const int32_t MOUSE_BASE_ID = 1000;
24 
25 enum class MouseAction : int32_t {
26     NONE = 0,
27     PRESS = 1,
28     RELEASE = 2,
29     MOVE = 3,
30     HOVER = 4,
31     HOVER_ENTER,
32     HOVER_MOVE,
33     HOVER_EXIT,
34 };
35 
36 enum class MouseState : int32_t {
37     NONE = 0,
38     HOVER = 1,
39 };
40 
41 enum class MouseButton : int32_t {
42     NONE_BUTTON = 0,
43     LEFT_BUTTON = 1,
44     RIGHT_BUTTON = 2,
45     MIDDLE_BUTTON = 4,
46     BACK_BUTTON = 8,
47     FORWARD_BUTTON = 16,
48 };
49 
50 struct MouseEvent final {
51     float x = 0.0f;
52     float y = 0.0f;
53     float z = 0.0f;
54     float deltaX = 0.0f;
55     float deltaY = 0.0f;
56     float deltaZ = 0.0f;
57     float scrollX = 0.0f;
58     float scrollY = 0.0f;
59     float scrollZ = 0.0f;
60     float screenX = 0.0f;
61     float screenY = 0.0f;
62     MouseAction action = MouseAction::NONE;
63     MouseButton button = MouseButton::NONE_BUTTON;
64     int32_t pressedButtons = 0; // combined by MouseButtons
65     TimeStamp time;
66     int64_t deviceId = 0;
67     SourceType sourceType = SourceType::NONE;
68 
GetOffsetfinal69     Offset GetOffset() const
70     {
71         return Offset(x, y);
72     }
73 
GetScreenOffsetfinal74     Offset GetScreenOffset() const
75     {
76         return Offset(screenX, screenY);
77     }
78 
GetIdfinal79     int32_t GetId() const
80     {
81         if (pressedButtons > 0) {
82             return pressedButtons + MOUSE_BASE_ID;
83         } else {
84             return (int32_t)button + MOUSE_BASE_ID;
85         }
86     }
87 
CreateScaleEventfinal88     MouseEvent CreateScaleEvent(float scale) const
89     {
90         if (NearZero(scale)) {
91             return { .x = x,
92                 .y = y,
93                 .z = z,
94                 .deltaX = deltaX,
95                 .deltaY = deltaY,
96                 .deltaZ = deltaZ,
97                 .scrollX = scrollX,
98                 .scrollY = scrollY,
99                 .scrollZ = scrollZ,
100                 .screenX = screenX,
101                 .screenY = screenY,
102                 .action = action,
103                 .button = button,
104                 .pressedButtons = pressedButtons,
105                 .time = time,
106                 .deviceId = deviceId,
107                 .sourceType = sourceType };
108         }
109 
110         return { .x = x / scale,
111             .y = y / scale,
112             .z = z / scale,
113             .deltaX = deltaX / scale,
114             .deltaY = deltaY / scale,
115             .deltaZ = deltaZ / scale,
116             .scrollX = scrollX / scale,
117             .scrollY = scrollY / scale,
118             .scrollZ = scrollZ / scale,
119             .screenX = screenX / scale,
120             .screenY = screenY / scale,
121             .action = action,
122             .button = button,
123             .pressedButtons = pressedButtons,
124             .time = time,
125             .deviceId = deviceId,
126             .sourceType = sourceType };
127     }
128 
CreateTouchPointfinal129     TouchEvent CreateTouchPoint() const
130     {
131         TouchType type = TouchType::UNKNOWN;
132         if (action == MouseAction::PRESS) {
133             type = TouchType::DOWN;
134         } else if (action == MouseAction::RELEASE) {
135             type = TouchType::UP;
136         } else if (action == MouseAction::MOVE) {
137             type = TouchType::MOVE;
138         } else {
139             type = TouchType::UNKNOWN;
140         }
141         int32_t id = GetId();
142         TouchPoint point { .id = id,
143             .x = x,
144             .y = y,
145             .screenX = screenX,
146             .screenY = screenY,
147             .downTime = time,
148             .size = 0.0,
149             .isPressed = (type == TouchType::DOWN) };
150         TouchEvent event { .id = id,
151             .x = x,
152             .y = y,
153             .screenX = screenX,
154             .screenY = screenY,
155             .type = type,
156             .time = time,
157             .size = 0.0,
158             .deviceId = deviceId,
159             .sourceType = sourceType };
160         event.pointers.emplace_back(std::move(point));
161         return event;
162     }
163 
164     MouseEvent operator-(const Offset& offset) const
165     {
166         return { .x = x - offset.GetX(),
167             .y = y - offset.GetY(),
168             .z = z,
169             .deltaX = deltaX,
170             .deltaY = deltaY,
171             .deltaZ = deltaZ,
172             .scrollX = scrollX,
173             .scrollY = scrollY,
174             .scrollZ = scrollZ,
175             .screenX = screenX - offset.GetX(),
176             .screenY = screenY - offset.GetY(),
177             .action = action,
178             .button = button,
179             .pressedButtons = pressedButtons,
180             .time = time,
181             .deviceId = deviceId,
182             .sourceType = sourceType };
183     }
184 };
185 
186 class MouseInfo : public BaseEventInfo {
187     DECLARE_RELATIONSHIP_OF_CLASSES(MouseInfo, BaseEventInfo);
188 
189 public:
MouseInfo()190     MouseInfo() : BaseEventInfo("onMouse") {}
191     ~MouseInfo() override = default;
192 
SetButton(MouseButton button)193     void SetButton(MouseButton button)
194     {
195         button_ = button;
196     }
197 
GetButton()198     MouseButton GetButton() const
199     {
200         return button_;
201     }
202 
SetAction(MouseAction action)203     void SetAction(MouseAction action)
204     {
205         action_ = action;
206     }
207 
GetAction()208     MouseAction GetAction() const
209     {
210         return action_;
211     }
212 
SetGlobalLocation(const Offset & globalLocation)213     MouseInfo& SetGlobalLocation(const Offset& globalLocation)
214     {
215         globalLocation_ = globalLocation;
216         return *this;
217     }
SetLocalLocation(const Offset & localLocation)218     MouseInfo& SetLocalLocation(const Offset& localLocation)
219     {
220         localLocation_ = localLocation;
221         return *this;
222     }
223 
SetScreenLocation(const Offset & screenLocation)224     MouseInfo& SetScreenLocation(const Offset& screenLocation)
225     {
226         screenLocation_ = screenLocation;
227         return *this;
228     }
229 
GetScreenLocation()230     const Offset& GetScreenLocation() const
231     {
232         return screenLocation_;
233     }
234 
GetLocalLocation()235     const Offset& GetLocalLocation() const
236     {
237         return localLocation_;
238     }
GetGlobalLocation()239     const Offset& GetGlobalLocation() const
240     {
241         return globalLocation_;
242     }
243 
244 private:
245     MouseButton button_ = MouseButton::NONE_BUTTON;
246     MouseAction action_ = MouseAction::NONE;
247     // global position at which the touch point contacts the screen.
248     Offset globalLocation_;
249     // Different from global location, The local location refers to the location of the contact point relative to the
250     // current node which has the recognizer.
251     Offset localLocation_;
252     Offset screenLocation_;
253 };
254 
255 class MouseEventTarget : public virtual AceType {
256     DECLARE_ACE_TYPE(MouseEventTarget, AceType);
257 
258 public:
259     virtual void HandleEvent(const MouseEvent& event) = 0;
260 };
261 
262 using MouseTestResult = std::list<RefPtr<MouseEventTarget>>;
263 
264 } // namespace OHOS::Ace
265 
266 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_MOUSE_EVENT_H
267