LESS CODE IS BETTER CODE

This commit is contained in:
Jeremy Penner 2020-10-18 00:03:00 -04:00
parent aa326c30df
commit 65663b9722
2 changed files with 7 additions and 12 deletions

View file

@ -1,19 +1,13 @@
(fn attach-imstate [view] (fn attach-imstate [view]
(set view.imstate {:x (love.mouse.getX) :y (love.mouse.getY)}) (set view.imstate {})
(local cls view.__index) (local cls view.__index)
(fn view.on_mouse_pressed [self button x y clicks] (fn view.on_mouse_pressed [self button x y clicks]
(tset self.imstate button :pressed) (tset self.imstate button :pressed)
(set self.imstate.x x)
(set self.imstate.y y)
(cls.on_mouse_pressed self button x y clicks)) (cls.on_mouse_pressed self button x y clicks))
(fn view.on_mouse_moved [self x y dx dy] (fn view.on_mouse_moved [self x y dx dy]
(set self.imstate.x x)
(set self.imstate.y y)
(cls.on_mouse_moved self x y dx dy)) (cls.on_mouse_moved self x y dx dy))
(fn view.on_mouse_released [self button x y] (fn view.on_mouse_released [self button x y]
(tset self.imstate button :released) (tset self.imstate button :released)
(set self.imstate.x x)
(set self.imstate.y y)
(cls.on_mouse_released self button x y)) (cls.on_mouse_released self button x y))
(fn view.draw [self] (fn view.draw [self]
(cls.draw self) (cls.draw self)
@ -32,16 +26,17 @@
:table (table.concat tag "::") :table (table.concat tag "::")
_ (tostring tag))) _ (tostring tag)))
(fn mouse-inside [view x y w h] (fn mouse-inside [x y w h]
(and (>= view.imstate.x x) (<= view.imstate.x (+ x w)) (>= view.imstate.y y) (<= view.imstate.y (+ y h)))) (local (mx my) (values (love.mouse.getX) (love.mouse.getY)))
(and (>= mx x) (<= mx (+ x w)) (>= my y) (<= my (+ y h))))
(fn activate [view tag x y w h] (fn activate [view tag x y w h]
(when (and (= view.imstate.left :pressed) (mouse-inside view x y w h)) (when (and (= view.imstate.left :pressed) (mouse-inside x y w h))
(set view.imstate.active (make-tag tag)) (set view.imstate.active (make-tag tag))
true)) true))
(fn active? [view tag] (= view.imstate.active (make-tag tag))) (fn active? [view tag] (= view.imstate.active (make-tag tag)))
(fn button [view tag x y w h] (fn button [view tag x y w h]
(activate view tag x y w h) (activate view tag x y w h)
(and (active? view tag) (= view.imstate.left :released) (mouse-inside view x y w h))) (and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h)))
{: attach-imstate : mouse-inside : activate : active? : button} {: attach-imstate : mouse-inside : activate : active? : button}

View file

@ -69,7 +69,7 @@
(if (or (= bitx 0) (= bitx 15)) (if (or (= bitx 0) (= bitx 15))
(draw-bit-color bit px py) (draw-bit-color bit px py)
(draw-bit bit px py (= (% bitx 2) 1))) (draw-bit bit px py (= (% bitx 2) 1)))
(when (and (active? self :tile) (mouse-inside self px py pixel-size pixel-size)) (when (and (active? self :tile) (mouse-inside px py pixel-size pixel-size))
(when (= self.bit nil) (set self.bit (not bit))) (when (= self.bit nil) (set self.bit (not bit)))
(when (not= self.bit bit) (when (not= self.bit bit)
(self:update-tile (set-tile-bit tile ibyte ibit self.bit)))))) (self:update-tile (set-tile-bit tile ibyte ibit self.bit))))))