diff --git a/defold-rive/api/rive.script_api b/defold-rive/api/rive.script_api index b72b4f1e..e461ae04 100644 --- a/defold-rive/api/rive.script_api +++ b/defold-rive/api/rive.script_api @@ -84,6 +84,22 @@ type: constant desc: The rate with which the animation will be played. Must be positive. + - name: callback_function + type: function + desc: function to call when a playback event occurs + parameters: + - name: self + type: object + desc: The context of the calling script + + - name: message_id + type: hash + desc: The name of the event + + - name: message + type: table + desc: A table that contains the event properties + #***************************************************************************************************** - name: cancel diff --git a/defold-rive/src/comp_rive.cpp b/defold-rive/src/comp_rive.cpp index db686590..7bafd6ce 100644 --- a/defold-rive/src/comp_rive.cpp +++ b/defold-rive/src/comp_rive.cpp @@ -46,6 +46,7 @@ #include "renderer.h" // DMSDK +#include #include #include #include @@ -543,39 +544,10 @@ namespace dmRive } rive::Event* event = reported_event.event(); - const char* event_name = event->name().c_str(); - for (auto child : event->children()) - { - if (child->is()) - { - if (!child->name().empty()) - { - const char* key = child->name().c_str(); - switch (child->coreType()) - { - case rive::CustomPropertyBoolean::typeKey: - { - bool b = child->as()->propertyValue(); - break; - } - case rive::CustomPropertyString::typeKey: - { - const char* s = child->as()->propertyValue().c_str(); - break; - } - case rive::CustomPropertyNumber::typeKey: - { - float f = child->as()->propertyValue(); - break; - } - } - } - } - } dmRive::RiveEventCallback callback; callback = component.m_EventCallbackData->m_EventCallback; - callback(component.m_EventCallbackData, sender, event_name); + callback(component.m_EventCallbackData, sender, event); } dmGameObject::UpdateResult CompRiveUpdate(const dmGameObject::ComponentsUpdateParams& params, dmGameObject::ComponentsUpdateResult& update_result) @@ -1313,6 +1285,55 @@ namespace dmRive // SCRIPTING HELPER FUNCTIONS // ****************************************************************************** + void CompRivePushEventName(lua_State* L, rive::Event* event) + { + const char* event_name = event->name().c_str(); + lua_pushstring(L, event_name); + } + + void CompRivePushEventProperties(lua_State* L, rive::Event* event) + { + lua_newtable(L); + + for (auto child : event->children()) + { + if (child->is()) + { + if (!child->name().empty()) + { + const char* key = child->name().c_str(); + switch (child->coreType()) + { + case rive::CustomPropertyBoolean::typeKey: + { + bool b = child->as()->propertyValue(); + lua_pushstring(L, key); + lua_pushboolean(L, b); + lua_settable(L, -3); + break; + } + case rive::CustomPropertyString::typeKey: + { + const char* s = child->as()->propertyValue().c_str(); + lua_pushstring(L, key); + lua_pushstring(L, s); + lua_settable(L, -3); + break; + } + case rive::CustomPropertyNumber::typeKey: + { + float f = child->as()->propertyValue(); + lua_pushstring(L, key); + lua_pushnumber(L, f); + lua_settable(L, -3); + break; + } + } + } + } + } + } + bool CompRiveGetBoneID(RiveComponent* component, dmhash_t bone_name, dmhash_t* id) { uint32_t num_bones = component->m_Bones.Size(); diff --git a/defold-rive/src/comp_rive.h b/defold-rive/src/comp_rive.h index f260998e..f46e61b7 100644 --- a/defold-rive/src/comp_rive.h +++ b/defold-rive/src/comp_rive.h @@ -14,6 +14,7 @@ #define DM_GAMESYS_COMP_RIVE_H #include +#include #include #include #include @@ -35,7 +36,7 @@ namespace dmRive struct RiveBuffer; // callback for event trigger - typedef void (*RiveEventCallback)(void* callback_data, dmMessage::URL component, const char* event_name); + typedef void (*RiveEventCallback)(void* callback_data, dmMessage::URL component, rive::Event* event); // struct to hold script callback data for events struct RiveEventCallbackData @@ -90,6 +91,9 @@ namespace dmRive // Get the game object identifier bool CompRiveGetBoneID(RiveComponent* component, dmhash_t bone_name, dmhash_t* id); + void CompRivePushEventName(lua_State* L, rive::Event* event); + void CompRivePushEventProperties(lua_State* L, rive::Event* event); + void CompRivePointerMove(RiveComponent* component, float x, float y); void CompRivePointerUp(RiveComponent* component, float x, float y); void CompRivePointerDown(RiveComponent* component, float x, float y); diff --git a/defold-rive/src/script_rive.cpp b/defold-rive/src/script_rive.cpp index 6e093bd9..9f40d402 100644 --- a/defold-rive/src/script_rive.cpp +++ b/defold-rive/src/script_rive.cpp @@ -102,7 +102,7 @@ namespace dmRive return 0; } - void OnRiveEvent(void* callback_data, dmMessage::URL component, const char* event_name) + void OnRiveEvent(void* callback_data, dmMessage::URL component, rive::Event* event) { RiveEventCallbackData* event_callback_data = (RiveEventCallbackData*)callback_data; @@ -125,9 +125,9 @@ namespace dmRive return; } - // dmScript::PushURL(L, component); - lua_pushstring(L, event_name); - dmScript::PCall(L, 2, 0); + CompRivePushEventName(L, event); + CompRivePushEventProperties(L, event); + dmScript::PCall(L, 3, 0); dmScript::TeardownCallback(lua_callback); } diff --git a/main/fighting-game/fighting-game.script b/main/fighting-game/fighting-game.script index cab6d1ea..5ddc8e12 100644 --- a/main/fighting-game/fighting-game.script +++ b/main/fighting-game/fighting-game.script @@ -1,11 +1,9 @@ function init(self) - --rive.play_anim("#model", "panelAnimateOn", go.PLAYBACK_LOOP_FORWARD) - --rive.play_anim("#model", "cape", go.PLAYBACK_LOOP_FORWARD) - --rive.play_anim("#model", "bodyLoop", go.PLAYBACK_LOOP_FORWARD) msg.post(".", "acquire_input_focus") rive.play_state_machine("#model", "State Machine 1", nil, function(self, message_id, message) print("play_state_machine", message_id) + pprint(message) end)