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
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "widget_curves.h"
35 #include <gtkmm/drawingarea.h>
39 #include <sigc++/object.h>
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
50 using namespace studio;
52 /* === M A C R O S ========================================================= */
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
60 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Color &color)
62 const int height(ca.get_height());
63 const int width(ca.get_width());
65 const int square_size(height/2);
67 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
69 if(color.get_alpha()!=1.0)
71 // In this case we need to render the alpha squares
73 const Color bg1(Color::blend(color,Color(0.75, 0.75, 0.75),1.0).clamped());
74 const Color bg2(Color::blend(color,Color(0.5, 0.5, 0.5),1.0).clamped());
76 Gdk::Color gdk_c1(colorconv_synfig2gdk(bg1));
77 Gdk::Color gdk_c2(colorconv_synfig2gdk(bg2));
80 for(int i=0;i<width;i+=square_size)
82 const int square_width(min(square_size,width-i));
86 gc->set_rgb_fg_color(gdk_c1);
87 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
89 gc->set_rgb_fg_color(gdk_c2);
90 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
95 gc->set_rgb_fg_color(gdk_c2);
96 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
98 gc->set_rgb_fg_color(gdk_c1);
99 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
106 // In this case we have a solid color to use
107 Gdk::Color gdk_c1(colorconv_synfig2gdk(color));
109 gc->set_rgb_fg_color(gdk_c1);
110 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
112 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
113 window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
114 gc->set_rgb_fg_color(Gdk::Color("#000000"));
115 window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
119 /* === C L A S S E S ======================================================= */
123 struct studio::Widget_Curves::Channel
127 std::map<synfig::Real,synfig::Real> values;
130 struct studio::Widget_Curves::CurveStruct : sigc::trackable
132 synfigapp::ValueDesc value_desc;
133 std::vector<Channel> channels;
135 CurveStruct(const synfigapp::ValueDesc& x):
138 switch(value_desc.get_value_type())
140 case ValueBase::TYPE_REAL:
141 channels.push_back(Channel());
142 channels.back().name="real";
143 channels.back().color=Gdk::Color("#007f7f");
145 case ValueBase::TYPE_TIME:
146 channels.push_back(Channel());
147 channels.back().name="time";
148 channels.back().color=Gdk::Color("#7f7f00");
150 case ValueBase::TYPE_INTEGER:
151 channels.push_back(Channel());
152 channels.back().name="int";
153 channels.back().color=Gdk::Color("#7f0000");
155 case ValueBase::TYPE_BOOL:
156 channels.push_back(Channel());
157 channels.back().name="bool";
158 channels.back().color=Gdk::Color("#ff7f00");
160 case ValueBase::TYPE_ANGLE:
161 channels.push_back(Channel());
162 channels.back().name="theta";
163 channels.back().color=Gdk::Color("#004f4f");
165 case ValueBase::TYPE_COLOR:
166 channels.push_back(Channel());
167 channels.back().name="red";
168 channels.back().color=Gdk::Color("#7f0000");
169 channels.push_back(Channel());
170 channels.back().name="green";
171 channels.back().color=Gdk::Color("#007f00");
172 channels.push_back(Channel());
173 channels.back().name="blue";
174 channels.back().color=Gdk::Color("#00007f");
175 channels.push_back(Channel());
176 channels.back().name="alpha";
177 channels.back().color=Gdk::Color("#000000");
179 case ValueBase::TYPE_VECTOR:
180 channels.push_back(Channel());
181 channels.back().name="x";
182 channels.back().color=Gdk::Color("#7f007f");
183 channels.push_back(Channel());
184 channels.back().name="y";
185 channels.back().color=Gdk::Color("#007f7f");
187 case ValueBase::TYPE_BLINEPOINT:
188 channels.push_back(Channel());
189 channels.back().name="v.x";
190 channels.back().color=Gdk::Color("#ff7f00");
191 channels.push_back(Channel());
192 channels.back().name="v.y";
193 channels.back().color=Gdk::Color("#7f3f00");
195 channels.push_back(Channel());
196 channels.back().name="width";
197 channels.back().color=Gdk::Color("#000000");
199 channels.push_back(Channel());
200 channels.back().name="origin";
201 channels.back().color=Gdk::Color("#ffffff");
203 channels.push_back(Channel());
204 channels.back().name="tsplit";
205 channels.back().color=Gdk::Color("#ff00ff");
207 channels.push_back(Channel());
208 channels.back().name="t1.x";
209 channels.back().color=Gdk::Color("#ff0000");
210 channels.push_back(Channel());
211 channels.back().name="t1.y";
212 channels.back().color=Gdk::Color("#7f0000");
214 channels.push_back(Channel());
215 channels.back().name="t2.x";
216 channels.back().color=Gdk::Color("#ffff00");
217 channels.push_back(Channel());
218 channels.back().name="t2.y";
219 channels.back().color=Gdk::Color("#7f7f00");
222 throw synfig::Exception::BadType("Bad type for curves");
226 void clear_all_values()
228 std::vector<Channel>::iterator iter;
229 for(iter=channels.begin();iter!=channels.end();++iter)
230 iter->values.clear();
233 synfig::Real get_value(int chan, synfig::Real time, synfig::Real tolerance)
235 std::map<synfig::Real,synfig::Real>::iterator iter;
237 // First check to see if we have a value
238 // that is "close enough" to the time
239 // we are looking for
240 iter=channels[chan].values.lower_bound(time);
241 if(iter!=channels[chan].values.end() && iter->first-time<=tolerance)
242 return -iter->second;
244 // Since that didn't work, we now need
245 // to go ahead and figure out what the
246 // actual value is at that time.
247 ValueBase value(value_desc.get_value(time));
248 switch(value.get_type())
250 case ValueBase::TYPE_REAL:
251 channels[0].values[time]=value.get(Real());
253 case ValueBase::TYPE_TIME:
254 channels[0].values[time]=value.get(Time());
256 case ValueBase::TYPE_INTEGER:
257 channels[0].values[time]=value.get(int());
259 case ValueBase::TYPE_BOOL:
260 channels[0].values[time]=value.get(bool());
262 case ValueBase::TYPE_ANGLE:
263 channels[0].values[time]=Angle::rad(value.get(Angle())).get();
265 case ValueBase::TYPE_COLOR:
266 channels[0].values[time]=value.get(Color()).get_r();
267 channels[1].values[time]=value.get(Color()).get_g();
268 channels[2].values[time]=value.get(Color()).get_b();
269 channels[3].values[time]=value.get(Color()).get_a();
271 case ValueBase::TYPE_VECTOR:
272 channels[0].values[time]=value.get(Vector())[0];
273 channels[1].values[time]=value.get(Vector())[1];
275 case ValueBase::TYPE_BLINEPOINT:
276 channels[0].values[time]=value.get(BLinePoint()).get_vertex()[0];
277 channels[1].values[time]=value.get(BLinePoint()).get_vertex()[1];
278 channels[2].values[time]=value.get(BLinePoint()).get_width();
279 channels[3].values[time]=value.get(BLinePoint()).get_origin();
280 channels[4].values[time]=value.get(BLinePoint()).get_split_tangent_flag();
281 channels[5].values[time]=value.get(BLinePoint()).get_tangent1()[0];
282 channels[6].values[time]=value.get(BLinePoint()).get_tangent1()[1];
283 channels[7].values[time]=value.get(BLinePoint()).get_tangent2()[0];
284 channels[8].values[time]=value.get(BLinePoint()).get_tangent2()[1];
290 return -channels[chan].values[time];
293 static bool is_supported(const synfigapp::ValueDesc& x)
295 return x.get_value_type() == ValueBase::TYPE_STRING
296 && x.get_value_type() == ValueBase::TYPE_CANVAS;
300 /* === M E T H O D S ======================================================= */
302 Widget_Curves::Widget_Curves():
303 range_adjustment_(new Gtk::Adjustment(-1,-2,2,0.1,0.1,2))
305 set_size_request(64,64);
307 range_adjustment_->signal_changed().connect(
310 &Widget_Curves::queue_draw
313 range_adjustment_->signal_value_changed().connect(
316 &Widget_Curves::queue_draw
319 //set_vadjustment(*range_adjustment_);
321 signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Curves::redraw));
322 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
326 Widget_Curves::~Widget_Curves()
331 Widget_Curves::set_time_adjustment(Gtk::Adjustment&x)
334 time_adjustment_->signal_changed().connect(
337 &Widget_Curves::queue_draw
340 time_adjustment_->signal_value_changed().connect(
343 &Widget_Curves::queue_draw
346 //set_hadjustment(*time_adjustment_);
350 Widget_Curves::clear()
356 Widget_Curves::refresh()
358 std::list<CurveStruct>::iterator curve_iter;
359 for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
361 curve_iter->clear_all_values();
367 Widget_Curves::set_value_descs(std::list<synfigapp::ValueDesc> value_descs)
371 std::list<synfigapp::ValueDesc>::iterator iter;
372 for(iter=value_descs.begin();iter!=value_descs.end();++iter)
374 if (! CurveStruct::is_supported(*iter))
378 curve_list_.push_back(*iter);
379 if(iter->is_value_node())
381 iter->get_value_node()->signal_changed().connect(
384 &studio::Widget_Curves::refresh
388 if(iter->parent_is_value_node())
390 iter->get_parent_value_node()->signal_changed().connect(
393 &studio::Widget_Curves::refresh
397 if(iter->parent_is_layer_param())
399 iter->get_layer()->signal_changed().connect(
402 &studio::Widget_Curves::refresh
406 }catch(synfig::Exception::BadType)
415 Widget_Curves::on_event(GdkEvent *event)
420 switch(event->scroll.direction)
423 range_adjustment_->set_page_size(range_adjustment_->get_page_size()/1.25);
424 range_adjustment_->changed();
426 case GDK_SCROLL_DOWN:
427 range_adjustment_->set_page_size(range_adjustment_->get_page_size()*1.25);
428 range_adjustment_->changed();
435 return Gtk::DrawingArea::on_event(event);
441 /* switch(event->type)
443 case GDK_BUTTON_PRESS:
444 if(event->button.button==1)
449 if(event->button.button==3)
464 Widget_Curves::redraw(GdkEventExpose */*bleh*/)
466 const int h(get_height());
467 const int w(get_width());
468 get_window()->clear();
470 if(!time_adjustment_ || !range_adjustment_ || !h || !w)
473 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
475 const Real t_begin(time_adjustment_->get_lower());
476 const Real t_end(time_adjustment_->get_upper());
477 const Real dt((t_end-t_begin)/w);
479 const Real r_bottom(range_adjustment_->get_value());
480 const Real r_top(r_bottom+range_adjustment_->get_page_size());
481 const Real dr((r_top-r_bottom)/h);
482 Real r_max(-100000000);
483 Real r_min(100000000);
485 std::list<CurveStruct>::iterator curve_iter;
487 vector<Gdk::Point> points[10];
489 gc->set_function(Gdk::COPY);
490 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
493 gc->set_rgb_fg_color(Gdk::Color("#4f4f4f"));
494 get_window()->draw_rectangle(gc, false, 0, round_to_int((0-r_bottom)/dr), w, 0);
497 gc->set_rgb_fg_color(Gdk::Color("#00007f"));
498 get_window()->draw_rectangle(gc, false, round_to_int((time_adjustment_->get_value()-t_begin)/dt), 0, 0, h);
500 for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
504 int channels(curve_iter->channels.size());
505 for(i=0;i<channels;i++)
508 for(i=0,t=t_begin;i<w;i++,t+=dt)
510 for(int chan=0;chan<channels;chan++)
512 Real x(curve_iter->get_value(chan,t,dt));
515 points[chan].push_back(
528 for(int chan=0;chan<channels;chan++)
530 gc->set_rgb_fg_color(curve_iter->channels[chan].color);
533 get_window()->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points[chan]));
535 Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
537 layout->set_text(curve_iter->channels[chan].name);
538 get_window()->draw_layout(gc, 1, points[chan][0].get_y()+1, layout);
542 if(!curve_list_.empty())
544 range_adjustment_->set_upper(r_max+range_adjustment_->get_page_size()/2);
545 range_adjustment_->set_lower(r_min-range_adjustment_->get_page_size()/2);
547 get_window()->get_update_area();