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_MATERIAL2D_BASE_MATERIALMENUSOURCE_H_
24 : #define XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALMENUSOURCE_H_
25 :
26 : #include "XLIcons.h"
27 : #include "SPSubscription.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::xenolith {
30 :
31 : class Node;
32 :
33 : }
34 :
35 : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d {
36 :
37 : class Button;
38 : class MenuSourceButton;
39 :
40 : class MenuSourceItem : public Subscription {
41 : public:
42 : enum class Type {
43 : Separator,
44 : Button,
45 : Custom,
46 : };
47 :
48 : using AttachCallback = Function<void(MenuSourceItem *, Node *)>;
49 :
50 : virtual bool init();
51 : virtual Rc<MenuSourceItem> copy() const;
52 :
53 : virtual void setCustomData(const Value &);
54 : virtual void setCustomData(Value &&);
55 : virtual const Value &getCustomData() const;
56 :
57 : virtual MenuSourceItem *setAttachCallback(const AttachCallback &);
58 : virtual MenuSourceItem *setDetachCallback(const AttachCallback &);
59 :
60 : virtual Type getType() const;
61 :
62 : virtual void handleNodeAttached(Node *);
63 : virtual void handleNodeDetached(Node *);
64 :
65 : void setDirty();
66 :
67 : protected:
68 : Type _type = Type::Separator;
69 : Value _customData;
70 :
71 : AttachCallback _attachCallback;
72 : AttachCallback _detachCallback;
73 : };
74 :
75 : class MenuSourceCustom : public MenuSourceItem {
76 : public:
77 : using FactoryFunction = Function<Rc<Node>(Node *, MenuSourceCustom *)>;
78 : using HeightFunction = Function<float(Node *, float)>;
79 :
80 : virtual bool init() override;
81 : virtual bool init(float h, const FactoryFunction &func, float minWidth = 0.0f);
82 : virtual bool init(const HeightFunction &h, const FactoryFunction &func, float minWidth = 0.0f);
83 :
84 : virtual Rc<MenuSourceItem> copy() const override;
85 :
86 : virtual float getMinWidth() const;
87 : virtual float getHeight(Node *, float) const;
88 : virtual const HeightFunction & getHeightFunction() const;
89 : virtual const FactoryFunction & getFactoryFunction() const;
90 :
91 : protected:
92 : float _minWidth = 0.0f;
93 : HeightFunction _heightFunction = nullptr;
94 : FactoryFunction _function = nullptr;
95 : };
96 :
97 : class MenuSource : public Subscription {
98 : public:
99 : using Callback = Function<void (Button *b, MenuSourceButton *)>;
100 : using FactoryFunction = MenuSourceCustom::FactoryFunction;
101 : using HeightFunction = MenuSourceCustom::HeightFunction;
102 :
103 62 : virtual bool init() { return true; }
104 : virtual ~MenuSource();
105 :
106 : void setHintCount(size_t);
107 : size_t getHintCount() const;
108 :
109 : Rc<MenuSource> copy() const;
110 :
111 : void addItem(MenuSourceItem *);
112 : MenuSourceButton *addButton(StringView, Callback && = nullptr);
113 : MenuSourceButton *addButton(StringView, IconName, Callback && = nullptr);
114 : MenuSourceButton *addButton(StringView, IconName, Rc<MenuSource> &&);
115 : MenuSourceCustom *addCustom(float h, const FactoryFunction &func, float minWidth = 0.0f);
116 : MenuSourceCustom *addCustom(const HeightFunction &h, const FactoryFunction &func, float minWidth = 0.0f);
117 : MenuSourceItem *addSeparator();
118 :
119 : void clear();
120 :
121 : uint32_t count();
122 :
123 : const Vector<Rc<MenuSourceItem>> &getItems() const;
124 :
125 : protected:
126 : Vector<Rc<MenuSourceItem>> _items;
127 : size_t _hintCount = 0;
128 : };
129 :
130 : class MenuSourceButton : public MenuSourceItem {
131 : public:
132 : using Callback = Function<void (Button *b, MenuSourceButton *)>;
133 :
134 : virtual ~MenuSourceButton();
135 :
136 : virtual bool init(StringView, IconName, Callback &&);
137 : virtual bool init(StringView, IconName, Rc<MenuSource> &&);
138 : virtual bool init() override;
139 : virtual Rc<MenuSourceItem> copy() const override;
140 :
141 : virtual void setName(StringView);
142 : virtual const String & getName() const;
143 :
144 : virtual void setValue(StringView);
145 : virtual const String & getValue() const;
146 :
147 : virtual void setNameIcon(IconName icon);
148 : virtual IconName getNameIcon() const;
149 :
150 : virtual void setValueIcon(IconName icon);
151 : virtual IconName getValueIcon() const;
152 :
153 : virtual void setCallback(Callback &&);
154 : virtual const Callback & getCallback() const;
155 :
156 : virtual void setNextMenu(MenuSource *);
157 : virtual MenuSource * getNextMenu() const;
158 :
159 : virtual void setSelected(bool value);
160 : virtual bool isSelected() const;
161 :
162 : protected:
163 : String _name;
164 : String _value;
165 :
166 : IconName _nameIcon = IconName::None;
167 : IconName _valueIcon = IconName::None;
168 :
169 : Rc<MenuSource> _nextMenu;
170 : Callback _callback = nullptr;
171 : bool _selected = false;
172 : };
173 :
174 : }
175 :
176 : #endif /* XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALMENUSOURCE_H_ */
|