diff --git a/examples/example1/src/example1.d b/examples/example1/src/example1.d index 3db6f55d..57950a49 100644 --- a/examples/example1/src/example1.d +++ b/examples/example1/src/example1.d @@ -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); diff --git a/src/dlangui/graphics/drawbuf.d b/src/dlangui/graphics/drawbuf.d index 2dbb349d..2b6ce4cc 100644 --- a/src/dlangui/graphics/drawbuf.d +++ b/src/dlangui/graphics/drawbuf.d @@ -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;