// // Copyright 2008 Johannes Hofmann // // This software may be used and distributed according to the terms // of the GNU General Public License, incorporated herein by reference. #include #include #include #include #include #include "CurveEditor.H" CurveEditor::CurveEditor(int my_x, int my_y, int my_w, int my_h) : Fl_Widget(my_x, my_y, my_w, my_h) { marked_point = -1; n = 5; X = (double*) calloc(n, sizeof(double)); Y = (double*) calloc(n, sizeof(double)); for (int i = 0; i < n; i++) { X[i] = (double) i / (n - 1); Y[i] = (double) i / (n - 1); } acc = gsl_interp_accel_alloc (); spline = gsl_spline_alloc (gsl_interp_cspline, n); gsl_spline_init (spline, X, Y, n); } void CurveEditor::draw() { fl_color(FL_BLACK); fl_rectf(x(), y(), w(), h()); fl_color(FL_WHITE); fl_begin_line(); for (double _x = 0.0; _x < 1.0; _x = _x + 0.01) { double _y = gsl_spline_eval(spline, _x, acc); fl_vertex(x() + _x * w(), y() + h() - h() * _y); } fl_end_line(); for (int i = 0; i < n; i++) { if (i == marked_point) { fl_color(FL_RED); } else { fl_color(FL_WHITE); } fl_rect(x() + w() * X[i] - 2, y() + h() - h() * Y[i] - 2, 4, 4); } } int CurveEditor::handle(int event) { int mark_x = Fl::event_x() - x(); int mark_y = Fl::event_y() - y(); switch(event) { case FL_PUSH: if (Fl::event_button() == 1) { for (int i = 0; i < n; i++) { int x_i = (int) rint(X[i] * w()); int y_i = (int) rint(h() - Y[i] * h()); if (mark_x >= x_i - 2 && mark_x <= x_i + 2 && mark_y >= y_i - 2 && mark_y <= y_i + 2) { marked_point = i; redraw(); break; } } } Fl::focus(this); return 1; break; case FL_DRAG: set(marked_point, (double) mark_x / w(), 1.0 - (double) mark_y / h()); return 1; break; case FL_RELEASE: marked_point = -1; return 1; break; case FL_FOCUS: return 1; break; case FL_UNFOCUS: return 0; break; } return 0; } void CurveEditor::set(int i, double _x, double _y) { if (i >= n || (_x < 0.0 || _x > 1.0 || _y < 0.0 || _y > 1.0) || (i < n - 1 && _x >= X[i + 1]) || (i > 0 && _x <= X[i - 1])) { return; } X[i] = _x; Y[i] = _y; gsl_spline_init (spline, X, Y, n); printf("%4f,%4f", X[0], Y[0]); for (int i = 1; i < n; i++) { printf(";%4f,%4f", X[i], Y[i]); } printf("\n"); redraw(); }