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 "scene_producer.h"
26 #include "../../frame/draw_frame.h"
27 #include "../../interaction/interaction_aggregator.h"
29 namespace caspar { namespace core { namespace scene {
31 layer::layer(const spl::shared_ptr<frame_producer>& producer)
36 adjustments::adjustments()
41 struct scene_producer::impl
43 constraints pixel_constraints_;
44 std::list<layer> layers_;
45 interaction_aggregator aggregator_;
46 binding<int64_t> frame_number_;
48 impl(int width, int height)
49 : pixel_constraints_(width, height)
50 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
55 const spl::shared_ptr<frame_producer>& producer, int x, int y)
57 layer& layer = create_layer(producer);
59 layer.position.x.set(x);
60 layer.position.y.set(y);
65 layer& create_layer(const spl::shared_ptr<frame_producer>& producer)
67 layer layer(producer);
69 layers_.push_back(layer);
71 return layers_.back();
74 binding<int64_t> frame()
79 frame_transform get_transform(const layer& layer) const
81 frame_transform transform;
83 auto& pos = transform.image_transform.fill_translation;
84 auto& scale = transform.image_transform.fill_scale;
86 pos[0] = static_cast<double>(layer.position.x.get()) / static_cast<double>(pixel_constraints_.width.get());
87 pos[1] = static_cast<double>(layer.position.y.get()) / static_cast<double>(pixel_constraints_.height.get());
88 scale[0] = static_cast<double>(layer.producer.get()->pixel_constraints().width.get())
89 / static_cast<double>(pixel_constraints_.width.get());
90 scale[1] = static_cast<double>(layer.producer.get()->pixel_constraints().height.get())
91 / static_cast<double>(pixel_constraints_.height.get());
93 transform.image_transform.opacity = layer.adjustments.opacity.get();
94 transform.image_transform.is_key = layer.is_key.get();
99 draw_frame render_frame()
101 std::vector<draw_frame> frames;
103 BOOST_FOREACH(auto& layer, layers_)
105 draw_frame frame(layer.producer.get()->receive());
106 frame.transform() = get_transform(layer);;
107 frames.push_back(frame);
110 frame_number_.set(frame_number_.get() + 1);
112 return draw_frame(frames);
115 void on_interaction(const interaction_event::ptr& event)
117 aggregator_.translate_and_send(event);
120 bool collides(double x, double y) const
122 return collission_detect(x, y);
125 boost::optional<interaction_target> collission_detect(double x, double y) const
127 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
129 auto transform = get_transform(layer);
130 auto translated = translate(x, y, transform);
132 if (translated.first >= 0.0
133 && translated.first <= 1.0
134 && translated.second >= 0.0
135 && translated.second <= 1.0
136 && layer.producer.get()->collides(translated.first, translated.second))
138 return std::make_pair(transform, layer.producer.get().get());
142 return boost::optional<interaction_target>();
145 std::wstring print() const
150 std::wstring name() const
155 boost::property_tree::wptree info() const
157 boost::property_tree::wptree info;
158 info.add(L"type", L"scene");
162 void subscribe(const monitor::observable::observer_ptr& o)
166 void unsubscribe(const monitor::observable::observer_ptr& o)
171 scene_producer::scene_producer(int width, int height)
172 : impl_(new impl(width, height))
176 scene_producer::~scene_producer()
180 layer& scene_producer::create_layer(
181 const spl::shared_ptr<frame_producer>& producer, int x, int y)
183 return impl_->create_layer(producer, x, y);
186 layer& scene_producer::create_layer(
187 const spl::shared_ptr<frame_producer>& producer)
189 return impl_->create_layer(producer);
192 binding<int64_t> scene_producer::frame()
194 return impl_->frame();
197 draw_frame scene_producer::receive_impl()
199 return impl_->render_frame();
202 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
204 void scene_producer::on_interaction(const interaction_event::ptr& event)
206 impl_->on_interaction(event);
209 bool scene_producer::collides(double x, double y) const
211 return impl_->collides(x, y);
214 std::wstring scene_producer::print() const
216 return impl_->print();
219 std::wstring scene_producer::name() const
221 return impl_->name();
224 boost::property_tree::wptree scene_producer::info() const
226 return impl_->info();
229 void scene_producer::subscribe(const monitor::observable::observer_ptr& o)
234 void scene_producer::unsubscribe(const monitor::observable::observer_ptr& o)
236 impl_->unsubscribe(o);
239 spl::shared_ptr<frame_producer> create_dummy_scene_producer(const spl::shared_ptr<class frame_factory>& frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params)
241 if (params.size() < 1 || params.at(0) != L"[SCENE]")
242 return core::frame_producer::empty();
244 auto scene = spl::make_shared<scene_producer>(1280, 720);
247 binding<double> text_width(10);
248 binding<double> padding(1);
249 binding<double> panel_width = padding + text_width + padding;
250 binding<double> panel_height(50);
252 auto subscription = panel_width.on_change([&]
254 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
262 auto create_param = [](std::wstring elem) -> std::vector<std::wstring>
264 std::vector<std::wstring> result;
265 result.push_back(elem);
269 scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"car")));
270 std::vector<std::wstring> sub_params;
271 sub_params.push_back(L"[FREEHAND]");
272 sub_params.push_back(L"640");
273 sub_params.push_back(L"360");
274 scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 10, 10);
277 scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"BLUE")), 110, 10);
279 //scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"SP")), 50, 50);
281 auto& upper_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_left")));
282 auto& upper_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_right")));
283 auto& lower_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_left")));
284 auto& lower_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_right")));
286 binding<double> panel_x = scene->frame()
288 .transformed([](double v) { return std::sin(v / 20.0); })
291 binding<double> panel_y(500);
292 upper_left.position.x = panel_x;
293 upper_left.position.y = panel_y;
294 upper_right.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + panel_width;
295 upper_right.position.y = upper_left.position.y;
296 lower_left.position.x = upper_left.position.x;
297 lower_left.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + panel_height;
298 lower_right.position.x = upper_right.position.x;
299 lower_right.position.y = lower_left.position.y;
302 panel_height.set(50);