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 23 #include "base/geometry/dimension_offset.h" 24 #include "base/geometry/dimension_rect.h" 25 #include "base/memory/type_info_base.h" 26 #include "base/utils/type_definition.h" 27 28 namespace OHOS::Ace { 29 30 enum class SourceType : int32_t { 31 NONE = 0, 32 MOUSE = 1, 33 TOUCH = 2, 34 TOUCH_PAD = 3, 35 KEYBOARD = 4 36 }; 37 38 enum class SourceTool : int32_t { 39 UNKNOWN = 0, 40 FINGER = 1, 41 PEN = 2, 42 RUBBER = 3, 43 BRUSH = 4, 44 PENCIL = 5, 45 AIRBRUSH = 6, 46 MOUSE = 7, 47 LENS = 8, 48 }; 49 50 struct EventTarget final { 51 std::string id; 52 std::string type; 53 DimensionRect area; 54 DimensionOffset origin; 55 }; 56 57 class BaseEventInfo : public virtual TypeInfoBase { 58 DECLARE_RELATIONSHIP_OF_CLASSES(BaseEventInfo, TypeInfoBase); 59 60 public: BaseEventInfo(const std::string & type)61 explicit BaseEventInfo(const std::string& type) : type_(type) {} 62 ~BaseEventInfo() override = default; 63 GetType()64 const std::string& GetType() const 65 { 66 return type_; 67 } SetType(const std::string & type)68 void SetType(const std::string& type) 69 { 70 type_ = type; 71 } 72 GetTimeStamp()73 const TimeStamp& GetTimeStamp() const 74 { 75 return timeStamp_; 76 } SetTimeStamp(const TimeStamp & timeStamp)77 BaseEventInfo& SetTimeStamp(const TimeStamp& timeStamp) 78 { 79 timeStamp_ = timeStamp; 80 return *this; 81 } 82 GetTarget()83 const EventTarget& GetTarget() const 84 { 85 return target_; 86 } GetTargetWithModify()87 EventTarget& GetTargetWithModify() 88 { 89 return target_; 90 } 91 SetTarget(const EventTarget & target)92 BaseEventInfo& SetTarget(const EventTarget& target) 93 { 94 target_ = target; 95 return *this; 96 } 97 GetDeviceId()98 int64_t GetDeviceId() const 99 { 100 return deviceId_; 101 } SetDeviceId(int64_t deviceId)102 void SetDeviceId(int64_t deviceId) 103 { 104 deviceId_ = deviceId; 105 } 106 GetSourceDevice()107 SourceType GetSourceDevice() const 108 { 109 return deviceType_; 110 } SetSourceDevice(SourceType deviceType)111 void SetSourceDevice(SourceType deviceType) 112 { 113 deviceType_ = deviceType; 114 } 115 SetForce(float force)116 void SetForce(float force) 117 { 118 force_ = force; 119 } GetForce()120 float GetForce() const 121 { 122 return force_; 123 } 124 SetTiltX(float tiltX)125 void SetTiltX(float tiltX) 126 { 127 tiltX_ = tiltX; 128 } GetTiltX()129 std::optional<float> GetTiltX() const 130 { 131 return tiltX_; 132 } 133 SetTiltY(float tiltY)134 void SetTiltY(float tiltY) 135 { 136 tiltY_ = tiltY; 137 } GetTiltY()138 std::optional<float> GetTiltY() const 139 { 140 return tiltY_; 141 } 142 SetSourceTool(SourceTool tool)143 void SetSourceTool(SourceTool tool) 144 { 145 sourceTool_ = tool; 146 } GetSourceTool()147 SourceTool GetSourceTool() const 148 { 149 return sourceTool_; 150 } 151 IsStopPropagation()152 bool IsStopPropagation() const 153 { 154 return stopPropagation_; 155 } SetStopPropagation(bool stopPropagation)156 void SetStopPropagation(bool stopPropagation) 157 { 158 stopPropagation_ = stopPropagation; 159 } 160 161 protected: 162 // Event type like onTouchDown, onClick and so on. 163 std::string type_; 164 // The origin event time stamp. 165 TimeStamp timeStamp_; 166 EventTarget target_; 167 SourceType deviceType_ = SourceType::NONE; 168 float force_ = 0.0f; 169 std::optional<float> tiltX_; 170 std::optional<float> tiltY_; 171 SourceTool sourceTool_ = SourceTool::UNKNOWN; 172 int64_t deviceId_ = 0; 173 bool stopPropagation_ = false; 174 }; 175 176 class PropagationEventInfo : public virtual TypeInfoBase { 177 DECLARE_RELATIONSHIP_OF_CLASSES(PropagationEventInfo, TypeInfoBase); 178 179 public: IsStopPropagation()180 bool IsStopPropagation() const 181 { 182 return stopPropagation_; 183 } SetStopPropagation(bool stopPropagation)184 void SetStopPropagation(bool stopPropagation) 185 { 186 stopPropagation_ = stopPropagation; 187 } 188 189 private: 190 bool stopPropagation_ = false; 191 }; 192 193 class EventToJSONStringAdapter : public virtual TypeInfoBase { 194 DECLARE_RELATIONSHIP_OF_CLASSES(EventToJSONStringAdapter, TypeInfoBase); 195 196 public: 197 EventToJSONStringAdapter() = default; 198 ~EventToJSONStringAdapter() = default; 199 200 virtual std::string ToJSONString() const = 0; 201 }; 202 203 } // namespace OHOS::Ace 204 205 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENTS_H 206