1 #pragma once
2 
3 #include <mbgl/map/camera.hpp>
4 #include <mbgl/map/map_observer.hpp>
5 #include <mbgl/map/mode.hpp>
6 #include <mbgl/map/transform_state.hpp>
7 #include <mbgl/util/chrono.hpp>
8 #include <mbgl/util/geo.hpp>
9 #include <mbgl/util/noncopyable.hpp>
10 #include <mbgl/util/optional.hpp>
11 
12 #include <cstdint>
13 #include <cmath>
14 #include <functional>
15 
16 namespace mbgl {
17 
18 class Transform : private util::noncopyable {
19 public:
20     Transform(MapObserver& = MapObserver::nullObserver(),
21               ConstrainMode = ConstrainMode::HeightOnly,
22               ViewportMode = ViewportMode::Default);
23 
Transform(const TransformState & state_)24     Transform(const TransformState &state_) : observer(MapObserver::nullObserver()), state(state_) {}
25 
26     // Map view
27     void resize(Size size);
28 
29     // Camera
30     /** Returns the current camera options. */
31     CameraOptions getCameraOptions(const EdgeInsets&) const;
32 
33     /** Instantaneously, synchronously applies the given camera options. */
34     void jumpTo(const CameraOptions&);
35     /** Asynchronously transitions all specified camera options linearly along
36         an optional time curve. */
37     void easeTo(const CameraOptions&, const AnimationOptions& = {});
38     /** Asynchronously zooms out, pans, and zooms back into the given camera
39         along a great circle, as though the viewer is riding a supersonic
40         jetcopter. */
41     void flyTo(const CameraOptions&, const AnimationOptions& = {});
42 
43     // Position
44 
45     /** Pans the map by the given amount.
46         @param offset The distance to pan the map by, measured in pixels from
47             top to bottom and from left to right. */
48     void moveBy(const ScreenCoordinate& offset, const AnimationOptions& = {});
49     void setLatLng(const LatLng&, const AnimationOptions& = {});
50     void setLatLng(const LatLng&, const EdgeInsets&, const AnimationOptions& = {});
51     void setLatLng(const LatLng&, optional<ScreenCoordinate>, const AnimationOptions& = {});
52     void setLatLngZoom(const LatLng&, double zoom, const AnimationOptions& = {});
53     void setLatLngZoom(const LatLng&, double zoom, const EdgeInsets&, const AnimationOptions& = {});
54     LatLng getLatLng(const EdgeInsets& = {}) const;
55     ScreenCoordinate getScreenCoordinate(const EdgeInsets& = {}) const;
56 
57     // Bounds
58 
59     void setLatLngBounds(optional<LatLngBounds>);
60     void setMinZoom(double);
61     void setMaxZoom(double);
62     void setMinPitch(double);
63     void setMaxPitch(double);
64 
65     // Zoom
66 
67     /** Sets the zoom level, keeping the given point fixed within the view.
68         @param zoom The new zoom level. */
69     void setZoom(double zoom, const AnimationOptions& = {});
70     /** Sets the zoom level, keeping the given point fixed within the view.
71         @param zoom The new zoom level.
72         @param anchor A point relative to the top-left corner of the view.
73             If unspecified, the center point is fixed within the view. */
74     void setZoom(double zoom, optional<ScreenCoordinate> anchor, const AnimationOptions& = {});
75     /** Sets the zoom level, keeping the center point fixed within the inset view.
76         @param zoom The new zoom level.
77         @param padding The viewport padding that affects the fixed center point. */
78     void setZoom(double zoom, const EdgeInsets& padding, const AnimationOptions& = {});
79     /** Returns the zoom level. */
80     double getZoom() const;
81 
82     // Angle
83 
84     void rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& second, const AnimationOptions& = {});
85     /** Sets the angle of rotation.
86         @param angle The new angle of rotation, measured in radians
87             counterclockwise from true north. */
88     void setAngle(double angle, const AnimationOptions& = {});
89     /** Sets the angle of rotation, keeping the given point fixed within the view.
90         @param angle The new angle of rotation, measured in radians
91             counterclockwise from true north.
92         @param anchor A point relative to the top-left corner of the view. */
93     void setAngle(double angle, optional<ScreenCoordinate> anchor, const AnimationOptions& = {});
94     /** Sets the angle of rotation, keeping the center point fixed within the inset view.
95         @param angle The new angle of rotation, measured in radians
96             counterclockwise from true north.
97         @param padding The viewport padding that affects the fixed center point. */
98     void setAngle(double angle, const EdgeInsets& padding, const AnimationOptions& = {});
99     /** Returns the angle of rotation.
100         @return The angle of rotation, measured in radians counterclockwise from
101             true north. */
102     double getAngle() const;
103 
104     // Pitch
105     /** Sets the pitch angle.
106         @param angle The new pitch angle, measured in radians toward the
107             horizon. */
108     void setPitch(double pitch, const AnimationOptions& = {});
109     /** Sets the pitch angle, keeping the given point fixed within the view.
110         @param angle The new pitch angle, measured in radians toward the
111             horizon.
112         @param anchor A point relative to the top-left corner of the view. */
113     void setPitch(double pitch, optional<ScreenCoordinate> anchor, const AnimationOptions& = {});
114     double getPitch() const;
115 
116     // North Orientation
117     void setNorthOrientation(NorthOrientation);
118     NorthOrientation getNorthOrientation() const;
119 
120     // Constrain mode
121     void setConstrainMode(ConstrainMode);
122     ConstrainMode getConstrainMode() const;
123 
124     // Viewport mode
125     void setViewportMode(ViewportMode);
126     ViewportMode getViewportMode() const;
127 
128     // Projection mode
129     void setAxonometric(bool);
130     bool getAxonometric() const;
131     void setXSkew(double xSkew);
132     double getXSkew() const;
133     void setYSkew(double ySkew);
134     double getYSkew() const;
135 
136     // Transitions
137     bool inTransition() const;
138     void updateTransitions(const TimePoint& now);
getTransitionStart() const139     TimePoint getTransitionStart() const { return transitionStart; }
getTransitionDuration() const140     Duration getTransitionDuration() const { return transitionDuration; }
141     void cancelTransitions();
142 
143     // Gesture
144     void setGestureInProgress(bool);
isGestureInProgress() const145     bool isGestureInProgress() const { return state.isGestureInProgress(); }
146 
147     // Transform state
getState() const148     const TransformState& getState() const { return state; }
isRotating() const149     bool isRotating() const { return state.isRotating(); }
isScaling() const150     bool isScaling() const { return state.isScaling(); }
isPanning() const151     bool isPanning() const { return state.isPanning(); }
152 
153     // Conversion and projection
154     ScreenCoordinate latLngToScreenCoordinate(const LatLng&) const;
155     LatLng screenCoordinateToLatLng(const ScreenCoordinate&) const;
156 
157 private:
158     MapObserver& observer;
159     TransformState state;
160 
161     void startTransition(const CameraOptions&,
162                          const AnimationOptions&,
163                          std::function<void(double)>,
164                          const Duration&);
165 
166     TimePoint transitionStart;
167     Duration transitionDuration;
168     std::function<bool(const TimePoint)> transitionFrameFn;
169     std::function<void()> transitionFinishFn;
170 };
171 
172 } // namespace mbgl
173