]> git.sesse.net Git - casparcg/blobdiff - core/producer/scene/xml_scene_producer.cpp
* Refactored blend_mode to be part of frame_transform instead of a global parameter...
[casparcg] / core / producer / scene / xml_scene_producer.cpp
index 666926b6e084e8255e6e726a006146d9d83fbf89..1ed285e573d49b5690f44fb03a54d3f49dcf7f9c 100644 (file)
 * Author: Helge Norberg, helge.norberg@svt.se
 */
 
-#include "../../stdafx.h"
+#include "../../StdAfx.h"
 
 #include "xml_scene_producer.h"
+#include "expression_parser.h"
 
 #include <boost/filesystem.hpp>
+#include <boost/filesystem/fstream.hpp>
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
 
 #include <common/env.h>
 #include <core/producer/frame_producer.h>
 
+#include <future>
+
 #include "scene_producer.h"
 
 namespace caspar { namespace core { namespace scene {
 
+void deduce_expression(variable& var, const variable_repository& repo)
+{
+       auto expr_str = var.original_expr();
+       auto trimmed = boost::trim_copy(expr_str);
+
+       if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
+       {
+               expr_str = trimmed.substr(2, expr_str.length() - 3);
+
+               if (var.is<double>())
+                       var.as<double>().bind(parse_expression<double>(expr_str, repo));
+               else if (var.is<bool>())
+                       var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
+               else if (var.is<std::wstring>())
+                       var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
+       }
+       else if (!expr_str.empty())
+       {
+               var.from_string(expr_str);
+       }
+}
+
 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
                const spl::shared_ptr<core::frame_factory>& frame_factory,
                const core::video_format_desc& format_desc,
@@ -42,13 +68,13 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
        if (params.empty())
                return core::frame_producer::empty();
 
-       std::wstring filename = env::media_folder() + L"\\" + params[0] + L".xml";
+       std::wstring filename = env::media_folder() + L"/" + params[0] + L".xml";
        
        if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
                return core::frame_producer::empty();
 
        boost::property_tree::wptree root;
-       std::wifstream file(filename);
+       boost::filesystem::wifstream file(filename);
        boost::property_tree::read_xml(
                        file,
                        root,
@@ -60,18 +86,25 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
 
        auto scene = spl::make_shared<scene_producer>(width, height);
 
-       BOOST_FOREACH(auto elem, root.get_child(L"scene.variables"))
+       for (auto elem : root.get_child(L"scene.variables"))
        {
                auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
-
-               if (type == L"double")
-                       scene->create_variable<double>(
-                                       L"variable." + elem.second.get<std::wstring>(L"<xmlattr>.id"),
-                                       false,
-                                       elem.second.get_value<std::wstring>());
+               auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
+               auto is_public = elem.second.get(L"<xmlattr>.public", false);
+               auto expr = elem.second.get_value<std::wstring>();
+
+               if (!is_public)
+                       id = L"variable." + id;
+
+               if (type == L"number")
+                       scene->create_variable<double>(id, is_public, expr);
+               else if (type == L"string")
+                       scene->create_variable<std::wstring>(id, is_public, expr);
+               else if (type == L"bool")
+                       scene->create_variable<bool>(id, is_public, expr);
        }
 
-       BOOST_FOREACH(auto elem, root.get_child(L"scene.layers"))
+       for (auto& elem : root.get_child(L"scene.layers"))
        {
                auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
                auto producer = create_producer(frame_factory, format_desc, elem.second.get<std::wstring>(L"producer"));
@@ -81,16 +114,49 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
                layer.hidden = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
                layer.position.x = scene->create_variable<double>(variable_prefix + L"x", false, elem.second.get<std::wstring>(L"x"));
                layer.position.y = scene->create_variable<double>(variable_prefix + L"y", false, elem.second.get<std::wstring>(L"y"));
+               layer.anchor.x = scene->create_variable<double>(variable_prefix + L"anchor_x", false, elem.second.get<std::wstring>(L"anchor_x", L"0.0"));
+               layer.anchor.y = scene->create_variable<double>(variable_prefix + L"anchor_y", false, elem.second.get<std::wstring>(L"anchor_y", L"0.0"));
+               layer.rotation = scene->create_variable<double>(variable_prefix + L"rotation", false, elem.second.get<std::wstring>(L"rotation", L"0.0"));
+               layer.crop.upper_left.x = scene->create_variable<double>(variable_prefix + L"crop_upper_left_x", false, elem.second.get<std::wstring>(L"crop_upper_left_x", L"0.0"));
+               layer.crop.upper_left.y = scene->create_variable<double>(variable_prefix + L"crop_upper_left_y", false, elem.second.get<std::wstring>(L"crop_upper_left_y", L"0.0"));
+               layer.crop.lower_right.x = scene->create_variable<double>(variable_prefix + L"crop_lower_right_x", false, elem.second.get<std::wstring>(L"crop_lower_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
+               layer.crop.lower_right.y = scene->create_variable<double>(variable_prefix + L"crop_lower_right_y", false, elem.second.get<std::wstring>(L"crop_lower_right_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
+               layer.perspective.upper_left.x = scene->create_variable<double>(variable_prefix + L"perspective_upper_left_x", false, elem.second.get<std::wstring>(L"perspective_upper_left_x", L"0.0"));
+               layer.perspective.upper_left.y = scene->create_variable<double>(variable_prefix + L"perspective_upper_left_y", false, elem.second.get<std::wstring>(L"perspective_upper_left_y", L"0.0"));
+               layer.perspective.upper_right.x = scene->create_variable<double>(variable_prefix + L"perspective_upper_right_x", false, elem.second.get<std::wstring>(L"perspective_upper_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
+               layer.perspective.upper_right.y = scene->create_variable<double>(variable_prefix + L"perspective_upper_right_y", false, elem.second.get<std::wstring>(L"perspective_upper_right_y", L"0.0"));
+               layer.perspective.lower_right.x = scene->create_variable<double>(variable_prefix + L"perspective_lower_right_x", false, elem.second.get<std::wstring>(L"perspective_lower_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
+               layer.perspective.lower_right.y = scene->create_variable<double>(variable_prefix + L"perspective_lower_right_y", false, elem.second.get<std::wstring>(L"perspective_lower_right_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
+               layer.perspective.lower_left.x = scene->create_variable<double>(variable_prefix + L"perspective_lower_left_x", false, elem.second.get<std::wstring>(L"perspective_lower_left_x", L"0.0"));
+               layer.perspective.lower_left.y = scene->create_variable<double>(variable_prefix + L"perspective_lower_left_y", false, elem.second.get<std::wstring>(L"perspective_lower_left_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
+
+               layer.adjustments.opacity = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
+               layer.is_key = scene->create_variable<bool>(variable_prefix + L"is_key", false, elem.second.get(L"is_key", L"false"));
+               layer.use_mipmap = scene->create_variable<bool>(variable_prefix + L"use_mipmap", false, elem.second.get(L"use_mipmap", L"false"));
+               layer.blend_mode = scene->create_variable<std::wstring>(variable_prefix + L"blend_mode", false, elem.second.get(L"blend_mode", L"normal")).transformed([](const std::wstring& b) { return get_blend_mode(b); });
 
                scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
                scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
+
+               for (auto& var_name : producer->get_variables())
+               {
+                       auto& var = producer->get_variable(var_name);
+                       auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
+
+                       if (var.is<double>())
+                               scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
+                       else if (var.is<std::wstring>())
+                               scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
+                       else if (var.is<bool>())
+                               scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
+               }
        }
 
-       BOOST_FOREACH(auto& elem, root.get_child(L"scene.timelines"))
+       for (auto& elem : root.get_child(L"scene.timelines"))
        {
                auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
 
-               BOOST_FOREACH(auto& k, elem.second)
+               for (auto& k : elem.second)
                {
                        if (k.first == L"<xmlattr>")
                                continue;
@@ -105,17 +171,21 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
                }
        }
 
-       BOOST_FOREACH(auto& elem, root.get_child(L"scene.parameters"))
+       auto repo = [&scene](const std::wstring& name) -> variable&
        {
-               auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
-               auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
+               return scene->get_variable(name); 
+       };
 
-               if (variable.is<double>())
-                       scene->create_variable<double>(id, true) = variable.as<double>();
-               else if (variable.is<std::wstring>())
-                       scene->create_variable<std::wstring>(id, true) = variable.as<std::wstring>();
+       for (auto& var_name : scene->get_variables())
+       {
+               deduce_expression(scene->get_variable(var_name), repo);
        }
 
+       auto params2 = params;
+       params2.erase(params2.begin());
+
+       scene->call(params2);
+
        return scene;
 }