]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.h
Added basic timeline/keyframe support in scene_producer
[casparcg] / core / producer / scene / scene_producer.h
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 #pragma once
23
24 #include "../frame_producer.h"
25
26 #include "../binding.h"
27
28 namespace caspar { namespace core { namespace scene {
29
30 struct coord
31 {
32         binding<double> x;
33         binding<double> y;
34 };
35
36 struct adjustments
37 {
38         binding<double> opacity;
39
40         adjustments();
41 };
42
43 struct layer
44 {
45         binding<std::wstring> name;
46         coord position;
47         adjustments adjustments;
48         binding<spl::shared_ptr<frame_producer>> producer;
49         binding<bool> hidden;
50         binding<bool> is_key;
51
52         explicit layer(const spl::shared_ptr<frame_producer>& producer);
53         layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer);
54 };
55
56 struct keyframe
57 {
58         std::function<void ()> on_start_animate;
59         std::function<void (int64_t start_frame, int64_t current_frame)> on_animate_to;
60         std::function<void ()> on_destination_frame;
61         int64_t destination_frame;
62 public:
63         keyframe(int64_t destination_frame)
64                 : destination_frame(destination_frame)
65         {
66         }
67 };
68
69 template<typename T> class parameter_holder;
70
71 class parameter_holder_base
72 {
73 public:
74         virtual ~parameter_holder_base()
75         {
76         }
77
78         virtual void set(const std::wstring& raw_value) = 0;
79
80         template<typename T>
81         binding<T>& value()
82         {
83                 return dynamic_cast<parameter_holder<T>>(*this).value();
84         }
85 };
86
87 template<typename T>
88 class parameter_holder : public parameter_holder_base
89 {
90         binding<T> value_;
91 public:
92         parameter_holder(T initial_value)
93                 : value_(initial_value)
94         {
95         }
96
97         binding<T>& value()
98         {
99                 return value_;
100         }
101
102         virtual void set(const std::wstring& raw_value)
103         {
104                 value_.set(boost::lexical_cast<T>(raw_value));
105         }
106 };
107
108 class scene_producer : public frame_producer_base
109 {
110 public:
111         scene_producer(int width, int height);
112         ~scene_producer();
113
114         class draw_frame receive_impl() override;
115         constraints& pixel_constraints() override;
116         void on_interaction(const interaction_event::ptr& event) override;
117         bool collides(double x, double y) const override;
118         std::wstring print() const override;
119         std::wstring name() const override;
120         boost::unique_future<std::wstring>      call(const std::vector<std::wstring>& params) override;
121         boost::property_tree::wptree info() const override;
122         void subscribe(const monitor::observable::observer_ptr& o) override;
123         void unsubscribe(const monitor::observable::observer_ptr& o) override;
124         layer& create_layer(
125                         const spl::shared_ptr<frame_producer>& producer, int x, int y);
126         layer& create_layer(
127                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name);
128         layer& create_layer(const spl::shared_ptr<frame_producer>& producer);
129         binding<int64_t> frame();
130
131         template<typename T> binding<T>& create_parameter(const std::wstring& name, T initial_value = T())
132         {
133                 auto param = std::make_shared<parameter_holder<T>>(initial_value);
134
135                 store_parameter(name, param);
136
137                 return param->value();
138         }
139
140         template<typename T>
141         void add_keyframe(
142                         binding<T>& to_affect,
143                         T destination_value,
144                         int64_t at_frame,
145                         const std::wstring& easing)
146         {
147                 add_keyframe(to_affect, binding<T>(destination_value), at_frame, easing);
148         }
149
150         template<typename T>
151         void add_keyframe(
152                         binding<T>& to_affect,
153                         const binding<T>& destination_value,
154                         int64_t at_frame,
155                         const std::wstring& easing)
156         {
157                 tweener tween(easing);
158                 keyframe k(at_frame);
159
160                 std::shared_ptr<T> start_value(new T);
161
162                 k.on_start_animate = [=]() mutable
163                 {
164                         *start_value = to_affect.get();
165                         to_affect.unbind();
166                 };
167
168                 k.on_destination_frame = [=]() mutable
169                 {
170                         to_affect.bind(destination_value);
171                 };
172
173                 k.on_animate_to =
174                                 [=](int64_t start_frame, int64_t current_frame) mutable
175                                 {
176                                         auto relative_frame = current_frame - start_frame;
177                                         auto duration = at_frame - start_frame;
178                                         auto tweened = static_cast<T>(tween(
179                                                         static_cast<double>(relative_frame),
180                                                         *start_value,
181                                                         destination_value.get() - *start_value,
182                                                         static_cast<double>(duration)));
183
184                                         to_affect.set(tweened);
185                                         
186                                         CASPAR_LOG(info) << relative_frame << L" " << *start_value << L" " << duration << L" " << tweened;
187                                 };
188
189                 store_keyframe(to_affect.identity(), k);
190         }
191
192         template<typename T>
193         void add_keyframe(binding<T>& to_affect, T set_value, int64_t at_frame)
194         {
195                 add_keyframe(to_affect, binding<T>(set_value), at_frame);
196         }
197
198         template<typename T>
199         void add_keyframe(binding<T>& to_affect, const binding<T>& set_value, int64_t at_frame)
200         {
201                 keyframe k(at_frame);
202
203                 k.on_destination_frame = [=]() mutable
204                 {
205                         to_affect.bind(set_value);
206                 };
207
208                 store_keyframe(to_affect.identity(), k);
209         }
210 private:
211         void store_parameter(
212                         const std::wstring& name,
213                         const std::shared_ptr<parameter_holder_base>& param);
214         void store_keyframe(void* timeline_identity, const keyframe& k);
215
216         struct impl;
217         std::unique_ptr<impl> impl_;
218 };
219
220 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);
221
222 }}}