1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
5 ** $Id: workarea.cpp,v 1.3 2005/01/16 19:55:57 darco Exp $
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 <sigc++/adaptors/hide.h>
35 #include "canvasview.h"
37 #include <gtkmm/window.h>
38 #include <gtkmm/image.h>
39 #include <gtkmm/drawingarea.h>
40 #include <gtkmm/ruler.h>
41 #include <gtkmm/arrow.h>
42 #include <gtkmm/image.h>
43 #include <gtkmm/scrollbar.h>
45 #include <sigc++/retype_return.h>
46 #include <sigc++/retype.h>
47 #include <sigc++/hide.h>
50 #include <synfig/target_scanline.h>
51 #include <synfig/target_tile.h>
52 #include <synfig/surface.h>
53 #include <synfigapp/canvasinterface.h>
54 #include "event_mouse.h"
55 #include "event_layerclick.h"
56 #include "widget_color.h"
57 #include <synfig/distance.h>
58 #include "workarearenderer.h"
60 #include "renderer_canvas.h"
61 #include "renderer_grid.h"
62 #include "renderer_guides.h"
63 #include "renderer_timecode.h"
64 #include "renderer_ducks.h"
65 #include "renderer_dragbox.h"
66 #include "renderer_bbox.h"
67 #include "asyncrenderer.h"
68 #include <gtkmm/frame.h>
70 #include <synfig/mutex.h>
74 /* === U S I N G =========================================================== */
78 using namespace synfig;
79 using namespace studio;
81 /* === M A C R O S ========================================================= */
86 #define stratof(X) (atof((X).c_str()))
87 #define stratoi(X) (atoi((X).c_str()))
91 /* === G L O B A L S ======================================================= */
93 /* === C L A S S E S ======================================================= */
95 class studio::WorkAreaTarget : public synfig::Target_Tile
101 int real_tile_w,real_tile_h;
102 //std::vector<Glib::RefPtr<Gdk::Pixbuf> >::iterator tile_iter;
104 int twindow_start, twindow_width, twindow_height, twindow_pad;
108 bool onion_first_tile;
111 std::list<synfig::Time> onion_skin_queue;
115 void set_onion_skin(bool x)
119 Time time(rend_desc().get_time_start());
121 onion_skin_queue.push_back(time);
122 //onion_skin_queue.push_back(time-1);
123 //onion_skin_queue.push_back(time+1);
127 onion_skin_queue.push_back(
128 get_canvas()->keyframe_list().find_prev(
138 onion_skin_queue.push_back(
139 get_canvas()->keyframe_list().find_next(
147 onion_layers=onion_skin_queue.size();
149 onion_first_tile=false;
153 WorkAreaTarget(WorkArea *workarea,int w, int h):
155 low_res(workarea->get_low_resolution_flag()),
158 real_tile_w(workarea->tile_w),
159 real_tile_h(workarea->tile_h),
160 refresh_id(workarea->refreshes),
164 //set_remove_alpha();
165 //set_avoid_time_sync();
169 set_tile_w(workarea->tile_w/2);
170 set_tile_h(workarea->tile_h/2);
174 set_tile_w(workarea->tile_w);
175 set_tile_h(workarea->tile_h);
177 set_canvas(workarea->get_canvas());
178 set_quality(workarea->get_quality());
183 workarea->queue_draw();
186 virtual bool set_rend_desc(synfig::RendDesc *newdesc)
189 newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
191 newdesc->set_wh(w/2,h/2);
193 newdesc->set_wh(w,h);
197 || workarea->get_h()!=h
198 ) workarea->set_wh(w,h,4);
200 workarea->full_frame=false;
206 virtual int total_tiles()const
208 int tw(rend_desc().get_w()/get_tile_w());
209 int th(rend_desc().get_h()/get_tile_h());
210 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
211 if(rend_desc().get_h()%get_tile_h()!=0)th++;
215 virtual int next_frame(Time& time)
217 synfig::Mutex::Lock lock(mutex);
220 return synfig::Target_Tile::next_frame(time);
222 onion_first_tile=(onion_layers==(signed)onion_skin_queue.size());
224 if(!onion_skin_queue.empty())
226 time=onion_skin_queue.front();
227 onion_skin_queue.pop_front();
232 return onion_skin_queue.size()+1;
235 virtual int next_tile(int& x, int& y)
237 synfig::Mutex::Lock lock(mutex);
238 //if(workarea->tile_queue.empty()) return 0;
240 //int curr_tile(workarea->tile_queue.front());
241 //workarea->tile_queue.pop_front();
242 int curr_tile(workarea->next_unrendered_tile(refresh_id-onion_skin_queue.size()));
246 // Width of the image(in tiles)
247 int tw(rend_desc().get_w()/get_tile_w());
248 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
250 y=(curr_tile/tw)*get_tile_w();
251 x=(curr_tile%tw)*get_tile_h();
253 // Mark this tile as "up-to-date"
255 workarea->tile_book[curr_tile].second=refresh_id-onion_skin_queue.size();
257 workarea->tile_book[curr_tile].second=refresh_id;
259 return total_tiles()-curr_tile+1;
263 virtual bool start_frame(synfig::ProgressCallback *cb)
265 synfig::Mutex::Lock lock(mutex);
267 int tw(rend_desc().get_w()/get_tile_w());
268 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
269 int th(rend_desc().get_h()/get_tile_h());
270 if(rend_desc().get_h()%get_tile_h()!=0)th++;
277 workarea->tile_book.resize(total_tiles());
278 //tile_iter=workarea->tile_book.begin()+twindow_start;
282 static void free_buff(const guint8 *x) { free(const_cast<guint8*>(x)); }
284 virtual bool add_tile(const synfig::Surface &surface, int x, int y)
286 synfig::Mutex::Lock lock(mutex);
289 PixelFormat pf(PF_RGB);
291 const int total_bytes(get_tile_w()*get_tile_h()*synfig::channels(pf));
293 unsigned char *buffer((unsigned char*)malloc(total_bytes));
295 if(!surface || !buffer)
298 unsigned char *dest(buffer);
299 const Color *src(surface[0]);
302 int x(surface.get_w()*surface.get_h());
303 //if(low_res) { w/=2,h/=2; }
304 Color dark(0.6,0.6,0.6);
305 Color lite(0.8,0.8,0.8);
307 dest=Color2PixelFormat(
310 ((i/surface.get_w())*8/h+(i%surface.get_w())*8/w)&1?dark:lite,
319 int tw(rend_desc().get_w()/get_tile_w());
320 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
321 unsigned int index=y*tw+x;
324 if(index>workarea->tile_book.size())
327 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
329 pixbuf=Gdk::Pixbuf::create_from_data(
330 buffer, // pointer to the data
331 Gdk::COLORSPACE_RGB, // the colorspace
332 ((pf&PF_A)==PF_A), // has alpha?
333 8, // bits per sample
334 surface.get_w(), // width
335 surface.get_h(), // height
336 surface.get_w()*synfig::channels(pf), // stride (pitch)
337 sigc::ptr_fun(&WorkAreaTarget::free_buff)
342 // We need to scale up
343 pixbuf=pixbuf->scale_simple(
350 if(!onionskin || onion_first_tile || !workarea->tile_book[index].first)
352 workarea->tile_book[index].first=pixbuf;
357 workarea->tile_book[index].first, // Dest
360 pixbuf->get_width(), // dest width
361 pixbuf->get_height(), // dest_height,
362 0, // double offset_x
363 0, // double offset_y
366 Gdk::INTERP_NEAREST, // interp
367 255/(onion_layers-onion_skin_queue.size()+1) //int overall_alpha
372 workarea->queue_draw();
373 assert(workarea->tile_book[index].first);
377 virtual void end_frame()
379 //workarea->queue_draw();
384 class studio::WorkAreaTarget_Full : public synfig::Target_Scanline
390 int real_tile_w,real_tile_h;
391 //std::vector<Glib::RefPtr<Gdk::Pixbuf> >::iterator tile_iter;
393 int twindow_start, twindow_width, twindow_height, twindow_pad;
397 bool onion_first_tile;
402 std::list<synfig::Time> onion_skin_queue;
404 void set_onion_skin(bool x)
408 Time time(rend_desc().get_time_start());
410 onion_skin_queue.push_back(time);
411 //onion_skin_queue.push_back(time-1);
412 //onion_skin_queue.push_back(time+1);
418 onion_skin_queue.push_back(
419 get_canvas()->keyframe_list().find_prev(
429 onion_skin_queue.push_back(
430 get_canvas()->keyframe_list().find_next(
438 onion_layers=onion_skin_queue.size();
440 onion_first_tile=false;
444 WorkAreaTarget_Full(WorkArea *workarea,int w, int h):
446 low_res(workarea->get_low_resolution_flag()),
449 refresh_id(workarea->refreshes),
453 set_canvas(workarea->get_canvas());
454 set_quality(workarea->get_quality());
457 ~WorkAreaTarget_Full()
461 virtual bool set_rend_desc(synfig::RendDesc *newdesc)
464 newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
466 newdesc->set_wh(w/2,h/2);
468 newdesc->set_wh(w,h);
472 || workarea->get_h()!=h
473 ) workarea->set_wh(w,h,4);
475 surface.set_wh(newdesc->get_w(),newdesc->get_h());
478 workarea->full_frame=true;
479 workarea->tile_book.resize(1);
483 virtual int next_frame(Time& time)
485 // Mark this tile as "up-to-date"
487 workarea->tile_book[0].second=refresh_id-onion_skin_queue.size();
489 workarea->tile_book[0].second=refresh_id;
492 return synfig::Target_Scanline::next_frame(time);
494 onion_first_tile=(onion_layers==(signed)onion_skin_queue.size());
496 if(!onion_skin_queue.empty())
498 time=onion_skin_queue.front();
499 onion_skin_queue.pop_front();
503 return onion_skin_queue.size()+1;
507 virtual bool start_frame(synfig::ProgressCallback *cb)
512 virtual Color * start_scanline(int scanline)
514 return surface[scanline];
517 virtual bool end_scanline()
522 static void free_buff(const guint8 *x) { free(const_cast<guint8*>(x)); }
524 virtual void end_frame()
528 PixelFormat pf(PF_RGB);
530 const int total_bytes(surface.get_w()*surface.get_h()*synfig::channels(pf));
532 unsigned char *buffer((unsigned char*)malloc(total_bytes));
534 if(!surface || !buffer)
537 unsigned char *dest(buffer);
538 const Color *src(surface[0]);
539 int w(surface.get_w());
540 //int h(surface.get_h());
541 int x(surface.get_w()*surface.get_h());
542 //if(low_res) { w/=2,h/=2; }
543 Color dark(0.6,0.6,0.6);
544 Color lite(0.8,0.8,0.8);
545 int tw=workarea->tile_w;
546 int th=workarea->tile_h;
553 dest=Color2PixelFormat(
556 ((i/w)*8/th+(i%w)*8/tw)&1?dark:lite,
565 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
567 pixbuf=Gdk::Pixbuf::create_from_data(
568 buffer, // pointer to the data
569 Gdk::COLORSPACE_RGB, // the colorspace
570 ((pf&PF_A)==PF_A), // has alpha?
571 8, // bits per sample
572 surface.get_w(), // width
573 surface.get_h(), // height
574 surface.get_w()*synfig::channels(pf), // stride (pitch)
575 sigc::ptr_fun(&WorkAreaTarget::free_buff)
580 // We need to scale up
581 pixbuf=pixbuf->scale_simple(
590 if(!onionskin || onion_first_tile || !workarea->tile_book[index].first)
592 workarea->tile_book[index].first=pixbuf;
597 workarea->tile_book[index].first, // Dest
600 pixbuf->get_width(), // dest width
601 pixbuf->get_height(), // dest_height,
602 0, // double offset_x
603 0, // double offset_y
606 Gdk::INTERP_NEAREST, // interp
607 255/(onion_layers-onion_skin_queue.size()+1) //int overall_alpha
611 workarea->queue_draw();
612 assert(workarea->tile_book[index].first);
619 /* === M E T H O D S ======================================================= */
622 WorkArea::WorkArea(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface):
623 Gtk::Table(4+RULER_FIX, 3, false),
624 canvas_interface(canvas_interface),
625 canvas(canvas_interface->get_canvas()),
626 scrollx_adjustment(0,-4,4,0.01,0.1),
627 scrolly_adjustment(0,-4,4,0.01,0.1),
637 allow_duck_clicks=true;
638 allow_layer_clicks=true;
639 render_idle_func_id=0;
647 last_focus_point=Point(0,0);
650 dirty_trap_enabled=false;
655 meta_data_lock=false;
657 insert_renderer(new Renderer_Canvas, 000);
658 insert_renderer(new Renderer_Grid, 100);
659 insert_renderer(new Renderer_Guides, 200);
660 insert_renderer(new Renderer_Ducks, 300);
661 insert_renderer(new Renderer_BBox, 399);
662 insert_renderer(new Renderer_Dragbox, 400);
663 insert_renderer(new Renderer_Timecode, 500);
665 signal_duck_selection_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
666 signal_strokes_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
667 signal_grid_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
668 signal_grid_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::save_meta_data));
669 signal_sketch_saved().connect(sigc::mem_fun(*this,&studio::WorkArea::save_meta_data));
671 // Not that it really makes a difference... (setting this to zero, that is)
674 drawing_area=manage(new class Gtk::DrawingArea());
675 drawing_area->show();
676 drawing_area->set_extension_events(Gdk::EXTENSION_EVENTS_ALL);
678 drawing_frame=manage(new Gtk::Frame);
679 drawing_frame->add(*drawing_area);
680 //drawing_frame->set_shadow_type(Gtk::SHADOW_NONE);
681 //drawing_frame->property_border_width()=5;
682 //drawing_frame->modify_fg(Gtk::STATE_NORMAL,Gdk::Color("#00ffff"));
683 //drawing_frame->modify_base(Gtk::STATE_NORMAL,Gdk::Color("#ff00ff"));
684 /*drawing_frame->modify_fg(Gtk::STATE_ACTIVE,Gdk::Color("#00ffff"));
685 drawing_frame->modify_base(Gtk::STATE_ACTIVE,Gdk::Color("#ff00ff"));
686 drawing_frame->modify_bg(Gtk::STATE_ACTIVE,Gdk::Color("#00ff00"));
687 drawing_frame->modify_fg(Gtk::STATE_INSENSITIVE,Gdk::Color("#00ffff"));
688 drawing_frame->modify_base(Gtk::STATE_INSENSITIVE,Gdk::Color("#ff00ff"));
689 drawing_frame->modify_bg(Gtk::STATE_INSENSITIVE,Gdk::Color("#00ff00"));
690 drawing_frame->modify_fg(Gtk::STATE_SELECTED,Gdk::Color("#00ffff"));
691 drawing_frame->modify_base(Gtk::STATE_SELECTED,Gdk::Color("#ff00ff"));
692 drawing_frame->modify_bg(Gtk::STATE_SELECTED,Gdk::Color("#00ff00"));
694 //drawing_frame->set_state(Gtk::STATE_NORMAL);
696 drawing_frame->show();
698 attach(*drawing_frame, 1, 3+RULER_FIX, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
700 Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
703 // Create the vertical and horizontal rulers
704 vruler = manage(new class Gtk::VRuler());
705 hruler = manage(new class Gtk::HRuler());
706 vruler->set_metric(Gtk::PIXELS);
707 hruler->set_metric(Gtk::PIXELS);
710 attach(*vruler, 0, 1, 1, 2, Gtk::SHRINK|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
711 attach(*hruler, 1, 3+RULER_FIX, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
712 hruler->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_hruler_event));
713 vruler->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_vruler_event));
714 hruler->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
715 vruler->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
717 // Create the menu button
718 menubutton=manage(new class Gtk::Button());
719 Gtk::Arrow *arrow1 = manage(new class Gtk::Arrow(Gtk::ARROW_RIGHT, Gtk::SHADOW_OUT));
720 menubutton->add(*arrow1);
721 menubutton->show_all();
722 menubutton->signal_pressed().connect(sigc::mem_fun(*this, &WorkArea::popup_menu));
723 attach(*menubutton, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
727 Gtk::VScrollbar *vscrollbar1 = manage(new class Gtk::VScrollbar(*get_scrolly_adjustment()));
728 Gtk::HScrollbar *hscrollbar1 = manage(new class Gtk::HScrollbar(*get_scrollx_adjustment()));
731 attach(*vscrollbar1, 3+RULER_FIX, 4+RULER_FIX, 1, 2, Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
732 attach(*hscrollbar1, 2+RULER_FIX, 3+RULER_FIX, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
734 ZoomDial *zoomdial=manage(new class ZoomDial(iconsize));
735 zoomdial->signal_zoom_in().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_in));
736 zoomdial->signal_zoom_out().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_out));
737 zoomdial->signal_zoom_fit().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_fit));
738 zoomdial->signal_zoom_norm().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_norm));
740 attach(*zoomdial, 0, 2+RULER_FIX, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
742 drawing_area->add_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
743 add_events(Gdk::KEY_PRESS_MASK);
744 drawing_area->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
745 drawing_area->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::POINTER_MOTION_MASK);
747 // ----------------- Attach signals
749 drawing_area->signal_expose_event().connect(sigc::mem_fun(*this, &WorkArea::refresh));
750 drawing_area->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_drawing_area_event));
751 drawing_area->signal_size_allocate().connect(sigc::hide(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info)));
755 canvas_interface->signal_rend_desc_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
756 // When either of the scrolling adjustments change, then redraw.
757 get_scrollx_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::queue_scroll));
758 get_scrolly_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::queue_scroll));
759 get_scrollx_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
760 get_scrolly_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
762 get_canvas()->signal_meta_data_changed("grid_size").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
763 get_canvas()->signal_meta_data_changed("grid_snap").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
764 get_canvas()->signal_meta_data_changed("grid_show").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
765 get_canvas()->signal_meta_data_changed("guide_show").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
766 get_canvas()->signal_meta_data_changed("guide_x").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
767 get_canvas()->signal_meta_data_changed("guide_y").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
768 get_canvas()->signal_meta_data_changed("onion_skin").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
769 get_canvas()->signal_meta_data_changed("guide_snap").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
770 get_canvas()->signal_meta_data_changed("sketch").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
771 get_canvas()->signal_meta_data_changed("solid_lines").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
774 meta_data_lock=false;
775 set_focus_point(Point(0,0));
781 String data(canvas->get_meta_data("sketch"));
784 if(!load_sketch(data))
785 load_sketch(dirname(canvas->get_file_name())+ETL_DIRECTORY_SEPERATOR+basename(data));
789 hruler->property_max_size()=double(10.0);
790 vruler->property_max_size()=double(10.0);
792 drawing_area->set_flags(drawing_area->get_flags()|Gtk::CAN_FOCUS);
795 WorkArea::~WorkArea()
801 WorkArea::save_meta_data()
807 Vector s(get_grid_size());
808 canvas_interface->set_meta_data("grid_size",strprintf("%f %f",s[0],s[1]));
809 canvas_interface->set_meta_data("grid_snap",get_grid_snap()?"1":"0");
810 canvas_interface->set_meta_data("guide_snap",get_guide_snap()?"1":"0");
811 canvas_interface->set_meta_data("guide_show",get_show_guides()?"1":"0");
812 canvas_interface->set_meta_data("grid_show",show_grid?"1":"0");
813 canvas_interface->set_meta_data("onion_skin",onion_skin?"1":"0");
816 GuideList::const_iterator iter;
817 for(iter=get_guide_list_x().begin();iter!=get_guide_list_x().end();++iter)
821 data+=strprintf("%f",*iter);
824 canvas_interface->set_meta_data("guide_x",data);
827 for(iter=get_guide_list_y().begin();iter!=get_guide_list_y().end();++iter)
831 data+=strprintf("%f",*iter);
834 canvas_interface->set_meta_data("guide_y",data);
837 if(get_sketch_filename().size())
839 if(dirname(canvas->get_file_name())==dirname(get_sketch_filename()))
840 canvas_interface->set_meta_data("sketch",basename(get_sketch_filename()));
842 canvas_interface->set_meta_data("sketch",get_sketch_filename());
845 meta_data_lock=false;
849 WorkArea::load_meta_data()
857 data=canvas->get_meta_data("grid_size");
860 float gx(get_grid_size()[0]),gy(get_grid_size()[1]);
862 String::iterator iter(find(data.begin(),data.end(),' '));
863 String tmp(data.begin(),iter);
871 tmp=String(iter+1,data.end());
876 set_grid_size(Vector(gx,gy));
879 synfig::error("WorkArea::load_meta_data(): Unable to parse data for \"grid_size\", which was \"%s\"",data.c_str());
881 data=canvas->get_meta_data("grid_show");
882 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
884 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
887 data=canvas->get_meta_data("solid_lines");
888 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
890 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
893 data=canvas->get_meta_data("guide_show");
894 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
896 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
899 data=canvas->get_meta_data("grid_snap");
900 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
902 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
903 set_grid_snap(false);
905 data=canvas->get_meta_data("guide_snap");
906 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
907 set_guide_snap(true);
908 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
909 set_guide_snap(false);
911 data=canvas->get_meta_data("onion_skin");
912 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
913 set_onion_skin(true);
914 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
915 set_onion_skin(false);
917 data=canvas->get_meta_data("guide_x");
918 get_guide_list_x().clear();
921 String::iterator iter(find(data.begin(),data.end(),' '));
922 String guide(data.begin(),iter);
925 get_guide_list_x().push_back(stratof(guide));
930 data=String(iter+1,data.end());
932 //sort(get_guide_list_x());
934 data=canvas->get_meta_data("guide_y");
935 get_guide_list_y().clear();
938 String::iterator iter(find(data.begin(),data.end(),' '));
939 String guide(data.begin(),iter);
942 get_guide_list_y().push_back(stratof(guide));
947 data=String(iter+1,data.end());
949 //sort(get_guide_list_y());
951 meta_data_lock=false;
956 WorkArea::set_onion_skin(bool x)
962 queue_render_preview();
963 signal_onion_skin_changed()();
967 WorkArea::get_onion_skin()const
973 WorkArea::enable_grid()
981 WorkArea::disable_grid()
989 WorkArea::set_show_guides(bool x)
997 WorkArea::toggle_grid()
999 show_grid=!show_grid;
1005 WorkArea::set_low_resolution_flag(bool x)
1007 if(x!=low_resolution)
1010 queue_render_preview();
1015 WorkArea::toggle_low_resolution_flag()
1017 set_low_resolution_flag(!get_low_resolution_flag());
1021 WorkArea::popup_menu()
1023 signal_popup_menu()();
1027 WorkArea::set_grid_size(const synfig::Vector &s)
1029 Duckmatic::set_grid_size(s);
1035 WorkArea::set_focus_point(const synfig::Point &point)
1037 // These next three lines try to ensure that we place the
1038 // focus on a pixel boundry
1039 /*Point adjusted(point[0]/abs(get_pw()),point[1]/abs(get_ph()));
1040 adjusted[0]=(abs(adjusted[0]-floor(adjusted[0]))<0.5)?floor(adjusted[0])*abs(get_pw()):ceil(adjusted[0])*abs(get_ph());
1041 adjusted[1]=(abs(adjusted[1]-floor(adjusted[1]))<0.5)?floor(adjusted[1])*abs(get_ph()):ceil(adjusted[1])*abs(get_ph());
1043 const synfig::Point& adjusted(point);
1045 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1046 Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1047 Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1049 get_scrollx_adjustment()->set_value(adjusted[0]*x_factor);
1050 get_scrolly_adjustment()->set_value(adjusted[1]*y_factor);
1054 WorkArea::get_focus_point()const
1056 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1057 Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1058 Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1060 return synfig::Point(get_scrollx_adjustment()->get_value()*x_factor, get_scrolly_adjustment()->get_value()*y_factor);
1064 WorkArea::set_wh(int W, int H,int CHAN)
1066 // If our size is already set, don't set it again
1067 if(W==w && H==h && CHAN==bpp)
1071 if(W<=0 || H<=0 || CHAN<=0)
1078 // Set all of the parameters
1083 refresh_dimension_info();
1091 WorkArea::on_key_press_event(GdkEventKey* event)
1093 if(get_selected_ducks().empty())
1096 Real multiplier(1.0);
1098 if(Gdk::ModifierType(event->state)&GDK_SHIFT_MASK)
1102 switch(event->keyval)
1105 nudge=Vector(-pw,0);
1111 nudge=Vector(0,-ph);
1121 synfigapp::Action::PassiveGrouper grouper(instance.get(),"Nudge");
1123 // Grid snap does not apply to nudging
1124 bool grid_snap_holder(get_grid_snap());
1125 bool guide_snap_holder(get_guide_snap());
1126 set_grid_snap(false);
1129 start_duck_drag(get_selected_duck()->get_trans_point());
1130 translate_selected_ducks(get_selected_duck()->get_trans_point()+nudge*multiplier);
1135 canvas_view->duck_refresh_flag=true;
1136 canvas_view->queue_rebuild_ducks();
1139 set_grid_snap(grid_snap_holder);
1140 set_guide_snap(guide_snap_holder);
1146 WorkArea::on_drawing_area_event(GdkEvent *event)
1148 synfig::Point mouse_pos;
1149 float bezier_click_pos;
1150 const float radius((abs(pw)+abs(ph))*4);
1151 int button_pressed(0);
1153 bool is_mouse(false);
1154 Gdk::ModifierType modifier(Gdk::ModifierType(0));
1156 drawing_area->grab_focus();
1158 // Handle input stuff
1160 event->any.type==GDK_MOTION_NOTIFY ||
1161 event->any.type==GDK_BUTTON_PRESS ||
1162 event->any.type==GDK_2BUTTON_PRESS ||
1163 event->any.type==GDK_3BUTTON_PRESS ||
1164 event->any.type==GDK_BUTTON_RELEASE
1168 if(event->any.type==GDK_MOTION_NOTIFY)
1170 device=event->motion.device;
1171 modifier=Gdk::ModifierType(event->motion.state);
1175 device=event->button.device;
1176 modifier=Gdk::ModifierType(event->button.state);
1179 // Make sure we recognise the device
1180 if(curr_input_device)
1182 if(curr_input_device!=device)
1185 curr_input_device=device;
1186 signal_input_device_changed()(curr_input_device);
1191 curr_input_device=device;
1192 signal_input_device_changed()(curr_input_device);
1195 assert(curr_input_device);
1197 // Calculate the position of the
1198 // input device in canvas coordinates
1200 if(!event->button.axes)
1202 mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->button.x,event->button.y)));
1203 button_pressed=event->button.button;
1206 if(isnan(event->button.x) || isnan(event->button.y))
1211 double x(event->button.axes[0]);
1212 double y(event->button.axes[1]);
1213 if(isnan(x) || isnan(y))
1216 pressure=event->button.axes[2];
1217 //synfig::info("pressure=%f",pressure);
1219 pressure/=1.0f-0.04f;
1222 assert(!isnan(pressure));
1224 mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)));
1226 button_pressed=event->button.button;
1228 if(button_pressed==1 && pressure<0 && (event->any.type!=GDK_BUTTON_RELEASE && event->any.type!=GDK_BUTTON_PRESS))
1233 //if(event->any.type==GDK_BUTTON_PRESS && button_pressed)
1234 // synfig::info("Button pressed on input device = %d",event->button.button);
1236 //if(event->button.axes[2]>0.1)
1237 // button_pressed=1;
1239 // button_pressed=0;
1244 // Handle the renderables
1246 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
1247 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
1249 if((*iter)->get_enabled())
1250 if((*iter)->event_vfunc(event))
1252 // Event handled. Return true.
1258 // Event hasn't been handled, pass it down
1261 case GDK_BUTTON_PRESS:
1263 switch(button_pressed)
1265 case 1: // Attempt to click on a duck
1267 etl::handle<Duck> duck;
1270 if(allow_duck_clicks)
1272 duck=find_duck(mouse_pos,radius);
1277 if(duck_is_selected(duck))
1283 if(modifier&GDK_SHIFT_MASK)
1287 else if(modifier&GDK_CONTROL_MASK)
1293 clear_selected_ducks();
1300 // clear_selected_ducks();
1304 selected_bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1305 if(duck && duck->get_editable())
1307 //get_selected_duck()->signal_user_click(0)();
1308 //if(clicked_duck)clicked_duck->signal_user_click(0)();
1310 drag_point=mouse_pos;
1311 //drawing_area->queue_draw();
1312 start_duck_drag(mouse_pos);
1313 get_canvas_view()->reset_cancel_status();
1316 // I commented out this section because
1317 // it was causing issues when rotoscoping.
1318 // At the moment, we don't need it, so
1319 // this was the easiest way to fix the problem.
1325 selected_bezier->signal_user_click(0)(bezier_click_pos);
1330 //clear_selected_ducks();
1332 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_LEFT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1334 // Check for a guide click
1335 GuideList::iterator iter;
1337 iter=find_guide_x(mouse_pos,radius);
1338 if(iter==get_guide_list_x().end())
1340 curr_guide_is_x=false;
1341 iter=find_guide_y(mouse_pos,radius);
1344 curr_guide_is_x=true;
1345 if(iter!=get_guide_list_x().end() && iter!=get_guide_list_y().end())
1347 dragging=DRAG_GUIDE;
1353 // All else fails, try making a selection box
1355 curr_point=drag_point=mouse_pos;
1361 case 2: // Attempt to drag and move the window
1363 etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1364 etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1366 duck->signal_user_click(1)();
1369 bezier->signal_user_click(1)(bezier_click_pos);
1371 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_MIDDLE,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1374 dragging=DRAG_WINDOW;
1375 drag_point=mouse_pos;
1376 signal_user_click(1)(mouse_pos);
1380 case 3: // Attempt to either get info on a duck, or open the menu
1382 etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1383 etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1385 Layer::Handle layer(get_canvas()->find_layer(mouse_pos));
1388 if(get_selected_ducks().size()<=1)
1389 duck->signal_user_click(2)();
1392 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1399 bezier->signal_user_click(2)(bezier_click_pos);
1405 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_RIGHT,mouse_pos))==Smach::RESULT_OK)
1410 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1412 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1421 signal_user_click(3)(mouse_pos);
1424 signal_user_click(4)(mouse_pos);
1431 case GDK_MOTION_NOTIFY:
1432 curr_point=mouse_pos;
1434 if(event->motion.time-last_event_time<25)
1437 last_event_time=event->motion.time;
1439 signal_cursor_moved_();
1441 // Guide/Duck hilights on hover
1442 if(dragging==DRAG_NONE)
1444 GuideList::iterator iter;
1446 iter=find_guide_x(mouse_pos,radius);
1447 if(iter==get_guide_list_x().end())
1448 iter=find_guide_y(mouse_pos,radius);
1450 if(iter!=curr_guide)
1453 drawing_area->queue_draw();
1456 etl::handle<Duck> duck;
1457 duck=find_duck(mouse_pos,radius);
1458 if(duck!=hover_duck)
1461 drawing_area->queue_draw();
1466 if(dragging==DRAG_DUCK)
1468 if(canvas_view->get_cancel_status())
1471 canvas_view->queue_rebuild_ducks();
1475 Point point((mouse_pos-selected_duck->get_origin())/selected_duck->get_scalar());
1478 point[0]=floor(point[0]/grid_size[0]+0.5)*grid_size[0];
1479 point[1]=floor(point[1]/grid_size[1]+0.5)*grid_size[1];
1481 selected_duck->set_point(point);
1484 //Point p(mouse_pos);
1486 set_axis_lock(event->motion.state&GDK_SHIFT_MASK);
1488 translate_selected_ducks(mouse_pos);
1490 drawing_area->queue_draw();
1492 if(dragging==DRAG_BOX)
1494 curr_point=mouse_pos;
1495 drawing_area->queue_draw();
1497 if(dragging==DRAG_GUIDE)
1500 *curr_guide=mouse_pos[0];
1502 *curr_guide=mouse_pos[1];
1503 drawing_area->queue_draw();
1505 if(dragging!=DRAG_WINDOW)
1506 { // Update those triangle things on the rulers
1507 const synfig::Point point(mouse_pos);
1508 hruler->property_position()=Distance(point[0],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1509 vruler->property_position()=Distance(point[1],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1511 if(dragging==DRAG_WINDOW)
1513 set_focus_point(get_focus_point()+mouse_pos-drag_point);
1516 if(event->motion.state&GDK_BUTTON1_MASK && canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,BUTTON_LEFT,mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1519 if(event->motion.state&GDK_BUTTON2_MASK && canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,BUTTON_MIDDLE,mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1522 if(event->motion.state&GDK_BUTTON3_MASK && canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,BUTTON_RIGHT,mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1525 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_MOTION,BUTTON_NONE,mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1529 case GDK_BUTTON_RELEASE:
1533 if(dragging==DRAG_GUIDE)
1540 if(dragging==DRAG_DUCK)
1542 synfigapp::Action::PassiveGrouper grouper(instance.get(),"Move");
1544 //translate_selected_ducks(mouse_pos);
1545 set_axis_lock(false);
1548 get_canvas_view()->duck_refresh_flag=false;
1549 get_canvas_view()->duck_refresh_needed=false;
1550 const bool drag_did_anything(end_duck_drag());
1551 get_canvas_view()->duck_refresh_flag=true;
1552 if(!drag_did_anything)
1554 //etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1556 if(modifier&GDK_SHIFT_MASK)
1558 //synfig::info("DUCK_DRAG_RELEASE: SHIFT-MASK ON!");
1561 //synfig::info("DUCK_DRAG_RELEASE: CLICKED DUCK!");
1562 unselect_duck(clicked_duck);
1565 else if(modifier&GDK_CONTROL_MASK)
1567 //synfig::info("DUCK_DRAG_RELEASE: CONTROL-MASK ON!");
1570 //synfig::info("DUCK_DRAG_RELEASE: CLICKED DUCK!");
1571 unselect_duck(clicked_duck);
1576 //synfig::info("DUCK_DRAG_RELEASE: NO MASK!");
1579 //synfig::info("DUCK_DRAG_RELEASE: CLICKED DUCK!");
1580 clear_selected_ducks();
1581 select_duck(clicked_duck);
1584 if(clicked_duck)clicked_duck->signal_user_click(0)();
1588 if(canvas_view->duck_refresh_needed)
1589 canvas_view->queue_rebuild_ducks();
1594 canvas_view->duck_refresh_flag=true;
1595 canvas_view->queue_rebuild_ducks();
1604 if(dragging==DRAG_BOX)
1607 if((drag_point-mouse_pos).mag()>radius/2.0f)
1609 if(canvas_view->get_smach().process_event(EventBox(drag_point,mouse_pos,MouseButton(event->button.button),modifier))==Smach::RESULT_ACCEPT)
1612 if(!(modifier&GDK_CONTROL_MASK) && !(modifier&GDK_SHIFT_MASK))
1613 clear_selected_ducks();
1614 select_ducks_in_box(drag_point,mouse_pos);
1619 if(allow_layer_clicks)
1621 Layer::Handle layer(get_canvas()->find_layer(drag_point));
1624 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_LEFT,mouse_pos,modifier))==Smach::RESULT_OK)
1625 signal_layer_selected_(layer);
1631 signal_user_click(0)(mouse_pos);
1638 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_UP,MouseButton(event->button.button),mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1651 WorkArea::on_hruler_event(GdkEvent *event)
1656 case GDK_BUTTON_PRESS:
1657 if(dragging==DRAG_NONE)
1659 dragging=DRAG_GUIDE;
1660 curr_guide=get_guide_list_y().insert(get_guide_list_y().begin());
1661 curr_guide_is_x=false;
1666 case GDK_MOTION_NOTIFY:
1668 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1671 if(event->button.axes)
1673 x=(event->button.axes[0]);
1674 y=(event->button.axes[1]);
1682 if(isnan(y) || isnan(x))
1685 *curr_guide=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)))[1];
1692 case GDK_BUTTON_RELEASE:
1693 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1696 get_guide_list_y().erase(curr_guide);
1708 WorkArea::on_vruler_event(GdkEvent *event)
1713 case GDK_BUTTON_PRESS:
1715 if(dragging==DRAG_NONE)
1718 dragging=DRAG_GUIDE;
1719 curr_guide=get_guide_list_x().insert(get_guide_list_x().begin());
1720 curr_guide_is_x=true;
1724 case GDK_BUTTON_RELEASE:
1726 if(dragging==DRAG_GUIDE && curr_guide_is_x==true)
1730 get_guide_list_x().erase(curr_guide);
1743 WorkArea::refresh_dimension_info()
1745 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1747 canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1748 canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1753 scrollx_adjustment.set_page_increment(abs(get_grid_size()[0]));
1754 scrollx_adjustment.set_step_increment(abs(pw));
1755 scrollx_adjustment.set_lower(-abs(canvaswidth));
1756 scrollx_adjustment.set_upper(abs(canvaswidth));
1757 scrolly_adjustment.set_lower(-abs(canvasheight));
1758 scrolly_adjustment.set_upper(abs(canvasheight));
1759 scrolly_adjustment.set_step_increment(abs(ph));
1760 scrolly_adjustment.set_page_increment(abs(get_grid_size()[1]));
1764 if(drawing_area->get_width()<=0 || drawing_area->get_height()<=0 || w==0 || h==0)
1767 const synfig::Point focus_point(get_focus_point());
1768 const synfig::Real x(focus_point[0]/pw+drawing_area->get_width()/2-w/2);
1769 const synfig::Real y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1771 window_tl[0]=rend_desc.get_tl()[0]-pw*x;
1772 window_br[0]=rend_desc.get_br()[0]+pw*(drawing_area->get_width()-x-w);
1774 window_tl[1]=rend_desc.get_tl()[1]-ph*y;
1775 window_br[1]=rend_desc.get_br()[1]+ph*(drawing_area->get_height()-y-h);
1777 hruler->property_lower()=Distance(window_tl[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1778 hruler->property_upper()=Distance(window_br[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1779 vruler->property_lower()=Distance(window_tl[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1780 vruler->property_upper()=Distance(window_br[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1782 view_window_changed();
1787 WorkArea::screen_to_comp_coords(synfig::Point pos)const
1789 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1790 //synfig::Vector::value_type canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1791 //synfig::Vector::value_type canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1792 //synfig::Vector::value_type pw=canvaswidth/w;
1793 //synfig::Vector::value_type ph=canvasheight/h;
1794 Vector focus_point=get_focus_point();
1795 synfig::Vector::value_type x=focus_point[0]/pw+drawing_area->get_width()/2-w/2;
1796 synfig::Vector::value_type y=focus_point[1]/ph+drawing_area->get_height()/2-h/2;
1798 return rend_desc.get_tl()-synfig::Point(pw*x,ph*y)+synfig::Point(pw*pos[0],ph*pos[1]);
1802 WorkArea::comp_to_screen_coords(synfig::Point pos)const
1804 synfig::warning("WorkArea::comp_to_screen_coords: Not yet implemented");
1805 return synfig::Point();
1809 WorkArea::next_unrendered_tile(int refreshes)const
1811 //assert(!tile_book.empty());
1812 if(tile_book.empty())
1815 //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1817 const synfig::Vector focus_point(get_focus_point());
1819 // Calculate the window coordinates of the top-left
1820 // corner of the canvas.
1821 const synfig::Vector::value_type
1822 x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
1823 y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1825 const int width_in_tiles(w/tile_w+(w%tile_w?1:0));
1826 const int height_in_tiles(h/tile_h+(h%tile_h?1:0));
1832 u2(int((-x+drawing_area->get_width())/tile_w+1)),
1833 v2(int((-y+drawing_area->get_height())/tile_h+1));
1835 if(u2>width_in_tiles)u2=width_in_tiles;
1836 if(v2>height_in_tiles)v2=height_in_tiles;
1840 int last_good_tile(-1);
1845 int index(v*width_in_tiles+u);
1846 if(tile_book[index].second<refreshes)
1848 last_good_tile=index;
1853 return last_good_tile;
1857 template <typename F, typename T=WorkAreaRenderer, typename R=typename F::result_type>
1858 class handle2ptr_t : public std::unary_function<typename etl::handle<T>,R>
1863 handle2ptr_t(const F &func):func(func) { };
1865 R operator()(typename etl::handle<T> x) { return func(*x); }
1868 template <typename F>
1872 return handle2ptr_t<F>(func);
1875 renderer_set_.begin(),
1876 renderer_set_.end(),
1881 &WorkAreaRenderer::render_vfunc
1883 Gdk::Rectangle(event->area)
1885 drawing_area->get_window()
1892 WorkArea::refresh(GdkEventExpose*event)
1894 assert(get_canvas());
1896 drawing_area->get_window()->clear();
1898 //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1900 const synfig::Vector focus_point(get_focus_point());
1902 // Update the old focus point
1903 last_focus_point=focus_point;
1905 // Draw out the renderables
1907 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
1908 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
1910 if((*iter)->get_enabled())
1911 (*iter)->render_vfunc(
1912 drawing_area->get_window(),
1913 Gdk::Rectangle(&event->area)
1919 // Calculate the window coordinates of the top-left
1920 // corner of the canvas.
1921 //const synfig::Vector::value_type
1922 // x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
1923 // y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1925 //const synfig::Vector::value_type window_startx(window_tl[0]);
1926 //const synfig::Vector::value_type window_endx(window_br[0]);
1927 //const synfig::Vector::value_type window_starty(window_tl[1]);
1928 //const synfig::Vector::value_type window_endy(window_br[1]);
1930 Glib::RefPtr<Gdk::GC> gc=Gdk::GC::create(drawing_area->get_window());
1934 // If we are in animate mode, draw a red border around the screen
1935 if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
1937 /*gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
1938 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
1939 drawing_area->get_window()->draw_rectangle(
1943 drawing_area->get_width()-1,drawing_area->get_height()-1 //w,h
1946 drawing_frame->modify_bg(Gtk::STATE_NORMAL,Gdk::Color("#FF0000"));
1947 //get_window()->set_background(Gdk::Color("#FF0000"));
1950 drawing_frame->unset_bg(Gtk::STATE_NORMAL);
1954 previous_focus=get_focus_point();
1960 WorkArea::done_rendering()
1966 pix_buf=Gdk::Pixbuf::create_from_data(
1967 buffer, // pointer to the data
1968 Gdk::COLORSPACE_RGB, // the colorspace
1970 8, // bits per sample
1973 w*bpp); // stride (pitch)
1980 WorkArea::set_quality(int x)
1985 queue_render_preview();
1989 class WorkAreaProgress : public synfig::ProgressCallback
1991 WorkArea *work_area;
1992 ProgressCallback *cb;
1996 WorkAreaProgress(WorkArea *work_area,ProgressCallback *cb):
1997 work_area(work_area),cb(cb)
2003 task(const std::string &str)
2005 if(work_area->dirty)
2007 return cb->task(str);
2011 error(const std::string &err)
2013 if(work_area->dirty)
2015 return cb->error(err);
2019 amount_complete(int current, int total)
2021 if(work_area->dirty)
2023 return cb->amount_complete(current,total);
2028 studio::WorkArea::async_update_preview()
2034 get_canvas_view()->reset_cancel_status();
2036 // This object will mark us as busy until
2038 //studio::App::Busy busy;
2040 //WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2041 //synfig::ProgressCallback *cb=&callback;
2043 if(!is_visible())return false;
2046 // If we are queued to render the scene at the next idle
2047 // go ahead and de-queue it.
2048 if(render_idle_func_id)
2050 g_source_remove(render_idle_func_id);
2052 render_idle_func_id=0;
2057 get_canvas_view()->reset_cancel_status();
2060 RendDesc desc=get_canvas()->rend_desc();
2062 int w=(int)(desc.get_w()*zoom);
2063 int h=(int)(desc.get_h()*zoom);
2065 // Setup the description parameters
2066 desc.set_antialias(1);
2067 desc.set_time(cur_time);
2069 set_rend_desc(desc);
2071 // Create the render target
2072 handle<Target> target;
2074 if(w*h>(low_resolution?480*270:480*270/2))
2076 handle<WorkAreaTarget> trgt(new class WorkAreaTarget(this,w,h));
2078 trgt->set_rend_desc(&desc);
2079 trgt->set_onion_skin(get_onion_skin());
2084 handle<WorkAreaTarget_Full> trgt(new class WorkAreaTarget_Full(this,w,h));
2086 trgt->set_rend_desc(&desc);
2087 trgt->set_onion_skin(get_onion_skin());
2091 // We can rest assured that our time has already
2092 // been set, so there is no need to have to
2093 // recalculate that over again.
2094 // UPDATE: This is kind of needless with
2095 // the way that time is handled now in SYNFIG.
2096 //target->set_avoid_time_sync(true);
2097 async_renderer=new AsyncRenderer(target);
2098 async_renderer->signal_finished().connect(
2099 sigc::mem_fun(this,&WorkArea::async_update_finished)
2103 async_renderer->start();
2105 synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2108 cb->task("Rendering...");
2115 studio::WorkArea::async_update_finished()
2117 synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2124 // If we completed successfuly, then
2125 // we aren't dirty anymore
2126 if(async_renderer->has_success())
2135 cb->task("Render Failed");
2137 //get_canvas_view()->reset_cancel_status();
2142 studio::WorkArea::sync_update_preview()
2144 // const Time &time(cur_time);
2147 get_canvas_view()->reset_cancel_status();
2152 // This object will mark us as busy until
2154 studio::App::Busy busy;
2156 WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2157 synfig::ProgressCallback *cb=&callback;
2159 // We don't want to render if we are already rendering
2166 if(!is_visible())return false;
2167 get_canvas()->set_time(get_time());
2168 get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2169 signal_rendering()();
2171 // If we are queued to render the scene at the next idle
2172 // go ahead and de-queue it.
2173 if(render_idle_func_id)
2175 g_source_remove(render_idle_func_id);
2177 render_idle_func_id=0;
2183 get_canvas_view()->reset_cancel_status();
2186 RendDesc desc=get_canvas()->rend_desc();
2187 //newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2189 int w=(int)(desc.get_w()*zoom);
2190 int h=(int)(desc.get_h()*zoom);
2192 // Setup the description parameters
2193 desc.set_antialias(1);
2194 desc.set_time(cur_time);
2197 set_rend_desc(desc);
2199 // Create the render target
2200 handle<WorkAreaTarget> target(new class WorkAreaTarget(this,w,h));
2202 target->set_rend_desc(&desc);
2204 // We can rest assured that our time has already
2205 // been set, so there is no need to have to
2206 // recalculate that over again.
2207 target->set_avoid_time_sync(true);
2210 cb->task(strprintf("Rendering canvas %s...",get_canvas()->get_name().c_str()));
2214 if(!ret && !get_canvas_view()->get_cancel_status() && dirty)
2220 if(get_canvas_view()->get_cancel_status())
2228 cb->task("Render Failed");
2229 cb->amount_complete(0,1);
2232 // Refresh the work area to make sure that
2233 // it is being displayed correctly
2234 drawing_area->queue_draw();
2236 // If we completed successfuly, then
2237 // we aren't dirty anymore
2245 //get_canvas_view()->reset_cancel_status();
2251 studio::WorkArea::async_render_preview(Time time)
2254 //tile_book.clear();
2257 if(!is_visible())return;
2259 get_canvas()->set_time(get_time());
2260 get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2261 signal_rendering()();
2263 async_update_preview();
2266 WorkArea::async_render_preview()
2268 return async_render_preview(get_canvas_view()->get_time());
2272 studio::WorkArea::sync_render_preview(Time time)
2275 //tile_book.clear();
2277 if(!is_visible())return false;
2278 return sync_update_preview();
2282 WorkArea::sync_render_preview()
2284 return sync_render_preview(get_canvas_view()->get_time());
2288 WorkArea::sync_render_preview_hook()
2290 sync_render_preview(get_canvas_view()->get_time());
2294 WorkArea::queue_scroll()
2296 // const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2298 const synfig::Point focus_point(get_focus_point());
2301 new_x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2302 new_y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2305 old_x(last_focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2306 old_y(last_focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2308 // If the coordinates didn't change, we shouldn't queue a draw
2309 if(old_x==new_x && old_y==new_y)
2313 dx(round_to_int(old_x)-round_to_int(new_x)),
2314 dy(round_to_int(old_y)-round_to_int(new_y));
2316 drawing_area->get_window()->scroll(-dx,-dy);
2318 /*drawing_area->queue_draw_area(
2325 last_focus_point=focus_point;
2329 studio::WorkArea::zoom_in()
2331 set_zoom(zoom*1.25);
2335 studio::WorkArea::zoom_out()
2337 set_zoom(zoom/1.25);
2341 studio::WorkArea::zoom_fit()
2343 // This really doesn't zoom to fit. Bug.
2348 studio::WorkArea::zoom_norm()
2351 set_zoom(prev_zoom);
2360 studio::WorkArea::__render_preview(gpointer data)
2363 WorkArea *work_area(static_cast<WorkArea*>(data));
2365 work_area->queued=false;
2366 work_area->async_render_preview(work_area->get_canvas_view()->get_time());
2372 studio::WorkArea::queue_render_preview()
2374 //synfig::info("queue_render_preview(): called for %s", get_canvas_view()->get_time().get_string().c_str());
2379 //synfig::info("queue_render_preview(): already queued, unqueuing");
2380 /* if(render_idle_func_id)
2381 g_source_remove(render_idle_func_id);
2382 render_idle_func_id=0;
2388 if(dirty_trap_enabled)
2390 dirty_trap_queued++;
2402 //synfig::info("queue_render_preview(): (re)queuing...");
2403 //render_idle_func_id=g_idle_add_full(G_PRIORITY_DEFAULT,__render_preview,this,NULL);
2404 render_idle_func_id=g_timeout_add_full(G_PRIORITY_DEFAULT,queue_time,__render_preview,this,NULL);
2407 /* else if(rendering)
2416 DirtyTrap::DirtyTrap(WorkArea *work_area):work_area(work_area)
2418 work_area->dirty_trap_enabled=true;
2420 work_area->dirty_trap_queued=0;
2423 DirtyTrap::~DirtyTrap()
2425 work_area->dirty_trap_enabled=false;
2426 if(work_area->dirty_trap_queued)
2427 work_area->queue_render_preview();
2431 studio::WorkArea::queue_draw_preview()
2433 drawing_area->queue_draw();
2437 studio::WorkArea::set_cursor(const Gdk::Cursor& x)
2439 drawing_area->get_window()->set_cursor(x);
2442 studio::WorkArea::set_cursor(Gdk::CursorType x)
2444 drawing_area->get_window()->set_cursor(Gdk::Cursor(x));
2447 #include "iconcontroler.h"
2450 studio::WorkArea::refresh_cursor()
2452 // set_cursor(IconControler::get_tool_cursor(canvas_view->get_smach().get_state_name(),drawing_area->get_window()));
2456 studio::WorkArea::reset_cursor()
2458 drawing_area->get_window()->set_cursor(Gdk::Cursor(Gdk::TOP_LEFT_ARROW));
2459 // set_cursor(Gdk::TOP_LEFT_ARROW);
2463 studio::WorkArea::set_zoom(float z)
2465 z=max(1.0f/128.0f,min(128.0f,z));
2469 refresh_dimension_info();
2470 /*if(async_renderer)
2472 async_renderer->stop();
2476 async_update_preview();
2477 //queue_render_preview();
2481 WorkArea::set_selected_value_node(etl::loose_handle<synfig::ValueNode> x)
2483 if(x!=selected_value_node_)
2485 selected_value_node_=x;
2491 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x)
2493 renderer_set_.insert(x);
2494 x->set_work_area(this);
2499 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x, int priority)
2501 x->set_priority(priority);
2506 WorkArea::erase_renderer(const etl::handle<WorkAreaRenderer> &x)
2508 x->set_work_area(0);
2509 renderer_set_.erase(x);
2514 WorkArea::resort_render_set()
2516 std::set<etl::handle<WorkAreaRenderer> > tmp(
2517 renderer_set_.begin(),
2520 renderer_set_.swap(tmp);