From 65663b972275d7f541bb621d6909484fb502c3b7 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Sun, 18 Oct 2020 00:03:00 -0400 Subject: [PATCH] LESS CODE IS BETTER CODE --- editor/imstate.fnl | 17 ++++++----------- editor/tileedit.fnl | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/editor/imstate.fnl b/editor/imstate.fnl index fa32c95..e315826 100644 --- a/editor/imstate.fnl +++ b/editor/imstate.fnl @@ -1,19 +1,13 @@ (fn attach-imstate [view] - (set view.imstate {:x (love.mouse.getX) :y (love.mouse.getY)}) + (set view.imstate {}) (local cls view.__index) (fn view.on_mouse_pressed [self button x y clicks] (tset self.imstate button :pressed) - (set self.imstate.x x) - (set self.imstate.y y) (cls.on_mouse_pressed self button x y clicks)) (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)) (fn view.on_mouse_released [self button x y] (tset self.imstate button :released) - (set self.imstate.x x) - (set self.imstate.y y) (cls.on_mouse_released self button x y)) (fn view.draw [self] (cls.draw self) @@ -32,16 +26,17 @@ :table (table.concat tag "::") _ (tostring tag))) -(fn mouse-inside [view x y w h] - (and (>= view.imstate.x x) (<= view.imstate.x (+ x w)) (>= view.imstate.y y) (<= view.imstate.y (+ y h)))) +(fn mouse-inside [x y w 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] - (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)) true)) (fn active? [view tag] (= view.imstate.active (make-tag tag))) (fn button [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} diff --git a/editor/tileedit.fnl b/editor/tileedit.fnl index aca8c53..8c3434f 100644 --- a/editor/tileedit.fnl +++ b/editor/tileedit.fnl @@ -69,7 +69,7 @@ (if (or (= bitx 0) (= bitx 15)) (draw-bit-color bit px py) (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 (not= self.bit bit) (self:update-tile (set-tile-bit tile ibyte ibit self.bit))))))