1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | #ifndef FLUTTER_LIB_UI_WINDOW_POINTER_DATA_PACKET_CONVERTER_H_ |
6 | #define FLUTTER_LIB_UI_WINDOW_POINTER_DATA_PACKET_CONVERTER_H_ |
7 | |
8 | #include <string.h> |
9 | |
10 | #include <map> |
11 | #include <memory> |
12 | #include <vector> |
13 | |
14 | #include "flutter/fml/macros.h" |
15 | #include "flutter/lib/ui/window/pointer_data_packet.h" |
16 | |
17 | namespace flutter { |
18 | |
19 | //------------------------------------------------------------------------------ |
20 | /// The current information about a pointer. This struct is used by |
21 | /// PointerDataPacketConverter to fill in necesarry information for raw pointer |
22 | /// packet sent from embedding. |
23 | /// |
24 | struct PointerState { |
25 | int64_t pointer_identifier; |
26 | bool isDown; |
27 | double physical_x; |
28 | double physical_y; |
29 | }; |
30 | |
31 | //------------------------------------------------------------------------------ |
32 | /// Converter to convert the raw pointer data packet from the platforms. |
33 | /// |
34 | /// Framework requires certain information to process pointer data. e.g. pointer |
35 | /// identifier and the delta of pointer moment. The converter keeps track each |
36 | /// pointer state and fill in those information appropriately. |
37 | /// |
38 | /// The converter is also resposible for providing a clean pointer data stream. |
39 | /// It will attempt to correct the stream if the it contains illegal pointer |
40 | /// transitions. |
41 | /// |
42 | /// Example 1 Missing Add: |
43 | /// |
44 | /// Down(position x) -> Up(position x) |
45 | /// |
46 | /// ###After Conversion### |
47 | /// |
48 | /// Synthesized_Add(position x) -> Down(position x) -> Up(position x) |
49 | /// |
50 | /// Example 2 Missing another move: |
51 | /// |
52 | /// Add(position x) -> Down(position x) -> Move(position y) -> |
53 | /// Up(position z) |
54 | /// |
55 | /// ###After Conversion### |
56 | /// |
57 | /// Add(position x) -> Down(position x) -> Move(position y) -> |
58 | /// Synthesized_Move(position z) -> Up(position z) |
59 | /// |
60 | /// Platform view is the only client that uses this class to convert all the |
61 | /// incoming pointer packet and is responsible for the life cycle of its |
62 | /// instance. |
63 | /// |
64 | class PointerDataPacketConverter { |
65 | public: |
66 | PointerDataPacketConverter(); |
67 | ~PointerDataPacketConverter(); |
68 | |
69 | //---------------------------------------------------------------------------- |
70 | /// @brief Converts pointer data packet into a form that framework |
71 | /// understands. The raw pointer data packet from embedding does |
72 | /// not have sufficient information and may contain illegal |
73 | /// pointer transitions. This method will fill out that |
74 | /// information and attempt to correct pointer transitions. |
75 | /// |
76 | /// @param[in] packet The raw pointer packet sent from |
77 | /// embedding. |
78 | /// |
79 | /// @return A full converted packet with all the required information |
80 | /// filled. |
81 | /// It may contain synthetic pointer data as the result of |
82 | /// converter's attempt to correct illegal pointer transitions. |
83 | /// |
84 | std::unique_ptr<PointerDataPacket> Convert( |
85 | std::unique_ptr<PointerDataPacket> packet); |
86 | |
87 | private: |
88 | std::map<int64_t, PointerState> states_; |
89 | |
90 | int64_t pointer_; |
91 | |
92 | void ConvertPointerData(PointerData pointer_data, |
93 | std::vector<PointerData>& converted_pointers); |
94 | |
95 | PointerState EnsurePointerState(PointerData pointer_data); |
96 | |
97 | void UpdateDeltaAndState(PointerData& pointer_data, PointerState& state); |
98 | |
99 | void UpdatePointerIdentifier(PointerData& pointer_data, |
100 | PointerState& state, |
101 | bool start_new_pointer); |
102 | |
103 | bool LocationNeedsUpdate(const PointerData pointer_data, |
104 | const PointerState state); |
105 | |
106 | FML_DISALLOW_COPY_AND_ASSIGN(PointerDataPacketConverter); |
107 | }; |
108 | |
109 | } // namespace flutter |
110 | |
111 | #endif // FLUTTER_LIB_UI_WINDOW_POINTER_DATA_PACKET_CONVERTER_H_ |
112 | |