implement dropdowns

This commit is contained in:
Jeremy Penner 2021-04-19 23:52:35 -04:00
parent b7637e5747
commit 415f179bd1
2 changed files with 41 additions and 7 deletions

View file

@ -23,6 +23,10 @@
(fn view.draw [self]
(set self.cursor nil)
(self.__index.draw self)
(when self.imstate.postponed
(each [_ action (ipairs self.imstate.postponed)]
(action))
(set self.imstate.postponed nil))
(when (= self.cursor nil) (set self.cursor :arrow))
(set self.imstate.keys nil)
(set self.imstate.text nil)
@ -57,6 +61,11 @@
(fn [] (when (= (-?> core.active_view.imstate (. :focus)) nil)
(p-fn))))
(fn postpone [view f]
(when (= view.imstate.postponed nil)
(set view.imstate.postponed []))
(table.insert view.imstate.postponed f))
(fn make-tag [tag]
(match (type tag)
:string tag
@ -78,9 +87,9 @@
(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))
(local (w h) (values (+ (style.font:get_width label) style.padding.x) (+ (style.font:get_height) style.padding.y)))
(renderer.draw_rect x y w h style.selection)
(renderer.draw_text style.font label (+ x 4) (+ y 4) style.text)
(renderer.draw_text style.font label (+ x (/ style.padding.x 2)) (+ y (/ style.padding.y 2)) style.text)
(values (button view label x y w h) (+ y h)))
(fn checkbox [view name isset x y ?tag]
@ -89,14 +98,14 @@
(love.graphics.setColor 1 1 1 1)
(button view (or ?tag name) x y (- xEnd x) 12))
(fn focused? [view tag] (= tag (-?> view.imstate.focus (. :tag))))
(fn focused? [view tag] (= (make-tag tag) (-?> view.imstate.focus (. :tag))))
(fn focus [view tag x y w h opts]
(if (activate view tag x y w h)
(set view.imstate.focus
(doto (lume.clone (or opts {}))
(tset :tag tag)))
(tset :tag (make-tag tag))))
(and (= view.imstate.left :released) (focused? view tag) (not (active? view tag)))
(and (= view.imstate.left :released) (focused? view tag) (not (mouse-inside x y w h)))
(set view.imstate.focus nil))
(focused? view tag))
@ -214,4 +223,29 @@
(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}
(fn dropdown [view tag selection options x y w]
(local row-h (+ (style.font:get_height) style.padding.y))
(local new-selection (and (focused? view tag) view.imstate.focus.selection))
(local focused-h (if (focused? view tag) (* row-h (+ (length options) 1)) row-h))
(print new-selection (focused? view tag) focused-h view.imstate.active)
(when new-selection (set view.imstate.focus nil))
(renderer.draw_rect x y w row-h style.selection)
(renderer.draw_text style.font selection (+ x style.padding.x) (+ y (/ style.padding.y 2)) style.text)
(renderer.draw_text style.icon_font "-" (+ x w (- style.padding.x)) (+ y (/ style.padding.y 2)) style.text)
(postpone view
#(when (focus view tag x y w focused-h)
(var row-y (+ y row-h))
(each [i option (ipairs options)]
(renderer.draw_rect x row-y w row-h style.selection)
(renderer.draw_text style.font option (+ x style.padding.x) (+ row-y (/ style.padding.y 2)) style.text)
(when (button view [(make-tag tag) i] x row-y w row-h)
(print "selected" option)
(set view.imstate.focus.selection option))
(set row-y (+ row-y row-h)))))
(or new-selection selection))
{: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active?
: button : checkbox : textbox : textfield : textbutton : dropdown}

View file

@ -2,7 +2,7 @@
(local style (require :core.style))
(local util (require :lib.util))
(local lume (require :lib.lume))
(local {: mouse-inside : activate : active? : checkbox : textfield : textbutton} (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 {: encode-yx : encode-itile : decode-itile} (util.require :game.tiles))