digitalmars.D.learn - DLangUI: Rendering a generated image
- Alex Parrill (76/76) Apr 14 2015 What is the simplest way of rendering an application-generated
- Alex Parrill (11/11) Apr 14 2015 I found the `ImageWidget` class in controls.d, but I'm still
What is the simplest way of rendering an application-generated image using DLangUI? I'm trying to render the outputs of some noise-generating functions, along with some controls to alter the parameters of that function. I didn't see an ImageWidget anywhere, so I tried making my own that takes in a DrawBuf. The code is below. When the `buf.drawRescaled` line in the `onDraw` method is commented out, the `buf.drawLine` will render a line over where the widget would be located. But when the `drawRescaled` line is uncommented, the entire window becomes white; not even the line drawn afterwards is visible. What am I doing wrong? Alternatively, is there a simpler way that I'm not thinking of or don't see? import std.stdio; import dlangui; mixin APP_ENTRY_POINT; enum W = 512; enum H = 512; final class CanvasWidget : Widget { private DrawBuf canvas; this(DrawBuf canvas) { this.canvas = canvas; } override void onDraw(DrawBuf buf) { if (visibility != Visibility.Visible) return; Rect rc = _pos; //buf.drawRescaled(rc, canvas, Rect(0,0,canvas.width, canvas.height)); buf.drawLine(Point(rc.left, rc.top), Point(rc.right, rc.bottom), 0xff0000); _needDraw = false; } } void draw(ColorDrawBuf img) { for(int y=0; y<img.height; y++) { auto scanline = img.scanLine(y); for(uint x=0; x<img.width; x++) { scanline[x] = 0xFF0000; } } } //void main() { extern(C) int UIAppMain(string[] args) { Platform.instance.resourceDirs = []; Platform.instance.uiLanguage = "en"; Platform.instance.uiTheme = "theme_default"; Window window = Platform.instance.createWindow("Window", null); VerticalLayout baseLayout = new VerticalLayout(); baseLayout.layoutWidth = FILL_PARENT; baseLayout.layoutHeight = FILL_PARENT; window.mainWidget = baseLayout; auto img = new ColorDrawBuf(W,H); draw(img); Widget canvas = new CanvasWidget(img); canvas.layoutWidth = W; canvas.layoutHeight = H; baseLayout.addChild(canvas); baseLayout.addChild((new Button()) .text("Test"d) .textColor(0xff0000) ); window.show(); return Platform.instance.enterMessageLoop(); }
Apr 14 2015
I found the `ImageWidget` class in controls.d, but I'm still having the same issue. Tweaked code (replaced the section between `window.mainWidget = baseLayout;` and `baseLayout.addChild((new Button())...` auto img = Ref!DrawBuf(new ColorDrawBuf(W,H)); draw(cast(ColorDrawBuf) (img.get)); auto drawableImg = Ref!ImageDrawable(new ImageDrawable(img)); auto canvas = new ImageWidget(); canvas.drawable = drawableImg; baseLayout.addChild(canvas);
Apr 14 2015