diff --git a/examples/example1/src/example1.d b/examples/example1/src/example1.d index 37e6cae2..3db6f55d 100644 --- a/examples/example1/src/example1.d +++ b/examples/example1/src/example1.d @@ -840,6 +840,19 @@ void main() tabs.addTab((new SampleAnimationWidget("tab6")).layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT), "TAB_ANIMATION"c); + CanvasWidget canvas = new CanvasWidget("canvas"); + canvas.layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT); + canvas.onDrawListener = delegate(CanvasWidget canvas, DrawBuf buf, Rect rc) { + 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); + 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); + }; + tabs.addTab(canvas, "TAB_CANVAS"c); //========================================================================== diff --git a/examples/example1/views/res/i18n/en.ini b/examples/example1/views/res/i18n/en.ini index 5016474a..f71127d3 100644 --- a/examples/example1/views/res/i18n/en.ini +++ b/examples/example1/views/res/i18n/en.ini @@ -32,4 +32,5 @@ TAB_BUTTONS=Buttons TAB_ANIMATION=Animation TAB_TABLE_LAYOUT=Table layout TAB_EDITORS=Editors +TAB_CANVAS=Canvas diff --git a/src/dlangui/widgets/controls.d b/src/dlangui/widgets/controls.d index 82968d80..b9a36186 100644 --- a/src/dlangui/widgets/controls.d +++ b/src/dlangui/widgets/controls.d @@ -994,3 +994,38 @@ class ScrollBar : AbstractSlider, OnClickHandler { } } +/// interface - slot for onClick +interface OnDrawHandler { + void doDraw(CanvasWidget canvas, DrawBuf buf, Rect rc); +} + +/// canvas widget - draw on it either by overriding of doDraw() or by assigning of onDrawListener +class CanvasWidget : Widget { + + Listener!OnDrawHandler onDrawListener; + + this(string ID = null) { + super(ID); + } + + override void measure(int parentWidth, int parentHeight) { + measuredContent(parentWidth, parentHeight, 0, 0); + } + + void doDraw(DrawBuf buf, Rect rc) { + if (onDrawListener.assigned) + onDrawListener(this, buf, rc); + } + + override void onDraw(DrawBuf buf) { + if (visibility != Visibility.Visible) + return; + super.onDraw(buf); + Rect rc = _pos; + applyMargins(rc); + auto saver = ClipRectSaver(buf, rc, alpha); + applyPadding(rc); + doDraw(buf, rc); + } +} +