LumixEngine/src/animation/property_animation.cpp

103 lines
2.7 KiB
C++
Raw Normal View History

2018-01-10 23:51:15 +01:00
#include "animation/property_animation.h"
#include "engine/crc32.h"
2019-06-20 16:49:34 +02:00
#include "engine/allocator.h"
2018-01-10 23:51:15 +01:00
#include "engine/log.h"
2020-12-25 18:11:09 +01:00
#include "engine/lua_wrapper.h"
2018-01-10 23:51:15 +01:00
#include "engine/reflection.h"
2020-12-25 18:11:09 +01:00
#include "engine/stream.h"
2018-01-10 23:51:15 +01:00
2020-12-25 18:11:09 +01:00
namespace Lumix {
2018-01-10 23:51:15 +01:00
2018-01-11 21:13:59 +01:00
const ResourceType PropertyAnimation::TYPE("property_animation");
PropertyAnimation::PropertyAnimation(const Path& path, ResourceManager& resource_manager, IAllocator& allocator)
2018-01-10 23:51:15 +01:00
: Resource(path, resource_manager, allocator)
2019-06-20 16:49:34 +02:00
, m_allocator(allocator)
2018-01-11 02:19:31 +01:00
, fps(30)
, curves(allocator)
2018-01-10 23:51:15 +01:00
{
}
2018-01-15 17:39:51 +01:00
PropertyAnimation::Curve& PropertyAnimation::addCurve()
{
2019-06-20 16:49:34 +02:00
return curves.emplace(m_allocator);
2018-01-15 17:39:51 +01:00
}
2020-12-25 18:11:09 +01:00
bool PropertyAnimation::save(OutputMemoryStream& blob) {
2018-01-24 00:37:44 +01:00
if (!isReady()) return false;
2020-12-25 18:11:09 +01:00
for (Curve& curve : curves) {
blob << "curve {\n";
blob << "\t component = \"" << reflection::getComponent(curve.cmp_type)->name << "\",\n";
blob << "\t property = \"" << curve.property->name << "\",\n";
blob << "\tkeyframes = {\n";
for (int i = 0; i < curve.frames.size(); ++i) {
if (i != 0) blob << ", ";
blob << curve.frames[i];
}
blob << "},\n";
blob << "\tvalues = {\n";
for (int i = 0; i < curve.values.size(); ++i) {
if (i != 0) blob << ", ";
blob << curve.values[i];
2018-01-24 00:37:44 +01:00
}
2020-12-25 18:11:09 +01:00
blob << "}\n}\n\n";
2018-01-24 00:37:44 +01:00
}
return true;
}
2020-12-25 18:11:09 +01:00
void PropertyAnimation::LUA_curve(lua_State* L) {
LuaWrapper::DebugGuard guard(L);
LuaWrapper::checkTableArg(L, 1);
const char* cmp_name;
const char* prop_name;
if (!LuaWrapper::checkField<const char*>(L, 1, "component", &cmp_name)) {
luaL_argerror(L, 1, "`component` field must be a string");
}
if (!LuaWrapper::checkField<const char*>(L, 1, "prop_name", &prop_name)) {
luaL_argerror(L, 1, "`property` field must be a string");
}
Curve& curve = curves.emplace(m_allocator);
curve.cmp_type = reflection::getComponentType(cmp_name);
// TODO property
if (!LuaWrapper::getField(L, 1, "keyframes")) {
luaL_argerror(L, 1, "`keyframes` field must be an array");
}
LuaWrapper::forEachArrayItem<i32>(L, -1, "`keyframes` field must be an array of keyframes", [&](i32 v){
curve.frames.emplace(v);
});
lua_pop(L, 1);
if (!LuaWrapper::getField(L, 1, "values")) {
luaL_argerror(L, 1, "`values` field must be an array");
}
LuaWrapper::forEachArrayItem<float>(L, -1, "`values` field must be an array of numbers", [&](float v){
curve.values.emplace(v);
});
lua_pop(L, 1);
}
2018-01-24 00:37:44 +01:00
2019-06-11 22:39:39 +02:00
bool PropertyAnimation::load(u64 size, const u8* mem)
2018-01-10 23:51:15 +01:00
{
2020-12-25 18:11:09 +01:00
lua_State* L = luaL_newstate(); // TODO reuse
auto fn = &LuaWrapper::wrapMethodClosure<&PropertyAnimation::LUA_curve>;
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, fn, 1);
lua_setglobal(L, "curve");
return LuaWrapper::execute(L, Span((const char*)mem, (u32)size), getPath().c_str(), 0);
2018-01-10 23:51:15 +01:00
}
void PropertyAnimation::unload()
{
2018-01-11 02:19:31 +01:00
curves.clear();
2018-01-10 23:51:15 +01:00
}
} // namespace Lumix