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