1 module widgets.animation; 2 3 import dlangui; 4 5 static if(BACKEND_GUI) 6 { 7 class AnimatedDrawable : Drawable 8 { 9 DrawableRef background; 10 this() 11 { 12 background = drawableCache.get("tx_fabric.tiled"); 13 } 14 15 void drawAnimatedRect(DrawBuf buf, uint p, Rect rc, int speedx, int speedy, int sz) 16 { 17 int x = (p * speedx % rc.width); 18 int y = (p * speedy % rc.height); 19 if (x < 0) 20 x += rc.width; 21 if (y < 0) 22 y += rc.height; 23 uint a = 64 + ((p / 2) & 0x7F); 24 uint r = 128 + ((p / 7) & 0x7F); 25 uint g = 128 + ((p / 5) & 0x7F); 26 uint b = 128 + ((p / 3) & 0x7F); 27 uint color = (a << 24) | (r << 16) | (g << 8) | b; 28 buf.fillRect(Rect(rc.left + x, rc.top + y, rc.left + x + sz, rc.top + y + sz), color); 29 } 30 31 void drawAnimatedIcon(DrawBuf buf, uint p, Rect rc, int speedx, int speedy, string resourceId) 32 { 33 int x = (p * speedx % rc.width); 34 int y = (p * speedy % rc.height); 35 if (x < 0) 36 x += rc.width; 37 if (y < 0) 38 y += rc.height; 39 DrawBufRef image = drawableCache.getImage(resourceId); 40 buf.drawImage(x, y, image.get); 41 } 42 43 override void drawTo(DrawBuf buf, Rect rc, uint state = 0, int tilex0 = 0, int tiley0 = 0) 44 { 45 background.drawTo(buf, rc, state, cast(int)(animationProgress / 695430), cast(int)(animationProgress / 1500000)); 46 drawAnimatedRect(buf, cast(uint)(animationProgress / 295430), rc, 2, 3, 100); 47 drawAnimatedRect(buf, cast(uint)(animationProgress / 312400) + 100, rc, 3, 2, 130); 48 drawAnimatedIcon(buf, cast(uint)(animationProgress / 212400) + 200, rc, -2, 1, "dlangui-logo1"); 49 drawAnimatedRect(buf, cast(uint)(animationProgress / 512400) + 300, rc, 2, -2, 200); 50 drawAnimatedRect(buf, cast(uint)(animationProgress / 214230) + 800, rc, 1, 2, 390); 51 drawAnimatedIcon(buf, cast(uint)(animationProgress / 123320) + 900, rc, 1, 2, "cr3_logo"); 52 drawAnimatedRect(buf, cast(uint)(animationProgress / 100000) + 100, rc, -1, -1, 120); 53 } 54 @property override int width() { return 1; } 55 @property override int height() { return 1; } 56 ulong animationProgress; 57 void animate(long interval) { animationProgress += interval; } 58 59 } 60 61 class SampleAnimationWidget : VerticalLayout 62 { 63 AnimatedDrawable drawable; 64 DrawableRef drawableRef; 65 this(string ID) 66 { 67 super(ID); 68 drawable = new AnimatedDrawable(); 69 drawableRef = drawable; 70 padding = Rect(20, 20, 20, 20); 71 addChild(new TextWidget(null, "This is TextWidget on top of animated background"d)); 72 addChild(new EditLine(null, "This is EditLine on top of animated background"d)); 73 addChild(new Button(null, "This is Button on top of animated background"d)); 74 addChild(new VSpacer()); 75 } 76 77 /// background drawable 78 @property override DrawableRef backgroundDrawable() const 79 { 80 return (cast(SampleAnimationWidget)this).drawableRef; 81 } 82 83 /// returns true is widget is being animated - need to call animate() and redraw 84 @property override bool animating() const { return true; } 85 86 /// animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second) 87 override void animate(long interval) 88 { 89 drawable.animate(interval); 90 invalidate(); 91 } 92 } 93 } 94 else 95 { 96 class SampleAnimationWidget : VerticalLayout 97 { 98 this(string ID) 99 { 100 super(ID); 101 addChild(new TextWidget(null, "Animations in text mode are not supported"d)); 102 } 103 } 104 }