2020-10-19 00:13:26 +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))
|
|
|
|
(local tiledraw (require :editor.tiledraw))
|
2020-10-18 03:52:41 +00:00
|
|
|
(local util (require :lib.util))
|
2020-10-30 16:55:47 +00:00
|
|
|
(local {: mouse-inside : activate : active? : checkbox : textfield} (util.require :editor.imstate))
|
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
|
|
|
|
2020-11-22 03:50:11 +00:00
|
|
|
(fn TileView.map-bitxy [self x y]
|
2020-10-12 15:48:14 +00:00
|
|
|
(when (and (>= x 0) (< x 16) (>= y 0) (< y 16))
|
2020-10-19 00:13:26 +00:00
|
|
|
(local ibyte (if (< x 8) y (+ y 16)))
|
|
|
|
(local ibit
|
|
|
|
(if (= x 0) 7
|
|
|
|
(< x 8) (- x 1)
|
|
|
|
(- x 8)))
|
|
|
|
(values ibyte ibit)))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2020-11-22 03:50:11 +00:00
|
|
|
(fn TileView.tilesize [self] (values 16 16))
|
2020-12-30 17:47:07 +00:00
|
|
|
(fn TileView.tilekeys [self] [:gfx :neut])
|
2020-11-22 03:50:11 +00:00
|
|
|
|
2020-10-12 15:48:14 +00:00
|
|
|
(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]
|
2021-01-02 19:47:57 +00:00
|
|
|
(util.splice tile ibyte (string.char (set-bit tile ibyte ibit is-set))))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
|
|
|
(fn draw-bit-color [bit x y]
|
2020-10-15 03:40:01 +00:00
|
|
|
(local (bgcolor color) (tiledraw.pal-from-bit bit))
|
2020-10-12 15:48:14 +00:00
|
|
|
(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])))
|
|
|
|
|
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))
|
2020-12-24 03:17:33 +00:00
|
|
|
(or (-?> self.tilecache.tiles (. self.itile) (. (or self.tilekey :gfx))) (string.rep "\0" (/ (* w h) 8))))
|
2020-10-18 03:52:41 +00:00
|
|
|
|
2020-10-18 14:53:43 +00:00
|
|
|
(fn TileView.draw-tile-editor [self tile x y]
|
2020-10-18 03:52:41 +00:00
|
|
|
(when (not (active? self :tile))
|
|
|
|
(set self.bit nil))
|
2020-11-22 03:50:11 +00:00
|
|
|
(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))
|
2020-10-18 03:52:41 +00:00
|
|
|
(local bit (get-bit tile ibyte ibit))
|
|
|
|
(local (px py) (values (+ x (* bitx (+ pixel-size 1))) (+ y (* bity (+ pixel-size 1)))))
|
2020-11-22 03:50:11 +00:00
|
|
|
(if (= ibit 7)
|
2020-10-18 03:52:41 +00:00
|
|
|
(draw-bit-color bit px py)
|
|
|
|
(draw-bit bit px py (= (% bitx 2) 1)))
|
2020-10-18 04:03:00 +00:00
|
|
|
(when (and (active? self :tile) (mouse-inside px py pixel-size pixel-size))
|
2020-10-18 03:52:41 +00:00
|
|
|
(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))))))
|
2020-10-29 02:59:47 +00:00
|
|
|
(love.graphics.setColor 1 1 1 1)
|
2020-11-22 03:50:11 +00:00
|
|
|
(values editor-w editor-h))
|
2020-10-29 02:59:47 +00:00
|
|
|
|
|
|
|
(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]
|
2020-10-30 16:55:47 +00:00
|
|
|
(local tile (-?> self.tilecache.tiles (. self.itile)))
|
2020-11-15 02:55:50 +00:00
|
|
|
(when tile
|
|
|
|
(set tile.word (textfield self "Default word" tile.word x y 100 200))
|
|
|
|
(set tile.label (textfield self "Label" tile.label x (+ y pixel-size 4) 100 200)))
|
2020-10-29 02:59:47 +00:00
|
|
|
(each [iflag flagname (ipairs tiles.flags)]
|
2020-11-15 02:55:50 +00:00
|
|
|
(self:draw-tile-flag flagname x (+ y (* (+ iflag 1) (+ pixel-size 4))))))
|
2020-10-18 03:52:41 +00:00
|
|
|
|
2020-10-15 03:40:01 +00:00
|
|
|
(fn TileView.update-tile [self newtile]
|
2020-12-24 03:17:33 +00:00
|
|
|
(self.tilecache:update-tile self.itile newtile self.tilekey))
|
2020-10-15 03:40:01 +00:00
|
|
|
|
2020-11-23 00:44:56 +00:00
|
|
|
(fn TileView.save [self] (tiles.savegfx (self:filename) self.tilecache.tiles))
|
2020-10-12 15:48:14 +00:00
|
|
|
|
2020-12-30 17:47:07 +00:00
|
|
|
(fn TileView.draw-neut-tile-selector [self x y]
|
|
|
|
(self:draw-tile-selector x (- self.size.x 20) :neut))
|
|
|
|
|
2020-10-18 14:53:43 +00:00
|
|
|
(fn TileView.draw [self]
|
|
|
|
(self:draw_background style.background)
|
|
|
|
(local (x y) (values (+ self.position.x 10) (+ self.position.y 10)))
|
2020-11-22 03:50:11 +00:00
|
|
|
(local (editor-w editor-h) (self:draw-tile-editor (self:tile) x y))
|
|
|
|
(self:draw-tile-flags (+ x editor-w pixel-size) y)
|
2020-12-30 17:47:07 +00:00
|
|
|
(var selector-y (+ y editor-h pixel-size))
|
|
|
|
(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))))
|
2020-10-18 14:53:43 +00:00
|
|
|
|
2020-10-12 15:48:14 +00:00
|
|
|
(fn TileView.get_name [self] "Tile Editor")
|
|
|
|
|
|
|
|
TileView
|