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 CriKeyPoints *points = gui->points;
58 int hot_point = points->get_selection_number(0, 0);
59 if( hot_point >= 0 && hot_point < gui->plugin->config.points.size() ) {
60 float v = atof(get_text());
61 gui->plugin->config.points[hot_point]->x = v;
62 points->set_point(hot_point, PT_X, v);
64 points->update_list();
65 gui->plugin->send_configure_change();
68 int CriKeyPointY::handle_event()
70 if( !CriKeyNum::handle_event() ) return 0;
71 CriKeyPoints *points = gui->points;
72 int hot_point = points->get_selection_number(0, 0);
73 if( hot_point >= 0 && hot_point < gui->plugin->config.points.size() ) {
74 float v = atof(get_text());
75 gui->plugin->config.points[hot_point]->y = v;
76 points->set_point(hot_point, PT_Y, v);
78 points->update_list();
79 gui->plugin->send_configure_change();
83 int CriKeyDrawModeItem::handle_event()
85 ((CriKeyDrawMode *)get_popup_menu())->update(id);
88 CriKeyDrawMode::CriKeyDrawMode(CriKeyWindow *gui, int x, int y)
89 : BC_PopupMenu(x, y, 100, "", 1)
92 draw_modes[DRAW_ALPHA] = _("Alpha");
93 draw_modes[DRAW_EDGE] = _("Edge");
94 draw_modes[DRAW_MASK] = _("Mask");
97 void CriKeyDrawMode::create_objects()
99 for( int i=0; i<DRAW_MODES; ++i )
100 add_item(new CriKeyDrawModeItem(draw_modes[i], i));
101 update(gui->plugin->config.draw_mode, 0);
103 void CriKeyDrawMode::update(int mode, int send)
105 if( this->mode == mode ) return;
107 set_text(draw_modes[mode]);
108 gui->plugin->config.draw_mode = mode;
109 if( send ) gui->plugin->send_configure_change();
112 CriKeyColorButton::CriKeyColorButton(CriKeyWindow *gui, int x, int y)
113 : BC_GenericButton(x, y, _("Color"))
117 int CriKeyColorButton::handle_event()
119 gui->start_color_thread();
123 CriKeyColorPicker::CriKeyColorPicker(CriKeyColorButton *color_button)
124 : ColorPicker(0, _("Color"))
126 this->color_button = color_button;
129 void CriKeyColorPicker::start(int color)
131 orig_color = this->color = color;
132 start_window(color, 0, 1);
135 void CriKeyColorPicker::handle_done_event(int result)
137 if( result ) color = orig_color;
138 CriKeyWindow *gui = color_button->gui;
139 gui->lock_window("CriKeyColorPicker::handle_done_event");
140 gui->update_color(color);
141 gui->plugin->config.color = color;
142 gui->plugin->send_configure_change();
143 gui->unlock_window();
146 int CriKeyColorPicker::handle_new_color(int color, int alpha)
148 CriKeyWindow *gui = color_button->gui;
149 gui->lock_window("CriKeyColorPicker::handle_new_color");
150 gui->update_color(this->color = color);
152 gui->plugin->config.color = color;
153 gui->plugin->send_configure_change();
154 gui->unlock_window();
159 void CriKeyWindow::start_color_thread()
163 color_picker = new CriKeyColorPicker(color_button);
164 color_picker->start(plugin->config.color);
165 lock_window("CriKeyWindow::start_color_thread");
169 CriKeyWindow::CriKeyWindow(CriKey *plugin)
170 : PluginClientWindow(plugin, 380, 360, 380, 360, 0)
172 this->plugin = plugin;
173 this->color_button = 0;
174 this->color_picker = 0;
175 this->title_x = 0; this->point_x = 0;
176 this->title_y = 0; this->point_y = 0;
177 this->new_point = 0; this->del_point = 0;
178 this->point_up = 0; this->point_dn = 0;
179 this->drag = 0; this->dragging = 0;
180 this->last_x = 0; this->last_y = 0;
181 this->points = 0; this->cur_point = 0;
184 CriKeyWindow::~CriKeyWindow()
190 void CriKeyWindow::create_objects()
193 int margin = plugin->get_theme()->widget_border;
195 add_subwindow(title = new BC_Title(x, y, _("Threshold:")));
196 y += title->get_h() + margin;
197 add_subwindow(threshold = new CriKeyThreshold(this, x, y, get_w() - x * 2));
198 y += threshold->get_h() + margin;
199 add_subwindow(color_button = new CriKeyColorButton(this, x, y));
200 int x1 = x + color_button->get_w() + margin;
201 color_x = x1; color_y = y;
202 update_color(plugin->config.color);
203 y += COLOR_H + 10 + margin ;
204 add_subwindow(title = new BC_Title(x, y+5, _("Draw mode:")));
205 x1 = x + title->get_w() + 10 + margin;
206 add_subwindow(draw_mode = new CriKeyDrawMode(this, x1, y));
207 draw_mode->create_objects();
208 y += draw_mode->get_h() + 10 + margin;
210 CriKeyPoint *pt = plugin->config.points[plugin->config.selected];
211 add_subwindow(title_x = new BC_Title(x, y, _("X:")));
212 x1 = x + title_x->get_w() + margin;
213 point_x = new CriKeyPointX(this, x1, y, pt->x);
214 point_x->create_objects();
215 x1 += point_x->get_w() + margin;
216 add_subwindow(new_point = new CriKeyNewPoint(this, plugin, x1, y));
217 x1 += new_point->get_w() + margin;
218 add_subwindow(point_up = new CriKeyPointUp(this, x1, y));
219 y += point_x->get_h() + margin;
220 add_subwindow(title_y = new BC_Title(x, y, _("Y:")));
221 x1 = x + title_y->get_w() + margin;
222 point_y = new CriKeyPointY(this, x1, y, pt->y);
223 point_y->create_objects();
224 x1 += point_y->get_w() + margin;
225 add_subwindow(del_point = new CriKeyDelPoint(this, plugin, x1, y));
226 x1 += del_point->get_w() + margin;
227 add_subwindow(point_dn = new CriKeyPointDn(this, x1, y));
228 y += point_y->get_h() + margin + 10;
230 add_subwindow(drag = new CriKeyDrag(this, x, y));
231 if( plugin->config.drag ) {
232 if( !grab(plugin->server->mwindow->cwindow->gui) )
233 eprintf("drag enabled, but compositor already grabbed\n");
236 x1 = x + drag->get_w() + margin + 20;
237 add_subwindow(cur_point = new CriKeyCurPoint(this, plugin, x1, y+3));
238 cur_point->update(plugin->config.selected);
239 y += drag->get_h() + margin;
240 add_subwindow(points = new CriKeyPoints(this, plugin, x, y));
241 points->update(plugin->config.selected);
246 int CriKeyWindow::grab_event(XEvent *event)
248 switch( event->type ) {
249 case ButtonPress: break;
250 case ButtonRelease: break;
251 case MotionNotify: break;
255 MWindow *mwindow = plugin->server->mwindow;
256 CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
257 CWindowCanvas *canvas = cwindow_gui->canvas;
258 int cx, cy; cwindow_gui->get_relative_cursor_xy(cx, cy);
259 cx -= mwindow->theme->ccanvas_x;
260 cy -= mwindow->theme->ccanvas_y;
263 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ) return 0;
264 if( cy < 0 || cy >= mwindow->theme->ccanvas_h ) return 0;
267 switch( event->type ) {
269 if( dragging ) return 0;
270 dragging = event->xbutton.state & ShiftMask ? -1 : 1;
273 if( !dragging ) return 0;
277 if( !dragging ) return 0;
283 float cursor_x = cx, cursor_y = cy;
284 canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
285 int64_t position = plugin->get_source_position();
286 float projector_x, projector_y, projector_z;
287 Track *track = plugin->server->plugin->track;
288 int track_w = track->track_w, track_h = track->track_h;
289 track->automation->get_projector(
290 &projector_x, &projector_y, &projector_z,
291 position, PLAY_FORWARD);
292 projector_x += mwindow->edl->session->output_w / 2;
293 projector_y += mwindow->edl->session->output_h / 2;
294 float output_x = (cursor_x - projector_x) / projector_z + track_w / 2;
295 float output_y = (cursor_y - projector_y) / projector_z + track_h / 2;
296 point_x->update((int64_t)(output_x));
297 point_y->update((int64_t)(output_y));
300 switch( event->type ) {
303 int n = plugin->config.points.size();
305 CriKeyPoint *pt = plugin->config.points[hot_point=0];
306 double dist = DISTANCE(output_x,output_y, pt->x,pt->y);
307 for( int i=1; i<n; ++i ) {
308 pt = plugin->config.points[i];
309 double d = DISTANCE(output_x,output_y, pt->x,pt->y);
310 if( d >= dist ) continue;
311 dist = d; hot_point = i;
314 if( hot_point >= 0 ) {
315 CriKeyPoint *pt = plugin->config.points[hot_point];
316 if( pt->x == output_x && pt->y == output_y ) break;
317 points->set_point(hot_point, PT_X, pt->x = output_x);
318 points->set_point(hot_point, PT_Y, pt->y = output_y);
319 plugin->config.selected = hot_point;
320 points->set_selected(hot_point);
321 points->update_list();
325 int hot_point = points->get_selection_number(0, 0);
326 if( hot_point >= 0 && hot_point < plugin->config.points.size() ) {
327 CriKeyPoint *pt = plugin->config.points[hot_point];
328 if( pt->x == output_x && pt->y == output_y ) break;
329 points->set_point(hot_point, PT_X, pt->x = output_x);
330 points->set_point(hot_point, PT_Y, pt->y = output_y);
331 point_x->update(pt->x);
332 point_y->update(pt->y);
333 points->update_list();
339 switch( event->type ) {
341 float dx = output_x - last_x, dy = output_y - last_y;
342 int n = plugin->config.points.size();
343 for( int i=0; i<n; ++i ) {
344 CriKeyPoint *pt = plugin->config.points[i];
345 points->set_point(i, PT_X, pt->x += dx);
346 points->set_point(i, PT_Y, pt->y += dy);
348 int hot_point = points->get_selection_number(0, 0);
349 if( hot_point >= 0 && hot_point < n ) {
350 CriKeyPoint *pt = plugin->config.points[hot_point];
351 point_x->update(pt->x);
352 point_y->update(pt->y);
353 points->update_list();
359 last_x = output_x; last_y = output_y;
360 plugin->send_configure_change();
364 void CriKeyWindow::done_event(int result)
366 ungrab(client->server->mwindow->cwindow->gui);
367 if( color_picker ) color_picker->close_window();
370 CriKeyPoints::CriKeyPoints(CriKeyWindow *gui, CriKey *plugin, int x, int y)
371 : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
374 this->plugin = plugin;
375 titles[PT_E] = _("E"); widths[PT_E] = 40;
376 titles[PT_X] = _("X"); widths[PT_X] = 120;
377 titles[PT_Y] = _("Y"); widths[PT_Y] = 120;
378 titles[PT_T] = _("Tag"); widths[PT_T] = 60;
380 CriKeyPoints::~CriKeyPoints()
384 void CriKeyPoints::clear()
386 for( int i=PT_SZ; --i>=0; )
387 cols[i].remove_all_objects();
390 int CriKeyPoints::column_resize_event()
392 for( int i=PT_SZ; --i>=0; )
393 widths[i] = get_column_width(i);
397 int CriKeyPoints::handle_event()
399 int hot_point = get_selection_number(0, 0);
400 const char *x_text = "", *y_text = "";
401 if( hot_point >= 0 && hot_point < plugin->config.points.size() ) {
402 if( get_cursor_x() < widths[0] ) {
403 plugin->config.points[hot_point]->e =
404 !plugin->config.points[hot_point]->e;
406 x_text = gui->points->cols[PT_X].get(hot_point)->get_text();
407 y_text = gui->points->cols[PT_Y].get(hot_point)->get_text();
411 gui->point_x->update(x_text);
412 gui->point_y->update(y_text);
413 plugin->config.selected = hot_point;
415 gui->plugin->send_configure_change();
419 int CriKeyPoints::selection_changed()
425 void CriKeyPoints::new_point(const char *ep, const char *xp, const char *yp, const char *tp)
427 cols[PT_E].append(new BC_ListBoxItem(ep));
428 cols[PT_X].append(new BC_ListBoxItem(xp));
429 cols[PT_Y].append(new BC_ListBoxItem(yp));
430 cols[PT_T].append(new BC_ListBoxItem(tp));
433 void CriKeyPoints::del_point(int i)
435 for( int n=cols[0].size()-1, c=PT_SZ; --c>=0; )
436 cols[c].remove_object_number(n-i);
439 void CriKeyPoints::set_point(int i, int c, float v)
441 char s[BCSTRLEN]; sprintf(s,"%0.4f",v);
444 void CriKeyPoints::set_point(int i, int c, const char *cp)
446 cols[c].get(i)->set_text(cp);
449 int CriKeyPoints::set_selected(int k)
451 int sz = gui->plugin->config.points.size();
454 int n = gui->points->get_selection_number(0, 0);
456 for( int i=0; i<PT_SZ; ++i )
457 cols[i].get(n)->set_selected(0);
459 for( int i=0; i<PT_SZ; ++i )
460 cols[i].get(k)->set_selected(1);
461 gui->cur_point->update(k);
464 void CriKeyPoints::update_list()
466 int xpos = get_xposition(), ypos = get_xposition();
467 int k = get_selection_number(0, 0);
468 BC_ListBox::update(&cols[0], &titles[0],&widths[0],PT_SZ, xpos,ypos,k);
470 void CriKeyPoints::update(int k)
472 if( k < 0 ) k = get_selection_number(0, 0);
473 gui->cur_point->update(k);
474 int xpos = get_xposition(), ypos = get_xposition();
477 ArrayList<CriKeyPoint*> &points = plugin->config.points;
478 int n = points.size();
479 for( int i=0; i<n; ++i ) {
480 CriKeyPoint *pt = points[i];
481 char etxt[BCSTRLEN]; sprintf(etxt,"%s", pt->e ? "*" : "");
482 char xtxt[BCSTRLEN]; sprintf(xtxt,"%0.4f", pt->x);
483 char ytxt[BCSTRLEN]; sprintf(ytxt,"%0.4f", pt->y);
484 char ttxt[BCSTRLEN]; sprintf(ttxt,"%d", pt->t);
485 new_point(etxt, xtxt, ytxt, ttxt);
488 for( int i=PT_SZ; --i>=0; )
489 cols[i].get(k)->set_selected(1);
490 gui->point_x->update(gui->points->cols[PT_X].get(k)->get_text());
491 gui->point_y->update(gui->points->cols[PT_Y].get(k)->get_text());
494 BC_ListBox::update(&cols[0], &titles[0],&widths[0],PT_SZ, xpos,ypos,k);
497 void CriKeyWindow::update_color(int color)
500 draw_box(color_x, color_y, COLOR_W, COLOR_H);
502 draw_rectangle(color_x, color_y, COLOR_W, COLOR_H);
503 flash(color_x, color_y, COLOR_W, COLOR_H);
506 void CriKeyWindow::update_gui()
508 draw_mode->update(plugin->config.draw_mode);
509 update_color(plugin->config.color);
510 threshold->update(plugin->config.threshold);
511 cur_point->update(plugin->config.selected);
512 drag->update(plugin->config.drag);
513 points->update(plugin->config.selected);
517 CriKeyThreshold::CriKeyThreshold(CriKeyWindow *gui, int x, int y, int w)
518 : BC_FSlider(x, y, 0, w, w, 0, 1, gui->plugin->config.threshold, 0)
521 set_precision(0.005);
524 int CriKeyThreshold::handle_event()
526 float v = get_value();
527 gui->plugin->config.threshold = v;
528 gui->plugin->send_configure_change();
533 CriKeyPointUp::CriKeyPointUp(CriKeyWindow *gui, int x, int y)
534 : BC_GenericButton(x, y, _("Up"))
538 CriKeyPointUp::~CriKeyPointUp()
542 int CriKeyPointUp::handle_event()
544 int n = gui->plugin->config.points.size();
545 int hot_point = gui->points->get_selection_number(0, 0);
547 if( n > 1 && hot_point > 0 ) {
548 CriKeyPoint *&pt0 = gui->plugin->config.points[hot_point];
549 CriKeyPoint *&pt1 = gui->plugin->config.points[--hot_point];
550 CriKeyPoint *t = pt0; pt0 = pt1; pt1 = t;
551 gui->plugin->config.selected = hot_point;
552 gui->points->update(hot_point);
554 gui->plugin->send_configure_change();
558 CriKeyPointDn::CriKeyPointDn(CriKeyWindow *gui, int x, int y)
559 : BC_GenericButton(x, y, _("Dn"))
563 CriKeyPointDn::~CriKeyPointDn()
567 int CriKeyPointDn::handle_event()
569 int n = gui->plugin->config.points.size();
570 int hot_point = gui->points->get_selection_number(0, 0);
571 if( n > 1 && hot_point < n-1 ) {
572 CriKeyPoint *&pt0 = gui->plugin->config.points[hot_point];
573 CriKeyPoint *&pt1 = gui->plugin->config.points[++hot_point];
574 CriKeyPoint *t = pt0; pt0 = pt1; pt1 = t;
575 gui->plugin->config.selected = hot_point;
576 gui->points->update(hot_point);
578 gui->plugin->send_configure_change();
582 CriKeyDrag::CriKeyDrag(CriKeyWindow *gui, int x, int y)
583 : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
587 int CriKeyDrag::handle_event()
589 CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
590 int value = get_value();
592 if( !gui->grab(cwindow_gui) ) {
598 gui->ungrab(cwindow_gui);
599 gui->plugin->config.drag = value;
600 gui->plugin->send_configure_change();
604 CriKeyNewPoint::CriKeyNewPoint(CriKeyWindow *gui, CriKey *plugin, int x, int y)
605 : BC_GenericButton(x, y, 80, _("New"))
608 this->plugin = plugin;
610 CriKeyNewPoint::~CriKeyNewPoint()
613 int CriKeyNewPoint::handle_event()
615 int k = plugin->new_point();
616 gui->points->update(k);
617 gui->plugin->send_configure_change();
621 CriKeyDelPoint::CriKeyDelPoint(CriKeyWindow *gui, CriKey *plugin, int x, int y)
622 : BC_GenericButton(x, y, 80, _("Del"))
625 this->plugin = plugin;
627 CriKeyDelPoint::~CriKeyDelPoint()
630 int CriKeyDelPoint::handle_event()
632 int hot_point = gui->points->get_selection_number(0, 0);
633 if( hot_point >= 0 && hot_point < gui->plugin->config.points.size() ) {
634 plugin->config.del_point(hot_point);
635 if( !plugin->config.points.size() ) plugin->new_point();
636 int n = gui->plugin->config.points.size();
637 if( hot_point >= n && hot_point > 0 ) --hot_point;
638 gui->plugin->config.selected = hot_point;
639 gui->points->update(hot_point);
640 gui->plugin->send_configure_change();
645 CriKeyCurPoint::CriKeyCurPoint(CriKeyWindow *gui, CriKey *plugin, int x, int y)
649 this->plugin = plugin;
651 CriKeyCurPoint::~CriKeyCurPoint()
654 void CriKeyCurPoint::update(int n)
656 char string[BCSTRLEN];
657 sprintf(string, _("Selected: %d "), n);
658 BC_Title::update(string);