92 lines
3.2 KiB
Fennel
92 lines
3.2 KiB
Fennel
(local GraphicsEditView (require :editor.gfxedit))
|
|
(local style (require :core.style))
|
|
(local tiles (require :game.tiles))
|
|
(local tiledraw (require :editor.tiledraw))
|
|
(local util (require :lib.util))
|
|
(local {: mouse-inside : activate : active? : checkbox} (util.require :editor.imstate))
|
|
|
|
(local TileView (GraphicsEditView:extend))
|
|
|
|
(local pixel-size 24)
|
|
|
|
(fn map-bitxy [x y]
|
|
(when (and (>= x 0) (< x 16) (>= y 0) (< y 16))
|
|
(local ibyte (if (< x 8) y (+ y 16)))
|
|
(local ibit
|
|
(if (= x 0) 7
|
|
(< x 8) (- x 1)
|
|
(- x 8)))
|
|
(values ibyte ibit)))
|
|
|
|
(fn get-byte [tile ibyte]
|
|
(: (tile:sub (+ ibyte 1) (+ ibyte 1)) :byte))
|
|
(fn get-bit [tile ibyte ibit]
|
|
(not= 0 (bit.band (get-byte tile ibyte) (bit.lshift 1 ibit))))
|
|
(fn set-bit [tile ibyte ibit is-set]
|
|
(local orval (bit.lshift 1 ibit))
|
|
(-> (get-byte tile ibyte)
|
|
(bit.band (bit.bnot orval))
|
|
(bit.bor (if is-set orval 0))))
|
|
|
|
(fn set-tile-bit [tile ibyte ibit is-set]
|
|
(..
|
|
(tile:sub 1 ibyte)
|
|
(string.char (set-bit tile ibyte ibit is-set))
|
|
(tile:sub (+ ibyte 2))))
|
|
|
|
(fn draw-bit-color [bit x y]
|
|
(local (bgcolor color) (tiledraw.pal-from-bit bit))
|
|
(renderer.draw_rect x y pixel-size pixel-size bgcolor)
|
|
(renderer.draw_rect (+ x 3) (+ y 3) (- pixel-size 6) (- pixel-size 6) color))
|
|
|
|
(fn draw-bit [bit x y even]
|
|
(renderer.draw_rect x y pixel-size pixel-size (if bit [255 255 255] [0 0 0])))
|
|
|
|
(fn TileView.tile [self]
|
|
(or (-?> self.tilecache.tiles (. self.itile) (. :gfx)) (string.rep "\0" 32)))
|
|
|
|
(fn TileView.draw-tile-editor [self tile x y]
|
|
(when (not (active? self :tile))
|
|
(set self.bit nil))
|
|
(local editor-size (* (+ pixel-size 1) 16))
|
|
(activate self :tile x y editor-size editor-size)
|
|
(for [bitx 0 15] (for [bity 0 15]
|
|
(local (ibyte ibit) (map-bitxy bitx bity))
|
|
(local bit (get-bit tile ibyte ibit))
|
|
(local (px py) (values (+ x (* bitx (+ pixel-size 1))) (+ y (* bity (+ pixel-size 1)))))
|
|
(if (or (= bitx 0) (= bitx 15))
|
|
(draw-bit-color bit px py)
|
|
(draw-bit bit px py (= (% bitx 2) 1)))
|
|
(when (and (active? self :tile) (mouse-inside px py pixel-size pixel-size))
|
|
(when (= self.bit nil) (set self.bit (not bit)))
|
|
(when (not= self.bit bit)
|
|
(self:update-tile (set-tile-bit tile ibyte ibit self.bit))))))
|
|
(love.graphics.setColor 1 1 1 1)
|
|
editor-size)
|
|
|
|
(fn TileView.draw-tile-flag [self flagname x y]
|
|
(local flags (-?> self.tilecache.tiles (. self.itile) (. :flags)))
|
|
(local flagset (if flags (. flags flagname) false))
|
|
(when (checkbox self flagname flagset x y)
|
|
(tset flags flagname (if flagset nil true))))
|
|
|
|
(fn TileView.draw-tile-flags [self x y]
|
|
(each [iflag flagname (ipairs tiles.flags)]
|
|
(self:draw-tile-flag flagname x (+ y (* (- iflag 1) (+ pixel-size 4))))))
|
|
|
|
(fn TileView.update-tile [self newtile]
|
|
(self.tilecache:update-tile self.itile newtile))
|
|
|
|
(fn TileView.save [self] (tiles.savetiles self.tilecache.tiles))
|
|
|
|
(fn TileView.draw [self]
|
|
(self:draw_background style.background)
|
|
(local (x y) (values (+ self.position.x 10) (+ self.position.y 10)))
|
|
(local editor-size (self:draw-tile-editor (self:tile) x y))
|
|
(self:draw-tile-flags (+ x editor-size pixel-size) y)
|
|
(self:draw-tile-selector x (+ y (* 18 (+ pixel-size 1))) (- self.size.x 20)))
|
|
|
|
(fn TileView.get_name [self] "Tile Editor")
|
|
|
|
TileView
|