1 /* === S Y N F I G ========================================================= */
2 /*! \file widget_curves.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Gerco Ballintijn
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "widget_curves.h"
36 #include <gtkmm/drawingarea.h>
40 #include <sigc++/object.h>
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
51 using namespace studio;
53 /* === M A C R O S ========================================================= */
55 /* === G L O B A L S ======================================================= */
57 /* === P R O C E D U R E S ================================================= */
61 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Color &color)
63 const int height(ca.get_height());
64 const int width(ca.get_width());
66 const int square_size(height/2);
68 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
70 if(color.get_alpha()!=1.0)
72 // In this case we need to render the alpha squares
74 const Color bg1(Color::blend(color,Color(0.75, 0.75, 0.75),1.0).clamped());
75 const Color bg2(Color::blend(color,Color(0.5, 0.5, 0.5),1.0).clamped());
77 Gdk::Color gdk_c1(colorconv_synfig2gdk(bg1));
78 Gdk::Color gdk_c2(colorconv_synfig2gdk(bg2));
81 for(int i=0;i<width;i+=square_size)
83 const int square_width(min(square_size,width-i));
87 gc->set_rgb_fg_color(gdk_c1);
88 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
90 gc->set_rgb_fg_color(gdk_c2);
91 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
96 gc->set_rgb_fg_color(gdk_c2);
97 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
99 gc->set_rgb_fg_color(gdk_c1);
100 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
107 // In this case we have a solid color to use
108 Gdk::Color gdk_c1(colorconv_synfig2gdk(color));
110 gc->set_rgb_fg_color(gdk_c1);
111 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
113 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
114 window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
115 gc->set_rgb_fg_color(Gdk::Color("#000000"));
116 window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
120 /* === C L A S S E S ======================================================= */
124 struct studio::Widget_Curves::Channel
128 std::map<synfig::Real,synfig::Real> values;
131 struct studio::Widget_Curves::CurveStruct : sigc::trackable
133 synfigapp::ValueDesc value_desc;
134 std::vector<Channel> channels;
136 CurveStruct(const synfigapp::ValueDesc& x):
139 switch(value_desc.get_value_type())
141 case ValueBase::TYPE_REAL:
142 channels.push_back(Channel());
143 channels.back().name="real";
144 channels.back().color=Gdk::Color("#007f7f");
146 case ValueBase::TYPE_TIME:
147 channels.push_back(Channel());
148 channels.back().name="time";
149 channels.back().color=Gdk::Color("#7f7f00");
151 case ValueBase::TYPE_INTEGER:
152 channels.push_back(Channel());
153 channels.back().name="int";
154 channels.back().color=Gdk::Color("#7f0000");
156 case ValueBase::TYPE_BOOL:
157 channels.push_back(Channel());
158 channels.back().name="bool";
159 channels.back().color=Gdk::Color("#ff7f00");
161 case ValueBase::TYPE_ANGLE:
162 channels.push_back(Channel());
163 channels.back().name="theta";
164 channels.back().color=Gdk::Color("#004f4f");
166 case ValueBase::TYPE_COLOR:
167 channels.push_back(Channel());
168 channels.back().name="red";
169 channels.back().color=Gdk::Color("#7f0000");
170 channels.push_back(Channel());
171 channels.back().name="green";
172 channels.back().color=Gdk::Color("#007f00");
173 channels.push_back(Channel());
174 channels.back().name="blue";
175 channels.back().color=Gdk::Color("#00007f");
176 channels.push_back(Channel());
177 channels.back().name="alpha";
178 channels.back().color=Gdk::Color("#000000");
180 case ValueBase::TYPE_VECTOR:
181 channels.push_back(Channel());
182 channels.back().name="x";
183 channels.back().color=Gdk::Color("#7f007f");
184 channels.push_back(Channel());
185 channels.back().name="y";
186 channels.back().color=Gdk::Color("#007f7f");
188 case ValueBase::TYPE_BLINEPOINT:
189 channels.push_back(Channel());
190 channels.back().name="v.x";
191 channels.back().color=Gdk::Color("#ff7f00");
192 channels.push_back(Channel());
193 channels.back().name="v.y";
194 channels.back().color=Gdk::Color("#7f3f00");
196 channels.push_back(Channel());
197 channels.back().name="width";
198 channels.back().color=Gdk::Color("#000000");
200 channels.push_back(Channel());
201 channels.back().name="origin";
202 channels.back().color=Gdk::Color("#ffffff");
204 channels.push_back(Channel());
205 channels.back().name="tsplit";
206 channels.back().color=Gdk::Color("#ff00ff");
208 channels.push_back(Channel());
209 channels.back().name="t1.x";
210 channels.back().color=Gdk::Color("#ff0000");
211 channels.push_back(Channel());
212 channels.back().name="t1.y";
213 channels.back().color=Gdk::Color("#7f0000");
215 channels.push_back(Channel());
216 channels.back().name="t2.x";
217 channels.back().color=Gdk::Color("#ffff00");
218 channels.push_back(Channel());
219 channels.back().name="t2.y";
220 channels.back().color=Gdk::Color("#7f7f00");
223 throw synfig::Exception::BadType("Bad type for curves");
227 void clear_all_values()
229 std::vector<Channel>::iterator iter;
230 for(iter=channels.begin();iter!=channels.end();++iter)
231 iter->values.clear();
234 synfig::Real get_value(int chan, synfig::Real time, synfig::Real tolerance)
236 std::map<synfig::Real,synfig::Real>::iterator iter;
238 // First check to see if we have a value
239 // that is "close enough" to the time
240 // we are looking for
241 iter=channels[chan].values.lower_bound(time);
242 if(iter!=channels[chan].values.end() && iter->first-time<=tolerance)
243 return -iter->second;
245 // Since that didn't work, we now need
246 // to go ahead and figure out what the
247 // actual value is at that time.
248 ValueBase value(value_desc.get_value(time));
249 switch(value.get_type())
251 case ValueBase::TYPE_REAL:
252 channels[0].values[time]=value.get(Real());
254 case ValueBase::TYPE_TIME:
255 channels[0].values[time]=value.get(Time());
257 case ValueBase::TYPE_INTEGER:
258 channels[0].values[time]=value.get(int());
260 case ValueBase::TYPE_BOOL:
261 channels[0].values[time]=value.get(bool());
263 case ValueBase::TYPE_ANGLE:
264 channels[0].values[time]=Angle::rad(value.get(Angle())).get();
266 case ValueBase::TYPE_COLOR:
267 channels[0].values[time]=value.get(Color()).get_r();
268 channels[1].values[time]=value.get(Color()).get_g();
269 channels[2].values[time]=value.get(Color()).get_b();
270 channels[3].values[time]=value.get(Color()).get_a();
272 case ValueBase::TYPE_VECTOR:
273 channels[0].values[time]=value.get(Vector())[0];
274 channels[1].values[time]=value.get(Vector())[1];
276 case ValueBase::TYPE_BLINEPOINT:
277 channels[0].values[time]=value.get(BLinePoint()).get_vertex()[0];
278 channels[1].values[time]=value.get(BLinePoint()).get_vertex()[1];
279 channels[2].values[time]=value.get(BLinePoint()).get_width();
280 channels[3].values[time]=value.get(BLinePoint()).get_origin();
281 channels[4].values[time]=value.get(BLinePoint()).get_split_tangent_flag();
282 channels[5].values[time]=value.get(BLinePoint()).get_tangent1()[0];
283 channels[6].values[time]=value.get(BLinePoint()).get_tangent1()[1];
284 channels[7].values[time]=value.get(BLinePoint()).get_tangent2()[0];
285 channels[8].values[time]=value.get(BLinePoint()).get_tangent2()[1];
291 return -channels[chan].values[time];
294 static bool is_not_supported(const synfigapp::ValueDesc& x)
296 return x.get_value_type() == ValueBase::TYPE_STRING
297 || x.get_value_type() == ValueBase::TYPE_CANVAS
298 || x.get_value_type() == ValueBase::TYPE_GRADIENT
299 || x.get_value_type() == ValueBase::TYPE_LIST
300 || x.get_value_type() == ValueBase::TYPE_SEGMENT;
304 /* === M E T H O D S ======================================================= */
306 Widget_Curves::Widget_Curves():
307 range_adjustment_(new Gtk::Adjustment(-1,-2,2,0.1,0.1,2))
309 set_size_request(64,64);
311 range_adjustment_->signal_changed().connect(
314 &Widget_Curves::queue_draw
317 range_adjustment_->signal_value_changed().connect(
320 &Widget_Curves::queue_draw
323 //set_vadjustment(*range_adjustment_);
325 signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Curves::redraw));
326 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
330 Widget_Curves::~Widget_Curves()
335 Widget_Curves::set_time_adjustment(Gtk::Adjustment&x)
338 time_adjustment_->signal_changed().connect(
341 &Widget_Curves::queue_draw
344 time_adjustment_->signal_value_changed().connect(
347 &Widget_Curves::queue_draw
350 //set_hadjustment(*time_adjustment_);
354 Widget_Curves::clear()
360 Widget_Curves::refresh()
362 std::list<CurveStruct>::iterator curve_iter;
363 for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
365 curve_iter->clear_all_values();
371 Widget_Curves::set_value_descs(std::list<synfigapp::ValueDesc> value_descs)
375 std::list<synfigapp::ValueDesc>::iterator iter;
376 for(iter=value_descs.begin();iter!=value_descs.end();++iter)
378 if (CurveStruct::is_not_supported(*iter))
382 curve_list_.push_back(*iter);
383 if(iter->is_value_node())
385 iter->get_value_node()->signal_changed().connect(
388 &studio::Widget_Curves::refresh
392 if(iter->parent_is_value_node())
394 iter->get_parent_value_node()->signal_changed().connect(
397 &studio::Widget_Curves::refresh
401 if(iter->parent_is_layer_param())
403 iter->get_layer()->signal_changed().connect(
406 &studio::Widget_Curves::refresh
410 }catch(synfig::Exception::BadType)
419 Widget_Curves::on_event(GdkEvent *event)
424 switch(event->scroll.direction)
427 range_adjustment_->set_page_size(range_adjustment_->get_page_size()/1.25);
428 range_adjustment_->changed();
430 case GDK_SCROLL_DOWN:
431 range_adjustment_->set_page_size(range_adjustment_->get_page_size()*1.25);
432 range_adjustment_->changed();
439 return Gtk::DrawingArea::on_event(event);
445 /* switch(event->type)
447 case GDK_BUTTON_PRESS:
448 if(event->button.button==1)
453 if(event->button.button==3)
468 Widget_Curves::redraw(GdkEventExpose */*bleh*/)
470 const int h(get_height());
471 const int w(get_width());
472 get_window()->clear();
474 if(!time_adjustment_ || !range_adjustment_ || !h || !w)
477 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
479 const Real t_begin(time_adjustment_->get_lower());
480 const Real t_end(time_adjustment_->get_upper());
481 const Real dt((t_end-t_begin)/w);
483 const Real r_bottom(range_adjustment_->get_value());
484 const Real r_top(r_bottom+range_adjustment_->get_page_size());
485 const Real dr((r_top-r_bottom)/h);
486 Real r_max(-100000000);
487 Real r_min(100000000);
489 std::list<CurveStruct>::iterator curve_iter;
491 vector<Gdk::Point> points[10];
493 gc->set_function(Gdk::COPY);
494 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
497 gc->set_rgb_fg_color(Gdk::Color("#4f4f4f"));
498 get_window()->draw_rectangle(gc, false, 0, round_to_int((0-r_bottom)/dr), w, 0);
501 gc->set_rgb_fg_color(Gdk::Color("#00007f"));
502 get_window()->draw_rectangle(gc, false, round_to_int((time_adjustment_->get_value()-t_begin)/dt), 0, 0, h);
504 for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
508 int channels(curve_iter->channels.size());
509 for(i=0;i<channels;i++)
512 for(i=0,t=t_begin;i<w;i++,t+=dt)
514 for(int chan=0;chan<channels;chan++)
516 Real x(curve_iter->get_value(chan,t,dt));
519 points[chan].push_back(
532 for(int chan=0;chan<channels;chan++)
534 gc->set_rgb_fg_color(curve_iter->channels[chan].color);
537 get_window()->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points[chan]));
539 Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
541 layout->set_text(curve_iter->channels[chan].name);
542 get_window()->draw_layout(gc, 1, points[chan][0].get_y()+1, layout);
546 if(!curve_list_.empty())
548 range_adjustment_->set_upper(r_max+range_adjustment_->get_page_size()/2);
549 range_adjustment_->set_lower(r_min-range_adjustment_->get_page_size()/2);
551 get_window()->get_update_area();