• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 /**
17  * @addtogroup UI_Events
18  * @{
19  *
20  * @brief Defines UI events, such as press, click and drag events.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file drag_event.h
28  *
29  * @brief Declares a drag event, which indicates a certain movement (more than 10 pixels) after a view is pressed.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_DRAG_EVENT_H
36 #define GRAPHIC_LITE_DRAG_EVENT_H
37 
38 #include "event.h"
39 
40 namespace OHOS {
41 /**
42  * @brief Defines a drag event, which indicates a certain movement (more than 10 pixels) after a view is pressed.
43  *
44  * @since 1.0
45  * @version 1.0
46  */
47 class DragEvent : public Event {
48 public:
49     /**
50      * @brief A constructor used to create a <b>DragEvent</b> instance.
51      * @param newPos Indicates the new position in the drag event.
52      * @param lastPos Indicates the last position in the drag event.
53      * @param totalLen Indicates the distance that the view has been dragged, including the movement on the x-axis
54      *                 and y-axis.
55      * @since 1.0
56      * @version 1.0
57      */
DragEvent(const Point & newPos,const Point & lastPos,const Point & totalLen)58     DragEvent(const Point& newPos, const Point& lastPos, const Point& totalLen) : Event(newPos)
59     {
60         lastPos_ = lastPos;
61         startPos_.x = newPos.x - totalLen.x;
62         startPos_.y = newPos.y - totalLen.y;
63         preLastPos_ = lastPos;
64         deltaX_ = newPos.x - lastPos.x;
65         deltaY_ = newPos.y - lastPos.y;
66     }
67 
68     /**
69      * @brief A destructor used to delete the <b>DragEvent</b> instance.
70      * @since 1.0
71      * @version 1.0
72      */
~DragEvent()73     virtual ~DragEvent() {}
74 
75     /**
76      * @brief Obtains the coordinates of the last position in the drag event.
77      * @return Returns the coordinates of the last position.
78      * @since 1.0
79      * @version 1.0
80      */
GetLastPoint()81     const Point& GetLastPoint() const
82     {
83         return lastPos_;
84     }
85 
86     /**
87      * @brief Obtains the start coordinates in the drag event.
88      * @return Returns the start coordinates.
89      * @since 1.0
90      * @version 1.0
91      */
GetStartPoint()92     const Point& GetStartPoint() const
93     {
94         return startPos_;
95     }
96 
97     /**
98      * @brief Sets the coordinates of the stay position before the last position in the drag event.
99      * @param preLastPos Indicates the coordinates of the stay position to set.
100      * @since 1.0
101      * @version 1.0
102      */
SetPreLastPoint(const Point & preLastPos)103     void SetPreLastPoint(const Point& preLastPos)
104     {
105         preLastPos_ = preLastPos;
106     }
107 
108     /**
109      * @brief Obtains the coordinates of the stay position before the last position in the drag event.
110      * @return Returns the coordinates of the stay position.
111      * @since 1.0
112      * @version 1.0
113      */
GetPreLastPoint()114     const Point& GetPreLastPoint() const
115     {
116         return preLastPos_;
117     }
118 
119     /**
120      * @brief Obtains the direction in the drag event.
121      * @return Returns the direction. Available values are as follows:
122      *         <b>0</b> indicates dragging from left to right.
123      *         <b>1</b> indicates dragging from right to left.
124      *         <b>2</b> indicates dragging from top to bottom.
125      *         <b>3</b> indicates dragging from bottom to top.
126      * @since 1.0
127      * @version 1.0
128      */
GetDragDirection()129     uint8_t GetDragDirection() const
130     {
131         if (MATH_ABS(curPos_.x - startPos_.x) >= MATH_ABS(curPos_.y - startPos_.y)) {
132             return (curPos_.x > startPos_.x) ? DIRECTION_LEFT_TO_RIGHT : DIRECTION_RIGHT_TO_LEFT;
133         } else {
134             return (curPos_.y > startPos_.y) ? DIRECTION_TOP_TO_BOTTOM : DIRECTION_BOTTOM_TO_TOP;
135         }
136     }
137 
138     /**
139      * @brief Obtains the difference between the current position and the last position of the view in the x-axis.
140      * @return Returns the different in the x-aix.
141      * @since 1.0
142      * @version 1.0
143      */
GetDeltaX()144     int16_t GetDeltaX() const
145     {
146         return deltaX_;
147     }
148 
149     /**
150      * @brief Obtains the difference between the current position and the last position of the view in the y-axis.
151      * @return Returns the different in the y-aix.
152      * @since 1.0
153      * @version 1.0
154      */
GetDeltaY()155     int16_t GetDeltaY() const
156     {
157         return deltaY_;
158     }
159 
160     static constexpr uint8_t DIRECTION_LEFT_TO_RIGHT = 0;
161     static constexpr uint8_t DIRECTION_RIGHT_TO_LEFT = 1;
162     static constexpr uint8_t DIRECTION_TOP_TO_BOTTOM = 2;
163     static constexpr uint8_t DIRECTION_BOTTOM_TO_TOP = 3;
164 
165 private:
166     Point lastPos_;
167     Point startPos_;
168     Point preLastPos_;
169     int16_t deltaX_;
170     int16_t deltaY_;
171 };
172 } // namespace OHOS
173 #endif // GRAPHIC_LITE_DRAG_EVENT_H
174