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 std::wstring& name, const spl::shared_ptr<frame_producer>& producer)
37 : name(name), producer(producer)
39 clipping.width.bind(producer.get()->pixel_constraints().width);
40 clipping.height.bind(producer.get()->pixel_constraints().height);
43 adjustments::adjustments()
50 std::map<int64_t, keyframe> keyframes;
52 void on_frame(int64_t frame)
54 auto before = --keyframes.upper_bound(frame);
55 bool found_before = before != keyframes.end() && before->first < frame;
56 auto after = keyframes.upper_bound(frame);
57 bool found_after = after != keyframes.end() && after->first > frame;
58 auto exact_frame = keyframes.find(frame);
59 bool found_exact_frame = exact_frame != keyframes.end();
61 if (found_exact_frame)
63 exact_frame->second.on_destination_frame();
65 auto next_frame = ++exact_frame;
67 if (next_frame != keyframes.end() && next_frame->second.on_start_animate)
68 next_frame->second.on_start_animate();
72 int64_t start_frame = 0;
76 start_frame = before->first;
79 if (after->second.on_start_animate && frame == 0)
80 after->second.on_start_animate();
81 else if (after->second.on_animate_to)
82 after->second.on_animate_to(start_frame, frame);
87 struct scene_producer::impl
89 constraints pixel_constraints_;
90 std::list<layer> layers_;
91 interaction_aggregator aggregator_;
92 binding<int64_t> frame_number_;
93 binding<double> speed_;
94 double frame_fraction_;
95 std::map<void*, timeline> timelines_;
96 std::map<std::wstring, std::shared_ptr<core::variable>> variables_;
97 std::vector<std::wstring> variable_names_;
99 impl(int width, int height)
100 : pixel_constraints_(width, height)
101 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
104 auto speed_variable = std::make_shared<core::variable_impl<double>>(L"1.0", true, 1.0);
105 store_variable(L"scene_speed", speed_variable);
106 speed_ = speed_variable->value();
107 auto frame_variable = std::make_shared<core::variable_impl<int64_t>>(L"0", true, 0);
108 store_variable(L"frame", frame_variable);
109 frame_number_ = frame_variable->value();
113 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
115 layer layer(name, producer);
117 layer.position.x.set(x);
118 layer.position.y.set(y);
120 layers_.push_back(layer);
122 return layers_.back();
125 void store_keyframe(void* timeline_identity, const keyframe& k)
127 timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
131 const std::wstring& name, const std::shared_ptr<core::variable>& var)
133 variables_.insert(std::make_pair(name, var));
134 variable_names_.push_back(name);
137 core::variable& get_variable(const std::wstring& name)
139 auto found = variables_.find(name);
141 if (found == variables_.end())
142 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(name + L" not found in scene"));
144 return *found->second;
147 const std::vector<std::wstring>& get_variables() const
149 return variable_names_;
152 binding<int64_t> frame()
154 return frame_number_;
157 frame_transform get_transform(const layer& layer) const
159 frame_transform transform;
161 auto& pos = transform.image_transform.fill_translation;
162 auto& scale = transform.image_transform.fill_scale;
163 //auto& clip_pos = transform.image_transform.clip_translation;
164 //auto& clip_scale = transform.image_transform.clip_scale;
166 pos[0] = static_cast<double>(layer.position.x.get()) / static_cast<double>(pixel_constraints_.width.get());
167 pos[1] = static_cast<double>(layer.position.y.get()) / static_cast<double>(pixel_constraints_.height.get());
168 scale[0] = static_cast<double>(layer.producer.get()->pixel_constraints().width.get())
169 / static_cast<double>(pixel_constraints_.width.get());
170 scale[1] = static_cast<double>(layer.producer.get()->pixel_constraints().height.get())
171 / static_cast<double>(pixel_constraints_.height.get());
173 /*clip_pos[0] = static_cast<double>(layer.clipping.upper_left.x.get()) / static_cast<double>(pixel_constraints_.width.get());
174 clip_pos[1] = static_cast<double>(layer.clipping.upper_left.y.get()) / static_cast<double>(pixel_constraints_.height.get());
175 clip_scale[0] = static_cast<double>(layer.clipping.width.get()) / static_cast<double>(pixel_constraints_.width.get());
176 clip_scale[1] = static_cast<double>(layer.clipping.height.get()) / static_cast<double>(pixel_constraints_.height.get());*/
178 transform.image_transform.opacity = layer.adjustments.opacity.get();
179 transform.image_transform.is_key = layer.is_key.get();
184 draw_frame render_frame()
186 BOOST_FOREACH(auto& timeline, timelines_)
187 timeline.second.on_frame(frame_number_.get());
189 std::vector<draw_frame> frames;
191 BOOST_FOREACH(auto& layer, layers_)
193 if (layer.hidden.get())
196 draw_frame frame(layer.producer.get()->receive());
197 frame.transform() = get_transform(layer);;
198 frames.push_back(frame);
201 frame_fraction_ += speed_.get();
203 if (std::abs(frame_fraction_) >= 1.0)
205 int64_t delta = static_cast<int64_t>(frame_fraction_);
206 frame_number_.set(frame_number_.get() + delta);
207 frame_fraction_ -= delta;
210 return draw_frame(frames);
213 void on_interaction(const interaction_event::ptr& event)
215 aggregator_.translate_and_send(event);
218 bool collides(double x, double y) const
220 return collission_detect(x, y);
223 boost::optional<interaction_target> collission_detect(double x, double y) const
225 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
227 if (layer.hidden.get())
230 auto transform = get_transform(layer);
231 auto translated = translate(x, y, transform);
233 if (translated.first >= 0.0
234 && translated.first <= 1.0
235 && translated.second >= 0.0
236 && translated.second <= 1.0
237 && layer.producer.get()->collides(translated.first, translated.second))
239 return std::make_pair(transform, layer.producer.get().get());
243 return boost::optional<interaction_target>();
246 boost::unique_future<std::wstring> call(const std::vector<std::wstring>& params)
248 for (int i = 0; i + 1 < params.size(); i += 2)
250 auto found = variables_.find(boost::to_lower_copy(params[i]));
252 if (found != variables_.end() && found->second->is_public())
253 found->second->from_string(params[i + 1]);
256 return wrap_as_future(std::wstring(L""));
259 std::wstring print() const
264 std::wstring name() const
269 boost::property_tree::wptree info() const
271 boost::property_tree::wptree info;
272 info.add(L"type", L"scene");
276 void subscribe(const monitor::observable::observer_ptr& o)
280 void unsubscribe(const monitor::observable::observer_ptr& o)
285 scene_producer::scene_producer(int width, int height)
286 : impl_(new impl(width, height))
290 scene_producer::~scene_producer()
294 layer& scene_producer::create_layer(
295 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
297 return impl_->create_layer(producer, x, y, name);
300 layer& scene_producer::create_layer(
301 const spl::shared_ptr<frame_producer>& producer, const std::wstring& name)
303 return impl_->create_layer(producer, 0, 0, name);
306 binding<int64_t> scene_producer::frame()
308 return impl_->frame();
311 draw_frame scene_producer::receive_impl()
313 return impl_->render_frame();
316 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
318 void scene_producer::on_interaction(const interaction_event::ptr& event)
320 impl_->on_interaction(event);
323 bool scene_producer::collides(double x, double y) const
325 return impl_->collides(x, y);
328 std::wstring scene_producer::print() const
330 return impl_->print();
333 std::wstring scene_producer::name() const
335 return impl_->name();
338 boost::property_tree::wptree scene_producer::info() const
340 return impl_->info();
343 boost::unique_future<std::wstring> scene_producer::call(const std::vector<std::wstring>& params)
345 return impl_->call(params);
348 void scene_producer::subscribe(const monitor::observable::observer_ptr& o)
353 void scene_producer::unsubscribe(const monitor::observable::observer_ptr& o)
355 impl_->unsubscribe(o);
358 void scene_producer::store_keyframe(void* timeline_identity, const keyframe& k)
360 impl_->store_keyframe(timeline_identity, k);
363 void scene_producer::store_variable(
364 const std::wstring& name, const std::shared_ptr<core::variable>& var)
366 impl_->store_variable(name, var);
369 core::variable& scene_producer::get_variable(const std::wstring& name)
371 return impl_->get_variable(name);
374 const std::vector<std::wstring>& scene_producer::get_variables() const
376 return impl_->get_variables();
379 spl::shared_ptr<core::frame_producer> create_dummy_scene_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params)
381 if (params.size() < 1 || !boost::iequals(params.at(0), L"[SCENE]"))
382 return core::frame_producer::empty();
384 auto scene = spl::make_shared<scene_producer>(format_desc.width, format_desc.height);
386 text::text_info text_info;
387 text_info.font = L"Arial";
389 text_info.color.r = 1;
390 text_info.color.g = 1;
391 text_info.color.b = 1;
392 text_info.color.a = 0.5;
393 auto text_area = text_producer::create(frame_factory, 0, 0, L"a", text_info, 1280, 720, false);
395 auto text_width = text_area->pixel_constraints().width;
396 binding<double> padding(1);
397 binding<double> panel_width = padding + text_width + padding;
398 binding<double> panel_height = padding + text_area->pixel_constraints().height + padding;
400 auto subscription = panel_width.on_change([&]
402 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
407 auto create_param = [](std::wstring elem) -> std::vector<std::wstring>
409 std::vector<std::wstring> result;
410 result.push_back(elem);
414 auto& car_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"car")), L"car");
415 car_layer.clipping.upper_left.x.set(80);
416 car_layer.clipping.upper_left.y.set(45);
417 car_layer.clipping.width.unbind();
418 car_layer.clipping.width.set(640);
419 car_layer.clipping.height.unbind();
420 car_layer.clipping.height.set(360);
421 car_layer.adjustments.opacity.set(0.5);
422 //car_layer.hidden = scene->frame() % 50 > 25 || !(scene->frame() < 1000);
423 std::vector<std::wstring> sub_params;
424 sub_params.push_back(L"[FREEHAND]");
425 sub_params.push_back(L"640");
426 sub_params.push_back(L"360");
427 scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 10, 10, L"freehand");
430 auto& color_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"RED")), 110, 10, L"color");
431 color_layer.producer.get()->pixel_constraints().width.set(1000);
432 color_layer.producer.get()->pixel_constraints().height.set(550);
434 //scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"SP")), 50, 50);
436 auto& upper_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_left")), L"upper_left");
437 auto& upper_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_right")), L"upper_right");
438 auto& lower_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_left")), L"lower_left");
439 auto& lower_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_right")), L"lower_right");
440 auto& text_layer = scene->create_layer(text_area, L"text_area");
441 upper_left.adjustments.opacity.bind(text_layer.adjustments.opacity);
442 upper_right.adjustments.opacity.bind(text_layer.adjustments.opacity);
443 lower_left.adjustments.opacity.bind(text_layer.adjustments.opacity);
444 lower_right.adjustments.opacity.bind(text_layer.adjustments.opacity);
447 binding<double> panel_x = (scene->frame()
449 .transformed([](double v) { return std::sin(v / 20.0); })
452 .transformed([](double v) { return std::floor(v); }); // snap to pixels instead of subpixels
454 tweener tween(L"easeoutbounce");
455 binding<double> panel_x(0);
457 scene->add_keyframe(panel_x, -panel_width, 0);
458 scene->add_keyframe(panel_x, 300.0, 50, L"easeinoutsine");
459 scene->add_keyframe(panel_x, 300.0, 50 * 4);
460 scene->add_keyframe(panel_x, 1000.0, 50 * 5, L"easeinoutsine");
461 //panel_x = delay(panel_x, add_tween(panel_x, scene->frame(), 200.0, int64_t(50), L"linear"), scene->frame(), int64_t(100));
462 /*binding<double> panel_x = when(scene->frame() < 50)
463 .then(scene->frame().as<double>().transformed([tween](double t) { return tween(t, 0.0, 200, 50); }))
465 //binding<double> panel_y = when(car_layer.hidden).then(500.0).otherwise(-panel_x + 300);
466 binding<double> panel_y(500.0);
467 scene->add_keyframe(panel_y, panel_y.get(), 50 * 4);
468 scene->add_keyframe(panel_y, 720.0, 50 * 5, L"easeinexpo");
470 scene->add_keyframe(text_layer.adjustments.opacity, 1.0, 100);
471 scene->add_keyframe(text_layer.adjustments.opacity, 0.0, 125, L"linear");
472 scene->add_keyframe(text_layer.adjustments.opacity, 1.0, 150, L"linear");
474 upper_left.position.x = panel_x;
475 upper_left.position.y = panel_y;
476 upper_right.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + panel_width;
477 upper_right.position.y = upper_left.position.y;
478 lower_left.position.x = upper_left.position.x;
479 lower_left.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + panel_height;
480 lower_right.position.x = upper_right.position.x;
481 lower_right.position.y = lower_left.position.y;
482 text_layer.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + padding;
483 text_layer.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + padding + text_area->current_bearing_y().as<double>();
485 text_area->text().bind(scene->create_variable<std::wstring>(L"text", true));
487 auto params2 = params;
488 params2.erase(params2.begin());
490 scene->call(params2);