]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.h
Changed meaning of frame variable in scene to a continously animating variable. The...
[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 <common/log.h>
25
26 #include "../frame_producer.h"
27 #include "../../fwd.h"
28
29 #include "../binding.h"
30 #include "../variable.h"
31
32 namespace caspar { namespace core { namespace scene {
33
34 struct coord
35 {
36         binding<double> x;
37         binding<double> y;
38 };
39
40 struct rect
41 {
42         coord upper_left;
43         coord lower_right;
44 };
45
46 struct corners
47 {
48         coord upper_left;
49         coord upper_right;
50         coord lower_right;
51         coord lower_left;
52 };
53
54 struct adjustments
55 {
56         binding<double> opacity;
57
58         adjustments();
59 };
60
61 struct chroma_key
62 {
63         binding<core::chroma::type>     key;
64         binding<double>                         threshold;
65         binding<double>                         softness;
66         binding<double>                         spill;
67 };
68
69 struct layer
70 {
71         binding<std::wstring>                                           name;
72         scene::coord                                                            anchor;
73         scene::coord                                                            position;
74         scene::rect                                                                     crop;
75         scene::corners                                                          perspective;
76         binding<double>                                                         rotation;
77         scene::adjustments                                                      adjustments;
78         binding<spl::shared_ptr<frame_producer>>        producer;
79         binding<bool>                                                           hidden;
80         binding<bool>                                                           is_key;
81         binding<bool>                                                           use_mipmap;
82         binding<core::blend_mode>                                       blend_mode;
83         scene::chroma_key                                                       chroma_key;
84
85         explicit layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer);
86 };
87
88 struct keyframe
89 {
90         std::function<void ()>                                                                                          on_start_animate;
91         std::function<void (int64_t start_frame, int64_t current_frame)>        on_animate_to;
92         std::function<void ()>                                                                                          on_destination_frame;
93         int64_t                                                                                                                         destination_frame;
94 public:
95         keyframe(int64_t destination_frame)
96                 : destination_frame(destination_frame)
97         {
98         }
99 };
100
101 enum class mark_action
102 {
103         start,
104         stop,
105         jump_to,
106         remove
107 };
108
109 mark_action get_mark_action(const std::wstring& name);
110
111 class scene_producer : public frame_producer_base
112 {
113 public:
114         scene_producer(std::wstring producer_name, int width, int height, const video_format_desc& format_desc);
115         ~scene_producer();
116
117         draw_frame receive_impl() override;
118         constraints& pixel_constraints() override;
119         void on_interaction(const interaction_event::ptr& event) override;
120         bool collides(double x, double y) const override;
121         std::wstring print() const override;
122         std::wstring name() const override;
123         std::future<std::wstring> call(const std::vector<std::wstring>& params) override;
124         boost::property_tree::wptree info() const override;
125         monitor::subject& monitor_output();
126
127         layer& create_layer(
128                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name);
129         layer& create_layer(
130                         const spl::shared_ptr<frame_producer>& producer, const std::wstring& name);
131         void reverse_layers();
132
133         binding<int64_t> timeline_frame();
134         binding<double> speed();
135
136         template<typename T> binding<T>& create_variable(
137                         const std::wstring& name, bool is_public, const std::wstring& expr = L"")
138         {
139                 std::shared_ptr<core::variable> var =
140                                 std::make_shared<core::variable_impl<T>>(expr, is_public);
141
142                 store_variable(name, var);
143
144                 return var->as<T>();
145         }
146
147         template<typename T>
148         void add_keyframe(
149                         binding<T>& to_affect,
150                         T destination_value,
151                         int64_t at_frame,
152                         const std::wstring& easing)
153         {
154                 add_keyframe(to_affect, binding<T>(destination_value), at_frame, easing);
155         }
156
157         template<typename T>
158         void add_keyframe(
159                         binding<T>& to_affect,
160                         const binding<T>& destination_value,
161                         int64_t at_frame,
162                         const std::wstring& easing)
163         {
164                 if (easing.empty())
165                 {
166                         add_keyframe(to_affect, destination_value, at_frame);
167                         return;
168                 }
169
170                 tweener tween(easing);
171                 keyframe k(at_frame);
172
173                 std::shared_ptr<T> start_value(new T);
174
175                 k.on_start_animate = [=]() mutable
176                 {
177                         *start_value = to_affect.get();
178                         to_affect.unbind();
179                 };
180
181                 k.on_destination_frame = [=]() mutable
182                 {
183                         to_affect.bind(destination_value);
184                 };
185
186                 k.on_animate_to =
187                                 [=](int64_t start_frame, int64_t current_frame) mutable
188                                 {
189                                         auto relative_frame = current_frame - start_frame;
190                                         auto duration = at_frame - start_frame;
191                                         auto tweened = static_cast<T>(tween(
192                                                         static_cast<double>(relative_frame),
193                                                         *start_value,
194                                                         destination_value.get() - *start_value,
195                                                         static_cast<double>(duration)));
196
197                                         to_affect.set(tweened);
198                                         
199                                         //CASPAR_LOG(info) << relative_frame << L" " << *start_value << L" " << duration << L" " << tweened;
200                                 };
201
202                 store_keyframe(to_affect.identity(), k);
203         }
204
205         template<typename T>
206         void add_keyframe(binding<T>& to_affect, T set_value, int64_t at_frame)
207         {
208                 add_keyframe(to_affect, binding<T>(set_value), at_frame);
209         }
210
211         template<typename T>
212         void add_keyframe(binding<T>& to_affect, const binding<T>& set_value, int64_t at_frame)
213         {
214                 keyframe k(at_frame);
215
216                 k.on_destination_frame = [=]() mutable
217                 {
218                         to_affect.bind(set_value);
219                 };
220
221                 store_keyframe(to_affect.identity(), k);
222         }
223
224         void add_mark(int64_t frame, mark_action action, const std::wstring& label);
225
226         core::variable& get_variable(const std::wstring& name) override;
227         const std::vector<std::wstring>& get_variables() const override;
228 private:
229         void store_keyframe(void* timeline_identity, const keyframe& k);
230         void store_variable(
231                         const std::wstring& name, const std::shared_ptr<core::variable>& var);
232
233         struct impl;
234         std::unique_ptr<impl> impl_;
235 };
236
237 }}}