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_XL2DSCROLLCONTROLLER_H_
24 : #define XENOLITH_RENDERER_BASIC2D_XL2DSCROLLCONTROLLER_H_
25 :
26 : #include "XLComponent.h"
27 : #include "XL2d.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d {
30 :
31 : class ScrollItemHandle;
32 : class ScrollViewBase;
33 :
34 : class ScrollController : public Component {
35 : public:
36 : struct Item;
37 :
38 : /// Callback for node creation
39 : using NodeFunction = Function<Rc<Node>(const Item &)>;
40 : using RebuildCallback = Function<bool(ScrollController *)>; // return true if item was rebuilded
41 :
42 : struct Item {
43 : Item(NodeFunction &&, Vec2 pos, Size2 size, ZOrder zIndex, StringView);
44 :
45 : NodeFunction nodeFunction = nullptr;
46 : Size2 size;
47 : Vec2 pos;
48 : ZOrder zIndex;
49 : String name;
50 :
51 : Node *node = nullptr;
52 : ScrollItemHandle *handle = nullptr;
53 : };
54 :
55 : virtual ~ScrollController();
56 :
57 : virtual void onAdded(Node *owner) override;
58 : virtual void onRemoved() override;
59 : virtual void onContentSizeDirty() override;
60 :
61 : /// Scroll view callbacks handlers
62 : virtual void onScrollPosition(bool force = false);
63 : virtual void onScroll(float delta, bool eneded);
64 : virtual void onOverscroll(float delta);
65 :
66 : virtual float getScrollMin();
67 : virtual float getScrollMax();
68 :
69 : Node *getRoot() const;
70 : ScrollViewBase *getScroll() const;
71 :
72 : virtual void clear(); /// remove all items and non-strict barriers
73 : virtual void update(float position, float size); /// update current scroll position and size
74 : virtual void reset(float position, float size); /// set new scroll position and size
75 :
76 : /// Scrollable area size and offset are strict limits of scrollable area
77 : /// It's usefull when scroll parameters (offset, size, items positions) are known
78 : /// If scroll parameters are dynamic or determined in runtime - use barriers
79 :
80 : virtual bool setScrollableArea(float offset, float size);
81 : virtual float getScrollableAreaOffset() const; // NaN by default
82 : virtual float getScrollableAreaSize() const; // NaN by default
83 :
84 : /// you should return true if this call successfully rebuilds visible objects
85 : virtual bool rebuildObjects();
86 :
87 : virtual size_t size() const;
88 400 : virtual size_t addItem(NodeFunction &&, Size2 size, Vec2 pos, ZOrder zIndex = ZOrder(0), StringView tag = StringView());
89 5674 : virtual size_t addItem(NodeFunction &&, float size, float pos, ZOrder zIndex = ZOrder(0), StringView tag = StringView());
90 92 : virtual size_t addItem(NodeFunction &&, float size = 0.0f, ZOrder zIndex = ZOrder(0), StringView tag = StringView());
91 :
92 : virtual size_t addPlaceholder(Size2 size, Vec2 pos);
93 : virtual size_t addPlaceholder(float size, float pos);
94 : virtual size_t addPlaceholder(float size);
95 :
96 : const Item *getItem(size_t);
97 : const Item *getItem(Node *);
98 : const Item *getItem(StringView);
99 :
100 : size_t getItemIndex(Node *);
101 :
102 : const Vector<Item> &getItems() const;
103 : Vector<Item> &getItems();
104 :
105 : virtual void setScrollRelativeValue(float value);
106 :
107 : Node * getNodeByName(StringView) const;
108 : Node * getFrontNode() const;
109 : Node * getBackNode() const;
110 : Vector<Rc<Node>> getNodes() const;
111 :
112 : float getNextItemPosition() const;
113 :
114 : void setKeepNodes(bool);
115 : bool isKeepNodes() const;
116 :
117 : void resizeItem(const Item *node, float newSize, bool forward = true);
118 :
119 : void setAnimationPadding(float padding);
120 : void dropAnimationPadding();
121 : void updateAnimationPadding(float value);
122 :
123 : void setRebuildCallback(const RebuildCallback &);
124 : const RebuildCallback &getRebuildCallback() const;
125 :
126 : protected:
127 : virtual void onNextObject(Item &, float pos, float size); /// insert new object at specified position
128 :
129 : virtual void addScrollNode(Item &);
130 : virtual void updateScrollNode(Item &);
131 : virtual void removeScrollNode(Item &);
132 :
133 : ScrollViewBase *_scroll = nullptr;
134 : Node *_root = nullptr;
135 :
136 : float _scrollAreaOffset = 0.0f;
137 : float _scrollAreaSize = 0.0f;
138 :
139 : float _currentMin = 0.0f;
140 : float _currentMax = 0.0f;
141 :
142 : float _windowBegin = 0.0f;
143 : float _windowEnd = 0.0f;
144 :
145 : float _currentPosition = 0.0f;
146 : float _currentSize = 0.0f;
147 :
148 : Vector<Item> _nodes;
149 :
150 : bool _infoDirty = true;
151 : bool _keepNodes = false;
152 :
153 : float _animationPadding = 0.0f;
154 : float _savedSize = 0.0f;
155 :
156 : RebuildCallback _callback;
157 : };
158 :
159 : }
160 :
161 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2DSCROLLCONTROLLER_H_ */
|