2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
22 #include "../../stdafx.h"
24 #include "xml_scene_producer.h"
26 #include <boost/filesystem.hpp>
27 #include <boost/property_tree/ptree.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
30 #include <common/env.h>
31 #include <core/producer/frame_producer.h>
33 #include "scene_producer.h"
35 namespace caspar { namespace core { namespace scene {
37 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
38 const spl::shared_ptr<core::frame_factory>& frame_factory,
39 const core::video_format_desc& format_desc,
40 const std::vector<std::wstring>& params)
43 return core::frame_producer::empty();
45 std::wstring filename = env::media_folder() + L"\\" + params[0] + L".xml";
47 if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
48 return core::frame_producer::empty();
50 boost::property_tree::wptree root;
51 std::wifstream file(filename);
52 boost::property_tree::read_xml(
55 boost::property_tree::xml_parser::trim_whitespace
56 | boost::property_tree::xml_parser::no_comments);
58 int width = root.get<int>(L"scene.<xmlattr>.width");
59 int height = root.get<int>(L"scene.<xmlattr>.height");
61 auto scene = spl::make_shared<scene_producer>(width, height);
63 BOOST_FOREACH(auto elem, root.get_child(L"scene.variables"))
65 auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
67 if (type == L"double")
68 scene->create_variable<double>(
69 L"variable." + elem.second.get<std::wstring>(L"<xmlattr>.id"),
71 elem.second.get_value<std::wstring>());
74 BOOST_FOREACH(auto elem, root.get_child(L"scene.layers"))
76 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
77 auto producer = create_producer(frame_factory, format_desc, elem.second.get<std::wstring>(L"producer"));
78 auto& layer = scene->create_layer(producer, 0, 0, id);
79 auto variable_prefix = L"layer." + id + L".";
81 layer.hidden = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
82 layer.position.x = scene->create_variable<double>(variable_prefix + L"x", false, elem.second.get<std::wstring>(L"x"));
83 layer.position.y = scene->create_variable<double>(variable_prefix + L"y", false, elem.second.get<std::wstring>(L"y"));
85 scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
86 scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
89 BOOST_FOREACH(auto& elem, root.get_child(L"scene.timelines"))
91 auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
93 BOOST_FOREACH(auto& k, elem.second)
95 if (k.first == L"<xmlattr>")
98 auto easing = k.second.get(L"<xmlattr>.easing", L"");
99 auto at = k.second.get<int64_t>(L"<xmlattr>.at");
101 if (variable.is<double>())
102 scene->add_keyframe(variable.as<double>(), k.second.get_value<double>(), at, easing);
103 else if (variable.is<int>())
104 scene->add_keyframe(variable.as<int>(), k.second.get_value<int>(), at, easing);
108 BOOST_FOREACH(auto& elem, root.get_child(L"scene.parameters"))
110 auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
111 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
113 if (variable.is<double>())
114 scene->create_variable<double>(id, true) = variable.as<double>();
115 else if (variable.is<std::wstring>())
116 scene->create_variable<std::wstring>(id, true) = variable.as<std::wstring>();