Przeglądaj źródła

Merge branch 'next' of https://github.com/blackberry/GamePlay into next

sgrenier 11 lat temu
rodzic
commit
8f2fb9ea48

+ 110 - 4
gameplay/src/AnimationClip.cpp

@@ -56,7 +56,7 @@ AnimationClip::~AnimationClip()
         {
             ListenerEvent* lEvt = **_listenerItr;
             SAFE_DELETE(lEvt);
-            ++*_listenerItr;
+            ++(*_listenerItr);
         }
         SAFE_DELETE(_listeners);
     }
@@ -309,9 +309,11 @@ void AnimationClip::addListener(AnimationClip::Listener* listener, unsigned long
                 {
                     float currentTime = fmodf(_elapsedTime, (float)_duration);
                     GP_ASSERT(**_listenerItr || *_listenerItr == _listeners->end());
-                    if ((_speed >= 0.0f && currentTime < eventTime && (*_listenerItr == _listeners->end() || eventTime < (**_listenerItr)->_eventTime)) || 
+                    if ((_speed >= 0.0f && currentTime < eventTime && (*_listenerItr == _listeners->end() || eventTime < (**_listenerItr)->_eventTime)) ||
                         (_speed <= 0 && currentTime > eventTime && (*_listenerItr == _listeners->begin() || eventTime > (**_listenerItr)->_eventTime)))
+                    {
                         *_listenerItr = itr;
+                    }
                 }
                 return;
             }
@@ -320,6 +322,32 @@ void AnimationClip::addListener(AnimationClip::Listener* listener, unsigned long
     }
 }
 
+void AnimationClip::removeListener(AnimationClip::Listener* listener, unsigned long eventTime)
+{
+    if (_listeners)
+    {
+        GP_ASSERT(listener);
+        std::list<ListenerEvent*>::iterator iter = std::find_if(_listeners->begin(), _listeners->end(), [&](ListenerEvent* lst){ return lst->_eventTime == eventTime && lst->_listener == listener; });
+        if (iter != _listeners->end())
+        {
+            if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
+            {
+                float currentTime = fmodf(_elapsedTime, (float)_duration);
+                GP_ASSERT(**_listenerItr || *_listenerItr == _listeners->end());
+
+                // We the listener has not been triggered yet, then check if it is next to be triggered, remove it, and update the iterator
+                if (((_speed >= 0.0f && currentTime < eventTime) || (_speed <= 0 && currentTime > eventTime)) &&
+                    *iter == **_listenerItr)
+                {
+                    *_listenerItr = _listeners->erase(iter);
+                    return;
+                }
+            }
+            _listeners->erase(iter);
+        }
+    }
+}
+
 void AnimationClip::addBeginListener(AnimationClip::Listener* listener)
 {
     if (!_beginListeners)
@@ -329,6 +357,19 @@ void AnimationClip::addBeginListener(AnimationClip::Listener* listener)
     _beginListeners->push_back(listener);
 }
 
+void AnimationClip::removeBeginListener(AnimationClip::Listener* listener)
+{
+    if (_beginListeners)
+    {
+        GP_ASSERT(listener);
+        std::vector<Listener*>::iterator iter = std::find(_beginListeners->begin(), _beginListeners->end(), listener);
+        if (iter != _beginListeners->end())
+        {
+            _beginListeners->erase(iter);
+        }
+    }
+}
+
 void AnimationClip::addEndListener(AnimationClip::Listener* listener)
 {
     if (!_endListeners)
@@ -338,6 +379,19 @@ void AnimationClip::addEndListener(AnimationClip::Listener* listener)
     _endListeners->push_back(listener);
 }
 
+void AnimationClip::removeEndListener(AnimationClip::Listener* listener)
+{
+    if (_endListeners)
+    {
+        GP_ASSERT(listener);
+        std::vector<Listener*>::iterator iter = std::find(_endListeners->begin(), _endListeners->end(), listener);
+        if (iter != _endListeners->end())
+        {
+            _endListeners->erase(iter);
+        }
+    }
+}
+
 void AnimationClip::addBeginListener(const char* function)
 {
     if (!_scriptListeners)
@@ -348,6 +402,23 @@ void AnimationClip::addBeginListener(const char* function)
     addBeginListener(listener);
 }
 
+void AnimationClip::removeBeginListener(const char* function)
+{
+    if (_scriptListeners)
+    {
+        std::string functionRef = Game::getInstance()->getScriptController()->loadUrl(function);
+        std::vector<ScriptListener*>::iterator iter = std::find_if(_scriptListeners->begin(), _scriptListeners->end(), [&](ScriptListener* listener){ return listener->function == functionRef; });
+        if (iter != _scriptListeners->end())
+        {
+            ScriptListener* listener = *iter;
+
+            removeBeginListener(listener);
+            _scriptListeners->erase(iter);
+            SAFE_DELETE(listener);
+        }
+    }
+}
+
 void AnimationClip::addEndListener(const char* function)
 {
     if (!_scriptListeners)
@@ -358,6 +429,23 @@ void AnimationClip::addEndListener(const char* function)
     addEndListener(listener);
 }
 
+void AnimationClip::removeEndListener(const char* function)
+{
+    if (_scriptListeners)
+    {
+        std::string functionRef = Game::getInstance()->getScriptController()->loadUrl(function);
+        std::vector<ScriptListener*>::iterator iter = std::find_if(_scriptListeners->begin(), _scriptListeners->end(), [&](ScriptListener* listener){ return listener->function == functionRef; });
+        if (iter != _scriptListeners->end())
+        {
+            ScriptListener* listener = *iter;
+
+            removeEndListener(listener);
+            _scriptListeners->erase(iter);
+            SAFE_DELETE(listener);
+        }
+    }
+}
+
 void AnimationClip::addListener(const char* function, unsigned long eventTime)
 {
     if (!_scriptListeners)
@@ -368,6 +456,24 @@ void AnimationClip::addListener(const char* function, unsigned long eventTime)
     addListener(listener, eventTime);
 }
 
+void AnimationClip::removeListener(const char* function, unsigned long eventTime)
+{
+    if (_scriptListeners)
+    {
+        GP_ASSERT(eventTime < _activeDuration); // Do this check here, before we modify any state
+        std::string functionRef = Game::getInstance()->getScriptController()->loadUrl(function);
+        std::vector<ScriptListener*>::iterator iter = std::find_if(_scriptListeners->begin(), _scriptListeners->end(), [&](ScriptListener* listener){ return listener->function == functionRef; });
+        if (iter != _scriptListeners->end())
+        {
+            ScriptListener* listener = *iter;
+
+            removeListener(listener, eventTime);
+            _scriptListeners->erase(iter);
+            SAFE_DELETE(listener);
+        }
+    }
+}
+
 bool AnimationClip::update(float elapsedTime)
 {
     if (isClipStateBitSet(CLIP_IS_PAUSED_BIT))
@@ -443,7 +549,7 @@ bool AnimationClip::update(float elapsedTime)
                 GP_ASSERT((**_listenerItr)->_listener);
 
                 (**_listenerItr)->_listener->animationEvent(this, Listener::TIME);
-                ++*_listenerItr;
+                ++(*_listenerItr);
             }
         }
         else
@@ -455,7 +561,7 @@ bool AnimationClip::update(float elapsedTime)
                 GP_ASSERT((**_listenerItr)->_listener);
 
                 (**_listenerItr)->_listener->animationEvent(this, Listener::TIME);
-                --*_listenerItr;
+                --(*_listenerItr);
             }
         }
     }

+ 44 - 0
gameplay/src/AnimationClip.h

@@ -231,6 +231,13 @@ public:
      */
     void addBeginListener(AnimationClip::Listener* listener);
 
+    /**
+     * Removes an animation begin listener.
+     *
+     * @param listener The listener to be removed.
+     */
+    void removeBeginListener(AnimationClip::Listener* listener);
+
     /**
      * Adds an animation end listener.
      *
@@ -238,6 +245,13 @@ public:
      */
     void addEndListener(AnimationClip::Listener* listener);
 
+    /**
+     * Removes an animation end listener.
+     *
+     * @param listener The listener to be removed.
+     */
+    void removeEndListener(AnimationClip::Listener* listener);
+
     /**
      * Adds an animation listener to be called back at the specified eventTime during the playback 
      * of the AnimationClip.
@@ -249,6 +263,14 @@ public:
      */
     void addListener(AnimationClip::Listener* listener, unsigned long eventTime);
 
+    /**
+     * Removes an animation listener assigned to the specified eventTime.
+     *
+     * @param listener The listener to be removed with the specified time.
+     * @param eventTime The time of the listener to be removed.
+     */
+    void removeListener(AnimationClip::Listener* listener, unsigned long eventTime);
+
     /**
      * Adds an animation begin listener.
      * 
@@ -258,6 +280,13 @@ public:
      */
     void addBeginListener(const char* function);
 
+    /**
+     * Removes an animation begin listener.
+     *
+     * @param function The Lua script function to remove.
+     */
+    void removeBeginListener(const char* function);
+
     /**
      * Adds an animation end listener.
      * 
@@ -267,6 +296,13 @@ public:
      */
     void addEndListener(const char* function);
 
+    /**
+     * Removes an animation end listener.
+     *
+     * @param function The Lua script function to remove.
+     */
+    void removeEndListener(const char* function);
+
     /**
      * Adds an animation listener to be called back at the specified eventTime during the playback 
      * of the AnimationClip.
@@ -280,6 +316,14 @@ public:
      */
     void addListener(const char* function, unsigned long eventTime);
 
+    /**
+     * Removes an animation listener assigned to the specified eventTime.
+     *
+     * @param function The Lua script function to remove with the specified time.
+     * @param eventTime The time of the listener to be removed.
+     */
+    void removeListener(const char* function, unsigned long eventTime);
+
 private:
     
     static const unsigned char CLIP_IS_PLAYING_BIT = 0x01;             // Bit representing whether AnimationClip is a running clip in AnimationController

+ 182 - 0
gameplay/src/lua/lua_AnimationClip.cpp

@@ -38,6 +38,9 @@ void luaRegister_AnimationClip()
         {"pause", lua_AnimationClip_pause},
         {"play", lua_AnimationClip_play},
         {"release", lua_AnimationClip_release},
+        {"removeBeginListener", lua_AnimationClip_removeBeginListener},
+        {"removeEndListener", lua_AnimationClip_removeEndListener},
+        {"removeListener", lua_AnimationClip_removeListener},
         {"setActiveDuration", lua_AnimationClip_setActiveDuration},
         {"setBlendWeight", lua_AnimationClip_setBlendWeight},
         {"setLoopBlendTime", lua_AnimationClip_setLoopBlendTime},
@@ -918,6 +921,185 @@ int lua_AnimationClip_release(lua_State* state)
     return 0;
 }
 
+int lua_AnimationClip_removeBeginListener(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 2:
+        {
+            do
+            {
+                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                    (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
+                {
+                    // Get parameter 1 off the stack.
+                    bool param1Valid;
+                    gameplay::ScriptUtil::LuaArray<AnimationClip::Listener> param1 = gameplay::ScriptUtil::getObjectPointer<AnimationClip::Listener>(2, "AnimationClipListener", false, &param1Valid);
+                    if (!param1Valid)
+                        break;
+
+                    AnimationClip* instance = getInstance(state);
+                    instance->removeBeginListener(param1);
+                    
+                    return 0;
+                }
+            } while (0);
+
+            do
+            {
+                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
+                {
+                    // Get parameter 1 off the stack.
+                    const char* param1 = gameplay::ScriptUtil::getString(2, false);
+
+                    AnimationClip* instance = getInstance(state);
+                    instance->removeBeginListener(param1);
+                    
+                    return 0;
+                }
+            } while (0);
+
+            lua_pushstring(state, "lua_AnimationClip_removeBeginListener - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 2).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
+int lua_AnimationClip_removeEndListener(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 2:
+        {
+            do
+            {
+                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                    (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
+                {
+                    // Get parameter 1 off the stack.
+                    bool param1Valid;
+                    gameplay::ScriptUtil::LuaArray<AnimationClip::Listener> param1 = gameplay::ScriptUtil::getObjectPointer<AnimationClip::Listener>(2, "AnimationClipListener", false, &param1Valid);
+                    if (!param1Valid)
+                        break;
+
+                    AnimationClip* instance = getInstance(state);
+                    instance->removeEndListener(param1);
+                    
+                    return 0;
+                }
+            } while (0);
+
+            do
+            {
+                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
+                {
+                    // Get parameter 1 off the stack.
+                    const char* param1 = gameplay::ScriptUtil::getString(2, false);
+
+                    AnimationClip* instance = getInstance(state);
+                    instance->removeEndListener(param1);
+                    
+                    return 0;
+                }
+            } while (0);
+
+            lua_pushstring(state, "lua_AnimationClip_removeEndListener - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 2).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
+int lua_AnimationClip_removeListener(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 3:
+        {
+            do
+            {
+                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                    (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL) &&
+                    lua_type(state, 3) == LUA_TNUMBER)
+                {
+                    // Get parameter 1 off the stack.
+                    bool param1Valid;
+                    gameplay::ScriptUtil::LuaArray<AnimationClip::Listener> param1 = gameplay::ScriptUtil::getObjectPointer<AnimationClip::Listener>(2, "AnimationClipListener", false, &param1Valid);
+                    if (!param1Valid)
+                        break;
+
+                    // Get parameter 2 off the stack.
+                    unsigned long param2 = (unsigned long)luaL_checkunsigned(state, 3);
+
+                    AnimationClip* instance = getInstance(state);
+                    instance->removeListener(param1, param2);
+                    
+                    return 0;
+                }
+            } while (0);
+
+            do
+            {
+                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
+                    lua_type(state, 3) == LUA_TNUMBER)
+                {
+                    // Get parameter 1 off the stack.
+                    const char* param1 = gameplay::ScriptUtil::getString(2, false);
+
+                    // Get parameter 2 off the stack.
+                    unsigned long param2 = (unsigned long)luaL_checkunsigned(state, 3);
+
+                    AnimationClip* instance = getInstance(state);
+                    instance->removeListener(param1, param2);
+                    
+                    return 0;
+                }
+            } while (0);
+
+            lua_pushstring(state, "lua_AnimationClip_removeListener - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 3).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_AnimationClip_setActiveDuration(lua_State* state)
 {
     // Get the number of parameters.

+ 3 - 0
gameplay/src/lua/lua_AnimationClip.h

@@ -27,6 +27,9 @@ int lua_AnimationClip_isPlaying(lua_State* state);
 int lua_AnimationClip_pause(lua_State* state);
 int lua_AnimationClip_play(lua_State* state);
 int lua_AnimationClip_release(lua_State* state);
+int lua_AnimationClip_removeBeginListener(lua_State* state);
+int lua_AnimationClip_removeEndListener(lua_State* state);
+int lua_AnimationClip_removeListener(lua_State* state);
 int lua_AnimationClip_setActiveDuration(lua_State* state);
 int lua_AnimationClip_setBlendWeight(lua_State* state);
 int lua_AnimationClip_setLoopBlendTime(lua_State* state);