[ascii] parse old Surface format

This commit is contained in:
2021-04-25 03:08:12 -06:00
parent 1157294733
commit 7eafa5fd85
3 changed files with 56 additions and 2 deletions

View File

@@ -1,4 +1,7 @@
(defnew [_width _height :T defaultValue]
[:Int width _width
:Int height _height
:Array<Array<T>> rows (for _ (range height) (for _ (range width) defaultValue))])
:Array<Array<T>> rows (for _ (range height) (for _ (range width) defaultValue))])
(defmethod getCell [x y] (nth (nth rows y) x))
(defmethod setCell [x y value] (setNth (nth rows y) x value))

View File

@@ -1,6 +1,8 @@
package asciilib;
import asciilib.Colors;
import kiss.Stream;
import haxe.ds.Option;
typedef Letter = {
char:String,

View File

@@ -18,4 +18,53 @@
(defmethod setLetter [x y letter]
(letters.setChar x y letter.char)
(letterColors.setPixel x y letter.color))
(letterColors.setPixel x y letter.color))
(defun fromString [text]
(let [stream (Stream.fromString text)
:Map<String,Color> colors (new Map)
:Map<String,String> infoCodes (new Map)
:Map<String,Bool> opacityCodes [=>"0" false =>"1" true]]
(stream.dropString "COLORS\n")
(loop
(case (stream.takeLine)
((Some "INFO CODES") (break))
((Some colorLine)
(let [[symbol _r _g _b] (colorLine.split " ")]
(dictSet colors symbol (object r (Std.parseInt _r) g (Std.parseInt _g) b (Std.parseInt _b)))))
(None (throw "Expected INFO CODES in Surface"))))
(loop
(case (stream.takeLine)
((Some "SIZE") (break))
((Some infoLine)
(let [infoTokens (infoLine.split " ")]
(dictSet infoCodes (infoTokens.shift) (infoTokens.join " "))))
(None (throw "Expected SIZE in Surface"))))
(case (stream.takeLine)
(None (throw "expected [width] [height] in Surface"))
((Some sizeLine)
(let [[width height] (.map (sizeLine.split " ") Std.parseInt)
surface (new Surface width height)]
(stream.dropString "CHARACTERS\n")
(doFor row (range height)
(setNth surface.letters.rows row (stream.expect "line of characters" ->{(stream.takeLine)})))
(stream.dropString "BACKGROUND COLORS\n")
(doFor y (range height)
(doFor x (range width)
(surface.setBackgroundColor x y (dictGet colors (stream.expect "a color code" ->{(stream.takeChars 1)}))))
(stream.dropString "\n"))
(stream.dropString "CHARACTER COLORS\n")
(doFor y (range height)
(doFor x (range width)
(surface.letterColors.setPixel x y (dictGet colors (stream.expect "a color code" ->{(stream.takeChars 1)}))))
(stream.dropString "\n"))
(stream.dropString "OPACITY\n")
(doFor y (range height)
(doFor x (range width)
(surface.opacity.setCell x y (dictGet opacityCodes (stream.expect "0 or 1" ->{(stream.takeChars 1)}))))
(stream.dropString "\n"))
(stream.dropString "SPECIAL INFO\n")
(doFor y (range height)
(doFor x (range width)
(surface.specialInfo.setCell x y (dictGet infoCodes (stream.expect "a special info code" ->{(stream.takeChars 1)}))))))))))