1 /** 2 Copyright: Copyright (c) 2013-2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module anchovy.gui.behaviors.imagebehavior; 8 9 import anchovy.graphics.texture; 10 import anchovy.graphics.bitmap; 11 12 import anchovy.gui; 13 import anchovy.gui.interfaces.iwidgetbehavior; 14 15 class ImageBehavior : IWidgetBehavior 16 { 17 override void attachTo(Widget widget) 18 { 19 widget.removeEventHandlers!DrawEvent(); 20 21 GuiContext context = widget.getPropertyAs!("context", GuiContext); 22 auto bitmap = new Bitmap(4); 23 Texture texture = new Texture(bitmap, TextureTarget.target2d, TextureFormat.rgba); 24 25 widget.setProperty!"texture"(texture); 26 widget.setProperty!"bitmap"(bitmap); 27 widget.addEventHandler(&handleDraw); 28 29 auto bitmapSlot = {widget["prefSize"] = cast(ivec2)widget.getPropertyAs!("bitmap", Bitmap).size;}; 30 bitmap.dataChanged.connect(bitmapSlot); 31 32 void onBitmapChanging(FlexibleObject obj, Variant* newBitmap) 33 { 34 widget.getPropertyAs!("bitmap", Bitmap).dataChanged.disconnect(bitmapSlot); 35 if ((*newBitmap).get!Bitmap is null) return; 36 37 auto bitmap = (*newBitmap).get!Bitmap; 38 39 Texture texture = widget.getPropertyAs!("texture", Texture); 40 texture.bitmap = bitmap; 41 bitmap.dataChanged.connect(bitmapSlot); 42 43 obj["prefSize"] = cast(ivec2)bitmap.size; 44 } 45 46 widget.property("bitmap").valueChanging.connect(&onBitmapChanging); 47 48 void onTextureChanged(FlexibleObject obj, Variant newTexture) 49 { 50 if (auto bitmap = widget.getPropertyAs!("bitmap", Bitmap)) 51 bitmap.dataChanged.disconnect(bitmapSlot); 52 53 auto texture = newTexture.get!Texture; 54 if (texture is null) return; 55 56 widget.setProperty!("bitmap", Bitmap)(texture.bitmap); 57 texture.bitmap.dataChanged.connect(bitmapSlot); 58 59 obj["prefSize"] = cast(ivec2)texture.bitmap.size; 60 } 61 62 widget.property("texture").valueChanged.connect(&onTextureChanged); 63 } 64 65 bool handleDraw(Widget widget, DrawEvent event) 66 { 67 if(widget.getPropertyAs!("hasBack", bool) && event.sinking) 68 { 69 Texture texture = widget.getPropertyAs!("texture", Texture); 70 //writeln(texture.size); 71 if (texture is null) return true; 72 event.guiRenderer.renderer.setColor(Color(255, 255, 255, 255)); 73 event.guiRenderer.renderer.drawTexRect(widget["staticRect"].get!Rect, Rect(ivec2(0,0), cast(ivec2)texture.size), texture); 74 } 75 return true; 76 } 77 }