honeylisp/editor/tileedit/init.fnl

131 lines
5.4 KiB
Fennel

(local GraphicsEditView (require :editor.gfxedit))
(local style (require :core.style))
(local tiles (require :game.tiles))
(local files (require :game.files))
(local util (require :lib.util))
(local {: mouse-inside : activate : active? : checkbox : textfield : button} (util.require :editor.imstate))
(local TileView (GraphicsEditView:extend))
(set TileView.pixel-size 24)
(local pixel-size TileView.pixel-size)
(fn TileView.tilesize [self] (values 16 16))
(fn TileView.tilekeys [self]
(if files.game.tilesets (icollect [_ key (pairs files.game.tilesets)] key)
[:gfx]))
(fn TileView.tilebytelen [self] (let [(w h) (self:tilesize)] (/ (* w h) (self:pixel-storage-divisor))))
(fn get-byte [tile ibyte]
(or (: (tile:sub (+ ibyte 1) (+ ibyte 1)) :byte) 0))
(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))
(-> (get-byte tile ibyte)
(bit.band (bit.bnot orval))
(bit.bor (bit.lshift bits ibit))))
(fn set-tile-bits [tile ibyte ibit mask bits]
(util.splice tile ibyte (string.char (set-bits tile ibyte ibit mask bits))))
(files.platform-methods TileView :editor.tileedit :map-bitxy :pixel-color :draw-on :draw-off :draw-bits
:palette :pixel-storage-divisor)
(files.default-platform-method TileView :editor.tileedit :preview-locations
(fn [self] (let [(w h) (self:tilesize)] [[0 0] [w 0] [0 h] [w h]])))
(fn TileView.tile [self]
(local (w h) (self:tilesize))
(or (-?> self.tilecache.tiles (. self.itile) (. (or self.tilekey :gfx)))
(string.rep "\0" (/ (* w h) (self:pixel-storage-divisor)))))
(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)))
(fn TileView.draw-tile-editor [self tile x y]
(when (not (active? self :tile)) (self:draw-off))
(local (w h) (self:tilesize))
(local editor-w (* (+ pixel-size 1) w))
(local editor-h (* (+ pixel-size 1) h))
(activate self :tile x y editor-w editor-h)
(for [bitx 0 (- w 1)] (for [bity 0 (- h 1)]
(local (ibyte ibit mask) (self:map-bitxy bitx bity))
(local b (get-bits tile ibyte ibit mask))
(local (px py) (values (+ x (* bitx (+ pixel-size 1))) (+ y (* bity (+ pixel-size 1)))))
(local (colorbg colorfg) (self:pixel-color b ibyte ibit))
(self:draw-pixel px py colorbg colorfg)
(when (and (active? self :tile) (mouse-inside px py pixel-size pixel-size))
(self:draw-on b)
(local bits (self:draw-bits))
(when (not= bits b)
(self:update-tile (set-tile-bits tile ibyte ibit mask bits))))))
(love.graphics.setColor 1 1 1 1)
(values editor-w editor-h))
(fn TileView.draw-tile-flag [self flagname x y]
(local flags (-?> self.tilecache.tiles (. self.itile) (. :flags)))
(local flagset (if flags (. flags flagname) false))
(let [(checked yNew) (checkbox self flagname flagset x y)]
(when checked (tset flags flagname (if flagset nil true)))
yNew))
(fn TileView.draw-tile-flags [self x y]
(local tile (-?> self.tilecache.tiles (. self.itile)))
(var y y)
(when tile
(set (tile.word y) (textfield self "Default word" tile.word x (+ y style.padding.y) (* 100 SCALE) (* 200 SCALE)))
(set (tile.label y) (textfield self "Label" tile.label x (+ y style.padding.y) (* 100 SCALE) (* 200 SCALE))))
(each [iflag flagname (ipairs (tiles.flags))]
(set y (self:draw-tile-flag flagname x (+ y style.padding.y))))
y)
(fn TileView.draw-tile-preview [self x y]
(each [_ [tx ty] (ipairs (self:preview-locations))]
(self:draw-sprite (+ x (* tx self.sprite-scale)) (+ y (* ty self.sprite-scale)) self.itile self.tilekey)))
(fn TileView.draw-tile-palette [self x y w]
(local pal (self:palette))
(if pal
(do (var cx x)
(var cy y)
(each [icolor color (ipairs pal)]
(when (>= cx w)
(set cx x)
(set cy (+ cy pixel-size style.padding.y)))
(when (button self [:pal icolor] cx cy pixel-size pixel-size)
(set self.icolor icolor))
(renderer.draw_rect cx cy pixel-size pixel-size color)
(when (= icolor self.icolor)
(love.graphics.setColor 1 1 1 1)
(love.graphics.rectangle :line (- cx 2) (- cy 2) (+ pixel-size 4) (+ pixel-size 4)))
(set cx (+ cx pixel-size style.padding.x)))
(+ pixel-size style.padding.y))
0))
(fn TileView.update-tile [self newtile]
(self.tilecache:update-tile self.itile newtile self.tilekey))
(fn TileView.draw [self]
(self:draw_background style.background)
(self:draw_scrollbar)
(local (x y) (values (+ self.position.x style.padding.x (- self.scroll.x))
(+ self.position.y style.padding.y (- self.scroll.y))))
(local (editor-w editor-h) (self:draw-tile-editor (self:tile) x y))
(local preview-y (self:draw-tile-flags (+ x editor-w pixel-size) y))
(self:draw-tile-preview (+ x editor-w pixel-size) (+ preview-y style.padding.y))
(var selector-y (+ y editor-h pixel-size))
(set selector-y (+ selector-y (self:draw-tile-palette x selector-y (- self.size.x 20))))
(each [_ key (ipairs (self:tilekeys))]
(local selector-h (self:draw-tile-selector x selector-y (- self.size.x 20) key))
(set selector-y (+ selector-y selector-h pixel-size)))
(set self.scrollheight (- selector-y y)))
(fn TileView.resource-key [self] :tiles)
(fn TileView.get_name [self] "Tile Editor")
TileView