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 struct scene_producer::impl
38 constraints pixel_constraints_;
39 std::vector<layer> layers_;
40 interaction_aggregator aggregator_;
42 impl(int width, int height)
43 : pixel_constraints_(width, height)
44 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
49 const spl::shared_ptr<frame_producer>& producer, int x, int y)
51 layer layer(producer);
53 layer.position.x.set(x);
54 layer.position.y.set(y);
56 layers_.push_back(layer);
58 return layers_.back();
61 frame_transform get_transform(const layer& layer) const
63 frame_transform transform;
65 auto& pos = transform.image_transform.fill_translation;
66 auto& scale = transform.image_transform.fill_scale;
68 pos[0] = static_cast<double>(layer.position.x.get()) / static_cast<double>(pixel_constraints_.width.get());
69 pos[1] = static_cast<double>(layer.position.y.get()) / static_cast<double>(pixel_constraints_.height.get());
70 scale[0] = static_cast<double>(layer.producer.get()->pixel_constraints().width.get())
71 / static_cast<double>(pixel_constraints_.width.get());
72 scale[1] = static_cast<double>(layer.producer.get()->pixel_constraints().height.get())
73 / static_cast<double>(pixel_constraints_.height.get());
78 draw_frame render_frame()
80 std::vector<draw_frame> frames;
82 BOOST_FOREACH(auto& layer, layers_)
84 draw_frame frame(layer.producer.get()->receive());
85 frame.transform() = get_transform(layer);;
86 frames.push_back(frame);
89 return draw_frame(frames);
92 void on_interaction(const interaction_event::ptr& event)
94 aggregator_.translate_and_send(event);
97 bool collides(double x, double y) const
99 return collission_detect(x, y);
102 boost::optional<interaction_target> collission_detect(double x, double y) const
104 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
106 auto transform = get_transform(layer);
107 auto translated = translate(x, y, transform);
109 if (translated.first >= 0.0
110 && translated.first <= 1.0
111 && translated.second >= 0.0
112 && translated.second <= 1.0
113 && layer.producer.get()->collides(translated.first, translated.second))
115 return std::make_pair(transform, layer.producer.get().get());
119 return boost::optional<interaction_target>();
122 std::wstring print() const
127 std::wstring name() const
132 boost::property_tree::wptree info() const
134 boost::property_tree::wptree info;
135 info.add(L"type", L"scene");
139 void subscribe(const monitor::observable::observer_ptr& o)
143 void unsubscribe(const monitor::observable::observer_ptr& o)
148 scene_producer::scene_producer(int width, int height)
149 : impl_(new impl(width, height))
153 scene_producer::~scene_producer()
157 layer& scene_producer::create_layer(
158 const spl::shared_ptr<frame_producer>& producer, int x, int y)
160 return impl_->create_layer(producer, x, y);
163 draw_frame scene_producer::receive_impl()
165 return impl_->render_frame();
168 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
170 void scene_producer::on_interaction(const interaction_event::ptr& event)
172 impl_->on_interaction(event);
175 bool scene_producer::collides(double x, double y) const
177 return impl_->collides(x, y);
180 std::wstring scene_producer::print() const
182 return impl_->print();
185 std::wstring scene_producer::name() const
187 return impl_->name();
190 boost::property_tree::wptree scene_producer::info() const
192 return impl_->info();
195 void scene_producer::subscribe(const monitor::observable::observer_ptr& o)
200 void scene_producer::unsubscribe(const monitor::observable::observer_ptr& o)
202 impl_->unsubscribe(o);
205 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)
207 if (params.size() < 1 || params.at(0) != L"[SCENE]")
208 return core::frame_producer::empty();
210 auto scene = spl::make_shared<scene_producer>(160, 90);
212 std::vector<std::wstring> sub_params;
214 binding<int> text_width(10);
215 binding<int> padding(1);
216 binding<int> panel_width = padding + text_width + padding;
218 auto subscription = panel_width.on_change([&]
220 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
228 sub_params.push_back(L"[FREEHAND]");
229 sub_params.push_back(L"100");
230 sub_params.push_back(L"50");
231 scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 10, 10);
234 sub_params.push_back(L"BLUE");
235 scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 110, 10);
238 sub_params.push_back(L"SP");
239 scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 50, 50);