done with functionality relating to performing file operations on terminal
This commit is contained in:
@@ -38,6 +38,7 @@ message-types/file-msg.sml
|
|||||||
message-types/input-msg.sml
|
message-types/input-msg.sml
|
||||||
message-types/update-msg.sml
|
message-types/update-msg.sml
|
||||||
|
|
||||||
|
fcore/file-string.sml
|
||||||
fcore/quad-tree.sml
|
fcore/quad-tree.sml
|
||||||
|
|
||||||
fcore/common-update.sml
|
fcore/common-update.sml
|
||||||
|
|||||||
@@ -48,34 +48,37 @@ struct
|
|||||||
|
|
||||||
fun getSaveSquaresMsg (model: app_type) =
|
fun getSaveSquaresMsg (model: app_type) =
|
||||||
let
|
let
|
||||||
val {layerTree, canvasWidth, canvasHeight, ...} = model
|
val {layerTree, canvasWidth, canvasHeight, openFilePath, ...} = model
|
||||||
val str =
|
val saveString =
|
||||||
CollisionTree.toSaveString (layerTree, canvasWidth, canvasHeight)
|
CollisionTree.toSaveString (layerTree, canvasWidth, canvasHeight)
|
||||||
val msg = SAVE_SQUARES str
|
val msg = SAVE_SQUARES {output = saveString, filepath = openFilePath}
|
||||||
in
|
in
|
||||||
(model, [FILE msg])
|
(model, [FILE msg])
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getLoadSquaresMsg model =
|
fun getLoadSquaresMsg (model: app_type) =
|
||||||
(model, [FILE LOAD_SQUARES])
|
let val msg = LOAD_SQUARES {filepath = #openFilePath model}
|
||||||
|
in (model, [FILE msg])
|
||||||
|
end
|
||||||
|
|
||||||
fun getExportSquaresMsg (model: app_type) =
|
fun getExportSquaresMsg (model: app_type) =
|
||||||
let
|
let
|
||||||
val {layerTree, canvasWidth, canvasHeight, ...} = model
|
val {layerTree, canvasWidth, canvasHeight, openFilePath, ...} = model
|
||||||
|
|
||||||
val maxSide = Int.max (canvasWidth, canvasHeight)
|
val maxSide = Int.max (canvasWidth, canvasHeight)
|
||||||
val squares = LayerTree.flatten (maxSide, layerTree)
|
val squares = LayerTree.flatten (maxSide, layerTree)
|
||||||
|
|
||||||
val exportString =
|
val exportString =
|
||||||
CollisionTree.toExportString (squares, canvasWidth, canvasHeight)
|
CollisionTree.toExportString (squares, canvasWidth, canvasHeight)
|
||||||
val msg = EXPORT_SQUARES exportString
|
val msg = EXPORT_SQUARES {output = exportString, filepath = openFilePath}
|
||||||
in
|
in
|
||||||
(model, [FILE msg])
|
(model, [FILE msg])
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getCollisionMsg (model: app_type) =
|
fun getCollisionMsg (model: app_type) =
|
||||||
let
|
let
|
||||||
val {layerTree, canvasWidth, canvasHeight, modalNum, ...} = model
|
val {layerTree, canvasWidth, canvasHeight, modalNum, openFilePath, ...} =
|
||||||
|
model
|
||||||
|
|
||||||
val maxSide = Int.max (canvasWidth, canvasHeight)
|
val maxSide = Int.max (canvasWidth, canvasHeight)
|
||||||
val squares = LayerTree.flatten (maxSide, layerTree)
|
val squares = LayerTree.flatten (maxSide, layerTree)
|
||||||
@@ -83,7 +86,10 @@ struct
|
|||||||
val exportString =
|
val exportString =
|
||||||
CollisionTree.toCollisionString
|
CollisionTree.toCollisionString
|
||||||
(squares, canvasWidth, canvasHeight, modalNum)
|
(squares, canvasWidth, canvasHeight, modalNum)
|
||||||
val msg = EXPORT_COLLISIONS exportString
|
|
||||||
|
val exportFilePath = FileString.getCollisionFilename openFilePath
|
||||||
|
val msg =
|
||||||
|
EXPORT_COLLISIONS {output = exportString, filepath = exportFilePath}
|
||||||
|
|
||||||
val model = AppWith.modalNum (model, 0)
|
val model = AppWith.modalNum (model, 0)
|
||||||
in
|
in
|
||||||
|
|||||||
59
fcore/file-string.sml
Normal file
59
fcore/file-string.sml
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
structure FileString =
|
||||||
|
struct
|
||||||
|
fun findLastChr (str, pos, findChr) =
|
||||||
|
if pos < 0 then ~1
|
||||||
|
else if String.sub (str, pos) = findChr then pos
|
||||||
|
else findLastChr (str, pos - 1, findChr)
|
||||||
|
|
||||||
|
fun extractFileName str =
|
||||||
|
let
|
||||||
|
val lastSlash = findLastChr (str, String.size str - 1, #"/")
|
||||||
|
val strStart = lastSlash + 1
|
||||||
|
in
|
||||||
|
if lastSlash = ~1 then str
|
||||||
|
else String.substring (str, strStart, String.size str - strStart)
|
||||||
|
end
|
||||||
|
|
||||||
|
fun removeFileExtension str =
|
||||||
|
let val lastDot = findLastChr (str, String.size str - 1, #".")
|
||||||
|
in if lastDot = ~1 then str else String.substring (str, 0, lastDot)
|
||||||
|
end
|
||||||
|
|
||||||
|
local
|
||||||
|
fun finish acc =
|
||||||
|
let val acc = List.rev acc
|
||||||
|
in String.implode acc
|
||||||
|
end
|
||||||
|
|
||||||
|
(* convert from kebab-case or snake_case to PascalCase *)
|
||||||
|
fun loop (#"-" :: chr :: tl, acc) =
|
||||||
|
let val acc = Char.toUpper chr :: acc
|
||||||
|
in loop (tl, acc)
|
||||||
|
end
|
||||||
|
| loop (#"_" :: chr :: tl, acc) =
|
||||||
|
let val acc = Char.toUpper chr :: acc
|
||||||
|
in loop (tl, acc)
|
||||||
|
end
|
||||||
|
| loop ([#"-"], acc) = finish acc
|
||||||
|
| loop ([#"_"], acc) = finish acc
|
||||||
|
| loop (chr :: tl, acc) =
|
||||||
|
loop (tl, chr :: acc)
|
||||||
|
| loop ([], acc) = finish acc
|
||||||
|
in
|
||||||
|
fun filenameToStructureName str =
|
||||||
|
let
|
||||||
|
val str = removeFileExtension str
|
||||||
|
val str = extractFileName str
|
||||||
|
in
|
||||||
|
(* capitalise first character in string *)
|
||||||
|
case String.explode str of
|
||||||
|
chr :: tl => let val chr = Char.toUpper chr in loop (tl, [chr]) end
|
||||||
|
| [] => ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fun getCollisionFilename str =
|
||||||
|
let val str = removeFileExtension str
|
||||||
|
in str ^ "-collisions.sml"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,57 +1,5 @@
|
|||||||
structure CollisionTree =
|
structure CollisionTree =
|
||||||
struct
|
struct
|
||||||
fun findLastChr (str, pos, findChr) =
|
|
||||||
if pos < 0 then ~1
|
|
||||||
else if String.sub (str, pos) = findChr then pos
|
|
||||||
else findLastChr (str, pos - 1, findChr)
|
|
||||||
|
|
||||||
fun extractFileName str =
|
|
||||||
let
|
|
||||||
val lastSlash = findLastChr (str, String.size str - 1, #"/")
|
|
||||||
val strStart = lastSlash + 1
|
|
||||||
in
|
|
||||||
if lastSlash = ~1 then str
|
|
||||||
else String.substring (str, strStart, String.size str - strStart)
|
|
||||||
end
|
|
||||||
|
|
||||||
local
|
|
||||||
fun removeFileExtension str =
|
|
||||||
let val lastDot = findLastChr (str, String.size str - 1, #".")
|
|
||||||
in if lastDot = ~1 then str else String.substring (str, 0, lastDot)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun finish acc =
|
|
||||||
let val acc = List.rev acc
|
|
||||||
in String.implode acc
|
|
||||||
end
|
|
||||||
|
|
||||||
(* convert from kebab-case or snake_case to PascalCase *)
|
|
||||||
fun loop (#"-" :: chr :: tl, acc) =
|
|
||||||
let val acc = Char.toUpper chr :: acc
|
|
||||||
in loop (tl, acc)
|
|
||||||
end
|
|
||||||
| loop (#"_" :: chr :: tl, acc) =
|
|
||||||
let val acc = Char.toUpper chr :: acc
|
|
||||||
in loop (tl, acc)
|
|
||||||
end
|
|
||||||
| loop ([#"-"], acc) = finish acc
|
|
||||||
| loop ([#"_"], acc) = finish acc
|
|
||||||
| loop (chr :: tl, acc) =
|
|
||||||
loop (tl, chr :: acc)
|
|
||||||
| loop ([], acc) = finish acc
|
|
||||||
in
|
|
||||||
fun filenameToStructureName str =
|
|
||||||
let
|
|
||||||
val str = removeFileExtension str
|
|
||||||
val str = extractFileName str
|
|
||||||
in
|
|
||||||
(* capitalise first character in string *)
|
|
||||||
case String.explode str of
|
|
||||||
chr :: tl => let val chr = Char.toUpper chr in loop (tl, [chr]) end
|
|
||||||
| [] => ""
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
structure BinTree =
|
structure BinTree =
|
||||||
struct
|
struct
|
||||||
datatype 'a bintree =
|
datatype 'a bintree =
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ struct
|
|||||||
let
|
let
|
||||||
val _ =
|
val _ =
|
||||||
case Mailbox.recv fileMailbox of
|
case Mailbox.recv fileMailbox of
|
||||||
SAVE_SQUARES str => saveString (filename, str)
|
SAVE_SQUARES {filepath, output} => saveString (filepath, output)
|
||||||
| EXPORT_SQUARES str => saveString (exportFilename, str)
|
| EXPORT_SQUARES {filepath, output} => saveString (filepath, output)
|
||||||
| EXPORT_COLLISIONS str => saveString (collisionFilename, str)
|
| EXPORT_COLLISIONS {filepath, output} => saveString (filepath, output)
|
||||||
| LOAD_SQUARES => loadSquares (filename, inputMailbox)
|
| LOAD_SQUARES filename => loadSquares (filename, inputMailbox)
|
||||||
in
|
in
|
||||||
run (fileMailbox, inputMailbox)
|
run (fileMailbox, inputMailbox)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
structure FileMessage =
|
structure FileMessage =
|
||||||
struct
|
struct
|
||||||
datatype t =
|
datatype t =
|
||||||
SAVE_SQUARES of string
|
SAVE_SQUARES of {output: string, filepath: string}
|
||||||
| LOAD_SQUARES
|
| LOAD_SQUARES of {filepath: string}
|
||||||
| EXPORT_SQUARES of string
|
| EXPORT_SQUARES of {output: string, filepath: string}
|
||||||
| EXPORT_COLLISIONS of string
|
| EXPORT_COLLISIONS of {output: string, filepath: string}
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user