From d7b5a9294264e6227e4c3470dad9a1376cf50d81 Mon Sep 17 00:00:00 2001 From: Macoy Madson Date: Sat, 24 Sep 2022 22:18:57 -0400 Subject: [PATCH] Draw from atlas, make simple selection code --- src/VectorPuppetShow.cake | 75 +++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/src/VectorPuppetShow.cake b/src/VectorPuppetShow.cake index 3de7193..81b0fc7 100644 --- a/src/VectorPuppetShow.cake +++ b/src/VectorPuppetShow.cake @@ -25,6 +25,7 @@ (define-keybind s-view-selection-keybind (array SDL_SCANCODE_F1)) (define-keybind s-view-atlas-keybind (array SDL_SCANCODE_F2)) (define-keybind s-view-svg-keybind (array SDL_SCANCODE_F3)) +(define-keybind s-view-puppet-keybind (array SDL_SCANCODE_F4)) (define-keybind s-toggle-fullscreen-keybind (array SDL_SCANCODE_F11)) @@ -45,6 +46,15 @@ (c-import "") (add-static-link-objects "User32.lib"))) +(defun-local is-shape-smaller (bounds (array 4 int) compare-to (array 4 int) + &return bool) + (var bounds-width int (abs (- (at 2 bounds) (at 0 bounds)))) + (var bounds-height int (abs (- (at 3 bounds) (at 1 bounds)))) + (var compare-to-width int (abs (- (at 2 compare-to) (at 0 compare-to)))) + (var compare-to-height int (abs (- (at 3 compare-to) (at 1 compare-to)))) + (return (and (< bounds-width compare-to-width) + (< bounds-height compare-to-height)))) + (defun main (&return int) (comptime-cond ('Windows @@ -153,8 +163,8 @@ (var puppet-atlas-width int 0) (var puppet-atlas-height int 0) (scope - (var filename (addr (const char)) "data/TestPuppet.svg") - ;; (var filename (addr (const char)) "data/DoNotCheckIn.svg") + ;; (var filename (addr (const char)) "data/TestPuppet.svg") + (var filename (addr (const char)) "data/DoNotCheckIn.svg") (set puppet-image (nsvgParseFromFile filename "px" 96.0f)) (unless puppet-image (vpslog "Failed to load SVG %s\n" filename) @@ -234,10 +244,14 @@ ;; current-key-states is owned by SDL, but we own last-frame-states (defer (dynarray-free (field s-key-states last-frame-states))) + (var selected-shape (addr NSVGshape) null) + (var selected-shape-bounds (array 4 int) (array 0)) + (var enable-fullscreen bool false) (var view-selection bool false) (var view-atlas bool false) - (var view-svg bool true) + (var view-svg bool false) + (var view-puppet bool true) (var exit-reason (addr (const char)) null) (while true @@ -263,6 +277,8 @@ (set view-atlas (not view-atlas))) (when (keybind-tapped (addr s-view-svg-keybind) (addr s-key-states)) (set view-svg (not view-svg))) + (when (keybind-tapped (addr s-view-puppet-keybind) (addr s-key-states)) + (set view-puppet (not view-puppet))) (var true-window-width int 0) (var true-window-height int 0) @@ -324,6 +340,8 @@ (sdl-print-error) (set exit-reason "Failed to render atlas texture"))) + ;; Start over selection to always ensure we pick the smallest shape + (set selected-shape null) (when view-selection ;; Shape selection and debug viewing (var hovered-shape-id-buffer (array 1024 char) (array 0)) (var hovered-write-head (addr char) hovered-shape-id-buffer) @@ -381,6 +399,10 @@ (>= mouse-y (at 1 sized-bounds)) (< mouse-x (at 2 sized-bounds)) (< mouse-y (at 3 sized-bounds))) + (when (or (not selected-shape) + (is-shape-smaller sized-bounds selected-shape-bounds)) + (set selected-shape shape) + (memcpy selected-shape-bounds sized-bounds (sizeof selected-shape-bounds))) (SDL_SetRenderDrawColor renderer 10 230 10 255) (set hovered-write-head (+ hovered-write-head @@ -397,7 +419,7 @@ (/ true-window-width 9) (- true-window-height 10) hovered-shape-id-buffer)) - (when (and view-atlas packed-rectangles) + (when packed-rectangles (var current-rectangle (addr stbrp_rect) packed-rectangles) (each-shape-in-svg-image puppet-image shape (unless (bit-and (path shape > flags) NSVG_FLAGS_VISIBLE) @@ -407,16 +429,41 @@ (path current-rectangle > y) (path current-rectangle > w) (path current-rectangle > h))) - (SDL_SetRenderDrawColor renderer 100 100 100 255) - (scope ;; Color on hover - (var sized-bounds (array 4 int) (array 0)) - (nanosvg-get-shape-bounds-with-stroke-int shape sized-bounds) - (when (and (>= mouse-x (at 0 sized-bounds)) - (>= mouse-y (at 1 sized-bounds)) - (< mouse-x (at 2 sized-bounds)) - (< mouse-y (at 3 sized-bounds))) - (SDL_SetRenderDrawColor renderer 10 10 230 255))) - (SDL_RenderDrawRect renderer (addr draw-rectangle)) + (when view-atlas + (SDL_SetRenderDrawColor renderer 100 100 100 255) + (scope ;; Color on hover + (var sized-bounds (array 4 int) (array 0)) + (nanosvg-get-shape-bounds-with-stroke-int shape sized-bounds) + (when (and (>= mouse-x (at 0 sized-bounds)) + (>= mouse-y (at 1 sized-bounds)) + (< mouse-x (at 2 sized-bounds)) + (< mouse-y (at 3 sized-bounds))) + (SDL_SetRenderDrawColor renderer 10 10 230 255))) + (SDL_RenderDrawRect renderer (addr draw-rectangle))) + + (when view-puppet + ;; We are ever so slightly off + (var sized-bounds (array 4 float) (array 0)) + (nanosvg-get-shape-bounds-with-stroke-float shape sized-bounds) + (var to-rectangle SDL_Rect + (array (type-cast (at 0 sized-bounds) int) + (type-cast (at 1 sized-bounds) int) + (path current-rectangle > w) + (path current-rectangle > h))) + (var-static angle float -2.5f) + (var-static angle-add float 0.001f) + (set angle (+ angle angle-add)) + (when (or (> angle 2.5f) + (< angle -2.5f)) + (set angle-add (negate angle-add))) + (var rotate-center SDL_Point (array 0 0)) + (unless (= 0 (SDL_RenderCopyEx renderer puppet-atlas-texture + (addr draw-rectangle) (addr to-rectangle) + (? (= shape selected-shape) angle 0.f) + (addr rotate-center) + SDL_FLIP_NONE)) + (sdl-print-error) + (set exit-reason "Failed to render atlas texture"))) (incr current-rectangle))) (when true ;; Debug position