Skip to content

Commit

Permalink
Add support for event properties (#79)
Browse files Browse the repository at this point in the history
* Added support for event properties

* Update rive.script_api

* Update rive.script_api
  • Loading branch information
britzl authored Dec 7, 2023
1 parent 1e87919 commit d098e0b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 38 deletions.
16 changes: 16 additions & 0 deletions defold-rive/api/rive.script_api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 51 additions & 30 deletions defold-rive/src/comp_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "renderer.h"

// DMSDK
#include <dmsdk/sdk.h>
#include <dmsdk/dlib/log.h>
#include <dmsdk/dlib/math.h>
#include <dmsdk/dlib/object_pool.h>
Expand Down Expand Up @@ -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<rive::CustomProperty>())
{
if (!child->name().empty())
{
const char* key = child->name().c_str();
switch (child->coreType())
{
case rive::CustomPropertyBoolean::typeKey:
{
bool b = child->as<rive::CustomPropertyBoolean>()->propertyValue();
break;
}
case rive::CustomPropertyString::typeKey:
{
const char* s = child->as<rive::CustomPropertyString>()->propertyValue().c_str();
break;
}
case rive::CustomPropertyNumber::typeKey:
{
float f = child->as<rive::CustomPropertyNumber>()->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)
Expand Down Expand Up @@ -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<rive::CustomProperty>())
{
if (!child->name().empty())
{
const char* key = child->name().c_str();
switch (child->coreType())
{
case rive::CustomPropertyBoolean::typeKey:
{
bool b = child->as<rive::CustomPropertyBoolean>()->propertyValue();
lua_pushstring(L, key);
lua_pushboolean(L, b);
lua_settable(L, -3);
break;
}
case rive::CustomPropertyString::typeKey:
{
const char* s = child->as<rive::CustomPropertyString>()->propertyValue().c_str();
lua_pushstring(L, key);
lua_pushstring(L, s);
lua_settable(L, -3);
break;
}
case rive::CustomPropertyNumber::typeKey:
{
float f = child->as<rive::CustomPropertyNumber>()->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();
Expand Down
6 changes: 5 additions & 1 deletion defold-rive/src/comp_rive.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define DM_GAMESYS_COMP_RIVE_H

#include <stdint.h>
#include <dmsdk/sdk.h>
#include <dmsdk/dlib/hash.h>
#include <dmsdk/dlib/vmath.h>
#include <dmsdk/dlib/transform.h>
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions defold-rive/src/script_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions main/fighting-game/fighting-game.script
Original file line number Diff line number Diff line change
@@ -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)


Expand Down

0 comments on commit d098e0b

Please sign in to comment.