• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_ACE_EVENTS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENTS_H
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <string>
22 #include <optional>
23 
24 #include "base/geometry/dimension_offset.h"
25 #include "base/geometry/dimension_rect.h"
26 #include "base/memory/type_info_base.h"
27 #include "base/utils/type_definition.h"
28 #include "core/components_ng/event/event_constants.h"
29 
30 namespace OHOS::Ace {
31 
32 using ConvertInfo = std::pair<UIInputEventType, UIInputEventType>;
33 
34 enum class KeyCode : int32_t;
35 
36 enum class SourceType : int32_t {
37     NONE = 0,
38     MOUSE = 1,
39     TOUCH = 2,
40     TOUCH_PAD = 3,
41     KEYBOARD = 4,
42     JOYSTICK = 5,
43     CROWN = 6,
44 };
45 
46 enum class SourceTool : int32_t {
47     UNKNOWN = 0,
48     FINGER = 1,
49     PEN = 2,
50     RUBBER = 3,
51     BRUSH = 4,
52     PENCIL = 5,
53     AIRBRUSH = 6,
54     MOUSE = 7,
55     LENS = 8,
56     TOUCHPAD = 9,
57     JOYSTICK = 10,
58 };
59 
60 struct EventTarget final {
61     std::string id;
62     std::string type;
63     DimensionRect area;
64     DimensionOffset origin;
65 };
66 
67 class BaseEventInfo : public virtual TypeInfoBase {
68     DECLARE_RELATIONSHIP_OF_CLASSES(BaseEventInfo, TypeInfoBase);
69 
70 public:
BaseEventInfo(const std::string & type)71     explicit BaseEventInfo(const std::string& type) : type_(type) {}
72     ~BaseEventInfo() override = default;
73 
GetType()74     const std::string& GetType() const
75     {
76         return type_;
77     }
SetType(const std::string & type)78     void SetType(const std::string& type)
79     {
80         type_ = type;
81     }
82 
GetTimeStamp()83     const TimeStamp& GetTimeStamp() const
84     {
85         return timeStamp_;
86     }
SetTimeStamp(const TimeStamp & timeStamp)87     BaseEventInfo& SetTimeStamp(const TimeStamp& timeStamp)
88     {
89         timeStamp_ = timeStamp;
90         return *this;
91     }
92 
GetTarget()93     const EventTarget& GetTarget() const
94     {
95         return target_;
96     }
GetTargetWithModify()97     EventTarget& GetTargetWithModify()
98     {
99         return target_;
100     }
101 
SetTarget(const EventTarget & target)102     BaseEventInfo& SetTarget(const EventTarget& target)
103     {
104         target_ = target;
105         return *this;
106     }
107 
GetDeviceId()108     int64_t GetDeviceId() const
109     {
110         return deviceId_;
111     }
SetDeviceId(int64_t deviceId)112     void SetDeviceId(int64_t deviceId)
113     {
114         deviceId_ = deviceId;
115     }
116 
GetTargetDisplayId()117     int32_t GetTargetDisplayId() const
118     {
119         return targetDisplayId_;
120     }
SetTargetDisplayId(int32_t targetDisplayId)121     void SetTargetDisplayId(int32_t targetDisplayId)
122     {
123         targetDisplayId_ = targetDisplayId;
124     }
125 
GetSourceDevice()126     SourceType GetSourceDevice() const
127     {
128         return deviceType_;
129     }
SetSourceDevice(SourceType deviceType)130     void SetSourceDevice(SourceType deviceType)
131     {
132         deviceType_ = deviceType;
133     }
134 
SetForce(float force)135     void SetForce(float force)
136     {
137         force_ = force;
138     }
GetForce()139     float GetForce() const
140     {
141         return force_;
142     }
143 
SetTiltX(float tiltX)144     void SetTiltX(float tiltX)
145     {
146         tiltX_ = tiltX;
147     }
GetTiltX()148     std::optional<float> GetTiltX() const
149     {
150         return tiltX_;
151     }
152 
SetTiltY(float tiltY)153     void SetTiltY(float tiltY)
154     {
155         tiltY_ = tiltY;
156     }
GetTiltY()157     std::optional<float> GetTiltY() const
158     {
159         return tiltY_;
160     }
161 
SetRollAngle(float rollAngle)162     void SetRollAngle(float rollAngle)
163     {
164         rollAngle_ = rollAngle;
165     }
GetRollAngle()166     std::optional<float> GetRollAngle() const
167     {
168         return rollAngle_;
169     }
170 
SetSourceTool(SourceTool tool)171     void SetSourceTool(SourceTool tool)
172     {
173         sourceTool_ = tool;
174     }
GetSourceTool()175     SourceTool GetSourceTool() const
176     {
177         return sourceTool_;
178     }
179 
IsStopPropagation()180     bool IsStopPropagation() const
181     {
182         return stopPropagation_;
183     }
SetStopPropagation(bool stopPropagation)184     void SetStopPropagation(bool stopPropagation)
185     {
186         stopPropagation_ = stopPropagation;
187     }
188 
IsPreventDefault()189     bool IsPreventDefault() const
190     {
191         return preventDefault_;
192     }
SetPreventDefault(bool preventDefault)193     void SetPreventDefault(bool preventDefault)
194     {
195         preventDefault_ = preventDefault;
196     }
197 
GetPatternName()198     const std::string& GetPatternName() const
199     {
200         return patternName_;
201     }
SetPatternName(const std::string & patternName)202     void SetPatternName(const std::string& patternName)
203     {
204         patternName_ = patternName;
205     }
206 
GetPressedKeyCodes()207     const std::vector<KeyCode>& GetPressedKeyCodes() const
208     {
209         return pressedKeyCodes_;
210     }
211 
SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)212     void SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes)
213     {
214         pressedKeyCodes_ = pressedKeyCodes;
215     }
216 
IsKeyPressed(KeyCode code)217     bool IsKeyPressed(KeyCode code) const
218     {
219         return (std::find(pressedKeyCodes_.begin(), pressedKeyCodes_.end(), code) != pressedKeyCodes_.end());
220     }
221 
GetPostEventNodeId()222     int32_t GetPostEventNodeId() const
223     {
224         return postEventNodeId_;
225     }
226 
SetPostEventNodeId(int32_t postEventNodeId)227     void SetPostEventNodeId(int32_t postEventNodeId)
228     {
229         postEventNodeId_ = postEventNodeId;
230     }
231 
GetIsPostEventResult()232     bool GetIsPostEventResult() const
233     {
234         return isPostEventResult_;
235     }
236 
SetIsPostEventResult(bool isPostEventResult)237     void SetIsPostEventResult(bool isPostEventResult)
238     {
239         isPostEventResult_ = isPostEventResult;
240     }
241 
GetOperatingHand()242     int32_t GetOperatingHand() const
243     {
244         return operatingHand_;
245     }
246 
SetOperatingHand(int32_t operatingHand)247     void SetOperatingHand(int32_t operatingHand)
248     {
249         operatingHand_ = operatingHand;
250     }
251 
GetHorizontalAxis()252     float GetHorizontalAxis() const
253     {
254         return horizontalAxis_;
255     }
256 
GetVerticalAxis()257     float GetVerticalAxis() const
258     {
259         return verticalAxis_;
260     }
261 
SetHorizontalAxis(float axis)262     void SetHorizontalAxis(float axis)
263     {
264         horizontalAxis_ = axis;
265     }
266 
SetVerticalAxis(float axis)267     void SetVerticalAxis(float axis)
268     {
269         verticalAxis_ = axis;
270     }
271 
CopyConvertInfoFrom(const ConvertInfo & info)272     void CopyConvertInfoFrom(const ConvertInfo& info)
273     {
274         convertInfo = info;
275     }
276 
GetOriginUIInputEventType()277     UIInputEventType GetOriginUIInputEventType() const
278     {
279         return convertInfo.first;
280     }
281 
GetCurrentUIInputEventType()282     UIInputEventType GetCurrentUIInputEventType() const
283     {
284         return convertInfo.second;
285     }
286 protected:
287     // Event type like onTouchDown, onClick and so on.
288     std::string type_;
289     // The origin event time stamp.
290     TimeStamp timeStamp_;
291     EventTarget target_;
292     // Will be used in drag.
293     SourceType deviceType_ = SourceType::NONE;
294     float force_ = 0.0f;
295     std::optional<float> tiltX_;
296     std::optional<float> tiltY_;
297     std::optional<float> rollAngle_;
298     SourceTool sourceTool_ = SourceTool::UNKNOWN;
299     int64_t deviceId_ = 0;
300     // Will be used in drag.
301     int32_t targetDisplayId_ = 0;
302     bool stopPropagation_ = false;
303     bool preventDefault_ = false;
304     std::string patternName_;
305     std::vector<KeyCode> pressedKeyCodes_;
306     bool isPostEventResult_ = false;
307     int32_t postEventNodeId_ = -1;
308     int32_t operatingHand_ = 0;
309     float horizontalAxis_ = 0.0;
310     float verticalAxis_ = 0.0;
311     ConvertInfo convertInfo = { UIInputEventType::NONE, UIInputEventType::NONE };
312 };
313 
314 class PropagationEventInfo : public virtual TypeInfoBase {
315     DECLARE_RELATIONSHIP_OF_CLASSES(PropagationEventInfo, TypeInfoBase);
316 
317 public:
IsStopPropagation()318     bool IsStopPropagation() const
319     {
320         return stopPropagation_;
321     }
SetStopPropagation(bool stopPropagation)322     void SetStopPropagation(bool stopPropagation)
323     {
324         stopPropagation_ = stopPropagation;
325     }
326 
327 private:
328     bool stopPropagation_ = false;
329 };
330 
331 class EventToJSONStringAdapter : public virtual TypeInfoBase {
332     DECLARE_RELATIONSHIP_OF_CLASSES(EventToJSONStringAdapter, TypeInfoBase);
333 
334 public:
335     EventToJSONStringAdapter() = default;
336     ~EventToJSONStringAdapter() = default;
337 
338     virtual std::string ToJSONString() const = 0;
339 };
340 
341 } // namespace OHOS::Ace
342 
343 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENTS_H
344