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 "crikeywindow.h"
28 #include "cwindowgui.h"
30 #include "edlsession.h"
32 #include "mainerror.h"
35 #include "pluginserver.h"
42 CriKeyNum::CriKeyNum(CriKeyWindow *gui, int x, int y, float output)
43 : BC_TumbleTextBox(gui, output, -32767.0f, 32767.0f, x, y, 120)
50 CriKeyNum::~CriKeyNum()
54 int CriKeyPointX::handle_event()
56 if( !CriKeyNum::handle_event() ) return 0;
57 CriKeyPointList *point_list = gui->point_list;
58 int hot_point = point_list->get_selection_number(0, 0);
59 CriKeyPoints &points = gui->plugin->config.points;
60 int sz = points.size();
61 if( hot_point >= 0 && hot_point < sz ) {
62 float v = atof(get_text());
63 points[hot_point]->x = v;
64 point_list->set_point(hot_point, PT_X, v);
66 point_list->update_list(hot_point);
67 gui->send_configure_change();
70 int CriKeyPointY::handle_event()
72 if( !CriKeyNum::handle_event() ) return 0;
73 CriKeyPointList *point_list = gui->point_list;
74 int hot_point = point_list->get_selection_number(0, 0);
75 CriKeyPoints &points = gui->plugin->config.points;
76 int sz = points.size();
77 if( hot_point >= 0 && hot_point < sz ) {
78 float v = atof(get_text());
79 points[hot_point]->y = v;
80 point_list->set_point(hot_point, PT_Y, v);
82 point_list->update_list(hot_point);
83 gui->send_configure_change();
87 int CriKeyDrawModeItem::handle_event()
89 ((CriKeyDrawMode *)get_popup_menu())->update(id);
92 CriKeyDrawMode::CriKeyDrawMode(CriKeyWindow *gui, int x, int y)
93 : BC_PopupMenu(x, y, 100, "", 1)
96 draw_modes[DRAW_ALPHA] = _("Alpha");
97 draw_modes[DRAW_EDGE] = _("Edge");
98 draw_modes[DRAW_MASK] = _("Mask");
101 void CriKeyDrawMode::create_objects()
103 for( int i=0; i<DRAW_MODES; ++i )
104 add_item(new CriKeyDrawModeItem(draw_modes[i], i));
105 update(gui->plugin->config.draw_mode, 0);
107 void CriKeyDrawMode::update(int mode, int send)
109 if( this->mode == mode ) return;
111 set_text(draw_modes[mode]);
112 gui->plugin->config.draw_mode = mode;
113 if( send ) gui->send_configure_change();
117 CriKeyWindow::CriKeyWindow(CriKey *plugin)
118 : PluginClientWindow(plugin, 380, 400, 380, 400, 0)
120 this->plugin = plugin;
121 this->title_x = 0; this->point_x = 0;
122 this->title_y = 0; this->point_y = 0;
123 this->new_point = 0; this->del_point = 0;
124 this->point_up = 0; this->point_dn = 0;
125 this->drag = 0; this->dragging = 0;
126 this->last_x = 0; this->last_y = 0;
127 this->point_list = 0; this->pending_config = 0;
130 CriKeyWindow::~CriKeyWindow()
136 void CriKeyWindow::create_objects()
139 int margin = plugin->get_theme()->widget_border;
141 add_subwindow(title = new BC_Title(x, y+5, _("Draw mode:")));
142 int x1 = x + title->get_w() + 10 + margin;
143 add_subwindow(draw_mode = new CriKeyDrawMode(this, x1, y));
144 draw_mode->create_objects();
145 y += draw_mode->get_h() + 10 + margin;
147 CriKeyPoint *pt = plugin->config.points[plugin->config.selected];
148 add_subwindow(title_x = new BC_Title(x, y, _("X:")));
149 x1 = x + title_x->get_w() + margin;
150 point_x = new CriKeyPointX(this, x1, y, pt->x);
151 point_x->create_objects();
152 x1 += point_x->get_w() + margin;
153 add_subwindow(new_point = new CriKeyNewPoint(this, plugin, x1, y));
154 x1 += new_point->get_w() + margin;
155 add_subwindow(point_up = new CriKeyPointUp(this, x1, y));
156 y += point_x->get_h() + margin;
157 add_subwindow(title_y = new BC_Title(x, y, _("Y:")));
158 x1 = x + title_y->get_w() + margin;
159 point_y = new CriKeyPointY(this, x1, y, pt->y);
160 point_y->create_objects();
161 x1 += point_y->get_w() + margin;
162 add_subwindow(del_point = new CriKeyDelPoint(this, plugin, x1, y));
163 x1 += del_point->get_w() + margin;
164 add_subwindow(point_dn = new CriKeyPointDn(this, x1, y));
165 y += point_y->get_h() + margin + 10;
166 add_subwindow(title = new BC_Title(x, y, _("Threshold:")));
167 y += title->get_h() + margin;
168 add_subwindow(threshold = new CriKeyThreshold(this, x, y, get_w() - x * 2));
169 y += threshold->get_h() + margin;
171 add_subwindow(drag = new CriKeyDrag(this, x, y));
172 if( plugin->config.drag ) {
173 if( !grab(plugin->server->mwindow->cwindow->gui) )
174 eprintf("drag enabled, but compositor already grabbed\n");
176 x1 = x + drag->get_w() + margin + 32;
177 add_subwindow(reset = new CriKeyReset(this, plugin, x1, y+3));
178 y += drag->get_h() + margin;
180 add_subwindow(point_list = new CriKeyPointList(this, plugin, x, y));
181 point_list->update(plugin->config.selected);
183 y += point_list->get_h() + 10;
184 add_subwindow(notes = new BC_Title(x, y,
185 _("Right click in composer: create new point\n"
186 "Shift-left click in Enable field:\n"
187 " if any off, turns all on\n"
188 " if all on, turns rest off.")));
192 void CriKeyWindow::send_configure_change()
195 plugin->send_configure_change();
198 int CriKeyWindow::grab_event(XEvent *event)
200 int ret = do_grab_event(event);
201 if( pending_config && !grab_event_count() )
202 send_configure_change();
206 int CriKeyWindow::do_grab_event(XEvent *event)
208 switch( event->type ) {
209 case ButtonPress: break;
210 case ButtonRelease: break;
211 case MotionNotify: break;
216 MWindow *mwindow = plugin->server->mwindow;
217 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
218 CWindowCanvas *canvas = cwindow_gui->canvas;
219 int cursor_x, cursor_y;
220 cwindow_gui->get_relative_cursor(cursor_x, cursor_y);
221 float output_x = cursor_x - canvas->view_x;
222 float output_y = cursor_y - canvas->view_y;
225 if( output_x < 0 || output_x >= canvas->view_w ||
226 output_y < 0 || output_y >= canvas->view_h )
230 switch( event->type ) {
232 if( dragging ) return 0;
233 if( event->xbutton.button == WHEEL_UP ) return threshold->wheel_event(1);
234 if( event->xbutton.button == WHEEL_DOWN ) return threshold->wheel_event(-1);
235 dragging = event->xbutton.state & ShiftMask ? -1 : 1;
238 if( !dragging ) return 0;
242 if( !dragging ) return 0;
248 float track_x, track_y;
249 canvas->canvas_to_output(mwindow->edl, 0, output_x, output_y);
250 plugin->output_to_track(output_x, output_y, track_x, track_y);
251 point_x->update((int64_t)track_x);
252 point_y->update((int64_t)track_y);
253 CriKeyPoints &points = plugin->config.points;
256 switch( event->type ) {
258 int button_no = event->xbutton.button;
260 if( button_no == RIGHT_BUTTON ) {
261 hot_point = plugin->new_point();
262 CriKeyPoint *pt = points[hot_point];
263 pt->x = track_x; pt->y = track_y;
264 point_list->update(hot_point);
267 int sz = points.size();
268 if( hot_point < 0 && sz > 0 ) {
269 CriKeyPoint *pt = points[hot_point=0];
270 double dist = DISTANCE(track_x,track_y, pt->x,pt->y);
271 for( int i=1; i<sz; ++i ) {
273 double d = DISTANCE(track_x,track_y, pt->x,pt->y);
274 if( d >= dist ) continue;
275 dist = d; hot_point = i;
277 pt = points[hot_point];
279 plugin->track_to_output(pt->x, pt->y, cx, cy);
280 canvas->output_to_canvas(mwindow->edl, 0, cx, cy);
281 cx += canvas->view_x; cy += canvas->view_y;
282 dist = DISTANCE(cx,cy, cursor_x,cursor_y);
283 if( dist >= HANDLE_W )
286 if( hot_point >= 0 && sz > 0 ) {
287 CriKeyPoint *pt = points[hot_point];
288 point_list->set_point(hot_point, PT_X, pt->x = track_x);
289 point_list->set_point(hot_point, PT_Y, pt->y = track_y);
290 for( int i=0; i<sz; ++i ) {
292 pt->e = i==hot_point ? !pt->e : 0;
293 point_list->set_point(i, PT_E, pt->e ? "*" : "");
295 point_list->update_list(hot_point);
299 int hot_point = point_list->get_selection_number(0, 0);
300 if( hot_point >= 0 && hot_point < points.size() ) {
301 CriKeyPoint *pt = points[hot_point];
302 if( pt->x == track_x && pt->y == track_y ) break;
303 point_list->set_point(hot_point, PT_X, pt->x = track_x);
304 point_list->set_point(hot_point, PT_Y, pt->y = track_y);
305 point_x->update(pt->x);
306 point_y->update(pt->y);
307 point_list->update_list(hot_point);
313 switch( event->type ) {
315 float dx = track_x - last_x, dy = track_y - last_y;
316 int sz = points.size();
317 for( int i=0; i<sz; ++i ) {
318 CriKeyPoint *pt = points[i];
319 point_list->set_point(i, PT_X, pt->x += dx);
320 point_list->set_point(i, PT_Y, pt->y += dy);
322 int hot_point = point_list->get_selection_number(0, 0);
323 if( hot_point >= 0 && hot_point < sz ) {
324 CriKeyPoint *pt = points[hot_point];
325 point_x->update(pt->x);
326 point_y->update(pt->y);
327 point_list->update_list(hot_point);
333 last_x = track_x; last_y = track_y;
338 void CriKeyWindow::done_event(int result)
340 ungrab(client->server->mwindow->cwindow->gui);
343 CriKeyPointList::CriKeyPointList(CriKeyWindow *gui, CriKey *plugin, int x, int y)
344 : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
347 this->plugin = plugin;
348 titles[PT_E] = _("E"); widths[PT_E] = 50;
349 titles[PT_X] = _("X"); widths[PT_X] = 90;
350 titles[PT_Y] = _("Y"); widths[PT_Y] = 90;
351 titles[PT_T] = _("T"); widths[PT_T] = 70;
352 titles[PT_TAG] = _("Tag"); widths[PT_TAG] = 50;
354 CriKeyPointList::~CriKeyPointList()
358 void CriKeyPointList::clear()
360 for( int i=PT_SZ; --i>=0; )
361 cols[i].remove_all_objects();
364 int CriKeyPointList::column_resize_event()
366 for( int i=PT_SZ; --i>=0; )
367 widths[i] = get_column_width(i);
371 int CriKeyPointList::handle_event()
373 int hot_point = get_selection_number(0, 0);
374 const char *x_text = "", *y_text = "";
375 float t = plugin->config.threshold;
376 CriKeyPoints &points = plugin->config.points;
378 int sz = points.size();
379 if( hot_point >= 0 && sz > 0 ) {
380 if( get_cursor_x() < widths[0] ) {
382 int all_on = points[0]->e;
383 for( int i=1; i<sz && all_on; ++i ) all_on = points[i]->e;
384 int e = !all_on ? 1 : 0;
385 for( int i=0; i<sz; ++i ) points[i]->e = e;
386 points[hot_point]->e = 1;
389 points[hot_point]->e = !points[hot_point]->e;
391 x_text = gui->point_list->cols[PT_X].get(hot_point)->get_text();
392 y_text = gui->point_list->cols[PT_Y].get(hot_point)->get_text();
393 t = points[hot_point]->t;
395 gui->point_x->update(x_text);
396 gui->point_y->update(y_text);
397 gui->threshold->update(t);
399 gui->send_configure_change();
403 int CriKeyPointList::selection_changed()
409 void CriKeyPointList::new_point(const char *ep, const char *xp, const char *yp,
410 const char *tp, const char *tag)
412 cols[PT_E].append(new BC_ListBoxItem(ep));
413 cols[PT_X].append(new BC_ListBoxItem(xp));
414 cols[PT_Y].append(new BC_ListBoxItem(yp));
415 cols[PT_T].append(new BC_ListBoxItem(tp));
416 cols[PT_TAG].append(new BC_ListBoxItem(tag));
419 void CriKeyPointList::del_point(int i)
421 for( int sz1=cols[0].size()-1, c=PT_SZ; --c>=0; )
422 cols[c].remove_object_number(sz1-i);
425 void CriKeyPointList::set_point(int i, int c, float v)
427 char s[BCSTRLEN]; sprintf(s,"%0.4f",v);
430 void CriKeyPointList::set_point(int i, int c, const char *cp)
432 cols[c].get(i)->set_text(cp);
435 int CriKeyPointList::set_selected(int k)
437 CriKeyPoints &points = plugin->config.points;
438 int sz = points.size();
441 for( int i=0; i<sz; ++i ) points[i]->e = 0;
443 update_selection(&cols[0], k);
446 void CriKeyPointList::update_list(int k)
448 int xpos = get_xposition(), ypos = get_yposition();
449 if( k < 0 ) k = get_selection_number(0, 0);
450 update_selection(&cols[0], k);
451 BC_ListBox::update(&cols[0], &titles[0],&widths[0],PT_SZ, xpos,ypos,k);
454 void CriKeyPointList::update(int k)
457 CriKeyPoints &points = plugin->config.points;
458 int sz = points.size();
459 for( int i=0; i<sz; ++i ) {
460 CriKeyPoint *pt = points[i];
461 char etxt[BCSTRLEN]; sprintf(etxt,"%s", pt->e ? "*" : "");
462 char xtxt[BCSTRLEN]; sprintf(xtxt,"%0.4f", pt->x);
463 char ytxt[BCSTRLEN]; sprintf(ytxt,"%0.4f", pt->y);
464 char ttxt[BCSTRLEN]; sprintf(ttxt,"%0.4f", pt->t);
465 char ttag[BCSTRLEN]; sprintf(ttag,"%d", pt->tag);
466 new_point(etxt, xtxt, ytxt, ttxt, ttag);
468 if( k >= 0 && k < sz ) {
469 gui->point_x->update(gui->point_list->cols[PT_X].get(k)->get_text());
470 gui->point_y->update(gui->point_list->cols[PT_Y].get(k)->get_text());
471 plugin->config.selected = k;
477 void CriKeyWindow::update_gui()
479 draw_mode->update(plugin->config.draw_mode);
480 threshold->update(plugin->config.threshold);
481 drag->update(plugin->config.drag);
482 point_list->update(-1);
486 CriKeyThreshold::CriKeyThreshold(CriKeyWindow *gui, int x, int y, int w)
487 : BC_FSlider(x, y, 0, w, w, 0, 1, gui->plugin->config.threshold, 0)
490 set_precision(0.005);
491 set_pagination(0.01, 0.1);
494 int CriKeyThreshold::wheel_event(int v)
496 if( v > 0 ) increase_value();
497 else if( v < 0 ) decrease_value();
503 int CriKeyThreshold::handle_event()
505 float v = get_value();
506 gui->plugin->config.threshold = v;
507 int hot_point = gui->point_list->get_selection_number(0, 0);
508 if( hot_point >= 0 ) {
509 CriKeyPoints &points = gui->plugin->config.points;
510 CriKeyPoint *pt = points[hot_point];
511 pt->t = v; pt->e = 1;
512 gui->point_list->update(hot_point);
514 gui->send_configure_change();
519 CriKeyPointUp::CriKeyPointUp(CriKeyWindow *gui, int x, int y)
520 : BC_GenericButton(x, y, _("Up"))
524 CriKeyPointUp::~CriKeyPointUp()
528 int CriKeyPointUp::handle_event()
530 CriKeyPoints &points = gui->plugin->config.points;
531 int sz = points.size();
532 int hot_point = gui->point_list->get_selection_number(0, 0);
534 if( sz > 1 && hot_point > 0 ) {
535 CriKeyPoint *&pt0 = points[hot_point];
536 CriKeyPoint *&pt1 = points[--hot_point];
537 CriKeyPoint *t = pt0; pt0 = pt1; pt1 = t;
538 gui->point_list->update(hot_point);
540 gui->send_configure_change();
544 CriKeyPointDn::CriKeyPointDn(CriKeyWindow *gui, int x, int y)
545 : BC_GenericButton(x, y, _("Dn"))
549 CriKeyPointDn::~CriKeyPointDn()
553 int CriKeyPointDn::handle_event()
555 CriKeyPoints &points = gui->plugin->config.points;
556 int sz = points.size();
557 int hot_point = gui->point_list->get_selection_number(0, 0);
558 if( sz > 1 && hot_point < sz-1 ) {
559 CriKeyPoint *&pt0 = points[hot_point];
560 CriKeyPoint *&pt1 = points[++hot_point];
561 CriKeyPoint *t = pt0; pt0 = pt1; pt1 = t;
562 gui->point_list->update(hot_point);
564 gui->send_configure_change();
568 CriKeyDrag::CriKeyDrag(CriKeyWindow *gui, int x, int y)
569 : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
573 int CriKeyDrag::handle_event()
575 CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
576 int value = get_value();
578 if( !gui->grab(cwindow_gui) ) {
584 gui->ungrab(cwindow_gui);
585 gui->plugin->config.drag = value;
586 gui->send_configure_change();
590 CriKeyNewPoint::CriKeyNewPoint(CriKeyWindow *gui, CriKey *plugin, int x, int y)
591 : BC_GenericButton(x, y, 80, _("New"))
594 this->plugin = plugin;
596 CriKeyNewPoint::~CriKeyNewPoint()
599 int CriKeyNewPoint::handle_event()
601 int k = plugin->new_point();
602 gui->point_list->update(k);
603 gui->send_configure_change();
607 CriKeyDelPoint::CriKeyDelPoint(CriKeyWindow *gui, CriKey *plugin, int x, int y)
608 : BC_GenericButton(x, y, 80, C_("Del"))
611 this->plugin = plugin;
613 CriKeyDelPoint::~CriKeyDelPoint()
616 int CriKeyDelPoint::handle_event()
618 int hot_point = gui->point_list->get_selection_number(0, 0);
619 CriKeyPoints &points = plugin->config.points;
620 if( hot_point >= 0 && hot_point < points.size() ) {
621 plugin->config.del_point(hot_point);
622 if( !points.size() ) plugin->new_point();
623 int sz = points.size();
624 if( hot_point >= sz && hot_point > 0 ) --hot_point;
625 gui->point_list->update(hot_point);
626 gui->send_configure_change();
631 CriKeyReset::CriKeyReset(CriKeyWindow *gui, CriKey *plugin, int x, int y)
632 : BC_GenericButton(x, y, _("Reset"))
635 this->plugin = plugin;
637 CriKeyReset::~CriKeyReset()
640 int CriKeyReset::handle_event()
642 CriKeyPoints &points = plugin->config.points;
643 points.remove_all_objects();
645 gui->point_list->update(0);
646 gui->send_configure_change();