From 2621fb7c5fccbe99a8e0430894967b10a619b000 Mon Sep 17 00:00:00 2001 From: emb <> Date: Tue, 17 Nov 2015 21:57:52 -0600 Subject: [PATCH] Support for image animations. --- .../Source/Graphics/Component/Component.cpp | 24 ++++++-- .../Source/Graphics/Component/ComponentData.h | 35 +++++++++--- RetroFE/Source/Lua/LuaImage.cpp | 56 +++++++++++++++++++ RetroFE/Source/Lua/LuaImage.h | 2 + RetroFE/Source/RetroFE.cpp | 53 +----------------- 5 files changed, 105 insertions(+), 65 deletions(-) diff --git a/RetroFE/Source/Graphics/Component/Component.cpp b/RetroFE/Source/Graphics/Component/Component.cpp index 5f85bd1..30b0e15 100644 --- a/RetroFE/Source/Graphics/Component/Component.cpp +++ b/RetroFE/Source/Graphics/Component/Component.cpp @@ -75,12 +75,24 @@ void Component::update(float dt) } if(start) { - info.x = (int)linear(elapsedTime, endInfo->duration, startInfo->x, endInfo->x - startInfo->x); - info.y = (int)linear(elapsedTime, endInfo->duration, startInfo->y, endInfo->y - startInfo->y); - info.alpha = (float)linear(elapsedTime, endInfo->duration, startInfo->alpha, endInfo->alpha - startInfo->alpha); - info.rotate = (float)linear(elapsedTime, endInfo->duration, startInfo->rotate, endInfo->rotate - startInfo->rotate); - info.width = (int)linear(elapsedTime, endInfo->duration, startInfo->width, endInfo->width - startInfo->width); - info.height = (int)linear(elapsedTime, endInfo->height, startInfo->height, endInfo->height - startInfo->height); + if(endInfo->isMaskSet(COMPONENT_DATA_X_MASK)) { + info.x = (int)linear(elapsedTime, endInfo->duration, startInfo->x, endInfo->x - startInfo->x); + } + if(endInfo->isMaskSet(COMPONENT_DATA_Y_MASK)) { + info.y = (int)linear(elapsedTime, endInfo->duration, startInfo->y, endInfo->y - startInfo->y); + } + if(endInfo->isMaskSet(COMPONENT_DATA_ALPHA_MASK)) { + info.alpha = (float)linear(elapsedTime, endInfo->duration, startInfo->alpha, endInfo->alpha - startInfo->alpha); + } + if(endInfo->isMaskSet(COMPONENT_DATA_ROTATE_MASK)) { + info.rotate = (float)linear(elapsedTime, endInfo->duration, startInfo->rotate, endInfo->rotate - startInfo->rotate); + } + if(endInfo->isMaskSet(COMPONENT_DATA_WIDTH_MASK)) { + info.width = (int)linear(elapsedTime, endInfo->duration, startInfo->width, endInfo->width - startInfo->width); + } + if(endInfo->isMaskSet(COMPONENT_DATA_HEIGHT_MASK)) { + info.height = (int)linear(elapsedTime, endInfo->duration, startInfo->height, endInfo->height - startInfo->height); + } if(!loop && done) { start = false; diff --git a/RetroFE/Source/Graphics/Component/ComponentData.h b/RetroFE/Source/Graphics/Component/ComponentData.h index b085979..02627d0 100644 --- a/RetroFE/Source/Graphics/Component/ComponentData.h +++ b/RetroFE/Source/Graphics/Component/ComponentData.h @@ -1,24 +1,43 @@ #pragma once + +#define COMPONENT_DATA_X_MASK 0x1 +#define COMPONENT_DATA_Y_MASK 0x2 +#define COMPONENT_DATA_WIDTH_MASK 0x4 +#define COMPONENT_DATA_HEIGHT_MASK 0x8 +#define COMPONENT_DATA_ROTATE_MASK 0x10 +#define COMPONENT_DATA_ALPHA_MASK 0x10 +#define COMPONENT_DATA_DURATION_MASK 0x20 +#define COMPONENT_DATA_LAYER_MASK 0x20 +#define COMPONENT_DATA_ALL_MASK 0xFF + class ComponentData { public: ComponentData() - : width(0) - , height(0) - , x(0) + : x(0) , y(0) + , width(0) + , height(0) + , rotate(0) , alpha(1) , duration(0) , layer(0) + , mask(COMPONENT_DATA_ALL_MASK) { } - - int width; - int height; +public: int x; int y; + int width; + int height; float rotate; float alpha; - float duration; + float duration; int layer; -}; \ No newline at end of file + void setMask(unsigned char flag) { mask |= flag; } + void clearMask(unsigned char flag) { mask &= ~flag; } + bool isMaskSet(unsigned char flag) { return ((mask & flag) != 0); } + +private: + unsigned char mask; +}; diff --git a/RetroFE/Source/Lua/LuaImage.cpp b/RetroFE/Source/Lua/LuaImage.cpp index d3154f6..908ea8c 100644 --- a/RetroFE/Source/Lua/LuaImage.cpp +++ b/RetroFE/Source/Lua/LuaImage.cpp @@ -295,3 +295,59 @@ int LuaImage::setLayer(lua_State *l) return 0; } + +int LuaImage::animate(lua_State *l) +{ + Image *i = (Image *)lua_tointeger(l, 1); + bool loop = (lua_toboolean(l, 2) != 0); + + i->animate(loop); + return 0; +} + +int LuaImage::addAnimation(lua_State *l) +{ + Image *i = (Image *)lua_tointeger(l, 1); + if(!i) return 0; + + float duration = (float)lua_tonumber(l, 2); + + if (!lua_istable(l, 3)) { + return 0; + } + + ComponentData newInfo; + newInfo.clearMask(COMPONENT_DATA_ALL_MASK); + + lua_pushstring (l, "x"); + lua_gettable(l, -2); + if(!lua_isnil(l, -1)) + { + newInfo.setMask(COMPONENT_DATA_X_MASK); + newInfo.x = (int)luaL_optinteger(l, -1, i->info.x); + } + lua_pop(l, 1); + + lua_pushstring (l, "y"); + lua_gettable(l, -2); + if(!lua_isnil(l, -1)) + { + newInfo.setMask(COMPONENT_DATA_Y_MASK); + newInfo.y = (int)luaL_optinteger(l, -1, i->info.y); + } + lua_pop(l, 1); + + lua_pushstring (l, "alpha"); + lua_gettable(l, -2); + if(!lua_isnil(l, -1)) + { + newInfo.setMask(COMPONENT_DATA_ALPHA_MASK); + newInfo.alpha = (float)luaL_optnumber(l, -1, i->info.alpha); + } + lua_pop(l, 1); + + newInfo.duration = duration; + i->addAnimation(newInfo); + + return 0; +} diff --git a/RetroFE/Source/Lua/LuaImage.h b/RetroFE/Source/Lua/LuaImage.h index 3982ca2..162850f 100644 --- a/RetroFE/Source/Lua/LuaImage.h +++ b/RetroFE/Source/Lua/LuaImage.h @@ -35,4 +35,6 @@ namespace LuaImage int setAlpha(lua_State *l); int setLayer(lua_State *l); int setHeight(lua_State *l); + int animate(lua_State *l); + int addAnimation(lua_State *l); }; diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index 9d90ab8..78cd3d0 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -47,50 +47,6 @@ static int lua_registerOnInit(lua_State *l) // events.registerOnInit(function); return 0; } -#if 0 -static int lua_imageAnimate(lua_State *l) -{ - Image *i = (Image *)lua_tointeger(l, 1); - bool loop = (lua_toboolean(l, 2) != 0); - - i->animate(loop); - return 0; -} - -static int lua_imageAddAnimation(lua_State *l) -{ -// int argc = lua_gettop(l); - Image *i = (Image *)lua_tointeger(l, 1); - if(!i) return 0; - - float duration = (float)lua_tonumber(l, 2); - - if (!lua_istable(l, 3)) { - return 0; - } - - ComponentData newInfo; - lua_pushstring (l, "x"); - lua_gettable(l, -2); - newInfo.x = (int)luaL_optinteger(l, -1, i->info.x); - lua_pop(l, 1); - - lua_pushstring (l, "y"); - lua_gettable(l, -2); - newInfo.y = (int)luaL_optinteger(l, -1, i->info.y); - lua_pop(l, 1); - - lua_pushstring (l, "alpha"); - lua_gettable(l, -2); - newInfo.alpha = (float)luaL_optnumber(l, -1, i->info.alpha); - lua_pop(l, 1); - - newInfo.duration = duration; - i->addAnimation(newInfo); - - return 0; -} -#endif const luaL_Reg RetroFE::luaImageFuncs[] = { // Creation @@ -115,15 +71,10 @@ const luaL_Reg RetroFE::luaImageFuncs[] = { {"setDimensions", LuaImage::setDimensions}, {"setRotate", LuaImage::setRotate}, {"setAlpha", LuaImage::setAlpha}, + {"addAnimation", LuaImage::addAnimation}, + {"animate", LuaImage::animate}, #if 0 {"delete", lua_imageDelete}, - {"delete", lua_imageDelete}, - {"setSize", lua_imageSetSize}, - {"setRotate", lua_imageSetRotate}, - {"setPosition", lua_imageSetPosition}, - {"setAlpha", lua_imageSetAlpha}, - {"addAnimation", lua_imageAddAnimation}, - {"animate", lua_imageAnimate}, #endif {NULL, NULL} };