Skip to content

Commit ff7c88e

Browse files
committed
More asset handling goodies in rive artboard
1 parent 078fcbb commit ff7c88e

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

modules/yup_core/yup_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
linuxLibs: rt dl pthread
5858
androidLibs: log android
5959
androidSearchpaths: {ANDROID_NDK}/sources/android/cpufeatures
60-
mingwLibs: ws2_32 uuid wininet version kernel32 user32 wsock32 advapi32 ole32 oleaut32 imm32 comdlg32 shlwapi rpcrt4 winmm
60+
mingwLibs: ws2_32 uuid wininet version kernel32 user32 wsock32 advapi32 secur32 ole32 oleaut32 imm32 comdlg32 shlwapi rpcrt4 winmm pathcch
6161
6262
END_YUP_MODULE_DECLARATION
6363

modules/yup_gui/artboard/yup_ArtboardFile.cpp

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@
2222
namespace yup
2323
{
2424

25+
namespace
26+
{
27+
28+
//==============================================================================
29+
30+
class LambdaAssetLoader : public rive::FileAssetLoader
31+
{
32+
public:
33+
LambdaAssetLoader (const ArtboardFile::AssetLoadCallback& assetCallback)
34+
: assetCallback (assetCallback)
35+
{
36+
}
37+
38+
bool loadContents (rive::FileAsset& asset,
39+
rive::Span<const uint8_t> inBandBytes,
40+
rive::Factory* factory) override
41+
{
42+
jassert (factory != nullptr);
43+
44+
ArtboardFile::AssetInfo assetInfo;
45+
assetInfo.uniqueName = String (asset.uniqueName());
46+
assetInfo.uniquePath = String (asset.uniqueFilename());
47+
assetInfo.extension = String (asset.fileExtension());
48+
49+
return assetCallback (assetInfo, Span<const uint8>{ inBandBytes.data(), inBandBytes.size() }, *factory);
50+
}
51+
52+
private:
53+
ArtboardFile::AssetLoadCallback assetCallback;
54+
};
55+
56+
} // namespace
57+
2558
//==============================================================================
2659

2760
ArtboardFile::ArtboardFile (std::unique_ptr<rive::File> rivFile)
@@ -44,6 +77,11 @@ rive::File* ArtboardFile::getRiveFile()
4477
//==============================================================================
4578

4679
ArtboardFile::LoadResult ArtboardFile::load (const File& file, rive::Factory& factory)
80+
{
81+
return load (file, factory, nullptr);
82+
}
83+
84+
ArtboardFile::LoadResult ArtboardFile::load (const File& file, rive::Factory& factory, const AssetLoadCallback& assetCallback)
4785
{
4886
if (! file.existsAsFile())
4987
return LoadResult::fail ("Failed to find artboard file to load");
@@ -52,19 +90,39 @@ ArtboardFile::LoadResult ArtboardFile::load (const File& file, rive::Factory& fa
5290
if (is == nullptr || ! is->openedOk())
5391
return LoadResult::fail ("Failed to open artboard file for reading");
5492

55-
return load (*is, factory);
93+
return load (*is, factory, assetCallback);
5694
}
5795

96+
//==============================================================================
97+
5898
ArtboardFile::LoadResult ArtboardFile::load (InputStream& is, rive::Factory& factory)
99+
{
100+
return load (is, factory, nullptr);
101+
}
102+
103+
ArtboardFile::LoadResult ArtboardFile::load (InputStream& is, rive::Factory& factory, const AssetLoadCallback& assetCallback)
59104
{
60105
yup::MemoryBlock mb;
61106
is.readIntoMemoryBlock (mb);
62107

63108
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));
109+
std::unique_ptr<rive::File> rivFile;
110+
111+
if (assetCallback != nullptr)
112+
{
113+
rivFile = rive::File::import (
114+
{ static_cast<const uint8_t*> (mb.getData()), mb.getSize() },
115+
std::addressof (factory),
116+
std::addressof (result),
117+
rive::make_rcp<LambdaAssetLoader> (assetCallback));
118+
}
119+
else
120+
{
121+
rivFile = rive::File::import (
122+
{ static_cast<const uint8_t*> (mb.getData()), mb.getSize() },
123+
std::addressof (factory),
124+
std::addressof (result));
125+
}
68126

69127
if (result == rive::ImportResult::malformed)
70128
return LoadResult::fail ("Malformed artboard file");

modules/yup_gui/artboard/yup_ArtboardFile.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ class Artboard;
3232
class YUP_API ArtboardFile
3333
{
3434
public:
35+
//==============================================================================
36+
37+
struct AssetInfo
38+
{
39+
AssetInfo() = default;
40+
41+
String uniqueName;
42+
File uniquePath;
43+
String extension;
44+
};
45+
3546
//==============================================================================
3647
/** The result of loading a Rive file. */
3748
using LoadResult = ResultValue<std::shared_ptr<ArtboardFile>>;
49+
using AssetLoadCallback = std::function<bool (const AssetInfo&, Span<const uint8>, rive::Factory& factory)>;
3850

3951
/** Loads a Rive file from a file.
4052
@@ -45,6 +57,15 @@ class YUP_API ArtboardFile
4557
*/
4658
static LoadResult load (const File& file, rive::Factory& factory);
4759

60+
/** Loads a Rive file from a file.
61+
62+
@param file The file to load.
63+
@param factory The factory to use to create the Rive file.
64+
65+
@return The result of loading the Rive file.
66+
*/
67+
static LoadResult load (const File& file, rive::Factory& factory, const AssetLoadCallback& assetCallback);
68+
4869
/** Loads a Rive file from an input stream.
4970
5071
@param is The input stream to load the Rive file from.
@@ -54,6 +75,16 @@ class YUP_API ArtboardFile
5475
*/
5576
static LoadResult load (InputStream& is, rive::Factory& factory);
5677

78+
/** Loads a Rive file from an input stream.
79+
80+
@param is The input stream to load the Rive file from.
81+
@param factory The factory to use to create the Rive file.
82+
@param assetCallback The callback that will be invoked when loading to use to create the Rive file.
83+
84+
@return The result of loading the Rive file.
85+
*/
86+
static LoadResult load (InputStream& is, rive::Factory& factory, const AssetLoadCallback& assetCallback);
87+
5788
//==============================================================================
5889
/** Returns the underlying Rive file. */
5990
const rive::File* getRiveFile() const;

modules/yup_gui/yup_gui.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
//==============================================================================
3636

3737
#include <rive/layout.hpp>
38+
#include <rive/file_asset_loader.hpp>
39+
#include <rive/assets/file_asset.hpp>
3840
#include <rive/custom_property_number.hpp>
3941
#include <rive/custom_property_boolean.hpp>
4042
#include <rive/custom_property_string.hpp>

0 commit comments

Comments
 (0)