summary refs log tree commit diff
path: root/lib/transition.fnl
blob: 137789aa1eade0041cb48b14c332929b991beb39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(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 (or (= key :tab) (= key :right)) (. boxes self.type))
    (set self.tab (+ (% self.tab (length (. boxes self.type))) 1)))
  (when (and (= key :left) (. boxes self.type))
    (set self.tab (+ (% (- self.tab 2) (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)]
    (when (and (= button 1) (. boxes self.type))
      (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}