1 /** 2 * The root widget to inherit from for a flat UI. 3 * 4 * Copyright: Copyright Auburn Sounds 2015-2017. 5 * Copyright: Cut Through Recordings 2017. 6 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 7 * Authors: Ethan Reker 8 */ 9 module dplug.flatwidgets.flatbackgroundgui; 10 11 import gfm.math.box; 12 import dplug.core.nogc; 13 import dplug.core.file; 14 import dplug.graphics.color; 15 import dplug.graphics.image; 16 import dplug.graphics.view; 17 import dplug.graphics.drawex; 18 19 import dplug.core.nogc; 20 import dplug.gui.graphics; 21 22 /// FlatBackgroundGUI provides a background that is loaded from a PNG or JPEG 23 /// image. The string for backgroundPath should be in "stringImportPaths" 24 /// specified in dub.json 25 class FlatBackgroundGUI(string backgroundPath) : GUIGraphics 26 { 27 public: 28 nothrow: 29 @nogc: 30 31 this(int width, int height) 32 { 33 super(width, height); 34 _backgroundImage = loadOwnedImage(cast(ubyte[])(import(backgroundPath))); 35 } 36 37 ~this() 38 { 39 if(_backgroundImage) 40 _backgroundImage.destroyFree(); 41 } 42 43 override void reflow(box2i availableSpace) 44 { 45 _position = availableSpace; 46 } 47 48 /// Fill diffuse map with diffuse from background image. Alpha is ignored since ideally a background image will not 49 /// need an alpha channel. 50 /// Material and depth maps are zeroed out to initialize them. Otherwise this can lead to nasty errors. 51 override void onDraw(ImageRef!RGBA diffuseMap, ImageRef!L16 depthMap, ImageRef!RGBA materialMap, box2i[] dirtyRects) nothrow @nogc 52 { 53 foreach(dirtyRect; dirtyRects) 54 { 55 auto croppedDiffuseIn = _backgroundImage.crop(dirtyRect); 56 auto croppedDiffuseOut = diffuseMap.crop(dirtyRect); 57 auto croppedMaterialOut = materialMap.crop(dirtyRect); 58 auto croppedDepthOut = depthMap.crop(dirtyRect); 59 60 immutable RGBA inputMaterial = RGBA(0, 0, 0, 0); 61 immutable L16 inputDepth = L16(0); 62 63 for(int j = 0; j < dirtyRect.height; ++j){ 64 RGBA[] inputDiffuse = croppedDiffuseIn.scanline(j); 65 RGBA[] outputDiffuse = croppedDiffuseOut.scanline(j); 66 RGBA[] outputMaterial = croppedMaterialOut.scanline(j); 67 L16[] outputDepth = croppedDepthOut.scanline(j); 68 69 for(int i = 0; i < dirtyRect.width; ++i){ 70 outputDiffuse[i] = inputDiffuse[i]; 71 outputMaterial[i] = inputMaterial; 72 outputDepth[i] = inputDepth; 73 } 74 } 75 } 76 } 77 78 private: 79 OwnedImage!RGBA _backgroundImage; 80 }