refactor map editor, add action editor

This commit is contained in:
Jeremy Penner 2021-04-20 20:13:51 -04:00
parent 6f40e7ff40
commit 79c8791dac
3 changed files with 71 additions and 34 deletions

12
editor/actions.fnl Normal file
View file

@ -0,0 +1,12 @@
(local util (require :lib.util))
(local {: defmulti : defmethod} (util.require :lib.multimethod))
(local {: textfield} (util.require :editor.imstate))
(local actions (util.hot-table ...))
(set actions.edit (defmulti #$1.action :edit ...))
(set actions.generate (defmulti #$1.action :generate ...))
(defmethod actions.edit :default (fn [action view x y w i] y))
actions.hot

View file

@ -84,7 +84,7 @@
(fn button [view tag x y w h] (fn button [view tag x y w h]
(when (mouse-inside x y w h) (set view.cursor :hand)) (when (mouse-inside x y w h) (set view.cursor :hand))
(activate view tag x y w h) (activate view tag x y w h)
(and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h))) (values (and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h)) (+ y h style.padding.y)))
(fn textbutton [view label x y] (fn textbutton [view label x y]
(local (w h) (values (+ (style.font:get_width label) style.padding.x) (+ (style.font:get_height) style.padding.y))) (local (w h) (values (+ (style.font:get_width label) style.padding.x) (+ (style.font:get_height) style.padding.y)))
@ -244,7 +244,7 @@
(set view.imstate.focus.selection option)) (set view.imstate.focus.selection option))
(set row-y (+ row-y row-h))))) (set row-y (+ row-y row-h)))))
(or new-selection selection)) (values (or new-selection selection) (+ y row-h)))
{: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active? {: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active?
: button : checkbox : textbox : textfield : textbutton : dropdown} : button : checkbox : textbox : textfield : textbutton : dropdown}

View file

@ -5,6 +5,7 @@
(local {: mouse-inside : activate : active? : checkbox : textfield : textbutton : dropdown} (util.require :editor.imstate)) (local {: mouse-inside : activate : active? : checkbox : textfield : textbutton : dropdown} (util.require :editor.imstate))
(local {: tilestrip-to-sprite} (util.require :editor.tiledraw)) (local {: tilestrip-to-sprite} (util.require :editor.tiledraw))
(local {: encode-yx : encode-itile : decode-itile} (util.require :game.tiles)) (local {: encode-yx : encode-itile : decode-itile} (util.require :game.tiles))
(local actions (require :editor.actions))
(local MapEditView (GraphicsEditView:extend)) (local MapEditView (GraphicsEditView:extend))
(local sprite-scale 3) (local sprite-scale 3)
@ -140,20 +141,43 @@
(if (= self.imstate.left :released) (set 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))))) (self:draw-link-line x y self.iobject-linking [0 1 0] true)))))
(fn MapEditView.draw-object-editor [self x y] (fn MapEditView.draw-object-code-editor [self object x y]
(var y y) (var y y)
(local object (self:object)) (var istep-to-delete nil)
(set (object.func y) (textfield self "Word" object.func x y 100 200)) (when (not object.steps) (set object.steps []))
(set (object.name y) (textfield self "Name" object.name x (+ y 5) 100 200)) (each [istep step (ipairs object.steps)]
(set (object.linkword y) (textfield self "Link word" object.linkword x (+ y 5) 100 200)) (when (textbutton self "X" (+ x 280) y)
(if object.link (set istep-to-delete istep))
(match (textbutton self "Unlink" x (+ y 5)) (set (step.action y) (dropdown self [:code-action istep] (or step.action :say) [:say :warp] x y 100))
(unlink yNext) (do (when unlink (set object.link nil)) (set y (actions.edit step self x y 300 istep))
(set y yNext))) (set y (+ y style.padding.y)))
(set (object.linkentity y) (textfield self "Link entity" object.linkentity x (+ y 5) 100 200))) (when istep-to-delete (table.remove object.steps istep-to-delete))
(when (textbutton self "Delete" x (+ y 40)) (let [(do-new y) (textbutton self "+ New Step" x (+ y style.padding.y))]
(when do-new (table.insert object.steps {}))
y))
(fn MapEditView.draw-object-advanced-editor [self object x y]
(let [(func y) (textfield self "Word" object.func x y 100 200)
(name y) (textfield self "Name" object.name x (+ y style.padding.y) 100 200)
(linkword y) (textfield self "Link word" object.linkword x (+ y style.padding.y) 100 200)
(do-unlink y) (if object.link (textbutton self "Unlink" x (+ y style.padding.y)) (values false y))
(linkentity y) (if object.link (values object.linkentity y) (textfield self "Link entity" object.linkentity x (+ y style.padding.y) 100 200))]
(lume.extend object {: func : name : linkword : linkentity})
(when do-unlink (set object.link nil))
y))
(fn MapEditView.draw-object-editor [self x y]
(let [object (self:object)
y (if object.advanced
(self:draw-object-advanced-editor object x y)
(self:draw-object-code-editor object x y))
do-delete (textbutton self "Delete" x (+ y 40))
(do-advanced y) (textbutton self (if object.advanced "Simple" "Advanced") (+ x 150) (+ y 40))]
(when do-delete
(move-object self.level.objects (+ self.iobject 1) self.iobject) (move-object self.level.objects (+ self.iobject 1) self.iobject)
(set self.iobject nil))) (set self.iobject nil))
(when do-advanced (set object.advanced (not object.advanced)))
y))
(fn MapEditView.reload [self] (fn MapEditView.reload [self]
(MapEditView.super.reload self) (MapEditView.super.reload self)
@ -175,27 +199,28 @@
(self:draw_background style.background) (self:draw_background style.background)
(self:draw_scrollbar) (self:draw_scrollbar)
(love.graphics.setColor 1 1 1 1) (love.graphics.setColor 1 1 1 1)
(local ytop y)
(self:draw-map-editor x y) (self:draw-map-editor x y)
(when self.iobject (set y (+ y (* tileh maph) style.padding.y))
(self:draw-object-editor (+ x (* tilew mapw) 10) y)) (set y (+ y (self:draw-tile-selector x y (- self.size.x (* style.padding.x 2)))))
(set y (+ y (* tileh maph) 10))
(set self.level.tickword (textfield self "Tick word" self.level.tickword x y 100 200)) (set (self.level.tickword y) (textfield self "Tick word" self.level.tickword x (+ y style.padding.y) 100 200))
(set y (+ y 30)) (set (self.level.moveword y) (textfield self "Move word" self.level.moveword x (+ y style.padding.y) 100 200))
(set self.level.moveword (textfield self "Move word" self.level.moveword x y 100 200)) (set (self.level.loadword y) (textfield self "Load word" self.level.loadword x (+ y style.padding.y) 100 200))
(set y (+ y 30)) (let [(checked y-new) (checkbox self "Edit objects" (= self.itile nil) x (+ y style.padding.y))
(set self.level.loadword (textfield self "Load word" self.level.loadword x y 100 200)) _ (when checked
(set y (+ y 30))
(when (checkbox self "Edit objects" (= self.itile nil) x y)
(set self.itile nil) (set self.itile nil)
(set self.playerpos nil)) (set self.playerpos nil))
(set y (+ y 30)) (checked y-new) (checkbox self (.. "Position " :player) (and (= self.itile nil) (= self.playerpos :player)) x (+ y-new style.padding.y))]
(let [player :player] (when checked
(when (checkbox self (.. "Position " player) (and (= self.itile nil) (= self.playerpos player)) x y)
(set self.itile nil) (set self.itile nil)
(set self.playerpos player)) (set self.playerpos :player))
(set y (+ y 30))) (set y y-new))
(set y (+ y 30)) (when self.iobject
(set y (+ y (self:draw-tile-selector x y (- self.size.x 20)))) (set y (math.max y (if (> self.size.x (+ (* tilew mapw) 300))
(self:draw-object-editor (+ x (* tilew mapw) style.padding.x) ytop)
(self:draw-object-editor x (+ y style.padding.y))))))
(set self.scrollheight (- y (+ self.position.y style.padding.y (- self.scroll.y))))) (set self.scrollheight (- y (+ self.position.y style.padding.y (- self.scroll.y)))))
(fn MapEditView.get_name [self] (.. "Map: " self.mapfilename)) (fn MapEditView.get_name [self] (.. "Map: " self.mapfilename))