-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathrive_lua_libs.hpp
More file actions
1344 lines (1159 loc) · 35.2 KB
/
rive_lua_libs.hpp
File metadata and controls
1344 lines (1159 loc) · 35.2 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef WITH_RIVE_SCRIPTING
#ifndef _RIVE_LUA_LIBS_HPP_
#define _RIVE_LUA_LIBS_HPP_
#include "lua.h"
#include "lualib.h"
#include "rive/animation/linear_animation_instance.hpp"
#include "rive/assets/file_asset.hpp"
#include "rive/assets/script_asset.hpp"
#include "rive/lua/lua_state.hpp"
#include "rive/math/raw_path.hpp"
#include "rive/renderer.hpp"
#include "rive/math/vec2d.hpp"
#include "rive/math/contour_measure.hpp"
#include "rive/math/path_measure.hpp"
#include "rive/shapes/paint/image_sampler.hpp"
#include "rive/shapes/paint/shape_paint.hpp"
#include "rive/viewmodel/data_enum.hpp"
#include "rive/viewmodel/viewmodel_instance_boolean.hpp"
#include "rive/viewmodel/viewmodel_instance_color.hpp"
#include "rive/viewmodel/viewmodel_instance_enum.hpp"
#include "rive/viewmodel/viewmodel_instance_value.hpp"
#include "rive/viewmodel/viewmodel_instance_viewmodel.hpp"
#include "rive/viewmodel/viewmodel_instance_color.hpp"
#include "rive/viewmodel/viewmodel_instance_number.hpp"
#include "rive/viewmodel/viewmodel_instance_string.hpp"
#include "rive/viewmodel/viewmodel_instance_trigger.hpp"
#include "rive/viewmodel/viewmodel_instance_list.hpp"
#include "rive/data_bind/data_values/data_value.hpp"
#include "rive/data_bind/data_values/data_value_boolean.hpp"
#include "rive/data_bind/data_values/data_value_color.hpp"
#include "rive/data_bind/data_values/data_value_list.hpp"
#include "rive/data_bind/data_values/data_value_number.hpp"
#include "rive/data_bind/data_values/data_value_string.hpp"
#include "rive/viewmodel/viewmodel.hpp"
#include "rive/hit_result.hpp"
#include "rive/lua/scripting_vm.hpp"
#include "rive/refcnt.hpp"
#ifdef WITH_RIVE_AUDIO
#include "rive/audio/audio_engine.hpp"
#include "rive/audio/audio_source.hpp"
#include "rive/audio/audio_sound.hpp"
#endif
#ifdef WITH_RIVE_TOOLS
#include "rive/core/binary_writer.hpp"
#include "rive/core/vector_binary_stream.hpp"
#endif
#include <chrono>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <string>
#include <vector>
static const int maxCStack = 8000;
static const int luaGlobalsIndex = -maxCStack - 2002;
static const int luaRegistryIndex = -maxCStack - 2000;
namespace rive
{
class Artboard;
class ArtboardInstance;
class Factory;
class File;
class ModuleDetails;
class ScriptedObject;
class StateMachineInstance;
class TransformComponent;
enum class LuaAtoms : int16_t
{
// Vector
length,
lengthSquared,
normalized,
distance,
distanceSquared,
dot,
lerp,
// Path
moveTo,
lineTo,
quadTo,
cubicTo,
close,
reset,
add,
contours,
measure,
// Path Command
type,
points,
// Mat2D
invert,
isIdentity,
// Image
width,
height,
// ImageSampler
clamp,
repeat,
mirror,
bilinear,
nearest,
// Paint
style,
join,
cap,
thickness,
blendMode,
feather,
gradient,
color,
stroke,
fill,
miter,
round,
bevel,
butt,
square,
srcOver,
screen,
overlay,
darken,
lighten,
colorDodge,
colorBurn,
hardLight,
softLight,
difference,
exclusion,
multiply,
hue,
saturation,
luminosity,
copy,
// Renderer
drawPath,
drawImage,
drawImageMesh,
clipPath,
save,
restore,
transform,
// Scripted Properties
value,
red,
green,
blue,
alpha,
getNumber,
getTrigger,
getString,
getBoolean,
getColor,
getList,
getViewModel,
getEnum,
values,
addListener,
removeListener,
fire,
push,
insert,
shift,
pop,
swap,
clear,
// Artboards
draw,
advance,
frameOrigin,
data,
instance,
animation,
newAtom,
bounds,
pointerDown,
pointerMove,
pointerUp,
pointerExit,
addToPath,
name,
// Scripted DataValues
isNumber,
isString,
isBoolean,
isColor,
// inputs
hit,
id,
position,
// nodes
rotation,
scale,
worldTransform,
scaleX,
scaleY,
decompose,
children,
parent,
node,
paint,
asPaint,
asPath,
// PathMeasure/ContourMeasure
positionAndTangent,
warp,
extract,
next,
isClosed,
// Scripted Context
markNeedsUpdate,
viewModel,
rootViewModel,
image,
blob,
size,
dataContext,
audio,
play,
playAtTime,
playInTime,
playAtFrame,
playInFrame,
stop,
pause,
resume,
seek,
seekFrame,
volume,
completed,
time,
timeFrame,
sampleRate,
// Animation
duration,
setTime,
setTimeFrames,
setTimePercentage,
};
struct ScriptedMat2D
{
static constexpr uint8_t luaTag = LUA_T_COUNT + 1;
static constexpr const char* luaName = "Mat2D";
static constexpr bool hasMetatable = true;
ScriptedMat2D(float x1, float y1, float x2, float y2, float tx, float ty) :
value(x1, y1, x2, y2, tx, ty)
{}
ScriptedMat2D(const Mat2D& mat) : value(mat) {}
ScriptedMat2D() {}
rive::Mat2D value;
};
static_assert(std::is_trivially_destructible<ScriptedMat2D>::value,
"ScriptedMat2D must be trivially destructible");
class ScriptedPathCommand
{
public:
ScriptedPathCommand(std::string type, std::vector<Vec2D> points = {}) :
m_type(type), m_points(points)
{}
static constexpr uint8_t luaTag = LUA_T_COUNT + 29;
static constexpr const char* luaName = "PathCommand";
static constexpr bool hasMetatable = true;
std::string type() { return m_type; }
std::vector<Vec2D> points() { return m_points; }
private:
std::string m_type;
std::vector<Vec2D> m_points;
};
class ScriptedPathData
{
public:
ScriptedPathData() {}
ScriptedPathData(const RawPath* path);
int totalCommands();
void markDirty() { m_isRenderPathDirty = true; }
RawPath rawPath;
static constexpr uint8_t luaTag = LUA_T_COUNT + 30;
static constexpr const char* luaName = "PathData";
static constexpr bool hasMetatable = true;
RenderPath* renderPath(lua_State* L);
protected:
rcp<RenderPath> m_renderPath;
bool m_isRenderPathDirty = true;
private:
uint64_t m_renderFrameId = 0;
};
class ScriptedPath : public ScriptedPathData
{
public:
ScriptedPath() {}
ScriptedPath(const RawPath* path) : ScriptedPathData(path) {}
static constexpr uint8_t luaTag = LUA_T_COUNT + 2;
static constexpr const char* luaName = "Path";
static constexpr bool hasMetatable = true;
};
class ScriptedGradient
{
public:
rcp<RenderShader> shader;
static constexpr uint8_t luaTag = LUA_T_COUNT + 3;
static constexpr const char* luaName = "Gradient";
static constexpr bool hasMetatable = false;
};
class ScriptedVertexBuffer
{
public:
std::vector<Vec2D> values;
rcp<RenderBuffer> vertexBuffer;
static constexpr uint8_t luaTag = LUA_T_COUNT + 4;
static constexpr const char* luaName = "VertexBuffer";
static constexpr bool hasMetatable = true;
void update(Factory* factory);
};
class ScriptedTriangleBuffer
{
public:
std::vector<uint16_t> values;
rcp<RenderBuffer> indexBuffer;
static constexpr uint8_t luaTag = LUA_T_COUNT + 5;
static constexpr const char* luaName = "TriangleBuffer";
static constexpr bool hasMetatable = true;
uint16_t max = 0;
void update(Factory* factory);
};
class ScriptedImage
{
public:
rcp<RenderImage> image;
static constexpr uint8_t luaTag = LUA_T_COUNT + 6;
static constexpr const char* luaName = "Image";
static constexpr bool hasMetatable = true;
};
class ScriptedBlob
{
public:
rcp<FileAsset> asset; // Holds ref to keep BlobAsset alive
static constexpr uint8_t luaTag = LUA_T_COUNT + 35;
static constexpr const char* luaName = "Blob";
static constexpr bool hasMetatable = true;
};
#ifdef WITH_RIVE_AUDIO
class ScriptedAudio
{
public:
static constexpr uint8_t luaTag = LUA_T_COUNT + 40;
static constexpr const char* luaName = "Audio";
static constexpr bool hasMetatable = true;
};
class ScriptedAudioSource
{
public:
static constexpr uint8_t luaTag = LUA_T_COUNT + 38;
static constexpr const char* luaName = "AudioSource";
static constexpr bool hasMetatable = true;
void source(rcp<AudioSource>);
rcp<AudioSource> source() { return m_source; }
int play(lua_State*, AudioEngine*);
int play(lua_State*, AudioEngine*, double, bool);
int playFrame(lua_State*, AudioEngine*);
int playFrame(lua_State*, AudioEngine*, uint64_t, bool);
private:
rcp<AudioSource> m_source;
int initializeSound(lua_State*, rcp<AudioSound>, Artboard*);
};
class ScriptedAudioSound
{
public:
ScriptedAudioSound(Artboard* artboard) : m_artboard(artboard) {}
rcp<AudioSound> sound;
static constexpr uint8_t luaTag = LUA_T_COUNT + 39;
static constexpr const char* luaName = "AudioSound";
static constexpr bool hasMetatable = true;
Artboard* artboard() { return m_artboard; }
private:
Artboard* m_artboard = nullptr;
};
#endif
class ScriptedImageSampler
{
public:
ImageSampler sampler;
ScriptedImageSampler(ImageWrap wx, ImageWrap wy, ImageFilter f)
{
sampler.wrapX = wx;
sampler.wrapY = wy;
sampler.filter = f;
}
static constexpr uint8_t luaTag = LUA_T_COUNT + 7;
static constexpr const char* luaName = "ImageSampler";
static constexpr bool hasMetatable = false;
};
class ScriptedPaintData
{
public:
ScriptedPaintData();
ScriptedPaintData(const ShapePaint* shapePaint);
virtual ~ScriptedPaintData() = default;
static constexpr uint8_t luaTag = LUA_T_COUNT + 33;
static constexpr const char* luaName = "PaintData";
static constexpr bool hasMetatable = true;
virtual void style(RenderPaintStyle style) { m_style = style; }
virtual void color(ColorInt value) { m_color = value; }
virtual void thickness(float value) { m_thickness = value; }
virtual void join(StrokeJoin value) { m_join = value; }
virtual void cap(StrokeCap value) { m_cap = value; }
virtual void feather(float value) { m_feather = value; }
virtual void blendMode(BlendMode value) { m_blendMode = value; }
virtual void gradient(rcp<RenderShader> value) { m_gradient = value; }
void pushStyle(lua_State* L);
void pushJoin(lua_State* L);
void pushCap(lua_State* L);
void pushThickness(lua_State* L);
void pushBlendMode(lua_State* L);
void pushFeather(lua_State* L);
void pushGradient(lua_State* L);
void pushColor(lua_State* L);
protected:
RenderPaintStyle m_style = RenderPaintStyle::fill;
rcp<RenderShader> m_gradient;
float m_thickness = 1;
StrokeJoin m_join = StrokeJoin::miter;
StrokeCap m_cap = StrokeCap::butt;
float m_feather = 0;
BlendMode m_blendMode = BlendMode::srcOver;
ColorInt m_color = 0xFF000000;
};
class ScriptedPaint : public ScriptedPaintData
{
public:
ScriptedPaint(Factory* factory);
ScriptedPaint(Factory* factory, const ScriptedPaint& source);
virtual ~ScriptedPaint() = default;
rcp<RenderPaint> renderPaint;
static constexpr uint8_t luaTag = LUA_T_COUNT + 8;
static constexpr const char* luaName = "Paint";
static constexpr bool hasMetatable = true;
void style(RenderPaintStyle style) override
{
ScriptedPaintData::style(style);
renderPaint->style(style);
}
void color(ColorInt value) override
{
ScriptedPaintData::color(value);
renderPaint->color(value);
}
void thickness(float value) override
{
ScriptedPaintData::thickness(value);
renderPaint->thickness(value);
}
void join(StrokeJoin value) override
{
ScriptedPaintData::join(value);
renderPaint->join(value);
}
void cap(StrokeCap value) override
{
ScriptedPaintData::cap(value);
renderPaint->cap(value);
}
void feather(float value) override
{
ScriptedPaintData::feather(value);
renderPaint->feather(value);
}
void blendMode(BlendMode value) override
{
ScriptedPaintData::blendMode(value);
renderPaint->blendMode(value);
}
void gradient(rcp<RenderShader> value) override
{
ScriptedPaintData::gradient(value);
renderPaint->shader(value);
}
};
class ScriptedRenderer
{
public:
ScriptedRenderer(Renderer* renderer) : m_renderer(renderer), m_saveCount(0)
{}
// End usage of this ScriptedRenderer.
bool end();
void save(lua_State* L);
void restore(lua_State* L);
void transform(lua_State* L, const Mat2D& mat2d);
void clipPath(lua_State* L, ScriptedPathData* path);
Renderer* validate(lua_State* L);
static constexpr uint8_t luaTag = LUA_T_COUNT + 9;
static constexpr const char* luaName = "Renderer";
static constexpr bool hasMetatable = true;
private:
// Not owned by the ScriptedRenderer, only valid when passed in.
Renderer* m_renderer = nullptr;
uint32_t m_saveCount = 0;
};
class ScriptReffedArtboard : public RefCnt<ScriptReffedArtboard>
{
public:
ScriptReffedArtboard(lua_State* L,
rcp<File> file,
std::unique_ptr<ArtboardInstance>&& artboardInstance,
rcp<ViewModelInstance> viewModelInstance,
rcp<DataContext> parentDataContext);
~ScriptReffedArtboard();
rive::rcp<rive::File> file();
Artboard* artboard();
StateMachineInstance* stateMachine();
rcp<ViewModelInstance> viewModelInstance() { return m_viewModelInstance; }
void releaseReferences();
static void releaseAll(lua_State* state);
private:
rcp<File> m_file;
std::unique_ptr<ArtboardInstance> m_artboard;
std::unique_ptr<StateMachineInstance> m_stateMachine;
rcp<ViewModelInstance> m_viewModelInstance;
lua_State* m_luaState = nullptr;
ScriptReffedArtboard* m_trackNext = nullptr;
ScriptReffedArtboard* m_trackPrev = nullptr;
static ScriptReffedArtboard* s_head;
};
class ScriptedArtboard
{
public:
ScriptedArtboard(lua_State* L,
rcp<File> file,
std::unique_ptr<ArtboardInstance>&& artboardInstance,
rcp<ViewModelInstance> viewModelInstance,
rcp<DataContext> dataContext);
~ScriptedArtboard();
static constexpr uint8_t luaTag = LUA_T_COUNT + 10;
static constexpr const char* luaName = "Artboard";
static constexpr bool hasMetatable = true;
Artboard* artboard() { return m_scriptReffedArtboard->artboard(); }
StateMachineInstance* stateMachine()
{
return m_scriptReffedArtboard->stateMachine();
}
rcp<ViewModelInstance> viewModelInstance()
{
return m_scriptReffedArtboard->viewModelInstance();
}
rcp<ScriptReffedArtboard> scriptReffedArtboard()
{
return m_scriptReffedArtboard;
}
int pushData(lua_State* L);
int instance(lua_State* L, rcp<ViewModelInstance> viewModelInstance);
int animation(lua_State* L, const char* animationName);
bool advance(float seconds);
void cleanupDataRef(lua_State* L);
private:
lua_State* m_state = nullptr;
rcp<ScriptReffedArtboard> m_scriptReffedArtboard = nullptr;
int m_dataRef = 0;
};
class ScriptedAnimation
{
public:
ScriptedAnimation(lua_State* L,
std::unique_ptr<LinearAnimationInstance> animation);
static constexpr uint8_t luaTag = LUA_T_COUNT + 32;
static constexpr const char* luaName = "Animation";
static constexpr bool hasMetatable = true;
float duration();
int advance();
int setTime(std::string mode);
private:
lua_State* m_state;
std::unique_ptr<LinearAnimationInstance> m_animation;
};
struct ScriptedListener
{
int function;
int userdata;
};
class ScriptedProperty : public ViewModelInstanceValueDelegate
{
public:
ScriptedProperty(lua_State* L, rcp<ViewModelInstanceValue> value);
virtual ~ScriptedProperty();
int addListener();
int removeListener();
void clearListeners();
void valueChanged() override;
const lua_State* state() const { return m_state; }
ViewModelInstanceValue* instanceValue() { return m_instanceValue.get(); }
private:
std::vector<ScriptedListener> m_listeners;
protected:
lua_State* m_state;
rcp<ViewModelInstanceValue> m_instanceValue;
};
class ScriptedViewModel
{
public:
ScriptedViewModel(lua_State* L,
rcp<ViewModel> viewModel,
rcp<ViewModelInstance> viewModelInstance);
~ScriptedViewModel();
static constexpr uint8_t luaTag = LUA_T_COUNT + 11;
static constexpr const char* luaName = "ViewModel";
static constexpr bool hasMetatable = true;
int pushValue(const char* name, int coreType = 0);
int instance(lua_State* L);
const lua_State* state() const { return m_state; }
rcp<ViewModelInstance> viewModelInstance() const
{
return m_viewModelInstance;
}
rcp<ViewModelInstance> mutableViewModelInstance()
{
return m_viewModelInstance;
}
void releaseReferences();
static void releaseAll(lua_State* state);
private:
lua_State* m_state;
rcp<ViewModel> m_viewModel;
rcp<ViewModelInstance> m_viewModelInstance;
std::unordered_map<std::string, int> m_propertyRefs;
ScriptedViewModel* m_trackNext = nullptr;
ScriptedViewModel* m_trackPrev = nullptr;
static ScriptedViewModel* s_head;
};
class ScriptedPropertyViewModel : public ScriptedProperty
{
public:
ScriptedPropertyViewModel(lua_State* L,
rcp<ViewModel> viewModel,
rcp<ViewModelInstanceViewModel> value);
~ScriptedPropertyViewModel();
static constexpr uint8_t luaTag = LUA_T_COUNT + 12;
static constexpr const char* luaName = "PropertyViewModel";
static constexpr bool hasMetatable = true;
int pushValue();
void setValue(ScriptedViewModel*);
private:
rcp<ViewModel> m_viewModel;
int m_valueRef = 0;
};
class ScriptedPropertyNumber : public ScriptedProperty
{
public:
ScriptedPropertyNumber(lua_State* L, rcp<ViewModelInstanceNumber> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 13;
static constexpr const char* luaName = "Property<number>";
static constexpr bool hasMetatable = true;
int pushValue();
void setValue(float value);
};
class ScriptedPropertyTrigger : public ScriptedProperty
{
public:
ScriptedPropertyTrigger(lua_State* L, rcp<ViewModelInstanceTrigger> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 14;
static constexpr const char* luaName = "PropertyTrigger";
static constexpr bool hasMetatable = true;
};
class ScriptedPropertyList : public ScriptedProperty
{
public:
ScriptedPropertyList(lua_State* L, rcp<ViewModelInstanceList> value);
~ScriptedPropertyList();
static constexpr uint8_t luaTag = LUA_T_COUNT + 15;
static constexpr const char* luaName = "PropertyList";
static constexpr bool hasMetatable = true;
int pushLength();
int pushValue(int index);
void valueChanged() override;
void append(ViewModelInstance*);
private:
bool m_changed = false;
std::unordered_map<ViewModelInstance*, int> m_propertyRefs;
};
class ScriptedPropertyColor : public ScriptedProperty
{
public:
ScriptedPropertyColor(lua_State* L, rcp<ViewModelInstanceColor> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 16;
static constexpr const char* luaName = "PropertyColor";
static constexpr bool hasMetatable = true;
int pushValue();
void setValue(unsigned value);
};
class ScriptedPropertyString : public ScriptedProperty
{
public:
ScriptedPropertyString(lua_State* L, rcp<ViewModelInstanceString> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 17;
static constexpr const char* luaName = "PropertyString";
static constexpr bool hasMetatable = true;
int pushValue();
void setValue(const std::string& value);
};
class ScriptedPropertyBoolean : public ScriptedProperty
{
public:
ScriptedPropertyBoolean(lua_State* L, rcp<ViewModelInstanceBoolean> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 18;
static constexpr const char* luaName = "Property<bool>";
static constexpr bool hasMetatable = true;
int pushValue();
void setValue(bool value);
};
class ScriptedEnumValues
{
public:
ScriptedEnumValues(lua_State* L, DataEnum* value) :
m_state(L), m_dataEnum(value)
{}
static constexpr uint8_t luaTag = LUA_T_COUNT + 34;
static constexpr const char* luaName = "EnumValues";
static constexpr bool hasMetatable = true;
void dataEnum(DataEnum* value) { m_dataEnum = value; }
int pushValue(int index);
int pushLength();
const lua_State* state() const { return m_state; }
private:
lua_State* m_state = nullptr;
DataEnum* m_dataEnum = nullptr;
};
class ScriptedPropertyEnum : public ScriptedProperty
{
public:
ScriptedPropertyEnum(lua_State* L, rcp<ViewModelInstanceEnum> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 19;
static constexpr const char* luaName = "Property<enum>";
static constexpr bool hasMetatable = true;
int pushValue();
void setValue(const std::string& value);
};
// Make
// ScriptedPropertyViewModel
// - Nullable ViewModelInstanceValue (ViewModelInstanceViewModel)
// - Requires ViewModel to know which properties to expect
// ScriptedPropertyArtboard
// - Nullable ViewModelInstanceValue (ViewModelInstanceArtboard)
// Make renderer: return lua_newrive<ScriptedRenderer>(L, renderer);
template <class T, class... Args>
static T* lua_newrive(lua_State* L, Args&&... args)
{
if (T::hasMetatable)
{
return new (lua_newuserdatataggedwithmetatable(L, sizeof(T), T::luaTag))
T(std::forward<Args>(args)...);
}
else
{
return new (lua_newuserdatatagged(L, sizeof(T), T::luaTag))
T(std::forward<Args>(args)...);
}
}
BlendMode lua_toblendmode(lua_State* L, int idx);
template <typename T>
static T* lua_torive(lua_State* L, int idx, bool allowNil = false)
{
T* riveObject = (T*)lua_touserdatatagged(L, idx, T::luaTag);
if (!allowNil && riveObject == nullptr)
{
luaL_typeerror(L, idx, T::luaName);
return nullptr;
}
return riveObject;
}
template <typename T> static void lua_register_rive(lua_State* L)
{
if (T::hasMetatable)
{
// create metatable for T
luaL_newmetatable(L, T::luaName);
// lua_createtable(L, 0, 1);
// push it again as lua_setuserdatametatable pops it
lua_pushvalue(L, -1);
lua_setuserdatametatable(L, T::luaTag);
}
if (!std::is_trivially_destructible<T>::value)
{
// We only need to call the C++ destructor if the object is not
// trivially destructible.
lua_setuserdatadtor(L, T::luaTag, [](lua_State* L, void* data) {
((T*)data)->~T();
});
}
}
inline const Vec2D* lua_checkvec2d(lua_State* L, int stack)
{
return (const Vec2D*)luaL_checkvector(L, stack);
}
inline const Vec2D* lua_tovec2d(lua_State* L, int stack)
{
return (const Vec2D*)lua_tovector(L, stack);
}
inline void lua_pushvec2d(lua_State* L, Vec2D vec)
{
return lua_pushvector2(L, vec.x, vec.y);
}
int luaopen_rive(lua_State* L);
int rive_luaErrorHandler(lua_State* L);
int rive_lua_pcall(lua_State* state, int nargs, int nresults);
int rive_lua_pushRef(lua_State* state, int ref);
void rive_lua_pop(lua_State* state, int count);
class ScriptingContext
{
public:
ScriptingContext(Factory* factory) : m_factory(factory) {}
virtual ~ScriptingContext() = default;
Factory* factory() const { return m_factory; }
virtual void printError(lua_State* state) = 0;
virtual void printBeginLine(lua_State* state) = 0;
virtual void print(Span<const char> data) = 0;
virtual void printEndLine() = 0;
virtual int pCall(lua_State* state, int nargs, int nresults) = 0;
// Add a module to be registered later via performRegistration()
void addModule(ModuleDetails* moduleDetails);
// Perform registration of all added modules, handling dependencies and
// retries
void performRegistration(lua_State* state);
// Called when a module is required but not found during registration
void recordMissingDependency(const std::string& requiringModule,
const std::string& missingModule);
private:
bool tryRegisterModule(lua_State* state, ModuleDetails* moduleDetails);
void sortNextModule(ModuleDetails* module,
std::vector<ModuleDetails*>* pendingModules,
std::vector<ModuleDetails*>* sortedModules,
std::unordered_set<ModuleDetails*>* visitedModules);
// Called when a module successfully registers
void onModuleRegistered(ModuleDetails* moduleDetails);
private:
Factory* m_factory;
std::vector<ModuleDetails*> m_modulesToRegister;
std::unordered_map<std::string, ModuleDetails*> m_moduleLookup;
std::unordered_set<ModuleDetails*> m_pendingModules;
#ifdef WITH_RIVE_TOOLS
// Editor-only: Map from asset ID to generator function ref.
// Allows direct ScriptedObject reinitialization without regenerating
// the runtime file.
std::unordered_map<uint32_t, int> m_assetGeneratorRefs;
bool m_isPlaying = false;
public:
void setGeneratorRef(uint32_t assetId, int ref);
int getGeneratorRef(uint32_t assetId) const;
void clearGeneratorRefs();
bool hasGeneratorRef(uint32_t assetId) const;
void isPlaying(bool value) { m_isPlaying = value; }
bool isPlaying() const { return m_isPlaying; }
#endif
};
class ScriptedDataValue
{
public:
ScriptedDataValue(lua_State* L) { m_state = L; }
virtual ~ScriptedDataValue();
static constexpr const char* luaName = "DataValue";
DataValue* dataValue() { return m_dataValue; }
virtual bool isNumber() { return false; }
virtual bool isString() { return false; }
virtual bool isBoolean() { return false; }
virtual bool isColor() { return false; }
const lua_State* state() const { return m_state; }
protected: