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 <common/future.h>
25 #include <boost/algorithm/string/split.hpp>
26 #include <boost/algorithm/string.hpp>
28 #include "scene_producer.h"
30 #include "../../frame/draw_frame.h"
31 #include "../../interaction/interaction_aggregator.h"
32 #include "../text/text_producer.h"
34 namespace caspar { namespace core { namespace scene {
36 layer::layer(const spl::shared_ptr<frame_producer>& producer)
40 layer::layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer)
41 : name(name), producer(producer)
45 adjustments::adjustments()
52 std::map<int64_t, keyframe> keyframes;
54 void on_frame(int64_t frame)
56 auto before = --keyframes.upper_bound(frame);
57 bool found_before = before != keyframes.end() && before->first < frame;
58 auto after = keyframes.upper_bound(frame);
59 bool found_after = after != keyframes.end() && after->first > frame;
60 auto exact_frame = keyframes.find(frame);
61 bool found_exact_frame = exact_frame != keyframes.end();
63 if (found_exact_frame)
65 exact_frame->second.on_destination_frame();
67 auto next_frame = ++exact_frame;
69 if (next_frame != keyframes.end() && next_frame->second.on_start_animate)
70 next_frame->second.on_start_animate();
74 int64_t start_frame = 0;
78 start_frame = before->first;
81 if (after->second.on_start_animate && frame == 0)
82 after->second.on_start_animate();
83 else if (after->second.on_animate_to)
84 after->second.on_animate_to(start_frame, frame);
89 struct scene_producer::impl
91 constraints pixel_constraints_;
92 std::list<layer> layers_;
93 interaction_aggregator aggregator_;
94 binding<int64_t> frame_number_;
95 std::map<std::wstring, std::shared_ptr<parameter_holder_base>> parameters_;
96 std::map<void*, timeline> timelines_;
98 impl(int width, int height)
99 : pixel_constraints_(width, height)
100 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
105 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
107 layer& layer = create_layer(producer, x, y);
108 layer.name.set(name);
114 const spl::shared_ptr<frame_producer>& producer, int x, int y)
116 layer& layer = create_layer(producer);
118 layer.position.x.set(x);
119 layer.position.y.set(y);
124 layer& create_layer(const spl::shared_ptr<frame_producer>& producer)
126 layer layer(producer);
128 layers_.push_back(layer);
130 return layers_.back();
133 void store_parameter(
134 const std::wstring& name,
135 const std::shared_ptr<parameter_holder_base>& param)
137 parameters_.insert(std::make_pair(boost::to_lower_copy(name), param));
140 void store_keyframe(void* timeline_identity, const keyframe& k)
142 timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
145 binding<int64_t> frame()
147 return frame_number_;
150 frame_transform get_transform(const layer& layer) const
152 frame_transform transform;
154 auto& pos = transform.image_transform.fill_translation;
155 auto& scale = transform.image_transform.fill_scale;
157 pos[0] = static_cast<double>(layer.position.x.get()) / static_cast<double>(pixel_constraints_.width.get());
158 pos[1] = static_cast<double>(layer.position.y.get()) / static_cast<double>(pixel_constraints_.height.get());
159 scale[0] = static_cast<double>(layer.producer.get()->pixel_constraints().width.get())
160 / static_cast<double>(pixel_constraints_.width.get());
161 scale[1] = static_cast<double>(layer.producer.get()->pixel_constraints().height.get())
162 / static_cast<double>(pixel_constraints_.height.get());
164 transform.image_transform.opacity = layer.adjustments.opacity.get();
165 transform.image_transform.is_key = layer.is_key.get();
170 draw_frame render_frame()
172 BOOST_FOREACH(auto& timeline, timelines_)
173 timeline.second.on_frame(frame_number_.get());
175 std::vector<draw_frame> frames;
177 BOOST_FOREACH(auto& layer, layers_)
179 if (layer.hidden.get())
182 draw_frame frame(layer.producer.get()->receive());
183 frame.transform() = get_transform(layer);;
184 frames.push_back(frame);
189 return draw_frame(frames);
192 void on_interaction(const interaction_event::ptr& event)
194 aggregator_.translate_and_send(event);
197 bool collides(double x, double y) const
199 return collission_detect(x, y);
202 boost::optional<interaction_target> collission_detect(double x, double y) const
204 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
206 if (layer.hidden.get())
209 auto transform = get_transform(layer);
210 auto translated = translate(x, y, transform);
212 if (translated.first >= 0.0
213 && translated.first <= 1.0
214 && translated.second >= 0.0
215 && translated.second <= 1.0
216 && layer.producer.get()->collides(translated.first, translated.second))
218 return std::make_pair(transform, layer.producer.get().get());
222 return boost::optional<interaction_target>();
225 boost::unique_future<std::wstring> call(const std::vector<std::wstring>& params)
227 for (int i = 0; i + 1 < params.size(); i += 2)
229 auto found = parameters_.find(boost::to_lower_copy(params[i]));
231 if (found != parameters_.end())
232 found->second->set(params[i + 1]);
235 return wrap_as_future(std::wstring(L""));
238 std::wstring print() const
243 std::wstring name() const
248 boost::property_tree::wptree info() const
250 boost::property_tree::wptree info;
251 info.add(L"type", L"scene");
255 void subscribe(const monitor::observable::observer_ptr& o)
259 void unsubscribe(const monitor::observable::observer_ptr& o)
264 scene_producer::scene_producer(int width, int height)
265 : impl_(new impl(width, height))
269 scene_producer::~scene_producer()
273 layer& scene_producer::create_layer(
274 const spl::shared_ptr<frame_producer>& producer, int x, int y)
276 return impl_->create_layer(producer, x, y);
279 layer& scene_producer::create_layer(
280 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
282 return impl_->create_layer(producer, x, y, name);
285 layer& scene_producer::create_layer(
286 const spl::shared_ptr<frame_producer>& producer)
288 return impl_->create_layer(producer);
291 binding<int64_t> scene_producer::frame()
293 return impl_->frame();
296 draw_frame scene_producer::receive_impl()
298 return impl_->render_frame();
301 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
303 void scene_producer::on_interaction(const interaction_event::ptr& event)
305 impl_->on_interaction(event);
308 bool scene_producer::collides(double x, double y) const
310 return impl_->collides(x, y);
313 std::wstring scene_producer::print() const
315 return impl_->print();
318 std::wstring scene_producer::name() const
320 return impl_->name();
323 boost::property_tree::wptree scene_producer::info() const
325 return impl_->info();
328 boost::unique_future<std::wstring> scene_producer::call(const std::vector<std::wstring>& params)
330 return impl_->call(params);
333 void scene_producer::subscribe(const monitor::observable::observer_ptr& o)
338 void scene_producer::unsubscribe(const monitor::observable::observer_ptr& o)
340 impl_->unsubscribe(o);
343 void scene_producer::store_parameter(
344 const std::wstring& name,
345 const std::shared_ptr<parameter_holder_base>& param)
347 impl_->store_parameter(name, param);
350 void scene_producer::store_keyframe(void* timeline_identity, const keyframe& k)
352 impl_->store_keyframe(timeline_identity, k);
355 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)
357 if (params.size() < 1 || !boost::iequals(params.at(0), L"[SCENE]"))
358 return core::frame_producer::empty();
360 auto scene = spl::make_shared<scene_producer>(format_desc.width, format_desc.height);
362 text::text_info text_info;
363 text_info.font = L"Arial";
365 text_info.color.r = 1;
366 text_info.color.g = 1;
367 text_info.color.b = 1;
368 text_info.color.a = 0.5;
369 auto text_area = text_producer::create(frame_factory, 0, 0, L"a", text_info, 1280, 720, false);
371 auto text_width = text_area->pixel_constraints().width;
372 binding<double> padding(1);
373 binding<double> panel_width = padding + text_width + padding;
374 binding<double> panel_height = padding + text_area->pixel_constraints().height + padding;
376 auto subscription = panel_width.on_change([&]
378 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
383 auto create_param = [](std::wstring elem) -> std::vector<std::wstring>
385 std::vector<std::wstring> result;
386 result.push_back(elem);
390 //auto& car_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"car")));
391 //car_layer.hidden = scene->frame() % 50 > 25 || !(scene->frame() < 1000);
392 std::vector<std::wstring> sub_params;
393 sub_params.push_back(L"[FREEHAND]");
394 sub_params.push_back(L"640");
395 sub_params.push_back(L"360");
396 scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 10, 10);
399 scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"BLUE")), 110, 10);
401 //scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"SP")), 50, 50);
403 auto& upper_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_left")));
404 auto& upper_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_right")));
405 auto& lower_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_left")));
406 auto& lower_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_right")));
407 auto& text_layer = scene->create_layer(text_area);
410 binding<double> panel_x = (scene->frame()
412 .transformed([](double v) { return std::sin(v / 20.0); })
415 .transformed([](double v) { return std::floor(v); }); // snap to pixels instead of subpixels
417 tweener tween(L"easeoutbounce");
418 binding<double> panel_x(0);
420 scene->add_keyframe(panel_x, -panel_width, 0);
421 scene->add_keyframe(panel_x, 300.0, 50, L"easeinoutsine");
422 scene->add_keyframe(panel_x, 300.0, 50 * 4);
423 scene->add_keyframe(panel_x, 1000.0, 50 * 5, L"easeinoutsine");
424 //panel_x = delay(panel_x, add_tween(panel_x, scene->frame(), 200.0, int64_t(50), L"linear"), scene->frame(), int64_t(100));
425 /*binding<double> panel_x = when(scene->frame() < 50)
426 .then(scene->frame().as<double>().transformed([tween](double t) { return tween(t, 0.0, 200, 50); }))
428 //binding<double> panel_y = when(car_layer.hidden).then(500.0).otherwise(-panel_x + 300);
429 binding<double> panel_y(500.0);
430 scene->add_keyframe(panel_y, panel_y.get(), 50 * 4);
431 scene->add_keyframe(panel_y, 720.0, 50 * 5, L"easeinexpo");
432 upper_left.position.x = panel_x;
433 upper_left.position.y = panel_y;
434 upper_right.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + panel_width;
435 upper_right.position.y = upper_left.position.y;
436 lower_left.position.x = upper_left.position.x;
437 lower_left.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + panel_height;
438 lower_right.position.x = upper_right.position.x;
439 lower_right.position.y = lower_left.position.y;
440 text_layer.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + padding;
441 text_layer.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + padding + text_area->current_bearing_y().as<double>();
443 text_area->text().bind(scene->create_parameter<std::wstring>(L"text"));
445 auto params2 = params;
446 params2.erase(params2.begin());
448 scene->call(params2);