-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContextMenu.hpp
283 lines (237 loc) · 9.36 KB
/
ContextMenu.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#pragma once
#include "API.hpp"
#include <Geode/DefaultInclude.hpp>
#include <Geode/loader/Dispatch.hpp>
#include <Geode/utils/cocos.hpp>
namespace mouse {
struct ContextMenuStyle {
float padding = 3.f;
float height = 15.f;
float minWidth = 50.f;
float maxWidth = 150.f;
float maxHeight = 140.f;
float itemGap = 0.f;
std::string fontName = "chatFont.fnt";
std::string arrowSprite = "geode.mouse-api/arrow.png";
bool flipArrow = false;
float arrowSize = 5.5f;
std::string bgSprite = "square02b_small.png";
bool bgSpriteIsFrame = false;
std::string hoverSprite = "square02b_small.png";
cocos2d::ccColor4B bgColor = { 0, 0, 0, 215 };
cocos2d::ccColor4B textColor = { 255, 255, 255, 255 };
cocos2d::ccColor4B hoverColor = { 255, 255, 255, 45 };
cocos2d::ccColor4B arrowColor = { 255, 255, 255, 255 };
};
}
// Defined inline so mods that depend on mouse-api optionally can convert
// styles to JSON without needing to link
// Also converting the styles over as JSON ensures ABI compatability
template <>
struct json::Serialize<mouse::ContextMenuStyle> {
static inline json::Value to_json(mouse::ContextMenuStyle const& style) {
return json::Object {
{ "padding", style.padding },
{ "height", style.height },
{ "minWidth", style.minWidth },
{ "maxWidth", style.maxWidth },
{ "maxHeight", style.maxHeight },
{ "itemGap", style.itemGap },
{ "fontName", style.fontName },
{ "arrowSprite", style.arrowSprite },
{ "flipArrow", style.flipArrow },
{ "arrowSize", style.arrowSize },
{ "bgSprite", style.bgSprite },
{ "bgSpriteIsFrame", style.bgSpriteIsFrame },
{ "hoverSprite", style.hoverSprite },
{ "bgColor", style.bgColor },
{ "textColor", style.textColor },
{ "hoverColor", style.hoverColor },
{ "arrowColor", style.arrowColor },
};
}
static inline mouse::ContextMenuStyle from_json(json::Value const& json) {
return mouse::ContextMenuStyle {
.padding = static_cast<float>(json["padding"].as_double()),
.height = static_cast<float>(json["height"].as_double()),
.minWidth = static_cast<float>(json["minWidth"].as_double()),
.maxWidth = static_cast<float>(json["maxWidth"].as_double()),
.maxHeight = static_cast<float>(json["maxHeight"].as_double()),
.itemGap = static_cast<float>(json["itemGap"].as_double()),
.fontName = json["fontName"].as_string(),
.arrowSprite = json["arrowSprite"].as_string(),
.flipArrow = json["flipArrow"].as_bool(),
.arrowSize = static_cast<float>(json["arrowSize"].as_double()),
.bgSprite = json["bgSprite"].as_string(),
.bgSpriteIsFrame = json["bgSpriteIsFrame"].as_bool(),
.hoverSprite = json["hoverSprite"].as_string(),
.bgColor = json["bgColor"].template as<cocos2d::ccColor4B>(),
.textColor = json["textColor"].template as<cocos2d::ccColor4B>(),
.hoverColor = json["hoverColor"].template as<cocos2d::ccColor4B>(),
.arrowColor = json["arrowColor"].template as<cocos2d::ccColor4B>(),
};
}
};
namespace mouse {
class ContextMenu;
class ActionMenuItem;
using ContextMenuEvent = geode::DispatchEvent<cocos2d::CCNode*>;
using ContextMenuFilter = geode::DispatchFilter<cocos2d::CCNode*>;
using ContextMenuDragEvent = geode::DispatchEvent<cocos2d::CCNode*, float>;
using ContextMenuDragFilter = geode::DispatchFilter<cocos2d::CCNode*, float>;
using ContextMenuDragInitEvent = geode::DispatchEvent<cocos2d::CCNode*, float*>;
using ContextMenuDragInitFilter = geode::DispatchFilter<cocos2d::CCNode*, float*>;
// Defined inline so this can be used without linking
struct ContextMenuBuilder {
json::Value result = json::Object {
{ "items", json::Array() },
};
inline ContextMenuBuilder& setStyle(ContextMenuStyle const& style) {
this->result["style"] = style;
return *this;
}
inline ContextMenuBuilder& addItem(
std::string const& text,
std::string const& callbackID,
float ratio = 1.f
) {
this->result["items"].as_array().push_back(json::Object {
{ "text", text },
{ "click", callbackID },
{ "ratio", ratio },
});
return *this;
}
inline ContextMenuBuilder& addItem(
std::string const& iconFrame,
std::string const& text,
std::string const& callbackID,
float ratio = 1.f
) {
this->result["items"].as_array().push_back(json::Object {
{ "text", text },
{ "frame", iconFrame },
{ "click", callbackID },
{ "ratio", ratio },
});
return *this;
}
inline ContextMenuBuilder& addSubMenu(
std::string const& text,
ContextMenuBuilder& subMenu
) {
this->result["items"].as_array().push_back(json::Object {
{ "text", text },
{ "sub-menu", subMenu },
});
return *this;
}
inline ContextMenuBuilder& addSubMenu(
std::string const& iconFrame,
std::string const& text,
ContextMenuBuilder& subMenu
) {
this->result["items"].as_array().push_back(json::Object {
{ "text", text },
{ "frame", iconFrame },
{ "sub-menu", subMenu },
});
return *this;
}
inline operator json::Value() {
return this->result;
}
};
inline ContextMenuBuilder buildContextMenu() {
return ContextMenuBuilder();
}
class MOUSEAPI_DLL ContextMenuItem : public cocos2d::CCNode {
protected:
ContextMenu* m_parentMenu;
cocos2d::CCNode* m_icon = nullptr;
cocos2d::CCLabelBMFont* m_label = nullptr;
cocos2d::extension::CCScale9Sprite* m_hoverBG;
cocos2d::CCPoint m_lastDrag;
bool m_dragged = false;
float m_ratio = 1.f;
bool init(ContextMenu* menu);
void draw() override;
friend class ContextMenu;
public:
virtual void setIcon(cocos2d::CCNode* icon);
virtual void setText(std::string const& text);
virtual void setRatio(float ratio);
virtual float getPreferredWidth();
virtual void fitToWidth(float width);
virtual bool isHovered();
virtual void hide();
virtual void select();
virtual void drag(float delta);
};
using ItemRef = geode::Ref<ContextMenuItem>;
class MOUSEAPI_DLL ActionMenuItem : public ContextMenuItem {
protected:
std::string m_eventID;
bool init(ContextMenu* menu, std::string const& eventID);
public:
static ActionMenuItem* create(ContextMenu* menu, std::string const& eventID);
void select() override;
};
class MOUSEAPI_DLL DragMenuItem : public ContextMenuItem {
protected:
std::string m_text;
std::string m_eventID;
float m_value = 0.f;
float m_rate = 1.f;
float m_precision = .25f;
bool init(ContextMenu* menu, std::string const& eventID);
void updateText();
public:
static DragMenuItem* create(ContextMenu* menu, std::string const& eventID);
void setText(std::string const& text) override;
void setValue(float value);
void setRate(float rate);
void setPrecision(float precision);
void drag(float delta) override;
};
class MOUSEAPI_DLL SubMenuItem : public ContextMenuItem {
protected:
json::Value m_menuJson;
ContextMenu* m_menu = nullptr;
cocos2d::CCSprite* m_arrow;
bool init(ContextMenu* menu, json::Value const& json);
void draw() override;
public:
static SubMenuItem* create(ContextMenu* menu, json::Value const& json);
float getPreferredWidth() override;
void fitToWidth(float width) override;
bool isHovered() override;
void select() override;
void hide() override;
};
class MOUSEAPI_DLL ContextMenu : public cocos2d::CCNode {
protected:
geode::Ref<cocos2d::CCNode> m_target;
std::vector<ItemRef> m_items;
cocos2d::CCNode* m_container;
cocos2d::extension::CCScale9Sprite* m_bg;
ContextMenuStyle m_style;
ContextMenu* m_parentMenu = nullptr;
bool init(cocos2d::CCNode* target, json::Value const& json, ContextMenu* parent);
ActionMenuItem* createError(std::string const& msg);
void parseItems(json::Value const& value);
public:
static ContextMenu* create(
cocos2d::CCNode* target,
json::Value const& json,
ContextMenu* parent = nullptr
);
cocos2d::CCNode* getTarget() const;
ContextMenuStyle const& getStyle() const;
ContextMenu* getParentMenu() const;
ContextMenu* getTopMostMenu() const;
bool isHovered();
void show(cocos2d::CCPoint const& pos);
void hide();
};
}