Skip to content

Instantly share code, notes, and snippets.

@aavogt
Created May 15, 2024 14:11
Show Gist options
  • Save aavogt/6fa6ad4c645a1dc1a2ff59e806ca86bd to your computer and use it in GitHub Desktop.
Save aavogt/6fa6ad4c645a1dc1a2ff59e806ca86bd to your computer and use it in GitHub Desktop.
use `rapid` to reload while keeping the h-raylib window open
{-# NOINLINE runLoop #-}
runLoop :: IORef Bool
runLoop = unsafePerformIO (newIORef True)
main = rapid 0 \ k -> do
HGCode{..} <- return $ HGCode { input = "CE5S1_hex_grid.gcode" &= typ "INPUT" &= argPos 0, output = def &= typ "OUTPUT" &= argPos 1 }
lines <- createRef k "lines" $ runResourceT $ runConduit $ sourceFile input
.| decodeUtf8C
.| parseGcodeC @Pico
.| concatMapC (either (const Nothing) Just)
.| scanlC (\(_, b) a -> (a, annot1 a b)) (undefined, mempty)
.| do dropC 100; takeC 200
.| slidingWindowC @_ @[(GCodeLine Pico, SMS)] 2
-- .| concatMapAccumC findConfinement (Kd.empty (toListOf traverse) :< Empty)
.| sinkList
let ls :: [(V3 Float, V3 Float, Double)]
ls = getSegs lines
lsA, lsB, lsC :: V3 Float
(lsA, lsC) = segBB ls
lsB = lerp 0.5 lsA lsC
camp <- createRef k "camp" $ newIORef (lsB + (lsC - lsA) / 2)
camt <- createRef k "camt" $ newIORef lsB
camo <- createRef k "camo" $ newIORef (V3 0 0 1)
camv <- createRef k "camv" $ newIORef 65
let cam = do
return $ Camera3D *camp *camt *camo *camv CameraOrthographic
render :: IORef (IO ()) <- createRef k "render" (newIORef (return ()))
writeIORef render $ void $ do
-- pan
when !(isMouseButtonDown MouseButtonRight) do
dx <- targetPlaneMouseDrag !cam <&> uncurry subtract
unless (any isNaN dx) do
modifyIORef camp (+ dx)
modifyIORef camt (+ dx)
-- when it's zoomed in this doesn't rotate enough
when !(isMouseButtonDown MouseButtonLeft) do
(x,x') <- targetPlaneMouseDrag !cam
let p = *camp
let t = *camt
let r = rotationFromTo (x' - p) (x - p)
unless (any isNaN r) $ writeIORef camp $ (r `rotate` (p - t)) + t
-- zoom
do mw <- getMouseWheelMove
when (mw /= 0) $ modifyIORef camv (* (1 - mw/8))
drawing do
clearBackground Colors.black
mode3D !cam do
for_ (take 80 ls) $ \ (from, to, extruded) -> drawLine3D from to Colors.yellow
drawCubeV lsB (lsC - lsA) Colors.red{ color'a = 80 }
startWith asyncBound k "window" $ withWindow 640 480 "hgcode" 10 \ wr -> do
whileM_ (do return (*runLoop && not !windowShouldClose))
(join (readIORef render))
@s-and-witch
Copy link

Whoa! Didn't know about monadic-bang, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment