widget grouping mechanism

This commit is contained in:
Jeremy Penner 2021-12-21 09:45:45 -06:00
parent e711557fdf
commit b9ec214b46
3 changed files with 43 additions and 8 deletions

View file

@ -331,8 +331,37 @@
(set form.y (+ form.y form.h (or form.ypad 0))))
(table.unpack result))))
(fn begin-group []
(let [group {}
update-dimension
(fn [form coord-key size-key]
(let [coord-group (. group coord-key) size-group (. group size-key)
coord-form (. form coord-key) size-form (. form size-key)]
(if (= coord-group nil) ; container takes on the size of its first item
(do (tset group coord-key coord-form)
(tset group size-key size-form))
(> coord-group coord-form) ; we have an item that is outside the bounds to the left / up; reduce the starting point and extend the size
(do (tset group coord-key coord-form)
(tset group size-key (- (math.max (+ coord-form size-form) (+ coord-group size-group)) coord-form)))
; extend the size if the new item is outside the bounds to the right / down
(tset group size-key (- (math.max (+ coord-form size-form) (+ coord-group size-group)) coord-group)))))]
(fn [viewfn-or-form ?form ...]
(if (= (type viewfn-or-form) :function)
(let [result [(viewfn-or-form ?form ...)]]
(update-dimension ?form :x :w)
(update-dimension ?form :y :h)
(table.unpack result))
(do (set viewfn-or-form.x group.x)
(set viewfn-or-form.y group.y)
(set viewfn-or-form.w group.w)
(set viewfn-or-form.h group.h)
viewfn-or-form)))))
{: attach-imstate : cmd-predicate : postpone : mouse-inside : activate : active?
: button : checkbox : textbox : textfield : textbutton : dropdown
: vert : horiz : horiz-wrapper
: vert : horiz : horiz-wrapper : begin-group
: with-style : prepare-form : form-defaults}

View file

@ -43,6 +43,13 @@
"graphics-editor:next-tile" #(core.active_view:select-rel 1)
"graphics-editor:previous-tile" #(core.active_view:select-rel -1)
})
(command.add (cmd-predicate :editor.gfxedit2) {
"graphics-editor:save" (fn [] (core.active_view:save) (core.log "Saved"))
"graphics-editor:reload" (fn [] (core.active_view:reload) (core.log "Reloaded"))
"graphics-editor:next-tile" #(core.active_view:select-rel 1)
"graphics-editor:previous-tile" #(core.active_view:select-rel -1)
})
(command.add (cmd-predicate :editor.tileedit) {
"tileedit:copy"
#(system.set_clipboard (: (core.active_view:tile) :tohex))

View file

@ -4,7 +4,8 @@
(local files (require :game.files))
(local util (require :lib.util))
(local lume (require :lib.lume))
(local {: mouse-inside : activate : active? : checkbox : textfield : button : dropdown : with-style : form-defaults : vert : horiz-wrapper} (util.require :editor.imgui))
(local {: mouse-inside : activate : active? : checkbox : textfield : button : dropdown : with-style : form-defaults
: vert : horiz-wrapper : begin-group} (util.require :editor.imgui))
(local TileView (GraphicsEditView:extend))
@ -93,19 +94,17 @@
(fn TileView.draw-tile-preview [self form] (vert form tile-preview {} self.itile self.tilekey))
(fn tile-palette [{:view self : x : y : w &as form} pal selected-color]
(let [wrap (horiz-wrapper (with-style form))]
(let [g (begin-group)
wrap (horiz-wrapper (with-style form))]
(var selected-color selected-color)
(each [icolor color (ipairs pal)]
(renderer.draw_rect form.x form.y pixel-size pixel-size color)
(when (= icolor selected-color)
(love.graphics.setColor 1 1 1 1)
(love.graphics.rectangle :line (- form.x 2) (- form.y 2) (+ pixel-size 4) (+ pixel-size 4)))
(when (wrap button {:tag [:pal icolor] :w pixel-size :h pixel-size})
(when (g wrap button {:tag [:pal icolor] :w pixel-size :h pixel-size})
(set selected-color icolor)))
(set form.w w) ;; todo: some kind of "group" combinator that does this for us
(set form.h (+ (- form.y y) pixel-size))
(set form.x x)
(set form.y y)
(g form)
selected-color))
(fn TileView.draw-tile-palette [self form]