Skip to content

Commit

Permalink
Library: Adding padding support for NeumorphDrawableShape
Browse files Browse the repository at this point in the history
  • Loading branch information
fornewid committed May 14, 2020
1 parent 9678eeb commit 9ec9540
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
45 changes: 35 additions & 10 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,31 @@ class NeumorphShapeDrawable : Drawable {
// not supported yet
}

private fun getBoundsInternal(): Rect {
return drawableState.padding?.let { padding ->
val bounds = super.getBounds()
Rect(
bounds.left + padding.left,
bounds.top + padding.top,
bounds.right - padding.right,
bounds.bottom - padding.bottom
)
} ?: super.getBounds()
}

private fun getBoundsAsRectF(): RectF {
rectF.set(bounds)
rectF.set(getBoundsInternal())
return rectF
}

fun setPadding(left: Int, top: Int, right: Int, bottom: Int) {
if (drawableState.padding == null) {
drawableState.padding = Rect()
}
drawableState.padding?.set(left, top, right, bottom)
invalidateSelf()
}

fun setShapeType(@ShapeType shapeType: Int) {
if (drawableState.shapeType != shapeType) {
drawableState.shapeType = shapeType
Expand Down Expand Up @@ -250,7 +270,7 @@ class NeumorphShapeDrawable : Drawable {

if (dirty) {
calculateOutlinePath(getBoundsAsRectF(), outlinePath)
shadow?.updateShadowBitmap(bounds)
shadow?.updateShadowBitmap(getBoundsInternal())
dirty = false
}

Expand Down Expand Up @@ -281,17 +301,19 @@ class NeumorphShapeDrawable : Drawable {
}

private fun calculateOutlinePath(bounds: RectF, path: Path) {
val w = bounds.width()
val h = bounds.height()
val left = drawableState.padding?.left?.toFloat() ?: 0f
val top = drawableState.padding?.top?.toFloat() ?: 0f
val right = left + bounds.width()
val bottom = top + bounds.height()
path.reset()
when (drawableState.shapeAppearanceModel.getCornerFamily()) {
CornerFamily.OVAL -> {
path.addOval(0f, 0f, w, h, Path.Direction.CW)
path.addOval(left, top, right, bottom, Path.Direction.CW)
}
CornerFamily.ROUNDED -> {
val cornerSize = drawableState.shapeAppearanceModel.getCornerSize()
path.addRoundRect(
0f, 0f, w, h,
left, top, right, bottom,
cornerSize, cornerSize,
Path.Direction.CW
)
Expand All @@ -303,11 +325,11 @@ class NeumorphShapeDrawable : Drawable {
override fun getOutline(outline: Outline) {
when (drawableState.shapeAppearanceModel.getCornerFamily()) {
CornerFamily.OVAL -> {
outline.setOval(bounds)
outline.setOval(getBoundsInternal())
}
CornerFamily.ROUNDED -> {
val cornerSize = drawableState.shapeAppearanceModel.getCornerSize()
outline.setRoundRect(bounds, cornerSize)
outline.setRoundRect(getBoundsInternal(), cornerSize)
}
}
}
Expand All @@ -318,8 +340,7 @@ class NeumorphShapeDrawable : Drawable {
}

override fun onStateChange(state: IntArray): Boolean {
val paintColorChanged = updateColorsForState(state)
val invalidateSelf = paintColorChanged
val invalidateSelf = updateColorsForState(state)
if (invalidateSelf) {
invalidateSelf()
}
Expand Down Expand Up @@ -352,6 +373,7 @@ class NeumorphShapeDrawable : Drawable {
var shapeAppearanceModel: NeumorphShapeAppearanceModel
val blurProvider: BlurProvider

var padding: Rect? = null
var fillColor: ColorStateList? = null
var strokeColor: ColorStateList? = null
var strokeWidth = 0f
Expand Down Expand Up @@ -387,6 +409,9 @@ class NeumorphShapeDrawable : Drawable {
strokeColor = orig.strokeColor
strokeWidth = orig.strokeWidth
paintStyle = orig.paintStyle
if (orig.padding != null) {
padding = Rect(orig.padding)
}
}

override fun newDrawable(): Drawable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,23 @@ internal class FlatShape(
canvas.withClipOut(outlinePath) {
val elevation = drawableState.shadowElevation
val z = drawableState.shadowElevation + drawableState.translationZ
val left: Float
val top: Float
val padding = drawableState.padding
if (padding != null) {
left = padding.left.toFloat()
top = padding.top.toFloat()
} else {
left = 0f
top = 0f
}
lightShadowBitmap?.let {
val offset = -elevation - z
drawBitmap(it, offset, offset, null)
drawBitmap(it, offset + left, offset + top, null)
}
darkShadowBitmap?.let {
val offset = -elevation + z
drawBitmap(it, offset, offset, null)
drawBitmap(it, offset + left, offset + top, null)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ internal class PressedShape(
override fun draw(canvas: Canvas, outlinePath: Path) {
canvas.withClip(outlinePath) {
shadowBitmap?.let {
drawBitmap(it, 0f, 0f, null)
val left: Float
val top: Float
val padding = drawableState.padding
if (padding != null) {
left = padding.left.toFloat()
top = padding.top.toFloat()
} else {
left = 0f
top = 0f
}
drawBitmap(it, left, top, null)
}
}
}
Expand Down

0 comments on commit 9ec9540

Please sign in to comment.