Skip to content

Commit

Permalink
Add support for selecting several files
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Nov 23, 2024
1 parent 16a4391 commit a24f93b
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 264 deletions.
12 changes: 2 additions & 10 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,15 @@ execWithArgs config cliArgs = do

when (args `isPresent` command "fastfix") $ do
let files = args `getAllArgs` argument "file"

filesAbs <- files & P.mapM makeAbsolute

let
file = case filesAbs of
[x] -> x
x : _ -> x
_ -> "This branch should not be reachable"

loadAndStart (config{transformAppFlag = Hip}) (Just file)
loadAndStart (config{transformAppFlag = Hip}) (Just filesAbs)

when (args `isPresent` command "fix") $ do
let files = args `getAllArgs` argument "file"

filesAbs <- files & P.mapM makeAbsolute

filesAbs <&> Just & P.mapM_ (loadAndStart config)
loadAndStart config (Just filesAbs)

when (args `isPresent` command "rename") $ do
directory <- args `getArgOrExit` argument "directory"
Expand Down
24 changes: 12 additions & 12 deletions source/Home.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Protolude (
Maybe (Just, Nothing),
Num,
putText,
(<&>),
)

import Brillo.Interface.IO.Game as Gl (
Expand All @@ -20,7 +21,7 @@ import Brillo.Interface.IO.Game as Gl (
import Data.Text qualified as T

import TinyFileDialogs (openFileDialog)
import Types (AppState (..), View (..))
import Types (AppState (..), ImageData (..), View (ImageView))
import Utils (isInRect, loadFileIntoState)


Expand All @@ -46,19 +47,18 @@ handleMsg msg appState =
{- Default path -} "/"
{- File patterns -} ["*.jpeg", ".jpg", ".png"]
{- Filter description -} "Image files"
{- Allow multiple selects -} False
{- Allow multiple selects -} True

case selectedFiles of
Just [filePath] -> do
loadFileIntoState
appState{currentView = ImageView}
(T.unpack filePath)
Just _files -> do
putText "Selecting several files is not supported yet!"
-- TODO
-- putText $ "Selected file: " <> filePath
-- loadAndStart appState filePath
pure appState
Just files -> do
let newState =
appState
{ currentView = ImageView
, images =
files <&> \filePath ->
ImageToLoad{filePath = T.unpack filePath}
}
loadFileIntoState newState
Nothing -> do
putText "No file selected"
pure appState
Expand Down
222 changes: 123 additions & 99 deletions source/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import Types (
Corner,
CornersTup,
ExportMode (..),
ImageData (..),
ProjMap,
TransformApp (..),
UiComponent (Button, Select, text),
Expand Down Expand Up @@ -171,30 +172,29 @@ bannerImage =

appStateToWindow :: (Int, Int) -> AppState -> Display
appStateToWindow screenSize appState = do
let appSize = (appState.appWidth, appState.appHeight)

case appState.currentView of
HomeView -> do
InWindow
"Perspec - Select a file"
appSize
(calcInitWindowPos screenSize appSize)
ImageView -> do
case appState.inputPath of
Nothing -> InWindow "SHOULD NOT BE POSSIBLE" (100, 100) (0, 0)
Just inPath ->
let
appSize = (appState.appWidth, appState.appHeight)
windowPos = calcInitWindowPos screenSize appSize

case appState.images of
[] -> InWindow "Perspec" appSize (0, 0)
image : _otherImages -> do
case appState.currentView of
HomeView -> InWindow "Perspec - Select a file" appSize windowPos
ImageView -> do
InWindow
( "Perspec - "
<> inPath
<> case image of
ImageToLoad{filePath} -> filePath
ImageData{inputPath} -> inputPath
<> if appState.isRegistered
then mempty
else " - ⚠️ NOT REGISTERED"
)
appSize
(calcInitWindowPos screenSize appSize)
BannerView ->
InWindow "Perspec - Banner" (800, 600) (10, 10)

windowPos
BannerView ->
InWindow "Perspec - Banner" (800, 600) (10, 10)


stepWorld :: Float -> AppState -> IO AppState
Expand Down Expand Up @@ -326,50 +326,59 @@ makePicture appState =
]
pure uiElements
ImageView -> do
let
appWidthInteg = fromIntegral appState.appWidth
sidebarWidthInteg = fromIntegral appState.sidebarWidth

pure $
Pictures $
( ( [ Scale
appState.scaleFactor
appState.scaleFactor
appState.image
, appState.corners & drawEdges
, appState.corners & drawGrid
]
<> (appState.corners <&> drawCorner)
)
<&> Translate (-(sidebarWidthInteg / 2.0)) 0
)
<> [ drawSidebar
appWidthInteg
appState.appHeight
appState.sidebarWidth
]
<> P.zipWith (drawUiComponent appState) appState.uiComponents [0 ..]
<> [ if appState.bannerIsVisible
then Scale 0.5 0.5 bannerImage
else mempty
, if appState.bannerIsVisible
then
Translate 300 (-250) $
Scale 0.2 0.2 $
ThickArc
0 -- Start angle
-- End angle
( ( fromIntegral appState.tickCounter
/ (bannerTime * fromIntegral ticksPerSecond)
)
* 360
)
50 -- Radius
100 -- Thickness
-- \$
-- -
else mempty
]
case appState.images of
[] -> pure mempty
image : _otherImages -> do
let
appWidthInteg = fromIntegral appState.appWidth
sidebarWidthInteg = fromIntegral appState.sidebarWidth

pure $
Pictures $
( ( [ Scale
appState.scaleFactor
appState.scaleFactor
( case image of
ImageToLoad{} -> mempty
ImageData{content} -> content
)
, appState.corners & drawEdges
, appState.corners & drawGrid
]
<> (appState.corners <&> drawCorner)
)
<&> Translate (-(sidebarWidthInteg / 2.0)) 0
)
<> [ drawSidebar
appWidthInteg
appState.appHeight
appState.sidebarWidth
]
<> P.zipWith
(drawUiComponent appState)
appState.uiComponents
[0 ..]
<> [ if appState.bannerIsVisible
then Scale 0.5 0.5 bannerImage
else mempty
, if appState.bannerIsVisible
then
Translate 300 (-250) $
Scale 0.2 0.2 $
ThickArc
0 -- Start angle
-- End angle
( ( fromIntegral appState.tickCounter
/ (bannerTime * fromIntegral ticksPerSecond)
)
* 360
)
50 -- Radius
100 -- Thickness
-- \$
-- -
else mempty
]
BannerView -> pure $ Pictures []


Expand Down Expand Up @@ -489,27 +498,28 @@ checkSidebarRectHit

submitSelection :: AppState -> ExportMode -> IO AppState
submitSelection appState exportMode = do
let
cornersTrans = getCorners appState
cornerTuple =
fromRight
((0, 0), (0, 0), (0, 0), (0, 0))
(toQuadTuple cornersTrans)
targetShape = getTargetShape cornerTuple
projectionMapNorm =
toCounterClock $
getProjectionMap cornerTuple targetShape

putText $ "Target shape: " <> show targetShape
putText $ "Marked corners: " <> show cornerTuple

case (appState.inputPath, appState.outputPath) of
(Just inputPath, Just outputPath) -> do
case appState.images of
[] -> pure appState
image : otherImages -> do
let
cornersTrans = getCorners appState
cornerTuple =
fromRight
((0, 0), (0, 0), (0, 0), (0, 0))
(toQuadTuple cornersTrans)
targetShape = getTargetShape cornerTuple
projectionMapNorm =
toCounterClock $
getProjectionMap cornerTuple targetShape

putText $ "Target shape: " <> show targetShape
putText $ "Marked corners: " <> show cornerTuple

let
convertArgs =
getConvertArgs
inputPath
outputPath
image.inputPath
image.outputPath
projectionMapNorm
targetShape
exportMode
Expand All @@ -519,19 +529,19 @@ submitSelection appState exportMode = do
putText $
"Arguments for convert command:\n"
<> T.unlines convertArgs
else putText $ "Write file to " <> show appState.outputPath
else putText $ "Write file to " <> show image.outputPath

correctAndWrite
appState.transformApp
inputPath
outputPath
image.inputPath
image.outputPath
projectionMapNorm
exportMode
convertArgs

exitSuccess
(_, _) -> do
P.die "Input path and output path must be set before submitting"
if P.null otherImages
then exitSuccess
else loadFileIntoState appState{images = otherImages}


handleImageViewEvent :: Event -> AppState -> IO AppState
Expand Down Expand Up @@ -786,8 +796,8 @@ correctAndWrite transformApp inPath outPath ((bl, _), (tl, _), (tr, _), (br, _))
pure ()


loadAndStart :: Config -> Maybe FilePath -> IO ()
loadAndStart config filePathMb = do
loadAndStart :: Config -> Maybe [FilePath] -> IO ()
loadAndStart config filePathsMb = do
let
isRegistered = True -- (config&licenseKey) `elem` licenses
stateDraft =
Expand All @@ -799,20 +809,34 @@ loadAndStart config filePathMb = do

screenSize <- getScreenSize

appState <- case filePathMb of
Nothing -> pure stateDraft
Just filePath -> loadFileIntoState stateDraft filePath

putText "Starting the app …"

playIO
(appStateToWindow screenSize appState)
black
ticksPerSecond
appState
makePicture
handleEvent
stepWorld
case filePathsMb of
Nothing -> do
playIO
(appStateToWindow screenSize stateDraft)
black
ticksPerSecond
stateDraft
makePicture
handleEvent
stepWorld
Just filePaths -> do
let
images =
filePaths <&> \filePath ->
ImageToLoad{filePath = filePath}

appState <- loadFileIntoState stateDraft{images = images}

playIO
(appStateToWindow screenSize appState)
black
ticksPerSecond
appState
makePicture
handleEvent
stepWorld


helpMessage :: Text
Expand Down
Loading

0 comments on commit a24f93b

Please sign in to comment.