(local state (require :lib.state)) (local font (require :lib.font)) (local translation (require :lib.translation)) (local music-state (require :lib.music-state)) (fn get-scale [] (if (> (select 2 (love.graphics.getDimensions)) 600) 2 1)) (fn draw-boxes [boxes scale tab] (let [(width height) (love.graphics.getDimensions) (mouse-x mouse-y) (love.mouse.getPosition) mouse-x (/ (- mouse-x (/ width 2)) scale) mouse-y (/ (- mouse-y (/ height 2)) scale)] (each [i box (ipairs boxes)] (if (and (>= mouse-x box.x) (< mouse-x (+ box.x box.width)) (>= mouse-y box.y) (< mouse-y (+ box.y box.height))) (love.graphics.setColor 1 1 1 0.2) (love.graphics.setColor 1 1 1 0.1)) (love.graphics.rectangle :fill (* scale box.x) (* scale box.y) (* scale box.width) (* scale box.height)) (when (= tab i) (love.graphics.setColor 1 1 1 0.2) (love.graphics.rectangle :line (* scale box.x) (* scale box.y) (* scale box.width) (* scale box.height))) (love.graphics.setFont (. font (* 12 scale))) (love.graphics.setColor 1 1 1 0.8) (love.graphics.printf (box.text) (* scale box.x) (* scale (+ box.y (/ box.height 6))) (* scale box.width) :center) ))) (local boxes {:menu [{:x -106 :y 94 :width 212 :height 24 :text #translation.text.start :action (fn [self] (set self.transition true))} {:x -106 :y 130 :width 100 :height 24 :text #"toki pona" :action #(set translation.text translation.tp)} {:x 6 :y 130 :width 100 :height 24 :text #"english" :action #(set translation.text translation.en)}]}) (fn draw [self] (local (width height) (love.graphics.getDimensions)) (state.draw self.present-state) ;; TODO: translation (love.graphics.reset) (love.graphics.translate (/ width 2) (/ height 2)) ;; TODO: this scaling is pretty limited and should probably get bigger! (local scale (get-scale)) (when (. boxes self.type) (draw-boxes (. boxes self.type) scale self.tab)) (love.graphics.setColor 0.8 0.8 0.8 (/ (math.max 0 (- self.age 30)) 10)) (love.graphics.setFont (. font (* 48 scale))) (love.graphics.printf (string.format (. translation.text self.type 1) (and self.level (translation.text.number self.level))) (* scale -500) (* scale -64) (* scale 1000) :center) (love.graphics.setFont (. font (* 12 scale))) (love.graphics.printf (string.format (. translation.text self.type 2) (and self.level (translation.text.number self.level))) (* scale -500) 0 (* scale 1000) :center)) (fn update [self] ;; TODO: actually update in case it returns something else (when (not= self.type :pause) (state.update self.present-state)) (when (= self.type :pause) (set music-state.screen :pause)) (set self.age (math.min 40 (+ self.age 1))) (if self.transition self.future-state nil)) (fn keypressed [self key scancode repeat] (when (and (not repeat) (= self.type :pause) (= key :escape)) (set self.transition true)) (when (and (not repeat) (or (= key :z) (= key :return))) (if (and (. boxes self.type) (> self.tab 0)) ((. boxes self.type self.tab :action) self) (set self.transition true))) (when (and (= key :tab) (. boxes self.type)) (set self.tab (+ (% self.tab (length (. boxes self.type))) 1))) ) (fn mousepressed [self x y button] (let [(width height) (love.graphics.getDimensions) scale (get-scale) x (/ (- x (/ width 2)) scale) y (/ (- y (/ height 2)) scale)] (print x y) (when (and (= button 1) (. boxes self.type)) (print :a) (each [i box (ipairs (. boxes self.type))] (when (and (>= x box.x) (< x (+ box.x box.width)) (>= y box.y) (< y (+ box.y box.height))) (box.action self)))))) (fn init [self type present-state future-state level] (setmetatable {: type : present-state : level : future-state :tab 0 :age (if (or (= type :pause) (= type :menu)) 40 0)} self)) {state.draw draw state.init init state.update update state.keypressed keypressed state.mousepressed mousepressed}