1 /* 2 Copyright (c) 2013 Andrey Penechko 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license the "Software" to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module anchovy.gui.behaviors.imagebehavior; 30 31 import anchovy.graphics.texture; 32 import anchovy.graphics.bitmap; 33 34 import anchovy.gui; 35 import anchovy.gui.interfaces.iwidgetbehavior; 36 37 class ImageBehavior : IWidgetBehavior 38 { 39 override void attachTo(Widget widget) 40 { 41 widget.removeEventHandlers!DrawEvent(); 42 43 GuiContext context = widget.getPropertyAs!("context", GuiContext); 44 auto bitmap = new Bitmap(4); 45 Texture texture = new Texture(bitmap, TextureTarget.target2d, TextureFormat.rgba); 46 47 widget.setProperty!"texture"(texture); 48 widget.setProperty!"bitmap"(bitmap); 49 widget.addEventHandler(&handleDraw); 50 51 auto bitmapSlot = {widget["prefSize"] = cast(ivec2)widget.getPropertyAs!("bitmap", Bitmap).size;}; 52 bitmap.dataChanged.connect(bitmapSlot); 53 54 void onBitmapChanging(FlexibleObject obj, Variant* newBitmap) 55 { 56 widget.getPropertyAs!("bitmap", Bitmap).dataChanged.disconnect(bitmapSlot); 57 if ((*newBitmap).get!Bitmap is null) return; 58 59 auto bitmap = (*newBitmap).get!Bitmap; 60 61 Texture texture = widget.getPropertyAs!("texture", Texture); 62 texture.bitmap = bitmap; 63 bitmap.dataChanged.connect(bitmapSlot); 64 65 obj["prefSize"] = cast(ivec2)bitmap.size; 66 } 67 68 widget.property("bitmap").valueChanging.connect(&onBitmapChanging); 69 70 void onTextureChanged(FlexibleObject obj, Variant newTexture) 71 { 72 if (auto bitmap = widget.getPropertyAs!("bitmap", Bitmap)) 73 bitmap.dataChanged.disconnect(bitmapSlot); 74 75 auto texture = newTexture.get!Texture; 76 if (texture is null) return; 77 78 widget.setProperty!("bitmap", Bitmap)(texture.bitmap); 79 texture.bitmap.dataChanged.connect(bitmapSlot); 80 81 obj["prefSize"] = cast(ivec2)texture.bitmap.size; 82 } 83 84 widget.property("texture").valueChanged.connect(&onTextureChanged); 85 } 86 87 bool handleDraw(Widget widget, DrawEvent event) 88 { 89 if(widget.getPropertyAs!("isVisible", bool)) 90 { 91 Texture texture = widget.getPropertyAs!("texture", Texture); 92 //writeln(texture.size); 93 if (texture is null) return true; 94 event.guiRenderer.renderer.setColor(Color(255, 255, 255, 255)); 95 event.guiRenderer.renderer.drawTexRect(widget["staticRect"].get!Rect, Rect(ivec2(0,0), cast(ivec2)texture.size), texture); 96 } 97 return true; 98 } 99 }