Skip to content

Commit d3e090f

Browse files
committed
More artboard fixes
1 parent 4a56981 commit d3e090f

File tree

8 files changed

+181
-61
lines changed

8 files changed

+181
-61
lines changed

examples/render/data/charge.riv

732 KB
Binary file not shown.

examples/render/source/main.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class CustomWindow
5353
.getChildFile ("alien.riv");
5454
#endif
5555

56+
auto factory = getNativeComponent()->getFactory();
57+
if (factory == nullptr)
58+
return;
59+
5660
// Setup artboards
5761
for (int i = 0; i < totalRows * totalColumns; ++i)
5862
{
@@ -61,9 +65,11 @@ class CustomWindow
6165

6266
#if JUCE_ANDROID
6367
yup::MemoryInputStream is (yup::RiveFile_data, yup::RiveFile_size, false);
64-
art->loadFromStream (is, 0, true);
68+
if (auto artboardFile = yup::ArtboardFile::load (is, *factory))
69+
art->setFile (artboardFile.getValue());
6570
#else
66-
art->loadFromFile (riveFilePath, 0, true);
71+
if (auto artboardFile = yup::ArtboardFile::load (riveFilePath, *factory))
72+
art->setFile (artboardFile.getValue());
6773
#endif
6874

6975
art->advanceAndApply (i * art->durationSeconds());
@@ -168,7 +174,7 @@ class CustomWindow
168174
}
169175

170176
yup::OwnedArray<yup::Artboard> artboards;
171-
int totalRows = 2;
177+
int totalRows = 1;
172178
int totalColumns = 1;
173179
};
174180

modules/yup_gui/artboard/yup_Artboard.cpp

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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

372366
void 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
}

modules/yup_gui/artboard/yup_Artboard.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ class JUCE_API Artboard : public Component
2828
public:
2929
//==============================================================================
3030
Artboard (StringRef componentID);
31+
Artboard (StringRef componentID, std::shared_ptr<ArtboardFile> artboardFile);
32+
33+
void setFile (std::shared_ptr<ArtboardFile> artboardFile);
3134

3235
//==============================================================================
33-
Result loadFromFile (const File& file, int defaultArtboardIndex = -1, bool shouldUseStateMachines = true);
34-
Result loadFromStream (InputStream& is, int defaultArtboardIndex = -1, bool shouldUseStateMachines = true);
36+
void clear();
3537

3638
//==============================================================================
3739
bool isPaused() const;
@@ -56,6 +58,8 @@ class JUCE_API Artboard : public Component
5658
void setInput (const String& state, const var& value);
5759

5860
//==============================================================================
61+
std::function<void (Artboard&, const String&, const String&, const var&, const var&)> onPropertyChanged;
62+
5963
virtual void propertyChanged (const String& eventName, const String& propertyName, const var& oldValue, const var& newValue);
6064

6165
//==============================================================================
@@ -74,20 +78,16 @@ class JUCE_API Artboard : public Component
7478
void updateSceneFromFile();
7579
void pullEventsFromStateMachines();
7680

77-
std::shared_ptr<rive::File> rivFile;
81+
std::shared_ptr<ArtboardFile> artboardFile;
82+
7883
std::unique_ptr<rive::Artboard> artboard;
7984
std::unique_ptr<rive::Scene> scene;
8085
rive::StateMachineInstance* stateMachine = nullptr;
86+
8187
HashMap<String, var> eventProperties;
8288

8389
rive::Mat2D viewTransform;
8490

85-
int artboardIndex = -1;
86-
int animationIndex = -1;
87-
int stateMachineIndex = -1;
88-
float animationTime = 0.0f;
89-
90-
bool useStateMachines = true;
9191
bool paused = false;
9292
};
9393

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
==============================================================================
3+
4+
This file is part of the YUP library.
5+
Copyright (c) 2024 - kunitoki@gmail.com
6+
7+
YUP is an open source library subject to open-source licensing.
8+
9+
The code included in this file is provided under the terms of the ISC license
10+
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
11+
to use, copy, modify, and/or distribute this software for any purpose with or
12+
without fee is hereby granted provided that the above copyright notice and
13+
this permission notice appear in all copies.
14+
15+
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
16+
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
17+
DISCLAIMED.
18+
19+
==============================================================================
20+
*/
21+
22+
namespace yup
23+
{
24+
25+
//==============================================================================
26+
27+
ArtboardFile::ArtboardFile (std::unique_ptr<rive::File> rivFile)
28+
: rivFile (std::move (rivFile))
29+
{
30+
}
31+
32+
//==============================================================================
33+
34+
const rive::File* ArtboardFile::getRiveFile() const
35+
{
36+
return rivFile.get();
37+
}
38+
39+
rive::File* ArtboardFile::getRiveFile()
40+
{
41+
return rivFile.get();
42+
}
43+
44+
//==============================================================================
45+
46+
ArtboardFile::LoadResult ArtboardFile::load(const File& file, rive::Factory& factory)
47+
{
48+
if (! file.existsAsFile())
49+
return LoadResult::fail ("Failed to find artboard file to load");
50+
51+
auto is = file.createInputStream();
52+
if (is == nullptr || ! is->openedOk())
53+
return LoadResult::fail ("Failed to open artboard file for reading");
54+
55+
return load (*is, factory);
56+
}
57+
58+
ArtboardFile::LoadResult ArtboardFile::load (InputStream& is, rive::Factory& factory)
59+
{
60+
yup::MemoryBlock mb;
61+
is.readIntoMemoryBlock (mb);
62+
63+
rive::ImportResult result;
64+
auto rivFile = rive::File::import (
65+
{ static_cast<const uint8_t*> (mb.getData()), mb.getSize() },
66+
std::addressof(factory),
67+
std::addressof(result));
68+
69+
if (result == rive::ImportResult::malformed)
70+
return LoadResult::fail ("Malformed artboard file");
71+
72+
if (result == rive::ImportResult::unsupportedVersion)
73+
return LoadResult::fail ("Unsupported artboard file for current runtime");
74+
75+
if (rivFile == nullptr)
76+
return LoadResult::fail ("Failed to import artboard file");
77+
78+
return LoadResult::ok (std::shared_ptr<ArtboardFile> (new ArtboardFile{ std::move (rivFile) }));
79+
}
80+
81+
} // namespace yup
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
==============================================================================
3+
4+
This file is part of the YUP library.
5+
Copyright (c) 2024 - kunitoki@gmail.com
6+
7+
YUP is an open source library subject to open-source licensing.
8+
9+
The code included in this file is provided under the terms of the ISC license
10+
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
11+
to use, copy, modify, and/or distribute this software for any purpose with or
12+
without fee is hereby granted provided that the above copyright notice and
13+
this permission notice appear in all copies.
14+
15+
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
16+
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
17+
DISCLAIMED.
18+
19+
==============================================================================
20+
*/
21+
22+
namespace yup
23+
{
24+
25+
//==============================================================================
26+
class JUCE_API ArtboardFile
27+
{
28+
public:
29+
//==============================================================================
30+
const rive::File* getRiveFile() const;
31+
rive::File* getRiveFile();
32+
33+
//==============================================================================
34+
using LoadResult = ResultValue<std::shared_ptr<ArtboardFile>>;
35+
36+
static LoadResult load(const File& file, rive::Factory& factory);
37+
static LoadResult load(InputStream& is, rive::Factory& factory);
38+
39+
private:
40+
ArtboardFile() = default;
41+
ArtboardFile (std::unique_ptr<rive::File> rivFile);
42+
43+
std::unique_ptr<rive::File> rivFile;
44+
};
45+
46+
} // namespace yup

modules/yup_gui/yup_gui.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include "widgets/yup_Button.cpp"
8686
#include "widgets/yup_TextButton.cpp"
8787
#include "widgets/yup_Slider.cpp"
88+
#include "artboard/yup_ArtboardFile.cpp"
8889
#include "artboard/yup_Artboard.cpp"
8990
#include "windowing/yup_DocumentWindow.cpp"
9091

modules/yup_gui/yup_gui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@
8181
#include "widgets/yup_Button.h"
8282
#include "widgets/yup_TextButton.h"
8383
#include "widgets/yup_Slider.h"
84+
#include "artboard/yup_ArtboardFile.h"
8485
#include "artboard/yup_Artboard.h"
8586
#include "windowing/yup_DocumentWindow.h"

0 commit comments

Comments
 (0)