blob: d064f9fe11fde9042c61ac160ca0137cf0bf4731 [file]
#ifdef WITH_RIVE_SCRIPTING
#include "rive/lua/rive_lua_libs.hpp"
#include <math.h>
#include <stdio.h>
using namespace rive;
ScriptedDataContext::ScriptedDataContext(lua_State* L,
rcp<DataContext> dataContext) :
m_state(L), m_dataContext(dataContext)
{}
int ScriptedDataContext::pushParent()
{
auto parent = m_dataContext->parent();
if (parent)
{
if (m_parentCache.push(m_state, parent))
{
return 1;
}
lua_newrive<ScriptedDataContext>(m_state, m_state, parent);
m_parentCache.store(m_state, parent);
}
else
{
lua_pushnil(m_state);
}
return 1;
}
int ScriptedDataContext::pushViewModel()
{
auto vmi = m_dataContext->mainViewModelInstance();
if (vmi)
{
if (m_viewModelCache.push(m_state, vmi))
{
return 1;
}
lua_newrive<ScriptedViewModel>(m_state,
m_state,
ref_rcp(vmi->viewModel()),
vmi);
m_viewModelCache.store(m_state, vmi);
}
else
{
lua_pushnil(m_state);
}
return 1;
}
static int data_value_namecall(lua_State* L)
{
int atom;
const char* str = lua_namecallatom(L, &atom);
if (str != nullptr)
{
auto dataContext = lua_torive<ScriptedDataContext>(L, 1);
switch (atom)
{
case (int)LuaAtoms::parent:
{
assert(dataContext->state() == L);
return dataContext->pushParent();
}
case (int)LuaAtoms::viewModel:
{
assert(dataContext->state() == L);
return dataContext->pushViewModel();
}
default:
break;
}
}
luaL_error(L,
"%s is not a valid method of %s",
str,
ScriptedDataContext::luaName);
return 0;
}
int luaopen_rive_data_context(lua_State* L)
{
{
lua_register_rive<ScriptedDataContext>(L);
lua_pushcfunction(L, data_value_namecall, nullptr);
lua_setfield(L, -2, "__namecall");
lua_setreadonly(L, -1, true);
lua_pop(L, 1); // pop the metatable
}
return 1;
}
#endif