@@ -29,41 +29,35 @@ Artboard::Artboard (StringRef componentID)
2929{
3030}
3131
32+ Artboard::Artboard (StringRef componentID, std::shared_ptr<ArtboardFile> file)
33+ : Component (componentID)
34+ {
35+ setFile (std::move (file));
36+ }
37+
3238// ==============================================================================
3339
34- Result Artboard::loadFromFile ( const File& file, int defaultArtboardIndex, bool shouldUseStateMachines )
40+ void Artboard::setFile (std::shared_ptr<ArtboardFile> file)
3541{
36- if (! file.existsAsFile ())
37- return Result::fail (" Failed to find file to load" );
42+ clear ();
3843
39- auto is = file.createInputStream ();
40- if (is == nullptr || ! is->openedOk ())
41- return Result::fail (" Failed to open file for reading" );
44+ artboardFile = std::move (file);
4245
43- return loadFromStream (*is, defaultArtboardIndex, shouldUseStateMachines );
46+ updateSceneFromFile ( );
4447}
4548
46- Result Artboard::loadFromStream (InputStream& is, int defaultArtboardIndex, bool shouldUseStateMachines)
47- {
48- if (getNativeComponent () == nullptr )
49- return Result::fail (" Unable to access top level native component" );
50-
51- auto factory = getNativeComponent ()->getFactory ();
52- if (factory == nullptr )
53- return Result::fail (" Failed to create a graphics context" );
54-
55- yup::MemoryBlock mb;
56- is.readIntoMemoryBlock (mb);
49+ // ==============================================================================
5750
58- rivFile = rive::File::import ({ static_cast <const uint8_t *> (mb.getData ()), mb.getSize () }, factory);
59- artboardIndex = jlimit (-1 , static_cast <int > (rivFile->artboardCount ()) - 1 , defaultArtboardIndex);
51+ void Artboard::clear ()
52+ {
53+ artboardFile.reset ();
6054
61- useStateMachines = shouldUseStateMachines;
55+ artboard.reset ();
56+ scene.reset ();
6257
63- updateSceneFromFile ();
64- repaint ();
58+ stateMachine = nullptr ;
6559
66- return Result::ok ();
60+ eventProperties. clear ();
6761}
6862
6963// ==============================================================================
@@ -371,38 +365,26 @@ void Artboard::propertyChanged (const String& eventName, const String& propertyN
371365
372366void Artboard::updateSceneFromFile ()
373367{
374- jassert (rivFile != nullptr );
375-
376368 artboard.reset ();
377369 scene.reset ();
378370 stateMachine = nullptr ;
379- eventProperties.clear ();
380371
381- auto currentArtboard = (artboardIndex == -1 )
382- ? rivFile->artboardDefault ()
383- : rivFile->artboard (artboardIndex)->instance ();
372+ auto rivFile = artboardFile->getRiveFile ();
373+ if (rivFile == nullptr )
374+ return ;
375+
376+ auto currentArtboard = rivFile->artboardDefault ();
377+ if (currentArtboard == nullptr )
378+ return ;
384379
385380 std::unique_ptr<rive::Scene> currentScene;
386381 rive::StateMachineInstance* currentStateMachine = nullptr ;
387382
388- if (useStateMachines)
389- {
390- if (yup::isPositiveAndBelow (stateMachineIndex, currentArtboard->stateMachineCount ()))
391- {
392- auto machine = currentArtboard->stateMachineAt (stateMachineIndex);
393- currentStateMachine = machine.get ();
394- currentScene = std::move (machine);
395- }
396- else if (currentArtboard->stateMachineCount () > 0 )
397- {
398- auto machine = currentArtboard->defaultStateMachine ();
399- currentStateMachine = machine.get ();
400- currentScene = std::move (machine);
401- }
402- }
403- else if (yup::isPositiveAndBelow (animationIndex, currentArtboard->animationCount ()))
383+ if (currentArtboard->stateMachineCount () > 0 )
404384 {
405- currentScene = currentArtboard->animationAt (animationIndex);
385+ auto machine = currentArtboard->defaultStateMachine ();
386+ currentStateMachine = machine.get ();
387+ currentScene = std::move (machine);
406388 }
407389 else if (currentArtboard->animationCount () > 0 )
408390 {
@@ -416,8 +398,7 @@ void Artboard::updateSceneFromFile()
416398
417399 artboard = std::move (currentArtboard);
418400 scene = std::move (currentScene);
419- if (currentStateMachine)
420- stateMachine = currentStateMachine;
401+ stateMachine = currentStateMachine;
421402}
422403
423404// ==============================================================================
@@ -455,7 +436,11 @@ void Artboard::pullEventsFromStateMachines()
455436 continue ;
456437
457438 eventProperties.set (eventName, newValue);
439+
458440 propertyChanged (eventName, String (child->name ()), oldValue, newValue);
441+
442+ if (onPropertyChanged)
443+ onPropertyChanged (eventName, String (child->name ()), oldValue, newValue);
459444 }
460445 }
461446}
0 commit comments