summaryrefslogtreecommitdiff
path: root/fig-frontend-client/src/Main.purs
blob: 871ef8ab08fd6c771abf7eb3401e93b87b620855 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
module Main where

import Prelude

import Config as Config
import Control.Monad.State (class MonadState, get, modify_, put, runStateT)
import Data.Array (concat, cons, delete, filter, foldM, foldr, fromFoldable, head, length, mapWithIndex, null, range, uncons, updateAt, (!!))
import Data.Int (ceil, floor, quot, rem, toNumber)
import Data.Maybe (Maybe(..))
import Data.String.CodeUnits (toCharArray)
import Data.String.CodeUnits as String
import Data.Traversable (for)
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Console (log)
import Effect.Exception (throw)
import Effect.Random (randomInt)
import Effect.Ref (Ref, new, read, write)
import Graphics.Canvas (CanvasElement, Context2D, clearRect, fillRect, fillText, getCanvasElementById, getContext2D, setCanvasDimensions, setFillStyle, setFont)
import Web.Event.Event (EventType(..))
import Web.Event.EventTarget (addEventListener, eventListener)
import Web.HTML (window)
import Web.HTML.Window (Window, innerHeight, innerWidth, open, requestAnimationFrame)
import Web.HTML.Window as Window
import Web.UIEvent.MouseEvent (fromEvent, pageX, pageY)

type Context =
  { window :: Window
  , canvas :: CanvasElement
  , render :: Context2D
  , width :: Number
  , height :: Number
  , cellWidth :: Number
  , cellHeight :: Number
  , widthCells :: Int
  , heightCells :: Int
  , events :: Array Event
  }

lookupPos :: forall m a t. MonadEffect m => Context -> {x :: Int, y :: Int | t} -> Array a -> m a
lookupPos ctx pos a = do
  let idx = pos.y * ctx.widthCells + pos.x
  case a !! idx of
    Nothing -> liftEffect $ throw "index out of bounds"
    Just x -> pure x

updatePos :: forall m a t. MonadEffect m => Context -> {x :: Int, y :: Int | t} -> a -> Array a -> m (Array a)
updatePos ctx pos v a = do
  let idx = pos.y * ctx.widthCells + pos.x
  case updateAt idx v a of
    Nothing -> liftEffect $ throw "index out of bounds"
    Just a' -> pure a'

main :: Effect Unit
main = do
  -- d <- toDocument <$> document w
  -- getElementById "foo" (toNonElementParentNode d) >>= case _ of
  --   Nothing -> log "failed to find foo"
  --   Just e -> do
  --     l <- eventListener \_e ->
  --       log "click"
  --     addEventListener click l false $ toEventTarget e
  log $ Config.apiServer
  w <- window
  newContext >>= case _ of
    Nothing -> log "failed to find canvas"
    Just ictx -> do
      rc <- new ictx
      lresize <- eventListener \_e -> do
        newContext >>= case _ of
          Nothing -> log "failed to find canvas"
          Just newctx -> write newctx rc
      addEventListener (EventType "resize") lresize false $ Window.toEventTarget w
      let lmouse h = eventListener \e -> case fromEvent e of
            Nothing -> pure unit
            Just me -> do
              let px = toNumber $ pageX me
              let py = toNumber $ pageY me
              ctx <- read rc
              write (ctx { events = cons (h (floor $ px / ctx.cellWidth) (floor $ py / ctx.cellHeight)) ctx.events }) rc
      lmouseclick <- lmouse EventMouseClick
      addEventListener (EventType "click") lmouseclick false $ Window.toEventTarget w
      lmousemove <- lmouse EventMouseMove
      addEventListener (EventType "mousemove") lmousemove false $ Window.toEventTarget w
      loop rc initialState
  pure unit

newContext :: Effect (Maybe Context)
newContext = do
  w <- window
  getCanvasElementById "lcolonq-canvas" >>= case _ of
    Nothing -> pure Nothing
    Just canvas -> do
      width <- toNumber <$> innerWidth w
      height <- toNumber <$> innerHeight w
      setCanvasDimensions canvas { width, height }
      render <- getContext2D canvas
      setFont render "bold 0.8vw Iosevka Comfy"
      let widthCells = 200.0
      let cellWidth = toNumber $ ceil $ width / widthCells
      let cellHeight = cellWidth * 2.0
      pure $ Just
        { window: w
        , canvas
        , render
        , width
        , height
        , cellHeight
        , cellWidth
        , widthCells: ceil widthCells
        , heightCells: ceil $ height / cellHeight
        , events: [EventGfx GfxWhiteout]
        }

newtype Cell = Cell
  { fg :: String
  , bg :: String
  , char :: String
  , click :: State -> Effect State
  }

drawCell :: forall t. Context -> State -> Cell -> { x :: Int, y :: Int | t } -> Effect Unit
drawCell ctx st (Cell c) pos = do
  let x = toNumber pos.x * ctx.cellWidth
  let y = toNumber pos.y * ctx.cellHeight
  inv <- lookupPos ctx pos st.inverse
  let fg = if inv /= 0 then c.bg else c.fg
  let bg = if inv /= 0 then c.fg else c.bg
  setFillStyle ctx.render bg
  fillRect ctx.render
    { x
    , y
    , width: ctx.cellWidth
    , height: ctx.cellHeight
    }
  setFillStyle ctx.render fg
  fillText ctx.render c.char (x + ctx.cellWidth / 4.0) (y + ctx.cellHeight / 1.4)

drawCells :: Context -> State -> Array Cell -> Effect Unit
drawCells ctx st cells = do
  void $ for (range 0 ctx.widthCells) \x -> do
    for (range 0 ctx.heightCells) \y -> do
      let pos = { x, y }
      c <- lookupPos ctx pos cells
      drawCell ctx st c pos

type Picker = Context -> Array Cell -> Effect (Maybe (Tuple { x :: Int, y :: Int} Cell))

pickRandom :: Picker
pickRandom ctx cells = do
  idx <- randomInt 0 $ length cells - 1
  case cells !! idx of
    Nothing -> pure Nothing
    Just c ->
      pure
      $ Just
      $ Tuple { x: rem idx ctx.widthCells, y: quot idx ctx.widthCells } c

type Transition =
  { cells :: Array Cell
  , speed :: Int
  , cadence :: Int
  , picker :: Picker
  }

type State =
  { tick :: Int
  , cells :: Array Cell
  , inverse :: Array Int
  , transitions :: Array Transition
  , redraw :: Boolean
  }

initialState :: State
initialState =
  { tick: 0
  , cells: []
  , inverse: []
  , transitions: []
  , redraw: true
  }

data Event
  = EventGfx Gfx
  | EventMouseClick Int Int
  | EventMouseMove Int Int

data Gfx
  = GfxWhiteout

gfxTransitions :: Context -> Gfx -> Array Transition
gfxTransitions ctx GfxWhiteout =
  let
    bg = fromFoldable $ concat $ flip map (range 0 ctx.widthCells) \x ->
      flip map (range 0 ctx.heightCells) \y ->
      Tuple
      { x, y }
      $ Cell
      { bg: "white"
      , fg: "black"
      , click: pure
      , char:
        let base = "LCOLONQ"
        in case String.charAt (rem (x + y) (String.length base)) base of
          Nothing -> "?"
          Just c -> String.singleton c
      }
    link :: Int -> String -> String -> String -> String -> Array Cell
    link y fgc bgc str url =
      fromFoldable $ mapWithIndex
      (\i c ->
        Tuple { x: i, y }
        $ Cell
        { bg: bgc, fg: fgc, char: String.singleton c
        , click: \st -> do
          void $ open url "_blank" "" ctx.window
          pure st
        }
      )
      $ toCharArray str
    linkRight :: Int -> String -> String -> String -> String -> Array Cell
    linkRight y fgc bgc str url =
      fromFoldable $ mapWithIndex
      (\i c ->
        Tuple { x: (ctx.widthCells - String.length str - 8) + i, y }
        $ Cell
        { bg: bgc, fg: fgc, char: String.singleton c
        , click: \st -> do
          void $ open url "_blank" "" ctx.window
          pure st
        }
      )
      $ toCharArray str
    -- fg =
    --   unions
    --   [ link 0 "purple" "white" "twitch.tv/lcolonq" "https://twitch.tv/lcolonq"
    --   , link 1 "blue" "white" "twitter.com/lcolonq" "https://twitter.com/lcolonq"
    --   , link (ctx.heightCells - 1) "white" "black" "the previous one" "https://pub.colonq.computer/~llll/cgi-bin/ring?me=llll&offset=-1"
    --   , linkRight (ctx.heightCells - 1) "white" "black" "the next one" "https://pub.colonq.computer/~llll/cgi-bin/ring?me=llll&offset=1"
    --   ]
    -- cells = union fg bg
    cells = bg
  in
   [ { cells
     , speed: 20
     , cadence: 1
     , picker: pickRandom
     }
   ]

tick :: forall m. MonadState State m => m Unit
tick = modify_ \st ->
  st
  { tick = st.tick + 1
  , redraw = false
  , transitions = case uncons st.transitions of
    Nothing -> st.transitions
    Just { head, tail } -> if null head.cells then tail else st.transitions
  , inverse = filter (st.tick <= _) st.inverse
  }

pullEvents :: forall m. MonadState State m => MonadEffect m => Ref Context -> m Unit
pullEvents rc = do
  ctx <- liftEffect $ read rc
  st <- get
  st' <- case uncons ctx.events of
    Nothing -> pure st
    Just { head, tail } -> do
      liftEffect $ write (ctx { events = tail }) rc
      case head of
        EventGfx gfx -> pure $ st { transitions = st.transitions <> gfxTransitions ctx gfx }
        EventMouseClick mx my ->
          lookupPos ctx { x: mx, y: my } st.cells >>= case _ of
            Nothing -> pure st
            Just (Cell c) -> liftEffect $ c.click st
        EventMouseMove mx my -> do
          inv <- foldM (\inv pos -> updatePos ctx pos (st.tick + 30) inv) st.inverse $ map (\x -> map (\y -> { x, y }) (range (my - 1) (my + 1))) (range (mx - 1) (mx + 1))
          pure st { inverse = inv }
  put st'

pickCell :: forall m. MonadState State m => MonadEffect m => Context -> m Unit
pickCell ctx = do
  st <- get
  case uncons st.transitions of
    Nothing -> pure unit
    Just { head: trans, tail } ->
      liftEffect (trans.picker trans.cells) >>= case _ of
        Nothing -> pure unit
        Just (Tuple pk pv) -> do
          cells <- updatePos ctx pk pv st.cells
          put st { redraw = true, cells = cells, transitions = cons (trans { cells = (delete pk trans.cells) }) tail }

loop :: Ref Context -> State -> Effect Unit
loop rc st = do
  ctx <- read rc
  -- render
  when st.redraw do
    clearRect ctx.render { x: 0.0, y: 0.0, width: ctx.width, height: ctx.height }
    drawCells ctx st st.cells
  -- update
  -- Tuple _ st' <- flip runStateT st do
  --   tick
  --   pullEvents rc
  --   case head st.transitions of
  --     Nothing -> pure unit
  --     Just trans -> do
  --       when (rem st.tick trans.cadence == 0) do
  --         void $ for (range 0 trans.speed) \_ -> pickCell
  -- void $ requestAnimationFrame (loop rc st') ctx.window
  void $ requestAnimationFrame (loop rc st) ctx.window