Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_curves.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_curves.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **  Copyright (c) 2008 Gerco Ballintijn
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "widget_curves.h"
34 #include <cmath>
35 #include "app.h"
36 #include <gtkmm/drawingarea.h>
37 #include <map>
38 #include <vector>
39 #include <ETL/misc>
40 #include <sigc++/object.h>
41
42 #include "general.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace std;
49 using namespace etl;
50 using namespace synfig;
51 using namespace studio;
52
53 /* === M A C R O S ========================================================= */
54
55 /* === G L O B A L S ======================================================= */
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /*
60 void
61 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Color &color)
62 {
63         const int height(ca.get_height());
64         const int width(ca.get_width());
65
66         const int square_size(height/2);
67
68         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
69
70         if(color.get_alpha()!=1.0)
71         {
72                 // In this case we need to render the alpha squares
73
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());
76
77                 Gdk::Color gdk_c1(colorconv_synfig2gdk(bg1));
78                 Gdk::Color gdk_c2(colorconv_synfig2gdk(bg2));
79
80                 bool toggle(false);
81                 for(int i=0;i<width;i+=square_size)
82                 {
83                         const int square_width(min(square_size,width-i));
84
85                         if(toggle)
86                         {
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);
89
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);
92                                 toggle=false;
93                         }
94                         else
95                         {
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);
98
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);
101                                 toggle=true;
102                         }
103                 }
104         }
105         else
106         {
107                 // In this case we have a solid color to use
108                 Gdk::Color gdk_c1(colorconv_synfig2gdk(color));
109
110                 gc->set_rgb_fg_color(gdk_c1);
111                 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
112         }
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);
117 }
118 */
119
120 /* === C L A S S E S ======================================================= */
121
122
123
124 struct studio::Widget_Curves::Channel
125 {
126         synfig::String name;
127         Gdk::Color color;
128         std::map<synfig::Real,synfig::Real> values;
129 };
130
131 struct studio::Widget_Curves::CurveStruct : sigc::trackable
132 {
133         synfigapp::ValueDesc value_desc;
134         std::vector<Channel> channels;
135
136         CurveStruct(const synfigapp::ValueDesc& x):
137                 value_desc(x)
138         {
139                 switch(value_desc.get_value_type())
140                 {
141                         case ValueBase::TYPE_REAL:
142                                 channels.push_back(Channel());
143                                 channels.back().name="real";
144                                 channels.back().color=Gdk::Color("#007f7f");
145                                 break;
146                         case ValueBase::TYPE_TIME:
147                                 channels.push_back(Channel());
148                                 channels.back().name="time";
149                                 channels.back().color=Gdk::Color("#7f7f00");
150                                 break;
151                         case ValueBase::TYPE_INTEGER:
152                                 channels.push_back(Channel());
153                                 channels.back().name="int";
154                                 channels.back().color=Gdk::Color("#7f0000");
155                                 break;
156                         case ValueBase::TYPE_BOOL:
157                                 channels.push_back(Channel());
158                                 channels.back().name="bool";
159                                 channels.back().color=Gdk::Color("#ff7f00");
160                                 break;
161                         case ValueBase::TYPE_ANGLE:
162                                 channels.push_back(Channel());
163                                 channels.back().name="theta";
164                                 channels.back().color=Gdk::Color("#004f4f");
165                                 break;
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");
179                                 break;
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");
187                                 break;
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");
195
196                                 channels.push_back(Channel());
197                                 channels.back().name="width";
198                                 channels.back().color=Gdk::Color("#000000");
199
200                                 channels.push_back(Channel());
201                                 channels.back().name="origin";
202                                 channels.back().color=Gdk::Color("#ffffff");
203
204                                 channels.push_back(Channel());
205                                 channels.back().name="tsplit";
206                                 channels.back().color=Gdk::Color("#ff00ff");
207
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");
214
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");
221                                 break;
222                         default:
223                                 throw synfig::Exception::BadType("Bad type for curves");
224                 }
225         }
226
227         void clear_all_values()
228         {
229                 std::vector<Channel>::iterator iter;
230                 for(iter=channels.begin();iter!=channels.end();++iter)
231                         iter->values.clear();
232         }
233
234         synfig::Real get_value(int chan, synfig::Real time, synfig::Real tolerance)
235         {
236                 std::map<synfig::Real,synfig::Real>::iterator iter;
237
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;
244
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())
250                 {
251                         case ValueBase::TYPE_REAL:
252                                 channels[0].values[time]=value.get(Real());
253                                 break;
254                         case ValueBase::TYPE_TIME:
255                                 channels[0].values[time]=value.get(Time());
256                                 break;
257                         case ValueBase::TYPE_INTEGER:
258                                 channels[0].values[time]=value.get(int());
259                                 break;
260                         case ValueBase::TYPE_BOOL:
261                                 channels[0].values[time]=value.get(bool());
262                                 break;
263                         case ValueBase::TYPE_ANGLE:
264                                 channels[0].values[time]=Angle::rad(value.get(Angle())).get();
265                                 break;
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();
271                                 break;
272                         case ValueBase::TYPE_VECTOR:
273                                 channels[0].values[time]=value.get(Vector())[0];
274                                 channels[1].values[time]=value.get(Vector())[1];
275                                 break;
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];
286                                 break;
287                         default:
288                                 return 0;
289                 }
290
291                 return -channels[chan].values[time];
292         }
293
294         static bool is_not_supported(const synfigapp::ValueDesc& x)
295         {
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;
301         }
302 };
303
304 /* === M E T H O D S ======================================================= */
305
306 Widget_Curves::Widget_Curves():
307         range_adjustment_(new Gtk::Adjustment(-1,-2,2,0.1,0.1,2))
308 {
309         set_size_request(64,64);
310
311         range_adjustment_->signal_changed().connect(
312                 sigc::mem_fun(
313                         *this,
314                         &Widget_Curves::queue_draw
315                 )
316         );
317         range_adjustment_->signal_value_changed().connect(
318                 sigc::mem_fun(
319                         *this,
320                         &Widget_Curves::queue_draw
321                 )
322         );
323         //set_vadjustment(*range_adjustment_);
324
325         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Curves::redraw));
326         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
327
328 }
329
330 Widget_Curves::~Widget_Curves()
331 {
332 }
333
334 void
335 Widget_Curves::set_time_adjustment(Gtk::Adjustment&x)
336 {
337         time_adjustment_=&x;
338         time_adjustment_->signal_changed().connect(
339                 sigc::mem_fun(
340                         *this,
341                         &Widget_Curves::queue_draw
342                 )
343         );
344         time_adjustment_->signal_value_changed().connect(
345                 sigc::mem_fun(
346                         *this,
347                         &Widget_Curves::queue_draw
348                 )
349         );
350         //set_hadjustment(*time_adjustment_);
351 }
352
353 void
354 Widget_Curves::clear()
355 {
356         curve_list_.clear();
357 }
358
359 void
360 Widget_Curves::refresh()
361 {
362         std::list<CurveStruct>::iterator curve_iter;
363         for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
364         {
365                 curve_iter->clear_all_values();
366         }
367         queue_draw();
368 }
369
370 void
371 Widget_Curves::set_value_descs(std::list<synfigapp::ValueDesc> value_descs)
372 {
373         curve_list_.clear();
374
375         std::list<synfigapp::ValueDesc>::iterator iter;
376         for(iter=value_descs.begin();iter!=value_descs.end();++iter)
377         {
378                 if (CurveStruct::is_not_supported(*iter))
379                         continue;
380
381                 try {
382                         curve_list_.push_back(*iter);
383                         if(iter->is_value_node())
384                         {
385                                 iter->get_value_node()->signal_changed().connect(
386                                         sigc::mem_fun(
387                                                 *this,
388                                                 &studio::Widget_Curves::refresh
389                                         )
390                                 );
391                         }
392                         if(iter->parent_is_value_node())
393                         {
394                                 iter->get_parent_value_node()->signal_changed().connect(
395                                         sigc::mem_fun(
396                                                 *this,
397                                                 &studio::Widget_Curves::refresh
398                                         )
399                                 );
400                         }
401                         if(iter->parent_is_layer_param())
402                         {
403                                 iter->get_layer()->signal_changed().connect(
404                                         sigc::mem_fun(
405                                                 *this,
406                                                 &studio::Widget_Curves::refresh
407                                         )
408                                 );
409                         }
410                 }catch(synfig::Exception::BadType)
411                 {
412                         continue;
413                 }
414         }
415         queue_draw();
416 }
417
418 bool
419 Widget_Curves::on_event(GdkEvent *event)
420 {
421         switch(event->type)
422         {
423         case GDK_SCROLL:
424                 switch(event->scroll.direction)
425                 {
426                         case GDK_SCROLL_UP:
427                                 range_adjustment_->set_page_size(range_adjustment_->get_page_size()/1.25);
428                                 range_adjustment_->changed();
429                                 break;
430                         case GDK_SCROLL_DOWN:
431                                 range_adjustment_->set_page_size(range_adjustment_->get_page_size()*1.25);
432                                 range_adjustment_->changed();
433                                 break;
434                         default:
435                                 break;
436                 }
437                 break;
438         default:
439                 return Gtk::DrawingArea::on_event(event);
440                 break;
441         }
442
443         return true;
444
445 /*      switch(event->type)
446         {
447         case GDK_BUTTON_PRESS:
448                 if(event->button.button==1)
449                 {
450                         signal_activate_();
451                         return true;
452                 }
453                 if(event->button.button==3)
454                 {
455                         signal_secondary_();
456                         return true;
457                 }
458                 break;
459
460         default:
461                 break;
462         }
463         return false;
464 */
465 }
466
467 bool
468 Widget_Curves::redraw(GdkEventExpose */*bleh*/)
469 {
470         const int h(get_height());
471         const int w(get_width());
472         get_window()->clear();
473
474         if(!time_adjustment_ || !range_adjustment_ || !h || !w)
475                 return false;
476
477         if(!curve_list_.size())
478                 return false;
479
480         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
481
482         const Real t_begin(time_adjustment_->get_lower());
483         const Real t_end(time_adjustment_->get_upper());
484         const Real dt((t_end-t_begin)/w);
485
486         const Real r_bottom(range_adjustment_->get_value());
487         const Real r_top(r_bottom+range_adjustment_->get_page_size());
488         const Real dr((r_top-r_bottom)/h);
489         Real r_max(-100000000);
490         Real r_min(100000000);
491
492         std::list<CurveStruct>::iterator curve_iter;
493
494         vector<Gdk::Point> points[10];
495
496         gc->set_function(Gdk::COPY);
497         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
498
499         // Draw zero mark
500         gc->set_rgb_fg_color(Gdk::Color("#4f4f4f"));
501         get_window()->draw_rectangle(gc, false, 0, round_to_int((0-r_bottom)/dr), w, 0);
502
503         // This try to find a valid vanvas to show the keyframes of those
504         // valuenodes. If not canvas found then no keyframes marks are shown.
505         synfig::Canvas::Handle canvas=0;
506         for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
507         {
508                 canvas=curve_iter->value_desc.get_canvas();
509                 if(canvas)
510                         break;
511         }
512
513         if(canvas)
514         {
515         // Draw vertical lines for the keyframes marks.
516                 const synfig::KeyframeList& keyframe_list(canvas->keyframe_list());
517                 synfig::KeyframeList::const_iterator iter;
518
519                 for(iter=keyframe_list.begin();iter!=keyframe_list.end();++iter)
520                 {
521                         if(!iter->get_time().is_valid())
522                                 continue;
523
524                         const int x((int)((float)w/(t_end-t_begin)*(iter->get_time()-t_begin)));
525                         if(iter->get_time()>=t_begin && iter->get_time()<t_end)
526                         {
527                                 gc->set_rgb_fg_color(Gdk::Color("#a07f7f")); // It should be user selectable
528                                 get_window()->draw_rectangle(gc, true, x, 0, 1, h);
529                         }
530                 }
531         }
532
533         // Draw current time
534         gc->set_rgb_fg_color(Gdk::Color("#0000ff")); // It should be user selectable
535         get_window()->draw_rectangle(gc, false, round_to_int((time_adjustment_->get_value()-t_begin)/dt), 0, 0, h);
536
537         // Draw curves for the valuenodes stored in the curve list
538         for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
539         {
540                 Real t;
541                 int i;
542                 int channels(curve_iter->channels.size());
543                 for(i=0;i<channels;i++)
544                         points[i].clear();
545
546                 for(i=0,t=t_begin;i<w;i++,t+=dt)
547                 {
548                         for(int chan=0;chan<channels;chan++)
549                         {
550                                 Real x(curve_iter->get_value(chan,t,dt));
551                                 r_max=max(r_max,x);
552                                 r_min=min(r_min,x);
553                                 points[chan].push_back(
554                                         Gdk::Point(
555                                                 i,
556                                                 round_to_int(
557                                                         (
558                                                                 x-r_bottom
559                                                         )/dr
560                                                 )
561                                         )
562                                 );
563                         }
564                 }
565
566                 for(int chan=0;chan<channels;chan++)
567                 {
568                         gc->set_rgb_fg_color(curve_iter->channels[chan].color);
569
570                         // Draw the curve
571                         get_window()->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points[chan]));
572
573                         Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
574
575                         layout->set_text(curve_iter->channels[chan].name);
576                         get_window()->draw_layout(gc, 1, points[chan][0].get_y()+1, layout);
577                 }
578         }
579
580         if(!curve_list_.empty())
581         {
582                 range_adjustment_->set_upper(r_max+range_adjustment_->get_page_size()/2);
583                 range_adjustment_->set_lower(r_min-range_adjustment_->get_page_size()/2);
584         }
585         get_window()->get_update_area();
586
587         return true;
588 }