summary refs log tree commit diff
path: root/lib/transition.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'lib/transition.fnl')
-rw-r--r--lib/transition.fnl81
1 files changed, 74 insertions, 7 deletions
diff --git a/lib/transition.fnl b/lib/transition.fnl
index e62dcb8..c1a81a0 100644
--- a/lib/transition.fnl
+++ b/lib/transition.fnl
@@ -3,6 +3,53 @@
 (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)
@@ -10,9 +57,9 @@
   (love.graphics.reset)
   (love.graphics.translate (/ width 2) (/ height 2))
   ;; TODO: this scaling is pretty limited and should probably get bigger!
-  (local scale (if (> height 600)
-                   2
-                   1))
+  (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
@@ -43,19 +90,39 @@
       nil))
 
 (fn keypressed [self key scancode repeat]
-  (when (and (not repeat) (or (and (= self.type :pause)
-                                   (= key :escape))
-                              (= key :z)))
+  (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.draw draw state.init init state.update update state.keypressed keypressed state.mousepressed mousepressed}