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
42 #define COLOR_W xS(40)
43 #define COLOR_H yS(24)
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,xS(100),_(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,
100 int x, int y, int w, int h, int color, int alpha)
101 : ColorBoxButton(_("Curve Color"), x, y, w, h, color, alpha, 1)
104 this->color = CV_COLOR;
107 SketcherCurveColor::~SketcherCurveColor()
111 void SketcherCurveColor::handle_done_event(int result)
113 if( result ) color = orig_color | (~orig_alpha<<24);
114 SketcherConfig &config = gui->plugin->config;
115 int ci = config.cv_selected;
116 if( ci >= 0 && ci < config.curves.size() ) {
117 SketcherCurve *cv = config.curves[ci];
119 gui->lock_window("SketcherCurveColor::handle_done_event");
120 gui->curve_list->update(ci);
121 gui->unlock_window();
122 gui->send_configure_change();
126 int SketcherCurveColor::handle_new_color(int color, int alpha)
128 color |= ~alpha<<24; this->color = color;
129 gui->lock_window("SketcherCurveColor::update_gui");
131 SketcherConfig &config = gui->plugin->config;
132 int ci = config.cv_selected;
134 SketcherCurve *cv = config.curves[ci];
136 gui->curve_list->update(ci);
137 gui->send_configure_change();
139 gui->unlock_window();
144 SketcherCoord::SketcherCoord(SketcherWindow *gui, int x, int y,
145 coord output, coord mn, coord mx)
146 : BC_TumbleTextBox(gui, output, mn, mx, x, y, xS(80), 1)
151 SketcherCoord::~SketcherCoord()
155 SketcherNum::SketcherNum(SketcherWindow *gui, int x, int y,
156 int output, int mn, int mx)
157 : BC_TumbleTextBox(gui, output, mn, mx, x, y, xS(54))
162 SketcherNum::~SketcherNum()
166 int SketcherPointX::handle_event()
168 if( !SketcherCoord::handle_event() ) return 0;
169 SketcherConfig &config = gui->plugin->config;
170 int ci = config.cv_selected;
171 if( ci >= 0 && ci < config.curves.size() ) {
172 SketcherCurve *cv = config.curves[ci];
173 SketcherPointList *point_list = gui->point_list;
174 int pi = config.pt_selected;
175 SketcherPoints &points = cv->points;
176 if( pi >= 0 && pi < points.size() ) {
177 coord v = atof(get_text());
179 point_list->set_point(pi, PT_X, v);
180 point_list->update_list(pi);
181 gui->send_configure_change();
186 int SketcherPointY::handle_event()
188 if( !SketcherCoord::handle_event() ) return 0;
189 SketcherConfig &config = gui->plugin->config;
190 int ci = config.cv_selected;
191 if( ci >= 0 && ci < config.curves.size() ) {
192 SketcherCurve *cv = config.curves[ci];
193 SketcherPointList *point_list = gui->point_list;
194 int pi = config.pt_selected;
195 SketcherPoints &points = cv->points;
196 if( pi >= 0 && pi < points.size() ) {
197 coord v = atof(get_text());
199 point_list->set_point(pi, PT_Y, v);
200 point_list->update_list(pi);
201 gui->send_configure_change();
207 int SketcherPointId::handle_event()
209 if( !SketcherNum::handle_event() ) return 0;
210 SketcherConfig &config = gui->plugin->config;
211 int ci = config.cv_selected;
212 if( ci >= 0 && ci < config.curves.size() ) {
213 SketcherCurve *cv = config.curves[ci];
214 SketcherPointList *point_list = gui->point_list;
215 int pi = config.pt_selected;
216 SketcherPoints &points = cv->points;
217 if( pi >= 0 && pi < points.size() ) {
218 int id = atoi(get_text());
220 point_list->set_point(pi, PT_ID, id);
221 point_list->update_list(pi);
222 gui->send_configure_change();
228 SketcherCurveWidth::SketcherCurveWidth(SketcherWindow *gui, int x, int y, int width)
229 : SketcherNum(gui, x, y, width, 0, 255)
233 SketcherCurveWidth::~SketcherCurveWidth()
237 int SketcherCurveWidth::handle_event()
239 if( !SketcherNum::handle_event() ) return 0;
240 SketcherConfig &config = gui->plugin->config;
241 int ci = config.cv_selected;
242 if( ci >= 0 && ci < config.curves.size() ) {
243 SketcherCurve *cv = config.curves[ci];
244 int v = atoi(get_text());
246 gui->curve_list->update(ci);
247 gui->send_configure_change();
252 void SketcherCurveWidth::update(int width)
254 SketcherNum::update(this->width=width);
258 SketcherAliasItem::SketcherAliasItem(SketcherAliasing *popup, int v)
259 : BC_MenuItem(popup->alias_to_text(v))
265 int SketcherAliasItem::handle_event()
267 popup->set_text(get_text());
268 popup->plugin->config.aliasing = v;
269 popup->gui->send_configure_change();
274 SketcherAliasing::SketcherAliasing(SketcherWindow *gui, Sketcher *plugin,
276 : BC_PopupMenu(x, y, xS(80),
277 alias_to_text(plugin->config.aliasing), 1, 0, xS(3))
280 this->plugin = plugin;
282 SketcherAliasing::~SketcherAliasing()
286 void SketcherAliasing::create_objects()
288 add_item(new SketcherAliasItem(this, -1));
289 add_item(new SketcherAliasItem(this, 0));
290 add_item(new SketcherAliasItem(this, 1));
293 const char *SketcherAliasing::alias_to_text(int alias)
295 if( alias < 0 ) return _("Off");
296 if( alias > 0 ) return _("Dbl");
302 SketcherWindow::SketcherWindow(Sketcher *plugin)
303 : PluginClientWindow(plugin, xS(460), yS(680), xS(460), yS(680), 0)
305 this->plugin = plugin;
306 this->title_pen = 0; this->curve_pen = 0;
307 this->title_color = 0; this->curve_color = 0;
309 this->new_curve = 0; this->del_curve = 0;
310 this->curve_up = 0; this->curve_dn = 0;
311 this->title_x = 0; this->point_x = 0;
312 this->title_y = 0; this->point_y = 0;
313 this->title_id = 0; this->point_id = 0;
314 this->new_point = 0; this->del_point = 0;
315 this->point_up = 0; this->point_dn = 0;
316 this->point_list = 0; this->notes0 = 0;
317 this->notes1 = 0; this->notes2 = 0;
320 track_w = track_h -1;
321 output_x = output_y = -1;
322 track_x = track_y = -1;
323 last_x = last_y = -1;
324 projector_x = projector_y = projector_z = -1;
325 state = 0; dragging = 0;
334 SketcherWindow::~SketcherWindow()
341 void SketcherWindow::create_objects()
343 int x = xS(10), y = yS(10), dy = 0, x1, y1;
344 int margin = plugin->get_theme()->widget_border;
346 int ci = plugin->config.cv_selected;
347 if( ci < 0 || ci >= plugin->config.curves.size() )
348 ci = plugin->new_curve();
349 SketcherCurve *cv = plugin->config.curves[ci];
351 reset_curves = new SketcherResetCurves(this, plugin, x1=x, y);
352 add_subwindow(reset_curves); dy = bmax(dy,reset_curves->get_h());
353 x1 += reset_curves->get_w() + 2*margin;
354 const char *curve_text = _("Curve");
355 title_width = new BC_Title(x1, y, _("Width:"));
356 add_subwindow(title_width); dy = bmax(dy,title_width->get_h());
357 x1 += title_width->get_w() + margin;
358 curve_width = new SketcherCurveWidth(this, x1, y, cv->width);
359 curve_width->create_objects();
360 x1 += curve_width->get_w() + margin;
361 aliasing = new SketcherAliasing(this, plugin, x1, y);
362 add_subwindow(aliasing); dy = bmax(dy,aliasing->get_h());
363 aliasing->create_objects();
364 y += dy + 2*margin; dy = 0;
366 x1 = get_w()-x - BC_Title::calculate_w(this, curve_text, LARGEFONT);
367 y1 = y-margin - BC_Title::calculate_h(this, curve_text, LARGEFONT);
368 title = new BC_Title(x1, y1, curve_text, LARGEFONT,
369 get_resources()->menu_highlighted_fontcolor);
370 add_subwindow(title); dy = bmax(dy,title->get_h());
371 curve_list = new SketcherCurveList(this, plugin, x, y);
372 add_subwindow(curve_list); dy = bmax(dy,curve_list->get_h());
373 y += dy + margin; dy = 0;
375 new_curve = new SketcherNewCurve(this, plugin, x1=x, y);
376 add_subwindow(new_curve); dy = bmax(dy,new_curve->get_h());
377 x1 += new_curve->get_w() + margin;
378 curve_up = new SketcherCurveUp(this, x1, y);
379 add_subwindow(curve_up); dy = bmax(dy,curve_up->get_h());
380 x1 += curve_up->get_w() + 4*margin;
381 y1 = BC_Title::calculate_h(this, _("Pen:"));
382 title_pen = new BC_Title(x1+xS(30), y+dy-y1, _("Pen:"));
383 add_subwindow(title_pen); dy = bmax(dy,title_pen->get_h());
384 int x2 = (get_w()+x1)/2 + xS(20);
385 y1 = BC_Title::calculate_h(this, _("Color:"));
386 title_color = new BC_Title(x2, y+dy-y1, _("Color:"));
387 add_subwindow(title_color); dy = bmax(dy,title_color->get_h());
388 y += dy + margin; dy = 0;
390 del_curve = new SketcherDelCurve(this, plugin, x1=x, y);
391 add_subwindow(del_curve); dy = bmax(dy,del_curve->get_h());
392 x1 += del_curve->get_w() + margin;
393 curve_dn = new SketcherCurveDn(this, x1, y);
394 add_subwindow(curve_dn); dy = bmax(dy,curve_dn->get_h());
395 x1 += curve_dn->get_w() + 4*margin;
396 curve_pen = new SketcherCurvePen(this, x1, y, cv->pen);
397 add_subwindow(curve_pen); dy = bmax(dy,curve_pen->get_h());
398 curve_pen->create_objects();
399 curve_color = new SketcherCurveColor(this, x2, y, COLOR_W, COLOR_H,
400 cv->color&0xffffff, (~cv->color>>24)&0xff);
401 add_subwindow(curve_color); dy = bmax(dy,curve_color->get_h());
402 y += dy + margin; dy = 0;
403 curve_list->update(ci);
406 bar = new BC_Bar(x, y, get_w()-xS(2)*x);
407 add_subwindow(bar); dy = bmax(dy,bar->get_h());
408 bar = new BC_Bar(x, y+=dy, get_w()-xS(2)*x);
409 add_subwindow(bar); dy = bmax(dy,bar->get_h());
410 y += dy + yS(2)*margin;
412 int pi = plugin->config.pt_selected;
413 SketcherPoint *pt = pi >= 0 && pi < cv->points.size() ? cv->points[pi] : 0;
414 reset_points = new SketcherResetPoints(this, plugin, x1=x, y+yS(3));
415 add_subwindow(reset_points); dy = bmax(dy,reset_points->get_h());
416 x1 += reset_points->get_w() + xS(2)*margin;
417 if( plugin->config.drag ) {
418 if( !grab(plugin->server->mwindow->cwindow->gui) ) {
419 eprintf("drag enabled, but compositor already grabbed\n");
420 plugin->config.drag = 0;
423 drag = new SketcherDrag(this, x1, y);
424 add_subwindow(drag); dy = bmax(dy,drag->get_h());
425 x1 += drag->get_w() + xS(2)*margin;
426 int arc = pt ? pt->arc : ARC_LINE;
427 point_type = new SketcherPointType(this, x1, y, arc);
428 add_subwindow(point_type); dy = bmax(dy,point_type->get_h());
429 point_type->create_objects();
430 y += dy + margin; dy = 0;
432 const char *point_text = _("Point");
433 x1 = get_w()-x - BC_Title::calculate_w(this, point_text, LARGEFONT);
434 y1 = y-margin - BC_Title::calculate_h(this, point_text, LARGEFONT);
435 add_subwindow(title = new BC_Title(x1, y1, point_text, LARGEFONT,
436 get_resources()->menu_highlighted_fontcolor));
437 point_list = new SketcherPointList(this, plugin, x, y);
438 add_subwindow(point_list); dy = bmax(dy,point_list->get_h());
439 y += dy + margin; dy = 0;
441 new_point = new SketcherNewPoint(this, plugin, x1=x, y);
442 add_subwindow(new_point); dy = bmax(dy,new_point->get_h());
443 x1 += new_point->get_w() + margin;
444 point_up = new SketcherPointUp(this, x1, y);
445 add_subwindow(point_up); dy = bmax(dy,point_up->get_h());
446 x1 += point_up->get_w() + xS(2)*margin;
447 title_x = new BC_Title(x1, y, _("X:"));
448 add_subwindow(title_x); dy = bmax(dy,title_x->get_h());
449 x1 += title_x->get_w() + margin;
450 point_x = new SketcherPointX(this, x1, y, !pt ? 0.f : pt->x);
451 point_x->create_objects(); dy = bmax(dy, point_x->get_h());
452 x2 = x1 + point_x->get_w() + xS(2)*margin + xS(10);
453 y1 = BC_Title::calculate_h(this, _("ID:"));
454 title_id = new BC_Title(x2+xS(16), y+dy-y1, _("ID:"));
455 add_subwindow(title_id); dy = bmax(dy, title_id->get_h());
456 y += dy + margin; dy = 0;
458 del_point = new SketcherDelPoint(this, plugin, x1=x, y);
459 add_subwindow(del_point); dy = bmax(dy,del_point->get_h());
460 x1 += del_point->get_w() + margin;
461 point_dn = new SketcherPointDn(this, x1, y);
462 add_subwindow(point_dn); dy = bmax(dy,point_dn->get_h());
463 x1 += point_dn->get_w() + xS(2)*margin;
464 title_y = new BC_Title(x1, y, _("Y:"));
465 add_subwindow(title_y); dy = bmax(dy,title_y->get_h());
466 x1 += title_y->get_w() + margin;
467 point_y = new SketcherPointY(this, x1, y, !pt ? 0.f : pt->y);
468 point_y->create_objects(); dy = bmax(dy, point_y->get_h());
469 point_id = new SketcherPointId(this, x2, y, !pt ? 0 : pt->id);
470 point_id->create_objects(); dy = bmax(dy, point_id->get_h());
471 y += dy + margin + yS(5); dy = 0;
472 point_list->update(pi);
474 add_subwindow(help = new SketcherHelp(this, plugin, x, y));
475 y += help->get_h() + yS(5);
477 bar = new BC_Bar(x, y, get_w()-xS(2)*x);
478 add_subwindow(bar); dy = bmax(dy,bar->get_h());
479 y += dy + yS(2)*margin;
481 add_subwindow(notes0 = new BC_Title(x, y,
487 "Ctrl+Shift="))); dy = bmax(dy, notes0->get_h());
488 add_subwindow(notes1 = new BC_Title(x+xS(100), y,
494 "new fill point"))); dy = bmax(dy, notes1->get_h());
495 add_subwindow(notes2 = new BC_Title(x+xS(220), y,
501 "new off point"))); dy = bmax(dy, notes2->get_h());
502 y += dy + margin + yS(10);
504 add_subwindow(notes3 = new BC_Title(x, y,
505 "Wheel: rotate, centered on cursor\n"
506 "Wheel: shift: scale, centered on cursor\n"
507 "Key DEL= delete point, +Shift= delete curve\n"));
509 resize_window(get_w(), help_y);
513 void SketcherWindow::done_event(int result)
515 curve_color->close_picker();
516 ungrab(plugin->server->mwindow->cwindow->gui);
519 void SketcherWindow::send_configure_change()
522 plugin->send_configure_change();
526 int SketcherWindow::grab_event(XEvent *event)
528 int ret = do_grab_event(event);
529 if( !grab_event_count() ) {
530 if( grab_cursor_motion() )
532 if( pending_config ) {
533 last_x = track_x; last_y = track_y;
534 send_configure_change();
540 int SketcherWindow::do_grab_event(XEvent *event)
542 switch( event->type ) {
543 case ButtonPress: break;
544 case ButtonRelease: break;
545 case MotionNotify: break;
547 if( keysym_lookup(event) > 0 ) {
548 switch( get_keysym() ) {
551 return (event->xkey.state & ShiftMask) ?
552 del_curve->handle_event() :
553 del_point->handle_event() ;
560 MWindow *mwindow = plugin->server->mwindow;
561 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
562 CWindowCanvas *canvas = cwindow_gui->canvas;
563 cwindow_gui->get_relative_cursor(cursor_x, cursor_y);
564 output_x = cursor_x - canvas->view_x;
565 output_y = cursor_y - canvas->view_y;
568 if( output_x < 0 || output_x >= canvas->view_w ||
569 output_y < 0 || output_y >= canvas->view_h )
574 switch( event->type ) {
576 if( dragging ) return 0;
583 if( dragging ) break;
584 default: // fall thru
588 canvas->canvas_to_output(mwindow->edl, 0, output_x, output_y);
589 plugin->output_to_track(output_x, output_y, track_x, track_y);
590 state = event->xmotion.state;
592 if( event->type == MotionNotify ) {
593 memcpy(&motion_event, event, sizeof(motion_event));
597 if( grab_cursor_motion() )
600 switch( event->type ) {
602 pending_config = grab_button_press(event);
612 int SketcherWindow::grab_button_press(XEvent *event)
614 SketcherConfig &config = plugin->config;
615 int ci = config.cv_selected;
616 if( ci < 0 || ci >= plugin->config.curves.size() )
618 MWindow *mwindow = plugin->server->mwindow;
619 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
620 CWindowCanvas *canvas = cwindow_gui->canvas;
622 SketcherCurves &curves = config.curves;
623 SketcherCurve *cv = curves[ci];
624 SketcherPoints &points = cv->points;
625 int pi = config.pt_selected;
627 float s = 1.001; // min scale
628 float th = 0.1 * M_PI/180.f; // min theta .1 deg per wheel_btn
629 int64_t ms = event->xbutton.time;
630 double dt = (ms - last_time) / 1000.; // seconds
632 double mx_accel = 100., r = mx_accel / exp(1.);
633 double mx_dt = 1./2., mn_dt = 1./15.; // mn..mx period in sec/xev
634 bclip(dt, mn_dt, mx_dt);
635 double accel = r * exp(-(dt-mn_dt)/(mx_dt-mn_dt));
636 int button_no = event->xbutton.button;
637 switch( button_no ) {
640 th = -th; // fall thru
641 case WHEEL_UP: { // shift_down scale, !shift_down rotate
644 float st = sin(th), ct = cos(th);
645 int sz = points.size();
646 int shift_down = (state & ShiftMask) ? 1 : 0;
647 for( int i=0; i<sz; ++i ) {
648 SketcherPoint *pt = points[i];
649 float px = pt->x - track_x, py = pt->y - track_y;
650 float nx = shift_down ? px*s : px*ct + py*st;
651 float ny = shift_down ? py*s : py*ct - px*st;
652 point_list->set_point(i, PT_X, pt->x = nx + track_x);
653 point_list->set_point(i, PT_Y, pt->y = ny + track_y);
655 point_list->update(-1);
660 if( (state & ShiftMask) ) { // create new point/string
662 pi = plugin->new_point(cv,
663 !(state & ControlMask) ? ARC_LINE : ARC_FILL,
664 track_x, track_y, pi+1);
665 point_list->update(pi);
668 SketcherPoint *pt = 0; // select point
669 double dist = cv->nearest_point(pi, track_x,track_y);
673 plugin->track_to_output(pt->x, pt->y, cx, cy);
674 canvas->output_to_canvas(mwindow->edl, 0, cx, cy);
675 cx += canvas->view_x; cy += canvas->view_y;
676 dist = DISTANCE(cx,cy, cursor_x,cursor_y);
677 if( (state & ControlMask) && dist >= HANDLE_W ) {
681 point_list->set_selected(pi);
684 if( (state & ShiftMask) ) { // create new curve point
686 pi = plugin->new_point(cv,
687 !(state & ControlMask) ? ARC_CURVE : ARC_OFF,
688 track_x, track_y, pi+1);
689 point_list->update(pi);
692 if( (state & ControlMask) && (state & AltMask) ) { // create new curve
693 ci = plugin->new_curve(cv->pen, cv->width, curve_color->color);
694 curve_list->update(ci);
695 point_list->update(-1);
698 SketcherPoint *pt = 0;
699 double dist = config.nearest_point(ci, pi, track_x,track_y);
701 pt = curves[ci]->points[pi];
703 plugin->track_to_output(pt->x, pt->y, cx, cy);
704 canvas->output_to_canvas(mwindow->edl, 0, cx, cy);
705 cx += canvas->view_x; cy += canvas->view_y;
706 dist = DISTANCE(cx,cy, cursor_x,cursor_y);
707 if( (state & ControlMask) && dist >= HANDLE_W ) {
708 ci = pi = -1; pt = 0;
712 curve_list->update(ci);
713 point_list->update(pi);
720 int SketcherWindow::grab_cursor_motion()
722 if( !pending_motion )
725 SketcherConfig &config = plugin->config;
726 int ci = config.cv_selected;
727 if( ci < 0 || ci >= plugin->config.curves.size() )
729 SketcherCurves &curves = config.curves;
730 SketcherCurve *cv = curves[ci];
731 SketcherPoints &points = cv->points;
732 int pi = config.pt_selected;
734 if( (state & ShiftMask) ) { // string of points
735 if( (state & (Button1Mask|Button3Mask)) ) {
736 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
738 float dist = DISTANCE(pt->x, pt->y, track_x, track_y);
739 if( dist < get_w()*0.1 ) return 0; // tolerance w/10
742 int arc = (state & Button1Mask) ? ARC_LINE : ARC_CURVE;
743 pi = plugin->new_point(cv, arc, track_x, track_y, pi+1);
744 point_list->update(pi);
748 if( (state & Button1Mask) ) {
749 if( (state & ControlMask) && !(state & AltMask) ) { // drag selected point
750 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
752 point_list->set_point(pi, PT_X, pt->x = track_x);
753 point_list->set_point(pi, PT_Y, pt->y = track_y);
754 point_list->update_list(pi);
755 point_x->update(pt->x);
756 point_y->update(pt->y);
760 if( (state & ControlMask) && (state & AltMask) ) { // drag all curves
761 int dx = round(track_x - last_x);
762 int dy = round(track_y - last_y);
763 for( int i=0; i<curves.size(); ++i ) {
764 SketcherCurve *crv = plugin->config.curves[i];
765 int pts = crv->points.size();
766 for( int k=0; k<pts; ++k ) {
767 SketcherPoint *pt = crv->points[k];
768 pt->x += dx; pt->y += dy;
771 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
773 point_x->update(pt ? pt->x : 0.f);
774 point_y->update(pt ? pt->y : 0.f);
775 point_id->update(pt ? pt->id : 0);
776 point_list->update(pi);
779 double dist = cv->nearest_point(pi, track_x,track_y);
781 point_list->set_selected(pi);
784 if( (state & Button3Mask) ) {
785 if( (state & (ControlMask | AltMask)) ) { // drag selected curve(s)
786 int dx = round(track_x - last_x);
787 int dy = round(track_y - last_y);
788 for( int i=0; i<points.size(); ++i ) {
789 SketcherPoint *pt = points[i];
790 pt->x += dx; pt->y += dy;
792 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
794 point_x->update(pt ? pt->x : 0.f);
795 point_y->update(pt ? pt->y : 0.f);
796 point_id->update(pt ? pt->id : 0);
797 point_list->update(pi);
800 double dist = config.nearest_point(ci, pi, track_x,track_y);
802 curve_list->update(ci);
803 point_list->update(pi);
810 int SketcherWindow::keypress_event()
812 int key = get_keypress();
814 case DELETE: return shift_down() ?
815 del_curve->handle_event() :
816 del_point->handle_event() ;
822 SketcherCurveList::SketcherCurveList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
823 : BC_ListBox(x, y, xS(360), yS(130), LISTBOX_TEXT)
826 this->plugin = plugin;
827 col_titles[CV_ID] = _("ID"); col_widths[CV_ID] = xS(64);
828 col_titles[CV_RAD] = _("width"); col_widths[CV_RAD] = xS(64);
829 col_titles[CV_PEN] = _("pen"); col_widths[CV_PEN] = xS(64);
830 col_titles[CV_CLR] = _("color"); col_widths[CV_CLR] = xS(80);
831 col_titles[CV_ALP] = _("alpha"); col_widths[CV_ALP] = xS(64);
833 SketcherCurveList::~SketcherCurveList()
837 void SketcherCurveList::clear()
839 for( int i=CV_SZ; --i>=0; )
840 cols[i].remove_all_objects();
843 int SketcherCurveList::column_resize_event()
845 for( int i=CV_SZ; --i>=0; )
846 col_widths[i] = get_column_width(i);
850 int SketcherCurveList::handle_event()
852 int ci = get_selection_number(0, 0);
854 gui->point_list->update(0);
855 gui->send_configure_change();
859 int SketcherCurveList::selection_changed()
861 gui->curve_color->close_picker();
866 void SketcherCurveList::set_selected(int k)
869 if( k >= 0 && k < plugin->config.curves.size() ) {
870 SketcherCurve *cv = plugin->config.curves[k];
871 gui->curve_width->update(cv->width);
872 gui->curve_pen->update(cv->pen);
873 gui->curve_color->update_gui(cv->color);
876 plugin->config.cv_selected = ci;
880 void SketcherCurveList::update_list(int k)
882 int xpos = get_xposition(), ypos = get_yposition();
883 if( k >= 0 ) update_selection(&cols[0], k);
884 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],CV_SZ, xpos,ypos,k);
888 void SketcherCurveList::update(int k)
891 SketcherCurves &curves = plugin->config.curves;
892 int sz = curves.size();
893 for( int i=0; i<sz; ++i ) {
894 SketcherCurve *cv = curves[i];
895 char itxt[BCSTRLEN]; sprintf(itxt,"%d", cv->id);
896 char ptxt[BCSTRLEN]; sprintf(ptxt,"%s", cv_pen[cv->pen]);
897 char rtxt[BCSTRLEN]; sprintf(rtxt,"%d", cv->width);
898 int color = cv->color;
899 int r = (color>>16)&0xff;
900 int g = (color>> 8)&0xff;
901 int b = (color>> 0)&0xff;
902 int a = (~color>>24)&0xff;
903 char ctxt[BCSTRLEN]; sprintf(ctxt,"#%02x%02x%02x", r, g, b);
904 char atxt[BCSTRLEN]; sprintf(atxt,"%5.3f", a/255.);
905 add_curve(itxt, ptxt, rtxt, ctxt, atxt);
910 void SketcherCurveList::add_curve(const char *id, const char *pen,
911 const char *width, const char *color, const char *alpha)
913 cols[CV_ID].append(new BC_ListBoxItem(id));
914 cols[CV_RAD].append(new BC_ListBoxItem(width));
915 cols[CV_PEN].append(new BC_ListBoxItem(pen));
916 cols[CV_CLR].append(new BC_ListBoxItem(color));
917 cols[CV_ALP].append(new BC_ListBoxItem(alpha));
920 SketcherNewCurve::SketcherNewCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
921 : BC_GenericButton(x, y, xS(96), _("New"))
924 this->plugin = plugin;
926 SketcherNewCurve::~SketcherNewCurve()
929 int SketcherNewCurve::handle_event()
931 int pen = gui->curve_pen->pen;
932 int color = gui->curve_color->color;
933 int width = gui->curve_width->width;
934 int ci = plugin->config.cv_selected;
935 if( ci >= 0 && ci < plugin->config.curves.size() ) {
936 SketcherCurve *cv = plugin->config.curves[ci];
937 pen = cv->pen; width = cv->width; color = cv->color;
939 ci = plugin->new_curve(pen, width, color);
940 gui->curve_list->update(ci);
941 gui->point_list->update(-1);
942 gui->send_configure_change();
946 SketcherDelCurve::SketcherDelCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
947 : BC_GenericButton(x, y, xS(96), C_("Del"))
950 this->plugin = plugin;
952 SketcherDelCurve::~SketcherDelCurve()
955 int SketcherDelCurve::handle_event()
957 SketcherConfig &config = plugin->config;
958 int ci = config.cv_selected;
959 SketcherCurves &curves = config.curves;
960 if( ci >= 0 && ci < curves.size() ) {
961 curves.remove_object_number(ci--);
964 ci = plugin->new_curve();
965 gui->curve_list->update(ci);
966 gui->point_list->update(-1);
967 gui->send_configure_change();
972 SketcherCurveUp::SketcherCurveUp(SketcherWindow *gui, int x, int y)
973 : BC_GenericButton(x, y, xS(96), _("Up"))
977 SketcherCurveUp::~SketcherCurveUp()
981 int SketcherCurveUp::handle_event()
983 SketcherConfig &config = gui->plugin->config;
984 int ci = config.cv_selected;
985 SketcherCurves &curves = config.curves;
986 if( ci > 0 && ci < curves.size() ) {
987 SketcherCurve *&cv0 = curves[ci];
988 SketcherCurve *&cv1 = curves[--ci];
989 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
990 gui->curve_list->update(ci);
992 gui->send_configure_change();
996 SketcherCurveDn::SketcherCurveDn(SketcherWindow *gui, int x, int y)
997 : BC_GenericButton(x, y, xS(96), _("Dn"))
1001 SketcherCurveDn::~SketcherCurveDn()
1005 int SketcherCurveDn::handle_event()
1007 SketcherConfig &config = gui->plugin->config;
1008 int ci = config.cv_selected;
1009 SketcherCurves &curves = config.curves;
1010 if( ci >= 0 && ci < curves.size()-1 ) {
1011 SketcherCurve *&cv0 = curves[ci];
1012 SketcherCurve *&cv1 = curves[++ci];
1013 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
1014 gui->curve_list->update(ci);
1016 gui->send_configure_change();
1021 SketcherPointTypeItem::SketcherPointTypeItem(int arc)
1022 : BC_MenuItem(_(pt_type[arc]))
1026 int SketcherPointTypeItem::handle_event()
1028 SketcherPointType *popup = (SketcherPointType*)get_popup_menu();
1030 SketcherWindow *gui = popup->gui;
1031 SketcherConfig &config = gui->plugin->config;
1032 SketcherCurves &curves = config.curves;
1033 int ci = config.cv_selected;
1034 if( ci < 0 || ci >= curves.size() )
1036 SketcherCurve *cv = curves[ci];
1037 SketcherPoints &points = cv->points;
1038 int pi = config.pt_selected;
1040 ArrayList<int> selected;
1041 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1044 for( int i=selected.size(); --i>=0; ) {
1045 int k = selected[i];
1046 if( k < 0 || k >= points.size() ) continue;
1047 SketcherPoint *pt = cv->points[k];
1049 gui->point_list->set_point(k, PT_TY, _(pt_type[arc]));
1052 gui->point_list->update_list(pi);
1053 gui->send_configure_change();
1057 SketcherPointType::SketcherPointType(SketcherWindow *gui, int x, int y, int arc)
1058 : BC_PopupMenu(x,y,xS(100),_(pt_type[arc]))
1063 void SketcherPointType::create_objects()
1065 for( int arc=0; arc<PT_SZ; ++arc )
1066 add_item(new SketcherPointTypeItem(arc));
1068 void SketcherPointType::update(int arc)
1070 set_text(_(pt_type[this->type=arc]));
1074 SketcherPointList::SketcherPointList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1075 : BC_ListBox(x, y, xS(360), yS(130), LISTBOX_TEXT)
1078 this->plugin = plugin;
1079 col_titles[PT_ID] = _("ID"); col_widths[PT_ID] = xS(50);
1080 col_titles[PT_TY] = _("Type"); col_widths[PT_TY] = xS(80);
1081 col_titles[PT_X] = _("X"); col_widths[PT_X] = xS(90);
1082 col_titles[PT_Y] = _("Y"); col_widths[PT_Y] = xS(90);
1083 set_selection_mode(LISTBOX_MULTIPLE);
1085 SketcherPointList::~SketcherPointList()
1089 void SketcherPointList::clear()
1091 for( int i=PT_SZ; --i>=0; )
1092 cols[i].remove_all_objects();
1095 int SketcherPointList::column_resize_event()
1097 for( int i=PT_SZ; --i>=0; )
1098 col_widths[i] = get_column_width(i);
1102 int SketcherPointList::handle_event()
1104 int pi = get_selection_number(0, 0);
1105 if( get_selection_number(0, 1) >= 0 ) pi = -1;
1107 gui->send_configure_change();
1111 int SketcherPointList::selection_changed()
1117 void SketcherPointList::add_point(const char *id, const char *ty, const char *xp, const char *yp)
1119 cols[PT_ID].append(new BC_ListBoxItem(id));
1120 cols[PT_TY].append(new BC_ListBoxItem(ty));
1121 cols[PT_X].append(new BC_ListBoxItem(xp));
1122 cols[PT_Y].append(new BC_ListBoxItem(yp));
1125 void SketcherPointList::set_point(int i, int c, int v)
1127 char stxt[BCSTRLEN];
1128 sprintf(stxt,"%d",v);
1129 set_point(i,c,stxt);
1131 void SketcherPointList::set_point(int i, int c, coord v)
1133 char stxt[BCSTRLEN];
1134 sprintf(stxt,"%0.1f",v);
1135 set_point(i,c,stxt);
1137 void SketcherPointList::set_point(int i, int c, const char *cp)
1139 cols[c].get(i)->set_text(cp);
1142 void SketcherPointList::set_selected(int k)
1144 SketcherPoint *pt = 0;
1145 int ci = plugin->config.cv_selected, pi = -1;
1146 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1147 SketcherCurve *cv = plugin->config.curves[ci];
1148 pt = k >= 0 && k < cv->points.size() ? cv->points[pi=k] : 0;
1150 gui->point_type->update(pt ? pt->arc : ARC_OFF);
1151 gui->point_x->update(pt ? pt->x : 0.f);
1152 gui->point_y->update(pt ? pt->y : 0.f);
1153 gui->point_id->update(pt ? pt->id : 0);
1154 plugin->config.pt_selected = pi;
1157 void SketcherPointList::update_list(int k)
1159 int xpos = get_xposition(), ypos = get_yposition();
1160 if( k >= 0 ) update_selection(&cols[0], k);
1161 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],PT_SZ, xpos,ypos,k);
1164 void SketcherPointList::update(int k)
1167 int ci = plugin->config.cv_selected, sz = 0;
1168 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1169 SketcherCurve *cv = plugin->config.curves[ci];
1170 SketcherPoints &points = cv->points;
1172 for( int i=0; i<sz; ++i ) {
1173 SketcherPoint *pt = points[i];
1174 char itxt[BCSTRLEN]; sprintf(itxt,"%d", pt->id);
1175 char ttxt[BCSTRLEN]; sprintf(ttxt,"%s", _(pt_type[pt->arc]));
1176 char xtxt[BCSTRLEN]; sprintf(xtxt,"%0.1f", pt->x);
1177 char ytxt[BCSTRLEN]; sprintf(ytxt,"%0.1f", pt->y);
1178 add_point(itxt, ttxt, xtxt, ytxt);
1184 void SketcherWindow::update_gui()
1186 SketcherConfig &config = plugin->config;
1187 int ci = config.cv_selected;
1188 int pi = config.pt_selected;
1189 curve_list->update(ci);
1190 point_list->update(pi);
1191 SketcherCurve *cv = ci >= 0 ? config.curves[ci] : 0;
1192 curve_width->update(cv ? cv->width : 1);
1193 curve_pen->update(cv ? cv->pen : PEN_SQUARE);
1194 curve_color->set_color(cv ? cv->color : CV_COLOR);
1195 SketcherPoint *pt = pi >= 0 ? cv->points[pi] : 0;
1196 point_x->update(pt ? pt->x : 0);
1197 point_y->update(pt ? pt->y : 0);
1198 point_id->update(pt ? pt->id : 0);
1199 drag->update(plugin->config.drag);
1203 SketcherPointUp::SketcherPointUp(SketcherWindow *gui, int x, int y)
1204 : BC_GenericButton(x, y, xS(96), _("Up"))
1208 SketcherPointUp::~SketcherPointUp()
1212 int SketcherPointUp::handle_event()
1214 SketcherConfig &config = gui->plugin->config;
1215 int ci = config.cv_selected;
1216 if( ci < 0 || ci >= config.curves.size() )
1218 SketcherCurve *cv = config.curves[ci];
1219 SketcherPoints &points = cv->points;
1220 if( points.size() < 2 )
1222 int pi = config.pt_selected;
1224 ArrayList<int> selected;
1225 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1228 for( int i=0; i<selected.size(); ++i ) {
1229 int k = selected[i];
1230 if( k <= 0 ) continue;
1232 SketcherPoint *&pt0 = points[k];
1233 SketcherPoint *&pt1 = points[--k];
1234 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1236 gui->point_list->update(pi);
1237 gui->send_configure_change();
1241 SketcherPointDn::SketcherPointDn(SketcherWindow *gui, int x, int y)
1242 : BC_GenericButton(x, y, xS(96), _("Dn"))
1246 SketcherPointDn::~SketcherPointDn()
1250 int SketcherPointDn::handle_event()
1252 SketcherConfig &config = gui->plugin->config;
1253 int ci = config.cv_selected;
1254 if( ci < 0 || ci >= config.curves.size() )
1256 SketcherCurve *cv = config.curves[ci];
1257 SketcherPoints &points = cv->points;
1258 int sz1 = points.size()-1;
1261 int pi = config.pt_selected;
1263 ArrayList<int> selected;
1264 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1267 for( int i=selected.size(); --i>=0; ) {
1268 int k = selected[i];
1269 if( k >= sz1 ) continue;
1271 SketcherPoint *&pt0 = points[k];
1272 SketcherPoint *&pt1 = points[++k];
1273 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1275 gui->point_list->update(pi);
1276 gui->send_configure_change();
1280 SketcherDrag::SketcherDrag(SketcherWindow *gui, int x, int y)
1281 : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
1285 int SketcherDrag::handle_event()
1287 CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
1288 int value = get_value();
1290 if( !gui->grab(cwindow_gui) ) {
1296 gui->ungrab(cwindow_gui);
1297 gui->plugin->config.drag = value;
1298 gui->send_configure_change();
1302 SketcherNewPoint::SketcherNewPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1303 : BC_GenericButton(x, y, xS(96), _("New"))
1306 this->plugin = plugin;
1308 SketcherNewPoint::~SketcherNewPoint()
1311 int SketcherNewPoint::handle_event()
1313 int pi = plugin->config.pt_selected;
1314 int arc = gui->point_type->type;
1315 int k = plugin->new_point(pi+1, arc);
1316 gui->point_list->update(k);
1317 gui->send_configure_change();
1321 SketcherDelPoint::SketcherDelPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1322 : BC_GenericButton(x, y, xS(96), C_("Del"))
1325 this->plugin = plugin;
1327 SketcherDelPoint::~SketcherDelPoint()
1330 int SketcherDelPoint::handle_event()
1332 SketcherConfig &config = gui->plugin->config;
1333 SketcherCurves &curves = config.curves;
1334 int ci = config.cv_selected;
1335 if( ci < 0 || ci >= curves.size() )
1337 SketcherCurve *cv = curves[ci];
1338 SketcherPoints &points = cv->points;
1339 int pi = config.pt_selected;
1341 ArrayList<int> selected;
1342 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1345 for( int i=selected.size(); --i>=0; ) {
1346 int k = selected[i];
1347 if( k < 0 || k >= points.size() ) continue;
1348 points.remove_object_number(k);
1349 if( k == pi && --pi < 0 && points.size() > 0 ) pi = 0;
1351 gui->point_list->update(pi);
1352 gui->send_configure_change();
1356 SketcherResetCurves::SketcherResetCurves(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1357 : BC_GenericButton(x, y, _("Reset"))
1360 this->plugin = plugin;
1362 SketcherResetCurves::~SketcherResetCurves()
1365 int SketcherResetCurves::handle_event()
1367 SketcherConfig &config = plugin->config;
1368 config.curves.remove_all_objects();
1369 int ci = plugin->new_curve();
1370 gui->curve_list->update(ci);
1371 gui->point_list->update(-1);
1372 gui->send_configure_change();
1376 SketcherResetPoints::SketcherResetPoints(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1377 : BC_GenericButton(x, y-yS(2), _("Reset"))
1380 this->plugin = plugin;
1382 SketcherResetPoints::~SketcherResetPoints()
1385 int SketcherResetPoints::handle_event()
1387 SketcherConfig &config = gui->plugin->config;
1388 int ci = config.cv_selected;
1389 if( ci >= 0 && ci < config.curves.size() ) {
1390 SketcherCurve *cv = config.curves[ci];
1391 cv->points.remove_all_objects();
1392 gui->point_list->update(-1);
1393 gui->send_configure_change();
1398 SketcherHelp::SketcherHelp(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1399 : BC_CheckBox(x, y, 0, _("Help"))
1402 this->plugin = plugin;
1403 set_tooltip(_("Show help text"));
1405 SketcherHelp::~SketcherHelp()
1409 int SketcherHelp::handle_event()
1411 gui->helped = get_value();
1412 gui->resize_window(gui->get_w(),
1413 gui->helped ? gui->help_h : gui->help_y);