-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathRiveCommandQueue.mm
More file actions
2353 lines (2110 loc) · 80.9 KB
/
Copy pathRiveCommandQueue.mm
File metadata and controls
2353 lines (2110 loc) · 80.9 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
//
// RiveCommandQueue.mm
// RiveRuntime
//
// Created by David Skuza on 8/4/25.
// Copyright © 2025 Rive. All rights reserved.
//
#import <Rive.h>
#import <RivePrivateHeaders.h>
#import <rive/command_queue.hpp>
#import <rive/command_server.hpp>
#import <QuartzCore/CoreAnimation.h>
#import "RiveArtboardListener.h"
#import "RiveRenderImageListener.h"
#import "RiveFontListener.h"
#import "RiveAudioListener.h"
#import "RiveViewModelInstanceData.h"
#import "RivePrivateHeaders.h"
#import "RiveExperimental_Private.hh"
#if TARGET_OS_OSX && !RIVE_MAC_CATALYST
#define RIVE_USE_CVDISPLAYLINK 1
#else
#define RIVE_USE_CVDISPLAYLINK 0
#endif
NS_ASSUME_NONNULL_BEGIN
rive::DataType RiveViewModelInstanceDataTypeToCppType(
RiveViewModelInstanceDataType type)
{
switch (type)
{
case RiveViewModelInstanceDataTypeNone:
return rive::DataType::none;
case RiveViewModelInstanceDataTypeString:
return rive::DataType::string;
case RiveViewModelInstanceDataTypeNumber:
return rive::DataType::number;
case RiveViewModelInstanceDataTypeBoolean:
return rive::DataType::boolean;
case RiveViewModelInstanceDataTypeColor:
return rive::DataType::color;
case RiveViewModelInstanceDataTypeList:
return rive::DataType::list;
case RiveViewModelInstanceDataTypeEnum:
return rive::DataType::enumType;
case RiveViewModelInstanceDataTypeTrigger:
return rive::DataType::trigger;
case RiveViewModelInstanceDataTypeViewModel:
return rive::DataType::viewModel;
case RiveViewModelInstanceDataTypeInteger:
return rive::DataType::integer;
case RiveViewModelInstanceDataTypeSymbolListIndex:
return rive::DataType::symbolListIndex;
case RiveViewModelInstanceDataTypeAssetImage:
return rive::DataType::assetImage;
case RiveViewModelInstanceDataTypeArtboard:
return rive::DataType::artboard;
case RiveViewModelInstanceDataTypeInput:
return rive::DataType::input;
case RiveViewModelInstanceDataTypeAny:
return rive::DataType::any;
}
}
RiveViewModelInstanceDataType RiveViewModelInstanceDataTypeFromCpp(
rive::DataType type)
{
switch (type)
{
case rive::DataType::none:
return RiveViewModelInstanceDataTypeNone;
case rive::DataType::string:
return RiveViewModelInstanceDataTypeString;
case rive::DataType::number:
return RiveViewModelInstanceDataTypeNumber;
case rive::DataType::boolean:
return RiveViewModelInstanceDataTypeBoolean;
case rive::DataType::color:
return RiveViewModelInstanceDataTypeColor;
case rive::DataType::list:
return RiveViewModelInstanceDataTypeList;
case rive::DataType::enumType:
return RiveViewModelInstanceDataTypeEnum;
case rive::DataType::trigger:
return RiveViewModelInstanceDataTypeTrigger;
case rive::DataType::viewModel:
return RiveViewModelInstanceDataTypeViewModel;
case rive::DataType::integer:
return RiveViewModelInstanceDataTypeInteger;
case rive::DataType::symbolListIndex:
return RiveViewModelInstanceDataTypeSymbolListIndex;
case rive::DataType::assetImage:
return RiveViewModelInstanceDataTypeAssetImage;
case rive::DataType::artboard:
return RiveViewModelInstanceDataTypeArtboard;
case rive::DataType::input:
return RiveViewModelInstanceDataTypeInput;
case rive::DataType::any:
return RiveViewModelInstanceDataTypeAny;
}
}
static rive::Fit RiveConfigurationFitCppValue(RiveConfigurationFit fit)
{
switch (fit)
{
case RiveConfigurationFitFill:
return rive::Fit::fill;
case RiveConfigurationFitContain:
return rive::Fit::contain;
case RiveConfigurationFitCover:
return rive::Fit::cover;
case RiveConfigurationFitFitWidth:
return rive::Fit::fitWidth;
case RiveConfigurationFitFitHeight:
return rive::Fit::fitHeight;
case RiveConfigurationFitNone:
return rive::Fit::none;
case RiveConfigurationFitScaleDown:
return rive::Fit::scaleDown;
case RiveConfigurationFitLayout:
return rive::Fit::layout;
}
}
static rive::Alignment RiveConfigurationAlignmentCppValue(
RiveConfigurationAlignment alignment)
{
switch (alignment)
{
case RiveConfigurationAlignmentTopLeft:
return rive::Alignment::topLeft;
case RiveConfigurationAlignmentTopCenter:
return rive::Alignment::topCenter;
case RiveConfigurationAlignmentTopRight:
return rive::Alignment::topRight;
case RiveConfigurationAlignmentCenterLeft:
return rive::Alignment::centerLeft;
case RiveConfigurationAlignmentCenter:
return rive::Alignment::center;
case RiveConfigurationAlignmentCenterRight:
return rive::Alignment::centerRight;
case RiveConfigurationAlignmentBottomLeft:
return rive::Alignment::bottomLeft;
case RiveConfigurationAlignmentBottomCenter:
return rive::Alignment::bottomCenter;
case RiveConfigurationAlignmentBottomRight:
return rive::Alignment::bottomRight;
}
}
static rive::CommandQueue::PointerEvent RivePointerEventToCpp(
CGPoint position,
CGSize screenBounds,
RiveConfigurationFit fit,
RiveConfigurationAlignment alignment,
float scaleFactor)
{
rive::CommandQueue::PointerEvent cppEvent;
cppEvent.fit = RiveConfigurationFitCppValue(fit);
cppEvent.alignment = RiveConfigurationAlignmentCppValue(alignment);
cppEvent.screenBounds =
rive::Vec2D(screenBounds.width, screenBounds.height);
cppEvent.position = rive::Vec2D(position.x, position.y);
cppEvent.scaleFactor = scaleFactor;
return cppEvent;
}
// MARK: - Internal Artboard Listener Implementation
namespace
{
/**
* @class _ArtboardListener
*
* Internal C++ implementation of the Rive artboard listener that bridges
* between the C++ command queue system and a single Objective-C observer.
*
* This class implements the rive::CommandQueue::ArtboardListener interface
* and forwards events to a single Objective-C observer.
*/
class _ArtboardListener : public rive::CommandQueue::ArtboardListener
{
public:
/**
* Constructs a new artboard listener with the specified observer.
*
* @param observer The Objective-C observer that will receive artboard
* events. The observer is held as a weak reference to avoid retain cycles.
*/
_ArtboardListener(id<RiveArtboardListener> observer)
{
_observer = observer;
}
/**
* Called when an artboard encounters an error during operations.
*
* This method is invoked by the C++ command queue when an artboard
* operation fails. It converts the C++ error data to Objective-C objects
* and forwards the event to the registered observer.
*
* @param handle The artboard handle that encountered the error.
* @param requestId The unique identifier for the request that failed.
* @param error A string describing the error that occurred.
*/
virtual void onArtboardError(const rive::ArtboardHandle handle,
uint64_t requestId,
std::string error) override;
/**
* Called when state machine names are listed for an artboard.
*
* This method is invoked by the C++ command queue when a state machine
* names request completes. It converts the C++ data to Objective-C objects
* and forwards the event to the registered observer.
*
* @param handle The artboard handle that was queried.
* @param requestId The unique identifier for the request that completed.
* @param stateMachineNames Vector of state machine names from the C++
* runtime.
*/
virtual void onStateMachinesListed(
const rive::ArtboardHandle handle,
uint64_t requestId,
std::vector<std::string> stateMachineNames) override;
/**
* Called when default view model information is received for an artboard.
*
* This method is invoked by the C++ command queue when a default view model
* info request completes. It converts the C++ data to Objective-C objects
* and forwards the event to the registered observer.
*
* @param handle The artboard handle that was queried.
* @param requestId The unique identifier for the request that completed.
* @param viewModelName The name of the default view model from the C++
* runtime.
* @param instanceName The name of the default view model instance from the
* C++ runtime.
*/
virtual void onDefaultViewModelInfoReceived(
const rive::ArtboardHandle handle,
uint64_t requestId,
std::string viewModelName,
std::string instanceName) override;
private:
__weak id<RiveArtboardListener> _observer;
};
} // namespace
void _ArtboardListener::onArtboardError(const rive::ArtboardHandle handle,
uint64_t requestId,
std::string error)
{
if (_observer)
{
[_observer
onArtboardError:reinterpret_cast<uint64_t>(handle)
requestID:requestId
message:[NSString stringWithUTF8String:error.c_str()]];
}
}
void _ArtboardListener::onStateMachinesListed(
const rive::ArtboardHandle handle,
uint64_t requestId,
std::vector<std::string> stateMachineNames)
{
if (_observer)
{
NSMutableArray<NSString*>* names =
[NSMutableArray arrayWithCapacity:stateMachineNames.size()];
for (const auto& name : stateMachineNames)
{
[names addObject:[NSString stringWithUTF8String:name.c_str()]];
}
[_observer onStateMachineNamesListed:reinterpret_cast<uint64_t>(handle)
names:names
requestID:requestId];
}
}
void _ArtboardListener::onDefaultViewModelInfoReceived(
const rive::ArtboardHandle handle,
uint64_t requestId,
std::string viewModelName,
std::string instanceName)
{
if (_observer)
{
NSString* viewModelNameObjC =
[NSString stringWithUTF8String:viewModelName.c_str()];
NSString* instanceNameObjC =
[NSString stringWithUTF8String:instanceName.c_str()];
[_observer
onDefaultViewModelInfoReceived:reinterpret_cast<uint64_t>(handle)
requestID:requestId
viewModelName:viewModelNameObjC
instanceName:instanceNameObjC];
}
}
// MARK: - Internal File Listener Implementation
namespace
{
/**
* @class _FileListener
*
* Internal C++ implementation of the Rive file listener that bridges
* between the C++ command queue system and a single Objective-C observer.
*
* This class implements the rive::CommandQueue::FileListener interface
* and forwards events to a single Objective-C observer.
*/
class _FileListener : public rive::CommandQueue::FileListener
{
public:
/**
* Constructs a new file listener with the specified observer.
*
* @param observer The Objective-C observer that will receive file events.
* The observer is held as a weak reference to avoid retain
* cycles.
*/
_FileListener(id<RiveFileListener> observer) { _observer = observer; }
/**
* Called when a file loading operation encounters an error.
*
* @param handle The file handle that was being loaded
* @param requestId The identifier of the failed request
* @param error A string describing the error that occurred
*/
virtual void onFileError(const rive::FileHandle handle,
uint64_t requestId,
std::string error) override;
/**
* Called when a file is successfully loaded by the command server.
*
* @param handle The unique identifier of the loaded file
* @param requestId The identifier of the loading request
*/
virtual void onFileLoaded(const rive::FileHandle handle,
uint64_t requestId) override;
/**
* Called when a file is deleted from the command server.
*
* @param handle The unique identifier of the deleted file
* @param requestId The identifier of the deletion request
*/
virtual void onFileDeleted(const rive::FileHandle handle,
uint64_t requestId) override;
/**
* Called when artboard names are listed for a file.
*
* @param handle The unique identifier of the file
* @param requestId The identifier of the listing request
* @param artboardNames Vector of artboard names in the file
*/
virtual void onArtboardsListed(
const rive::FileHandle handle,
uint64_t requestId,
std::vector<std::string> artboardNames) override;
/**
* Called when view model names are listed for a file.
*
* @param handle The unique identifier of the file
* @param requestId The identifier of the listing request
* @param viewModelNames Vector of view model names in the file
*/
virtual void onViewModelsListed(
const rive::FileHandle handle,
uint64_t requestId,
std::vector<std::string> viewModelNames) override;
/**
* Called when view model instance names are listed for a file.
*
* @param handle The unique identifier of the file
* @param requestId The identifier of the listing request
* @param viewModelName The name of the view model
* @param instanceNames Vector of view model instance names
*/
virtual void onViewModelInstanceNamesListed(
const rive::FileHandle handle,
uint64_t requestId,
std::string viewModelName,
std::vector<std::string> instanceNames) override;
/**
* Called when view model properties are listed for a file.
*
* @param handle The unique identifier of the file
* @param requestId The identifier of the listing request
* @param viewModelName The name of the view model
* @param properties Vector of view model property data
*/
virtual void onViewModelPropertiesListed(
const rive::FileHandle handle,
uint64_t requestId,
std::string viewModelName,
std::vector<rive::CommandQueue::FileListener::ViewModelPropertyData>
properties) override;
/**
* Called when view model enums are listed for a file.
*
* @param handle The unique identifier of the file
* @param requestId The identifier of the listing request
* @param enums Vector of view model enum data
*/
virtual void onViewModelEnumsListed(
const rive::FileHandle handle,
uint64_t requestId,
std::vector<rive::ViewModelEnum> enums) override;
private:
__weak id<RiveFileListener> _observer;
};
} // namespace
void _FileListener::onFileError(const rive::FileHandle handle,
uint64_t requestId,
std::string error)
{
if (_observer)
{
[_observer onFileError:reinterpret_cast<uint64_t>(handle)
requestID:requestId
message:[NSString stringWithUTF8String:error.c_str()]];
}
}
void _FileListener::onFileLoaded(const rive::FileHandle handle,
uint64_t requestId)
{
if (_observer)
{
[_observer onFileLoaded:reinterpret_cast<uint64_t>(handle)
requestID:reinterpret_cast<uint64_t>(requestId)];
}
}
void _FileListener::onFileDeleted(const rive::FileHandle handle,
uint64_t requestId)
{
if (_observer)
{
[_observer onFileDeleted:reinterpret_cast<uint64_t>(handle)
requestID:reinterpret_cast<uint64_t>(requestId)];
}
}
void _FileListener::onArtboardsListed(const rive::FileHandle handle,
uint64_t requestId,
std::vector<std::string> artboardNames)
{
if (_observer)
{
NSMutableArray<NSString*>* names =
[NSMutableArray arrayWithCapacity:artboardNames.size()];
for (const auto& name : artboardNames)
{
[names addObject:[NSString stringWithUTF8String:name.c_str()]];
}
[_observer onArtboardsListed:reinterpret_cast<uint64_t>(handle)
requestID:requestId
names:names];
}
}
void _FileListener::onViewModelsListed(const rive::FileHandle handle,
uint64_t requestId,
std::vector<std::string> viewModelNames)
{
if (_observer)
{
NSMutableArray<NSString*>* names =
[NSMutableArray arrayWithCapacity:viewModelNames.size()];
for (const auto& name : viewModelNames)
{
[names addObject:[NSString stringWithUTF8String:name.c_str()]];
}
[_observer onViewModelsListed:reinterpret_cast<uint64_t>(handle)
requestID:requestId
names:names];
}
}
void _FileListener::onViewModelInstanceNamesListed(
const rive::FileHandle handle,
uint64_t requestId,
std::string viewModelName,
std::vector<std::string> instanceNames)
{
if (_observer)
{
NSMutableArray<NSString*>* names =
[NSMutableArray arrayWithCapacity:instanceNames.size()];
for (const auto& name : instanceNames)
{
[names addObject:[NSString stringWithUTF8String:name.c_str()]];
}
NSString* nsViewModelName =
[NSString stringWithUTF8String:viewModelName.c_str()];
[_observer
onViewModelInstanceNamesListed:reinterpret_cast<uint64_t>(handle)
requestID:requestId
viewModelName:nsViewModelName
names:names];
}
}
void _FileListener::onViewModelPropertiesListed(
const rive::FileHandle handle,
uint64_t requestId,
std::string viewModelName,
std::vector<rive::CommandQueue::FileListener::ViewModelPropertyData>
properties)
{
if (_observer)
{
NSMutableArray<NSDictionary<NSString*, id>*>* propertyArray =
[NSMutableArray arrayWithCapacity:properties.size()];
for (const auto& prop : properties)
{
NSMutableDictionary<NSString*, id>* propertyDict =
[NSMutableDictionary dictionary];
// Convert type
RiveViewModelInstanceDataType type =
RiveViewModelInstanceDataTypeFromCpp(prop.type);
propertyDict[@"type"] = @(type);
// Convert name
propertyDict[@"name"] =
[NSString stringWithUTF8String:prop.name.c_str()];
// Convert metaData (may be empty)
propertyDict[@"metaData"] =
[NSString stringWithUTF8String:prop.metaData.c_str()];
[propertyArray addObject:propertyDict];
}
NSString* nsViewModelName =
[NSString stringWithUTF8String:viewModelName.c_str()];
[_observer
onViewModelPropertiesListed:reinterpret_cast<uint64_t>(handle)
requestID:requestId
viewModelName:nsViewModelName
properties:propertyArray];
}
}
void _FileListener::onViewModelEnumsListed(
const rive::FileHandle handle,
uint64_t requestId,
std::vector<rive::ViewModelEnum> enums)
{
if (_observer)
{
NSMutableArray<NSDictionary<NSString*, id>*>* enumArray =
[NSMutableArray arrayWithCapacity:enums.size()];
for (const auto& enumData : enums)
{
NSMutableDictionary<NSString*, id>* enumDict =
[NSMutableDictionary dictionary];
// Convert name
enumDict[@"name"] =
[NSString stringWithUTF8String:enumData.name.c_str()];
// Convert enumerants
NSMutableArray<NSString*>* values =
[NSMutableArray arrayWithCapacity:enumData.enumerants.size()];
for (const auto& enumerant : enumData.enumerants)
{
[values addObject:[NSString
stringWithUTF8String:enumerant.c_str()]];
}
enumDict[@"values"] = values;
[enumArray addObject:enumDict];
}
[_observer onViewModelEnumsListed:reinterpret_cast<uint64_t>(handle)
requestID:requestId
enums:enumArray];
}
}
namespace
{
/**
* @class _ViewModelInstanceListener
*
* Internal C++ implementation of the Rive view model instance listener that
* bridges between the C++ command queue system and a single Objective-C
* observer.
*
* This class implements the rive::CommandQueue::ViewModelInstanceListener
* interface and forwards events to a single Objective-C observer.
*/
class _ViewModelInstanceListener
: public rive::CommandQueue::ViewModelInstanceListener
{
public:
/**
* Constructs a new view model instance listener with the specified
* observer.
*
* @param observer The Objective-C observer that will receive view model
* instance events. The observer is held as a weak reference
* to avoid retain cycles.
*/
_ViewModelInstanceListener(id<RiveViewModelInstanceListener> observer)
{
_observer = observer;
}
/**
* Called when a view model instance operation encounters an error.
*
* @param handle The view model instance handle that encountered the error
* @param requestId The identifier of the failed request
* @param error A string describing the error that occurred
*/
virtual void onViewModelInstanceError(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
std::string error) override;
/**
* Called when a view model instance is deleted from the command server.
*
* @param handle The unique identifier of the deleted view model instance
* @param requestId The identifier of the deletion request
*/
virtual void onViewModelDeleted(const rive::ViewModelInstanceHandle handle,
uint64_t requestId) override;
/**
* Called when view model instance data is received.
*
* @param handle The unique identifier of the view model instance
* @param requestId The identifier of the data request
* @param data The view model instance data that was received
*/
virtual void onViewModelDataReceived(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
rive::CommandQueue::ViewModelInstanceData data) override;
/**
* Called when a view model instance list size is received.
*
* @param handle The unique identifier of the view model instance
* @param requestId The identifier of the list size request
* @param path The path to the list property
* @param size The size of the list
*/
virtual void onViewModelListSizeReceived(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
std::string path,
size_t size) override;
virtual void onViewModelInstanceNameReceived(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
std::string name) override;
private:
__weak id<RiveViewModelInstanceListener> _observer;
};
} // namespace
void _ViewModelInstanceListener::onViewModelInstanceError(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
std::string error)
{}
void _ViewModelInstanceListener::onViewModelDeleted(
const rive::ViewModelInstanceHandle handle, uint64_t requestId)
{}
void _ViewModelInstanceListener::onViewModelDataReceived(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
rive::CommandQueue::ViewModelInstanceData data)
{
if (_observer)
{
// Convert C++ ViewModelInstanceData to Objective-C object
RiveViewModelInstanceData* dataObj =
[[RiveViewModelInstanceData alloc] initWithData:data];
[_observer onViewModelDataReceived:reinterpret_cast<uint64_t>(handle)
requestID:requestId
data:dataObj];
}
}
void _ViewModelInstanceListener::onViewModelListSizeReceived(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
std::string path,
size_t size)
{
if (_observer)
{
NSString* nsPath = [NSString stringWithUTF8String:path.c_str()];
[_observer
onViewModelListSizeReceived:reinterpret_cast<uint64_t>(handle)
requestID:requestId
path:nsPath
size:static_cast<NSInteger>(size)];
}
}
void _ViewModelInstanceListener::onViewModelInstanceNameReceived(
const rive::ViewModelInstanceHandle handle,
uint64_t requestId,
std::string name)
{
if (_observer)
{
NSString* nsName = [NSString stringWithUTF8String:name.c_str()];
[_observer
onViewModelInstanceNameReceived:reinterpret_cast<uint64_t>(handle)
requestID:requestId
name:nsName];
}
}
namespace
{
class _RenderImageListener : public rive::CommandQueue::RenderImageListener
{
public:
_RenderImageListener(id<RiveRenderImageListener> observer)
{
_observer = observer;
}
virtual void onRenderImageDecoded(const rive::RenderImageHandle handle,
uint64_t requestId) override;
virtual void onRenderImageError(const rive::RenderImageHandle handle,
uint64_t requestId,
std::string error) override;
virtual void onRenderImageDeleted(const rive::RenderImageHandle handle,
uint64_t requestId) override;
private:
__weak id<RiveRenderImageListener> _observer;
};
} // namespace
void _RenderImageListener::onRenderImageDecoded(
const rive::RenderImageHandle handle, uint64_t requestId)
{
if (_observer)
{
[_observer onRenderImageDecoded:reinterpret_cast<uint64_t>(handle)
requestID:requestId];
}
}
void _RenderImageListener::onRenderImageError(
const rive::RenderImageHandle handle, uint64_t requestId, std::string error)
{
if (_observer)
{
[_observer
onRenderImageError:reinterpret_cast<uint64_t>(handle)
requestID:requestId
message:[NSString stringWithUTF8String:error.c_str()]];
}
}
void _RenderImageListener::onRenderImageDeleted(
const rive::RenderImageHandle handle, uint64_t requestId)
{
if (_observer)
{
[_observer onRenderImageDeleted:reinterpret_cast<uint64_t>(handle)
requestID:requestId];
}
}
namespace
{
class _FontListener : public rive::CommandQueue::FontListener
{
public:
_FontListener(id<RiveFontListener> observer) { _observer = observer; }
virtual void onFontDecoded(const rive::FontHandle handle,
uint64_t requestId) override;
virtual void onFontError(const rive::FontHandle handle,
uint64_t requestId,
std::string error) override;
virtual void onFontDeleted(const rive::FontHandle handle,
uint64_t requestId) override;
private:
__weak id<RiveFontListener> _observer;
};
} // namespace
void _FontListener::onFontDecoded(const rive::FontHandle handle,
uint64_t requestId)
{
if (_observer)
{
[_observer onFontDecoded:reinterpret_cast<uint64_t>(handle)
requestID:requestId];
}
}
void _FontListener::onFontError(const rive::FontHandle handle,
uint64_t requestId,
std::string error)
{
if (_observer)
{
[_observer onFontError:reinterpret_cast<uint64_t>(handle)
requestID:requestId
message:[NSString stringWithUTF8String:error.c_str()]];
}
}
void _FontListener::onFontDeleted(const rive::FontHandle handle,
uint64_t requestId)
{
if (_observer)
{
[_observer onFontDeleted:reinterpret_cast<uint64_t>(handle)
requestID:requestId];
}
}
namespace
{
class _AudioListener : public rive::CommandQueue::AudioSourceListener
{
public:
_AudioListener(id<RiveAudioListener> observer) { _observer = observer; }
virtual void onAudioSourceDecoded(const rive::AudioSourceHandle handle,
uint64_t requestId) override;
virtual void onAudioSourceError(const rive::AudioSourceHandle handle,
uint64_t requestId,
std::string error) override;
virtual void onAudioSourceDeleted(const rive::AudioSourceHandle handle,
uint64_t requestId) override;
private:
__weak id<RiveAudioListener> _observer;
};
} // namespace
void _AudioListener::onAudioSourceDecoded(const rive::AudioSourceHandle handle,
uint64_t requestId)
{
if (_observer)
{
[_observer onAudioSourceDecoded:reinterpret_cast<uint64_t>(handle)
requestID:requestId];
}
}
void _AudioListener::onAudioSourceError(const rive::AudioSourceHandle handle,
uint64_t requestId,
std::string error)
{
if (_observer)
{
[_observer
onAudioSourceError:reinterpret_cast<uint64_t>(handle)
requestID:requestId
message:[NSString stringWithUTF8String:error.c_str()]];
}
}
void _AudioListener::onAudioSourceDeleted(const rive::AudioSourceHandle handle,
uint64_t requestId)
{
if (_observer)
{
[_observer onAudioSourceDeleted:reinterpret_cast<uint64_t>(handle)
requestID:requestId];
}
}
/**
* A concrete implementation of RiveCommandQueueProtocol that bridges with the
* C++ command queue.
*/
@implementation RiveCommandQueue
{
/** The underlying C++ command queue that handles Rive operations */
rive::rcp<rive::CommandQueue> _commandQueue;
/** Dictionary mapping file handles to their listeners for proper cleanup */
NSMutableDictionary<NSNumber*, NSValue*>* _fileListeners;
/** Dictionary mapping artboard handles to their listeners for proper
* cleanup */
NSMutableDictionary<NSNumber*, NSValue*>* _artboardListeners;
/** Dictionary mapping view model instance handles to their listeners for
* proper cleanup */
NSMutableDictionary<NSNumber*, NSValue*>* _viewModelInstanceListeners;
/** Dictionary mapping render image handles to their listeners for proper
* cleanup */
NSMutableDictionary<NSNumber*, NSValue*>* _renderImageListeners;
/** Dictionary mapping font handles to their listeners for proper cleanup */
NSMutableDictionary<NSNumber*, NSValue*>* _fontListeners;
/** Dictionary mapping audio handles to their listeners for proper cleanup
*/
NSMutableDictionary<NSNumber*, NSValue*>* _audioListeners;
/** The next request ID to use when making a request via command queue */
uint64_t _nextRequestID;
/** The display link used for processing messages synchronized with display
* refresh */
#if RIVE_USE_CVDISPLAYLINK
CVDisplayLinkRef _processDisplayLink;
#else
CADisplayLink* _processDisplayLink;
#endif
}
/**
* Initializes a new RiveCommandQueue instance.
*
* This initializer sets up the command queue with a new C++ command queue
* instance and a file listener for handling file-related events. The
* initialization must occur on the main thread to ensure proper UI integration.
*
* @return An initialized RiveCommandQueue instance
* @note This method must be called on the main thread
*/
- (instancetype)init
{
assert([NSThread isMainThread]);
if (self = [super init])
{
_commandQueue = rive::make_rcp<rive::CommandQueue>();
_fileListeners = [[NSMutableDictionary alloc] init];
_artboardListeners = [[NSMutableDictionary alloc] init];
_viewModelInstanceListeners = [[NSMutableDictionary alloc] init];
_renderImageListeners = [[NSMutableDictionary alloc] init];
_fontListeners = [[NSMutableDictionary alloc] init];
_audioListeners = [[NSMutableDictionary alloc] init];
_nextRequestID = 0;
}
return self;
}
/**
* Cleans up resources when the command queue is deallocated.
*/
- (void)dealloc
{
#if RIVE_USE_CVDISPLAYLINK
CVDisplayLinkStop(_processDisplayLink);
#else
[_processDisplayLink invalidate];
#endif
// Clean up all file listeners
for (NSValue* listenerValue in _fileListeners.allValues)
{
_FileListener* listener =
static_cast<_FileListener*>(listenerValue.pointerValue);
delete listener;
}
// Clean up all artboard listeners