1 /* === S I N F G =========================================================== */
2 /*! \file widget_curves.cpp
3 ** \brief Template File
5 ** $Id: widget_curves.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "widget_curves.h"
34 #include <gtkmm/drawingarea.h>
38 #include <sigc++/object.h>
42 /* === U S I N G =========================================================== */
46 using namespace sinfg;
47 using namespace studio;
49 /* === M A C R O S ========================================================= */
51 /* === G L O B A L S ======================================================= */
53 /* === P R O C E D U R E S ================================================= */
57 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const sinfg::Color &color)
59 const int height(ca.get_height());
60 const int width(ca.get_width());
62 const int square_size(height/2);
64 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
66 if(color.get_alpha()!=1.0)
68 // In this case we need to render the alpha squares
70 const Color bg1(Color::blend(color,Color(0.75, 0.75, 0.75),1.0).clamped());
71 const Color bg2(Color::blend(color,Color(0.5, 0.5, 0.5),1.0).clamped());
73 Gdk::Color gdk_c1(colorconv_sinfg2gdk(bg1));
74 Gdk::Color gdk_c2(colorconv_sinfg2gdk(bg2));
77 for(int i=0;i<width;i+=square_size)
79 const int square_width(min(square_size,width-i));
83 gc->set_rgb_fg_color(gdk_c1);
84 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
86 gc->set_rgb_fg_color(gdk_c2);
87 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
92 gc->set_rgb_fg_color(gdk_c2);
93 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
95 gc->set_rgb_fg_color(gdk_c1);
96 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
103 // In this case we have a solid color to use
104 Gdk::Color gdk_c1(colorconv_sinfg2gdk(color));
106 gc->set_rgb_fg_color(gdk_c1);
107 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
109 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
110 window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
111 gc->set_rgb_fg_color(Gdk::Color("#000000"));
112 window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
116 /* === C L A S S E S ======================================================= */
120 struct studio::Widget_Curves::Channel
124 std::map<sinfg::Real,sinfg::Real> values;
127 struct studio::Widget_Curves::CurveStruct : sigc::trackable
129 sinfgapp::ValueDesc value_desc;
130 std::vector<Channel> channels;
132 CurveStruct(const sinfgapp::ValueDesc& x):
135 switch(value_desc.get_value_type())
137 case ValueBase::TYPE_REAL:
138 channels.push_back(Channel());
139 channels.back().name="real";
140 channels.back().color=Gdk::Color("#007f7f");
142 case ValueBase::TYPE_TIME:
143 channels.push_back(Channel());
144 channels.back().name="time";
145 channels.back().color=Gdk::Color("#7f7f00");
147 case ValueBase::TYPE_INTEGER:
148 channels.push_back(Channel());
149 channels.back().name="int";
150 channels.back().color=Gdk::Color("#7f0000");
152 case ValueBase::TYPE_BOOL:
153 channels.push_back(Channel());
154 channels.back().name="bool";
155 channels.back().color=Gdk::Color("#ff7f00");
157 case ValueBase::TYPE_ANGLE:
158 channels.push_back(Channel());
159 channels.back().name="theta";
160 channels.back().color=Gdk::Color("#004f4f");
162 case ValueBase::TYPE_COLOR:
163 channels.push_back(Channel());
164 channels.back().name="red";
165 channels.back().color=Gdk::Color("#7f0000");
166 channels.push_back(Channel());
167 channels.back().name="green";
168 channels.back().color=Gdk::Color("#007f00");
169 channels.push_back(Channel());
170 channels.back().name="blue";
171 channels.back().color=Gdk::Color("#00007f");
172 channels.push_back(Channel());
173 channels.back().name="alpha";
174 channels.back().color=Gdk::Color("#000000");
176 case ValueBase::TYPE_VECTOR:
177 channels.push_back(Channel());
178 channels.back().name="x";
179 channels.back().color=Gdk::Color("#7f007f");
180 channels.push_back(Channel());
181 channels.back().name="y";
182 channels.back().color=Gdk::Color("#007f7f");
184 case ValueBase::TYPE_BLINEPOINT:
185 channels.push_back(Channel());
186 channels.back().name="v.x";
187 channels.back().color=Gdk::Color("#ff7f00");
188 channels.push_back(Channel());
189 channels.back().name="v.y";
190 channels.back().color=Gdk::Color("#7f3f00");
192 channels.push_back(Channel());
193 channels.back().name="width";
194 channels.back().color=Gdk::Color("#000000");
196 channels.push_back(Channel());
197 channels.back().name="origin";
198 channels.back().color=Gdk::Color("#ffffff");
200 channels.push_back(Channel());
201 channels.back().name="tsplit";
202 channels.back().color=Gdk::Color("#ff00ff");
204 channels.push_back(Channel());
205 channels.back().name="t1.x";
206 channels.back().color=Gdk::Color("#ff0000");
207 channels.push_back(Channel());
208 channels.back().name="t1.y";
209 channels.back().color=Gdk::Color("#7f0000");
211 channels.push_back(Channel());
212 channels.back().name="t2.x";
213 channels.back().color=Gdk::Color("#ffff00");
214 channels.push_back(Channel());
215 channels.back().name="t2.y";
216 channels.back().color=Gdk::Color("#7f7f00");
219 throw sinfg::Exception::BadType("Bad type for curves");
223 void clear_all_values()
226 std::vector<Channel>::iterator iter;
227 for(iter=channels.begin();iter!=channels.end();++iter)
228 iter->values.clear();
231 sinfg::Real get_value(int chan, sinfg::Real time, sinfg::Real tolerance)
233 std::map<sinfg::Real,sinfg::Real>::iterator iter;
235 // First check to see if we have a value
236 // that is "close enough" to the time
237 // we are looking for
238 iter=channels[chan].values.lower_bound(time);
239 if(iter!=channels[chan].values.end() && iter->first-time<=tolerance)
240 return -iter->second;
242 // Since that didn't work, we now need
243 // to go ahead and figure out what the
244 // actual value is at that time.
245 ValueBase value(value_desc.get_value(time));
246 switch(value.get_type())
248 case ValueBase::TYPE_REAL:
249 channels[0].values[time]=value.get(Real());
251 case ValueBase::TYPE_TIME:
252 channels[0].values[time]=value.get(Time());
254 case ValueBase::TYPE_INTEGER:
255 channels[0].values[time]=value.get(int());
257 case ValueBase::TYPE_BOOL:
258 channels[0].values[time]=value.get(bool());
260 case ValueBase::TYPE_ANGLE:
261 channels[0].values[time]=Angle::rad(value.get(Angle())).get();
263 case ValueBase::TYPE_COLOR:
264 channels[0].values[time]=value.get(Color()).get_r();
265 channels[1].values[time]=value.get(Color()).get_g();
266 channels[2].values[time]=value.get(Color()).get_b();
267 channels[3].values[time]=value.get(Color()).get_a();
269 case ValueBase::TYPE_VECTOR:
270 channels[0].values[time]=value.get(Vector())[0];
271 channels[1].values[time]=value.get(Vector())[1];
273 case ValueBase::TYPE_BLINEPOINT:
274 channels[0].values[time]=value.get(BLinePoint()).get_vertex()[0];
275 channels[1].values[time]=value.get(BLinePoint()).get_vertex()[1];
276 channels[2].values[time]=value.get(BLinePoint()).get_width();
277 channels[3].values[time]=value.get(BLinePoint()).get_origin();
278 channels[4].values[time]=value.get(BLinePoint()).get_split_tangent_flag();
279 channels[5].values[time]=value.get(BLinePoint()).get_tangent1()[0];
280 channels[6].values[time]=value.get(BLinePoint()).get_tangent1()[1];
281 channels[7].values[time]=value.get(BLinePoint()).get_tangent2()[0];
282 channels[8].values[time]=value.get(BLinePoint()).get_tangent2()[1];
288 return -channels[chan].values[time];
292 /* === M E T H O D S ======================================================= */
294 Widget_Curves::Widget_Curves():
295 range_adjustment_(new Gtk::Adjustment(-1,-2,2,0.1,0.1,2))
297 set_size_request(64,64);
299 range_adjustment_->signal_changed().connect(
302 &Widget_Curves::queue_draw
305 range_adjustment_->signal_value_changed().connect(
308 &Widget_Curves::queue_draw
311 //set_vadjustment(*range_adjustment_);
313 signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Curves::redraw));
314 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
318 Widget_Curves::~Widget_Curves()
323 Widget_Curves::set_time_adjustment(Gtk::Adjustment&x)
326 time_adjustment_->signal_changed().connect(
329 &Widget_Curves::queue_draw
332 time_adjustment_->signal_value_changed().connect(
335 &Widget_Curves::queue_draw
338 //set_hadjustment(*time_adjustment_);
342 Widget_Curves::clear()
348 Widget_Curves::refresh()
350 std::list<CurveStruct>::iterator curve_iter;
351 for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
353 curve_iter->clear_all_values();
359 Widget_Curves::set_value_descs(std::list<sinfgapp::ValueDesc> value_descs)
363 std::list<sinfgapp::ValueDesc>::iterator iter;
364 for(iter=value_descs.begin();iter!=value_descs.end();++iter)
367 curve_list_.push_back(*iter);
368 if(iter->is_value_node())
371 iter->get_value_node()->signal_changed().connect(
374 &studio::Widget_Curves::refresh
378 if(iter->parent_is_value_node())
381 iter->get_parent_value_node()->signal_changed().connect(
384 &studio::Widget_Curves::refresh
388 if(iter->parent_is_layer_param())
391 iter->get_layer()->signal_changed().connect(
394 &studio::Widget_Curves::refresh
398 }catch(sinfg::Exception::BadType)
407 Widget_Curves::on_event(GdkEvent *event)
412 switch(event->scroll.direction)
415 range_adjustment_->set_page_size(range_adjustment_->get_page_size()/1.25);
416 range_adjustment_->changed();
418 case GDK_SCROLL_DOWN:
419 range_adjustment_->set_page_size(range_adjustment_->get_page_size()*1.25);
420 range_adjustment_->changed();
427 return Gtk::DrawingArea::on_event(event);
433 /* switch(event->type)
435 case GDK_BUTTON_PRESS:
436 if(event->button.button==1)
441 if(event->button.button==3)
456 Widget_Curves::redraw(GdkEventExpose*bleh)
458 const int h(get_height());
459 const int w(get_width());
460 get_window()->clear();
462 if(!time_adjustment_ || !range_adjustment_ || !h || !w)
465 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
467 const Real t_begin(time_adjustment_->get_lower());
468 const Real t_end(time_adjustment_->get_upper());
469 const Real dt((t_end-t_begin)/w);
471 const Real r_bottom(range_adjustment_->get_value());
472 const Real r_top(r_bottom+range_adjustment_->get_page_size());
473 const Real dr((r_top-r_bottom)/h);
474 Real r_max(-100000000);
475 Real r_min(100000000);
477 std::list<CurveStruct>::iterator curve_iter;
479 vector<Gdk::Point> points[10];
481 gc->set_function(Gdk::COPY);
482 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
485 gc->set_rgb_fg_color(Gdk::Color("#4f4f4f"));
486 get_window()->draw_rectangle(gc, false, 0, round_to_int((0-r_bottom)/dr), w, 0);
489 gc->set_rgb_fg_color(Gdk::Color("#00007f"));
490 get_window()->draw_rectangle(gc, false, round_to_int((time_adjustment_->get_value()-t_begin)/dt), 0, 0, h);
492 for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
496 int channels(curve_iter->channels.size());
497 for(i=0;i<channels;i++)
500 for(i=0,t=t_begin;i<w;i++,t+=dt)
502 for(int chan=0;chan<channels;chan++)
504 Real x(curve_iter->get_value(chan,t,dt));
507 points[chan].push_back(
520 for(int chan=0;chan<channels;chan++)
522 gc->set_rgb_fg_color(curve_iter->channels[chan].color);
525 get_window()->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points[chan]));
527 Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
529 layout->set_text(curve_iter->channels[chan].name);
530 get_window()->draw_layout(gc, 1, points[chan][0].get_y()+1, layout);
534 if(!curve_list_.empty())
536 range_adjustment_->set_upper(r_max+range_adjustment_->get_page_size()/2);
537 range_adjustment_->set_lower(r_min-range_adjustment_->get_page_size()/2);
539 get_window()->get_update_area();