add load functionality (but note that saving is still hardcoded right now)

This commit is contained in:
2024-09-29 22:26:07 +01:00
parent 249247b704
commit 13682f1c6c
5 changed files with 53 additions and 21 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -364,9 +364,9 @@ struct
(model, fileMsg)
end
fun useTriangles (model, triangles) =
fun useTrianglesInNormalMode (model, triangles) =
let
val model = AppWith.useTriangles (model, triangles)
val model = AppWith.useTrianglesAndSetNormalMode (model, triangles)
val drawVec = Triangles.toVector model
val drawMsg = DRAW_TRIANGLES_AND_RESET_DOTS drawVec
val drawMsg = [DRAW drawMsg]
@@ -413,7 +413,7 @@ struct
| ARROW_DOWN => moveArrowDown model
| KEY_ENTER => enterOrSpaceCoordinates model
| KEY_SPACE => enterOrSpaceCoordinates model
| USE_TRIANGLES triangles => useTriangles (model, triangles)
| USE_TRIANGLES triangles => useTrianglesInNormalMode (model, triangles)
| TRIANGLES_LOAD_ERROR => trianglesLoadError model
| FILE_BROWSER_AND_PATH {fileBrowser, path} =>
handleFileBrowserAndPathInNormalMode (model, fileBrowser, path)
@@ -538,6 +538,25 @@ struct
redrawFileBrowser model
end
fun selectCurrentFileItem model =
let
val {fileBrowser, fileBrowserIdx, openFilePath, ...} = model
in
if Vector.length fileBrowser > 0 then
let
val path =
case Vector.sub (fileBrowser, fileBrowserIdx) of
IS_FILE str => str
| IS_FOLDER str => str
val path = String.concat [openFilePath, "/", path]
val fileMsg = SELECT_PATH path
in
(model, [FILE fileMsg])
end
else
(model, [])
end
fun updateBrowseMode (model: app_type, inputMsg) =
case inputMsg of
ARROW_UP => browseModeArrowUp model
@@ -545,12 +564,13 @@ struct
| TRIANGLES_LOAD_ERROR => trianglesLoadError model
(* todo:
| ARROW_LEFT =>
| ARROW_RIGHT =>
| KEY_ENTER =>
| KEY_SPACE =>
*)
| ARROW_RIGHT => selectCurrentFileItem model
| KEY_ENTER => selectCurrentFileItem model
| KEY_SPACE => selectCurrentFileItem model
| FILE_BROWSER_AND_PATH {fileBrowser, path} =>
handleFileBrowserAndPathInBrowseMode (model, fileBrowser, path)
| USE_TRIANGLES triangles => useTrianglesInNormalMode (model, triangles)
| _ => (model, [])
fun update (model: app_type, inputMsg) =

View File

@@ -58,7 +58,8 @@ sig
* int
-> AppType.app_type
val useTriangles: AppType.app_type * AppType.triangle list -> AppType.app_type
val useTrianglesAndSetNormalMode: AppType.app_type * AppType.triangle list
-> AppType.app_type
end
structure AppWith :> APP_WITH =
@@ -559,10 +560,10 @@ struct
}
end
fun useTriangles (app: app_type, triangles) =
fun useTrianglesAndSetNormalMode (app: app_type, triangles) =
let
val
{ mode
{ mode = _
, xClickPoints
, yClickPoints
, numClickPoints
@@ -584,7 +585,7 @@ struct
val triangleStage = NO_TRIANGLE
in
{ mode = mode
{ mode = AppType.NORMAL_MODE
, triangleStage = triangleStage
, triangles = triangles
, undo = []

View File

@@ -117,9 +117,9 @@ struct
end
| NONE => let val triangles = List.rev acc in OK triangles end
fun loadTriangles inputMailbox =
fun loadTriangles (path, inputMailbox) =
let
val io = TextIO.openIn filename
val io = TextIO.openIn path
val triangles = parse (io, [])
val _ = TextIO.closeIn io
@@ -168,36 +168,45 @@ struct
()
end
fun getDirList (dir, acc) =
fun getDirList (dir, acc, rootPath) =
case OS.FileSys.readDir dir of
SOME path =>
if OS.FileSys.isDir path then
getDirList (dir, AppType.IS_FOLDER path :: acc)
else if OS.FileSys.isLink path then
getDirList (dir, acc)
else
getDirList (dir, AppType.IS_FILE path :: acc)
let
val folderPath = String.concat [rootPath, "/", path]
in
if OS.FileSys.isDir folderPath then
getDirList (dir, AppType.IS_FOLDER path :: acc, rootPath)
else if OS.FileSys.isLink folderPath then
getDirList (dir, acc, rootPath)
else
getDirList (dir, AppType.IS_FILE path :: acc, rootPath)
end
| NONE => let val acc = List.rev acc in Vector.fromList acc end
fun loadFiles (path, inputMailbox) =
let
val path = if String.size path = 0 then OS.FileSys.getDir () else path
val dir = OS.FileSys.openDir path
val dirList = getDirList (dir, [])
val dirList = getDirList (dir, [], path)
val _ = OS.FileSys.closeDir dir
val inputMsg = FILE_BROWSER_AND_PATH {fileBrowser = dirList, path = path}
in
Mailbox.send (inputMailbox, inputMsg)
end
fun selectPath (path, inputMailbox) =
if OS.FileSys.isDir path then loadFiles (path, inputMailbox)
else loadTriangles (path, inputMailbox)
fun run (fileMailbox, inputMailbox) =
let
val _ =
case Mailbox.recv fileMailbox of
SAVE_TRIANGLES triangles => saveTriangles triangles
| LOAD_TRIANGLES => loadTriangles inputMailbox
| LOAD_TRIANGLES => loadTriangles (filename, inputMailbox)
| EXPORT_TRIANGLES triangles => exportTriangles triangles
| LOAD_FILES path => loadFiles (path, inputMailbox)
| SELECT_PATH path => selectPath (path, inputMailbox)
in
run (fileMailbox, inputMailbox)
end

View File

@@ -5,6 +5,7 @@ sig
| LOAD_TRIANGLES
| EXPORT_TRIANGLES of AppType.triangle list
| LOAD_FILES of string
| SELECT_PATH of string
end
structure FileMessage :> FILE_MESSAGE =
@@ -14,4 +15,5 @@ struct
| LOAD_TRIANGLES
| EXPORT_TRIANGLES of AppType.triangle list
| LOAD_FILES of string
| SELECT_PATH of string
end