]> git.sesse.net Git - nageru/blobdiff - nageru/scene.cpp
Refactor out find_inputs_for_block().
[nageru] / nageru / scene.cpp
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);