Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion defold-rive/api/rive.script_api
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,72 @@

- name: handle
type: integer
desc: The handle to the currently set ViewModelInstanceRuntime instance
desc: The handle to the ViewModelInstanceRuntime instance

- name: properties
type: table
desc: A table of properties, where each key is a Rive "path", and the values are mapped to the corresponding property value type.

- name: get_property
type: function
desc: Gets a property from the ViewModelInstanceRuntime instance

parameters:
- name: url
type: url
desc: The Rive model component

- name: handle
type: integer
desc: The handle to the ViewModelInstanceRuntime instance

- name: path
type: string
desc: The path to the property

return:
- name: result
type: string,number,vmath.vector4
desc: The value of the selected property. Raises error if property doesn't exist.

- name: list_add_instance
type: function
desc: Add a ViewModelInstanceRuntime instance to a list property

parameters:
- name: url
type: url
desc: The Rive model component

- name: handle
type: integer
desc: The handle to the ViewModelInstanceRuntime instance

- name: path
type: string
desc: The path to the list property

- name: instance_handle
type: integer
desc: The handle to the ViewModelInstanceRuntime instance to add to the list

- name: list_remove_instance
type: function
desc: Remove a ViewModelInstanceRuntime instance from a list property

parameters:
- name: url
type: url
desc: The Rive model component

- name: handle
type: integer
desc: The handle to the ViewModelInstanceRuntime instance

- name: path
type: string
desc: The path to the list property

- name: instance_handle
type: integer
desc: The handle to the ViewModelInstanceRuntime instance to add to the list
12 changes: 11 additions & 1 deletion defold-rive/src/comp_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ namespace dmRive
component->m_World = Matrix4::identity();
component->m_DoRender = 0;
component->m_RenderConstants = 0;
component->m_CurrentViewModelInstanceRuntime = 0;
component->m_CurrentViewModelInstanceRuntime = INVALID_HANDLE;
component->m_HandleCounter = 0;

InstantiateArtboard(component);
Expand Down Expand Up @@ -1101,6 +1101,11 @@ namespace dmRive
component->m_AnimationPlaybackRate = playback_rate;
component->m_StateMachineInstance = component->m_ArtboardInstance->stateMachineAt(state_machine_index);

if (component->m_CurrentViewModelInstanceRuntime != INVALID_HANDLE)
{
CompRiveSetViewModelInstanceRuntime(component, component->m_CurrentViewModelInstanceRuntime);
}

// update the list of current state machine inputs
GetStateMachineInputNames(component->m_StateMachineInstance.get(), component->m_StateMachineInputs);
return true;
Expand Down Expand Up @@ -1543,6 +1548,11 @@ namespace dmRive
g_RenderBeginParams.m_DoFinalBlit = value;
#endif
}

RiveSceneData* CompRiveGetRiveSceneData(RiveComponent* component)
{
return GetRiveResource(component, component->m_Resource);
}
}

DM_DECLARE_COMPONENT_TYPE(ComponentTypeRive, "rivemodelc", dmRive::ComponentTypeCreate, dmRive::ComponentTypeDestroy);
Expand Down
132 changes: 125 additions & 7 deletions defold-rive/src/comp_rive_databinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ namespace dmRive

#define CHECK_VMIR(VMIR, HANDLE) \
if (!(VMIR)) { \
dmLogError("No viewmodel runtime instance with handle '%u'", (HANDLE)); \
dmLogError("%s:%d: No viewmodel runtime instance with handle '%u'", __FUNCTION__, __LINE__, (HANDLE)); \
return false; \
}

#define CHECK_PROP_RESULT(PROP, TYPE, PATH) \
if (!(PROP)) { \
dmLogError("No property of type '%s', with path '%s'", (TYPE), (PATH)); \
dmLogError("%s:%d: No property of type '%s', with path '%s'", __FUNCTION__, __LINE__, (TYPE), (PATH)); \
return false; \
}

Expand Down Expand Up @@ -79,13 +79,14 @@ static rive::ViewModelInstanceRuntime* CreateViewModelInstanceRuntimeByHash(Rive

void SetViewModelInstanceRuntime(RiveComponent* component, rive::ViewModelInstanceRuntime* vmir)
{
if (component->m_StateMachineInstance)
if (component->m_ArtboardInstance)
{
component->m_StateMachineInstance->bindViewModelInstance(vmir->instance());
component->m_ArtboardInstance->bindViewModelInstance(vmir->instance());
}
else

if (component->m_StateMachineInstance)
{
component->m_ArtboardInstance->bindViewModelInstance(vmir->instance());
component->m_StateMachineInstance->bindViewModelInstance(vmir->instance());
}
}

Expand Down Expand Up @@ -118,6 +119,21 @@ void DebugModelViews(RiveComponent* component)
}
}

void DebugVMIR(rive::ViewModelInstanceRuntime* vmir)
{
printf("NAME: '%s'\n", vmir->name().c_str());

size_t num_properties = vmir->propertyCount();
std::vector<rive::PropertyData> pdatas = vmir->properties();
for (size_t j = 0; j < num_properties; ++j)
{
rive::PropertyData& property = pdatas[j];
printf(" DATA: %d '%s'\n", (int)property.type, property.name.c_str());
}

printf(" instance: '%s' %p\n", vmir->instance()->name().c_str(), vmir->instance().get());
}

// Scripting api + helpers

bool CompRiveSetViewModelInstanceRuntime(RiveComponent* component, uint32_t handle)
Expand Down Expand Up @@ -252,7 +268,6 @@ bool CompRiveRuntimeSetPropertyEnum(RiveComponent* component, uint32_t handle, c

prop->value(value);
return true;

}

bool CompRiveRuntimeSetPropertyTrigger(RiveComponent* component, uint32_t handle, const char* path)
Expand All @@ -265,9 +280,112 @@ bool CompRiveRuntimeSetPropertyTrigger(RiveComponent* component, uint32_t handle

prop->trigger();
return true;
}

bool CompRiveRuntimeSetPropertyImage(RiveComponent* component, uint32_t handle, const char* path, rive::RenderImage* image)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceAssetImageRuntime* prop = vmir->propertyImage(path);
CHECK_PROP_RESULT(prop, "image", path);

prop->value(image);
return true;
}

bool CompRiveRuntimeGetPropertyBool(RiveComponent* component, uint32_t handle, const char* path, bool* value)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceBooleanRuntime* prop = vmir->propertyBoolean(path);
CHECK_PROP_RESULT(prop, "boolean", path);
*value = prop->value();
return true;
}

bool CompRiveRuntimeGetPropertyF32(RiveComponent* component, uint32_t handle, const char* path, float* value)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceNumberRuntime* prop = vmir->propertyNumber(path);
CHECK_PROP_RESULT(prop, "number", path);
*value = prop->value();
return true;
}

bool CompRiveRuntimeGetPropertyColor(RiveComponent* component, uint32_t handle, const char* path, dmVMath::Vector4* color)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceColorRuntime* prop = vmir->propertyColor(path);
CHECK_PROP_RESULT(prop, "color", path);

uint32_t argb = (uint32_t)prop->value();
float a = (argb >> 24) % 0xFF;
float r = (argb >> 16) % 0xFF;
float g = (argb >> 8) % 0xFF;
float b = (argb >> 0) % 0xFF;
*color = dmVMath::Vector4(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
return true;
}

bool CompRiveRuntimeGetPropertyString(RiveComponent* component, uint32_t handle, const char* path, const char** value)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceStringRuntime* prop = vmir->propertyString(path);
CHECK_PROP_RESULT(prop, "string", path);

*value = prop->value().c_str(); // the temp string is not for storage
return true;
}

bool CompRiveRuntimeGetPropertyEnum(RiveComponent* component, uint32_t handle, const char* path, const char** value)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceEnumRuntime* prop = vmir->propertyEnum(path);
CHECK_PROP_RESULT(prop, "enum", path);

*value = prop->value().c_str(); // the temp string is not for storage
return true;
}

// LISTS

bool CompRiveRuntimeListAddInstance(RiveComponent* component, uint32_t handle, const char* path, uint32_t instance)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceListRuntime* prop = vmir->propertyList(path);
CHECK_PROP_RESULT(prop, "list", path);

rive::ViewModelInstanceRuntime* instance_vmir = FromHandle(component, instance);
prop->addInstance(instance_vmir);
return true;
}

bool CompRiveRuntimeListRemoveInstance(RiveComponent* component, uint32_t handle, const char* path, uint32_t instance)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
CHECK_VMIR(vmir, handle);

rive::ViewModelInstanceListRuntime* prop = vmir->propertyList(path);
CHECK_PROP_RESULT(prop, "list", path);

rive::ViewModelInstanceRuntime* instance_vmir = FromHandle(component, instance);
prop->removeInstance(instance_vmir);
return true;
}


} // namespace

#endif // DM_RIVE_UNSUPPORTED
16 changes: 15 additions & 1 deletion defold-rive/src/comp_rive_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace rive
class StateMachineInstance;
class ViewModelInstance;
class ViewModelInstanceRuntime;
class RenderImage;
enum class DataType : unsigned int;
}

Expand Down Expand Up @@ -126,14 +127,27 @@ namespace dmRive
bool CompRiveDestroyViewModelInstanceRuntime(RiveComponent* component, uint32_t handle);
bool CompRiveSetViewModelInstanceRuntime(RiveComponent* component, uint32_t handle);
uint32_t CompRiveGetViewModelInstanceRuntime(RiveComponent* component);

bool GetPropertyDataType(RiveComponent* component, uint32_t handle, const char* path, rive::DataType* type);

bool CompRiveRuntimeSetPropertyBool(RiveComponent* component, uint32_t handle, const char* path, bool value);
bool CompRiveRuntimeSetPropertyF32(RiveComponent* component, uint32_t handle, const char* path, float value);
bool CompRiveRuntimeSetPropertyColor(RiveComponent* component, uint32_t handle, const char* path, dmVMath::Vector4* color);
bool CompRiveRuntimeSetPropertyString(RiveComponent* component, uint32_t handle, const char* path, const char* value);
bool CompRiveRuntimeSetPropertyEnum(RiveComponent* component, uint32_t handle, const char* path, const char* value);
bool CompRiveRuntimeSetPropertyTrigger(RiveComponent* component, uint32_t handle, const char* path);
bool CompRiveRuntimeSetPropertyImage(RiveComponent* component, uint32_t handle, const char* path, rive::RenderImage* image);

bool GetPropertyDataType(RiveComponent* component, uint32_t handle, const char* path, rive::DataType* type);
bool CompRiveRuntimeGetPropertyBool(RiveComponent* component, uint32_t handle, const char* path, bool* value);
bool CompRiveRuntimeGetPropertyF32(RiveComponent* component, uint32_t handle, const char* path, float* value);
bool CompRiveRuntimeGetPropertyColor(RiveComponent* component, uint32_t handle, const char* path, dmVMath::Vector4* color);
bool CompRiveRuntimeGetPropertyString(RiveComponent* component, uint32_t handle, const char* path, const char** value);
bool CompRiveRuntimeGetPropertyEnum(RiveComponent* component, uint32_t handle, const char* path, const char** value);

bool CompRiveRuntimeListAddInstance(RiveComponent* component, uint32_t handle, const char* path, uint32_t instance);
bool CompRiveRuntimeListRemoveInstance(RiveComponent* component, uint32_t handle, const char* path, uint32_t instance);

RiveSceneData* CompRiveGetRiveSceneData(RiveComponent* component);
}

#endif //DM_COMP_RIVE_PRIVATE_H
6 changes: 6 additions & 0 deletions defold-rive/src/res_rive_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ namespace dmRive
dmLogError("We currently don't support swapping the asset type of '%s'", asset_name);
return dmResource::RESULT_NOT_SUPPORTED;
}

rive::RenderImage* ResRiveDataCreateRenderImage(dmResource::HFactory factory, RiveSceneData* resource, uint8_t* data, uint32_t data_length)
{
rive::rcp<rive::RenderImage> image = dmRive::LoadImageFromMemory(resource->m_RiveRenderContext, data, data_length);
return image.release();
}
}


Expand Down
5 changes: 3 additions & 2 deletions defold-rive/src/res_rive_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ namespace dmRive
dmArray<rive::FileAsset*> m_FileAssets; // For runtime swapping
};

dmResource::Result ResRiveDataSetAssetFromMemory(RiveSceneData* resource, const char* asset_name, void* payload, uint32_t payload_size);
dmResource::Result ResRiveDataSetAsset(dmResource::HFactory factory, RiveSceneData* resource, const char* asset_name, const char* path);
dmResource::Result ResRiveDataSetAssetFromMemory(RiveSceneData* resource, const char* asset_name, void* payload, uint32_t payload_size);
dmResource::Result ResRiveDataSetAsset(dmResource::HFactory factory, RiveSceneData* resource, const char* asset_name, const char* path);
rive::RenderImage* ResRiveDataCreateRenderImage(dmResource::HFactory factory, RiveSceneData* resource, uint8_t* data, uint32_t data_length);
}

#endif // DM_RES_RIVE_DATA_H
4 changes: 2 additions & 2 deletions defold-rive/src/script_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,12 @@ namespace dmRive
{0, 0}
};

extern void ScriptInitializeDataBinding(lua_State* L);
extern void ScriptInitializeDataBinding(lua_State* L, dmResource::HFactory factory);

void ScriptRegister(lua_State* L, dmResource::HFactory factory)
{
luaL_register(L, "rive", RIVE_FUNCTIONS);
ScriptInitializeDataBinding(L);
ScriptInitializeDataBinding(L, factory);
lua_pop(L, 1);

g_Factory = factory;
Expand Down
Loading
Loading