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))
|
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))
|
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.tilesize [self] (values 16 16))
|
2021-06-26 02:30:15 +00:00
|
|
|
(fn TileView.tilekeys [self]
|
|
|
|
(if files.game.tilesets (icollect [_ key (pairs files.game.tilesets)] key)
|
|
|
|
[:gfx]))
|
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))
|
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
|
|
|
|
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
|
|
|
|
2021-08-25 01:24:06 +00:00
|
|
|
(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)))
|
|
|
|
|
2020-10-18 14:53:43 +00:00
|
|
|
(fn TileView.draw-tile-editor [self tile x y]
|
2021-08-25 01:24:06 +00:00
|
|
|
(when (not (active? self :tile)) (self:draw-off))
|
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)]
|
2021-08-25 01:24:06 +00:00
|
|
|
(local (ibyte ibit mask) (self:map-bitxy bitx bity))
|
|
|
|
(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)
|
2020-10-18 04:03:00 +00:00
|
|
|
(when (and (active? self :tile) (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))))))
|
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))
|
2021-07-04 02:13:04 +00:00
|
|
|
(let [(checked yNew) (checkbox self flagname flagset x y)]
|
|
|
|
(when checked (tset flags flagname (if flagset nil true)))
|
|
|
|
yNew))
|
2020-10-29 02:59:47 +00:00
|
|
|
|
|
|
|
(fn TileView.draw-tile-flags [self x y]
|
2020-10-30 16:55:47 +00:00
|
|
|
(local tile (-?> self.tilecache.tiles (. self.itile)))
|
2021-07-04 02:13:04 +00:00
|
|
|
(var y y)
|
2020-11-15 02:55:50 +00:00
|
|
|
(when tile
|
2021-07-04 02:13:04 +00:00
|
|
|
(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))))
|
2021-06-26 01:55:15 +00:00
|
|
|
(each [iflag flagname (ipairs (tiles.flags))]
|
2021-07-04 02:13:04 +00:00
|
|
|
(set y (self:draw-tile-flag flagname x (+ y style.padding.y)))))
|
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-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)
|
|
|
|
(local (x y) (values (+ self.position.x style.padding.x (- self.scroll.x))
|
|
|
|
(+ self.position.y style.padding.y (- self.scroll.y))))
|
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))
|
2021-04-19 02:26:18 +00:00
|
|
|
(set selector-y (+ selector-y selector-h pixel-size)))
|
|
|
|
(set self.scrollheight (- selector-y y)))
|
2020-10-18 14:53:43 +00:00
|
|
|
|
2021-04-24 02:54:28 +00:00
|
|
|
(fn TileView.resource-key [self] :tiles)
|
2020-10-12 15:48:14 +00:00
|
|
|
(fn TileView.get_name [self] "Tile Editor")
|
|
|
|
|
|
|
|
TileView
|