From 318d90e5097f33a3dc515ce1928b81aa55b3d604 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Thu, 18 Feb 2021 16:23:54 -0600 Subject: [PATCH] drawRoundedRect + updated example --- README.md | 17 ----------------- examples/rounded_rectangle.nim | 13 +++---------- src/pixie.nim | 32 ++++++++++++++++++++++++++++++++ src/pixie/paths.nim | 16 +++++++++++----- tests/test_images_draw.nim | 10 ++++++++++ tests/test_masks.nim | 5 +++++ 6 files changed, 61 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 9a3de71..ec98a8a 100644 --- a/README.md +++ b/README.md @@ -46,23 +46,6 @@ image.drawSegment(segment(start, stop), color, strokeWidth = 10) ``` ![example output](examples/line.png) -### Rounded rectangle -[examples/rounded_rectangle.nim](examples/rounded_rectangle.nim) -```nim -let - x = 50.0 - y = 50.0 - w = 100.0 - h = 100.0 - r = 25.0 - -var path: Path -path.roundedRect(vec2(x, y), vec2(w, h), r, r, r, r) - -image.fillPath(path, rgba(0, 255, 0, 255)) -``` -![example output](examples/rounded_rectangle.png) - ### Heart [examples/heart.nim](examples/heart.nim) ```nim diff --git a/examples/rounded_rectangle.nim b/examples/rounded_rectangle.nim index 67f41f2..8b6f41b 100644 --- a/examples/rounded_rectangle.nim +++ b/examples/rounded_rectangle.nim @@ -4,15 +4,8 @@ let image = newImage(200, 200) image.fill(rgba(255, 255, 255, 255)) let - x = 50.0 - y = 50.0 - w = 100.0 - h = 100.0 + pos = vec2(50, 50) + wh = vec2(100, 100) r = 25.0 -var path: Path -path.roundedRect(vec2(x, y), vec2(w, h), r, r, r, r) - -image.fillPath(path, rgba(0, 255, 0, 255)) - -image.writeFile("examples/rounded_rectangle.png") +image.drawRoundedRect(rect(pos, wh), r, rgba(0, 255, 0, 255)) diff --git a/src/pixie.nim b/src/pixie.nim index 5305e4b..ba91f48 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -62,6 +62,38 @@ proc drawRect*(mask: Mask, rect: Rect) = path.rect(rect) mask.fillPath(path) +proc drawRoundedRect*( + image: Image, + rect: Rect, + nw, ne, se, sw: float32, + color: ColorRGBA, + blendMode = bmNormal +) = + var path: Path + path.roundedRect(rect, nw, ne, se, sw) + image.fillPath(path, color, wrNonZero, blendMode) + +proc drawRoundedRect*( + image: Image, + rect: Rect, + radius: float32, + color: ColorRGBA, + blendMode = bmNormal +) = + var path: Path + path.roundedRect(rect, radius, radius, radius, radius) + image.fillPath(path, color, wrNonZero, blendMode) + +proc drawRoundedRect*(mask: Mask, rect: Rect, nw, ne, se, sw: float32) = + var path: Path + path.roundedRect(rect, nw, ne, se, sw) + mask.fillPath(path) + +proc drawRoundedRect*(mask: Mask, rect: Rect, radius: float32) = + var path: Path + path.roundedRect(rect, radius, radius, radius, radius) + mask.fillPath(path) + proc drawSegment*( image: Image, segment: Segment, diff --git a/src/pixie/paths.nim b/src/pixie/paths.nim index acd5308..d040cf6 100644 --- a/src/pixie/paths.nim +++ b/src/pixie/paths.nim @@ -376,13 +376,9 @@ proc rect*(path: var Path, rect: Rect, clockwise = true) {.inline.} = const splineCircleK = 4.0 * (-1.0 + sqrt(2.0)) / 3 proc roundedRect*( - path: var Path, pos, wh: Vec2, nw, ne, se, sw: float32, clockwise = true + path: var Path, x, y, w, h, nw, ne, se, sw: float32, clockwise = true ) = let - x = pos.x - y = pos.y - w = wh.x - h = wh.y s = splineCircleK maxRadius = min(w / 2, h / 2) @@ -432,6 +428,16 @@ proc roundedRect*( path.closePath() +proc roundedRect*( + path: var Path, pos, wh: Vec2, nw, ne, se, sw: float32, clockwise = true +) {.inline.} = + path.roundedRect(pos.x, pos.y, wh.x, wh.y, nw, ne, se, sw, clockwise) + +proc roundedRect*( + path: var Path, rect: Rect, nw, ne, se, sw: float32, clockwise = true +) {.inline.} = + path.roundedRect(rect.x, rect.y, rect.w, rect.h, nw, ne, se, sw, clockwise) + proc ellipse*(path: var Path, cx, cy, rx, ry: float32) = let magicX = splineCircleK * rx diff --git a/tests/test_images_draw.nim b/tests/test_images_draw.nim index 274c369..cbebcb5 100644 --- a/tests/test_images_draw.nim +++ b/tests/test_images_draw.nim @@ -56,6 +56,16 @@ block: image.drawRect(rect(vec2(10, 10), vec2(30, 30)), rgba(255, 255, 0, 255)) image.writeFile("tests/images/drawRect.png") +block: + let image = newImage(100, 100) + image.fill(rgba(0, 255, 255, 255)) + image.drawRoundedRect( + rect(vec2(10, 10), vec2(30, 30)), + 10, + rgba(255, 255, 0, 255) + ) + image.writeFile("tests/images/drawRoundedRect.png") + block: let image = newImage(100, 100) image.fill(rgba(0, 255, 255, 255)) diff --git a/tests/test_masks.nim b/tests/test_masks.nim index 48d163f..2338bec 100644 --- a/tests/test_masks.nim +++ b/tests/test_masks.nim @@ -108,6 +108,11 @@ block: mask.drawRect(rect(vec2(10, 10), vec2(30, 30))) writeFile("tests/images/masks/drawRect.png", mask.encodePng()) +block: + let mask = newMask(100, 100) + mask.drawRoundedRect(rect(vec2(10, 10), vec2(30, 30)), 10) + writeFile("tests/images/masks/drawRoundedRect.png", mask.encodePng()) + block: let mask = newMask(100, 100) mask.drawSegment(