Skip to content
Open
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
2 changes: 1 addition & 1 deletion defold-rive/ext.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: RiveExt
platforms:
common:
context:
defines: []
defines: ['WITH_RIVE_SCRIPTING', 'RIVE_USE_VANILLA_LUA', 'RIVE_USE_LUA_WRAPPER']
symbols: ["ResourceTypeRiveScene", "ResourceTypeRiveModel", "ResourceTypeRiveData", "ComponentTypeRive"]

linux:
Expand Down
66 changes: 50 additions & 16 deletions defold-rive/include/rive/lua/rive_lua_libs.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#ifdef WITH_RIVE_SCRIPTING
#ifndef _RIVE_LUA_LIBS_HPP_
#define _RIVE_LUA_LIBS_HPP_
#include "lua.h"
#include "lualib.h"
#if defined(RIVE_USE_LUA_WRAPPER) // DEFOLD
#include "rive_lua_wrapper.h"
#else
#include "lua.h"
#include "lualib.h"
#endif
#include "rive/math/raw_path.hpp"
#include "rive/renderer.hpp"
#include "rive/math/vec2d.hpp"
#include "rive/shapes/paint/image_sampler.hpp"
#include "rive/viewmodel/viewmodel_instance_color.hpp"
#include "rive/viewmodel/viewmodel_instance_value.hpp"
#include "rive/viewmodel/viewmodel_instance_viewmodel.hpp"
#include "rive/viewmodel/viewmodel_instance_number.hpp"
#include "rive/viewmodel/viewmodel_instance_string.hpp"
#include "rive/viewmodel/viewmodel_instance_trigger.hpp"
#include "rive/viewmodel/viewmodel_instance_list.hpp"
#include "rive/viewmodel/viewmodel.hpp"
Expand All @@ -21,6 +27,8 @@

namespace rive
{
#if !defined(RIVE_USE_VANILLA_LUA) // DEFOLD

class Factory;
enum class LuaAtoms : int16_t
{
Expand Down Expand Up @@ -314,6 +322,8 @@ class ScriptedRenderer
uint32_t m_saveCount = 0;
};

#endif // !RIVE_USE_VANILLA_LUA

class ScriptedArtboard
{
public:
Expand Down Expand Up @@ -359,7 +369,9 @@ struct ScriptedListener
class ScriptedProperty : public ViewModelInstanceValueDelegate
{
public:
ScriptedProperty(lua_State* L, rcp<ViewModelInstanceValue> value);
ScriptedProperty(lua_State* L,
rcp<File> file,
rcp<ViewModelInstanceValue> value);
virtual ~ScriptedProperty();
int addListener();
int removeListener();
Expand All @@ -376,13 +388,15 @@ class ScriptedProperty : public ViewModelInstanceValueDelegate

protected:
lua_State* m_state;
rcp<File> m_file;
rcp<ViewModelInstanceValue> m_instanceValue;
};

class ScriptedViewModel
{
public:
ScriptedViewModel(lua_State* L,
rcp<File> file,
rcp<ViewModel> viewModel,
rcp<ViewModelInstance> viewModelInstance);
~ScriptedViewModel();
Expand All @@ -395,6 +409,7 @@ class ScriptedViewModel

private:
lua_State* m_state;
rcp<File> m_file;
rcp<ViewModel> m_viewModel;
rcp<ViewModelInstance> m_viewModelInstance;
std::unordered_map<std::string, int> m_propertyRefs;
Expand All @@ -404,6 +419,7 @@ class ScriptedPropertyViewModel : public ScriptedProperty
{
public:
ScriptedPropertyViewModel(lua_State* L,
rcp<File> file,
rcp<ViewModel> viewModel,
rcp<ViewModelInstanceViewModel> value);
~ScriptedPropertyViewModel();
Expand All @@ -420,7 +436,7 @@ class ScriptedPropertyViewModel : public ScriptedProperty
class ScriptedPropertyNumber : public ScriptedProperty
{
public:
ScriptedPropertyNumber(lua_State* L, rcp<ViewModelInstanceNumber> value);
ScriptedPropertyNumber(lua_State* L, rcp<File> file, rcp<ViewModelInstanceNumber> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 13;
static constexpr const char* luaName = "Property<number>";
static constexpr bool hasMetatable = true;
Expand All @@ -432,7 +448,7 @@ class ScriptedPropertyNumber : public ScriptedProperty
class ScriptedPropertyTrigger : public ScriptedProperty
{
public:
ScriptedPropertyTrigger(lua_State* L, rcp<ViewModelInstanceTrigger> value);
ScriptedPropertyTrigger(lua_State* L, rcp<File> file, rcp<ViewModelInstanceTrigger> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 14;
static constexpr const char* luaName = "PropertyTrigger";
static constexpr bool hasMetatable = true;
Expand All @@ -441,7 +457,7 @@ class ScriptedPropertyTrigger : public ScriptedProperty
class ScriptedPropertyList : public ScriptedProperty
{
public:
ScriptedPropertyList(lua_State* L, rcp<ViewModelInstanceList> value);
ScriptedPropertyList(lua_State* L, rcp<File> file, rcp<ViewModelInstanceList> value);
~ScriptedPropertyList();
static constexpr uint8_t luaTag = LUA_T_COUNT + 15;
static constexpr const char* luaName = "PropertyList";
Expand All @@ -456,25 +472,41 @@ class ScriptedPropertyList : public ScriptedProperty
std::unordered_map<ViewModelInstance*, int> m_propertyRefs;
};

class ScriptedPropertyColor : public ScriptedProperty
{
public:
ScriptedPropertyColor(lua_State* L, rcp<File> file, rcp<ViewModelInstanceColor> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 16;
static constexpr const char* luaName = "PropertyColor";
static constexpr bool hasMetatable = true;

int pushValue();
void setValue(unsigned value);
};

class ScriptedPropertyString : public ScriptedProperty
{
public:
ScriptedPropertyString(lua_State* L, rcp<File> file, rcp<ViewModelInstanceString> value);
static constexpr uint8_t luaTag = LUA_T_COUNT + 17;
static constexpr const char* luaName = "PropertyString";
static constexpr bool hasMetatable = true;

int pushValue();
void setValue(const std::string& value);
};

// Make
// ScriptedPropertyViewModel
// - Nullable ViewModelInstanceValue (ViewModelInstanceViewModel)
// - Requires ViewModel to know which properties to expect
// ScriptedPropertyEnum
// - Nullable ViewModelInstanceValue (ViewModelInstanceEnum)
// - Requires DataEnum for expected types
// ScriptedPropertyNumber
// - Nullable ViewModelInstanceValue (ViewModelInstanceNumber)
// ScriptedPropertyString
// - Nullable ViewModelInstanceValue (ViewModelInstanceString)
// ScriptedPropertyTrigger
// - Nullable ViewModelInstanceValue (ViewModelInstanceTrigger)
// ScriptedPropertyArtboard
// - Nullable ViewModelInstanceValue (ViewModelInstanceArtboard)
// ScriptedPropertyColor
// - Nullable ViewModelInstanceValue (ViewModelInstanceColor)
// ScriptedPropertyList
// - Nullable ViewModelInstanceValue (ViewModelInstanceList)

#if !defined(RIVE_USE_VANILLA_LUA)

// Make renderer: return lua_newrive<ScriptedRenderer>(L, renderer);
template <class T, class... Args>
Expand Down Expand Up @@ -588,6 +620,8 @@ class ScriptingVM
ScriptingContext* m_context;
};

#endif // !RIVE_USE_VANILLA_LUA

} // namespace rive
#endif
#endif
81 changes: 81 additions & 0 deletions defold-rive/include/rive_lua_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2020 The Defold Foundation
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#ifndef DM_RIVE_LUA_WRAPPER_H
#define DM_RIVE_LUA_WRAPPER_H

extern "C" {
#include <dmsdk/lua/lua.h>
#include <dmsdk/lua/lauxlib.h>
}
#include <dmsdk/dlib/dstrings.h>

// Defold helpers to aid with using classes directly from the rive-runtime
#include <assert.h>
#include <utility>
static const uint32_t LUA_T_COUNT = 80; // after defold types

namespace dmRive
{
void* ToUserType(lua_State* L, int index);
}

inline
const char* GetMetaTableName(const char* name, char* buffer, uint32_t buffer_size)
{
dmSnPrintf(buffer, buffer_size, "%s_mt", name);
return buffer;
}

template <class T, class... Args>
static T* lua_newrive(lua_State* L, Args&&... args)
{
char mtname[256];
GetMetaTableName(T::luaName, mtname, sizeof(mtname));
printf("MAWE lua_newrive: %s / %s\n", T::luaName, mtname);

T* instance = (T*)lua_newuserdata(L, sizeof(T));
luaL_getmetatable(L, mtname);
lua_setmetatable(L, -2);
return new (instance) T(std::forward<Args>(args)...);
}

template <typename T>
static T* lua_torive(lua_State* L, int idx, bool allowNil = false)
{
//T* riveObject = (T*)lua_touserdatatagged(L, idx, T::luaTag);
T* riveObject = (T*)dmRive::ToUserType(L, idx);

if (!allowNil && riveObject == nullptr)
{
luaL_typeerror(L, idx, T::luaName);
return nullptr;
}
return riveObject;
}

inline
void lua_pushunsigned(lua_State* L, uint32_t value)
{
lua_pushinteger(L, value);
}

inline
void luaL_typeerrorL(lua_State* L, int index, const char* type)
{
luaL_error(L, "argument at index %d was not of type %s", index, type);
}


// end defold

#endif // DM_RIVE_LUA_WRAPPER_H
2 changes: 1 addition & 1 deletion defold-rive/src/comp_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ namespace dmRive

// RIVE UPDATE
dmRive::RiveSceneData* data = (dmRive::RiveSceneData*) component.m_Resource->m_Scene->m_Scene;
rive::File* f = data->m_File;
rive::File* f = data->m_File.get();
rive::Artboard* artboard = f->artboard();

if (!artboard)
Expand Down
15 changes: 9 additions & 6 deletions defold-rive/src/comp_rive_databinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ static rive::ViewModelInstanceRuntime* FromHandle(RiveComponent* component, uint
static rive::ViewModelRuntime* FindViewModelRuntimeByHash(RiveComponent* component, dmhash_t name_hash)
{
dmRive::RiveSceneData* data = (dmRive::RiveSceneData*) component->m_Resource->m_Scene->m_Scene;
rive::File* file = data->m_File;

size_t n = file->viewModelCount();
size_t n = data->m_File->viewModelCount();
for (size_t i = 0; i < n; ++i)
{
rive::ViewModelRuntime* vmr = file->viewModelByIndex(i);
rive::ViewModelRuntime* vmr = data->m_File->viewModelByIndex(i);
dmhash_t hash = dmHashString64(vmr->name().c_str());
if (hash == name_hash)
return vmr;
Expand All @@ -71,8 +70,6 @@ static rive::ViewModelRuntime* FindViewModelRuntimeByHash(RiveComponent* compone
static rive::ViewModelInstanceRuntime* CreateViewModelInstanceRuntimeByHash(RiveComponent* component, dmhash_t name_hash)
{
dmRive::RiveSceneData* data = component->m_Resource->m_Scene->m_Scene;
rive::File* file = data->m_File;

if (name_hash != 0)
{
rive::ViewModelRuntime* vmr = FindViewModelRuntimeByHash(component, name_hash);
Expand All @@ -83,7 +80,7 @@ static rive::ViewModelInstanceRuntime* CreateViewModelInstanceRuntimeByHash(Rive
if (!component->m_ArtboardInstance)
return 0;

rive::ViewModelRuntime* vmr = file->defaultArtboardViewModel(component->m_ArtboardInstance.get());
rive::ViewModelRuntime* vmr = data->m_File->defaultArtboardViewModel(component->m_ArtboardInstance.get());
if (vmr)
{
return vmr->createDefaultInstance();
Expand Down Expand Up @@ -187,6 +184,12 @@ uint32_t CompRiveCreateViewModelInstanceRuntime(RiveComponent* component, dmhash
return handle;
}

rive::ViewModelInstanceRuntime* CompRiveGetViewModelInstance(RiveComponent* component)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, component->m_CurrentViewModelInstanceRuntime);
return vmir;
}

bool CompRiveDestroyViewModelInstanceRuntime(RiveComponent* component, uint32_t handle)
{
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);
Expand Down
3 changes: 2 additions & 1 deletion defold-rive/src/comp_rive_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ namespace dmRive
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);
RiveSceneData* CompRiveGetRiveSceneData(RiveComponent* component);
rive::ViewModelInstanceRuntime* CompRiveGetViewModelInstance(RiveComponent* component);
}

#endif //DM_COMP_RIVE_PRIVATE_H
Loading
Loading