Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef XENOLITH_RENDERER_BASIC2D_XL2DSCROLLVIEWBASE_H_
24 : #define XENOLITH_RENDERER_BASIC2D_XL2DSCROLLVIEWBASE_H_
25 :
26 : #include "XL2d.h"
27 : #include "XLDynamicStateNode.h"
28 : #include "XL2dActionAcceleratedMove.h"
29 :
30 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d {
31 :
32 : class ScrollController;
33 : class ActionAcceleratedMove;
34 :
35 : class ScrollViewBase : public DynamicStateNode {
36 : public:
37 : using ScrollFilterCallback = std::function<float (float delta)>;
38 : using ScrollCallback = std::function<void (float delta, bool finished)>;
39 : using OverscrollCallback = std::function<void (float delta)>;
40 :
41 : enum Layout {
42 : Vertical,
43 : Horizontal
44 : };
45 :
46 46 : virtual ~ScrollViewBase() { }
47 :
48 : virtual bool init(Layout l);
49 : virtual void setLayout(Layout l);
50 :
51 : virtual bool visitDraw(FrameInfo &, NodeFlags parentFlags) override;
52 :
53 : virtual void onEnter(Scene *) override;
54 :
55 1741898 : inline bool isVertical() const { return _layout == Vertical; }
56 320 : inline bool isHorizontal() const { return _layout == Horizontal; }
57 20 : virtual Layout getLayout() const { return _layout; }
58 :
59 : virtual void onContentSizeDirty() override;
60 : virtual void onTransformDirty(const Mat4 &) override;
61 :
62 : virtual void setEnabled(bool value);
63 : virtual bool isEnabled() const;
64 :
65 : virtual bool isInMotion() const;
66 : virtual bool isMoved() const;
67 :
68 : virtual void setScrollCallback(const ScrollCallback & cb);
69 : virtual const ScrollCallback &getScrollCallback() const;
70 :
71 : virtual void setOverscrollCallback(const OverscrollCallback & cb);
72 : virtual const OverscrollCallback &getOverscrollCallback() const;
73 :
74 68 : virtual Node *getRoot() const { return _root; }
75 0 : virtual InputListener *getInputListener() const { return _inputListener; }
76 :
77 : virtual bool addComponentItem(Component *) override;
78 :
79 : template <typename T>
80 12 : auto setController(const Rc<T> &ptr) -> T * {
81 12 : setController(ptr.get());
82 12 : return ptr.get();
83 : }
84 :
85 : virtual void setController(ScrollController *c);
86 : virtual ScrollController *getController();
87 :
88 : virtual void setPadding(const Padding &);
89 : virtual const Padding &getPadding() const;
90 :
91 : virtual void setSpaceLimit(float, float padding = 0.0f);
92 : virtual float getSpaceLimit() const;
93 : virtual float getSpacePadding() const;
94 :
95 : virtual float getScrollableAreaOffset() const; // NaN by default
96 : virtual float getScrollableAreaSize() const; // NaN by default
97 :
98 : virtual Vec2 getPositionForNode(float scrollPos) const;
99 : virtual Size2 getContentSizeForNode(float size) const;
100 : virtual Vec2 getAnchorPointForNode() const;
101 :
102 : virtual float getNodeScrollSize(Size2) const;
103 : virtual float getNodeScrollPosition(Vec2) const;
104 :
105 : virtual bool addScrollNode(Node *, Vec2 pos, Size2 size, ZOrder z, StringView name);
106 : virtual void updateScrollNode(Node *, Vec2 pos, Size2 size, ZOrder z, StringView name);
107 : virtual bool removeScrollNode(Node *);
108 :
109 : virtual float getDistanceFromStart() const;
110 :
111 : virtual void setScrollMaxVelocity(float value);
112 : virtual float getScrollMaxVelocity() const;
113 :
114 : virtual Node * getFrontNode() const;
115 : virtual Node * getBackNode() const;
116 :
117 : virtual Vec2 convertFromScrollableSpace(const Vec2 &);
118 : virtual Vec2 convertToScrollableSpace(const Vec2 &);
119 :
120 : virtual Vec2 convertFromScrollableSpace(Node *, Vec2);
121 : virtual Vec2 convertToScrollableSpace(Node *, Vec2);
122 :
123 : // internal, use with care
124 : virtual float getScrollMinPosition() const; // NaN by default
125 : virtual float getScrollMaxPosition() const; // NaN by default
126 : virtual float getScrollLength() const; // NaN by default
127 : virtual float getScrollSize() const;
128 :
129 : virtual void setScrollRelativePosition(float value);
130 : virtual float getScrollRelativePosition() const;
131 : virtual float getScrollRelativePosition(float pos) const;
132 :
133 : virtual void setScrollPosition(float pos);
134 : virtual float getScrollPosition() const;
135 :
136 : virtual Vec2 getPointForScrollPosition(float pos);
137 :
138 : virtual void setScrollDirty(bool value);
139 : virtual void updateScrollBounds();
140 :
141 : // common overload points
142 : virtual void onDelta(float delta);
143 : virtual void onOverscrollPerformed(float velocity, float pos, float boundary);
144 :
145 : virtual bool onSwipeEventBegin(uint32_t id, const Vec2 &loc, const Vec2 &d, const Vec2 &v);
146 : virtual bool onSwipeEvent(uint32_t id, const Vec2 &loc, const Vec2 &d, const Vec2 &v);
147 : virtual bool onSwipeEventEnd(uint32_t id, const Vec2 &loc, const Vec2 &d, const Vec2 &v);
148 :
149 : virtual void onSwipeBegin();
150 : virtual bool onSwipe(float delta, float velocity, bool ended);
151 : virtual void onAnimationFinished();
152 :
153 : virtual Rc<ActionInterval> onSwipeFinalizeAction(float velocity);
154 :
155 : virtual void onScroll(float delta, bool finished);
156 : virtual void onOverscroll(float delta);
157 : virtual void onPosition();
158 : virtual void doSetScrollPosition(float pos);
159 :
160 : virtual void fixPosition();
161 :
162 : virtual bool onPressBegin(const Vec2 &);
163 : virtual bool onLongPress(const Vec2 &, const TimeInterval &, int count);
164 : virtual bool onPressEnd(const Vec2 &, const TimeInterval &);
165 : virtual bool onPressCancel(const Vec2 &, const TimeInterval &);
166 :
167 : virtual void onTap(int count, const Vec2 &loc);
168 :
169 : protected:
170 : using DynamicStateNode::init;
171 :
172 : Layout _layout = Vertical;
173 :
174 : enum class Movement {
175 : None,
176 : Manual,
177 : Auto,
178 : Overscroll
179 : } _movement = Movement::None;
180 :
181 : float _scrollPosition = 0.0f; // current position of scrolling
182 : float _scrollSize = 0.0f; // size of scrolling area
183 :
184 : float _savedRelativePosition = nan();
185 :
186 : float _scrollMin = nan();
187 : float _scrollMax = nan();
188 :
189 : float _maxVelocity = nan();
190 : float _scrollSpaceLimit = nan();
191 : float _scrollSpacePadding = 0.0f;
192 :
193 : Vec2 _globalScale; // values to scale input gestures
194 :
195 : Node *_root = nullptr;
196 : ScrollFilterCallback _scrollFilter = nullptr;
197 : ScrollCallback _scrollCallback = nullptr;
198 : OverscrollCallback _overscrollCallback = nullptr;
199 : InputListener *_inputListener = nullptr;
200 : ScrollController *_controller;
201 :
202 : Rc<ActionInterval> _animationAction;
203 : Rc<ActionAcceleratedMove> _movementAction;
204 :
205 : bool _bounce = false;
206 : bool _scrollDirty = true;
207 : bool _animationDirty = false;
208 :
209 : Padding _paddingGlobal;
210 : };
211 :
212 : }
213 :
214 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2DSCROLLVIEWBASE_H_ */
|