3 * Copyright (C) 2014 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "automation.h"
22 #include "bcdisplayinfo.h"
25 #include "sketcherwindow.h"
28 #include "cwindowgui.h"
30 #include "edlsession.h"
33 #include "mainerror.h"
36 #include "pluginserver.h"
40 #define AltMask Mod1Mask
45 const char *SketcherPoint::types[] = {
51 const char *SketcherCurve::pens[] = {
60 SketcherCurvePenItem::SketcherCurvePenItem(int pen)
61 : BC_MenuItem(_(SketcherCurve::pens[pen]))
65 int SketcherCurvePenItem::handle_event()
67 SketcherCurvePen *popup = (SketcherCurvePen*)get_popup_menu();
69 SketcherWindow *gui = popup->gui;
70 SketcherConfig &config = gui->plugin->config;
71 int ci = config.cv_selected;
72 if( ci >= 0 && ci < config.curves.size() ) {
73 SketcherCurve *cv = config.curves[ci];
75 gui->curve_list->update(ci);
76 gui->send_configure_change();
81 SketcherCurvePen::SketcherCurvePen(SketcherWindow *gui, int x, int y, int pen)
82 : BC_PopupMenu(x,y,72,_(cv_pen[pen]))
87 void SketcherCurvePen::create_objects()
89 int n = sizeof(cv_pen)/sizeof(cv_pen[0]);
90 for( int pen=0; pen<n; ++pen )
91 add_item(new SketcherCurvePenItem(pen));
93 void SketcherCurvePen::update(int pen)
95 set_text(_(cv_pen[this->pen=pen]));
99 SketcherCurveColor::SketcherCurveColor(SketcherWindow *gui, int x, int y, int w, int h)
100 : BC_Button(x, y, w, vframes)
103 this->color = CV_COLOR;
104 for( int i=0; i<3; ++i ) {
105 vframes[i] = new VFrame(w, h, BC_RGB888);
106 vframes[i]->clear_frame();
110 SketcherCurveColor::~SketcherCurveColor()
112 for( int i=0; i<3; ++i )
116 void SketcherCurveColor::set_color(int color)
119 int r = (color>>16) & 0xff;
120 int g = (color>>8) & 0xff;
121 int b = (color>>0) & 0xff;
122 for( int i=0; i<3; ++i ) {
123 VFrame *vframe = vframes[i];
124 int ww = vframe->get_w(), hh = vframe->get_h();
125 uint8_t **rows = vframe->get_rows();
126 int rr = r, gg = g, bb = b;
131 if( (rr+=48) > 0xff ) rr = 0xff;
132 if( (gg+=48) > 0xff ) gg = 0xff;
133 if( (bb+=48) > 0xff ) bb = 0xff;
136 if( (rr-=48) < 0x00 ) rr = 0x00;
137 if( (gg-=48) < 0x00 ) gg = 0x00;
138 if( (bb-=48) < 0x00 ) bb = 0x00;
141 for( int y=0; y<hh; ++y ) {
142 uint8_t *rp = rows[y];
143 for( int x=0; x<ww; ++x ) {
144 *rp++ = rr; *rp++ = gg; *rp++ = bb;
151 void SketcherCurveColor::update_gui(int color)
157 int SketcherCurveColor::handle_event()
159 gui->start_color_thread(this);
163 SketcherCurveColorPicker::SketcherCurveColorPicker(SketcherWindow *gui, SketcherCurveColor *color_button)
164 : ColorPicker(1, _("Color"))
167 this->color_button = color_button;
168 this->color = CV_COLOR;
169 color_update = new SketcherCurveColorThread(this);
172 SketcherCurveColorPicker::~SketcherCurveColorPicker()
177 void SketcherCurveColorPicker::start(int color)
180 int alpha = (~color>>24) & 0xff;
181 start_window(color & 0xffffff, alpha, 1);
182 color_update->start();
185 void SketcherCurveColorPicker::handle_done_event(int result)
187 color_update->stop();
188 gui->lock_window("SketcherCurveColorPicker::handle_done_event");
189 if( result ) { color = orig_color | (~orig_alpha<<24); }
190 color_button->update_gui(color);
191 gui->unlock_window();
192 SketcherConfig &config = gui->plugin->config;
193 int ci = config.cv_selected;
194 if( ci >= 0 && ci < config.curves.size() ) {
195 SketcherCurve *cv = config.curves[ci];
197 gui->curve_list->update(ci);
198 gui->send_configure_change();
202 int SketcherCurveColorPicker::handle_new_color(int color, int alpha)
204 this->color = color | (~alpha<<24);
205 color_update->update_lock->unlock();
209 void SketcherCurveColorPicker::update_gui()
211 gui->lock_window("SketcherCurveColorPicker::update_gui");
212 color_button->update_gui(color);
213 SketcherConfig &config = gui->plugin->config;
214 int ci = config.cv_selected;
216 SketcherCurve *cv = config.curves[ci];
218 gui->curve_list->update(ci);
219 gui->send_configure_change();
221 gui->unlock_window();
224 SketcherCurveColorThread::SketcherCurveColorThread(SketcherCurveColorPicker *color_picker)
227 this->color_picker = color_picker;
228 this->update_lock = new Condition(0,"SketcherCurveColorThread::update_lock");
232 SketcherCurveColorThread::~SketcherCurveColorThread()
238 void SketcherCurveColorThread::start()
246 void SketcherCurveColorThread::stop()
250 update_lock->unlock();
255 void SketcherCurveColorThread::run()
258 update_lock->lock("SketcherCurveColorThread::run");
260 color_picker->update_gui();
265 SketcherCoord::SketcherCoord(SketcherWindow *gui, int x, int y,
266 coord output, coord mn, coord mx)
267 : BC_TumbleTextBox(gui, output, mn, mx, x, y, 64, 1)
272 SketcherCoord::~SketcherCoord()
276 SketcherNum::SketcherNum(SketcherWindow *gui, int x, int y,
277 int output, int mn, int mx)
278 : BC_TumbleTextBox(gui, output, mn, mx, x, y, 54)
283 SketcherNum::~SketcherNum()
287 int SketcherPointX::handle_event()
289 if( !SketcherCoord::handle_event() ) return 0;
290 SketcherConfig &config = gui->plugin->config;
291 int ci = config.cv_selected;
292 if( ci >= 0 && ci < config.curves.size() ) {
293 SketcherCurve *cv = config.curves[ci];
294 SketcherPointList *point_list = gui->point_list;
295 int pi = config.pt_selected;
296 SketcherPoints &points = cv->points;
297 if( pi >= 0 && pi < points.size() ) {
298 coord v = atof(get_text());
300 point_list->set_point(pi, PT_X, v);
301 point_list->update_list(pi);
302 gui->send_configure_change();
307 int SketcherPointY::handle_event()
309 if( !SketcherCoord::handle_event() ) return 0;
310 SketcherConfig &config = gui->plugin->config;
311 int ci = config.cv_selected;
312 if( ci >= 0 && ci < config.curves.size() ) {
313 SketcherCurve *cv = config.curves[ci];
314 SketcherPointList *point_list = gui->point_list;
315 int pi = config.pt_selected;
316 SketcherPoints &points = cv->points;
317 if( pi >= 0 && pi < points.size() ) {
318 coord v = atof(get_text());
320 point_list->set_point(pi, PT_Y, v);
321 point_list->update_list(pi);
322 gui->send_configure_change();
328 int SketcherPointId::handle_event()
330 if( !SketcherNum::handle_event() ) return 0;
331 SketcherConfig &config = gui->plugin->config;
332 int ci = config.cv_selected;
333 if( ci >= 0 && ci < config.curves.size() ) {
334 SketcherCurve *cv = config.curves[ci];
335 SketcherPointList *point_list = gui->point_list;
336 int pi = config.pt_selected;
337 SketcherPoints &points = cv->points;
338 if( pi >= 0 && pi < points.size() ) {
339 int id = atoi(get_text());
341 point_list->set_point(pi, PT_ID, id);
342 point_list->update_list(pi);
343 gui->send_configure_change();
349 SketcherCurveWidth::SketcherCurveWidth(SketcherWindow *gui, int x, int y, int width)
350 : SketcherNum(gui, x, y, width, 0, 255)
354 SketcherCurveWidth::~SketcherCurveWidth()
358 int SketcherCurveWidth::handle_event()
360 if( !SketcherNum::handle_event() ) return 0;
361 SketcherConfig &config = gui->plugin->config;
362 int ci = config.cv_selected;
363 if( ci >= 0 && ci < config.curves.size() ) {
364 SketcherCurve *cv = config.curves[ci];
365 int v = atoi(get_text());
367 gui->curve_list->update(ci);
368 gui->send_configure_change();
373 void SketcherCurveWidth::update(int width)
375 SketcherNum::update(this->width=width);
379 SketcherWindow::SketcherWindow(Sketcher *plugin)
380 : PluginClientWindow(plugin, 380, 620, 380, 620, 0)
382 this->plugin = plugin;
383 this->title_pen = 0; this->curve_pen = 0;
384 this->title_color = 0; this->curve_color = 0;
385 this->color_picker = 0; this->drag = 0;
386 this->new_curve = 0; this->del_curve = 0;
387 this->curve_up = 0; this->curve_dn = 0;
388 this->title_x = 0; this->point_x = 0;
389 this->title_y = 0; this->point_y = 0;
390 this->title_id = 0; this->point_id = 0;
391 this->new_point = 0; this->del_point = 0;
392 this->point_up = 0; this->point_dn = 0;
393 this->point_list = 0; this->notes0 = 0;
394 this->notes1 = 0; this->notes2 = 0;
397 track_w = track_h -1;
398 cursor_x = cursor_y = -1;
399 output_x = output_y = -1;
400 last_x = last_y = -1;
401 projector_x = projector_y = projector_z = -1;
402 state = 0; dragging = 0;
408 SketcherWindow::~SketcherWindow()
416 void SketcherWindow::create_objects()
418 int x = 10, y = 10, dy = 0, x1, y1;
419 int margin = plugin->get_theme()->widget_border;
421 int ci = plugin->config.cv_selected;
422 if( ci < 0 || ci >= plugin->config.curves.size() )
423 ci = plugin->new_curve();
424 SketcherCurve *cv = plugin->config.curves[ci];
426 reset_curves = new SketcherResetCurves(this, plugin, x1=x, y+3);
427 add_subwindow(reset_curves); dy = bmax(dy,reset_curves->get_h());
428 x1 += reset_curves->get_w() + 2*margin;
429 const char *curve_text = _("Curve");
430 title_width = new BC_Title(x1, y, _("Width:"));
431 add_subwindow(title_width); dy = bmax(dy,title_width->get_h());
432 x1 += title_width->get_w() + margin;
433 curve_width = new SketcherCurveWidth(this, x1, y, cv->width);
434 curve_width->create_objects();
435 y += dy + 2*margin; dy = 0;
437 x1 = get_w()-x - BC_Title::calculate_w(this, curve_text, LARGEFONT);
438 y1 = y-margin - BC_Title::calculate_h(this, curve_text, LARGEFONT);
439 title = new BC_Title(x1, y1, curve_text, LARGEFONT,
440 get_resources()->menu_highlighted_fontcolor);
441 add_subwindow(title); dy = bmax(dy,title->get_h());
442 curve_list = new SketcherCurveList(this, plugin, x, y);
443 add_subwindow(curve_list); dy = bmax(dy,curve_list->get_h());
444 y += dy + margin; dy = 0;
446 new_curve = new SketcherNewCurve(this, plugin, x1=x, y);
447 add_subwindow(new_curve); dy = bmax(dy,new_curve->get_h());
448 x1 += new_curve->get_w() + margin;
449 curve_up = new SketcherCurveUp(this, x1, y);
450 add_subwindow(curve_up); dy = bmax(dy,curve_up->get_h());
451 x1 += curve_up->get_w() + 4*margin;
452 y1 = BC_Title::calculate_h(this, _("Pen:"));
453 title_pen = new BC_Title(x1+30, y+dy-y1, _("Pen:"));
454 add_subwindow(title_pen); dy = bmax(dy,title_pen->get_h());
455 int x2 = (get_w()+x1)/2 + 20;
456 y1 = BC_Title::calculate_h(this, _("Color:"));
457 title_color = new BC_Title(x2, y+dy-y1, _("Color:"));
458 add_subwindow(title_color); dy = bmax(dy,title_color->get_h());
459 y += dy + margin; dy = 0;
461 del_curve = new SketcherDelCurve(this, plugin, x1=x, y);
462 add_subwindow(del_curve); dy = bmax(dy,del_curve->get_h());
463 x1 += del_curve->get_w() + margin;
464 curve_dn = new SketcherCurveDn(this, x1, y);
465 add_subwindow(curve_dn); dy = bmax(dy,curve_dn->get_h());
466 x1 += curve_dn->get_w() + 4*margin;
467 curve_pen = new SketcherCurvePen(this, x1, y, cv->pen);
468 add_subwindow(curve_pen); dy = bmax(dy,curve_pen->get_h());
469 curve_pen->create_objects();
470 curve_color = new SketcherCurveColor(this, x2, y, COLOR_W, COLOR_H);
471 add_subwindow(curve_color); dy = bmax(dy,curve_color->get_h());
472 curve_color->set_color(cv->color);
473 curve_color->draw_face();
474 y += dy + margin; dy = 0;
475 curve_list->update(ci);
478 bar = new BC_Bar(x, y, get_w()-2*x);
479 add_subwindow(bar); dy = bmax(dy,bar->get_h());
480 bar = new BC_Bar(x, y+=dy, get_w()-2*x);
481 add_subwindow(bar); dy = bmax(dy,bar->get_h());
484 int pi = plugin->config.pt_selected;
485 SketcherPoint *pt = pi >= 0 && pi < cv->points.size() ? cv->points[pi] : 0;
486 reset_points = new SketcherResetPoints(this, plugin, x1=x, y+3);
487 add_subwindow(reset_points); dy = bmax(dy,reset_points->get_h());
488 x1 += reset_points->get_w() + 2*margin;
489 if( plugin->config.drag ) {
490 if( !grab(plugin->server->mwindow->cwindow->gui) ) {
491 eprintf("drag enabled, but compositor already grabbed\n");
492 plugin->config.drag = 0;
495 drag = new SketcherDrag(this, x1, y);
496 add_subwindow(drag); dy = bmax(dy,drag->get_h());
497 x1 += drag->get_w() + 2*margin;
498 int arc = pt ? pt->arc : ARC_LINE;
499 point_type = new SketcherPointType(this, x1, y, arc);
500 add_subwindow(point_type); dy = bmax(dy,point_type->get_h());
501 point_type->create_objects();
502 y += dy + margin; dy = 0;
504 const char *point_text = _("Point");
505 x1 = get_w()-x - BC_Title::calculate_w(this, point_text, LARGEFONT);
506 y1 = y-margin - BC_Title::calculate_h(this, point_text, LARGEFONT);
507 add_subwindow(title = new BC_Title(x1, y1, point_text, LARGEFONT,
508 get_resources()->menu_highlighted_fontcolor));
509 point_list = new SketcherPointList(this, plugin, x, y);
510 add_subwindow(point_list); dy = bmax(dy,point_list->get_h());
511 y += dy + margin; dy = 0;
513 new_point = new SketcherNewPoint(this, plugin, x1=x, y);
514 add_subwindow(new_point); dy = bmax(dy,new_point->get_h());
515 x1 += new_point->get_w() + margin;
516 point_up = new SketcherPointUp(this, x1, y);
517 add_subwindow(point_up); dy = bmax(dy,point_up->get_h());
518 x1 += point_up->get_w() + 2*margin;
519 title_x = new BC_Title(x1, y, _("X:"));
520 add_subwindow(title_x); dy = bmax(dy,title_x->get_h());
521 x1 += title_x->get_w() + margin;
522 point_x = new SketcherPointX(this, x1, y, !pt ? 0.f : pt->x);
523 point_x->create_objects(); dy = bmax(dy, point_x->get_h());
524 x2 = x1 + point_x->get_w() + 2*margin + 10;
525 y1 = BC_Title::calculate_h(this, _("ID:"));
526 title_id = new BC_Title(x2+16, y+dy-y1, _("ID:"));
527 add_subwindow(title_id); dy = bmax(dy, title_id->get_h());
528 y += dy + margin; dy = 0;
530 del_point = new SketcherDelPoint(this, plugin, x1=x, y);
531 add_subwindow(del_point); dy = bmax(dy,del_point->get_h());
532 x1 += del_point->get_w() + margin;
533 point_dn = new SketcherPointDn(this, x1, y);
534 add_subwindow(point_dn); dy = bmax(dy,point_dn->get_h());
535 x1 += point_dn->get_w() + 2*margin;
536 title_y = new BC_Title(x1, y, _("Y:"));
537 add_subwindow(title_y); dy = bmax(dy,title_y->get_h());
538 x1 += title_y->get_w() + margin;
539 point_y = new SketcherPointY(this, x1, y, !pt ? 0.f : pt->y);
540 point_y->create_objects(); dy = bmax(dy, point_y->get_h());
541 point_id = new SketcherPointId(this, x2, y, !pt ? 0 : pt->id);
542 point_id->create_objects(); dy = bmax(dy, point_id->get_h());
543 y += dy + margin + 5; dy = 0;
544 point_list->update(pi);
546 bar = new BC_Bar(x, y, get_w()-2*x);
547 add_subwindow(bar); dy = bmax(dy,bar->get_h());
550 add_subwindow(notes0 = new BC_Title(x, y,
556 "Ctrl+Shift="))); dy = bmax(dy, notes0->get_h());
557 add_subwindow(notes1 = new BC_Title(x+100, y,
563 "new fill point"))); dy = bmax(dy, notes1->get_h());
564 add_subwindow(notes2 = new BC_Title(x+220, y,
570 "new off point"))); dy = bmax(dy, notes2->get_h());
571 y += dy + margin + 10;
573 add_subwindow(notes3 = new BC_Title(x, y,
574 "Key DEL= delete point, +Shift= delete curve\n"));
578 void SketcherWindow::send_configure_change()
581 plugin->send_configure_change();
585 int SketcherWindow::grab_event(XEvent *event)
587 int ret = do_grab_event(event);
588 if( !grab_event_count() ) {
589 if( grab_cursor_motion() )
591 if( pending_config ) {
592 last_x = output_x; last_y = output_y;
593 send_configure_change();
599 int SketcherWindow::do_grab_event(XEvent *event)
601 switch( event->type ) {
602 case ButtonPress: break;
603 case ButtonRelease: break;
604 case MotionNotify: break;
606 if( keysym_lookup(event) > 0 ) {
607 switch( get_keysym() ) {
610 return (event->xkey.state & ShiftMask) ?
611 del_curve->handle_event() :
612 del_point->handle_event() ;
619 MWindow *mwindow = plugin->server->mwindow;
620 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
621 CWindowCanvas *canvas = cwindow_gui->canvas;
622 int cx, cy; cwindow_gui->get_relative_cursor(cx, cy);
623 cx -= mwindow->theme->ccanvas_x;
624 cy -= mwindow->theme->ccanvas_y;
627 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ||
628 cy < 0 || cy >= mwindow->theme->ccanvas_h )
632 switch( event->type ) {
634 if( dragging ) return 0;
641 if( dragging ) break;
642 default: // fall thru
646 cursor_x = cx, cursor_y = cy;
647 canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
648 position = plugin->get_source_position();
649 Track *track = plugin->server->plugin->track;
650 track_w = track->track_w;
651 track_h = track->track_h;
652 track->automation->get_projector(
653 &projector_x, &projector_y, &projector_z,
654 position, PLAY_FORWARD);
655 projector_x += mwindow->edl->session->output_w / 2;
656 projector_y += mwindow->edl->session->output_h / 2;
657 output_x = (cursor_x - projector_x) / projector_z + track_w / 2;
658 output_y = (cursor_y - projector_y) / projector_z + track_h / 2;
659 state = event->xmotion.state;
661 if( event->type == MotionNotify ) {
662 memcpy(&motion_event, event, sizeof(motion_event));
666 if( grab_cursor_motion() )
669 switch( event->type ) {
671 pending_config = grab_button_press(event);
681 int SketcherWindow::grab_button_press(XEvent *event)
683 SketcherConfig &config = plugin->config;
684 int ci = config.cv_selected;
685 if( ci < 0 || ci >= plugin->config.curves.size() )
687 SketcherCurves &curves = config.curves;
688 SketcherCurve *cv = curves[ci];
689 SketcherPoints &points = cv->points;
690 int pi = config.pt_selected;
692 int button_no = event->xbutton.button;
693 switch( button_no ) {
695 if( (state & ShiftMask) ) { // create new point/string
697 pi = plugin->new_point(cv,
698 !(state & ControlMask) ? ARC_LINE : ARC_FILL,
699 output_x, output_y, pi+1);
700 point_list->update(pi);
703 SketcherPoint *pt = 0; // select point
704 double dist = cv->nearest_point(pi, output_x,output_y);
707 Track *track = plugin->server->plugin->track;
708 int track_w = track->track_w, track_h = track->track_h;
709 float px = (pt->x - track_w / 2) * projector_z + projector_x;
710 float py = (pt->y - track_h / 2) * projector_z + projector_y;
711 float pix = DISTANCE(px, py, cursor_x,cursor_y);
712 if( (state & ControlMask) && pix >= HANDLE_W ) { pi = -1; pt = 0; }
714 point_list->set_selected(pi);
717 if( (state & ShiftMask) ) { // create new curve point
719 pi = plugin->new_point(cv,
720 !(state & ControlMask) ? ARC_CURVE : ARC_OFF,
721 output_x, output_y, pi+1);
722 point_list->update(pi);
725 if( (state & AltMask) ) { // create new curve
726 ci = plugin->new_curve(cv->pen, cv->width, curve_color->color);
727 curve_list->update(ci);
728 point_list->update(-1);
731 SketcherPoint *pt = 0;
732 double dist = config.nearest_point(ci, pi, output_x,output_y);
734 pt = curves[ci]->points[pi];
735 Track *track = plugin->server->plugin->track;
736 int track_w = track->track_w, track_h = track->track_h;
737 float px = (pt->x - track_w / 2) * projector_z + projector_x;
738 float py = (pt->y - track_h / 2) * projector_z + projector_y;
739 float pix = DISTANCE(px, py, cursor_x,cursor_y);
740 if( (state & ControlMask) && pix >= HANDLE_W ) { ci = pi = -1; pt = 0; }
743 curve_list->update(ci);
744 point_list->update(pi);
751 int SketcherWindow::grab_cursor_motion()
753 if( !pending_motion )
756 SketcherConfig &config = plugin->config;
757 int ci = config.cv_selected;
758 if( ci < 0 || ci >= plugin->config.curves.size() )
760 SketcherCurves &curves = config.curves;
761 SketcherCurve *cv = curves[ci];
762 SketcherPoints &points = cv->points;
763 int pi = config.pt_selected;
765 if( (state & ShiftMask) ) { // string of points
766 if( (state & (Button1Mask|Button3Mask)) ) {
767 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
769 float dist = DISTANCE(pt->x, pt->y, output_x, output_y);
770 if( dist < get_w()*0.1 ) return 0; // tolerance w/10
773 int arc = (state & Button1Mask) ? ARC_LINE : ARC_CURVE;
774 pi = plugin->new_point(cv, arc, output_x, output_y, pi+1);
775 point_list->update(pi);
779 if( (state & Button1Mask) ) {
780 if( (state & ControlMask) ) { // drag selected point
781 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
783 point_list->set_point(pi, PT_X, pt->x = output_x);
784 point_list->set_point(pi, PT_Y, pt->y = output_y);
785 point_list->update_list(pi);
786 point_x->update(pt->x);
787 point_y->update(pt->y);
791 if( (state & AltMask) ) { // drag all curves
792 int dx = round(output_x - last_x);
793 int dy = round(output_y - last_y);
794 for( int i=0; i<curves.size(); ++i ) {
795 SketcherCurve *crv = plugin->config.curves[i];
796 int pts = crv->points.size();
797 for( int k=0; k<pts; ++k ) {
798 SketcherPoint *pt = crv->points[k];
799 pt->x += dx; pt->y += dy;
802 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
804 point_x->update(pt ? pt->x : 0.f);
805 point_y->update(pt ? pt->y : 0.f);
806 point_id->update(pt ? pt->id : 0);
807 point_list->update(pi);
810 double dist = cv->nearest_point(pi, output_x,output_y);
812 point_list->set_selected(pi);
815 if( (state & Button3Mask) ) {
816 if( (state & (ControlMask | AltMask)) ) { // drag selected curve(s)
817 int dx = round(output_x - last_x);
818 int dy = round(output_y - last_y);
819 for( int i=0; i<points.size(); ++i ) {
820 SketcherPoint *pt = points[i];
821 pt->x += dx; pt->y += dy;
823 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
825 point_x->update(pt ? pt->x : 0.f);
826 point_y->update(pt ? pt->y : 0.f);
827 point_id->update(pt ? pt->id : 0);
828 point_list->update(pi);
831 double dist = config.nearest_point(ci, pi, output_x,output_y);
833 curve_list->update(ci);
834 point_list->update(pi);
841 int SketcherWindow::keypress_event()
843 int key = get_keypress();
845 case DELETE: return shift_down() ?
846 del_curve->handle_event() :
847 del_point->handle_event() ;
852 void SketcherWindow::start_color_thread(SketcherCurveColor *color_button)
856 color_picker = new SketcherCurveColorPicker(this, color_button);
857 int color = CV_COLOR, ci = plugin->config.cv_selected;
858 if( ci >= 0 && ci < plugin->config.curves.size() ) {
859 SketcherCurve *cv = plugin->config.curves[ci];
862 color_picker->start(color);
863 lock_window("SketcherWindow::start_color_thread");
867 SketcherCurveList::SketcherCurveList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
868 : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
871 this->plugin = plugin;
872 col_titles[CV_ID] = _("ID"); col_widths[CV_ID] = 64;
873 col_titles[CV_RAD] = _("width"); col_widths[CV_RAD] = 64;
874 col_titles[CV_PEN] = _("pen"); col_widths[CV_PEN] = 64;
875 col_titles[CV_CLR] = _("color"); col_widths[CV_CLR] = 80;
876 col_titles[CV_ALP] = _("alpha"); col_widths[CV_ALP] = 64;
878 SketcherCurveList::~SketcherCurveList()
882 void SketcherCurveList::clear()
884 for( int i=CV_SZ; --i>=0; )
885 cols[i].remove_all_objects();
888 int SketcherCurveList::column_resize_event()
890 for( int i=CV_SZ; --i>=0; )
891 col_widths[i] = get_column_width(i);
895 int SketcherCurveList::handle_event()
897 int ci = get_selection_number(0, 0);
899 gui->point_list->update(0);
900 gui->send_configure_change();
904 int SketcherCurveList::selection_changed()
910 void SketcherCurveList::set_selected(int k)
913 if( k >= 0 && k < plugin->config.curves.size() ) {
914 SketcherCurve *cv = plugin->config.curves[k];
915 gui->curve_width->update(cv->width);
916 gui->curve_pen->update(cv->pen);
917 gui->curve_color->update_gui(cv->color);
920 plugin->config.cv_selected = ci;
924 void SketcherCurveList::update_list(int k)
926 int xpos = get_xposition(), ypos = get_yposition();
927 if( k >= 0 ) update_selection(&cols[0], k);
928 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],CV_SZ, xpos,ypos,k);
932 void SketcherCurveList::update(int k)
935 SketcherCurves &curves = plugin->config.curves;
936 int sz = curves.size();
937 for( int i=0; i<sz; ++i ) {
938 SketcherCurve *cv = curves[i];
939 char itxt[BCSTRLEN]; sprintf(itxt,"%d", cv->id);
940 char ptxt[BCSTRLEN]; sprintf(ptxt,"%s", cv_pen[cv->pen]);
941 char rtxt[BCSTRLEN]; sprintf(rtxt,"%d", cv->width);
942 int color = cv->color;
943 int r = (color>>16)&0xff;
944 int g = (color>> 8)&0xff;
945 int b = (color>> 0)&0xff;
946 int a = (~color>>24)&0xff;
947 char ctxt[BCSTRLEN]; sprintf(ctxt,"#%02x%02x%02x", r, g, b);
948 char atxt[BCSTRLEN]; sprintf(atxt,"%5.3f", a/255.);
949 add_curve(itxt, ptxt, rtxt, ctxt, atxt);
954 void SketcherCurveList::add_curve(const char *id, const char *pen,
955 const char *width, const char *color, const char *alpha)
957 cols[CV_ID].append(new BC_ListBoxItem(id));
958 cols[CV_RAD].append(new BC_ListBoxItem(width));
959 cols[CV_PEN].append(new BC_ListBoxItem(pen));
960 cols[CV_CLR].append(new BC_ListBoxItem(color));
961 cols[CV_ALP].append(new BC_ListBoxItem(alpha));
964 SketcherNewCurve::SketcherNewCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
965 : BC_GenericButton(x, y, 64, _("New"))
968 this->plugin = plugin;
970 SketcherNewCurve::~SketcherNewCurve()
973 int SketcherNewCurve::handle_event()
975 int pen = gui->curve_pen->pen;
976 int color = gui->curve_color->color;
977 int width = gui->curve_width->width;
978 int ci = plugin->config.cv_selected;
979 if( ci >= 0 && ci < plugin->config.curves.size() ) {
980 SketcherCurve *cv = plugin->config.curves[ci];
981 pen = cv->pen; width = cv->width; color = cv->color;
983 ci = plugin->new_curve(pen, width, color);
984 gui->curve_list->update(ci);
985 gui->point_list->update(-1);
986 gui->send_configure_change();
990 SketcherDelCurve::SketcherDelCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
991 : BC_GenericButton(x, y, 64, C_("Del"))
994 this->plugin = plugin;
996 SketcherDelCurve::~SketcherDelCurve()
999 int SketcherDelCurve::handle_event()
1001 SketcherConfig &config = plugin->config;
1002 int ci = config.cv_selected;
1003 SketcherCurves &curves = config.curves;
1004 if( ci >= 0 && ci < curves.size() ) {
1005 curves.remove_object_number(ci--);
1006 if( ci < 0 ) ci = 0;
1007 if( !curves.size() )
1008 ci = plugin->new_curve();
1009 gui->curve_list->update(ci);
1010 gui->point_list->update(-1);
1011 gui->send_configure_change();
1016 SketcherCurveUp::SketcherCurveUp(SketcherWindow *gui, int x, int y)
1017 : BC_GenericButton(x, y, _("Up"))
1021 SketcherCurveUp::~SketcherCurveUp()
1025 int SketcherCurveUp::handle_event()
1027 SketcherConfig &config = gui->plugin->config;
1028 int ci = config.cv_selected;
1029 SketcherCurves &curves = config.curves;
1030 if( ci > 0 && ci < curves.size() ) {
1031 SketcherCurve *&cv0 = curves[ci];
1032 SketcherCurve *&cv1 = curves[--ci];
1033 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
1034 gui->curve_list->update(ci);
1036 gui->send_configure_change();
1040 SketcherCurveDn::SketcherCurveDn(SketcherWindow *gui, int x, int y)
1041 : BC_GenericButton(x, y, _("Dn"))
1045 SketcherCurveDn::~SketcherCurveDn()
1049 int SketcherCurveDn::handle_event()
1051 SketcherConfig &config = gui->plugin->config;
1052 int ci = config.cv_selected;
1053 SketcherCurves &curves = config.curves;
1054 if( ci >= 0 && ci < curves.size()-1 ) {
1055 SketcherCurve *&cv0 = curves[ci];
1056 SketcherCurve *&cv1 = curves[++ci];
1057 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
1058 gui->curve_list->update(ci);
1060 gui->send_configure_change();
1065 SketcherPointTypeItem::SketcherPointTypeItem(int arc)
1066 : BC_MenuItem(_(pt_type[arc]))
1070 int SketcherPointTypeItem::handle_event()
1072 SketcherPointType *popup = (SketcherPointType*)get_popup_menu();
1074 SketcherWindow *gui = popup->gui;
1075 SketcherConfig &config = gui->plugin->config;
1076 SketcherCurves &curves = config.curves;
1077 int ci = config.cv_selected;
1078 if( ci < 0 || ci >= curves.size() )
1080 SketcherCurve *cv = curves[ci];
1081 SketcherPoints &points = cv->points;
1082 int pi = config.pt_selected;
1084 ArrayList<int> selected;
1085 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1088 for( int i=selected.size(); --i>=0; ) {
1089 int k = selected[i];
1090 if( k < 0 || k >= points.size() ) continue;
1091 SketcherPoint *pt = cv->points[k];
1093 gui->point_list->set_point(k, PT_TY, _(pt_type[arc]));
1096 gui->point_list->update_list(pi);
1097 gui->send_configure_change();
1101 SketcherPointType::SketcherPointType(SketcherWindow *gui, int x, int y, int arc)
1102 : BC_PopupMenu(x,y,64,_(pt_type[arc]))
1107 void SketcherPointType::create_objects()
1109 for( int arc=0; arc<PT_SZ; ++arc )
1110 add_item(new SketcherPointTypeItem(arc));
1112 void SketcherPointType::update(int arc)
1114 set_text(_(pt_type[this->type=arc]));
1118 SketcherPointList::SketcherPointList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1119 : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
1122 this->plugin = plugin;
1123 col_titles[PT_ID] = _("ID"); col_widths[PT_ID] = 50;
1124 col_titles[PT_TY] = _("Type"); col_widths[PT_TY] = 80;
1125 col_titles[PT_X] = _("X"); col_widths[PT_X] = 90;
1126 col_titles[PT_Y] = _("Y"); col_widths[PT_Y] = 90;
1127 set_selection_mode(LISTBOX_MULTIPLE);
1129 SketcherPointList::~SketcherPointList()
1133 void SketcherPointList::clear()
1135 for( int i=PT_SZ; --i>=0; )
1136 cols[i].remove_all_objects();
1139 int SketcherPointList::column_resize_event()
1141 for( int i=PT_SZ; --i>=0; )
1142 col_widths[i] = get_column_width(i);
1146 int SketcherPointList::handle_event()
1148 int pi = get_selection_number(0, 0);
1149 if( get_selection_number(0, 1) >= 0 ) pi = -1;
1151 gui->send_configure_change();
1155 int SketcherPointList::selection_changed()
1161 void SketcherPointList::add_point(const char *id, const char *ty, const char *xp, const char *yp)
1163 cols[PT_ID].append(new BC_ListBoxItem(id));
1164 cols[PT_TY].append(new BC_ListBoxItem(ty));
1165 cols[PT_X].append(new BC_ListBoxItem(xp));
1166 cols[PT_Y].append(new BC_ListBoxItem(yp));
1169 void SketcherPointList::set_point(int i, int c, int v)
1171 char stxt[BCSTRLEN];
1172 sprintf(stxt,"%d",v);
1173 set_point(i,c,stxt);
1175 void SketcherPointList::set_point(int i, int c, coord v)
1177 char stxt[BCSTRLEN];
1178 sprintf(stxt,"%0.1f",v);
1179 set_point(i,c,stxt);
1181 void SketcherPointList::set_point(int i, int c, const char *cp)
1183 cols[c].get(i)->set_text(cp);
1186 void SketcherPointList::set_selected(int k)
1188 SketcherPoint *pt = 0;
1189 int ci = plugin->config.cv_selected, pi = -1;
1190 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1191 SketcherCurve *cv = plugin->config.curves[ci];
1192 pt = k >= 0 && k < cv->points.size() ? cv->points[pi=k] : 0;
1194 gui->point_type->update(pt ? pt->arc : ARC_OFF);
1195 gui->point_x->update(pt ? pt->x : 0.f);
1196 gui->point_y->update(pt ? pt->y : 0.f);
1197 gui->point_id->update(pt ? pt->id : 0);
1198 plugin->config.pt_selected = pi;
1201 void SketcherPointList::update_list(int k)
1203 int xpos = get_xposition(), ypos = get_yposition();
1204 if( k >= 0 ) update_selection(&cols[0], k);
1205 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],PT_SZ, xpos,ypos,k);
1208 void SketcherPointList::update(int k)
1211 int ci = plugin->config.cv_selected, sz = 0;
1212 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1213 SketcherCurve *cv = plugin->config.curves[ci];
1214 SketcherPoints &points = cv->points;
1216 for( int i=0; i<sz; ++i ) {
1217 SketcherPoint *pt = points[i];
1218 char itxt[BCSTRLEN]; sprintf(itxt,"%d", pt->id);
1219 char ttxt[BCSTRLEN]; sprintf(ttxt,"%s", _(pt_type[pt->arc]));
1220 char xtxt[BCSTRLEN]; sprintf(xtxt,"%0.1f", pt->x);
1221 char ytxt[BCSTRLEN]; sprintf(ytxt,"%0.1f", pt->y);
1222 add_point(itxt, ttxt, xtxt, ytxt);
1228 void SketcherWindow::update_gui()
1230 SketcherConfig &config = plugin->config;
1231 int ci = config.cv_selected;
1232 int pi = config.pt_selected;
1233 curve_list->update(ci);
1234 point_list->update(pi);
1235 SketcherCurve *cv = ci >= 0 ? config.curves[ci] : 0;
1236 curve_width->update(cv ? cv->width : 1);
1237 curve_pen->update(cv ? cv->pen : PEN_SQUARE);
1238 curve_color->set_color(cv ? cv->color : CV_COLOR);
1239 SketcherPoint *pt = pi >= 0 ? cv->points[pi] : 0;
1240 point_x->update(pt ? pt->x : 0);
1241 point_y->update(pt ? pt->y : 0);
1242 point_id->update(pt ? pt->id : 0);
1243 drag->update(plugin->config.drag);
1247 SketcherPointUp::SketcherPointUp(SketcherWindow *gui, int x, int y)
1248 : BC_GenericButton(x, y, _("Up"))
1252 SketcherPointUp::~SketcherPointUp()
1256 int SketcherPointUp::handle_event()
1258 SketcherConfig &config = gui->plugin->config;
1259 int ci = config.cv_selected;
1260 if( ci < 0 || ci >= config.curves.size() )
1262 SketcherCurve *cv = config.curves[ci];
1263 SketcherPoints &points = cv->points;
1264 if( points.size() < 2 )
1266 int pi = config.pt_selected;
1268 ArrayList<int> selected;
1269 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1272 for( int i=0; i<selected.size(); ++i ) {
1273 int k = selected[i];
1274 if( k <= 0 ) continue;
1276 SketcherPoint *&pt0 = points[k];
1277 SketcherPoint *&pt1 = points[--k];
1278 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1280 gui->point_list->update(pi);
1281 gui->send_configure_change();
1285 SketcherPointDn::SketcherPointDn(SketcherWindow *gui, int x, int y)
1286 : BC_GenericButton(x, y, _("Dn"))
1290 SketcherPointDn::~SketcherPointDn()
1294 int SketcherPointDn::handle_event()
1296 SketcherConfig &config = gui->plugin->config;
1297 int ci = config.cv_selected;
1298 if( ci < 0 || ci >= config.curves.size() )
1300 SketcherCurve *cv = config.curves[ci];
1301 SketcherPoints &points = cv->points;
1302 int sz1 = points.size()-1;
1305 int pi = config.pt_selected;
1307 ArrayList<int> selected;
1308 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1311 for( int i=selected.size(); --i>=0; ) {
1312 int k = selected[i];
1313 if( k >= sz1 ) continue;
1315 SketcherPoint *&pt0 = points[k];
1316 SketcherPoint *&pt1 = points[++k];
1317 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1319 gui->point_list->update(pi);
1320 gui->send_configure_change();
1324 SketcherDrag::SketcherDrag(SketcherWindow *gui, int x, int y)
1325 : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
1329 int SketcherDrag::handle_event()
1331 CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
1332 int value = get_value();
1334 if( !gui->grab(cwindow_gui) ) {
1340 gui->ungrab(cwindow_gui);
1341 gui->plugin->config.drag = value;
1342 gui->send_configure_change();
1346 SketcherNewPoint::SketcherNewPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1347 : BC_GenericButton(x, y, 64, _("New"))
1350 this->plugin = plugin;
1352 SketcherNewPoint::~SketcherNewPoint()
1355 int SketcherNewPoint::handle_event()
1357 int pi = plugin->config.pt_selected;
1358 int arc = gui->point_type->type;
1359 int k = plugin->new_point(pi+1, arc);
1360 gui->point_list->update(k);
1361 gui->send_configure_change();
1365 SketcherDelPoint::SketcherDelPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1366 : BC_GenericButton(x, y, 64, C_("Del"))
1369 this->plugin = plugin;
1371 SketcherDelPoint::~SketcherDelPoint()
1374 int SketcherDelPoint::handle_event()
1376 SketcherConfig &config = gui->plugin->config;
1377 SketcherCurves &curves = config.curves;
1378 int ci = config.cv_selected;
1379 if( ci < 0 || ci >= curves.size() )
1381 SketcherCurve *cv = curves[ci];
1382 SketcherPoints &points = cv->points;
1383 int pi = config.pt_selected;
1385 ArrayList<int> selected;
1386 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1389 for( int i=selected.size(); --i>=0; ) {
1390 int k = selected[i];
1391 if( k < 0 || k >= points.size() ) continue;
1392 points.remove_object_number(k);
1393 if( k == pi && --pi < 0 && points.size() > 0 ) pi = 0;
1395 gui->point_list->update(pi);
1396 gui->send_configure_change();
1400 SketcherResetCurves::SketcherResetCurves(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1401 : BC_GenericButton(x, y, _("Reset"))
1404 this->plugin = plugin;
1406 SketcherResetCurves::~SketcherResetCurves()
1409 int SketcherResetCurves::handle_event()
1411 SketcherConfig &config = plugin->config;
1412 config.curves.remove_all_objects();
1413 int ci = plugin->new_curve();
1414 gui->curve_list->update(ci);
1415 gui->point_list->update(-1);
1416 gui->send_configure_change();
1420 SketcherResetPoints::SketcherResetPoints(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1421 : BC_GenericButton(x, y, _("Reset"))
1424 this->plugin = plugin;
1426 SketcherResetPoints::~SketcherResetPoints()
1429 int SketcherResetPoints::handle_event()
1431 SketcherConfig &config = gui->plugin->config;
1432 int ci = config.cv_selected;
1433 if( ci >= 0 && ci < config.curves.size() ) {
1434 SketcherCurve *cv = config.curves[ci];
1435 cv->points.remove_all_objects();
1436 gui->point_list->update(-1);
1437 gui->send_configure_change();