-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.cpp
372 lines (312 loc) · 7.93 KB
/
Test.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include<Siv3D.hpp>
#include"Input.hpp"
#include"NodeEditor.hpp"
#include"HamFramework.hpp"
class UpdateFrameNode : public NodeEditor::Node
{
public:
UpdateFrameNode()
{
cfgNextExecSocket({ U"" });
Name = U"Update";
CanDelete = false;
}
};
class BranchNode : public NodeEditor::Node
{
private:
void childRun() override
{
if (getInput<bool>(0))
{
NextExecIdx = 0;
}
else
{
NextExecIdx = 1;
}
}
public:
BranchNode()
{
cfgInputSockets({ {Type::getType<bool>(),U"Cond"} });
cfgPrevExecSocket({ U"" });
cfgNextExecSocket({ U"True",U"False" });
Name = U"Branch";
}
};
namespace Value
{
class IntegerNode : public NodeEditor::Node
{
private:
Optional<TextBox> m_textbox;
int m_value = 0;
void childRun() override
{
setOutput(0, m_value);
}
void childUpdate(const NodeEditor::Config& cfg, NodeEditor::Input& input) override
{
bool init = !m_textbox;
if (init)
{
m_textbox = TextBox(cfg.font, { 0,0 }, 100, unspecified, Format(m_value));
}
switch (m_textbox->update(!input.getProc() && MouseL.down()))
{
case TextBox::State::Active:
if (MouseL.down())
{
input.setProc();
}
break;
case TextBox::State::Enter:
case TextBox::State::Tab:
case TextBox::State::ShiftTab:
auto result = ParseIntOpt<int>(m_textbox->getText());
if (result)
{
m_value = result.value();
}
else
{
m_textbox->setText(U"0");
}
break;
}
if (init)
{
ChildSize = m_textbox->getRect().size;
}
}
void childDraw(const NodeEditor::Config&) override
{
if (m_textbox->isActive())
{
m_textbox->drawOverlay();
}
else
{
m_textbox->draw();
}
}
void childSerialize(JSONWriter& writer) const override
{
writer.write(m_value);
}
void childDeserialize(const JSONValue& json) override
{
m_value = json.get<decltype(m_value)>();
if (m_textbox)
{
m_textbox->setText(Format(m_value));
}
}
public:
IntegerNode()
{
cfgOutputSockets({ {Type::getType<int>(),U"Val"} });
ChildSize = SizeF(1, 0);
Name = U"Int32";
}
};
}
namespace Input
{
class KeyUpNode : public NodeEditor::Node
{
private:
void childRun() override
{
setOutput(0, KeyUp.down());
setOutput(1, KeyUp.pressed());
setOutput(2, KeyUp.up());
}
void childDraw(const NodeEditor::Config& cfg) override
{
ChildSize = InputDeviceSymbol::GetSize(KeyUp, cfg.font, cfg.font.fontSize() * 2);
InputDeviceSymbol::Draw(KeyUp, KeyUp.pressed(), { 0,0 }, cfg.font, cfg.font.fontSize() * 2);
}
public:
KeyUpNode()
{
cfgOutputSockets({ {Type::getType<bool>(),U"down"},{Type::getType<bool>(),U"pressed"},{Type::getType<bool>(),U"up"} });
ChildSize = SizeF(1, 0);
Name = U"KeyUp";
}
};
class KeyDownNode : public NodeEditor::Node
{
private:
void childRun() override
{
setOutput(0, KeyDown.down());
setOutput(1, KeyDown.pressed());
setOutput(2, KeyDown.up());
}
void childDraw(const NodeEditor::Config& cfg) override
{
ChildSize = InputDeviceSymbol::GetSize(KeyDown, cfg.font, cfg.font.fontSize() * 2);
InputDeviceSymbol::Draw(KeyDown, KeyDown.pressed(), { 0,0 }, cfg.font, cfg.font.fontSize() * 2);
}
public:
KeyDownNode()
{
cfgOutputSockets({ {Type::getType<bool>(),U"down"},{Type::getType<bool>(),U"pressed"},{Type::getType<bool>(),U"up"} });
ChildSize = SizeF(1, 0);
Name = U"KeyDown";
}
};
class KeyLeftNode : public NodeEditor::Node
{
private:
void childRun() override
{
setOutput(0, KeyLeft.down());
setOutput(1, KeyLeft.pressed());
setOutput(2, KeyLeft.up());
}
void childDraw(const NodeEditor::Config& cfg) override
{
ChildSize = InputDeviceSymbol::GetSize(KeyLeft, cfg.font, cfg.font.fontSize() * 2);
InputDeviceSymbol::Draw(KeyLeft, KeyLeft.pressed(), { 0,0 }, cfg.font, cfg.font.fontSize() * 2);
}
public:
KeyLeftNode()
{
cfgOutputSockets({ {Type::getType<bool>(),U"down"},{Type::getType<bool>(),U"pressed"},{Type::getType<bool>(),U"up"} });
ChildSize = SizeF(1, 0);
Name = U"KeyLeft";
}
};
class KeyRightNode : public NodeEditor::Node
{
private:
void childRun() override
{
setOutput(0, KeyRight.down());
setOutput(1, KeyRight.pressed());
setOutput(2, KeyRight.up());
}
void childDraw(const NodeEditor::Config& cfg) override
{
ChildSize = InputDeviceSymbol::GetSize(KeyRight, cfg.font, cfg.font.fontSize() * 2);
InputDeviceSymbol::Draw(KeyRight, KeyRight.pressed(), { 0,0 }, cfg.font, cfg.font.fontSize() * 2);
}
public:
KeyRightNode()
{
cfgOutputSockets({ {Type::getType<bool>(),U"down"},{Type::getType<bool>(),U"pressed"},{Type::getType<bool>(),U"up"} });
ChildSize = SizeF(1, 0);
Name = U"KeyRight";
}
};
}
void RegisterNodes(NodeEditor::NodeEditor& editor, P2Body& player)
{
editor.registerNodeType<UpdateFrameNode>(false);
editor.registerNodeType<BranchNode>();
editor.registerNodeType<Value::IntegerNode>();
editor.registerNodeType<Input::KeyUpNode>();
editor.registerNodeType<Input::KeyDownNode>();
editor.registerNodeType<Input::KeyLeftNode>();
editor.registerNodeType<Input::KeyRightNode>();
editor.registerNodeFunction<void(Point)>(U"Player::AddForce", { U"Point" }, [&](Point point)
{
return player.applyForce(point);
});
editor.registerNodeFunction<Point(int, int)>(U"Point::Point(int,int)", { U"Point",U"x",U"y" }, [](int x, int y)
{
return Point(x, y);
});
}
void Main()
{
Window::Resize(1280, 800);
const int gripHeight = 5;
bool gripGrab = false;
int editorHeight = 300;
bool editorVisible = true;
Size nodeEditorSize(Scene::Width(), editorHeight);
NodeEditor::NodeEditor editor(nodeEditorSize);
// 2D 物理演算
Camera2D camera(Vec2(0, 0), 20.0, Camera2DParameters::NoControl());
P2World world(9.8);
const P2Body line = world.createStaticLine(Vec2(0, 0), Line(-10, 0, 60, 0));
const P2Body block = world.createStaticRect({ 20,-1 }, SizeF(2, 2));
P2Body plus = world.createStaticPolygon({ 40,-6 }, Shape2D::Plus(5, 0.4).asPolygon());
P2Body player = world.createCircle({ 0,-5 }, 1);
// ノード登録
RegisterNodes(editor, player);
auto updateNode = *editor.addNode<UpdateFrameNode>();
while (System::Update())
{
// 2D カメラを更新
camera.update();
{
const auto t = camera.createTransformer();
line.draw(Palette::Skyblue);
block.draw(Palette::White);
plus.draw(Palette::White);
player.draw(Palette::Red);
}
auto gripRect = RectF(0, Scene::Height() - editorHeight - gripHeight, Scene::Width(), gripHeight);
auto visibleBtn = RectF(Arg::bottomRight = editorVisible ? gripRect.tr() : Scene::Size(), 40, 20);
if (visibleBtn.leftClicked())
{
editorVisible = !editorVisible;
}
visibleBtn.draw();
if (editorVisible)
{
if (gripRect.mouseOver())
{
Cursor::RequestStyle(CursorStyle::ResizeUpDown);
if (MouseL.down())
{
gripGrab = true;
}
}
if (gripGrab)
{
Cursor::RequestStyle(CursorStyle::ResizeUpDown);
editorHeight -= Cursor::Delta().y;
editorHeight = Clamp(editorHeight, 100, Scene::Height() - gripHeight);
if (!MouseL.pressed())
{
nodeEditorSize.y = editorHeight;
editor.resize(nodeEditorSize);
gripGrab = false;
}
}
const Vec2 editorPos(0, Scene::Height() - editorHeight);
editor.update(editorPos);
editor.draw(editorPos);
gripRect.draw();
}
updateNode->run();
plus.setAngle(Periodic::Sawtooth0_1(2s) * Math::TwoPi);
camera.setTargetCenter(player.getPos());
world.update();
if (SimpleGUI::Button(U"Save", { 10,10 }))
{
if (auto path = Dialog::SaveFile({ FileFilter::JSON() }, U"node.json"))
{
TextWriter(*path).write(editor.save());
}
}
if (SimpleGUI::Button(U"Load", { 10,50 }))
{
if (auto path = Dialog::OpenFile({ FileFilter::JSON() }, U"node.json"))
{
editor.load(JSONReader(*path));
//UpdateFrameNodeのインスタンスを検索
if (auto node = editor.searchNode(U"UpdateFrameNode"))
{
updateNode = std::dynamic_pointer_cast<UpdateFrameNode>(*node);
}
}
}
}
}