platform-specific tile editor

This commit is contained in:
Jeremy Penner 2021-08-24 21:24:06 -04:00
parent b2d374622a
commit f54ebea6bc
3 changed files with 48 additions and 35 deletions

19
editor/tileedit/ii.fnl Normal file
View file

@ -0,0 +1,19 @@
(local tiledraw (require :editor.tiledraw))
{:map-bitxy (fn [self 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 1)))
:pixel-color (fn [self b _ ibit]
(if (= ibit 7) (tiledraw.pal-from-bit (= b 1))
(= b 1) [255 255 255]
[0 0 0]))
:draw-off (fn [self] (set self.bit nil))
:draw-on (fn [self b] (when (= self.bit nil) (set self.bit (if (= b 1) 0 1))))
:draw-bits (fn [self] self.bit)
}

View file

@ -2,7 +2,6 @@
(local style (require :core.style))
(local tiles (require :game.tiles))
(local files (require :game.files))
(local tiledraw (require :editor.tiledraw))
(local util (require :lib.util))
(local {: mouse-inside : activate : active? : checkbox : textfield} (util.require :editor.imstate))
@ -11,15 +10,6 @@
(set TileView.pixel-size 24)
(local pixel-size TileView.pixel-size)
(fn TileView.map-bitxy [self 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 TileView.tilesize [self] (values 16 16))
(fn TileView.tilekeys [self]
(if files.game.tilesets (icollect [_ key (pairs files.game.tilesets)] key)
@ -27,47 +17,46 @@
(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))
(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 (if is-set orval 0))))
(bit.bor (bit.lshift bits ibit))))
(fn set-tile-bit [tile ibyte ibit is-set]
(util.splice tile ibyte (string.char (set-bit tile ibyte ibit is-set))))
(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 set-tile-bits [tile ibyte ibit mask bits]
(util.splice tile ibyte (string.char (set-bits tile ibyte ibit mask bits))))
(fn TileView.tile [self]
(local (w h) (self:tilesize))
(or (-?> self.tilecache.tiles (. self.itile) (. (or self.tilekey :gfx))) (string.rep "\0" (/ (* w h) 8))))
(files.platform-methods TileView :editor.tileedit :map-bitxy :pixel-color :draw-on :draw-off :draw-bits)
(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))
(set self.bit nil))
(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) (self:map-bitxy bitx bity))
(local b (get-bit tile ibyte ibit))
(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)))))
(if (= ibit 7)
(draw-bit-color b px py)
(draw-bit b px py (= (% bitx 2) 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))
(when (= self.bit nil) (set self.bit (not b)))
(when (not= self.bit b)
(self:update-tile (set-tile-bit tile ibyte ibit self.bit))))))
(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))

View file

@ -86,6 +86,11 @@
(fn files.module []
(or files.game.module (: (filename) :match "^[^/]+")))
(fn files.platform [] (or files.game.platform :ii))
(fn files.platform-methods [cls module-prefix ...]
(each [_ key (ipairs [...])]
(tset cls key (fn [...] ((. (require (.. module-prefix :. (files.platform))) key) ...)))))
(when (= files.game nil)
(files.load))