honeylisp/editor/gfxedit2.fnl

70 lines
2.6 KiB
Plaintext
Raw Normal View History

2021-12-21 01:40:18 +00:00
(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 : vert} (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 : x : y : w : scale &as form} chosen-itile ?key]
(var tilex x)
(var tiley y)
(var selected-itile nil)
(var (pixw pixh) (view:tilesize))
(when (= files.game.platform :ii) (set pixw (* (/ pixw 8) 7)))
(local tilew (* scale pixw))
(local tileh (* scale pixh))
(for [itile 1 (length view.tilecache.tiles)]
(view:draw-sprite tilex tiley itile ?key)
(when (= itile chosen-itile)
(love.graphics.rectangle :line (- tilex 2) (- tiley 2) (+ tilew 4) (+ tileh 4)))
(when (button {: view :tag [:tile itile] :x tilex :y tiley :w tilew :h tileh})
(set selected-itile itile))
(set tilex (+ tilex tilew 4))
(when (>= (+ tilex tilew) (+ x w))
(set tilex x)
(set tiley (+ tiley tileh 4))))
(set form.h (+ tiley tileh (- y)))
selected-itile)
(fn GraphicsEditView.draw-tile-selector [self form ?key]
(match (vert form tile-selector {: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