]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.cpp
Added basic timeline/keyframe support in scene_producer
[casparcg] / core / producer / scene / 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 <common/future.h>
25 #include <boost/algorithm/string/split.hpp>
26 #include <boost/algorithm/string.hpp>
27
28 #include "scene_producer.h"
29
30 #include "../../frame/draw_frame.h"
31 #include "../../interaction/interaction_aggregator.h"
32 #include "../text/text_producer.h"
33
34 namespace caspar { namespace core { namespace scene {
35
36 layer::layer(const spl::shared_ptr<frame_producer>& producer)
37         : producer(producer)
38 {
39 }
40 layer::layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer)
41         : name(name), producer(producer)
42 {
43 }
44
45 adjustments::adjustments()
46         : opacity(1.0)
47 {
48 }
49
50 struct timeline
51 {
52         std::map<int64_t, keyframe> keyframes;
53
54         void on_frame(int64_t frame)
55         {
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();
62
63                 if (found_exact_frame)
64                 {
65                         exact_frame->second.on_destination_frame();
66
67                         auto next_frame = ++exact_frame;
68
69                         if (next_frame != keyframes.end() && next_frame->second.on_start_animate)
70                                 next_frame->second.on_start_animate();
71                 }
72                 else if (found_after)
73                 {
74                         int64_t start_frame = 0;
75
76                         if (found_before)
77                         {
78                                 start_frame = before->first;
79                         }
80
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);
85                 }
86         }
87 };
88
89 struct scene_producer::impl
90 {
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_;
97
98         impl(int width, int height)
99                 : pixel_constraints_(width, height)
100                 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
101         {
102         }
103
104         layer& create_layer(
105                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
106         {
107                 layer& layer = create_layer(producer, x, y);
108                 layer.name.set(name);
109
110                 return layer;
111         }
112
113         layer& create_layer(
114                         const spl::shared_ptr<frame_producer>& producer, int x, int y)
115         {
116                 layer& layer = create_layer(producer);
117
118                 layer.position.x.set(x);
119                 layer.position.y.set(y);
120
121                 return layer;
122         }
123
124         layer& create_layer(const spl::shared_ptr<frame_producer>& producer)
125         {
126                 layer layer(producer);
127
128                 layers_.push_back(layer);
129
130                 return layers_.back();
131         }
132
133         void store_parameter(
134                         const std::wstring& name,
135                         const std::shared_ptr<parameter_holder_base>& param)
136         {
137                 parameters_.insert(std::make_pair(boost::to_lower_copy(name), param));
138         }
139
140         void store_keyframe(void* timeline_identity, const keyframe& k)
141         {
142                 timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
143         }
144
145         binding<int64_t> frame()
146         {
147                 return frame_number_;
148         }
149
150         frame_transform get_transform(const layer& layer) const
151         {
152                 frame_transform transform;
153
154                 auto& pos = transform.image_transform.fill_translation;
155                 auto& scale = transform.image_transform.fill_scale;
156
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());
163
164                 transform.image_transform.opacity = layer.adjustments.opacity.get();
165                 transform.image_transform.is_key = layer.is_key.get();
166
167                 return transform;
168         }
169
170         draw_frame render_frame()
171         {
172                 BOOST_FOREACH(auto& timeline, timelines_)
173                         timeline.second.on_frame(frame_number_.get());
174
175                 std::vector<draw_frame> frames;
176
177                 BOOST_FOREACH(auto& layer, layers_)
178                 {
179                         if (layer.hidden.get())
180                                 continue;
181
182                         draw_frame frame(layer.producer.get()->receive());
183                         frame.transform() = get_transform(layer);;
184                         frames.push_back(frame);
185                 }
186
187                 ++frame_number_;
188
189                 return draw_frame(frames);
190         }
191
192         void on_interaction(const interaction_event::ptr& event)
193         {
194                 aggregator_.translate_and_send(event);
195         }
196
197         bool collides(double x, double y) const
198         {
199                 return collission_detect(x, y);
200         }
201
202         boost::optional<interaction_target> collission_detect(double x, double y) const
203         {
204                 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
205                 {
206                         if (layer.hidden.get())
207                                 continue;
208
209                         auto transform = get_transform(layer);
210                         auto translated = translate(x, y, transform);
211
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))
217                         {
218                                 return std::make_pair(transform, layer.producer.get().get());
219                         }
220                 }
221
222                 return boost::optional<interaction_target>();
223         }
224
225         boost::unique_future<std::wstring> call(const std::vector<std::wstring>& params) 
226         {
227                 for (int i = 0; i + 1 < params.size(); i += 2)
228                 {
229                         auto found = parameters_.find(boost::to_lower_copy(params[i]));
230
231                         if (found != parameters_.end())
232                                 found->second->set(params[i + 1]);
233                 }
234
235                 return wrap_as_future(std::wstring(L""));
236         }
237
238         std::wstring print() const
239         {
240                 return L"scene[]";
241         }
242
243         std::wstring name() const
244         {
245                 return L"scene";
246         }
247         
248         boost::property_tree::wptree info() const
249         {
250                 boost::property_tree::wptree info;
251                 info.add(L"type", L"scene");
252                 return info;
253         }
254
255         void subscribe(const monitor::observable::observer_ptr& o)
256         {
257         }
258
259         void unsubscribe(const monitor::observable::observer_ptr& o)
260         {
261         }
262 };
263
264 scene_producer::scene_producer(int width, int height)
265         : impl_(new impl(width, height))
266 {
267 }
268
269 scene_producer::~scene_producer()
270 {
271 }
272
273 layer& scene_producer::create_layer(
274                 const spl::shared_ptr<frame_producer>& producer, int x, int y)
275 {
276         return impl_->create_layer(producer, x, y);
277 }
278
279 layer& scene_producer::create_layer(
280                 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
281 {
282         return impl_->create_layer(producer, x, y, name);
283 }
284
285 layer& scene_producer::create_layer(
286                 const spl::shared_ptr<frame_producer>& producer)
287 {
288         return impl_->create_layer(producer);
289 }
290
291 binding<int64_t> scene_producer::frame()
292 {
293         return impl_->frame();
294 }
295
296 draw_frame scene_producer::receive_impl()
297 {
298         return impl_->render_frame();
299 }
300
301 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
302
303 void scene_producer::on_interaction(const interaction_event::ptr& event)
304 {
305         impl_->on_interaction(event);
306 }
307
308 bool scene_producer::collides(double x, double y) const
309 {
310         return impl_->collides(x, y);
311 }
312
313 std::wstring scene_producer::print() const
314 {
315         return impl_->print();
316 }
317
318 std::wstring scene_producer::name() const
319 {
320         return impl_->name();
321 }
322
323 boost::property_tree::wptree scene_producer::info() const
324 {
325         return impl_->info();
326 }
327
328 boost::unique_future<std::wstring> scene_producer::call(const std::vector<std::wstring>& params) 
329 {
330         return impl_->call(params);
331 }
332
333 void scene_producer::subscribe(const monitor::observable::observer_ptr& o)
334 {
335         impl_->subscribe(o);
336 }
337
338 void scene_producer::unsubscribe(const monitor::observable::observer_ptr& o)
339 {
340         impl_->unsubscribe(o);
341 }
342
343 void scene_producer::store_parameter(
344                 const std::wstring& name,
345                 const std::shared_ptr<parameter_holder_base>& param)
346 {
347         impl_->store_parameter(name, param);
348 }
349
350 void scene_producer::store_keyframe(void* timeline_identity, const keyframe& k)
351 {
352         impl_->store_keyframe(timeline_identity, k);
353 }
354
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)
356 {
357         if (params.size() < 1 || !boost::iequals(params.at(0), L"[SCENE]"))
358                 return core::frame_producer::empty();
359
360         auto scene = spl::make_shared<scene_producer>(format_desc.width, format_desc.height);
361
362         text::text_info text_info;
363         text_info.font = L"Arial";
364         text_info.size = 62;
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);
370
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;
375
376         auto subscription = panel_width.on_change([&]
377         {
378                 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
379         });
380
381         padding.set(2);
382
383         auto create_param = [](std::wstring elem) -> std::vector<std::wstring>
384         {
385                 std::vector<std::wstring> result;
386                 result.push_back(elem);
387                 return result;
388         };
389
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);
397         sub_params.clear();
398
399         scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"BLUE")), 110, 10);
400
401         //scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"SP")), 50, 50);
402
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);
408
409         /*
410         binding<double> panel_x = (scene->frame()
411                         .as<double>()
412                         .transformed([](double v) { return std::sin(v / 20.0); })
413                         * 20.0
414                         + 40.0)
415                         .transformed([](double v) { return std::floor(v); }); // snap to pixels instead of subpixels
416                         */
417         tweener tween(L"easeoutbounce");
418         binding<double> panel_x(0);
419
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); }))
427                 .otherwise(200.0);*/
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>();
442
443         text_area->text().bind(scene->create_parameter<std::wstring>(L"text"));
444
445         auto params2 = params;
446         params2.erase(params2.begin());
447
448         scene->call(params2);
449
450         return scene;
451 }
452
453 }}}