Line data Source code
1 : /**
2 : Copyright (c) 2024 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_MATERIAL2D_LAYOUT_MATERIALMULTIVIEWLAYOUT_H_
24 : #define XENOLITH_RENDERER_MATERIAL2D_LAYOUT_MATERIALMULTIVIEWLAYOUT_H_
25 :
26 : #include "MaterialFlexibleLayout.h"
27 : #include "XLComponent.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d {
30 :
31 : class MultiViewLayout : public FlexibleLayout {
32 : public:
33 : class Generator : public Component {
34 : public:
35 : using IndexViewCallback = Function< Rc<ScrollView>(int64_t) >;
36 : using SequenceViewCallback = Function< Rc<ScrollView>(ScrollView *, int64_t viewIndex, int8_t offset) >;
37 :
38 : using ViewCallback = Function< void(ScrollView *, int64_t) >;
39 : using ProgressCallback = Function< void(ScrollView *, ScrollView *, float progress) >;
40 :
41 0 : virtual ~Generator() = default;
42 :
43 : virtual bool init();
44 : virtual bool init(size_t, const IndexViewCallback &);
45 : virtual bool init(const SequenceViewCallback &);
46 :
47 : virtual bool isViewLocked(ScrollView *, int64_t index);
48 :
49 : virtual Rc<ScrollView> makeIndexView(int64_t viewIndex);
50 : virtual Rc<ScrollView> makeNextView(ScrollView *, int64_t viewIndex);
51 : virtual Rc<ScrollView> makePrevView(ScrollView *, int64_t viewIndex);
52 :
53 : virtual bool isInfinite() const;
54 : virtual int64_t getViewCount() const;
55 :
56 : virtual void setViewSelectedCallback(const ViewCallback &);
57 : virtual void setViewCreatedCallback(const ViewCallback &);
58 : virtual void setApplyViewCallback(const ViewCallback &);
59 : virtual void setApplyProgressCallback(const ProgressCallback &);
60 :
61 : virtual void onViewSelected(ScrollView *current, int64_t id);
62 : virtual void onViewCreated(ScrollView *current, int64_t id);
63 :
64 : virtual void onApplyView(ScrollView *current, int64_t id);
65 : virtual void onApplyProgress(ScrollView *current, ScrollView *n, float progress);
66 :
67 : protected:
68 : MultiViewLayout *getLayout();
69 :
70 : size_t _viewCount = maxOf<size_t>();
71 : IndexViewCallback _makeViewByIndex;
72 : SequenceViewCallback _makeViewSeq;
73 : ViewCallback _viewSelectedCallback;
74 : ViewCallback _viewCreatedCallback;
75 : ViewCallback _applyViewCallback;
76 : ProgressCallback _applyProgressCallback;
77 : };
78 :
79 0 : virtual ~MultiViewLayout() = default;
80 :
81 : virtual bool init(Generator * = nullptr);
82 :
83 : virtual void onPush(SceneContent2d *l, bool replace) override;
84 :
85 : void setGenerator(Generator *);
86 : Generator *getGenerator() const;
87 :
88 : int64_t getCurrentIndex() const;
89 :
90 : void showNextView(float val = 0.35f);
91 : void showPrevView(float val = 0.35f);
92 :
93 : void showIndexView(int64_t, float val = 0.35f);
94 :
95 : protected:
96 : virtual Rc<ScrollView> makeInitialView();
97 :
98 : virtual bool onSwipeProgress();
99 : virtual bool beginSwipe(const Vec2 &diff);
100 : virtual bool setSwipeProgress(const Vec2 &delta);
101 : virtual bool endSwipeProgress(const Vec2 &delta, const Vec2 &velocity);
102 : virtual void onSwipeAction(Node *node);
103 :
104 : virtual void setNextView(int64_t id, float newProgress = 0.0f);
105 : virtual void setPrevView(int64_t id, float newProgress = 0.0f);
106 :
107 : virtual void onViewSelected(ScrollView *current, int64_t id);
108 :
109 : virtual void onPrevViewCreated(ScrollView *current, ScrollView *prev);
110 : virtual void onNextViewCreated(ScrollView *current, ScrollView *next);
111 :
112 : virtual void applyView(ScrollView *current);
113 : virtual void applyViewProgress(ScrollView *current, ScrollView *n, float progress);
114 :
115 : virtual void onBaseNode(const NodeParams &, const Padding &, float offset) override;
116 :
117 : float _currentPos = 0.0f;
118 : float _swipeProgress = 0.0f;
119 :
120 : int64_t _currentViewIndex = 0;
121 : ScrollView *_currentView = nullptr;
122 : ScrollView *_nextView = nullptr;
123 : ScrollView *_prevView = nullptr;
124 :
125 : InputListener *_swipeListener = nullptr;
126 : Generator *_generator = nullptr;
127 : };
128 :
129 : }
130 :
131 : #endif /* XENOLITH_RENDERER_MATERIAL2D_LAYOUT_MATERIALMULTIVIEWLAYOUT_H_ */
|