{-# OPTIONS_GHC -optc-D_LARGEFILE_SOURCE #-}
{-# OPTIONS_GHC -optc-D_LARGEFILE64_SOURCE #-}
{-# OPTIONS_GHC -optc-D_THREAD_SAFE #-}
{-# OPTIONS_GHC -optc-D_REENTRANT #-}
{-# OPTIONS_GHC -optc-DBUILD_DIR="build/debug" #-}
{-# OPTIONS_GHC -optc-DGHC_VERSION=90205 #-}
{-# OPTIONS_GHC -optc-D__APPLE__ #-}
{-# LINE 1 "Ui/Zoom.hsc" #-}
-- Copyright 2013 Evan Laforge
-- This program is distributed under the terms of the GNU General Public
-- License 3.0, see COPYING or http://www.gnu.org/licenses/gpl-3.0.txt

-- | The 'Zoom' type.
module Ui.Zoom (Zoom(..), to_pixels, to_time) where
import ForeignC
import qualified Util.FFI as FFI
import qualified Util.Num as Num

import qualified Ui.ScoreTime as ScoreTime
import Ui.ScoreTime (TrackTime)

import Global




-- | View zoom and time scroll offset.
data Zoom = Zoom {
    offset :: !TrackTime
    , factor :: !Double
    } deriving (Eq, Ord, Show, Read)

instance Pretty Zoom where
    pretty (Zoom offset factor) =
        "+" <> pretty offset <> "*" <> Num.showFloat 1 factor

instance CStorable Zoom where
    sizeOf _ = (16)
{-# LINE 31 "Ui/Zoom.hsc" #-}
    alignment _ = alignment (0 :: CDouble)
    peek zoomp = do
        offset <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) zoomp
{-# LINE 34 "Ui/Zoom.hsc" #-}
        factor <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) zoomp :: IO CDouble
{-# LINE 35 "Ui/Zoom.hsc" #-}
        return $ Zoom offset (FFI.hs_double factor)
    poke zoomp (Zoom offset factor) = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) zoomp offset
{-# LINE 38 "Ui/Zoom.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) zoomp (FFI.c_double factor)
{-# LINE 39 "Ui/Zoom.hsc" #-}

-- | Convert a position at a given zoom factor to a pixel position.  Doesn't
-- take the zoom offset into account.
to_pixels :: Zoom -> TrackTime -> Int
to_pixels zoom pos = Num.d2i $ ScoreTime.to_double pos * factor zoom

-- | Convert a pixel position to a TrackTime at the given zoom factor.
-- Doesn't take the zoom offset into account.
to_time :: Zoom -> Int -> TrackTime
to_time zoom pixels = ScoreTime.from_double (fromIntegral pixels / factor zoom)