Skip to content

Commit

Permalink
Add editor support for setting a state machine or animation from a di…
Browse files Browse the repository at this point in the history
…fferent artboard
  • Loading branch information
Jhonnyg committed Nov 14, 2024
1 parent e011210 commit 3519239
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 72 deletions.
77 changes: 43 additions & 34 deletions defold-rive/editor/src/rive.clj
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@

(property content g/Any)
(property rive-handle g/Any) ; The cpp pointer
(property animations g/Any)
(property state-machine-ids g/Any)
(property artboard-id-list g/Any)
(property aabb g/Any)
(property vertices g/Any)
(property bones g/Any)
Expand Down Expand Up @@ -220,15 +219,25 @@
; bones is a list of root Rive$Bone (Rive.java)
(mapcat (fn [bone] (create-bone-hierarchy parent-id bone)) bones))

;; Converts a list of java objects into a clojure map where we can get
;; all the animations and state machines based on an artboard name.
(defn- to-artboard-id-list [artboard-id-list-from-java]
(into {}
(map (fn [id-list]
[(.-artboardId id-list)
{:animation-ids (into [] (.-animations id-list))
:state-machine-ids (mapv (fn [sm] (.-name sm)) (.-stateMachines id-list))}])
artboard-id-list-from-java)))

; Loads the .riv file
(defn- load-rive-file
[project node-id resource]
(let [content (resource->bytes resource)
rive-handle (plugin-load-file content (resource/resource->proj-path resource))
animations (.-animations rive-handle)
artboard-id-list-from-java (.-artboardId rive-handle)
artboard-id-list (to-artboard-id-list artboard-id-list-from-java)
artboards (.-artboards rive-handle)
state-machines (.-stateMachines rive-handle)
state-machine-ids (map (fn [state-machine] (.-name state-machine)) state-machines)

_ (.Update rive-handle 0.0)
aabb (convert-aabb (.-aabb rive-handle))
bones (.-bones rive-handle)
Expand All @@ -237,8 +246,7 @@
(g/set-property node-id :content content)
(g/set-property node-id :rive-handle rive-handle)
(g/set-property node-id :artboards artboards)
(g/set-property node-id :animations animations)
(g/set-property node-id :state-machine-ids state-machine-ids)
(g/set-property node-id :artboard-id-list artboard-id-list)
(g/set-property node-id :aabb aabb)
(g/set-property node-id :bones bones))

Expand Down Expand Up @@ -542,7 +550,7 @@
(assert (= (:pass render-args) pass/outline))
(render/render-aabb-outline gl render-args ::rive-outline renderables rcount))

(g/defnk produce-main-scene [_node-id material-shader rive-file-handle rive-anim-ids aabb gpu-texture default-tex-params rive-scene-pb scene-structure texture-set-pb]
(g/defnk produce-main-scene [_node-id material-shader rive-file-handle aabb gpu-texture default-tex-params rive-scene-pb scene-structure texture-set-pb]
(when rive-file-handle
(let [blend-mode :blend-mode-alpha]
(assoc {:node-id _node-id :aabb aabb}
Expand Down Expand Up @@ -590,8 +598,7 @@
[:rive-handle :rive-file-handle]
[:structure :scene-structure]
[:artboards :rive-artboards]
[:animations :rive-anim-ids]
[:state-machine-ids :rive-state-machine-ids]
[:artboard-id-list :rive-artboard-id-list]
[:aabb :aabb]
[:node-outline :source-outline]
[:build-targets :dep-build-targets])))
Expand Down Expand Up @@ -628,8 +635,7 @@
(input rive-file-resource resource/Resource)
(input rive-file-handle g/Any)
(input rive-artboards g/Any)
(input rive-anim-ids g/Any)
(input rive-state-machine-ids g/Any)
(input rive-artboard-id-list g/Any)
(input aabb g/Any)
(input atlas-resource resource/Resource)

Expand All @@ -653,8 +659,7 @@
(output material-shader ShaderLifecycle (gu/passthrough material-shader))
(output rive-file-handle g/Any :cached (gu/passthrough rive-file-handle))
(output rive-artboards g/Any :cached (gu/passthrough rive-artboards))
(output rive-anim-ids g/Any :cached (gu/passthrough rive-anim-ids))
(output rive-state-machine-ids g/Any :cached (gu/passthrough rive-state-machine-ids))
(output rive-artboard-id-list g/Any :cached (gu/passthrough rive-artboard-id-list))
(output aabb g/Any :cached (gu/passthrough aabb)))

; .rivescene
Expand Down Expand Up @@ -695,37 +700,39 @@
artboard
(set rive-artboards))))

(defn- validate-model-default-animation [node-id rive-scene rive-anim-ids default-animation]
(defn- validate-model-default-animation [node-id rive-scene animation-ids default-animation]
(when (and rive-scene (not-empty default-animation))
(validation/prop-error :fatal node-id :default-animation
(fn [anim ids]
(when-not (contains? ids anim)
(format "animation '%s' could not be found in the specified rive scene" anim)))
default-animation
(set rive-anim-ids))))
(set animation-ids))))

(defn- validate-model-default-state-machine [node-id rive-scene rive-state-machine-ids default-state-machine]
(defn- validate-model-default-state-machine [node-id rive-scene state-machine-ids default-state-machine]
(when (and rive-scene (not-empty default-state-machine))
(validation/prop-error :fatal node-id :default-state-machine
(fn [anim ids]
(when-not (contains? ids anim)
(format "state machine '%s' could not be found in the specified rive scene" anim)))
(format "state machine '%s' could not be found in the specified artboard or rive scene" anim)))
default-state-machine
(set rive-state-machine-ids))))
(set state-machine-ids))))

(defn- validate-model-material [node-id material]
(prop-resource-error node-id :material material "Material"))

(defn- validate-model-rive-scene [node-id rive-scene]
(prop-resource-error node-id :scene rive-scene "Rive Scene"))

(g/defnk produce-model-own-build-errors [_node-id artboard default-animation default-state-machine material rive-artboards rive-anim-ids rive-state-machine-ids rive-scene scene-structure]
(g/package-errors _node-id
(g/defnk produce-model-own-build-errors [_node-id artboard default-animation default-state-machine material rive-artboards rive-artboard-id-list rive-scene scene-structure]
(let [state-machine-ids (:state-machine-ids (get rive-artboard-id-list artboard))
animation-ids (:animation-ids (get rive-artboard-id-list artboard))]
(g/package-errors _node-id
(validate-model-material _node-id material)
(validate-model-rive-scene _node-id rive-scene)
(validate-model-artboard _node-id rive-scene rive-artboards artboard)
(validate-model-default-animation _node-id rive-scene rive-anim-ids default-animation)
(validate-model-default-state-machine _node-id rive-scene rive-state-machine-ids default-state-machine)))
(validate-model-default-animation _node-id rive-scene animation-ids default-animation)
(validate-model-default-state-machine _node-id rive-scene state-machine-ids default-state-machine))))

(defn- build-rive-model [resource dep-resources user-data]
(let [pb (:proto-msg user-data)
Expand Down Expand Up @@ -759,8 +766,7 @@
[: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]
[:rive-artboard-id-list :rive-artboard-id-list]
[:build-targets :dep-build-targets]
[:anim-data :anim-data]
[:scene-structure :scene-structure])))
Expand All @@ -781,20 +787,24 @@
(dynamic edit-type (g/constantly {:type resource/Resource :ext "material"}))
(dynamic error (g/fnk [_node-id material]
(validate-model-material _node-id material))))

(property default-state-machine g/Str (default (protobuf/default rive-model-pb-class :default-state-machine))
(dynamic error (g/fnk [_node-id rive-state-machine-ids default-state-machine rive-scene]
(validate-model-default-state-machine _node-id rive-scene rive-state-machine-ids default-state-machine)))
(dynamic edit-type (g/fnk [rive-state-machine-ids] (properties/->choicebox (cons "" rive-state-machine-ids)))))
(dynamic error (g/fnk [_node-id artboard rive-artboard-id-list default-state-machine rive-scene]
(validate-model-default-state-machine _node-id rive-scene (:state-machine-ids (get rive-artboard-id-list artboard)) default-state-machine)))
(dynamic edit-type (g/fnk [artboard rive-artboard-id-list]
(properties/->choicebox (cons "" (:state-machine-ids (get rive-artboard-id-list artboard)))))))

(property default-animation g/Str
(dynamic error (g/fnk [_node-id artboard rive-artboard-id-list default-animation rive-scene]
(validate-model-default-animation _node-id rive-scene (:animation-ids (get rive-artboard-id-list artboard)) default-animation)))
(dynamic edit-type (g/fnk [artboard rive-artboard-id-list]
(properties/->choicebox (cons "" (:animation-ids (get rive-artboard-id-list artboard)))))))

(property artboard g/Str (default (protobuf/default rive-model-pb-class :artboard))
(dynamic error (g/fnk [_node-id rive-artboards artboard rive-scene]
(validate-model-artboard _node-id rive-scene rive-artboards artboard)))
(dynamic edit-type (g/fnk [rive-artboards] (properties/->choicebox (cons "" rive-artboards)))))

(property default-animation g/Str ; Required protobuf field.
(dynamic error (g/fnk [_node-id rive-anim-ids default-animation rive-scene]
(validate-model-default-animation _node-id rive-scene rive-anim-ids default-animation)))
(dynamic edit-type (g/fnk [rive-anim-ids] (properties/->choicebox (cons "" rive-anim-ids)))))
(property create-go-bones g/Bool (default (protobuf/default rive-model-pb-class :create-go-bones)))

(property coordinate-system g/Any (default (protobuf/default rive-model-pb-class :coordinate-system))
Expand All @@ -812,8 +822,7 @@
(input rive-main-scene g/Any)
(input scene-structure g/Any)
(input rive-artboards g/Any)
(input rive-anim-ids g/Any)
(input rive-state-machine-ids g/Any)
(input rive-artboard-id-list g/Any)
(input texture-set-pb g/Any)
(input atlas-resource resource/Resource)
(input material-resource resource/Resource)
Expand Down
Binary file modified defold-rive/plugins/lib/arm64-osx/libRiveExt.dylib
Binary file not shown.
Binary file modified defold-rive/plugins/lib/x86_64-linux/libRiveExt.so
Binary file not shown.
Binary file modified defold-rive/plugins/lib/x86_64-osx/libRiveExt.dylib
Binary file not shown.
Binary file modified defold-rive/plugins/lib/x86_64-win32/libRiveExt.dll
Binary file not shown.
Binary file modified defold-rive/plugins/share/pluginRiveExt.jar
Binary file not shown.
36 changes: 23 additions & 13 deletions defold-rive/pluginsrc/com/defold/bob/pipeline/Rive.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public static class StateMachine {
public StateMachineInput[] inputs;
}

public static class ArtboardIdList {
public String artboardId;
public StateMachine[] stateMachines;
public String[] animations;
}

public static class Bone {
public String name;
public int index;
Expand All @@ -99,12 +105,10 @@ public static class Bone {
public static class RiveFile {
public String path;
public long pointer;

public Aabb aabb;
public float[] vertices;
public int[] indices;
public String[] animations;
public StateMachine[] stateMachines;
public ArtboardIdList[] artboardId;
public Bone[] bones;
public RenderObject[] renderObjects;
public byte[] texture_set_bytes;
Expand Down Expand Up @@ -319,19 +323,25 @@ public static void main(String[] args) throws IOException {

System.out.printf("--------------------------------\n");

System.out.printf("Num animations: %d\n", rive_file.animations.length);
for (String animation : rive_file.animations)
{
System.out.printf("Num artboard id lists: %d\n", rive_file.artboardId.length);
for (ArtboardIdList artboardIdEntry : rive_file.artboardId) {
PrintIndent(1);
System.out.printf("%s\n", animation);
}
System.out.printf("Id: %s\n", artboardIdEntry.artboardId);

System.out.printf("--------------------------------\n");
PrintIndent(1);
System.out.printf("Num animations: %d\n", artboardIdEntry.animations.length);

System.out.printf("Num state machines: %d\n", rive_file.stateMachines.length);
for (StateMachine stateMachine : rive_file.stateMachines)
{
DebugStateMachine(stateMachine);
for (String animation : artboardIdEntry.animations) {
PrintIndent(2);
System.out.printf("%s\n", animation);
}

PrintIndent(1);
System.out.printf("Num state machines: %d\n", artboardIdEntry.stateMachines.length);

for (StateMachine stateMachine : artboardIdEntry.stateMachines) {
DebugStateMachine(stateMachine);
}
}

System.out.printf("--------------------------------\n");
Expand Down
11 changes: 8 additions & 3 deletions defold-rive/pluginsrc/rive_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ RiveFile* LoadFileFromBuffer(const void* buffer, size_t buffer_size, const char*
&result,
&atlas_resolver);

if (result != rive::ImportResult::success) {
//file = 0;
if (result != rive::ImportResult::success)
{
dmLogError("Failed to load rive file '%s'", path);
delete factory;
return 0;
Expand All @@ -45,7 +45,8 @@ RiveFile* LoadFileFromBuffer(const void* buffer, size_t buffer_size, const char*
out->m_Path = 0;
out->m_File = 0;

if (file) {
if (file)
{
out->m_Path = strdup(path);
out->m_File = file.release();
out->m_Factory = factory;
Expand Down Expand Up @@ -321,6 +322,10 @@ void SetArtboard(RiveFile* rive_file, const char* artboard)
return;
}

rive_file->m_ArtboardInstance.reset();
rive_file->m_AnimationInstance.reset();
rive_file->m_StateMachineInstance.reset();

if (artboard != 0)
{
rive_file->m_ArtboardInstance = rive_file->m_File->artboardNamed(artboard);
Expand Down
Loading

0 comments on commit 3519239

Please sign in to comment.