improve object editor

This commit is contained in:
Jeremy Penner 2020-10-30 12:55:47 -04:00
parent e22331a9e1
commit e38fda0b57
5 changed files with 93 additions and 23 deletions

View file

@ -77,6 +77,12 @@
(activate view tag x y w h)
(and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h)))
(fn textbutton [view label x y]
(local (w h) (values (+ (style.font:get_width label) 8) 24))
(renderer.draw_rect x y w h style.selection)
(renderer.draw_text style.font label (+ x 4) (+ y 4) style.text)
(values (button view label x y w h) (+ y h)))
(fn checkbox [view name isset x y ?tag]
(love.graphics.rectangle (if isset :fill :line) x y 12 12)
(local xEnd (renderer.draw_text style.font name (+ x 16) y style.text))
@ -148,7 +154,7 @@
text)
(fn textbox [view tag text x y w]
(var textNew text)
(var textNew (or text ""))
(local (h hText xText yText) (values 20 16 (+ x 2) (+ y 2)))
; handle key events
@ -200,6 +206,10 @@
; just draw the text
(renderer.draw_text style.font textNew xText yText style.text))
(love.graphics.setColor 1 1 1)
textNew)
(values textNew (+ y h)))
{: attach-imstate : cmd-predicate : mouse-inside : activate : active? : button : checkbox : textbox}
(fn textfield [view label text x y wLabel wText]
(renderer.draw_text style.font label x y style.text)
(textbox view label text (+ x wLabel) y wText))
{: attach-imstate : cmd-predicate : mouse-inside : activate : active? : button : checkbox : textbox : textfield : textbutton}

View file

@ -1,8 +1,8 @@
(local GraphicsEditView (require :editor.gfxedit))
(local GraphicsEditView (require :editor.gfxedit))
(local style (require :core.style))
(local util (require :lib.util))
(local lume (require :lib.lume))
(local {: mouse-inside : activate : active? : checkbox : textbox} (util.require :editor.imstate))
(local {: mouse-inside : activate : active? : checkbox : textfield : textbutton} (util.require :editor.imstate))
(local MapEditView (GraphicsEditView:extend))
(local sprite-scale 3)
@ -14,7 +14,6 @@
(fn MapEditView.new [self]
(MapEditView.super.new self)
(set self.sprite-scale sprite-scale)
(set self.testtext "Hello")
(self:reload))
; map is stored bottom-to-top
@ -49,27 +48,84 @@
(if (and (= obj.x mx) (= obj.y my))
iobj
(self:iobject-from-xy mx my (+ iobj 1)))))
(fn MapEditView.object [self] (. self.level.objects self.iobject))
(fn move-object [objects iobjectsrc iobjectdst]
(each [_ object (pairs objects)]
(when (= object.link iobjectsrc)
(set object.link iobjectdst)))
(tset objects iobjectdst (. objects iobjectsrc))
(tset objects iobjectsrc nil)
(when (. objects (+ iobjectsrc 1))
(move-object objects (+ iobjectsrc 1) iobjectsrc)))
(fn MapEditView.linking-obj [self] (. self.level.objects self.iobject-linking))
(fn MapEditView.draw-link-line [self x y iobjectSrc color toMouse?]
(local objectSrc (. self.level.objects iobjectSrc))
(local objectDest (. self.level.objects objectSrc.link))
(local coord (fn [c m d] (+ c (* (- m 1) d) (/ d 2))))
(local xStart (coord x objectSrc.x tilew))
(local yStart (coord y objectSrc.y tileh))
(when (or toMouse? objectDest)
(local xEnd (if toMouse? (love.mouse.getX) (coord x objectDest.x tilew)))
(local yEnd (if toMouse? (love.mouse.getY) (coord y objectDest.y tileh)))
(love.graphics.setColor (table.unpack color))
(love.graphics.line xStart yStart xEnd yEnd)
(love.graphics.circle :line xEnd yEnd (/ tilew 5))
(love.graphics.setColor 1 1 1)))
(fn MapEditView.draw-map-editor [self x y]
(activate self :map x y (* tilew mapw) (* tileh maph))
(var iobject-over nil)
(for [mx 1 mapw] (for [my 1 maph]
(local tilex (+ x (* (- mx 1) tilew)))
(local tiley (+ y (* (- my 1) tileh)))
(local itile (self:itile-from-xy mx my))
(local iobject (self:iobject-from-xy mx my))
(self:draw-sprite tilex tiley itile)
(when (not= iobject nil)
(when (and (not= iobject nil) (= self.itile nil))
(love.graphics.setColor 1 0 (if (and (= self.itile nil) (= iobject self.iobject)) 1 0))
(love.graphics.setLineWidth 3)
(love.graphics.rectangle :line tilex tiley tilew tileh)
(love.graphics.setColor 1 1 1))
(when (and (mouse-inside tilex tiley tilew tileh) (not= iobject nil))
(set iobject-over iobject))
(when (and self.itile (active? self :map) (mouse-inside tilex tiley tilew tileh) (not= itile self.itile))
(self:set-tile mx my self.itile))
(when (and (= self.itile nil) (active? self :map) (mouse-inside tilex tiley tilew tileh) (= self.imstate.left :released))
(if (= iobject nil)
(do (table.insert self.level.objects {:x mx :y my :func ""})
(set self.iobject (length self.level.objects)))
(set self.iobject iobject))))))
(when (and (= self.itile nil) (active? self :map) (mouse-inside tilex tiley tilew tileh))
(match self.imstate.left
:pressed (set self.iobject-linking iobject)
:released
(if (and (not= iobject nil) (= self.iobject-linking iobject))
(set self.iobject iobject)
(not= self.iobject-linking nil)
(tset (self:linking-obj) :link iobject)
(= iobject nil)
(let [tile (self.tilecache:tile itile)]
(table.insert self.level.objects {:x mx :y my :func (or tile.word "")})
(set self.iobject (length self.level.objects))))))))
(when (= self.itile nil)
(for [iobject 1 (length self.level.objects)]
(self:draw-link-line x y iobject [0 0 1 0.3]))
(when (not= iobject-over nil) (self:draw-link-line x y iobject-over [0 0.5 1] false))
(when (not= self.iobject-linking nil)
(if (= self.imstate.left :released) (set self.iobject-linking nil)
(self:draw-link-line x y self.iobject-linking [0 1 0] true)))))
(fn MapEditView.draw-object-editor [self x y]
(var y y)
(local object (self:object))
(set (object.func y) (textfield self "Word" object.func x y 100 200))
(set (object.name y) (textfield self "Name" object.name x (+ y 5) 100 200))
(set (object.linkword y) (textfield self "Link word" object.linkword x (+ y 5) 100 200))
(match (if object.link (textbutton self "Unlink" x (+ y 5)))
(unlink yNext) (do (when unlink (set object.link nil))
(set y yNext)))
(when (textbutton self "Delete" x (+ y 40))
(move-object self.level.objects (+ self.iobject 1) self.iobject)
(set self.iobject nil)))
(fn MapEditView.reload [self]
(MapEditView.super.reload self)
@ -85,16 +141,18 @@
(tset :map (self.level.map:tohex)))))
(fn MapEditView.draw [self]
(var x (+ self.position.x 10))
(var y (+ self.position.y 10))
(self:draw_background style.background)
(love.graphics.setColor 1 1 1 1)
(self:draw-map-editor (+ self.position.x 10) (+ self.position.y 10))
(when (checkbox self "Edit objects" (= self.itile nil) (+ self.position.x 10) (+ self.position.y 20 (* tileh maph)))
(self:draw-map-editor x y)
(when self.iobject
(self:draw-object-editor (+ x (* tilew mapw) 10) y))
(set y (+ y (* tileh maph) 10))
(when (checkbox self "Edit objects" (= self.itile nil) x y)
(set self.itile nil))
(set self.testtext (textbox self :test self.testtext (+ self.position.x 10) (+ self.position.y 40 (* tileh maph)) 200))
(self:draw-tile-selector
(+ self.position.x 10)
(+ self.position.y 65 (* tileh maph))
(- self.size.x 20)))
(set y (+ y 30))
(self:draw-tile-selector x y (- self.size.x 20)))
(fn MapEditView.get_name [self] "Map Editor")

View file

@ -3,7 +3,7 @@
(local tiles (require :game.tiles))
(local tiledraw (require :editor.tiledraw))
(local util (require :lib.util))
(local {: mouse-inside : activate : active? : checkbox} (util.require :editor.imstate))
(local {: mouse-inside : activate : active? : checkbox : textfield} (util.require :editor.imstate))
(local TileView (GraphicsEditView:extend))
@ -71,8 +71,10 @@
(tset flags flagname (if flagset nil true))))
(fn TileView.draw-tile-flags [self x y]
(local tile (-?> self.tilecache.tiles (. self.itile)))
(when tile (set tile.word (textfield self "Default word" tile.word x y 100 200)))
(each [iflag flagname (ipairs tiles.flags)]
(self:draw-tile-flag flagname x (+ y (* (- iflag 1) (+ pixel-size 4))))))
(self:draw-tile-flag flagname x (+ y (* iflag (+ pixel-size 4))))))
(fn TileView.update-tile [self newtile]
(self.tilecache:update-tile self.itile newtile))

View file

@ -1 +1 @@
{"map":"2121212141212121212121212121412121212121610261616102616161026161616161026161612161C0C0C0C0C0C0C0C0C0C0C061C0C0C0C0C0612161C0C0A282C0C0C0A2C0C0C081C080C0C0C2024161C0C0C0C0C0C0C0C0C2C0A2C1A2C0C0E082612161E2C08282C0C0C0C0A2C0C06161616161616121616161616161C1816161616161C0C0C0C0C0612161C0C0C0C0C0C0C0C0C0C0C061C0C0C0C0C0022161E08282A2C0C0C0C0C0C0C081C0C0C0C0C0612161C0C0C0C0C0C0C0C0C0C0C061C0C0C0C0C0614161C0C0C0C0C0C0C0C0C0C0C061C0C0C0C0C061216161616161616161228161616161616161610221","objects":[{"func":"","x":17,"y":8},{"func":"","x":13,"y":9},{"func":"","x":13,"y":8},{"func":"","x":13,"y":4},{"func":"","x":8,"y":6},{"func":"","x":7,"y":6},{"func":"","x":2,"y":4},{"func":"","x":9,"y":1},{"func":"","x":10,"y":1}]}
{"map":"212121214121212121212121212141212121212161026161610261616102616161616102616161216143C0C0E2C0C0C0C0C0C06361C0C0C0C0C0612161C0C0A282C0C0C0A2C0C0C081C080C0C0C2024161C0C0C0C0C0C0C0C0C2C0A2C1A2C0C0E082612161E2C08282C0C0C0C0A2C0C06161616161616121616161616161C181616161616143C0C0E282612161C0C0C0C0C0C0C0C0C0C0C061C0C0C0C0C0022161E08282A2C0C0C0C0E2C0C081C0C0C0C003612161E2C2C2C0C0C0C0C0C0C0C061C0C0C0C0C06141610303C063E2C0C0C0C0C0C061C0C0C023C061216161616161616161228161616161616161610221","objects":[{"func":"neutterm","x":17,"y":8,"link":6},{"func":"door","x":13,"y":9},{"func":"switch","x":13,"y":8,"link":2},{"func":"door","x":8,"y":6},{"func":"switch","x":7,"y":6,"link":4},{"func":"termtut","x":2,"y":4,"link":1},{"func":"scantut","x":9,"y":1,"link":8},{"x":10,"func":"exit","linkword":"","name":"","y":1}]}

View file

@ -1 +1 @@
[{"flags":[],"gfx":"000000020A0820406A4020080A02000000010141511005025702051051410100"},{"flags":[],"gfx":"00000000020A0820406A4020080A020000000101415110050257020510514101"},{"flags":[],"gfx":"808080C0C0C0E0C0D0C8C04040404080808083858585828A9282820A08081980"},{"flags":[],"gfx":"8080C0A0A0A0C0C0D0C8C0501010188080808183838782828A8A920202020380"},{"flags":[],"gfx":"8080E0B0B0B098C0D0D0C840404060808080878D8D8D99828A8A920202020780"},{"flags":[],"gfx":"8080C0E0E0E0B0C0D0C8C040404060808080838787878D828A92820202020780"},{"flags":{"walkable":true},"gfx":"80808C8080808080B08080808C808080808C80808083B0808080808080868080"},{"flags":[],"gfx":"007C0C0C0C0C7C007C7E7EAA88888800001F181818181F001F0F979584848400"},{"flags":{"neutable":true},"gfx":"007C2C0C0C2C7C007C7E7EAA88888800001F18191C191F001F0F979584848400"},{"flags":[],"gfx":"D5D5D5D5D5D5D5D5D5D5D5D5D5D5D5D5AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"},{"flags":[],"gfx":"D5D5D5D5D5F5F5FDDDD5D5D5D5D5D5D5AAAAAAAAAEAEBFBFBFABAAAAAAAAAAAA"},{"flags":{"neutable":true},"gfx":"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"},{"flags":[],"gfx":"FF8FA7B3B3B3B3B3B3B3B3B3B3B383FFFFF8F2E6E6E6E6E6E6E6E6E6E6E6E0FF"},{"flags":{"walkable":true},"gfx":"FF8F87838383838383838383838383FFFFF8F0E0E0E0E0E0E0E0E0E0E0E0E0FF"},{"flags":{"neutable":true},"gfx":"FFFFCFCFCFCF898183838787878FFFFFFFFCE4E4E4E4E0E0E0E0E0E0F0F8FFFF"},{"flags":{"neutable":true},"gfx":"FFFFCFCFCFCF898123232727878FFFFFFFFCE4E4E4E0E0616565656571F8FFFF"},{"flags":[],"gfx":"FFFF83D3D3D3D3D3D3D3D3D3D383FFFFFFFFC0CACACECACBCACACACACAC0FFFF"},{"flags":{"neutable":true},"gfx":"FFFFAFEBFBFBFBBBBBFBFBFBEBAFFFFFFFFFF5D7DFDFDFDDDDDFDFDFD7F5FFFF"},{"flags":{"neutable":true},"gfx":"FFFF2F2B2B2B6B6B6B6B2B2B2B2FFFFFFFFF755555555757575755555575FFFF"},{"flags":[],"gfx":"FFF3C78FBFFCF98187BFFFFFBF9F9FC7FFCFE1F1FCFCF8FEFEFCF9F0E6CE8F9F"},{"flags":[],"gfx":"80808C80808080A8AAAAAAA888888880808C8080808380859595958584848480"},{"flags":{"debris":true},"gfx":"80808C8080A0A0A8AAAA8AA0A8808080808C8081919090848594959585858080"},{"flags":[],"gfx":"8080D0848484D0C0C0D0D4D49090B880808C8AA0A0A08A83838AAAAA88889C80"},{"flags":{"debris":true},"gfx":"808080809890D4D0C0E0E0908484D480808C808682828A8A81808288888A8280"}]
[{"word":"","gfx":"000000020A0820406A4020080A02000000010141511005025702051051410100","flags":[]},{"flags":[],"gfx":"00000000020A0820406A4020080A020000000101415110050257020510514101"},{"flags":[],"gfx":"808080C0C0C0E0C0D0C8C04040404080808083858585828A9282820A08081980"},{"flags":[],"gfx":"8080C0A0A0A0C0C0D0C8C0501010188080808183838782828A8A920202020380"},{"flags":[],"gfx":"8080E0B0B0B098C0D0D0C840404060808080878D8D8D99828A8A920202020780"},{"flags":[],"gfx":"8080C0E0E0E0B0C0D0C8C040404060808080838787878D828A92820202020780"},{"word":"","gfx":"80808C8080808080B08080808C808080808C80808083B0808080808080868080","flags":{"walkable":true}},{"word":"term","gfx":"007C0C0C0C0C7C007C7E7EAA88888800001F181818181F001F0F979584848400","flags":[]},{"word":"term","gfx":"007C2C0C0C2C7C007C7E7EAA88888800001F18191C191F001F0F979584848400","flags":{"neutable":true}},{"flags":[],"gfx":"D5D5D5D5D5D5D5D5D5D5D5D5D5D5D5D5AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"},{"flags":[],"gfx":"D5D5D5D5D5F5F5FDDDD5D5D5D5D5D5D5AAAAAAAAAEAEBFBFBFABAAAAAAAAAAAA"},{"flags":{"neutable":true},"gfx":"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"},{"word":"door","gfx":"FF8FA7B3B3B3B3B3B3B3B3B3B3B383FFFFF8F2E6E6E6E6E6E6E6E6E6E6E6E0FF","flags":[]},{"word":"door","gfx":"FF8F87838383838383838383838383FFFFF8F0E0E0E0E0E0E0E0E0E0E0E0E0FF","flags":{"walkable":true}},{"word":"switch","gfx":"FFFFCFCFCFCF898183838787878FFFFFFFFCE4E4E4E4E0E0E0E0E0E0F0F8FFFF","flags":{"neutable":true}},{"word":"switch","gfx":"FFFFCFCFCFCF898123232727878FFFFFFFFCE4E4E4E0E0616565656571F8FFFF","flags":{"neutable":true}},{"flags":[],"gfx":"FFFF83D3D3D3D3D3D3D3D3D3D383FFFFFFFFC0CACACECACBCACACACACAC0FFFF"},{"word":"scan","gfx":"FFFFAFEBFBFBFBBBBBFBFBFBEBAFFFFFFFFFF5D7DFDFDFDDDDDFDFDFD7F5FFFF","flags":{"neutable":true}},{"word":"scan","gfx":"FFFF2F2B2B2B6B6B6B6B2B2B2B2FFFFFFFFF755555555757575755555575FFFF","flags":{"neutable":true}},{"flags":[],"gfx":"FFF3C78FBFFCF98187BFFFFFBF9F9FC7FFCFE1F1FCFCF8FEFEFCF9F0E6CE8F9F"},{"flags":[],"gfx":"80808C80808080A8AAAAAAA888888880808C8080808380859595958584848480"},{"flags":{"debris":true},"gfx":"80808C8080A0A0A8AAAA8AA0A8808080808C8081919090848594959585858080"},{"word":"","gfx":"00005054545450404054545010383800000C0A2A2A2A0A03032A2A0A081C1C00","flags":[]},{"word":"","gfx":"0000001C1C10545040606010545454000030070702020A0A0100020A080A0200","flags":{"debris":true}},{"word":"","gfx":"80A0A8AA92D2D2AAC2C2AA92D2AA808080959595949494959494959494858080","flags":[]},{"word":"","gfx":"80808C808080A8C292AAAAAAAA8AC0D0808C80808083959290959194948580A8","flags":{"debris":true}},{"word":"","gfx":"80806008282A0800202880A8A8A8A08080980000141501051511819595958580","flags":[]},{"word":"","gfx":"80808C808080A0A8AAAA8AA2AAAAAA80808C0000000330010105051511010514","flags":{"debris":true}}]