better animation demo

This commit is contained in:
Vadim Lopatin 2014-05-20 17:03:55 +04:00
parent 767e6734cb
commit c71da5b7fc
3 changed files with 61 additions and 27 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

@ -24,18 +24,10 @@ Widget createAboutWidget()
return res; return res;
} }
class SampleAnimationWidget : Widget { class AnimatedDrawable : Drawable {
ulong animationProgress; DrawableRef background;
this(string ID) { this() {
super(ID); background = drawableCache.get("tx_fabric.tiled");
backgroundImageId = "tx_fabric.tiled"; // tx_fabric.jpg tiled
}
/// returns true is widget is being animated - need to call animate() and redraw
@property override bool animating() { return true; }
/// animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second)
override void animate(long interval) {
animationProgress += interval;
invalidate();
} }
void drawAnimatedRect(DrawBuf buf, uint p, Rect rc, int speedx, int speedy, int sz) { void drawAnimatedRect(DrawBuf buf, uint p, Rect rc, int speedx, int speedy, int sz) {
int x = (p * speedx % rc.width); int x = (p * speedx % rc.width);
@ -51,23 +43,65 @@ class SampleAnimationWidget : Widget {
uint color = (a << 24) | (r << 16) | (g << 8) | b; uint color = (a << 24) | (r << 16) | (g << 8) | b;
buf.fillRect(Rect(rc.left + x, rc.top + y, rc.left + x + sz, rc.top + y + sz), color); buf.fillRect(Rect(rc.left + x, rc.top + y, rc.left + x + sz, rc.top + y + sz), color);
} }
/// Draw widget at its position to buffer void drawAnimatedIcon(DrawBuf buf, uint p, Rect rc, int speedx, int speedy, string resourceId) {
override void onDraw(DrawBuf buf) { int x = (p * speedx % rc.width);
if (visibility != Visibility.Visible) int y = (p * speedy % rc.height);
return; if (x < 0)
super.onDraw(buf); x += rc.width;
Rect rc = _pos; if (y < 0)
applyMargins(rc); y += rc.height;
auto saver = ClipRectSaver(buf, rc, alpha); DrawBufRef image = drawableCache.getImage(resourceId);
applyPadding(rc); buf.drawImage(x, y, image.get);
}
override void drawTo(DrawBuf buf, Rect rc, uint state = 0, int tilex0 = 0, int tiley0 = 0) {
background.drawTo(buf, rc, state, cast(int)(animationProgress / 695430), cast(int)(animationProgress / 1500000));
drawAnimatedRect(buf, cast(uint)(animationProgress / 295430), rc, 2, 3, 100); drawAnimatedRect(buf, cast(uint)(animationProgress / 295430), rc, 2, 3, 100);
drawAnimatedRect(buf, cast(uint)(animationProgress / 312400) + 100, rc, 3, 2, 130); drawAnimatedRect(buf, cast(uint)(animationProgress / 312400) + 100, rc, 3, 2, 130);
drawAnimatedRect(buf, cast(uint)(animationProgress / 212400) + 200, rc, -2, 1, 290); drawAnimatedIcon(buf, cast(uint)(animationProgress / 212400) + 200, rc, -2, 1, "dlangui-logo1");
drawAnimatedRect(buf, cast(uint)(animationProgress / 512400) + 300, rc, 2, -2, 200); drawAnimatedRect(buf, cast(uint)(animationProgress / 512400) + 300, rc, 2, -2, 200);
drawAnimatedRect(buf, cast(uint)(animationProgress / 214230) + 800, rc, 1, 2, 390); drawAnimatedRect(buf, cast(uint)(animationProgress / 214230) + 800, rc, 1, 2, 390);
drawAnimatedRect(buf, cast(uint)(animationProgress / 123320) + 900, rc, -1, -3, 150); drawAnimatedIcon(buf, cast(uint)(animationProgress / 123320) + 900, rc, 1, 2, "cr3_logo");
drawAnimatedRect(buf, cast(uint)(animationProgress / 100000) + 100, rc, -1, -1, 120); drawAnimatedRect(buf, cast(uint)(animationProgress / 100000) + 100, rc, -1, -1, 120);
} }
@property override int width() {
return 1;
}
@property override int height() {
return 1;
}
ulong animationProgress;
void animate(long interval) {
animationProgress += interval;
}
}
class SampleAnimationWidget : VerticalLayout {
AnimatedDrawable drawable;
DrawableRef drawableRef;
this(string ID) {
super(ID);
drawable = new AnimatedDrawable();
drawableRef = drawable;
padding = Rect(20, 20, 20, 20);
addChild(new TextWidget(null, "This is TextWidget on top of animated background"d));
addChild(new EditLine(null, "This is EditLine on top of animated background"d));
addChild(new Button(null, "This is Button on top of animated background"d));
addChild(new VSpacer());
}
/// background drawable
@property override DrawableRef backgroundDrawable() const {
return (cast(SampleAnimationWidget)this).drawableRef;
}
/// returns true is widget is being animated - need to call animate() and redraw
@property override bool animating() { return true; }
/// animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second)
override void animate(long interval) {
drawable.animate(interval);
invalidate();
}
} }
Widget createEditorSettingsControl(EditWidgetBase editor) { Widget createEditorSettingsControl(EditWidgetBase editor) {

View File

@ -302,9 +302,9 @@ class Widget {
@property Rect padding() const { @property Rect padding() const {
// get max padding from style padding and background drawable padding // get max padding from style padding and background drawable padding
Rect p = style.padding; Rect p = style.padding;
DrawableRef d = style.backgroundDrawable; DrawableRef d = backgroundDrawable;
if (!d.isNull) { if (!d.isNull) {
Rect dp = style.backgroundDrawable.padding; Rect dp = d.padding;
if (p.left < dp.left) if (p.left < dp.left)
p.left = dp.left; p.left = dp.left;
if (p.right < dp.right) if (p.right < dp.right)
@ -344,7 +344,7 @@ class Widget {
/// background drawable /// background drawable
@property DrawableRef backgroundDrawable() const { @property DrawableRef backgroundDrawable() const {
return style.backgroundDrawable; return stateStyle.backgroundDrawable;
} }
/// widget drawing alpha value (0=opaque .. 255=transparent) /// widget drawing alpha value (0=opaque .. 255=transparent)
@ -1058,7 +1058,7 @@ class Widget {
Rect rc = _pos; Rect rc = _pos;
applyMargins(rc); applyMargins(rc);
auto saver = ClipRectSaver(buf, rc, alpha); auto saver = ClipRectSaver(buf, rc, alpha);
DrawableRef bg = stateStyle.backgroundDrawable; DrawableRef bg = backgroundDrawable;
if (!bg.isNull) { if (!bg.isNull) {
bg.drawTo(buf, rc, state); bg.drawTo(buf, rc, state);
} }