forked from yvt/terravox
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathluaapi.cpp
More file actions
392 lines (355 loc) · 12.1 KB
/
luaapi.cpp
File metadata and controls
392 lines (355 loc) · 12.1 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "luaapi.h"
#include "terrain.h"
#include <QSharedPointer>
#include <QApplication>
#include "luaengine.h"
#include "terrainedit.h"
#include <QWidget>
#include <QHBoxLayout>
#include <QHash>
#include "labelslider.h"
#include <QSpinBox>
#include <QDoubleSpinBox>
#include "effectcontroller.h"
#include <QDebug>
#include <QSettings>
struct ToolEditor
{
struct Control
{
int id;
virtual ~Control() {}
virtual void setValue(double) {}
virtual double getValue() { return 0.; }
};
class IntSlider : public Control
{
LabelSlider *label;
QSpinBox *spinBox;
public:
IntSlider(QHBoxLayout *layout, QString text, int min, int max, bool log,
std::function<void(Control*)> onchange)
{
label = new LabelSlider();
spinBox = new QSpinBox();
label->setText(text + ":");
label->bindSpinBox(spinBox);
spinBox->setRange(min, max);
if (log)
label->setLogarithmic();
layout->addWidget(label);
layout->addWidget(spinBox);
spinBox->connect(spinBox, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, [=](int) {
onchange(this);
});
}
void setValue(double value) { spinBox->setValue(static_cast<int>(value)); }
double getValue() { return spinBox->value(); }
};
class RealSlider : public Control
{
LabelSlider *label;
QDoubleSpinBox *spinBox;
public:
RealSlider(QHBoxLayout *layout, QString text, double min, double max, int digits, bool log,
std::function<void(Control*)> onchange)
{
label = new LabelSlider();
spinBox = new QDoubleSpinBox();
label->setText(text + ":");
label->bindSpinBox(spinBox);
spinBox->setRange(min, max);
spinBox->setDecimals(digits);
if (log)
label->setLogarithmic();
layout->addWidget(label);
layout->addWidget(spinBox);
spinBox->connect(spinBox, (void(QDoubleSpinBox::*)(double))&QDoubleSpinBox::valueChanged, [=](double) {
onchange(this);
});
}
void setValue(double value) { spinBox->setValue(value);}
double getValue() { return spinBox->value(); }
};
QWidget *widget;
QHBoxLayout *layout;
QHash<int, QSharedPointer<Control>> controls;
int nextControlId = 1;
LuaHost &host;
std::vector<std::function<void()>> finalizers;
ToolEditor(LuaHost &host) :
widget(new QWidget()),
layout(new QHBoxLayout()),
host(host)
{
layout->setMargin(0);
widget->setLayout(layout);
}
int addIntSlider(std::function<void(Control*)> handler, QString label,
int minValue, int maxValue, bool logarithmic)
{
int id = nextControlId++;
QSharedPointer<IntSlider> ctrl = QSharedPointer<IntSlider>::create
(layout, label, minValue, maxValue, logarithmic,
handler);
ctrl->id = id;
controls[id] = ctrl;
return id;
}
int addRealSlider(std::function<void(Control*)> handler, QString label,
double minValue, double maxValue, int digits, bool logarithmic)
{
int id = nextControlId++;
QSharedPointer<RealSlider> ctrl = QSharedPointer<RealSlider>::create
(layout, label, minValue, maxValue, digits, logarithmic,
handler);
ctrl->id = id;
controls[id] = ctrl;
return id;
}
Control *controlById(int id)
{
return controls.value(id).data();
}
QWidget *takeWidget()
{
Q_ASSERT(widget);
QWidget *w = widget;
widget = nullptr;
return w;
}
~ToolEditor()
{
host.pcall([=]{
for (const auto &f: finalizers)
f();
});
if (widget)
widget->deleteLater();
qDebug() << "delete " << this;
}
};
struct LuaTerrainEdit
{
QSharedPointer<TerrainEdit> edit;
QSharedPointer<Terrain> terrain;
};
static TerrainHandle createHandle(QSharedPointer<Terrain> t)
{
return reinterpret_cast<TerrainHandle>(new QSharedPointer<Terrain>(t));
}
static TerrainEditHandle createHandle(LuaTerrainEdit e)
{
return reinterpret_cast<TerrainEditHandle>(new LuaTerrainEdit(e));
}
static ToolEditorHandle createHandle(QSharedPointer<ToolEditor> t)
{
return reinterpret_cast<ToolEditorHandle>(new QSharedPointer<ToolEditor>(t));
}
static QSharedPointer<Terrain> *fromTerrainHandle(TerrainHandle h)
{
return reinterpret_cast<QSharedPointer<Terrain> *>(h);
}
static LuaTerrainEdit *fromTerrainEditHandle(TerrainEditHandle h)
{
return reinterpret_cast<LuaTerrainEdit *>(h);
}
static QSharedPointer<ToolEditor> *fromToolEditorHandle(ToolEditorHandle h)
{
return reinterpret_cast<QSharedPointer<ToolEditor> *>(h);
}
static LuaHost *toHost(Host host)
{
return reinterpret_cast<LuaHost *>(host);
}
static QSettings *fromSettingsHandle(SettingsHandle h)
{
return reinterpret_cast<QSettings *>(h);
}
class LuaEffectController : public EffectController
{
QString name_;
TerravoxEffectController sctrl;
QSharedPointer<ToolEditor> editor;
TerravoxEffectApi api;
LuaHost host;
public:
LuaEffectController(QString name, TerravoxEffectControllerFactory factory, const LuaHost &host) :
name_(name),
host(host)
{
api.state = reinterpret_cast<void *>(this);
api.preview = [](void *self_) {
auto *self = reinterpret_cast<LuaEffectController *>(self_);
self->preview();
};
factory(&api, &sctrl);
if (sctrl.editor) {
editor = *fromToolEditorHandle(sctrl.editor); // copy handle
}
}
~LuaEffectController()
{
host.pcall([=]{
sctrl.destroy();
});
}
QString name() override { return name_; }
protected:
QWidget *createEffectEditor(Session *) Q_DECL_OVERRIDE
{
if (!editor) {
return nullptr;
}
return editor->takeWidget();
}
void applyEffect(QSharedPointer<Terrain> t, QSharedPointer<TerrainEdit> edit, Session *) Q_DECL_OVERRIDE
{
LuaTerrainEdit e;
e.edit = edit;
e.terrain = t;
edit->copyBeforeTo(QRect(QPoint(0, 0), t->size()), t.data(), t.data()); // restore to original
host.pcall([=]{
sctrl.apply(createHandle(e));
});
t->quantize(); // enforce quantization
}
};
LuaScriptInterface::LuaScriptInterface(LuaInterface *interface, std::function<bool(std::function<void ()>)> pcall)
{
auto &api = api_;
auto &host = host_;
host.interface = interface;
host.pcall = pcall;
api.host = reinterpret_cast<Host>(&host);
api.aboutQt = []() {
QApplication::aboutQt();
};
// Host api
api.registerEffect = [](Host host, const char *name, TerravoxEffectControllerFactory factory) {
auto *h = toHost(host);
QString qname = name;
h->interface->registerEffect(qname, [=]() -> EffectController * {
EffectController *ret = nullptr;
if (h->pcall([&](){ ret = new LuaEffectController(qname, factory, *h); })) {
return ret;
} else {
return nullptr;
}
});
};
// Setttings API
api.settingsOpen = [](const char *group) -> SettingsHandle {
auto *settings = new QSettings();
settings->beginGroup(group);
return reinterpret_cast<SettingsHandle>(settings);
};
api.settingsClose = [](SettingsHandle h) -> void {
delete fromSettingsHandle(h);
};
api.settingsSetValue = [](SettingsHandle h, const char *name, const char *value) -> void {
auto *settings = fromSettingsHandle(h);;
settings->setValue(name, value);
};
api.settingsGetValue = [](SettingsHandle h, const char *name) -> const char * {
auto *settings = fromSettingsHandle(h);
auto v = settings->value(name);
static QByteArray buffer;
if (v.isNull())
buffer.clear();
else
buffer = v.toString().toUtf8(); // FIXME: this isn't thread safe
return buffer.data();
};
// Terrain API
api.terrainCreate = [](int width, int height) -> TerrainHandle {
auto t = QSharedPointer<Terrain>::create(QSize(width, height));
return createHandle(t);
};
api.terrainRelease = [](TerrainHandle h) -> void {
delete fromTerrainHandle(h);
};
api.terrainQuantize = [](TerrainHandle h) -> void {
(*fromTerrainHandle(h))->quantize();
};
api.terrainGetColorData = [](TerrainHandle h) -> uint32_t* {
return (*fromTerrainHandle(h))->colorData();
};
api.terrainGetLandformData = [](TerrainHandle h) -> float* {
return (*fromTerrainHandle(h))->landformData();
};
api.terrainGetSize = [](TerrainHandle h, int *outDim) -> void {
auto &t = (*fromTerrainHandle(h));
outDim[0] = t->size().width();
outDim[1] = t->size().height();
};
api.terrainClone = [](TerrainHandle h) -> TerrainHandle {
auto &t = (*fromTerrainHandle(h));
QSharedPointer<Terrain> newTer = QSharedPointer<Terrain>::create(t->size());
newTer->copyFrom(t.data(), QPoint(0, 0));
return createHandle(newTer);
};
// TerrainEdit API
api.terrainEditRelease = [](TerrainEditHandle h) -> void {
delete fromTerrainEditHandle(h);
};
api.terrainEditBeginEdit = [](TerrainEditHandle h, int x, int y, int width, int height) -> void {
LuaTerrainEdit &e = *fromTerrainEditHandle(h);
e.edit->beginEdit(QRect(x, y, width, height), e.terrain.data());
};
api.terrainEditEndEdit = [](TerrainEditHandle h) -> void {
LuaTerrainEdit &e = *fromTerrainEditHandle(h);
e.edit->endEdit(e.terrain.data());
};
api.terrainEditGetTerrain = [](TerrainEditHandle h) -> TerrainHandle {
LuaTerrainEdit &e = *fromTerrainEditHandle(h);
return createHandle(e.terrain);
};
// ToolEditor API
api.toolEditorCreate = [](Host host) -> ToolEditorHandle {
auto *h = toHost(host);
return createHandle(QSharedPointer<ToolEditor>::create(*h));
};
api.toolEditorRelease = [](ToolEditorHandle h) -> void {
delete fromToolEditorHandle(h);
};
api.toolEditorAddFinalizer = [](ToolEditorHandle h, void (*fin)()) -> void {
(*fromToolEditorHandle(h))->finalizers.push_back(fin);
};
api.toolEditorAddIntegerSlider = [](ToolEditorHandle h, ToolEditorControlEventHandler callback, const char *text,
int minValue, int maxValue, int logarithmic) -> int {
QSharedPointer<ToolEditor> &e = *fromToolEditorHandle(h);
auto cb = [=](ToolEditor::Control *ctrl) {
if (callback)
e->host.pcall([=](){
callback(ctrl->id);
});
};
return e->addIntSlider(cb, text, minValue, maxValue, logarithmic);
};
api.toolEditorAddRealSlider = [](ToolEditorHandle h, ToolEditorControlEventHandler callback, const char *text,
double minValue, double maxValue, int digits, int logarithmic) -> int {
QSharedPointer<ToolEditor> &e = *fromToolEditorHandle(h);
auto cb = [=](ToolEditor::Control *ctrl) {
if (callback)
e->host.pcall([=](){
callback(ctrl->id);
});
};
return e->addRealSlider(cb, text, minValue, maxValue, digits, logarithmic);
};
api.toolEditorSetValue = [](ToolEditorHandle h, int controlId, double value) -> void {
QSharedPointer<ToolEditor> &e = *fromToolEditorHandle(h);
auto *ctrl = e->controlById(controlId);
if (ctrl)
ctrl->setValue(value);
};
api.toolEditorGetValue = [](ToolEditorHandle h, int controlId) -> double {
QSharedPointer<ToolEditor> &e = *fromToolEditorHandle(h);
auto *ctrl = e->controlById(controlId);
if (ctrl)
return ctrl->getValue();
else
return 0;
};
}