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()
134 void CriKeyWindow::create_objects()
137 int margin = plugin->get_theme()->widget_border;
139 add_subwindow(title = new BC_Title(x, y+5, _("Draw mode:")));
140 int x1 = x + title->get_w() + 10 + margin;
141 add_subwindow(draw_mode = new CriKeyDrawMode(this, x1, y));
142 draw_mode->create_objects();
143 y += draw_mode->get_h() + 10 + margin;
145 CriKeyPoint *pt = plugin->config.points[plugin->config.selected];
146 add_subwindow(title_x = new BC_Title(x, y, _("X:")));
147 x1 = x + title_x->get_w() + margin;
148 point_x = new CriKeyPointX(this, x1, y, pt->x);
149 point_x->create_objects();
150 x1 += point_x->get_w() + margin;
151 add_subwindow(new_point = new CriKeyNewPoint(this, plugin, x1, y));
152 x1 += new_point->get_w() + margin;
153 add_subwindow(point_up = new CriKeyPointUp(this, x1, y));
154 y += point_x->get_h() + margin;
155 add_subwindow(title_y = new BC_Title(x, y, _("Y:")));
156 x1 = x + title_y->get_w() + margin;
157 point_y = new CriKeyPointY(this, x1, y, pt->y);
158 point_y->create_objects();
159 x1 += point_y->get_w() + margin;
160 add_subwindow(del_point = new CriKeyDelPoint(this, plugin, x1, y));
161 x1 += del_point->get_w() + margin;
162 add_subwindow(point_dn = new CriKeyPointDn(this, x1, y));
163 y += point_y->get_h() + margin + 10;
164 add_subwindow(title = new BC_Title(x, y, _("Threshold:")));
165 y += title->get_h() + margin;
166 add_subwindow(threshold = new CriKeyThreshold(this, x, y, get_w() - x * 2));
167 y += threshold->get_h() + margin;
169 add_subwindow(drag = new CriKeyDrag(this, x, y));
170 if( plugin->config.drag ) {
171 if( !grab(plugin->server->mwindow->cwindow->gui) )
172 eprintf("drag enabled, but compositor already grabbed\n");
174 x1 = x + drag->get_w() + margin + 32;
175 add_subwindow(reset = new CriKeyReset(this, plugin, x1, y+3));
176 y += drag->get_h() + margin;
178 add_subwindow(point_list = new CriKeyPointList(this, plugin, x, y));
179 point_list->update(plugin->config.selected);
181 y += point_list->get_h() + 10;
182 add_subwindow(notes = new BC_Title(x, y,
183 _("Right click in composer: create new point\n"
184 "Shift-left click in Enable field:\n"
185 " if any off, turns all on\n"
186 " if all on, turns rest off.")));
190 void CriKeyWindow::send_configure_change()
193 plugin->send_configure_change();
195 int CriKeyWindow::check_configure_change(int ret)
197 if( pending_config && !grab_event_count() )
198 send_configure_change();
202 int CriKeyWindow::grab_event(XEvent *event)
204 switch( event->type ) {
205 case ButtonPress: break;
206 case ButtonRelease: break;
207 case MotionNotify: break;
209 return check_configure_change(0);
212 MWindow *mwindow = plugin->server->mwindow;
213 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
214 CWindowCanvas *canvas = cwindow_gui->canvas;
215 int cx, cy; cwindow_gui->get_relative_cursor(cx, cy);
216 cx -= mwindow->theme->ccanvas_x;
217 cy -= mwindow->theme->ccanvas_y;
220 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ||
221 cy < 0 || cy >= mwindow->theme->ccanvas_h )
222 return check_configure_change(0);
225 switch( event->type ) {
227 if( dragging ) return check_configure_change(0);
228 dragging = event->xbutton.state & ShiftMask ? -1 : 1;
231 if( !dragging ) return check_configure_change(0);
235 if( !dragging ) return check_configure_change(0);
238 return check_configure_change(0);
241 float cursor_x = cx, cursor_y = cy;
242 canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
243 int64_t position = plugin->get_source_position();
244 float projector_x, projector_y, projector_z;
245 Track *track = plugin->server->plugin->track;
246 int track_w = track->track_w, track_h = track->track_h;
247 track->automation->get_projector(
248 &projector_x, &projector_y, &projector_z,
249 position, PLAY_FORWARD);
250 projector_x += mwindow->edl->session->output_w / 2;
251 projector_y += mwindow->edl->session->output_h / 2;
252 float output_x = (cursor_x - projector_x) / projector_z + track_w / 2;
253 float output_y = (cursor_y - projector_y) / projector_z + track_h / 2;
254 point_x->update((int64_t)(output_x));
255 point_y->update((int64_t)(output_y));
256 CriKeyPoints &points = plugin->config.points;
259 switch( event->type ) {
261 int button_no = event->xbutton.button;
263 if( button_no == RIGHT_BUTTON ) {
264 hot_point = plugin->new_point();
265 CriKeyPoint *pt = points[hot_point];
266 pt->x = output_x; pt->y = output_y;
267 point_list->update(hot_point);
270 int sz = points.size();
271 if( hot_point < 0 && sz > 0 ) {
272 CriKeyPoint *pt = points[hot_point=0];
273 double dist = DISTANCE(output_x,output_y, pt->x,pt->y);
274 for( int i=1; i<sz; ++i ) {
276 double d = DISTANCE(output_x,output_y, pt->x,pt->y);
277 if( d >= dist ) continue;
278 dist = d; hot_point = i;
280 pt = points[hot_point];
281 float px = (pt->x - track_w / 2) * projector_z + projector_x;
282 float py = (pt->y - track_h / 2) * projector_z + projector_y;
283 dist = DISTANCE(px, py, cursor_x,cursor_y);
284 if( dist >= HANDLE_W ) hot_point = -1;
286 if( hot_point >= 0 && sz > 0 ) {
287 CriKeyPoint *pt = points[hot_point];
288 point_list->set_point(hot_point, PT_X, pt->x = output_x);
289 for( int i=0; i<sz; ++i ) {
291 pt->e = i==hot_point ? !pt->e : 0;
292 point_list->set_point(i, PT_E, pt->e ? "*" : "");
294 point_list->update_list(hot_point);
298 int hot_point = point_list->get_selection_number(0, 0);
299 if( hot_point >= 0 && hot_point < points.size() ) {
300 CriKeyPoint *pt = points[hot_point];
301 if( pt->x == output_x && pt->y == output_y ) break;
302 point_list->set_point(hot_point, PT_X, pt->x = output_x);
303 point_list->set_point(hot_point, PT_Y, pt->y = output_y);
304 point_x->update(pt->x);
305 point_y->update(pt->y);
306 point_list->update_list(hot_point);
312 switch( event->type ) {
314 float dx = output_x - last_x, dy = output_y - last_y;
315 int sz = points.size();
316 for( int i=0; i<sz; ++i ) {
317 CriKeyPoint *pt = points[i];
318 point_list->set_point(i, PT_X, pt->x += dx);
319 point_list->set_point(i, PT_Y, pt->y += dy);
321 int hot_point = point_list->get_selection_number(0, 0);
322 if( hot_point >= 0 && hot_point < sz ) {
323 CriKeyPoint *pt = points[hot_point];
324 point_x->update(pt->x);
325 point_y->update(pt->y);
326 point_list->update_list(hot_point);
332 last_x = output_x; last_y = output_y;
333 if( !grab_event_count() )
334 send_configure_change();
340 void CriKeyWindow::done_event(int result)
342 ungrab(client->server->mwindow->cwindow->gui);
345 CriKeyPointList::CriKeyPointList(CriKeyWindow *gui, CriKey *plugin, int x, int y)
346 : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
349 this->plugin = plugin;
350 titles[PT_E] = _("E"); widths[PT_E] = 50;
351 titles[PT_X] = _("X"); widths[PT_X] = 90;
352 titles[PT_Y] = _("Y"); widths[PT_Y] = 90;
353 titles[PT_T] = _("T"); widths[PT_T] = 70;
354 titles[PT_TAG] = _("Tag"); widths[PT_TAG] = 50;
356 CriKeyPointList::~CriKeyPointList()
360 void CriKeyPointList::clear()
362 for( int i=PT_SZ; --i>=0; )
363 cols[i].remove_all_objects();
366 int CriKeyPointList::column_resize_event()
368 for( int i=PT_SZ; --i>=0; )
369 widths[i] = get_column_width(i);
373 int CriKeyPointList::handle_event()
375 int hot_point = get_selection_number(0, 0);
376 const char *x_text = "", *y_text = "";
377 float t = plugin->config.threshold;
378 CriKeyPoints &points = plugin->config.points;
380 int sz = points.size();
381 if( hot_point >= 0 && sz > 0 ) {
382 if( get_cursor_x() < widths[0] ) {
384 int all_on = points[0]->e;
385 for( int i=1; i<sz && all_on; ++i ) all_on = points[i]->e;
386 int e = !all_on ? 1 : 0;
387 for( int i=0; i<sz; ++i ) points[i]->e = e;
388 points[hot_point]->e = 1;
391 points[hot_point]->e = !points[hot_point]->e;
393 x_text = gui->point_list->cols[PT_X].get(hot_point)->get_text();
394 y_text = gui->point_list->cols[PT_Y].get(hot_point)->get_text();
395 t = points[hot_point]->t;
397 gui->point_x->update(x_text);
398 gui->point_y->update(y_text);
399 gui->threshold->update(t);
401 gui->send_configure_change();
405 int CriKeyPointList::selection_changed()
411 void CriKeyPointList::new_point(const char *ep, const char *xp, const char *yp,
412 const char *tp, const char *tag)
414 cols[PT_E].append(new BC_ListBoxItem(ep));
415 cols[PT_X].append(new BC_ListBoxItem(xp));
416 cols[PT_Y].append(new BC_ListBoxItem(yp));
417 cols[PT_T].append(new BC_ListBoxItem(tp));
418 cols[PT_TAG].append(new BC_ListBoxItem(tag));
421 void CriKeyPointList::del_point(int i)
423 for( int sz1=cols[0].size()-1, c=PT_SZ; --c>=0; )
424 cols[c].remove_object_number(sz1-i);
427 void CriKeyPointList::set_point(int i, int c, float v)
429 char s[BCSTRLEN]; sprintf(s,"%0.4f",v);
432 void CriKeyPointList::set_point(int i, int c, const char *cp)
434 cols[c].get(i)->set_text(cp);
437 int CriKeyPointList::set_selected(int k)
439 CriKeyPoints &points = plugin->config.points;
440 int sz = points.size();
443 for( int i=0; i<sz; ++i ) points[i]->e = 0;
445 update_selection(&cols[0], k);
448 void CriKeyPointList::update_list(int k)
450 int xpos = get_xposition(), ypos = get_yposition();
451 if( k < 0 ) k = get_selection_number(0, 0);
452 update_selection(&cols[0], k);
453 BC_ListBox::update(&cols[0], &titles[0],&widths[0],PT_SZ, xpos,ypos,k);
456 void CriKeyPointList::update(int k)
459 CriKeyPoints &points = plugin->config.points;
460 int sz = points.size();
461 for( int i=0; i<sz; ++i ) {
462 CriKeyPoint *pt = points[i];
463 char etxt[BCSTRLEN]; sprintf(etxt,"%s", pt->e ? "*" : "");
464 char xtxt[BCSTRLEN]; sprintf(xtxt,"%0.4f", pt->x);
465 char ytxt[BCSTRLEN]; sprintf(ytxt,"%0.4f", pt->y);
466 char ttxt[BCSTRLEN]; sprintf(ttxt,"%0.4f", pt->t);
467 char ttag[BCSTRLEN]; sprintf(ttag,"%d", pt->tag);
468 new_point(etxt, xtxt, ytxt, ttxt, ttag);
470 if( k >= 0 && k < sz ) {
471 gui->point_x->update(gui->point_list->cols[PT_X].get(k)->get_text());
472 gui->point_y->update(gui->point_list->cols[PT_Y].get(k)->get_text());
473 plugin->config.selected = k;
479 void CriKeyWindow::update_gui()
481 draw_mode->update(plugin->config.draw_mode);
482 threshold->update(plugin->config.threshold);
483 drag->update(plugin->config.drag);
484 point_list->update(-1);
488 CriKeyThreshold::CriKeyThreshold(CriKeyWindow *gui, int x, int y, int w)
489 : BC_FSlider(x, y, 0, w, w, 0, 1, gui->plugin->config.threshold, 0)
492 set_precision(0.005);
495 int CriKeyThreshold::handle_event()
497 float v = get_value();
498 gui->plugin->config.threshold = v;
499 int hot_point = gui->point_list->get_selection_number(0, 0);
500 if( hot_point >= 0 ) {
501 CriKeyPoints &points = gui->plugin->config.points;
502 CriKeyPoint *pt = points[hot_point];
503 pt->t = v; pt->e = 1;
504 gui->point_list->update(hot_point);
506 gui->send_configure_change();
511 CriKeyPointUp::CriKeyPointUp(CriKeyWindow *gui, int x, int y)
512 : BC_GenericButton(x, y, _("Up"))
516 CriKeyPointUp::~CriKeyPointUp()
520 int CriKeyPointUp::handle_event()
522 CriKeyPoints &points = gui->plugin->config.points;
523 int sz = points.size();
524 int hot_point = gui->point_list->get_selection_number(0, 0);
526 if( sz > 1 && hot_point > 0 ) {
527 CriKeyPoint *&pt0 = points[hot_point];
528 CriKeyPoint *&pt1 = points[--hot_point];
529 CriKeyPoint *t = pt0; pt0 = pt1; pt1 = t;
530 gui->point_list->update(hot_point);
532 gui->send_configure_change();
536 CriKeyPointDn::CriKeyPointDn(CriKeyWindow *gui, int x, int y)
537 : BC_GenericButton(x, y, _("Dn"))
541 CriKeyPointDn::~CriKeyPointDn()
545 int CriKeyPointDn::handle_event()
547 CriKeyPoints &points = gui->plugin->config.points;
548 int sz = points.size();
549 int hot_point = gui->point_list->get_selection_number(0, 0);
550 if( sz > 1 && hot_point < sz-1 ) {
551 CriKeyPoint *&pt0 = points[hot_point];
552 CriKeyPoint *&pt1 = points[++hot_point];
553 CriKeyPoint *t = pt0; pt0 = pt1; pt1 = t;
554 gui->point_list->update(hot_point);
556 gui->send_configure_change();
560 CriKeyDrag::CriKeyDrag(CriKeyWindow *gui, int x, int y)
561 : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
565 int CriKeyDrag::handle_event()
567 CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
568 int value = get_value();
570 if( !gui->grab(cwindow_gui) ) {
576 gui->ungrab(cwindow_gui);
577 gui->plugin->config.drag = value;
578 gui->send_configure_change();
582 CriKeyNewPoint::CriKeyNewPoint(CriKeyWindow *gui, CriKey *plugin, int x, int y)
583 : BC_GenericButton(x, y, 80, _("New"))
586 this->plugin = plugin;
588 CriKeyNewPoint::~CriKeyNewPoint()
591 int CriKeyNewPoint::handle_event()
593 int k = plugin->new_point();
594 gui->point_list->update(k);
595 gui->send_configure_change();
599 CriKeyDelPoint::CriKeyDelPoint(CriKeyWindow *gui, CriKey *plugin, int x, int y)
600 : BC_GenericButton(x, y, 80, C_("Del"))
603 this->plugin = plugin;
605 CriKeyDelPoint::~CriKeyDelPoint()
608 int CriKeyDelPoint::handle_event()
610 int hot_point = gui->point_list->get_selection_number(0, 0);
611 CriKeyPoints &points = plugin->config.points;
612 if( hot_point >= 0 && hot_point < points.size() ) {
613 plugin->config.del_point(hot_point);
614 if( !points.size() ) plugin->new_point();
615 int sz = points.size();
616 if( hot_point >= sz && hot_point > 0 ) --hot_point;
617 gui->point_list->update(hot_point);
618 gui->send_configure_change();
623 CriKeyReset::CriKeyReset(CriKeyWindow *gui, CriKey *plugin, int x, int y)
624 : BC_GenericButton(x, y, _("Reset"))
627 this->plugin = plugin;
629 CriKeyReset::~CriKeyReset()
632 int CriKeyReset::handle_event()
634 CriKeyPoints &points = plugin->config.points;
635 points.remove_all_objects();
637 gui->point_list->update(0);
638 gui->send_configure_change();