]> git.sesse.net Git - nageru/commitdiff
Refactor out find_inputs_for_block().
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 16 Jun 2019 17:12:11 +0000 (19:12 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 16 Jun 2019 17:12:11 +0000 (19:12 +0200)
nageru/scene.cpp
nageru/scene.h

index ccb3b1bf241c74c7c72ca1a1bcaac4c0f0c927ce..fded0e00b494c72d8740c7382de31cfa334cea25 100644 (file)
@@ -109,6 +109,37 @@ int Scene::add_input(lua_State* L)
        return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
 }
 
+void Scene::find_inputs_for_block(lua_State *L, Scene *scene, Block *block)
+{
+       if (lua_gettop(L) == 2) {
+               // Implicitly the last added effect.
+               assert(!scene->blocks.empty());
+               block->inputs.push_back(scene->blocks.size() - 1);
+               return;
+       }
+
+       for (int idx = 3; idx <= lua_gettop(L); ++idx) {
+               Block *input_block = nullptr;
+               if (luaL_testudata(L, idx, "Block")) {
+                       input_block = *(Block **)luaL_checkudata(L, idx, "Block");
+               } else {
+                       EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
+
+                       // Search through all the blocks to figure out which one contains this effect.
+                       for (Block *block : scene->blocks) {
+                               if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) {
+                                       input_block = block;
+                                       break;
+                               }
+                       }
+                       if (input_block == nullptr) {
+                               luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1);
+                       }
+               }
+               block->inputs.push_back(input_block->idx);
+       }
+}
+
 int Scene::add_effect(lua_State* L)
 {
        assert(lua_gettop(L) >= 2);
@@ -130,33 +161,7 @@ int Scene::add_effect(lua_State* L)
                block->alternatives.push_back(blueprint);
        }
 
-       // Find the inputs.
-       if (lua_gettop(L) == 2) {
-               assert(!scene->blocks.empty());
-               block->inputs.push_back(scene->blocks.size() - 1);
-       } else {
-               for (int idx = 3; idx <= lua_gettop(L); ++idx) {
-                       Block *input_block = nullptr;
-                       if (luaL_testudata(L, idx, "Block")) {
-                               input_block = *(Block **)luaL_checkudata(L, idx, "Block");
-                       } else {
-                               EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
-
-                               // Search through all the blocks to figure out which one contains this effect.
-                               for (Block *block : scene->blocks) {
-                                       if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) {
-                                               input_block = block;
-                                               break;
-                                       }
-                               }
-                               if (input_block == nullptr) {
-                                       luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1);
-                               }
-                       }
-                       block->inputs.push_back(input_block->idx);
-               }
-       }
-
+       find_inputs_for_block(L, scene, block);
        scene->blocks.push_back(block);
 
        return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
index 7d83a27e13ed3af6842c3082f6172ff68d5be25e..bdd5d7c78c924aa561567ff55a74a42492406cc0 100644 (file)
@@ -134,6 +134,7 @@ private:
 
        movit::Effect *instantiate_effects(const Block *block, size_t chain_idx, Instantiation *instantiation);
        size_t compute_chain_number_for_block(size_t block_idx) const;
+       static void find_inputs_for_block(lua_State *L, Scene *scene, Block *block);
 
 public:
        Scene(Theme *theme, float aspect_nom, float aspect_denom);