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::done_event(int result)
580 delete color_picker; color_picker = 0;
583 void SketcherWindow::send_configure_change()
586 plugin->send_configure_change();
590 int SketcherWindow::grab_event(XEvent *event)
592 int ret = do_grab_event(event);
593 if( !grab_event_count() ) {
594 if( grab_cursor_motion() )
596 if( pending_config ) {
597 last_x = output_x; last_y = output_y;
598 send_configure_change();
604 int SketcherWindow::do_grab_event(XEvent *event)
606 switch( event->type ) {
607 case ButtonPress: break;
608 case ButtonRelease: break;
609 case MotionNotify: break;
611 if( keysym_lookup(event) > 0 ) {
612 switch( get_keysym() ) {
615 return (event->xkey.state & ShiftMask) ?
616 del_curve->handle_event() :
617 del_point->handle_event() ;
624 MWindow *mwindow = plugin->server->mwindow;
625 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
626 CWindowCanvas *canvas = cwindow_gui->canvas;
627 int cx, cy; cwindow_gui->get_relative_cursor(cx, cy);
628 cx -= mwindow->theme->ccanvas_x;
629 cy -= mwindow->theme->ccanvas_y;
632 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ||
633 cy < 0 || cy >= mwindow->theme->ccanvas_h )
637 switch( event->type ) {
639 if( dragging ) return 0;
646 if( dragging ) break;
647 default: // fall thru
651 cursor_x = cx, cursor_y = cy;
652 canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
653 position = plugin->get_source_position();
654 Track *track = plugin->server->plugin->track;
655 track_w = track->track_w;
656 track_h = track->track_h;
657 track->automation->get_projector(
658 &projector_x, &projector_y, &projector_z,
659 position, PLAY_FORWARD);
660 projector_x += mwindow->edl->session->output_w / 2;
661 projector_y += mwindow->edl->session->output_h / 2;
662 output_x = (cursor_x - projector_x) / projector_z + track_w / 2;
663 output_y = (cursor_y - projector_y) / projector_z + track_h / 2;
664 state = event->xmotion.state;
666 if( event->type == MotionNotify ) {
667 memcpy(&motion_event, event, sizeof(motion_event));
671 if( grab_cursor_motion() )
674 switch( event->type ) {
676 pending_config = grab_button_press(event);
686 int SketcherWindow::grab_button_press(XEvent *event)
688 SketcherConfig &config = plugin->config;
689 int ci = config.cv_selected;
690 if( ci < 0 || ci >= plugin->config.curves.size() )
692 SketcherCurves &curves = config.curves;
693 SketcherCurve *cv = curves[ci];
694 SketcherPoints &points = cv->points;
695 int pi = config.pt_selected;
697 int button_no = event->xbutton.button;
698 switch( button_no ) {
700 if( (state & ShiftMask) ) { // create new point/string
702 pi = plugin->new_point(cv,
703 !(state & ControlMask) ? ARC_LINE : ARC_FILL,
704 output_x, output_y, pi+1);
705 point_list->update(pi);
708 SketcherPoint *pt = 0; // select point
709 double dist = cv->nearest_point(pi, output_x,output_y);
712 Track *track = plugin->server->plugin->track;
713 int track_w = track->track_w, track_h = track->track_h;
714 float px = (pt->x - track_w / 2) * projector_z + projector_x;
715 float py = (pt->y - track_h / 2) * projector_z + projector_y;
716 float pix = DISTANCE(px, py, cursor_x,cursor_y);
717 if( (state & ControlMask) && pix >= HANDLE_W ) { pi = -1; pt = 0; }
719 point_list->set_selected(pi);
722 if( (state & ShiftMask) ) { // create new curve point
724 pi = plugin->new_point(cv,
725 !(state & ControlMask) ? ARC_CURVE : ARC_OFF,
726 output_x, output_y, pi+1);
727 point_list->update(pi);
730 if( (state & AltMask) ) { // create new curve
731 ci = plugin->new_curve(cv->pen, cv->width, curve_color->color);
732 curve_list->update(ci);
733 point_list->update(-1);
736 SketcherPoint *pt = 0;
737 double dist = config.nearest_point(ci, pi, output_x,output_y);
739 pt = curves[ci]->points[pi];
740 Track *track = plugin->server->plugin->track;
741 int track_w = track->track_w, track_h = track->track_h;
742 float px = (pt->x - track_w / 2) * projector_z + projector_x;
743 float py = (pt->y - track_h / 2) * projector_z + projector_y;
744 float pix = DISTANCE(px, py, cursor_x,cursor_y);
745 if( (state & ControlMask) && pix >= HANDLE_W ) { ci = pi = -1; pt = 0; }
748 curve_list->update(ci);
749 point_list->update(pi);
756 int SketcherWindow::grab_cursor_motion()
758 if( !pending_motion )
761 SketcherConfig &config = plugin->config;
762 int ci = config.cv_selected;
763 if( ci < 0 || ci >= plugin->config.curves.size() )
765 SketcherCurves &curves = config.curves;
766 SketcherCurve *cv = curves[ci];
767 SketcherPoints &points = cv->points;
768 int pi = config.pt_selected;
770 if( (state & ShiftMask) ) { // string of points
771 if( (state & (Button1Mask|Button3Mask)) ) {
772 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
774 float dist = DISTANCE(pt->x, pt->y, output_x, output_y);
775 if( dist < get_w()*0.1 ) return 0; // tolerance w/10
778 int arc = (state & Button1Mask) ? ARC_LINE : ARC_CURVE;
779 pi = plugin->new_point(cv, arc, output_x, output_y, pi+1);
780 point_list->update(pi);
784 if( (state & Button1Mask) ) {
785 if( (state & ControlMask) ) { // drag selected point
786 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
788 point_list->set_point(pi, PT_X, pt->x = output_x);
789 point_list->set_point(pi, PT_Y, pt->y = output_y);
790 point_list->update_list(pi);
791 point_x->update(pt->x);
792 point_y->update(pt->y);
796 if( (state & AltMask) ) { // drag all curves
797 int dx = round(output_x - last_x);
798 int dy = round(output_y - last_y);
799 for( int i=0; i<curves.size(); ++i ) {
800 SketcherCurve *crv = plugin->config.curves[i];
801 int pts = crv->points.size();
802 for( int k=0; k<pts; ++k ) {
803 SketcherPoint *pt = crv->points[k];
804 pt->x += dx; pt->y += dy;
807 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
809 point_x->update(pt ? pt->x : 0.f);
810 point_y->update(pt ? pt->y : 0.f);
811 point_id->update(pt ? pt->id : 0);
812 point_list->update(pi);
815 double dist = cv->nearest_point(pi, output_x,output_y);
817 point_list->set_selected(pi);
820 if( (state & Button3Mask) ) {
821 if( (state & (ControlMask | AltMask)) ) { // drag selected curve(s)
822 int dx = round(output_x - last_x);
823 int dy = round(output_y - last_y);
824 for( int i=0; i<points.size(); ++i ) {
825 SketcherPoint *pt = points[i];
826 pt->x += dx; pt->y += dy;
828 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
830 point_x->update(pt ? pt->x : 0.f);
831 point_y->update(pt ? pt->y : 0.f);
832 point_id->update(pt ? pt->id : 0);
833 point_list->update(pi);
836 double dist = config.nearest_point(ci, pi, output_x,output_y);
838 curve_list->update(ci);
839 point_list->update(pi);
846 int SketcherWindow::keypress_event()
848 int key = get_keypress();
850 case DELETE: return shift_down() ?
851 del_curve->handle_event() :
852 del_point->handle_event() ;
857 void SketcherWindow::start_color_thread(SketcherCurveColor *color_button)
861 color_picker = new SketcherCurveColorPicker(this, color_button);
862 int color = CV_COLOR, ci = plugin->config.cv_selected;
863 if( ci >= 0 && ci < plugin->config.curves.size() ) {
864 SketcherCurve *cv = plugin->config.curves[ci];
867 color_picker->start(color);
868 lock_window("SketcherWindow::start_color_thread");
872 SketcherCurveList::SketcherCurveList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
873 : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
876 this->plugin = plugin;
877 col_titles[CV_ID] = _("ID"); col_widths[CV_ID] = 64;
878 col_titles[CV_RAD] = _("width"); col_widths[CV_RAD] = 64;
879 col_titles[CV_PEN] = _("pen"); col_widths[CV_PEN] = 64;
880 col_titles[CV_CLR] = _("color"); col_widths[CV_CLR] = 80;
881 col_titles[CV_ALP] = _("alpha"); col_widths[CV_ALP] = 64;
883 SketcherCurveList::~SketcherCurveList()
887 void SketcherCurveList::clear()
889 for( int i=CV_SZ; --i>=0; )
890 cols[i].remove_all_objects();
893 int SketcherCurveList::column_resize_event()
895 for( int i=CV_SZ; --i>=0; )
896 col_widths[i] = get_column_width(i);
900 int SketcherCurveList::handle_event()
902 int ci = get_selection_number(0, 0);
904 gui->point_list->update(0);
905 gui->send_configure_change();
909 int SketcherCurveList::selection_changed()
915 void SketcherCurveList::set_selected(int k)
918 if( k >= 0 && k < plugin->config.curves.size() ) {
919 SketcherCurve *cv = plugin->config.curves[k];
920 gui->curve_width->update(cv->width);
921 gui->curve_pen->update(cv->pen);
922 gui->curve_color->update_gui(cv->color);
925 plugin->config.cv_selected = ci;
929 void SketcherCurveList::update_list(int k)
931 int xpos = get_xposition(), ypos = get_yposition();
932 if( k >= 0 ) update_selection(&cols[0], k);
933 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],CV_SZ, xpos,ypos,k);
937 void SketcherCurveList::update(int k)
940 SketcherCurves &curves = plugin->config.curves;
941 int sz = curves.size();
942 for( int i=0; i<sz; ++i ) {
943 SketcherCurve *cv = curves[i];
944 char itxt[BCSTRLEN]; sprintf(itxt,"%d", cv->id);
945 char ptxt[BCSTRLEN]; sprintf(ptxt,"%s", cv_pen[cv->pen]);
946 char rtxt[BCSTRLEN]; sprintf(rtxt,"%d", cv->width);
947 int color = cv->color;
948 int r = (color>>16)&0xff;
949 int g = (color>> 8)&0xff;
950 int b = (color>> 0)&0xff;
951 int a = (~color>>24)&0xff;
952 char ctxt[BCSTRLEN]; sprintf(ctxt,"#%02x%02x%02x", r, g, b);
953 char atxt[BCSTRLEN]; sprintf(atxt,"%5.3f", a/255.);
954 add_curve(itxt, ptxt, rtxt, ctxt, atxt);
959 void SketcherCurveList::add_curve(const char *id, const char *pen,
960 const char *width, const char *color, const char *alpha)
962 cols[CV_ID].append(new BC_ListBoxItem(id));
963 cols[CV_RAD].append(new BC_ListBoxItem(width));
964 cols[CV_PEN].append(new BC_ListBoxItem(pen));
965 cols[CV_CLR].append(new BC_ListBoxItem(color));
966 cols[CV_ALP].append(new BC_ListBoxItem(alpha));
969 SketcherNewCurve::SketcherNewCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
970 : BC_GenericButton(x, y, 64, _("New"))
973 this->plugin = plugin;
975 SketcherNewCurve::~SketcherNewCurve()
978 int SketcherNewCurve::handle_event()
980 int pen = gui->curve_pen->pen;
981 int color = gui->curve_color->color;
982 int width = gui->curve_width->width;
983 int ci = plugin->config.cv_selected;
984 if( ci >= 0 && ci < plugin->config.curves.size() ) {
985 SketcherCurve *cv = plugin->config.curves[ci];
986 pen = cv->pen; width = cv->width; color = cv->color;
988 ci = plugin->new_curve(pen, width, color);
989 gui->curve_list->update(ci);
990 gui->point_list->update(-1);
991 gui->send_configure_change();
995 SketcherDelCurve::SketcherDelCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
996 : BC_GenericButton(x, y, 64, C_("Del"))
999 this->plugin = plugin;
1001 SketcherDelCurve::~SketcherDelCurve()
1004 int SketcherDelCurve::handle_event()
1006 SketcherConfig &config = plugin->config;
1007 int ci = config.cv_selected;
1008 SketcherCurves &curves = config.curves;
1009 if( ci >= 0 && ci < curves.size() ) {
1010 curves.remove_object_number(ci--);
1011 if( ci < 0 ) ci = 0;
1012 if( !curves.size() )
1013 ci = plugin->new_curve();
1014 gui->curve_list->update(ci);
1015 gui->point_list->update(-1);
1016 gui->send_configure_change();
1021 SketcherCurveUp::SketcherCurveUp(SketcherWindow *gui, int x, int y)
1022 : BC_GenericButton(x, y, _("Up"))
1026 SketcherCurveUp::~SketcherCurveUp()
1030 int SketcherCurveUp::handle_event()
1032 SketcherConfig &config = gui->plugin->config;
1033 int ci = config.cv_selected;
1034 SketcherCurves &curves = config.curves;
1035 if( ci > 0 && ci < curves.size() ) {
1036 SketcherCurve *&cv0 = curves[ci];
1037 SketcherCurve *&cv1 = curves[--ci];
1038 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
1039 gui->curve_list->update(ci);
1041 gui->send_configure_change();
1045 SketcherCurveDn::SketcherCurveDn(SketcherWindow *gui, int x, int y)
1046 : BC_GenericButton(x, y, _("Dn"))
1050 SketcherCurveDn::~SketcherCurveDn()
1054 int SketcherCurveDn::handle_event()
1056 SketcherConfig &config = gui->plugin->config;
1057 int ci = config.cv_selected;
1058 SketcherCurves &curves = config.curves;
1059 if( ci >= 0 && ci < curves.size()-1 ) {
1060 SketcherCurve *&cv0 = curves[ci];
1061 SketcherCurve *&cv1 = curves[++ci];
1062 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
1063 gui->curve_list->update(ci);
1065 gui->send_configure_change();
1070 SketcherPointTypeItem::SketcherPointTypeItem(int arc)
1071 : BC_MenuItem(_(pt_type[arc]))
1075 int SketcherPointTypeItem::handle_event()
1077 SketcherPointType *popup = (SketcherPointType*)get_popup_menu();
1079 SketcherWindow *gui = popup->gui;
1080 SketcherConfig &config = gui->plugin->config;
1081 SketcherCurves &curves = config.curves;
1082 int ci = config.cv_selected;
1083 if( ci < 0 || ci >= curves.size() )
1085 SketcherCurve *cv = curves[ci];
1086 SketcherPoints &points = cv->points;
1087 int pi = config.pt_selected;
1089 ArrayList<int> selected;
1090 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1093 for( int i=selected.size(); --i>=0; ) {
1094 int k = selected[i];
1095 if( k < 0 || k >= points.size() ) continue;
1096 SketcherPoint *pt = cv->points[k];
1098 gui->point_list->set_point(k, PT_TY, _(pt_type[arc]));
1101 gui->point_list->update_list(pi);
1102 gui->send_configure_change();
1106 SketcherPointType::SketcherPointType(SketcherWindow *gui, int x, int y, int arc)
1107 : BC_PopupMenu(x,y,64,_(pt_type[arc]))
1112 void SketcherPointType::create_objects()
1114 for( int arc=0; arc<PT_SZ; ++arc )
1115 add_item(new SketcherPointTypeItem(arc));
1117 void SketcherPointType::update(int arc)
1119 set_text(_(pt_type[this->type=arc]));
1123 SketcherPointList::SketcherPointList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1124 : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
1127 this->plugin = plugin;
1128 col_titles[PT_ID] = _("ID"); col_widths[PT_ID] = 50;
1129 col_titles[PT_TY] = _("Type"); col_widths[PT_TY] = 80;
1130 col_titles[PT_X] = _("X"); col_widths[PT_X] = 90;
1131 col_titles[PT_Y] = _("Y"); col_widths[PT_Y] = 90;
1132 set_selection_mode(LISTBOX_MULTIPLE);
1134 SketcherPointList::~SketcherPointList()
1138 void SketcherPointList::clear()
1140 for( int i=PT_SZ; --i>=0; )
1141 cols[i].remove_all_objects();
1144 int SketcherPointList::column_resize_event()
1146 for( int i=PT_SZ; --i>=0; )
1147 col_widths[i] = get_column_width(i);
1151 int SketcherPointList::handle_event()
1153 int pi = get_selection_number(0, 0);
1154 if( get_selection_number(0, 1) >= 0 ) pi = -1;
1156 gui->send_configure_change();
1160 int SketcherPointList::selection_changed()
1166 void SketcherPointList::add_point(const char *id, const char *ty, const char *xp, const char *yp)
1168 cols[PT_ID].append(new BC_ListBoxItem(id));
1169 cols[PT_TY].append(new BC_ListBoxItem(ty));
1170 cols[PT_X].append(new BC_ListBoxItem(xp));
1171 cols[PT_Y].append(new BC_ListBoxItem(yp));
1174 void SketcherPointList::set_point(int i, int c, int v)
1176 char stxt[BCSTRLEN];
1177 sprintf(stxt,"%d",v);
1178 set_point(i,c,stxt);
1180 void SketcherPointList::set_point(int i, int c, coord v)
1182 char stxt[BCSTRLEN];
1183 sprintf(stxt,"%0.1f",v);
1184 set_point(i,c,stxt);
1186 void SketcherPointList::set_point(int i, int c, const char *cp)
1188 cols[c].get(i)->set_text(cp);
1191 void SketcherPointList::set_selected(int k)
1193 SketcherPoint *pt = 0;
1194 int ci = plugin->config.cv_selected, pi = -1;
1195 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1196 SketcherCurve *cv = plugin->config.curves[ci];
1197 pt = k >= 0 && k < cv->points.size() ? cv->points[pi=k] : 0;
1199 gui->point_type->update(pt ? pt->arc : ARC_OFF);
1200 gui->point_x->update(pt ? pt->x : 0.f);
1201 gui->point_y->update(pt ? pt->y : 0.f);
1202 gui->point_id->update(pt ? pt->id : 0);
1203 plugin->config.pt_selected = pi;
1206 void SketcherPointList::update_list(int k)
1208 int xpos = get_xposition(), ypos = get_yposition();
1209 if( k >= 0 ) update_selection(&cols[0], k);
1210 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],PT_SZ, xpos,ypos,k);
1213 void SketcherPointList::update(int k)
1216 int ci = plugin->config.cv_selected, sz = 0;
1217 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1218 SketcherCurve *cv = plugin->config.curves[ci];
1219 SketcherPoints &points = cv->points;
1221 for( int i=0; i<sz; ++i ) {
1222 SketcherPoint *pt = points[i];
1223 char itxt[BCSTRLEN]; sprintf(itxt,"%d", pt->id);
1224 char ttxt[BCSTRLEN]; sprintf(ttxt,"%s", _(pt_type[pt->arc]));
1225 char xtxt[BCSTRLEN]; sprintf(xtxt,"%0.1f", pt->x);
1226 char ytxt[BCSTRLEN]; sprintf(ytxt,"%0.1f", pt->y);
1227 add_point(itxt, ttxt, xtxt, ytxt);
1233 void SketcherWindow::update_gui()
1235 SketcherConfig &config = plugin->config;
1236 int ci = config.cv_selected;
1237 int pi = config.pt_selected;
1238 curve_list->update(ci);
1239 point_list->update(pi);
1240 SketcherCurve *cv = ci >= 0 ? config.curves[ci] : 0;
1241 curve_width->update(cv ? cv->width : 1);
1242 curve_pen->update(cv ? cv->pen : PEN_SQUARE);
1243 curve_color->set_color(cv ? cv->color : CV_COLOR);
1244 SketcherPoint *pt = pi >= 0 ? cv->points[pi] : 0;
1245 point_x->update(pt ? pt->x : 0);
1246 point_y->update(pt ? pt->y : 0);
1247 point_id->update(pt ? pt->id : 0);
1248 drag->update(plugin->config.drag);
1252 SketcherPointUp::SketcherPointUp(SketcherWindow *gui, int x, int y)
1253 : BC_GenericButton(x, y, _("Up"))
1257 SketcherPointUp::~SketcherPointUp()
1261 int SketcherPointUp::handle_event()
1263 SketcherConfig &config = gui->plugin->config;
1264 int ci = config.cv_selected;
1265 if( ci < 0 || ci >= config.curves.size() )
1267 SketcherCurve *cv = config.curves[ci];
1268 SketcherPoints &points = cv->points;
1269 if( points.size() < 2 )
1271 int pi = config.pt_selected;
1273 ArrayList<int> selected;
1274 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1277 for( int i=0; i<selected.size(); ++i ) {
1278 int k = selected[i];
1279 if( k <= 0 ) continue;
1281 SketcherPoint *&pt0 = points[k];
1282 SketcherPoint *&pt1 = points[--k];
1283 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1285 gui->point_list->update(pi);
1286 gui->send_configure_change();
1290 SketcherPointDn::SketcherPointDn(SketcherWindow *gui, int x, int y)
1291 : BC_GenericButton(x, y, _("Dn"))
1295 SketcherPointDn::~SketcherPointDn()
1299 int SketcherPointDn::handle_event()
1301 SketcherConfig &config = gui->plugin->config;
1302 int ci = config.cv_selected;
1303 if( ci < 0 || ci >= config.curves.size() )
1305 SketcherCurve *cv = config.curves[ci];
1306 SketcherPoints &points = cv->points;
1307 int sz1 = points.size()-1;
1310 int pi = config.pt_selected;
1312 ArrayList<int> selected;
1313 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1316 for( int i=selected.size(); --i>=0; ) {
1317 int k = selected[i];
1318 if( k >= sz1 ) continue;
1320 SketcherPoint *&pt0 = points[k];
1321 SketcherPoint *&pt1 = points[++k];
1322 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1324 gui->point_list->update(pi);
1325 gui->send_configure_change();
1329 SketcherDrag::SketcherDrag(SketcherWindow *gui, int x, int y)
1330 : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
1334 int SketcherDrag::handle_event()
1336 CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
1337 int value = get_value();
1339 if( !gui->grab(cwindow_gui) ) {
1345 gui->ungrab(cwindow_gui);
1346 gui->plugin->config.drag = value;
1347 gui->send_configure_change();
1351 SketcherNewPoint::SketcherNewPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1352 : BC_GenericButton(x, y, 64, _("New"))
1355 this->plugin = plugin;
1357 SketcherNewPoint::~SketcherNewPoint()
1360 int SketcherNewPoint::handle_event()
1362 int pi = plugin->config.pt_selected;
1363 int arc = gui->point_type->type;
1364 int k = plugin->new_point(pi+1, arc);
1365 gui->point_list->update(k);
1366 gui->send_configure_change();
1370 SketcherDelPoint::SketcherDelPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1371 : BC_GenericButton(x, y, 64, C_("Del"))
1374 this->plugin = plugin;
1376 SketcherDelPoint::~SketcherDelPoint()
1379 int SketcherDelPoint::handle_event()
1381 SketcherConfig &config = gui->plugin->config;
1382 SketcherCurves &curves = config.curves;
1383 int ci = config.cv_selected;
1384 if( ci < 0 || ci >= curves.size() )
1386 SketcherCurve *cv = curves[ci];
1387 SketcherPoints &points = cv->points;
1388 int pi = config.pt_selected;
1390 ArrayList<int> selected;
1391 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1394 for( int i=selected.size(); --i>=0; ) {
1395 int k = selected[i];
1396 if( k < 0 || k >= points.size() ) continue;
1397 points.remove_object_number(k);
1398 if( k == pi && --pi < 0 && points.size() > 0 ) pi = 0;
1400 gui->point_list->update(pi);
1401 gui->send_configure_change();
1405 SketcherResetCurves::SketcherResetCurves(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1406 : BC_GenericButton(x, y, _("Reset"))
1409 this->plugin = plugin;
1411 SketcherResetCurves::~SketcherResetCurves()
1414 int SketcherResetCurves::handle_event()
1416 SketcherConfig &config = plugin->config;
1417 config.curves.remove_all_objects();
1418 int ci = plugin->new_curve();
1419 gui->curve_list->update(ci);
1420 gui->point_list->update(-1);
1421 gui->send_configure_change();
1425 SketcherResetPoints::SketcherResetPoints(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1426 : BC_GenericButton(x, y, _("Reset"))
1429 this->plugin = plugin;
1431 SketcherResetPoints::~SketcherResetPoints()
1434 int SketcherResetPoints::handle_event()
1436 SketcherConfig &config = gui->plugin->config;
1437 int ci = config.cv_selected;
1438 if( ci >= 0 && ci < config.curves.size() ) {
1439 SketcherCurve *cv = config.curves[ci];
1440 cv->points.remove_all_objects();
1441 gui->point_list->update(-1);
1442 gui->send_configure_change();