aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/je_lua_animator.cpp
blob: c8dd4205d4bfda2cd6ee9db4abcfd0e8c190e69c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "common/je_lua_object.h"
#include "common/je_lua_common.h"
#include "libjin/jin.h"

#include "je_lua_sprite.h"
#include "je_lua_canvas.h"
#include "je_lua_texture.h"
#include "je_lua_shader.h"
#include "je_lua_animator.h"
#include "je_lua_animation.h"

using namespace JinEngine::Math;
using namespace JinEngine::Graphics;
using namespace JinEngine::Graphics::Shaders;
using namespace JinEngine::Graphics::Animations;

namespace JinEngine
{
    namespace Lua
    {
        const char* Jin_Lua_Animator = "Animator";

        typedef Shared<Animator>& SharedAnimator;

        LUA_IMPLEMENT inline SharedAnimator checkAnimator(lua_State* L)
        {
            LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animator);
            return luaObj->getShared<Animator>();
        }

        LUA_IMPLEMENT int l_gc(lua_State* L)
        {
            LuaObject* p = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animation);
            p->release();
            return 0;
        }

        LUA_IMPLEMENT int l_update(lua_State* L)
        {
            SharedAnimator animator = checkAnimator(L);
            float dt = luax_checknumber(L, 2);
            animator->update(dt);
            return 0;
        }

        LUA_IMPLEMENT int l_play(lua_State* L)
        {
            SharedAnimator animator = checkAnimator(L);
            animator->play();
            return 0;
        }

        LUA_IMPLEMENT int l_pause(lua_State* L)
        {
            SharedAnimator animator = checkAnimator(L);
            animator->pause();
            return 0;
        }

        LUA_IMPLEMENT int l_resume(lua_State* L)
        {
            SharedAnimator animator = checkAnimator(L);
            animator->resume();
            return 0;
        }

        LUA_IMPLEMENT int l_rewind(lua_State* L)
        {
            SharedAnimator animator = checkAnimator(L);
            animator->rewind(); 
            return 0;
        }

        LUA_IMPLEMENT int l_render(lua_State* L)
        {
            SharedAnimator animator = checkAnimator(L);
            float x = luax_checknumber(L, 2);
            float y = luax_checknumber(L, 3);
            float sx = luax_checknumber(L, 4);
            float sy = luax_checknumber(L, 5);
            float r = luax_checknumber(L, 6);
            animator->render(x, y, sx, sy, r);
            return 0;
        }

        LUA_IMPLEMENT int l_setAnimation(lua_State* L)
        {
            LuaObject* luaAnimator = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animator);
            SharedAnimator shrAnimator = luaAnimator->getShared<Animator>();
            LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animation);
            Shared<Animation>& shrAnimation = luaObj->getShared<Animation>();
            luaAnimator->setDependency((int)AnimatorDependency::DEP_ANIMATION, &shrAnimation);
            shrAnimator->setAnimation(shrAnimation.getObject());
            return 0;
        }

        LUA_IMPLEMENT int l_forceToFrame(lua_State* L)
        {
            SharedAnimator shrAnimator = checkAnimator(L);
            int index = luax_checkinteger(L, 2);
            shrAnimator->forceToFrame(index);
            return 0;
        }

        LUA_IMPLEMENT int l_setSpeed(lua_State* L)
        {
            SharedAnimator shrAnimator = checkAnimator(L);
            float fps = luax_checknumber(L, 2);
            shrAnimator->setSpeed(fps);
            return 0;
        }

        LUA_IMPLEMENT int l_setDefaultSpeed(lua_State* L)
        {
            SharedAnimator shrAnimator = checkAnimator(L);
            shrAnimator->setDefaultSpeed();
            return 0;
        }

        LUA_IMPLEMENT int l_setLoop(lua_State* L)
        {
            SharedAnimator shrAnimator = checkAnimator(L);
            bool loop = luax_checkbool(L, 2);
            shrAnimator->setLoop(loop);
            return 0;
        }

        LUA_IMPLEMENT int l_setDefaultLoop(lua_State* L)
        {
            SharedAnimator shrAnimator = checkAnimator(L);
            shrAnimator->setDefaultLoop();
            return 0;
        }

        LUA_IMPLEMENT int l_getSpeed(lua_State* L)
        {
            SharedAnimator shrAnimator = checkAnimator(L);
            float speed = shrAnimator->getSpeed();
            luax_pushnumber(L, speed);
            return 1;
        }

        LUA_EXPORT void luaopen_Animator(lua_State* L)
        {
            luaL_Reg methods[] = {
                { "__gc",            l_gc              },
                { "update",          l_update          },
                { "play",            l_play            },
                { "pause",           l_pause           },
                { "resume",          l_resume          },
                { "rewind",          l_rewind          },
                { "render",          l_render          },
                { "setAnimation",    l_setAnimation    },
                { "forceToFrame",    l_forceToFrame    },
                { "setSpeed",        l_setSpeed        },
                { "setDefaultSpeed", l_setDefaultSpeed }, 
                { "setLoop",         l_setLoop         },
                { "setDefaultLoop",  l_setDefaultLoop  },
                { "getSpeed",        l_getSpeed        },
                { 0,                 0                 }
            };
            luax_newtype(L, Jin_Lua_Animator, methods);
        }

    } // namespace Lua
} // namespace JinEngine