added DrawBuf,drawLine() - issue #64 - based on code from Ted Bullen; OpenGL version of drawLine still needs optimization to avoid drawing by-pixel

This commit is contained in:
Vadim Lopatin 2015-03-24 21:56:54 +03:00
parent 0c2b25d558
commit a9d1a31f6b
2 changed files with 50 additions and 0 deletions

View File

@ -843,14 +843,20 @@ void main()
CanvasWidget canvas = new CanvasWidget("canvas");
canvas.layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT);
canvas.onDrawListener = delegate(CanvasWidget canvas, DrawBuf buf, Rect rc) {
buf.fill(0xFFFFFF);
int x = rc.left;
int y = rc.top;
buf.fillRect(Rect(x+20, y+20, x+150, y+200), 0x80FF80);
buf.fillRect(Rect(x+90, y+80, x+250, y+250), 0x80FF80FF);
canvas.font.drawText(buf, x + 40, y + 50, "fillRect()"d, 0xC080C0);
buf.drawFrame(Rect(x + 400, y + 30, x + 550, y + 150), 0x204060, Rect(2,3,4,5), 0x80704020);
canvas.font.drawText(buf, x + 400, y + 5, "drawFrame()"d, 0x208020);
canvas.font.drawText(buf, x + 300, y + 100, "drawPixel()"d, 0x000080);
for (int i = 0; i < 80; i++)
buf.drawPixel(x+300 + i * 4, y+140 + i * 3 % 100, 0xFF0000 + i * 2);
canvas.font.drawText(buf, x + 200, y + 250, "drawLine()"d, 0x800020);
for (int i = 0; i < 40; i+=3)
buf.drawLine(Point(x+200 + i * 4, y+290), Point(x+150 + i * 7, y+420 + i * 2), 0x008000 + i * 5);
};
tabs.addTab(canvas, "TAB_CANVAS"c);

View File

@ -338,6 +338,50 @@ class DrawBuf : RefCountedObject {
}
}
/// draw line from point p1 to p2 with specified color
void drawLine(Point p1, Point p2, uint colour) {
if (p1.x < _clipRect.left && p2.x < _clipRect.left)
return;
if (p1.y < _clipRect.top && p2.y < _clipRect.top)
return;
if (p1.x >= _clipRect.right && p2.x >= _clipRect.right)
return;
if (p1.y >= _clipRect.bottom && p2.y >= _clipRect.bottom)
return;
// from rosettacode.org
import std.math: abs;
immutable int dx = p2.x - p1.x;
immutable int ix = (dx > 0) - (dx < 0);
immutable int dx2 = abs(dx) * 2;
int dy = p2.y - p1.y;
immutable int iy = (dy > 0) - (dy < 0);
immutable int dy2 = abs(dy) * 2;
drawPixel(p1.x, p1.y, colour);
if (dx2 >= dy2) {
int error = dy2 - (dx2 / 2);
while (p1.x != p2.x) {
if (error >= 0 && (error || (ix > 0))) {
error -= dx2;
p1.y += iy;
}
error += dy2;
p1.x += ix;
drawPixel(p1.x, p1.y, colour);
}
} else {
int error = dx2 - (dy2 / 2);
while (p1.y != p2.y) {
if (error >= 0 && (error || (iy > 0))) {
error -= dy2;
p1.x += ix;
}
error += dx2;
p1.y += iy;
drawPixel(p1.x, p1.y, colour);
}
}
}
/// create drawbuf with copy of current buffer with changed colors (returns this if not supported)
DrawBuf transformColors(ref ColorTransform transform) {
return this;