MCDE dtpaint: curve and polygon tools (D3)
Curve is the reference's two-phase tool: drag the two end points, release, then move to bend the quadratic Bezier and click to commit. Polygon adds a vertex per left click, previews the open outline with a rubber line to the pointer, and closes on a right click or a double click; leaving the tool restores the snapshot. Both keep the one-step undo (snapshot at the first press). Measured under Xvfb: a bent curve from (300,200) to (500,300) through (400,150); a closed quadrilateral; Undo removes the polygon and keeps the earlier curve.
This commit is contained in:
1 parent
e987e0379f
commit
ffbf8c7f2d
1 file changed
+150
-3
+150
-3
@@ -124,12 +124,21 @@ static int tool = T_PENCIL;
|
||||
static unsigned long fgPix, bgPix;
|
||||
static XColor palette[PAL_N];
|
||||
static int drawing, sx, sy, lx, ly;
|
||||
static int curveState; /* 0 idle, 1 drag ends, 2 bend */
|
||||
static int curveX0, curveY0, curveX1, curveY1;
|
||||
static unsigned long curveColor;
|
||||
#define POLY_MAX 128
|
||||
static int polyActive, polyN, polyX[POLY_MAX], polyY[POLY_MAX];
|
||||
static unsigned long polyColor;
|
||||
static int polyLastX, polyLastY;
|
||||
static Time polyLastTime;
|
||||
static int toolsShown = 1, colorsShown = 1, statusShown = 1;
|
||||
static char curPath[512];
|
||||
static int dirty;
|
||||
static int pendingAction;
|
||||
|
||||
static void DestroyDialog(Widget w, XtPointer data, XtPointer call);
|
||||
static void Redraw(void);
|
||||
|
||||
/* ------------------------------------------------------------------ colour */
|
||||
|
||||
@@ -607,6 +616,62 @@ DrawShape(int x0, int y0, int x1, int y1, unsigned long color)
|
||||
}
|
||||
}
|
||||
|
||||
/* quadratic Bezier P0 -> control -> P1, sampled into short line segments */
|
||||
static void
|
||||
DrawCurve(int x0, int y0, int cx, int cy, int x1, int y1, unsigned long color)
|
||||
{
|
||||
double px = x0, py = y0;
|
||||
int i;
|
||||
|
||||
SetFg(color);
|
||||
for (i = 1; i <= 64; i++) {
|
||||
double t = i / 64.0, u = 1.0 - t;
|
||||
double x = u * u * x0 + 2.0 * u * t * cx + t * t * x1;
|
||||
double y = u * u * y0 + 2.0 * u * t * cy + t * t * y1;
|
||||
|
||||
XDrawLine(dpy, pix, gc, (int) (px + 0.5), (int) (py + 0.5),
|
||||
(int) (x + 0.5), (int) (y + 0.5));
|
||||
px = x;
|
||||
py = y;
|
||||
}
|
||||
}
|
||||
|
||||
/* the polygon so far plus a rubber line to the pointer, over the snapshot */
|
||||
static void
|
||||
DrawPolyPreview(int curx, int cury)
|
||||
{
|
||||
int i;
|
||||
|
||||
RestoreSnap();
|
||||
SetFg(polyColor);
|
||||
for (i = 1; i < polyN; i++)
|
||||
XDrawLine(dpy, pix, gc, polyX[i - 1], polyY[i - 1],
|
||||
polyX[i], polyY[i]);
|
||||
if (polyN > 0)
|
||||
XDrawLine(dpy, pix, gc, polyX[polyN - 1], polyY[polyN - 1],
|
||||
curx, cury);
|
||||
}
|
||||
|
||||
static void
|
||||
ClosePolygon(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
RestoreSnap();
|
||||
SetFg(polyColor);
|
||||
if (polyN >= 2) {
|
||||
for (i = 1; i < polyN; i++)
|
||||
XDrawLine(dpy, pix, gc, polyX[i - 1], polyY[i - 1],
|
||||
polyX[i], polyY[i]);
|
||||
XDrawLine(dpy, pix, gc, polyX[polyN - 1], polyY[polyN - 1],
|
||||
polyX[0], polyY[0]);
|
||||
dirty = 1;
|
||||
}
|
||||
polyActive = 0;
|
||||
drawing = 0;
|
||||
Redraw();
|
||||
}
|
||||
|
||||
/* Scanline flood fill: the stack holds one seed per run, not four neighbours
|
||||
* per pixel, so its size follows the shape's perimeter, not its area. */
|
||||
struct Seed { int x, y; };
|
||||
@@ -1094,6 +1159,54 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
||||
XDrawPoint(dpy, pix, gc, x, y);
|
||||
dirty = 1;
|
||||
break;
|
||||
case T_CURVE:
|
||||
if (curveState == 2) {
|
||||
/* the preview is the result */
|
||||
curveState = 0;
|
||||
drawing = 0;
|
||||
dirty = 1;
|
||||
Redraw();
|
||||
} else if (curveState == 0) {
|
||||
Snapshot();
|
||||
curveX0 = curveX1 = x;
|
||||
curveY0 = curveY1 = y;
|
||||
curveColor = color;
|
||||
curveState = 1;
|
||||
drawing = 1;
|
||||
} else {
|
||||
drawing = 0;
|
||||
}
|
||||
break;
|
||||
case T_POLYGON:
|
||||
if (polyActive && ev->xbutton.button == Button3) {
|
||||
ClosePolygon();
|
||||
break;
|
||||
}
|
||||
if (ev->xbutton.button != Button1) {
|
||||
drawing = 0;
|
||||
break;
|
||||
}
|
||||
if (!polyActive) {
|
||||
Snapshot();
|
||||
polyActive = 1;
|
||||
polyN = 0;
|
||||
polyColor = color;
|
||||
} else if (ev->xbutton.time - polyLastTime < 300 &&
|
||||
abs(x - polyLastX) < 4 && abs(y - polyLastY) < 4) {
|
||||
ClosePolygon();
|
||||
break;
|
||||
}
|
||||
if (polyN < POLY_MAX) {
|
||||
polyX[polyN] = x;
|
||||
polyY[polyN] = y;
|
||||
polyN++;
|
||||
}
|
||||
polyLastX = x;
|
||||
polyLastY = y;
|
||||
polyLastTime = ev->xbutton.time;
|
||||
DrawPolyPreview(x, y);
|
||||
drawing = 0;
|
||||
break;
|
||||
case T_ZOOM:
|
||||
SetZoom(ev->xbutton.button == Button3 ?
|
||||
(zoom > 1 ? zoom / 2 : 8) :
|
||||
@@ -1109,7 +1222,8 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
||||
x = ev->xmotion.x / zoom;
|
||||
y = ev->xmotion.y / zoom;
|
||||
StatusPos(x, y);
|
||||
if (!drawing)
|
||||
if (!drawing && !(tool == T_CURVE && curveState == 2) &&
|
||||
!(tool == T_POLYGON && polyActive))
|
||||
return;
|
||||
if (x < 0) x = 0;
|
||||
if (y < 0) y = 0;
|
||||
@@ -1133,6 +1247,18 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
||||
Spray(x, y, color);
|
||||
dirty = 1;
|
||||
break;
|
||||
case T_CURVE:
|
||||
RestoreSnap();
|
||||
if (curveState == 1)
|
||||
DrawLineTo(curveX0, curveY0, x, y, curveColor);
|
||||
else if (curveState == 2)
|
||||
DrawCurve(curveX0, curveY0, x, y, curveX1,
|
||||
curveY1, curveColor);
|
||||
break;
|
||||
case T_POLYGON:
|
||||
if (polyActive)
|
||||
DrawPolyPreview(x, y);
|
||||
break;
|
||||
case T_LINE: case T_RECT: case T_ELLIPSE: case T_ROUNDRECT:
|
||||
RestoreSnap();
|
||||
DrawShape(sx, sy, x, y, color);
|
||||
@@ -1145,6 +1271,17 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep)
|
||||
Redraw();
|
||||
break;
|
||||
case ButtonRelease:
|
||||
if (tool == T_CURVE && curveState == 1) {
|
||||
x = ev->xbutton.x / zoom;
|
||||
y = ev->xbutton.y / zoom;
|
||||
if (x < 0) x = 0;
|
||||
if (y < 0) y = 0;
|
||||
if (x >= imgW) x = imgW - 1;
|
||||
if (y >= imgH) y = imgH - 1;
|
||||
curveX1 = x;
|
||||
curveY1 = y;
|
||||
curveState = 2;
|
||||
}
|
||||
drawing = 0;
|
||||
Redraw();
|
||||
break;
|
||||
@@ -1157,8 +1294,18 @@ static void
|
||||
ToolChanged(Widget w, XtPointer data, XtPointer call)
|
||||
{
|
||||
(void) call;
|
||||
if (XmToggleButtonGetState(w))
|
||||
tool = (int) (long) data;
|
||||
if (!XmToggleButtonGetState(w))
|
||||
return;
|
||||
if (curveState != 0) {
|
||||
RestoreSnap();
|
||||
curveState = 0;
|
||||
}
|
||||
if (polyActive) {
|
||||
RestoreSnap();
|
||||
polyActive = 0;
|
||||
}
|
||||
tool = (int) (long) data;
|
||||
Redraw();
|
||||
}
|
||||
|
||||
static void
|
||||
|
||||
Reference in new issue
Block a user