Drag to select a region of the image, then display it

This commit is contained in:
Joseph Ferano 2026-08-16 22:15:16 +07:00
parent 96bc3717ec
commit 93e8ea751f
2 changed files with 40 additions and 7 deletions

View File

@ -166,6 +166,8 @@
(def ^:const key-kp-equal 336)
(def ^:const mouse-button-left 0)
(def ^:const mouse-button-right 1)
(def ^:const mouse-button-middle 2)
(def ^:const log-warning 4)
(def ^:const pixelformat-r8g8b8a8 7)
(def ^:const texture-filter-point 0)
@ -189,6 +191,8 @@
(defcfn mouse-button-pressed? "IsMouseButtonPressed" [::mem/int] ::bool)
(defcfn mouse-button-released? "IsMouseButtonReleased" [::mem/int] ::bool)
(defcfn get-mouse-position "GetMousePosition" [] ::vector-2)
(defcfn get-mouse-wheel-move "GetMouseWheelMove" [] ::mem/float)
(defcfn get-mouse-wheel-move-v "GetMouseWheelMoveV" [] ::vector-2)
;; The !* forms take pre-serialized struct segments and allocate nothing per
;; call. Everything in a per-frame path uses these.

View File

@ -10,18 +10,47 @@
(let [path (:sprite-path @state)]
(if (.exists (io/file path))
{:atlas (rl/load-texture path)}
(println "Unable to load " path))))
{})))
(defn handle-sprite-atlas-input [config game]
game)
(defn handle-sprite-atlas-input [_ game]
(cond-> game
(rl/mouse-button-pressed? rl/mouse-button-left)
(assoc :drag-start (rl/get-mouse-position))
(defn update-sprite-atlas [config game]
game)
(rl/mouse-button-released? rl/mouse-button-left)
(assoc :event-drag-ended true)
(defn draw-sprite-atlas [config game]
:always
(as-> g (cond-> g
(contains? g :drag-start)
(assoc :current-mouse-pos (rl/get-mouse-position))))))
(defn- get-selection-rect [{ax :x ay :y} {bx :x by :y}]
(let [start-x (min ax bx)
start-y (min ay by)
end-x (max ax bx)
end-y (max ay by)]
{:x start-x :y start-y :w (- end-x start-x) :h (- end-y start-y)}))
(defn update-sprite-atlas [_ game]
(cond-> game
(contains? game :event-drag-ended)
(-> (assoc :selected-sprite (get-selection-rect (:drag-start game) (:current-mouse-pos game)))
(dissoc :event-drag-ended :drag-start :current-mouse-pos))))
(defn draw-sprite-atlas [_ {:keys [atlas drag-start current-mouse-pos selected-sprite]}]
(rl/clear-background!* rl/black)
(when-let [atlas (:atlas game)]
(when atlas
(rl/draw-texture!* (:seg atlas) 0 0 rl/white)
(when drag-start
(let [start-x (:x drag-start)
start-y (:y drag-start)
end-x (:x current-mouse-pos)
end-y (:y current-mouse-pos)]
(rl/draw-rectangle-lines!* start-x start-y (- end-x start-x) (- end-y start-y) rl/white)))
(when selected-sprite
(let [{:keys [x y w h]} selected-sprite]
(rl/draw-texture-rec!* (:seg atlas) (rl/rect-seg x y w h) (rl/vec2-seg 30 30) rl/white)))
(rl/draw-fps 10 10)))
(defn watch-sprite-atlas [config game]