From cae8df9a063efbfd80dad12ca5c47be616273aab Mon Sep 17 00:00:00 2001 From: Macoy Madson Date: Sat, 17 Sep 2022 13:00:30 -0400 Subject: [PATCH] Improve selection display --- src/NanoSVG.cake | 33 ++++++++++++++++++++ src/VectorPuppetShow.cake | 63 +++++++++++++++++++++++---------------- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/src/NanoSVG.cake b/src/NanoSVG.cake index 2d806ad..d2646ea 100644 --- a/src/NanoSVG.cake +++ b/src/NanoSVG.cake @@ -1,4 +1,5 @@ ;; NanoSVG.cake: Load and rasterize SVG files +(import "CHelpers.cake") (c-preprocessor-define-constant NANOSVG_IMPLEMENTATION 1) (c-preprocessor-define-constant NANOSVGRAST_IMPLEMENTATION 1) @@ -14,3 +15,35 @@ (set (token-splice shape-name) (path (token-splice shape-name) > next)) (token-splice-rest body tokens))) (return true)) + +(forward-declare (struct NSVGshape)) + +;; This isn't 100% accurate I think, but does seem to make containment likely +(defun nanosvg-get-shape-bounds-with-stroke-int (shape (addr NSVGshape) bounds-out (addr int)) + ;; Minimums + (each-in-range 2 i + (set (at i bounds-out) + (type-cast (- (at i (path shape > bounds)) + (path shape > strokeWidth)) + int))) + ;; Maximums + (each-in-interval 2 4 i + (set (at i bounds-out) + (type-cast (+ (at i (path shape > bounds)) + (path shape > strokeWidth)) + int)))) + +;; This isn't 100% accurate I think, but does seem to make containment likely +(defun nanosvg-get-shape-bounds-with-stroke-float (shape (addr NSVGshape) bounds-out (addr float)) + ;; Minimums + (each-in-range 2 i + (set (at i bounds-out) + (type-cast (- (at i (path shape > bounds)) + (path shape > strokeWidth)) + float))) + ;; Maximums + (each-in-interval 2 4 i + (set (at i bounds-out) + (type-cast (+ (at i (path shape > bounds)) + (path shape > strokeWidth)) + float)))) diff --git a/src/VectorPuppetShow.cake b/src/VectorPuppetShow.cake index 63e6a40..f7440ca 100644 --- a/src/VectorPuppetShow.cake +++ b/src/VectorPuppetShow.cake @@ -267,31 +267,44 @@ (SDL_RenderCopy renderer svg-image-texture (addr source-rectangle) (addr destination-rectangle))) - (var mouse-x int 0) - (var mouse-y int 0) - (SDL_GetMouseState (addr mouse-x) (addr mouse-y)) - (each-shape-in-svg-image puppet-image shape - (var shape-bounds SDL_Rect - (array (type-cast (at 0 (path shape > bounds)) int) - (type-cast (at 1 (path shape > bounds)) int) - (type-cast (- (at 2 (path shape > bounds)) - (at 0 (path shape > bounds))) - int) - (type-cast (- (at 3 (path shape > bounds)) - (at 1 (path shape > bounds))) - int))) - (SDL_SetRenderDrawColor renderer 230 10 10 255) - (when (and (>= mouse-x (type-cast (at 0 (path shape > bounds)) float)) - (>= mouse-y (type-cast (at 1 (path shape > bounds)) float)) - (< mouse-x (type-cast (at 2 (path shape > bounds)) float)) - (< mouse-y (type-cast (at 3 (path shape > bounds)) float))) - (SDL_SetRenderDrawColor renderer 10 230 10 255) - (render-string - renderer (addr (field (at body-font-index font-atlases) atlas)) - (field (at body-font-index font-atlases) texture) - (/ true-window-width 9) (- true-window-height 10) - (path shape > id))) - (SDL_RenderDrawRect renderer (addr shape-bounds))) + (scope ;; Shape selection and debug viewing + (var mouse-x int 0) + (var mouse-y int 0) + (SDL_GetMouseState (addr mouse-x) (addr mouse-y)) + (var hovered-shape-id-buffer (array 1024 char) (array 0)) + (var hovered-write-head (addr char) hovered-shape-id-buffer) + (each-shape-in-svg-image puppet-image shape + (var sized-bounds (array 4 int) (array 0)) + (nanosvg-get-shape-bounds-with-stroke-int shape sized-bounds) + (var shape-bounds SDL_Rect + (array (at 0 sized-bounds) + (at 1 sized-bounds) + (type-cast (- (at 2 sized-bounds) + (at 0 sized-bounds)) + int) + (type-cast (- (at 3 sized-bounds) + (at 1 sized-bounds)) + int))) + (SDL_SetRenderDrawColor renderer 230 10 10 255) + (when (and (>= mouse-x (type-cast (at 0 sized-bounds) float)) + (>= mouse-y (type-cast (at 1 sized-bounds) float)) + (< mouse-x (type-cast (at 2 sized-bounds) float)) + (< mouse-y (type-cast (at 3 sized-bounds) float))) + (SDL_SetRenderDrawColor renderer 10 230 10 255) + (set hovered-write-head + (+ hovered-write-head + (snprintf hovered-write-head + (- (array-size hovered-shape-id-buffer) + (- hovered-write-head hovered-shape-id-buffer)) + (? (!= hovered-shape-id-buffer hovered-write-head) + ", %s" "%s") + (path shape > id))))) + (SDL_RenderDrawRect renderer (addr shape-bounds))) + (render-string + renderer (addr (field (at body-font-index font-atlases) atlas)) + (field (at body-font-index font-atlases) texture) + (/ true-window-width 9) (- true-window-height 10) + hovered-shape-id-buffer)) (when true (var buffer (array 256 char) (array 0))