]> git.sesse.net Git - nageru/blobdiff - nageru/scene.cpp
Refactor out find_inputs_for_block().
[nageru] / nageru / scene.cpp
index d7da7db997ba1fbc3d4b74a7549ed36feaca1335..fded0e00b494c72d8740c7382de31cfa334cea25 100644 (file)
@@ -67,10 +67,10 @@ size_t Scene::compute_chain_number_for_block(size_t block_idx) const
 int Scene::add_input(lua_State* L)
 {
        assert(lua_gettop(L) == 1 || lua_gettop(L) == 2);
-       Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+       Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
 
        Block *block = new Block;
-       block->idx = chain->blocks.size();
+       block->idx = scene->blocks.size();
        if (lua_gettop(L) == 1) {
                // No parameter given, so a flexible input.
                block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
@@ -104,18 +104,49 @@ int Scene::add_input(lua_State* L)
                assert(ok);
        }
        block->is_input = true;
-       chain->blocks.push_back(block);
+       scene->blocks.push_back(block);
 
        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);
-       Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+       Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
 
        Block *block = new Block;
-       block->idx = chain->blocks.size();
+       block->idx = scene->blocks.size();
 
        if (lua_istable(L, 2)) {
                size_t len = lua_objlen(L, 2);
@@ -130,34 +161,8 @@ int Scene::add_effect(lua_State* L)
                block->alternatives.push_back(blueprint);
        }
 
-       // Find the inputs.
-       if (lua_gettop(L) == 2) {
-               assert(!chain->blocks.empty());
-               block->inputs.push_back(chain->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 : chain->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 chain", idx - 1);
-                               }
-                       }
-                       block->inputs.push_back(input_block->idx);
-               }
-       }
-
-       chain->blocks.push_back(block);
+       find_inputs_for_block(L, scene, block);
+       scene->blocks.push_back(block);
 
        return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
 }
@@ -165,11 +170,11 @@ int Scene::add_effect(lua_State* L)
 int Scene::add_optional_effect(lua_State* L)
 {
        assert(lua_gettop(L) >= 2);
-       Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+       Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
 
        // NOTE: We only support effects with a single parent, since that's what IdentityEffect does.
        Block *block = new Block;
-       block->idx = chain->blocks.size();
+       block->idx = scene->blocks.size();
 
        EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
        block->alternatives.push_back(blueprint);
@@ -177,8 +182,8 @@ int Scene::add_optional_effect(lua_State* L)
        // An IdentityEffect will be the alternative for when the effect is disabled.
        block->alternatives.push_back(new EffectBlueprint(IDENTITY_EFFECT));
 
-       block->inputs.push_back(chain->blocks.size() - 1);
-       chain->blocks.push_back(block);
+       block->inputs.push_back(scene->blocks.size() - 1);
+       scene->blocks.push_back(block);
 
        return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
 }
@@ -191,8 +196,7 @@ Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::
        }
 
        // Find the chosen alternative for this block in this instance.
-       size_t chosen_alternative = (chain_idx / block->cardinality_base) % block->alternatives.size();
-       EffectType chosen_type = block->alternatives[chosen_alternative]->effect_type;
+       EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
 
        Effect *effect;
        switch (chosen_type) {
@@ -225,7 +229,7 @@ Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::
                break;
        }
        default:
-               effect = instantiate_effect(instantiation->chain.get(), block->alternatives[chosen_alternative]->effect_type);
+               effect = instantiate_effect(instantiation->chain.get(), chosen_type);
                instantiation->chain->add_effect(effect, inputs);
                break;
        }
@@ -243,11 +247,11 @@ int Scene::finalize(lua_State* L)
        } else {
                assert(lua_gettop(L) == 1);
        }
-       Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
+       Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
        Theme *theme = get_theme_updata(L);
 
        size_t base = 1;
-       for (Block *block : chain->blocks) {
+       for (Block *block : scene->blocks) {
                block->cardinality_base = base;
                base *= block->alternatives.size();
        }
@@ -259,20 +263,20 @@ int Scene::finalize(lua_State* L)
                        total_cardinality);
        }
 
-       Block *output_block = chain->blocks.back();
+       Block *output_block = scene->blocks.back();
        for (bool is_main_chain : { false, true }) {
                for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
                        if (only_one_mode && is_main_chain != chosen_mode) {
-                               chain->chains.emplace_back();
+                               scene->chains.emplace_back();
                                continue;
                        }
 
                        Scene::Instantiation instantiation;
-                       instantiation.chain.reset(new EffectChain(chain->aspect_nom, chain->aspect_denom, theme->get_resource_pool()));
-                       chain->instantiate_effects(output_block, chain_idx, &instantiation);
+                       instantiation.chain.reset(new EffectChain(scene->aspect_nom, scene->aspect_denom, theme->get_resource_pool()));
+                       scene->instantiate_effects(output_block, chain_idx, &instantiation);
 
                        add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
-                       chain->chains.emplace_back(move(instantiation));
+                       scene->chains.emplace_back(move(instantiation));
                }
        }
        return 0;
@@ -327,6 +331,8 @@ Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &inp
 #endif
                        } else if (block->signal_type_to_connect == Block::CONNECT_VIDEO) {
                                signals_to_connect.emplace(input, block->video_to_connect->get_card_index());
+                       } else if (block->signal_type_to_connect == Block::CONNECT_NONE) {
+                               luaL_error(L, "An input in a scene was not connected to anything (forgot to call display())");
                        } else {
                                assert(false);
                        }
@@ -364,16 +370,16 @@ Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &inp
                if (!block->alternatives.empty()) {
                        EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative];
                        for (const auto &key_and_tuple : blueprint->int_parameters) {
-                               int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+                               int_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
                        }
                        for (const auto &key_and_tuple : blueprint->float_parameters) {
-                               float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+                               float_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
                        }
                        for (const auto &key_and_tuple : blueprint->vec3_parameters) {
-                               vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+                               vec3_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
                        }
                        for (const auto &key_and_tuple : blueprint->vec4_parameters) {
-                               vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
+                               vec4_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
                        }
                }
        }
@@ -383,9 +389,6 @@ Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &inp
        auto setup_chain = [L, theme, signals_to_connect, images_to_select, int_to_set, float_to_set, vec3_to_set, vec4_to_set, input_state]{
                lock_guard<mutex> lock(theme->m);
 
-               assert(theme->input_state == nullptr);
-               theme->input_state = &input_state;
-
                // Set up state, including connecting signals.
                for (const auto &input_and_signal : signals_to_connect) {
                        LiveInputWrapper *input = input_and_signal.first;
@@ -428,8 +431,6 @@ Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &inp
                                                value[0], value[1], value[2], value[3]);
                        }
                }
-
-               theme->input_state = nullptr;
        };
        return make_pair(effect_chain, move(setup_chain));
 }
@@ -494,17 +495,32 @@ int Block_display(lua_State* L)
        return 0;
 }
 
-int Block_choose_alternative(lua_State* L)
+int Block_choose(lua_State* L)
 {
        assert(lua_gettop(L) == 2);
        Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
-       int alternative_idx = luaL_checknumber(L, 2);
+       int alternative_idx = -1;
+       if (lua_isnumber(L, 2)) {
+               alternative_idx = luaL_checknumber(L, 2);
+       } else if (lua_istable(L, 2)) {
+               // See if it's an Effect metatable (e.g. foo:choose(ResampleEffect))
+               lua_getfield(L, 2, "__effect_type_id");
+               if (lua_isnumber(L, -1)) {
+                       EffectType effect_type = EffectType(luaL_checknumber(L, -1));
+                       alternative_idx = find_index_of(block, effect_type);
+               }
+               lua_pop(L, 1);
+       }
+
+       if (alternative_idx == -1) {
+               luaL_error(L, "choose() called with something that was not an index or an effect type (e.g. ResampleEffect) that was part of the alternatives");
+       }
 
        assert(alternative_idx >= 0);
        assert(size_t(alternative_idx) < block->alternatives.size());
        block->currently_chosen_alternative = alternative_idx;
 
-       return 0;
+       return wrap_lua_existing_object_nonowned<EffectBlueprint>(L, "EffectBlueprint", block->alternatives[alternative_idx]);
 }
 
 int Block_enable(lua_State *L)
@@ -520,16 +536,29 @@ int Block_enable(lua_State *L)
        return 0;
 }
 
-int Block_disable(lua_State *L)
+int Block_enable_if(lua_State *L)
 {
-       assert(lua_gettop(L) == 1);
+       assert(lua_gettop(L) == 2);
        Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
 
        if (block->alternatives.size() != 2 ||
            block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
-               luaL_error(L, "disable() called on something that wasn't added with add_optional_effect()");
+               luaL_error(L, "enable_if() called on something that wasn't added with add_optional_effect()");
        }
+       bool enabled = checkbool(L, 2);
+       block->currently_chosen_alternative = enabled ? 0 : 1;
+       return 0;
+}
+
+int Block_disable(lua_State *L)
+{
+       assert(lua_gettop(L) == 1);
+       Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
+
        block->currently_chosen_alternative = find_index_of(block, IDENTITY_EFFECT);
+       if (block->currently_chosen_alternative == -1) {
+               luaL_error(L, "disable() called on something that didn't have an IdentityEffect fallback (try add_optional_effect())");
+       }
        assert(block->currently_chosen_alternative != -1);
        return 0;
 }