module Rattletrap.Type.Keyframe where

import qualified Rattletrap.ByteGet as ByteGet
import qualified Rattletrap.BytePut as BytePut
import qualified Rattletrap.Schema as Schema
import qualified Rattletrap.Type.F32 as F32
import qualified Rattletrap.Type.U32 as U32
import qualified Rattletrap.Utility.Json as Json

data Keyframe = Keyframe
  { -- | When this key frame occurs, in seconds.
    Keyframe -> F32
time :: F32.F32,
    -- | The frame number of this key frame, starting from 0.
    Keyframe -> U32
frame :: U32.U32,
    -- | The bit position of this key frame in the stream.
    Keyframe -> U32
position :: U32.U32
  }
  deriving (Keyframe -> Keyframe -> Bool
(Keyframe -> Keyframe -> Bool)
-> (Keyframe -> Keyframe -> Bool) -> Eq Keyframe
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Keyframe -> Keyframe -> Bool
== :: Keyframe -> Keyframe -> Bool
$c/= :: Keyframe -> Keyframe -> Bool
/= :: Keyframe -> Keyframe -> Bool
Eq, Int -> Keyframe -> ShowS
[Keyframe] -> ShowS
Keyframe -> String
(Int -> Keyframe -> ShowS)
-> (Keyframe -> String) -> ([Keyframe] -> ShowS) -> Show Keyframe
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Keyframe -> ShowS
showsPrec :: Int -> Keyframe -> ShowS
$cshow :: Keyframe -> String
show :: Keyframe -> String
$cshowList :: [Keyframe] -> ShowS
showList :: [Keyframe] -> ShowS
Show)

instance Json.FromJSON Keyframe where
  parseJSON :: Value -> Parser Keyframe
parseJSON = String -> (Object -> Parser Keyframe) -> Value -> Parser Keyframe
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Json.withObject String
"Keyframe" ((Object -> Parser Keyframe) -> Value -> Parser Keyframe)
-> (Object -> Parser Keyframe) -> Value -> Parser Keyframe
forall a b. (a -> b) -> a -> b
$ \Object
object -> do
    F32
time <- Object -> String -> Parser F32
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"time"
    U32
frame <- Object -> String -> Parser U32
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"frame"
    U32
position <- Object -> String -> Parser U32
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"position"
    Keyframe -> Parser Keyframe
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Keyframe {F32
time :: F32
time :: F32
time, U32
frame :: U32
frame :: U32
frame, U32
position :: U32
position :: U32
position}

instance Json.ToJSON Keyframe where
  toJSON :: Keyframe -> Value
toJSON Keyframe
x =
    [(Key, Value)] -> Value
Json.object
      [ String -> F32 -> (Key, Value)
forall value e p.
(ToJSON value, KeyValue e p) =>
String -> value -> p
Json.pair String
"time" (F32 -> (Key, Value)) -> F32 -> (Key, Value)
forall a b. (a -> b) -> a -> b
$ Keyframe -> F32
time Keyframe
x,
        String -> U32 -> (Key, Value)
forall value e p.
(ToJSON value, KeyValue e p) =>
String -> value -> p
Json.pair String
"frame" (U32 -> (Key, Value)) -> U32 -> (Key, Value)
forall a b. (a -> b) -> a -> b
$ Keyframe -> U32
frame Keyframe
x,
        String -> U32 -> (Key, Value)
forall value e p.
(ToJSON value, KeyValue e p) =>
String -> value -> p
Json.pair String
"position" (U32 -> (Key, Value)) -> U32 -> (Key, Value)
forall a b. (a -> b) -> a -> b
$ Keyframe -> U32
position Keyframe
x
      ]

schema :: Schema.Schema
schema :: Schema
schema =
  String -> Value -> Schema
Schema.named String
"keyframe" (Value -> Schema) -> Value -> Schema
forall a b. (a -> b) -> a -> b
$
    [((Key, Value), Bool)] -> Value
Schema.object
      [ (String -> Value -> (Key, Value)
forall value e p.
(ToJSON value, KeyValue e p) =>
String -> value -> p
Json.pair String
"time" (Value -> (Key, Value)) -> Value -> (Key, Value)
forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
F32.schema, Bool
True),
        (String -> Value -> (Key, Value)
forall value e p.
(ToJSON value, KeyValue e p) =>
String -> value -> p
Json.pair String
"frame" (Value -> (Key, Value)) -> Value -> (Key, Value)
forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
U32.schema, Bool
True),
        (String -> Value -> (Key, Value)
forall value e p.
(ToJSON value, KeyValue e p) =>
String -> value -> p
Json.pair String
"position" (Value -> (Key, Value)) -> Value -> (Key, Value)
forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
U32.schema, Bool
True)
      ]

bytePut :: Keyframe -> BytePut.BytePut
bytePut :: Keyframe -> BytePut
bytePut Keyframe
x =
  F32 -> BytePut
F32.bytePut (Keyframe -> F32
time Keyframe
x) BytePut -> BytePut -> BytePut
forall a. Semigroup a => a -> a -> a
<> U32 -> BytePut
U32.bytePut (Keyframe -> U32
frame Keyframe
x) BytePut -> BytePut -> BytePut
forall a. Semigroup a => a -> a -> a
<> U32 -> BytePut
U32.bytePut (Keyframe -> U32
position Keyframe
x)

byteGet :: ByteGet.ByteGet Keyframe
byteGet :: ByteGet Keyframe
byteGet = String -> ByteGet Keyframe -> ByteGet Keyframe
forall a. String -> ByteGet a -> ByteGet a
ByteGet.label String
"Keyframe" (ByteGet Keyframe -> ByteGet Keyframe)
-> ByteGet Keyframe -> ByteGet Keyframe
forall a b. (a -> b) -> a -> b
$ do
  F32
time <- String -> ByteGet F32 -> ByteGet F32
forall a. String -> ByteGet a -> ByteGet a
ByteGet.label String
"time" ByteGet F32
F32.byteGet
  U32
frame <- String -> ByteGet U32 -> ByteGet U32
forall a. String -> ByteGet a -> ByteGet a
ByteGet.label String
"frame" ByteGet U32
U32.byteGet
  U32
position <- String -> ByteGet U32 -> ByteGet U32
forall a. String -> ByteGet a -> ByteGet a
ByteGet.label String
"position" ByteGet U32
U32.byteGet
  Keyframe -> ByteGet Keyframe
forall a. a -> Get ByteString Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Keyframe {F32
time :: F32
time :: F32
time, U32
frame :: U32
frame :: U32
frame, U32
position :: U32
position :: U32
position}