]> git.sesse.net Git - casparcg/blob - core/producer/scene/xml_scene_producer.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[casparcg] / core / producer / scene / xml_scene_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../../stdafx.h"
23
24 #include "xml_scene_producer.h"
25
26 #include <boost/filesystem.hpp>
27 #include <boost/property_tree/ptree.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
29
30 #include <common/env.h>
31 #include <core/producer/frame_producer.h>
32
33 #include "scene_producer.h"
34
35 namespace caspar { namespace core { namespace scene {
36
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)
41 {
42         if (params.empty())
43                 return core::frame_producer::empty();
44
45         std::wstring filename = env::media_folder() + L"\\" + params[0] + L".xml";
46         
47         if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
48                 return core::frame_producer::empty();
49
50         boost::property_tree::wptree root;
51         std::wifstream file(filename);
52         boost::property_tree::read_xml(
53                         file,
54                         root,
55                         boost::property_tree::xml_parser::trim_whitespace
56                                         | boost::property_tree::xml_parser::no_comments);
57
58         int width = root.get<int>(L"scene.<xmlattr>.width");
59         int height = root.get<int>(L"scene.<xmlattr>.height");
60
61         auto scene = spl::make_shared<scene_producer>(width, height);
62
63         BOOST_FOREACH(auto elem, root.get_child(L"scene.variables"))
64         {
65                 auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
66
67                 if (type == L"double")
68                         scene->create_variable<double>(
69                                         L"variable." + elem.second.get<std::wstring>(L"<xmlattr>.id"),
70                                         false,
71                                         elem.second.get_value<std::wstring>());
72         }
73
74         BOOST_FOREACH(auto elem, root.get_child(L"scene.layers"))
75         {
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".";
80
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"));
84
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;
87         }
88
89         BOOST_FOREACH(auto& elem, root.get_child(L"scene.timelines"))
90         {
91                 auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
92
93                 BOOST_FOREACH(auto& k, elem.second)
94                 {
95                         if (k.first == L"<xmlattr>")
96                                 continue;
97
98                         auto easing = k.second.get(L"<xmlattr>.easing", L"");
99                         auto at = k.second.get<int64_t>(L"<xmlattr>.at");
100
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);
105                 }
106         }
107
108         BOOST_FOREACH(auto& elem, root.get_child(L"scene.parameters"))
109         {
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");
112
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>();
117         }
118
119         return scene;
120 }
121
122 }}}