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;
281 set_tooltip(_("Anti-Aliasing"));
283 SketcherAliasing::~SketcherAliasing()
287 void SketcherAliasing::create_objects()
289 add_item(new SketcherAliasItem(this, -1));
290 add_item(new SketcherAliasItem(this, 0));
291 add_item(new SketcherAliasItem(this, 1));
294 const char *SketcherAliasing::alias_to_text(int alias)
296 if( alias < 0 ) return _("Off");
297 if( alias > 0 ) return _("Double");
303 SketcherWindow::SketcherWindow(Sketcher *plugin)
304 : PluginClientWindow(plugin, xS(460), yS(680), xS(460), yS(680), 0)
306 this->plugin = plugin;
307 this->title_pen = 0; this->curve_pen = 0;
308 this->title_color = 0; this->curve_color = 0;
310 this->new_curve = 0; this->del_curve = 0;
311 this->curve_up = 0; this->curve_dn = 0;
312 this->title_x = 0; this->point_x = 0;
313 this->title_y = 0; this->point_y = 0;
314 this->title_id = 0; this->point_id = 0;
315 this->new_point = 0; this->del_point = 0;
316 this->point_up = 0; this->point_dn = 0;
317 this->point_list = 0; this->notes0 = 0;
318 this->notes1 = 0; this->notes2 = 0;
321 track_w = track_h -1;
322 output_x = output_y = -1;
323 track_x = track_y = -1;
324 last_x = last_y = -1;
325 projector_x = projector_y = projector_z = -1;
326 state = 0; dragging = 0;
335 SketcherWindow::~SketcherWindow()
342 void SketcherWindow::create_objects()
344 int x = xS(10), y = yS(10), dy = 0, x1, y1;
345 int margin = plugin->get_theme()->widget_border;
347 int ci = plugin->config.cv_selected;
348 if( ci < 0 || ci >= plugin->config.curves.size() )
349 ci = plugin->new_curve();
350 SketcherCurve *cv = plugin->config.curves[ci];
352 reset_curves = new SketcherResetCurves(this, plugin, x1=x, y);
353 add_subwindow(reset_curves); dy = bmax(dy,reset_curves->get_h());
354 x1 += reset_curves->get_w() + 2*margin;
355 const char *curve_text = _("Curve");
356 title_width = new BC_Title(x1, y, _("Width:"));
357 add_subwindow(title_width); dy = bmax(dy,title_width->get_h());
358 x1 += title_width->get_w() + margin;
359 curve_width = new SketcherCurveWidth(this, x1, y, cv->width);
360 curve_width->create_objects();
361 x1 += curve_width->get_w() + margin;
362 aliasing = new SketcherAliasing(this, plugin, x1, y);
363 add_subwindow(aliasing); dy = bmax(dy,aliasing->get_h());
364 aliasing->create_objects();
365 y += dy + 2*margin; dy = 0;
367 x1 = get_w()-x - BC_Title::calculate_w(this, curve_text, LARGEFONT);
368 y1 = y-margin - BC_Title::calculate_h(this, curve_text, LARGEFONT);
369 title = new BC_Title(x1, y1, curve_text, LARGEFONT,
370 get_resources()->menu_highlighted_fontcolor);
371 add_subwindow(title); dy = bmax(dy,title->get_h());
372 curve_list = new SketcherCurveList(this, plugin, x, y);
373 add_subwindow(curve_list); dy = bmax(dy,curve_list->get_h());
374 y += dy + margin; dy = 0;
376 new_curve = new SketcherNewCurve(this, plugin, x1=x, y);
377 add_subwindow(new_curve); dy = bmax(dy,new_curve->get_h());
378 x1 += new_curve->get_w() + margin;
379 curve_up = new SketcherCurveUp(this, x1, y);
380 add_subwindow(curve_up); dy = bmax(dy,curve_up->get_h());
381 x1 += curve_up->get_w() + 4*margin;
382 y1 = BC_Title::calculate_h(this, _("Pen:"));
383 title_pen = new BC_Title(x1+xS(30), y+dy-y1, _("Pen:"));
384 add_subwindow(title_pen); dy = bmax(dy,title_pen->get_h());
385 int x2 = (get_w()+x1)/2 + xS(20);
386 y1 = BC_Title::calculate_h(this, _("Color:"));
387 title_color = new BC_Title(x2, y+dy-y1, _("Color:"));
388 add_subwindow(title_color); dy = bmax(dy,title_color->get_h());
389 y += dy + margin; dy = 0;
391 del_curve = new SketcherDelCurve(this, plugin, x1=x, y);
392 add_subwindow(del_curve); dy = bmax(dy,del_curve->get_h());
393 x1 += del_curve->get_w() + margin;
394 curve_dn = new SketcherCurveDn(this, x1, y);
395 add_subwindow(curve_dn); dy = bmax(dy,curve_dn->get_h());
396 x1 += curve_dn->get_w() + 4*margin;
397 curve_pen = new SketcherCurvePen(this, x1, y, cv->pen);
398 add_subwindow(curve_pen); dy = bmax(dy,curve_pen->get_h());
399 curve_pen->create_objects();
400 curve_color = new SketcherCurveColor(this, x2, y, COLOR_W, COLOR_H,
401 cv->color&0xffffff, (~cv->color>>24)&0xff);
402 add_subwindow(curve_color); dy = bmax(dy,curve_color->get_h());
403 y += dy + margin; dy = 0;
404 curve_list->update(ci);
407 bar = new BC_Bar(x, y, get_w()-xS(2)*x);
408 add_subwindow(bar); dy = bmax(dy,bar->get_h());
409 bar = new BC_Bar(x, y+=dy, get_w()-xS(2)*x);
410 add_subwindow(bar); dy = bmax(dy,bar->get_h());
411 y += dy + yS(2)*margin;
413 int pi = plugin->config.pt_selected;
414 SketcherPoint *pt = pi >= 0 && pi < cv->points.size() ? cv->points[pi] : 0;
415 reset_points = new SketcherResetPoints(this, plugin, x1=x, y+yS(3));
416 add_subwindow(reset_points); dy = bmax(dy,reset_points->get_h());
417 x1 += reset_points->get_w() + xS(2)*margin;
418 if( plugin->config.drag ) {
419 if( !grab(plugin->server->mwindow->cwindow->gui) ) {
420 eprintf("drag enabled, but compositor already grabbed\n");
421 plugin->config.drag = 0;
424 drag = new SketcherDrag(this, x1, y);
425 add_subwindow(drag); dy = bmax(dy,drag->get_h());
426 x1 += drag->get_w() + xS(2)*margin;
427 int arc = pt ? pt->arc : ARC_LINE;
428 point_type = new SketcherPointType(this, x1, y, arc);
429 add_subwindow(point_type); dy = bmax(dy,point_type->get_h());
430 point_type->create_objects();
431 y += dy + margin; dy = 0;
433 const char *point_text = _("Point");
434 x1 = get_w()-x - BC_Title::calculate_w(this, point_text, LARGEFONT);
435 y1 = y-margin - BC_Title::calculate_h(this, point_text, LARGEFONT);
436 add_subwindow(title = new BC_Title(x1, y1, point_text, LARGEFONT,
437 get_resources()->menu_highlighted_fontcolor));
438 point_list = new SketcherPointList(this, plugin, x, y);
439 add_subwindow(point_list); dy = bmax(dy,point_list->get_h());
440 y += dy + margin; dy = 0;
442 new_point = new SketcherNewPoint(this, plugin, x1=x, y);
443 add_subwindow(new_point); dy = bmax(dy,new_point->get_h());
444 x1 += new_point->get_w() + margin;
445 point_up = new SketcherPointUp(this, x1, y);
446 add_subwindow(point_up); dy = bmax(dy,point_up->get_h());
447 x1 += point_up->get_w() + xS(2)*margin;
448 title_x = new BC_Title(x1, y, _("X:"));
449 add_subwindow(title_x); dy = bmax(dy,title_x->get_h());
450 x1 += title_x->get_w() + margin;
451 point_x = new SketcherPointX(this, x1, y, !pt ? 0.f : pt->x);
452 point_x->create_objects(); dy = bmax(dy, point_x->get_h());
453 x2 = x1 + point_x->get_w() + xS(2)*margin + xS(10);
454 y1 = BC_Title::calculate_h(this, _("ID:"));
455 title_id = new BC_Title(x2+xS(16), y+dy-y1, _("ID:"));
456 add_subwindow(title_id); dy = bmax(dy, title_id->get_h());
457 y += dy + margin; dy = 0;
459 del_point = new SketcherDelPoint(this, plugin, x1=x, y);
460 add_subwindow(del_point); dy = bmax(dy,del_point->get_h());
461 x1 += del_point->get_w() + margin;
462 point_dn = new SketcherPointDn(this, x1, y);
463 add_subwindow(point_dn); dy = bmax(dy,point_dn->get_h());
464 x1 += point_dn->get_w() + xS(2)*margin;
465 title_y = new BC_Title(x1, y, _("Y:"));
466 add_subwindow(title_y); dy = bmax(dy,title_y->get_h());
467 x1 += title_y->get_w() + margin;
468 point_y = new SketcherPointY(this, x1, y, !pt ? 0.f : pt->y);
469 point_y->create_objects(); dy = bmax(dy, point_y->get_h());
470 point_id = new SketcherPointId(this, x2, y, !pt ? 0 : pt->id);
471 point_id->create_objects(); dy = bmax(dy, point_id->get_h());
472 y += dy + margin + yS(5); dy = 0;
473 point_list->update(pi);
475 add_subwindow(help = new SketcherHelp(this, plugin, x, y));
476 y += help->get_h() + yS(5);
478 bar = new BC_Bar(x, y, get_w()-xS(2)*x);
479 add_subwindow(bar); dy = bmax(dy,bar->get_h());
480 y += dy + yS(2)*margin;
482 add_subwindow(notes0 = new BC_Title(x, y,
488 "Ctrl+Shift="))); dy = bmax(dy, notes0->get_h());
489 add_subwindow(notes1 = new BC_Title(x+xS(100), y,
495 "new fill point"))); dy = bmax(dy, notes1->get_h());
496 add_subwindow(notes2 = new BC_Title(x+xS(280), y,
502 "new off point"))); dy = bmax(dy, notes2->get_h());
503 y += dy + margin + yS(10);
505 add_subwindow(notes3 = new BC_Title(x, y,
506 _("Wheel: rotate, centered on cursor\n"
507 "Wheel: shift: scale, centered on cursor\n"
508 "Key DEL= delete point, +Shift= delete curve\n")));
510 resize_window(get_w(), help_y);
514 void SketcherWindow::done_event(int result)
516 curve_color->close_picker();
517 ungrab(plugin->server->mwindow->cwindow->gui);
520 void SketcherWindow::send_configure_change()
523 plugin->send_configure_change();
527 int SketcherWindow::grab_event(XEvent *event)
529 int ret = do_grab_event(event);
530 if( !grab_event_count() ) {
531 if( grab_cursor_motion() )
533 if( pending_config ) {
534 last_x = track_x; last_y = track_y;
535 send_configure_change();
541 int SketcherWindow::do_grab_event(XEvent *event)
543 switch( event->type ) {
544 case ButtonPress: break;
545 case ButtonRelease: break;
546 case MotionNotify: break;
548 if( keysym_lookup(event) > 0 ) {
549 switch( get_keysym() ) {
552 return (event->xkey.state & ShiftMask) ?
553 del_curve->handle_event() :
554 del_point->handle_event() ;
561 MWindow *mwindow = plugin->server->mwindow;
562 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
563 CWindowCanvas *canvas = cwindow_gui->canvas;
564 cwindow_gui->get_relative_cursor(cursor_x, cursor_y);
565 output_x = cursor_x - canvas->view_x;
566 output_y = cursor_y - canvas->view_y;
569 if( output_x < 0 || output_x >= canvas->view_w ||
570 output_y < 0 || output_y >= canvas->view_h )
575 switch( event->type ) {
577 if( dragging ) return 0;
584 if( dragging ) break;
585 default: // fall thru
589 canvas->canvas_to_output(mwindow->edl, 0, output_x, output_y);
590 plugin->output_to_track(output_x, output_y, track_x, track_y);
591 state = event->xmotion.state;
593 if( event->type == MotionNotify ) {
594 memcpy(&motion_event, event, sizeof(motion_event));
598 if( grab_cursor_motion() )
601 switch( event->type ) {
603 pending_config = grab_button_press(event);
613 int SketcherWindow::grab_button_press(XEvent *event)
615 SketcherConfig &config = plugin->config;
616 int ci = config.cv_selected;
617 if( ci < 0 || ci >= plugin->config.curves.size() )
619 MWindow *mwindow = plugin->server->mwindow;
620 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
621 CWindowCanvas *canvas = cwindow_gui->canvas;
623 SketcherCurves &curves = config.curves;
624 SketcherCurve *cv = curves[ci];
625 SketcherPoints &points = cv->points;
626 int pi = config.pt_selected;
628 float s = 1.001; // min scale
629 float th = 0.1 * M_PI/180.f; // min theta .1 deg per wheel_btn
630 int64_t ms = event->xbutton.time;
631 double dt = (ms - last_time) / 1000.; // seconds
633 double mx_accel = 100., r = mx_accel / exp(1.);
634 double mx_dt = 1./2., mn_dt = 1./15.; // mn..mx period in sec/xev
635 bclip(dt, mn_dt, mx_dt);
636 double accel = r * exp(-(dt-mn_dt)/(mx_dt-mn_dt));
637 int button_no = event->xbutton.button;
638 switch( button_no ) {
641 th = -th; // fall thru
642 case WHEEL_UP: { // shift_down scale, !shift_down rotate
645 float st = sin(th), ct = cos(th);
646 int sz = points.size();
647 int shift_down = (state & ShiftMask) ? 1 : 0;
648 for( int i=0; i<sz; ++i ) {
649 SketcherPoint *pt = points[i];
650 float px = pt->x - track_x, py = pt->y - track_y;
651 float nx = shift_down ? px*s : px*ct + py*st;
652 float ny = shift_down ? py*s : py*ct - px*st;
653 point_list->set_point(i, PT_X, pt->x = nx + track_x);
654 point_list->set_point(i, PT_Y, pt->y = ny + track_y);
656 point_list->update(-1);
661 if( (state & ShiftMask) ) { // create new point/string
663 pi = plugin->new_point(cv,
664 !(state & ControlMask) ? ARC_LINE : ARC_FILL,
665 track_x, track_y, pi+1);
666 point_list->update(pi);
669 SketcherPoint *pt = 0; // select point
670 double dist = cv->nearest_point(pi, track_x,track_y);
674 plugin->track_to_output(pt->x, pt->y, cx, cy);
675 canvas->output_to_canvas(mwindow->edl, 0, cx, cy);
676 cx += canvas->view_x; cy += canvas->view_y;
677 dist = DISTANCE(cx,cy, cursor_x,cursor_y);
678 if( (state & ControlMask) && dist >= HANDLE_W ) {
682 point_list->set_selected(pi);
685 if( (state & ShiftMask) ) { // create new curve point
687 pi = plugin->new_point(cv,
688 !(state & ControlMask) ? ARC_CURVE : ARC_OFF,
689 track_x, track_y, pi+1);
690 point_list->update(pi);
693 if( (state & ControlMask) && (state & AltMask) ) { // create new curve
694 ci = plugin->new_curve(cv->pen, cv->width, curve_color->color);
695 curve_list->update(ci);
696 point_list->update(-1);
699 SketcherPoint *pt = 0;
700 double dist = config.nearest_point(ci, pi, track_x,track_y);
702 pt = curves[ci]->points[pi];
704 plugin->track_to_output(pt->x, pt->y, cx, cy);
705 canvas->output_to_canvas(mwindow->edl, 0, cx, cy);
706 cx += canvas->view_x; cy += canvas->view_y;
707 dist = DISTANCE(cx,cy, cursor_x,cursor_y);
708 if( (state & ControlMask) && dist >= HANDLE_W ) {
709 ci = pi = -1; pt = 0;
713 curve_list->update(ci);
714 point_list->update(pi);
721 int SketcherWindow::grab_cursor_motion()
723 if( !pending_motion )
726 SketcherConfig &config = plugin->config;
727 int ci = config.cv_selected;
728 if( ci < 0 || ci >= plugin->config.curves.size() )
730 SketcherCurves &curves = config.curves;
731 SketcherCurve *cv = curves[ci];
732 SketcherPoints &points = cv->points;
733 int pi = config.pt_selected;
735 if( (state & ShiftMask) ) { // string of points
736 if( (state & (Button1Mask|Button3Mask)) ) {
737 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
739 float dist = DISTANCE(pt->x, pt->y, track_x, track_y);
740 if( dist < get_w()*0.1 ) return 0; // tolerance w/10
743 int arc = (state & Button1Mask) ? ARC_LINE : ARC_CURVE;
744 pi = plugin->new_point(cv, arc, track_x, track_y, pi+1);
745 point_list->update(pi);
749 if( (state & Button1Mask) ) {
750 if( (state & ControlMask) && !(state & AltMask) ) { // drag selected point
751 SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
753 point_list->set_point(pi, PT_X, pt->x = track_x);
754 point_list->set_point(pi, PT_Y, pt->y = track_y);
755 point_list->update_list(pi);
756 point_x->update(pt->x);
757 point_y->update(pt->y);
761 if( (state & ControlMask) && (state & AltMask) ) { // drag all curves
762 int dx = round(track_x - last_x);
763 int dy = round(track_y - last_y);
764 for( int i=0; i<curves.size(); ++i ) {
765 SketcherCurve *crv = plugin->config.curves[i];
766 int pts = crv->points.size();
767 for( int k=0; k<pts; ++k ) {
768 SketcherPoint *pt = crv->points[k];
769 pt->x += dx; pt->y += dy;
772 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
774 point_x->update(pt ? pt->x : 0.f);
775 point_y->update(pt ? pt->y : 0.f);
776 point_id->update(pt ? pt->id : 0);
777 point_list->update(pi);
780 double dist = cv->nearest_point(pi, track_x,track_y);
782 point_list->set_selected(pi);
785 if( (state & Button3Mask) ) {
786 if( (state & (ControlMask | AltMask)) ) { // drag selected curve(s)
787 int dx = round(track_x - last_x);
788 int dy = round(track_y - last_y);
789 for( int i=0; i<points.size(); ++i ) {
790 SketcherPoint *pt = points[i];
791 pt->x += dx; pt->y += dy;
793 SketcherPoint *pt = pi >= 0 && pi < points.size() ?
795 point_x->update(pt ? pt->x : 0.f);
796 point_y->update(pt ? pt->y : 0.f);
797 point_id->update(pt ? pt->id : 0);
798 point_list->update(pi);
801 double dist = config.nearest_point(ci, pi, track_x,track_y);
803 curve_list->update(ci);
804 point_list->update(pi);
811 int SketcherWindow::keypress_event()
813 int key = get_keypress();
815 case DELETE: return shift_down() ?
816 del_curve->handle_event() :
817 del_point->handle_event() ;
823 SketcherCurveList::SketcherCurveList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
824 : BC_ListBox(x, y, xS(360), yS(130), LISTBOX_TEXT)
827 this->plugin = plugin;
828 col_titles[CV_ID] = _("ID"); col_widths[CV_ID] = xS(64);
829 col_titles[CV_RAD] = _("width"); col_widths[CV_RAD] = xS(64);
830 col_titles[CV_PEN] = _("pen"); col_widths[CV_PEN] = xS(64);
831 col_titles[CV_CLR] = _("color"); col_widths[CV_CLR] = xS(80);
832 col_titles[CV_ALP] = _("alpha"); col_widths[CV_ALP] = xS(64);
834 SketcherCurveList::~SketcherCurveList()
838 void SketcherCurveList::clear()
840 for( int i=CV_SZ; --i>=0; )
841 cols[i].remove_all_objects();
844 int SketcherCurveList::column_resize_event()
846 for( int i=CV_SZ; --i>=0; )
847 col_widths[i] = get_column_width(i);
851 int SketcherCurveList::handle_event()
853 int ci = get_selection_number(0, 0);
855 gui->point_list->update(0);
856 gui->send_configure_change();
860 int SketcherCurveList::selection_changed()
862 gui->curve_color->close_picker();
867 void SketcherCurveList::set_selected(int k)
870 if( k >= 0 && k < plugin->config.curves.size() ) {
871 SketcherCurve *cv = plugin->config.curves[k];
872 gui->curve_width->update(cv->width);
873 gui->curve_pen->update(cv->pen);
874 gui->curve_color->update_gui(cv->color);
877 plugin->config.cv_selected = ci;
881 void SketcherCurveList::update_list(int k)
883 int xpos = get_xposition(), ypos = get_yposition();
884 if( k >= 0 ) update_selection(&cols[0], k);
885 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],CV_SZ, xpos,ypos,k);
889 void SketcherCurveList::update(int k)
892 SketcherCurves &curves = plugin->config.curves;
893 int sz = curves.size();
894 for( int i=0; i<sz; ++i ) {
895 SketcherCurve *cv = curves[i];
896 char itxt[BCSTRLEN]; sprintf(itxt,"%d", cv->id);
897 char ptxt[BCSTRLEN]; sprintf(ptxt,"%s", cv_pen[cv->pen]);
898 char rtxt[BCSTRLEN]; sprintf(rtxt,"%d", cv->width);
899 int color = cv->color;
900 int r = (color>>16)&0xff;
901 int g = (color>> 8)&0xff;
902 int b = (color>> 0)&0xff;
903 int a = (~color>>24)&0xff;
904 char ctxt[BCSTRLEN]; sprintf(ctxt,"#%02x%02x%02x", r, g, b);
905 char atxt[BCSTRLEN]; sprintf(atxt,"%5.3f", a/255.);
906 add_curve(itxt, ptxt, rtxt, ctxt, atxt);
911 void SketcherCurveList::add_curve(const char *id, const char *pen,
912 const char *width, const char *color, const char *alpha)
914 cols[CV_ID].append(new BC_ListBoxItem(id));
915 cols[CV_RAD].append(new BC_ListBoxItem(width));
916 cols[CV_PEN].append(new BC_ListBoxItem(pen));
917 cols[CV_CLR].append(new BC_ListBoxItem(color));
918 cols[CV_ALP].append(new BC_ListBoxItem(alpha));
921 SketcherNewCurve::SketcherNewCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
922 : BC_GenericButton(x, y, xS(96), _("New"))
925 this->plugin = plugin;
927 SketcherNewCurve::~SketcherNewCurve()
930 int SketcherNewCurve::handle_event()
932 int pen = gui->curve_pen->pen;
933 int color = gui->curve_color->color;
934 int width = gui->curve_width->width;
935 int ci = plugin->config.cv_selected;
936 if( ci >= 0 && ci < plugin->config.curves.size() ) {
937 SketcherCurve *cv = plugin->config.curves[ci];
938 pen = cv->pen; width = cv->width; color = cv->color;
940 ci = plugin->new_curve(pen, width, color);
941 gui->curve_list->update(ci);
942 gui->point_list->update(-1);
943 gui->send_configure_change();
947 SketcherDelCurve::SketcherDelCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
948 : BC_GenericButton(x, y, xS(96), C_("Del"))
951 this->plugin = plugin;
953 SketcherDelCurve::~SketcherDelCurve()
956 int SketcherDelCurve::handle_event()
958 SketcherConfig &config = plugin->config;
959 int ci = config.cv_selected;
960 SketcherCurves &curves = config.curves;
961 if( ci >= 0 && ci < curves.size() ) {
962 curves.remove_object_number(ci--);
965 ci = plugin->new_curve();
966 gui->curve_list->update(ci);
967 gui->point_list->update(-1);
968 gui->send_configure_change();
973 SketcherCurveUp::SketcherCurveUp(SketcherWindow *gui, int x, int y)
974 : BC_GenericButton(x, y, xS(96), _("Up"))
978 SketcherCurveUp::~SketcherCurveUp()
982 int SketcherCurveUp::handle_event()
984 SketcherConfig &config = gui->plugin->config;
985 int ci = config.cv_selected;
986 SketcherCurves &curves = config.curves;
987 if( ci > 0 && ci < curves.size() ) {
988 SketcherCurve *&cv0 = curves[ci];
989 SketcherCurve *&cv1 = curves[--ci];
990 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
991 gui->curve_list->update(ci);
993 gui->send_configure_change();
997 SketcherCurveDn::SketcherCurveDn(SketcherWindow *gui, int x, int y)
998 : BC_GenericButton(x, y, xS(96), _("Dn"))
1002 SketcherCurveDn::~SketcherCurveDn()
1006 int SketcherCurveDn::handle_event()
1008 SketcherConfig &config = gui->plugin->config;
1009 int ci = config.cv_selected;
1010 SketcherCurves &curves = config.curves;
1011 if( ci >= 0 && ci < curves.size()-1 ) {
1012 SketcherCurve *&cv0 = curves[ci];
1013 SketcherCurve *&cv1 = curves[++ci];
1014 SketcherCurve *t = cv0; cv0 = cv1; cv1 = t;
1015 gui->curve_list->update(ci);
1017 gui->send_configure_change();
1022 SketcherPointTypeItem::SketcherPointTypeItem(int arc)
1023 : BC_MenuItem(_(pt_type[arc]))
1027 int SketcherPointTypeItem::handle_event()
1029 SketcherPointType *popup = (SketcherPointType*)get_popup_menu();
1031 SketcherWindow *gui = popup->gui;
1032 SketcherConfig &config = gui->plugin->config;
1033 SketcherCurves &curves = config.curves;
1034 int ci = config.cv_selected;
1035 if( ci < 0 || ci >= curves.size() )
1037 SketcherCurve *cv = curves[ci];
1038 SketcherPoints &points = cv->points;
1039 int pi = config.pt_selected;
1041 ArrayList<int> selected;
1042 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1045 for( int i=selected.size(); --i>=0; ) {
1046 int k = selected[i];
1047 if( k < 0 || k >= points.size() ) continue;
1048 SketcherPoint *pt = cv->points[k];
1050 gui->point_list->set_point(k, PT_TY, _(pt_type[arc]));
1053 gui->point_list->update_list(pi);
1054 gui->send_configure_change();
1058 SketcherPointType::SketcherPointType(SketcherWindow *gui, int x, int y, int arc)
1059 : BC_PopupMenu(x,y,xS(100),_(pt_type[arc]))
1064 void SketcherPointType::create_objects()
1066 for( int arc=0; arc<PT_SZ; ++arc )
1067 add_item(new SketcherPointTypeItem(arc));
1069 void SketcherPointType::update(int arc)
1071 set_text(_(pt_type[this->type=arc]));
1075 SketcherPointList::SketcherPointList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1076 : BC_ListBox(x, y, xS(360), yS(130), LISTBOX_TEXT)
1079 this->plugin = plugin;
1080 col_titles[PT_ID] = _("ID"); col_widths[PT_ID] = xS(50);
1081 col_titles[PT_TY] = _("Type"); col_widths[PT_TY] = xS(80);
1082 col_titles[PT_X] = _("X"); col_widths[PT_X] = xS(90);
1083 col_titles[PT_Y] = _("Y"); col_widths[PT_Y] = xS(90);
1084 set_selection_mode(LISTBOX_MULTIPLE);
1086 SketcherPointList::~SketcherPointList()
1090 void SketcherPointList::clear()
1092 for( int i=PT_SZ; --i>=0; )
1093 cols[i].remove_all_objects();
1096 int SketcherPointList::column_resize_event()
1098 for( int i=PT_SZ; --i>=0; )
1099 col_widths[i] = get_column_width(i);
1103 int SketcherPointList::handle_event()
1105 int pi = get_selection_number(0, 0);
1106 if( get_selection_number(0, 1) >= 0 ) pi = -1;
1108 gui->send_configure_change();
1112 int SketcherPointList::selection_changed()
1118 void SketcherPointList::add_point(const char *id, const char *ty, const char *xp, const char *yp)
1120 cols[PT_ID].append(new BC_ListBoxItem(id));
1121 cols[PT_TY].append(new BC_ListBoxItem(ty));
1122 cols[PT_X].append(new BC_ListBoxItem(xp));
1123 cols[PT_Y].append(new BC_ListBoxItem(yp));
1126 void SketcherPointList::set_point(int i, int c, int v)
1128 char stxt[BCSTRLEN];
1129 sprintf(stxt,"%d",v);
1130 set_point(i,c,stxt);
1132 void SketcherPointList::set_point(int i, int c, coord v)
1134 char stxt[BCSTRLEN];
1135 sprintf(stxt,"%0.1f",v);
1136 set_point(i,c,stxt);
1138 void SketcherPointList::set_point(int i, int c, const char *cp)
1140 cols[c].get(i)->set_text(cp);
1143 void SketcherPointList::set_selected(int k)
1145 SketcherPoint *pt = 0;
1146 int ci = plugin->config.cv_selected, pi = -1;
1147 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1148 SketcherCurve *cv = plugin->config.curves[ci];
1149 pt = k >= 0 && k < cv->points.size() ? cv->points[pi=k] : 0;
1151 gui->point_type->update(pt ? pt->arc : ARC_OFF);
1152 gui->point_x->update(pt ? pt->x : 0.f);
1153 gui->point_y->update(pt ? pt->y : 0.f);
1154 gui->point_id->update(pt ? pt->id : 0);
1155 plugin->config.pt_selected = pi;
1158 void SketcherPointList::update_list(int k)
1160 int xpos = get_xposition(), ypos = get_yposition();
1161 if( k >= 0 ) update_selection(&cols[0], k);
1162 BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],PT_SZ, xpos,ypos,k);
1165 void SketcherPointList::update(int k)
1168 int ci = plugin->config.cv_selected, sz = 0;
1169 if( ci >= 0 && ci < plugin->config.curves.size() ) {
1170 SketcherCurve *cv = plugin->config.curves[ci];
1171 SketcherPoints &points = cv->points;
1173 for( int i=0; i<sz; ++i ) {
1174 SketcherPoint *pt = points[i];
1175 char itxt[BCSTRLEN]; sprintf(itxt,"%d", pt->id);
1176 char ttxt[BCSTRLEN]; sprintf(ttxt,"%s", _(pt_type[pt->arc]));
1177 char xtxt[BCSTRLEN]; sprintf(xtxt,"%0.1f", pt->x);
1178 char ytxt[BCSTRLEN]; sprintf(ytxt,"%0.1f", pt->y);
1179 add_point(itxt, ttxt, xtxt, ytxt);
1185 void SketcherWindow::update_gui()
1187 SketcherConfig &config = plugin->config;
1188 int ci = config.cv_selected;
1189 int pi = config.pt_selected;
1190 curve_list->update(ci);
1191 point_list->update(pi);
1192 SketcherCurve *cv = ci >= 0 ? config.curves[ci] : 0;
1193 curve_width->update(cv ? cv->width : 1);
1194 curve_pen->update(cv ? cv->pen : PEN_SQUARE);
1195 curve_color->set_color(cv ? cv->color : CV_COLOR);
1196 SketcherPoint *pt = pi >= 0 ? cv->points[pi] : 0;
1197 point_x->update(pt ? pt->x : 0);
1198 point_y->update(pt ? pt->y : 0);
1199 point_id->update(pt ? pt->id : 0);
1200 drag->update(plugin->config.drag);
1204 SketcherPointUp::SketcherPointUp(SketcherWindow *gui, int x, int y)
1205 : BC_GenericButton(x, y, xS(96), _("Up"))
1209 SketcherPointUp::~SketcherPointUp()
1213 int SketcherPointUp::handle_event()
1215 SketcherConfig &config = gui->plugin->config;
1216 int ci = config.cv_selected;
1217 if( ci < 0 || ci >= config.curves.size() )
1219 SketcherCurve *cv = config.curves[ci];
1220 SketcherPoints &points = cv->points;
1221 if( points.size() < 2 )
1223 int pi = config.pt_selected;
1225 ArrayList<int> selected;
1226 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1229 for( int i=0; i<selected.size(); ++i ) {
1230 int k = selected[i];
1231 if( k <= 0 ) continue;
1233 SketcherPoint *&pt0 = points[k];
1234 SketcherPoint *&pt1 = points[--k];
1235 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1237 gui->point_list->update(pi);
1238 gui->send_configure_change();
1242 SketcherPointDn::SketcherPointDn(SketcherWindow *gui, int x, int y)
1243 : BC_GenericButton(x, y, xS(96), _("Dn"))
1247 SketcherPointDn::~SketcherPointDn()
1251 int SketcherPointDn::handle_event()
1253 SketcherConfig &config = gui->plugin->config;
1254 int ci = config.cv_selected;
1255 if( ci < 0 || ci >= config.curves.size() )
1257 SketcherCurve *cv = config.curves[ci];
1258 SketcherPoints &points = cv->points;
1259 int sz1 = points.size()-1;
1262 int pi = config.pt_selected;
1264 ArrayList<int> selected;
1265 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1268 for( int i=selected.size(); --i>=0; ) {
1269 int k = selected[i];
1270 if( k >= sz1 ) continue;
1272 SketcherPoint *&pt0 = points[k];
1273 SketcherPoint *&pt1 = points[++k];
1274 SketcherPoint *t = pt0; pt0 = pt1; pt1 = t;
1276 gui->point_list->update(pi);
1277 gui->send_configure_change();
1281 SketcherDrag::SketcherDrag(SketcherWindow *gui, int x, int y)
1282 : BC_CheckBox(x, y, gui->plugin->config.drag, (C_("Drag")))
1286 int SketcherDrag::handle_event()
1288 CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
1289 int value = get_value();
1291 if( !gui->grab(cwindow_gui) ) {
1297 gui->ungrab(cwindow_gui);
1298 gui->plugin->config.drag = value;
1299 gui->send_configure_change();
1303 SketcherNewPoint::SketcherNewPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1304 : BC_GenericButton(x, y, xS(96), _("New"))
1307 this->plugin = plugin;
1309 SketcherNewPoint::~SketcherNewPoint()
1312 int SketcherNewPoint::handle_event()
1314 int pi = plugin->config.pt_selected;
1315 int arc = gui->point_type->type;
1316 int k = plugin->new_point(pi+1, arc);
1317 gui->point_list->update(k);
1318 gui->send_configure_change();
1322 SketcherDelPoint::SketcherDelPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1323 : BC_GenericButton(x, y, xS(96), C_("Del"))
1326 this->plugin = plugin;
1328 SketcherDelPoint::~SketcherDelPoint()
1331 int SketcherDelPoint::handle_event()
1333 SketcherConfig &config = gui->plugin->config;
1334 SketcherCurves &curves = config.curves;
1335 int ci = config.cv_selected;
1336 if( ci < 0 || ci >= curves.size() )
1338 SketcherCurve *cv = curves[ci];
1339 SketcherPoints &points = cv->points;
1340 int pi = config.pt_selected;
1342 ArrayList<int> selected;
1343 for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1346 for( int i=selected.size(); --i>=0; ) {
1347 int k = selected[i];
1348 if( k < 0 || k >= points.size() ) continue;
1349 points.remove_object_number(k);
1350 if( k == pi && --pi < 0 && points.size() > 0 ) pi = 0;
1352 gui->point_list->update(pi);
1353 gui->send_configure_change();
1357 SketcherResetCurves::SketcherResetCurves(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1358 : BC_GenericButton(x, y, _("Reset"))
1361 this->plugin = plugin;
1363 SketcherResetCurves::~SketcherResetCurves()
1366 int SketcherResetCurves::handle_event()
1368 SketcherConfig &config = plugin->config;
1369 config.curves.remove_all_objects();
1370 int ci = plugin->new_curve();
1371 gui->curve_list->update(ci);
1372 gui->point_list->update(-1);
1373 gui->send_configure_change();
1377 SketcherResetPoints::SketcherResetPoints(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1378 : BC_GenericButton(x, y-yS(2), _("Reset"))
1381 this->plugin = plugin;
1383 SketcherResetPoints::~SketcherResetPoints()
1386 int SketcherResetPoints::handle_event()
1388 SketcherConfig &config = gui->plugin->config;
1389 int ci = config.cv_selected;
1390 if( ci >= 0 && ci < config.curves.size() ) {
1391 SketcherCurve *cv = config.curves[ci];
1392 cv->points.remove_all_objects();
1393 gui->point_list->update(-1);
1394 gui->send_configure_change();
1399 SketcherHelp::SketcherHelp(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1400 : BC_CheckBox(x, y, 0, _("Help"))
1403 this->plugin = plugin;
1404 set_tooltip(_("Show help text"));
1406 SketcherHelp::~SketcherHelp()
1410 int SketcherHelp::handle_event()
1412 gui->helped = get_value();
1413 gui->resize_window(gui->get_w(),
1414 gui->helped ? gui->help_h : gui->help_y);