diff options
| author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2008-01-12 23:45:33 +0100 |
|---|---|---|
| committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2008-01-12 23:45:33 +0100 |
| commit | f17430be7bf4aaef23b991d04c8804b4f171d7b0 (patch) | |
| tree | 1ef42ef7012803d0cc9b226aa72a3d1c29ac1fc0 /src | |
| parent | e9dce46bed3cc0ec6993d09183ac858a32d619e2 (diff) | |
some fixes
Diffstat (limited to 'src')
| -rw-r--r-- | src/CurveEditor.H | 4 | ||||
| -rw-r--r-- | src/CurveEditor.cxx | 80 | ||||
| -rw-r--r-- | src/flcurve.cxx | 5 |
3 files changed, 79 insertions, 10 deletions
diff --git a/src/CurveEditor.H b/src/CurveEditor.H index e774310..333bd45 100644 --- a/src/CurveEditor.H +++ b/src/CurveEditor.H @@ -15,12 +15,16 @@ class CurveEditor : public Fl_Widget { double *X; double *Y; int n; + int marked_point; gsl_interp_accel *acc; gsl_spline *spline; public: CurveEditor(int _x, int _y, int _w, int _h); + + void set(int i, double _x, double _y); void draw(); + int handle(int e); }; #endif diff --git a/src/CurveEditor.cxx b/src/CurveEditor.cxx index 699261d..ad73758 100644 --- a/src/CurveEditor.cxx +++ b/src/CurveEditor.cxx @@ -6,6 +6,7 @@ #include <stdio.h> #include <stdlib.h> +#include <math.h> #include <FL/Fl.H> #include <FL/fl_draw.H> @@ -15,18 +16,17 @@ CurveEditor::CurveEditor(int my_x, int my_y, int my_w, int my_h) : Fl_Widget(my_x, my_y, my_w, my_h) { - n = 3; + marked_point = -1; + + n = 5; X = (double*) calloc(n, sizeof(double)); Y = (double*) calloc(n, sizeof(double)); - X[0] = 0.0; - Y[0] = 0.0; - - X[1] = 0.2; - Y[1] = 0.4; - X[2] = 0.8; - Y[2] = 0.6; + 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); @@ -48,6 +48,70 @@ CurveEditor::draw() { 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() * 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(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(), (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); + redraw(); +} diff --git a/src/flcurve.cxx b/src/flcurve.cxx index ce8de6a..2769a51 100644 --- a/src/flcurve.cxx +++ b/src/flcurve.cxx @@ -46,9 +46,10 @@ main(int argc, char **argv) { } } - Fl_Window window(800, 600); - CurveEditor ce(10, 10, 780, 580); + Fl_Window window(800, 800); + CurveEditor ce(10, 10, 780, 780); window.end(); + window.resizable(ce); window.show(1, argv); |
