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 "scene_producer.h"
26 #include "../../frame/draw_frame.h"
27 #include "../../interaction/interaction_aggregator.h"
29 namespace caspar { namespace core { namespace scene {
31 layer::layer(const spl::shared_ptr<frame_producer>& producer)
36 adjustments::adjustments()
41 struct scene_producer::impl
43 constraints pixel_constraints_;
44 std::list<layer> layers_;
45 interaction_aggregator aggregator_;
46 binding<int64_t> frame_number_;
48 impl(int width, int height)
49 : pixel_constraints_(width, height)
50 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
55 const spl::shared_ptr<frame_producer>& producer, int x, int y)
57 layer& layer = create_layer(producer);
59 layer.position.x.set(x);
60 layer.position.y.set(y);
65 layer& create_layer(const spl::shared_ptr<frame_producer>& producer)
67 layer layer(producer);
69 layers_.push_back(layer);
71 return layers_.back();
74 binding<int64_t> frame()
79 frame_transform get_transform(const layer& layer) const
81 frame_transform transform;
83 auto& pos = transform.image_transform.fill_translation;
84 auto& scale = transform.image_transform.fill_scale;
86 pos[0] = static_cast<double>(layer.position.x.get()) / static_cast<double>(pixel_constraints_.width.get());
87 pos[1] = static_cast<double>(layer.position.y.get()) / static_cast<double>(pixel_constraints_.height.get());
88 scale[0] = static_cast<double>(layer.producer.get()->pixel_constraints().width.get())
89 / static_cast<double>(pixel_constraints_.width.get());
90 scale[1] = static_cast<double>(layer.producer.get()->pixel_constraints().height.get())
91 / static_cast<double>(pixel_constraints_.height.get());
93 transform.image_transform.opacity = layer.adjustments.opacity.get();
94 transform.image_transform.is_key = layer.is_key.get();
99 draw_frame render_frame()
101 std::vector<draw_frame> frames;
103 BOOST_FOREACH(auto& layer, layers_)
105 if (layer.hidden.get())
108 draw_frame frame(layer.producer.get()->receive());
109 frame.transform() = get_transform(layer);;
110 frames.push_back(frame);
113 frame_number_.set(frame_number_.get() + 1);
115 return draw_frame(frames);
118 void on_interaction(const interaction_event::ptr& event)
120 aggregator_.translate_and_send(event);
123 bool collides(double x, double y) const
125 return collission_detect(x, y);
128 boost::optional<interaction_target> collission_detect(double x, double y) const
130 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
132 if (layer.hidden.get())
135 auto transform = get_transform(layer);
136 auto translated = translate(x, y, transform);
138 if (translated.first >= 0.0
139 && translated.first <= 1.0
140 && translated.second >= 0.0
141 && translated.second <= 1.0
142 && layer.producer.get()->collides(translated.first, translated.second))
144 return std::make_pair(transform, layer.producer.get().get());
148 return boost::optional<interaction_target>();
151 std::wstring print() const
156 std::wstring name() const
161 boost::property_tree::wptree info() const
163 boost::property_tree::wptree info;
164 info.add(L"type", L"scene");
168 void subscribe(const monitor::observable::observer_ptr& o)
172 void unsubscribe(const monitor::observable::observer_ptr& o)
177 scene_producer::scene_producer(int width, int height)
178 : impl_(new impl(width, height))
182 scene_producer::~scene_producer()
186 layer& scene_producer::create_layer(
187 const spl::shared_ptr<frame_producer>& producer, int x, int y)
189 return impl_->create_layer(producer, x, y);
192 layer& scene_producer::create_layer(
193 const spl::shared_ptr<frame_producer>& producer)
195 return impl_->create_layer(producer);
198 binding<int64_t> scene_producer::frame()
200 return impl_->frame();
203 draw_frame scene_producer::receive_impl()
205 return impl_->render_frame();
208 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
210 void scene_producer::on_interaction(const interaction_event::ptr& event)
212 impl_->on_interaction(event);
215 bool scene_producer::collides(double x, double y) const
217 return impl_->collides(x, y);
220 std::wstring scene_producer::print() const
222 return impl_->print();
225 std::wstring scene_producer::name() const
227 return impl_->name();
230 boost::property_tree::wptree scene_producer::info() const
232 return impl_->info();
235 void scene_producer::subscribe(const monitor::observable::observer_ptr& o)
240 void scene_producer::unsubscribe(const monitor::observable::observer_ptr& o)
242 impl_->unsubscribe(o);
245 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)
247 if (params.size() < 1 || params.at(0) != L"[SCENE]")
248 return core::frame_producer::empty();
250 auto scene = spl::make_shared<scene_producer>(1280, 720);
252 binding<double> text_width(10);
253 binding<double> padding(1);
254 binding<double> panel_width = padding + text_width + padding;
255 binding<double> panel_height(50);
257 auto subscription = panel_width.on_change([&]
259 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
267 auto create_param = [](std::wstring elem) -> std::vector<std::wstring>
269 std::vector<std::wstring> result;
270 result.push_back(elem);
274 auto& car_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"car")));
275 car_layer.hidden = scene->frame() % 50 > 25 || !(scene->frame() < 1000);
276 std::vector<std::wstring> sub_params;
277 sub_params.push_back(L"[FREEHAND]");
278 sub_params.push_back(L"640");
279 sub_params.push_back(L"360");
280 scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 10, 10);
283 scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"BLUE")), 110, 10);
285 //scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"SP")), 50, 50);
287 auto& upper_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_left")));
288 auto& upper_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_right")));
289 auto& lower_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_left")));
290 auto& lower_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_right")));
292 binding<double> panel_x = (scene->frame()
294 .transformed([](double v) { return std::sin(v / 20.0); })
297 .transformed([](double v) { return std::floor(v); }); // snap to pixels instead of subpixels
298 binding<double> panel_y = when(car_layer.hidden).then(500.0).otherwise(-panel_x + 300);
299 upper_left.position.x = panel_x;
300 upper_left.position.y = panel_y;
301 upper_right.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + panel_width;
302 upper_right.position.y = upper_left.position.y;
303 lower_left.position.x = upper_left.position.x;
304 lower_left.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + panel_height;
305 lower_right.position.x = upper_right.position.x;
306 lower_right.position.y = lower_left.position.y;
309 panel_height.set(50);