Skip to content

Commit

Permalink
Added rive model update when changing selected artboard
Browse files Browse the repository at this point in the history
  • Loading branch information
JCash committed Nov 16, 2023
1 parent 2c4bb5f commit 0419aba
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 6 deletions.
10 changes: 8 additions & 2 deletions defold-rive/editor/src/rive.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
(defn- plugin-update-file [file dt bytes-texture-set]
(plugin-invoke-static rive-plugin-cls "UpdateInternal" (into-array Class [rive-plugin-file-cls Float/TYPE byte-array-cls]) [file (float dt) bytes-texture-set]))

(defn- plugin-set-artboard [file artboard]
(plugin-invoke-static rive-plugin-cls "SetArtboardInternal" (into-array Class [rive-plugin-file-cls String]) [file artboard]))

; .rivemodel
(defn load-rive-model [project self resource content]
(let [rive-scene-resource (workspace/resolve-resource resource (:scene content))
Expand Down Expand Up @@ -742,6 +745,7 @@
[:atlas-resource :atlas-resource]
[:texture-set-pb :texture-set-pb]
[:main-scene :rive-main-scene]
[:rive-file-handle :rive-file-handle]
[:rive-artboards :rive-artboards]
[:rive-anim-ids :rive-anim-ids]
[:rive-state-machine-ids :rive-state-machine-ids]
Expand Down Expand Up @@ -782,6 +786,7 @@
(property create-go-bones g/Bool (default false))

(input dep-build-targets g/Any :array)
(input rive-file-handle g/Any)
(input rive-scene-resource resource/Resource)
(input rive-main-scene g/Any)
(input scene-structure g/Any)
Expand All @@ -802,10 +807,11 @@
(output anim-ids g/Any :cached (g/fnk [anim-data] (vec (sort (keys anim-data)))))
(output state-machine-ids g/Any :cached (g/fnk [anim-data] (vec (sort (keys anim-data)))))
(output material-shader ShaderLifecycle (gu/passthrough material-shader))
(output scene g/Any :cached (g/fnk [_node-id rive-main-scene material-shader]
(output scene g/Any :cached (g/fnk [_node-id rive-file-handle artboard rive-main-scene artboard default-animation material-shader]
(if (and (some? material-shader) (some? (:renderable rive-main-scene)))
(let [aabb (:aabb rive-main-scene)
rive-scene-node-id (:node-id rive-main-scene)]
rive-scene-node-id (:node-id rive-main-scene)
_ (plugin-set-artboard rive-file-handle artboard )]
(-> rive-main-scene
(assoc-in [:renderable :user-data :shader] material-shader)
;;;;;;;;;;;(assoc :gpu-texture texture/white-pixel)
Expand Down
Binary file modified defold-rive/plugins/lib/arm64-osx/libRiveExt.dylib
Binary file not shown.
Binary file modified defold-rive/plugins/share/pluginRiveExt.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions defold-rive/pluginsrc/com/defold/bob/pipeline/Rive.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static class Bone {
public static native RiveFile LoadFromBufferInternal(String path, byte[] buffer);
public static native void Destroy(RiveFile rive_file);
public static native void Update(RiveFile rive_file, float dt, byte[] texture_set_buffer);
public static native void SetArtboard(RiveFile rive_file, String artboard);
public static native void DebugPrint();

public static class RiveFile {
Expand Down Expand Up @@ -123,6 +124,11 @@ public static void UpdateInternal(RiveFile rive_file, float dt, byte[] texture_s
Rive.Update(rive_file, dt, texture_set_pb);
}

public static void SetArtboardInternal(RiveFile rive_file, String artboard)
{
Rive.SetArtboard(rive_file, artboard);
}

public static RiveFile LoadFromBuffer(String path, byte[] bytes)
{
RiveFile rive_file = Rive.LoadFromBufferInternal(path, bytes);
Expand Down
14 changes: 13 additions & 1 deletion defold-rive/pluginsrc/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ static void JNICALL Java_Rive_Update(JNIEnv* env, jclass cls, jobject rive_file,
DM_CHECK_JNI_ERROR();
}

static void JNICALL Java_Rive_SetArtboard(JNIEnv* env, jclass cls, jobject rive_file, jstring _artboard)
{
DM_CHECK_JNI_ERROR();

dmDefoldJNI::ScopedString j_artboard(env, _artboard);
const char* artboard = j_artboard.m_String;

TypeRegister register_t(env);
dmRiveJNI::SetArtboard(env, cls, rive_file, artboard);
DM_CHECK_JNI_ERROR();
}

// static JNIEXPORT jlong JNICALL Java_RiveFile_AddressOf(JNIEnv* env, jclass cls, jobject object)
// {
// TypeRegister register_t(env);
Expand Down Expand Up @@ -215,6 +227,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
DM_JNI_FUNCTION(LoadFromBufferInternal, "(Ljava/lang/String;[B)Lcom/dynamo/bob/pipeline/Rive$RiveFile;"),
DM_JNI_FUNCTION(Destroy, "(Lcom/dynamo/bob/pipeline/Rive$RiveFile;)V"),
DM_JNI_FUNCTION(Update, "(Lcom/dynamo/bob/pipeline/Rive$RiveFile;F[B)V"),
DM_JNI_FUNCTION(SetArtboard, "(Lcom/dynamo/bob/pipeline/Rive$RiveFile;Ljava/lang/String;)V"),
//DM_JNI_FUNCTION(AddressOf, "(Ljava/lang/Object;)J"),
};
#undef DM_JNI_FUNCTION
Expand All @@ -227,4 +240,3 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
dmLogDebug("JNI_OnLoad return.\n");
return JNI_VERSION_1_8;
}

24 changes: 24 additions & 0 deletions defold-rive/pluginsrc/rive_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,30 @@ static dmGameSystemDDF::TextureSet* LoadAtlasFromBuffer(void* buffer, size_t buf
return texture_set_ddf;
}

void SetArtboard(RiveFile* rive_file, const char* artboard)
{
if (!rive_file->m_File)
{
return;
}

if (artboard != 0)
{
rive_file->m_ArtboardInstance = rive_file->m_File->artboardNamed(artboard);
}

if (!rive_file->m_ArtboardInstance)
{
rive_file->m_ArtboardInstance = rive_file->m_File->artboardAt(0);
}
rive_file->m_ArtboardInstance->advance(0.0f);

dmRive::SetupBones(rive_file);

PlayAnimation(rive_file, 0);
Update(rive_file, 0.0f, 0, 0);
}

void Update(RiveFile* rive_file, float dt, const uint8_t* texture_set_data, uint32_t texture_set_data_length)
{
if (!rive_file->m_File)
Expand Down
1 change: 1 addition & 0 deletions defold-rive/pluginsrc/rive_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void SetupBones(RiveFile* file);

void PlayAnimation(RiveFile* rive_file, int index);
void Update(RiveFile* rive_file, float dt, const uint8_t* texture_set_data, uint32_t texture_set_data_length);
void SetArtboard(RiveFile* rive_file, const char* artboard);

} // namespace

Expand Down
14 changes: 14 additions & 0 deletions defold-rive/pluginsrc/rive_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,18 @@ void Update(JNIEnv* env, jclass cls, jobject rive_file_obj, jfloat dt, const uin
UpdateJNIRenderData(env, rive_file_obj, rive_file);
}

void SetArtboard(JNIEnv* env, jclass cls, jobject rive_file_obj, const char* artboard)
{
dmRive::RiveFile* rive_file = FromObject(env, rive_file_obj);
if (!rive_file || !rive_file->m_File)
{
return;
}

dmRive::SetArtboard(rive_file, artboard);

UpdateJNIRenderData(env, rive_file_obj, rive_file);
}


} // namespace
1 change: 1 addition & 0 deletions defold-rive/pluginsrc/rive_jni.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace dmRiveJNI
jobject LoadFileFromBuffer(JNIEnv* env, jclass cls, const char* path, const uint8_t* data, uint32_t data_length);
void DestroyFile(JNIEnv* env, jclass cls, jobject rive_file);
void Update(JNIEnv* env, jclass cls, jobject rive_file, jfloat dt, const uint8_t* texture_set_data, uint32_t texture_set_data_length);
void SetArtboard(JNIEnv* env, jclass cls, jobject rive_file, const char* artboard);
}


Expand Down
3 changes: 0 additions & 3 deletions defold-rive/src/comp_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ namespace dmRive

dmRive::RiveSceneData* data = (dmRive::RiveSceneData*) component->m_Resource->m_Scene->m_Scene;

//Todo: choose artboard by index: m_ArtboardIndex = (index == REQUEST_DEFAULT_SCENE) ? 0 : index;
//component->m_ArtboardInstance = data->m_File->artboardAt(0);

if (component->m_Resource->m_DDF->m_Artboard)
{
component->m_ArtboardInstance = data->m_File->artboardNamed(component->m_Resource->m_DDF->m_Artboard);
Expand Down

0 comments on commit 0419aba

Please sign in to comment.