honeylisp/editor/gfxedit.fnl
2021-12-26 14:04:34 -06:00

65 lines
2.4 KiB
Fennel

(local View (require :core.view))
(local tiles (require :game.tiles))
(local tiledraw (require :editor.tiledraw))
(local util (require :lib.util))
(local files (require :game.files))
(local {: attach-imstate : button : reform : horiz-wrapper : group-wrapper} (util.require :editor.imgui))
(local GraphicsEditView (View:extend))
(local sprite-scale 4)
(set GraphicsEditView.sprite-scale sprite-scale)
(fn GraphicsEditView.new [self]
(GraphicsEditView.super.new self)
(self:set-style (self:initial-style))
(set self.scrollheight math.huge)
(set self.scrollable true)
(attach-imstate self))
(fn GraphicsEditView.get_scrollable_size [self] self.scrollheight)
(fn GraphicsEditView.initial-style [self] :tiles)
(fn GraphicsEditView.tilesize [self]
(let [style (tiles.style self.style)]
(values (or style.editw style.tilew) (or style.edith style.tileh))))
(fn GraphicsEditView.set-style [self key]
(set self.style key)
(set self.tilecache (files.cache key))
(set self.itile 1))
(fn GraphicsEditView.reload [self] (files.reload))
(fn GraphicsEditView.save [self] (files.save))
(fn GraphicsEditView.select-rel [self ditile]
(when self.itile
(local itile (+ self.itile ditile))
(when (>= itile 1) (set self.itile itile))))
(fn GraphicsEditView.draw-sprite [self x y itile ?key]
(let [sprite (self.tilecache:sprite itile ?key)]
(when sprite
(love.graphics.setColor 1 1 1)
(love.graphics.draw sprite x y 0 self.sprite-scale self.sprite-scale)
(values (* (sprite:getWidth) self.sprite-scale) (* (sprite:getHeight) self.sprite-scale)))))
(fn tile-selector [{: view &as form} selected-itile ?key]
(var selected-itile selected-itile)
(let [g (group-wrapper form)
wrap (horiz-wrapper form)]
(for [itile 1 (length view.tilecache.tiles)]
(let [{: x : y} form
(w h) (view:draw-sprite x y itile ?key)]
(when (and w h)
(when (= itile selected-itile)
(love.graphics.rectangle :line (- x 2) (- y 2) (+ w 4) (+ h 4)))
(when (g button (reform form {:tag [:tile itile] : w : h}))
(set selected-itile itile))
(wrap form))))
(g)
selected-itile))
(fn GraphicsEditView.draw-tile-selector [self form ?key]
(match (tile-selector (reform form {:scale self.sprite-scale :w form.w}) (when (= self.tilekey ?key) self.itile) ?key)
selected-itile (do (set self.itile selected-itile)
(set self.tilekey ?key))))
GraphicsEditView