diff --git a/projects/asciilib2/src/asciilib/Grid.kiss b/projects/asciilib2/src/asciilib/Grid.kiss index 8daa13b8..451de8af 100644 --- a/projects/asciilib2/src/asciilib/Grid.kiss +++ b/projects/asciilib2/src/asciilib/Grid.kiss @@ -1,4 +1,7 @@ (defnew [_width _height :T defaultValue] [:Int width _width :Int height _height - :Array> rows (for _ (range height) (for _ (range width) defaultValue))]) \ No newline at end of file + :Array> 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)) \ No newline at end of file diff --git a/projects/asciilib2/src/asciilib/Surface.hx b/projects/asciilib2/src/asciilib/Surface.hx index 7a0f1866..9cecafba 100644 --- a/projects/asciilib2/src/asciilib/Surface.hx +++ b/projects/asciilib2/src/asciilib/Surface.hx @@ -1,6 +1,8 @@ package asciilib; import asciilib.Colors; +import kiss.Stream; +import haxe.ds.Option; typedef Letter = { char:String, diff --git a/projects/asciilib2/src/asciilib/Surface.kiss b/projects/asciilib2/src/asciilib/Surface.kiss index e95ff706..ee4fc6ba 100644 --- a/projects/asciilib2/src/asciilib/Surface.kiss +++ b/projects/asciilib2/src/asciilib/Surface.kiss @@ -18,4 +18,53 @@ (defmethod setLetter [x y letter] (letters.setChar x y letter.char) - (letterColors.setPixel x y letter.color)) \ No newline at end of file + (letterColors.setPixel x y letter.color)) + +(defun fromString [text] + (let [stream (Stream.fromString text) + :Map colors (new Map) + :Map infoCodes (new Map) + :Map 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)})))))))))) \ No newline at end of file