From 2b74271c310c49f8ca4ddac1b76c79641452ad5e Mon Sep 17 00:00:00 2001 From: dayllenger Date: Mon, 16 Oct 2017 12:16:15 +0300 Subject: [PATCH] draw box shadows as nine-patch; fix style's child states --- src/dlangui/graphics/resources.d | 41 +++++++++++++++++++++++++------- src/dlangui/widgets/styles.d | 1 + 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/dlangui/graphics/resources.d b/src/dlangui/graphics/resources.d index 3a433f4b..b595aab1 100644 --- a/src/dlangui/graphics/resources.d +++ b/src/dlangui/graphics/resources.d @@ -422,10 +422,10 @@ class BoxShadowDrawable : Drawable { _blurSize = blurSize; _color = color; // now create a texture which will contain the shadow - uint size = 2 * blurSize + 3; + uint size = 4 * blurSize + 1; texture = new ColorDrawBuf(size, size); // TODO: get from/put to cache // clear - texture.fill(0xFFFFFFFF); + texture.fill(color | 0xFF000000); // draw a square in center of the texture texture.fillRect(Rect(blurSize, blurSize, size - blurSize, size - blurSize), color); // blur the square @@ -433,14 +433,39 @@ class BoxShadowDrawable : Drawable { } override void drawTo(DrawBuf buf, Rect rc, uint state = 0, int tilex0 = 0, int tiley0 = 0) { + // this is a size of blurred part + uint b = _blurSize + _blurSize / 2 + 1; // move and expand the shadow - rc.left += _offsetX - _blurSize; - rc.right += _offsetX + _blurSize; - rc.top += _offsetY - _blurSize; - rc.bottom += _offsetY + _blurSize; - // apply new clipping to DrawBuf to draw outside of the widget + rc.left += _offsetX - b; + rc.top += _offsetY - b; + rc.right += _offsetX + b; + rc.bottom += _offsetY + b; + + // apply new clipping to the DrawBuf to draw outside of the widget auto saver = ClipRectSaver(buf, rc, 0, false); - buf.drawRescaled(rc, texture, Rect(0, 0, texture.width, texture.height)); // TODO: nine-patch + + if (_blurSize > 0) { + // Manual nine-patch + uint w = texture.width; + uint h = texture.height; + + buf.drawFragment(rc.left, rc.top, texture, Rect(0, 0, b, b)); // top left + buf.drawRescaled(Rect(rc.left + b, rc.top, rc.right - b, rc.top + b), texture, Rect(b, 0, w - b, b)); // top center + buf.drawFragment(rc.right - b, rc.top, texture, Rect(w - b, 0, w, b)); // top right + + buf.drawRescaled(Rect(rc.left, rc.top + b, rc.left + b, rc.bottom - b), texture, Rect(0, b, b, h - b)); // middle left + buf.drawRescaled(Rect(rc.left + b, rc.top + b, rc.right - b, rc.bottom - b), texture, Rect(b, b, w - b, h - b)); // middle center + buf.drawRescaled(Rect(rc.right - b, rc.top + b, rc.right, rc.bottom - b), texture, Rect(w - b, b, w, h - b)); // middle right + + buf.drawFragment(rc.left, rc.bottom - b, texture, Rect(0, h - b, b, h)); // bottom left + buf.drawRescaled(Rect(rc.left + b, rc.bottom - b, rc.right - b, rc.bottom), texture, Rect(b, h - b, w - b, h)); // bottom center + buf.drawFragment(rc.right - b, rc.bottom - b, texture, Rect(w - b, h - b, w, h)); // bottom right + + // debug + //~ buf.drawFragment(rc.left, rc.top, texture, Rect(0, 0, w, h)); + } else { + buf.fillRect(rc, _color); + } } @property override int width() { return 1; } diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index 2e762538..28d907a4 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -926,6 +926,7 @@ public: res._alpha = _alpha; res._fontFace = _fontFace; res._backgroundImageId = _backgroundImageId; + res._boxShadow = _boxShadow; res._border = _border; res._padding = _padding; res._margins = _margins;