Drag LMB to select tiles, draw bottom bar, wip file picker
This commit is contained in:
parent
2aeda5c833
commit
6f3948d62e
@ -287,7 +287,6 @@
|
|||||||
(reset! e/game-state (init-game {}))
|
(reset! e/game-state (init-game {}))
|
||||||
(swap! e/game-state :assoc {})
|
(swap! e/game-state :assoc {})
|
||||||
|
|
||||||
hello
|
|
||||||
(:tilemaps @e/game-state)
|
(:tilemaps @e/game-state)
|
||||||
|
|
||||||
:-)
|
:-)
|
||||||
|
|||||||
@ -5,32 +5,56 @@
|
|||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[engine :as e]
|
[engine :as e]
|
||||||
[rl :as rl]
|
[rl :as rl]
|
||||||
[watch :as w]))
|
[watch :as w])
|
||||||
|
(:import (java.awt FileDialog Frame)))
|
||||||
|
|
||||||
(def ^:const screen-width 1280)
|
(def screen-width 1280)
|
||||||
(def ^:const screen-height 630)
|
(def screen-height 630)
|
||||||
|
;; (def screen-width 960)
|
||||||
|
;; (def screen-height 490)
|
||||||
|
(def cell-size 16)
|
||||||
|
|
||||||
(defn sprite-atlas-path []
|
(defn sprite-atlas-path []
|
||||||
(let [asset-path "./source-assets/Sprout Lands Premium/"
|
(let [asset-path "./source-assets/Sprout Lands Premium/"
|
||||||
level-deco "Objects/Mushrooms, Flowers, Stones.png"]
|
level-deco "Objects/Mushrooms, Flowers, Stones.png"]
|
||||||
(str asset-path level-deco)))
|
(str asset-path level-deco)))
|
||||||
|
|
||||||
(defn init-sprite-atlas [config]
|
(defn zoom-to-fit [{:keys [width height]}]
|
||||||
{:zoom 1
|
(min (/ screen-width (double width))
|
||||||
:atlas (e/load-texture-once :main-atlas (sprite-atlas-path))
|
(/ screen-height (double height))))
|
||||||
:atlas-pos [0 0]
|
|
||||||
:drag-delta [0 0]})
|
|
||||||
|
|
||||||
(defn- auto-tile [game cell-size cell-count]
|
(defn init-sprite-atlas [config]
|
||||||
(assoc game :auto-tile-info {:cell-size 16 :cell-count 11}))
|
(let [atlas (e/load-texture-once :main-atlas (sprite-atlas-path))]
|
||||||
|
{:zoom (zoom-to-fit atlas)
|
||||||
|
:selected-cells #{}
|
||||||
|
:atlas atlas
|
||||||
|
:atlas-pos [0 0]
|
||||||
|
:drag-delta [0 0]}))
|
||||||
|
|
||||||
(defn handle-input [_ game]
|
(defn handle-input [_ game]
|
||||||
(update-as-> [g game]
|
(update-as-> [g game]
|
||||||
(when (rl/key-pressed? rl/key-r)
|
(when (rl/key-pressed? rl/key-r)
|
||||||
(assoc g :atlas-pos [0 0]))
|
(assoc g :atlas-pos [0 0]))
|
||||||
|
;; TODO: We want to add auto sprite sheet gen, so first detect non-empty, and we populate with that
|
||||||
|
;; then we have a selection box that allows grouping
|
||||||
|
(when (rl/key-pressed? rl/key-enter)
|
||||||
|
(let [d (doto (FileDialog. (Frame.) "Open" FileDialog/LOAD)
|
||||||
|
;; TODO: Maybe add a check if the directory exists?
|
||||||
|
(.setDirectory (str (System/getProperty "user.dir") "/assets"))
|
||||||
|
(.setVisible true))]
|
||||||
|
(when-let [f (.getFile d)]
|
||||||
|
(println (str (.getDirectory d) f)))
|
||||||
|
game)
|
||||||
|
#_(this should be to write the sprite sheet to some path))
|
||||||
|
(when (rl/key-pressed? rl/key-zero)
|
||||||
|
(assoc g :zoom (zoom-to-fit (:atlas g))))
|
||||||
|
|
||||||
(when (rl/mouse-button-pressed? rl/mouse-button-left)
|
(when (rl/mouse-button-pressed? rl/mouse-button-left)
|
||||||
(assoc g :drag-start-pos (rl/get-mouse-position) :drag-mode :zooming))
|
(assoc g
|
||||||
|
:drag-start-pos (rl/get-mouse-position)
|
||||||
|
:current-mouse-pos (rl/get-mouse-position)
|
||||||
|
:drag-mode :selecting
|
||||||
|
:selected-cells #{}))
|
||||||
(when (rl/mouse-button-released? rl/mouse-button-left)
|
(when (rl/mouse-button-released? rl/mouse-button-left)
|
||||||
(-> g
|
(-> g
|
||||||
(assoc :selected-sprite (get-selection-rect (:drag-start-pos g) (:current-mouse-pos g)))
|
(assoc :selected-sprite (get-selection-rect (:drag-start-pos g) (:current-mouse-pos g)))
|
||||||
@ -64,41 +88,70 @@
|
|||||||
(handle-input {} g)
|
(handle-input {} g)
|
||||||
(when (contains? g :drag-start-pos)
|
(when (contains? g :drag-start-pos)
|
||||||
(case (:drag-mode g)
|
(case (:drag-mode g)
|
||||||
:zooming (assoc g :current-mouse-pos (rl/get-mouse-position))
|
:selecting (assoc g
|
||||||
|
:current-mouse-pos (rl/get-mouse-position)
|
||||||
|
;; TODO: I have to fix this algorith, I'm pretty sure there's a way to make it O(1)
|
||||||
|
:selected-cells
|
||||||
|
(let [zoom (:zoom g)
|
||||||
|
{:keys [drag-start-pos current-mouse-pos atlas-pos]} g
|
||||||
|
{dsp-x :x dsp-y :y} drag-start-pos
|
||||||
|
{mx :x my :y} current-mouse-pos
|
||||||
|
[ax ay] atlas-pos
|
||||||
|
x0 (- (min dsp-x mx) ax)
|
||||||
|
y0 (- (min dsp-y my) ay)
|
||||||
|
x1 (- (max dsp-x mx) ax)
|
||||||
|
y1 (- (max dsp-y my) ay)
|
||||||
|
curr-cell-size (* cell-size zoom)
|
||||||
|
start-row (quot y0 curr-cell-size)
|
||||||
|
start-col (quot x0 curr-cell-size)
|
||||||
|
start-x (* start-col curr-cell-size)
|
||||||
|
start-y (* start-row curr-cell-size)
|
||||||
|
w (+ (- x0 start-x) (- x1 x0))
|
||||||
|
h (+ (- y0 start-y) (- y1 y0))
|
||||||
|
row-count (inc (quot h curr-cell-size))
|
||||||
|
col-count (inc (quot w curr-cell-size))]
|
||||||
|
(w/spy :info [row-count col-count])
|
||||||
|
(when (> (* w h) 256)
|
||||||
|
(set (for [r (range row-count)
|
||||||
|
c (range col-count)]
|
||||||
|
[(+ start-row r) (+ start-col c)])))))
|
||||||
|
|
||||||
:dragging (assoc g :drag-delta (let [{mx :x my :y} (rl/get-mouse-position)
|
:dragging (assoc g :drag-delta (let [{mx :x my :y} (rl/get-mouse-position)
|
||||||
{:keys [x y]} (:drag-start-pos g)]
|
{:keys [x y]} (:drag-start-pos g)]
|
||||||
[(- mx x) (- my y)]))))))
|
[(- mx x) (- my y)]))))))
|
||||||
(swap! e/game-state assoc :atlas-pos [0 0])
|
|
||||||
|
|
||||||
(defn draw-sprite-atlas [_ {:keys [atlas atlas-pos drag-delta zoom drag-start current-mouse-pos selected-sprite auto-tile-info]}]
|
(defn draw-sprite-atlas [_ {:keys [atlas atlas-pos drag-delta zoom drag-mode drag-start-pos current-mouse-pos selected-cells]}]
|
||||||
(rl/clear-background!* rl/black)
|
(rl/clear-background!* rl/black)
|
||||||
(let [current-scale (* (/ screen-height (:height atlas)) (max 0.1 (* zoom 0.1)))
|
(let [[ax ay] atlas-pos
|
||||||
[ax ay] atlas-pos
|
|
||||||
[dx dy] (or drag-delta [0 0])
|
[dx dy] (or drag-delta [0 0])
|
||||||
ax (+ ax dx)
|
ax (+ ax dx)
|
||||||
ay (+ ay dy)]
|
ay (+ ay dy)
|
||||||
|
curr-cell-size (* cell-size zoom)]
|
||||||
(when atlas
|
(when atlas
|
||||||
(let []
|
(let []
|
||||||
(rl/draw-texture-ex!* (:seg atlas) (rl/vec2-seg ax ay) 0.0 current-scale rl/white))
|
(rl/draw-texture-ex!* (:seg atlas) (rl/vec2-seg ax ay) 0.0 zoom rl/white))
|
||||||
(when drag-start
|
(when (= :selecting drag-mode)
|
||||||
(let [start-x (:x drag-start)
|
(let [start-x (:x drag-start-pos)
|
||||||
start-y (:y drag-start)
|
start-y (:y drag-start-pos)
|
||||||
end-x (:x current-mouse-pos)
|
end-x (:x current-mouse-pos)
|
||||||
end-y (:y 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)))
|
(rl/draw-rectangle-lines!* start-x start-y (- end-x start-x) (- end-y start-y) rl/white)))
|
||||||
#_(when selected-sprite
|
#_(when selected-sprite
|
||||||
(let [{:keys [x y w h]} 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-texture-rec!* (:seg atlas) (rl/rect-seg x y w h) (rl/vec2-seg 30 30) rl/white)))
|
||||||
(let [cell-size 16
|
(let [rows (quot (:height atlas) cell-size)
|
||||||
rows 11
|
cols (quot (:width atlas) cell-size)]
|
||||||
cols 12]
|
|
||||||
(doseq [row (range rows)
|
(doseq [row (range rows)
|
||||||
col (range cols)]
|
col (range cols)]
|
||||||
(rl/draw-rectangle-lines!* (+ ax (* col cell-size current-scale))
|
(rl/draw-rectangle-lines!* (+ ax (* col curr-cell-size))
|
||||||
(+ ay (* row cell-size current-scale))
|
(+ ay (* row curr-cell-size))
|
||||||
(* cell-size current-scale)
|
curr-cell-size
|
||||||
(* cell-size current-scale)
|
curr-cell-size
|
||||||
rl/gray)))))
|
rl/gray)))
|
||||||
|
(doseq [[row col] selected-cells]
|
||||||
|
(rl/draw-rectangle-lines!* (+ ax (* col curr-cell-size)) (+ ay (* row curr-cell-size))
|
||||||
|
(* cell-size zoom) (* cell-size zoom)
|
||||||
|
rl/white))))
|
||||||
(let [font-size 28
|
(let [font-size 28
|
||||||
width (quot screen-width 2)
|
width (quot screen-width 2)
|
||||||
height 30
|
height 30
|
||||||
@ -110,9 +163,11 @@
|
|||||||
(rl/draw-text!* (format "Y: %d" (int my)) (+ x 150) y font-size rl/black)))
|
(rl/draw-text!* (format "Y: %d" (int my)) (+ x 150) y font-size rl/black)))
|
||||||
|
|
||||||
(defn watch-sprite-atlas [config game]
|
(defn watch-sprite-atlas [config game]
|
||||||
{:zoom (:zoom game)
|
{#_#_:zoom (:zoom game)
|
||||||
|
:selected-cells (:selected-cells game)
|
||||||
|
:mouse-pos (:current-mouse-pos game)
|
||||||
:atlas-pos (:atlas-pos game)
|
:atlas-pos (:atlas-pos game)
|
||||||
:drag-delta (:drag-delta game)})
|
#_#_:drag-delta (:drag-delta game)})
|
||||||
|
|
||||||
(defn unload-sprite-atlas [game]
|
(defn unload-sprite-atlas [game]
|
||||||
(rl/unload-texture! (:atlas game)))
|
(rl/unload-texture! (:atlas game)))
|
||||||
@ -132,4 +187,5 @@
|
|||||||
(future (-main))
|
(future (-main))
|
||||||
(reset! e/game-state (init-sprite-atlas {}))
|
(reset! e/game-state (init-sprite-atlas {}))
|
||||||
(rl/set-clipboard-text!* "foo bar")
|
(rl/set-clipboard-text!* "foo bar")
|
||||||
|
(swap! e/game-state assoc :atlas-pos [0 0])
|
||||||
:-)
|
:-)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user