2021-12-26 20:04:34 +00:00
|
|
|
(local GraphicsEditView (require :editor.gfxedit))
|
2020-10-12 15:48:14 +00:00
|
|
|
(local style (require :core.style))
|
2020-10-15 03:40:01 +00:00
|
|
|
(local tiles (require :game.tiles))
|
2021-06-26 02:30:15 +00:00
|
|
|
(local files (require :game.files))
|
2020-10-18 03:52:41 +00:00
|
|
|
(local util (require :lib.util))
|
2021-12-21 01:40:18 +00:00
|
|
|
(local lume (require :lib.lume))
|
2021-12-24 04:36:20 +00:00
|
|
|
(local {: show} (util.require :inspector.debug))
|
|
|
|
(local {: mouse-inside : activate : active? : checkbox : textfield : button : dropdown : with-style
|
|
|
|
: under : right-of : reform : horiz-wrapper : group-wrapper} (util.require :editor.imgui))
|
2020-10-19 00:13:26 +00:00
|
|
|
|
|
|
|
(local TileView (GraphicsEditView:extend))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2020-11-22 03:50:11 +00:00
|
|
|
(set TileView.pixel-size 24)
|
|
|
|
(local pixel-size TileView.pixel-size)
|
2020-10-18 03:52:41 +00:00
|
|
|
|
2021-06-26 02:30:15 +00:00
|
|
|
(fn TileView.tilekeys [self]
|
|
|
|
(if files.game.tilesets (icollect [_ key (pairs files.game.tilesets)] key)
|
|
|
|
[:gfx]))
|
2021-09-06 03:29:16 +00:00
|
|
|
(fn TileView.tilebytelen [self] (let [(w h) (self:tilesize)] (/ (* w h) (self:pixel-storage-divisor))))
|
2020-11-22 03:50:11 +00:00
|
|
|
|
2020-10-12 15:48:14 +00:00
|
|
|
(fn get-byte [tile ibyte]
|
2021-08-29 02:04:54 +00:00
|
|
|
(or (: (tile:sub (+ ibyte 1) (+ ibyte 1)) :byte) 0))
|
2021-08-25 01:24:06 +00:00
|
|
|
(fn get-bits [tile ibyte ibit mask]
|
|
|
|
(-> (get-byte tile ibyte)
|
|
|
|
(bit.band (bit.lshift mask ibit))
|
|
|
|
(bit.rshift ibit)))
|
|
|
|
(fn set-bits [tile ibyte ibit mask bits]
|
|
|
|
(local orval (bit.lshift mask ibit))
|
2020-10-12 15:48:14 +00:00
|
|
|
(-> (get-byte tile ibyte)
|
|
|
|
(bit.band (bit.bnot orval))
|
2021-08-25 01:24:06 +00:00
|
|
|
(bit.bor (bit.lshift bits ibit))))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2021-08-25 01:24:06 +00:00
|
|
|
(fn set-tile-bits [tile ibyte ibit mask bits]
|
|
|
|
(util.splice tile ibyte (string.char (set-bits tile ibyte ibit mask bits))))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2021-08-29 02:04:54 +00:00
|
|
|
(files.platform-methods TileView :editor.tileedit :map-bitxy :pixel-color :draw-on :draw-off :draw-bits
|
|
|
|
:palette :pixel-storage-divisor)
|
|
|
|
|
2021-09-14 03:16:03 +00:00
|
|
|
(files.default-platform-method TileView :editor.tileedit :preview-locations
|
|
|
|
(fn [self] (let [(w h) (self:tilesize)] [[0 0] [w 0] [0 h] [w h]])))
|
|
|
|
|
2021-11-14 19:55:41 +00:00
|
|
|
(files.default-platform-method TileView :editor.tileedit :blank-tile-byte #"\0")
|
|
|
|
|
2020-10-29 02:59:47 +00:00
|
|
|
(fn TileView.tile [self]
|
2020-11-22 03:50:11 +00:00
|
|
|
(local (w h) (self:tilesize))
|
2021-08-29 02:04:54 +00:00
|
|
|
(or (-?> self.tilecache.tiles (. self.itile) (. (or self.tilekey :gfx)))
|
2021-11-14 19:55:41 +00:00
|
|
|
(string.rep (self:blank-tile-byte) (/ (* w h) (self:pixel-storage-divisor)))))
|
2021-08-25 01:24:06 +00:00
|
|
|
|
|
|
|
(fn TileView.draw-pixel [self x y colorbg ?colorfg]
|
|
|
|
(renderer.draw_rect x y pixel-size pixel-size colorbg)
|
|
|
|
(when ?colorfg (renderer.draw_rect (+ x 3) (+ y 3) (- pixel-size 6) (- pixel-size 6) ?colorfg)))
|
|
|
|
|
2021-12-21 01:40:18 +00:00
|
|
|
(fn tile-editor [{:view self : x : y &as form} tile]
|
|
|
|
(local {: tag} (with-style form :tag :tile))
|
|
|
|
(when (not (active? self tag)) (self:draw-off))
|
2020-11-22 03:50:11 +00:00
|
|
|
(local (w h) (self:tilesize))
|
2021-12-21 01:40:18 +00:00
|
|
|
(set form.w (* (+ pixel-size 1) w))
|
|
|
|
(set form.h (* (+ pixel-size 1) h))
|
|
|
|
(activate form)
|
2020-11-22 03:50:11 +00:00
|
|
|
(for [bitx 0 (- w 1)] (for [bity 0 (- h 1)]
|
2021-11-14 19:55:41 +00:00
|
|
|
(local (ibyte ibit mask) (self:map-bitxy bitx bity w h))
|
2021-08-25 01:24:06 +00:00
|
|
|
(local b (get-bits tile ibyte ibit mask))
|
2020-10-18 03:52:41 +00:00
|
|
|
(local (px py) (values (+ x (* bitx (+ pixel-size 1))) (+ y (* bity (+ pixel-size 1)))))
|
2021-08-25 01:24:06 +00:00
|
|
|
(local (colorbg colorfg) (self:pixel-color b ibyte ibit))
|
|
|
|
(self:draw-pixel px py colorbg colorfg)
|
2021-12-21 01:40:18 +00:00
|
|
|
(when (and (active? self tag) (mouse-inside px py pixel-size pixel-size))
|
2021-08-25 01:24:06 +00:00
|
|
|
(self:draw-on b)
|
|
|
|
(local bits (self:draw-bits))
|
|
|
|
(when (not= bits b)
|
|
|
|
(self:update-tile (set-tile-bits tile ibyte ibit mask bits))))))
|
2021-12-21 01:40:18 +00:00
|
|
|
(love.graphics.setColor 1 1 1 1))
|
|
|
|
|
2021-12-24 04:36:20 +00:00
|
|
|
(fn TileView.draw-tile-editor [self form tile] (tile-editor form tile))
|
2021-12-21 01:40:18 +00:00
|
|
|
|
|
|
|
(fn tile-flag [form tile flagname]
|
|
|
|
(local flagset (?. tile :flags flagname))
|
|
|
|
(when (checkbox form flagname flagset)
|
|
|
|
(tset tile :flags flagname (if flagset nil true))))
|
|
|
|
|
|
|
|
(fn TileView.draw-tile-flags [self form]
|
2021-12-24 04:36:20 +00:00
|
|
|
(let [tile (-?> self.tilecache.tiles (. self.itile))
|
|
|
|
fieldform {:wlabel (* 100 SCALE) :wtext (* 200 SCALE)}]
|
2021-12-21 01:40:18 +00:00
|
|
|
(when tile
|
2021-12-24 04:36:20 +00:00
|
|
|
(set tile.word (textfield (reform form fieldform) "Default word" tile.word))
|
|
|
|
(set tile.label (textfield (under form fieldform) "Label" tile.label)))
|
2021-12-21 01:40:18 +00:00
|
|
|
(each [iflag flagname (ipairs (tiles.flags))]
|
2021-12-24 04:36:20 +00:00
|
|
|
(tile-flag (under form) tile flagname))))
|
2021-12-21 01:40:18 +00:00
|
|
|
|
|
|
|
(fn tile-preview [{:view self : x : y &as form} itile tilekey]
|
2021-09-14 03:16:03 +00:00
|
|
|
(each [_ [tx ty] (ipairs (self:preview-locations))]
|
2021-12-21 01:40:18 +00:00
|
|
|
(let [dx (* tx self.sprite-scale) dy (* ty self.sprite-scale)
|
|
|
|
(w h) (self:draw-sprite (+ x dx) (+ y dy) itile tilekey)]
|
|
|
|
(when (and w (or (= form.w nil) (< form.w (+ w dx)))) (set form.w (+ w dx)))
|
|
|
|
(when (and h (or (= form.h nil) (< form.h (+ h dy)))) (set form.h (+ h dy))))))
|
2021-12-24 04:36:20 +00:00
|
|
|
(fn TileView.draw-tile-preview [self form] (tile-preview form self.itile self.tilekey))
|
2021-12-21 01:40:18 +00:00
|
|
|
|
|
|
|
(fn tile-palette [{:view self : x : y : w &as form} pal selected-color]
|
2021-12-24 04:36:20 +00:00
|
|
|
(let [g (group-wrapper (with-style form))
|
|
|
|
wrap (horiz-wrapper form)]
|
2021-12-21 01:40:18 +00:00
|
|
|
(var selected-color selected-color)
|
|
|
|
(each [icolor color (ipairs pal)]
|
|
|
|
(renderer.draw_rect form.x form.y pixel-size pixel-size color)
|
|
|
|
(when (= icolor selected-color)
|
|
|
|
(love.graphics.setColor 1 1 1 1)
|
|
|
|
(love.graphics.rectangle :line (- form.x 2) (- form.y 2) (+ pixel-size 4) (+ pixel-size 4)))
|
2021-12-24 04:36:20 +00:00
|
|
|
(when (g button (reform form {:tag [:pal icolor] :w pixel-size :h pixel-size}))
|
|
|
|
(set selected-color icolor))
|
|
|
|
(wrap form))
|
|
|
|
(g)
|
2021-12-21 01:40:18 +00:00
|
|
|
selected-color))
|
|
|
|
|
|
|
|
(fn TileView.draw-tile-palette [self form]
|
|
|
|
(match (self:palette)
|
2021-12-24 04:36:20 +00:00
|
|
|
pal (set self.icolor (tile-palette form pal self.icolor))))
|
2021-08-29 02:04:54 +00:00
|
|
|
|
2020-10-15 03:40:01 +00:00
|
|
|
(fn TileView.update-tile [self newtile]
|
2021-11-14 19:55:41 +00:00
|
|
|
(self.tilecache:update-tile self.itile newtile self.tilekey))
|
|
|
|
|
2021-12-21 01:40:18 +00:00
|
|
|
(fn style-selector [form current-style]
|
|
|
|
(let [{:view self : x : y : font : color : ypad} (with-style form)
|
|
|
|
form-drop (lume.merge form {:x (+ x (* 50 SCALE)) :w (* 100 SCALE) :tag :layer-selector})
|
|
|
|
selection (dropdown form-drop current-style (tiles.tile-styles))]
|
|
|
|
(renderer.draw_text font "Style" x (+ y (/ ypad 2)) color)
|
|
|
|
(set form.w (- (+ form-drop.x form-drop.w) x))
|
|
|
|
(set form.h form-drop.h)
|
|
|
|
(when (not= current-style selection) selection)))
|
|
|
|
|
|
|
|
(fn TileView.draw-style-selector [self form]
|
2021-12-24 04:36:20 +00:00
|
|
|
(match (style-selector form self.style)
|
2021-12-21 01:40:18 +00:00
|
|
|
new-style (self:set-style new-style)))
|
2020-10-15 03:40:01 +00:00
|
|
|
|
2020-10-18 14:53:43 +00:00
|
|
|
(fn TileView.draw [self]
|
|
|
|
(self:draw_background style.background)
|
2021-04-19 02:26:18 +00:00
|
|
|
(self:draw_scrollbar)
|
2021-12-21 01:40:18 +00:00
|
|
|
(let [form (self:form)
|
2021-12-24 04:36:20 +00:00
|
|
|
form-side (self:form)
|
|
|
|
full-width {:w form.w}]
|
2021-12-21 01:40:18 +00:00
|
|
|
(self:draw-tile-editor form (self:tile))
|
|
|
|
|
|
|
|
; layout sidebar
|
2021-12-24 04:36:20 +00:00
|
|
|
(self:draw-tile-flags (right-of form {:into form-side}))
|
|
|
|
(self:draw-tile-preview (under form-side))
|
2021-12-21 01:40:18 +00:00
|
|
|
|
|
|
|
; continue laying out under tile editor
|
2021-12-24 04:36:20 +00:00
|
|
|
(self:draw-tile-palette (under form full-width))
|
2021-12-21 01:40:18 +00:00
|
|
|
(when (> (length (tiles.tile-styles)) 1)
|
2021-12-24 04:36:20 +00:00
|
|
|
(self:draw-style-selector (under form)))
|
2021-12-21 01:40:18 +00:00
|
|
|
(each [_ key (ipairs (self:tilekeys))]
|
2021-12-24 04:36:20 +00:00
|
|
|
(self:draw-tile-selector (under form full-width) key))
|
2021-12-21 01:40:18 +00:00
|
|
|
(self:end-scroll form)))
|
2020-10-18 14:53:43 +00:00
|
|
|
|
2021-11-14 19:55:41 +00:00
|
|
|
(fn TileView.initial-style [self] :tiles)
|
2020-10-12 15:48:14 +00:00
|
|
|
(fn TileView.get_name [self] "Tile Editor")
|
|
|
|
|
|
|
|
TileView
|