summaryrefslogtreecommitdiff
path: root/src/pnmcurvedit.cxx
blob: 52a473d3ca26656f22ba7da9c313ce21ea1703b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Copyright 2008 Johannes Hofmann <Johannes.Hofmann@gmx.de>
//
// This software may be used and distributed according to the terms
// of the GNU General Public License, incorporated herein by reference.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>

#include "CurveEditor.H"

static CurveEditor *ce;
static char cmd[2048];

static void editor_cb(Fl_Widget* o, void* d) {
	double x, y;

	printf("%s ", cmd);
	for (int i = 0; i<ce->get_n(); i++) {
		if (i != 0)
			printf(",");
		ce->get_point(i, &x, &y);
		printf("%g:%g", x, y);
	}
	printf("\n");
	fflush(stdout);
}

static void
stdin_cb(int fd, void *d) {
	char *curve = NULL;

	fgets(cmd, sizeof(cmd), stdin);

	for (int i = strlen(cmd) - 1; i >= 0; i--) {
		if (isspace(cmd[i])) {
			cmd[i] = '\0';
			if (curve)
				break;
		} else {
			curve = &cmd[i];
		}
	}

	if (curve) {
		double X, Y;
		char *pstr;

		ce->clear();
		while (pstr = strsep(&curve, ",")) {
			if (sscanf(pstr, "%lf:%lf", &X, &Y) != 2 ||
				X < 0 || X > 1 || Y < 0 || Y > 1) {
				fprintf(stderr, "Could not parse control point %s.\n", pstr);
				continue;
			}       
			ce->add_point(X, Y);
		} 
	}
}

int
main(int argc, char **argv) {
	Fl_Double_Window window(800, 600, "pnmcurvedit");
	ce = new CurveEditor(0, 0, 800, 600);
	ce->add_point(0.0, 0.0);
	ce->add_point(0.5, 0.4);
	ce->add_point(1.0, 1.0);
	ce->callback(editor_cb, NULL);

	window.resizable(ce);
	window.show();

	Fl::add_fd(0, FL_READ, stdin_cb);

	return Fl::run();
}