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
24 #include "../frame_producer.h"
26 #include "../binding.h"
28 namespace caspar { namespace core { namespace scene {
38 binding<double> opacity;
45 binding<std::wstring> name;
47 adjustments adjustments;
48 binding<spl::shared_ptr<frame_producer>> producer;
52 explicit layer(const spl::shared_ptr<frame_producer>& producer);
53 layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer);
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;
63 keyframe(int64_t destination_frame)
64 : destination_frame(destination_frame)
69 template<typename T> class parameter_holder;
71 class parameter_holder_base
74 virtual ~parameter_holder_base()
78 virtual void set(const std::wstring& raw_value) = 0;
83 return dynamic_cast<parameter_holder<T>>(*this).value();
88 class parameter_holder : public parameter_holder_base
92 parameter_holder(T initial_value)
93 : value_(initial_value)
102 virtual void set(const std::wstring& raw_value)
104 value_.set(boost::lexical_cast<T>(raw_value));
108 class scene_producer : public frame_producer_base
111 scene_producer(int width, int height);
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;
125 const spl::shared_ptr<frame_producer>& producer, int x, int y);
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();
131 template<typename T> binding<T>& create_parameter(const std::wstring& name, T initial_value = T())
133 auto param = std::make_shared<parameter_holder<T>>(initial_value);
135 store_parameter(name, param);
137 return param->value();
142 binding<T>& to_affect,
145 const std::wstring& easing)
147 add_keyframe(to_affect, binding<T>(destination_value), at_frame, easing);
152 binding<T>& to_affect,
153 const binding<T>& destination_value,
155 const std::wstring& easing)
157 tweener tween(easing);
158 keyframe k(at_frame);
160 std::shared_ptr<T> start_value(new T);
162 k.on_start_animate = [=]() mutable
164 *start_value = to_affect.get();
168 k.on_destination_frame = [=]() mutable
170 to_affect.bind(destination_value);
174 [=](int64_t start_frame, int64_t current_frame) mutable
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),
181 destination_value.get() - *start_value,
182 static_cast<double>(duration)));
184 to_affect.set(tweened);
186 CASPAR_LOG(info) << relative_frame << L" " << *start_value << L" " << duration << L" " << tweened;
189 store_keyframe(to_affect.identity(), k);
193 void add_keyframe(binding<T>& to_affect, T set_value, int64_t at_frame)
195 add_keyframe(to_affect, binding<T>(set_value), at_frame);
199 void add_keyframe(binding<T>& to_affect, const binding<T>& set_value, int64_t at_frame)
201 keyframe k(at_frame);
203 k.on_destination_frame = [=]() mutable
205 to_affect.bind(set_value);
208 store_keyframe(to_affect.identity(), k);
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);
217 std::unique_ptr<impl> impl_;
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);