diff --git a/projects/handloose/source/DocumentModel.hx b/projects/handloose/source/DocumentModel.hx index 37de4b13..d21f042d 100644 --- a/projects/handloose/source/DocumentModel.hx +++ b/projects/handloose/source/DocumentModel.hx @@ -5,8 +5,7 @@ import kiss.List; import sys.io.File; import sys.FileSystem; import flixel.math.FlxRandom; - -using haxe.io.Path; +import haxe.io.Path; typedef ArrowStuff = { text:String, diff --git a/projects/handloose/source/DocumentModel.kiss b/projects/handloose/source/DocumentModel.kiss index 738e0d6f..f913cb0d 100644 --- a/projects/handloose/source/DocumentModel.kiss +++ b/projects/handloose/source/DocumentModel.kiss @@ -1,30 +1,34 @@ (defNew [&prop :String path] (type "") // If the file can't be written to, throw the error right away // Use files with the same extension in the current working directory to determine letter frequencies - (walkDirectory (FileSystem.absolutePath "") - ->file (when (= (file.extension) (path.extension)) - (learnFrequencies (File.getContent file))))) + (walkDirectory "" (FileSystem.absolutePath "") + ->file (when (= (Path.extension file) (Path.extension path)) + (learnFrequencies ~(File.getContent ~file)) + ~charFrequencies))) (prop &mut :String content "") -(method type [:String str] +(method :Void type [:String str] (+= content str) - (File.saveContent path content)) + (File.saveContent path content) + (print content)) (prop :Map> charFrequencies (new Map)) (prop :Map> wordFrequencies (new Map)) (method incFrequency [:Map> m :String c :String following] - (unless (m.exists following) (dictSet m following (new Map))) - (let [followingMap (dictGet m following)] - (dictSet followingMap c (+ 1 (or (dictGet followingMap c) 0))))) + (when c + (unless (m.exists following) (dictSet m following (new Map))) + (let [followingMap (dictGet m following)] + (dictSet followingMap c (+ 1 (or (dictGet followingMap c) 0)))))) (method learnFrequencies [:String str] (let [chars (str.split "")] - (incFrequency charFrequencies (first chars) "") - (doFor [following c] (pairs chars) - (incFrequency charFrequencies c following))) - // TODO learn word frequencies - ) + (when chars + (incFrequency charFrequencies (first chars) "") + (doFor [following c] (pairs chars) + (incFrequency charFrequencies c following))) + // TODO learn word frequencies + )) (prop :FlxRandom r (new FlxRandom)) (method :ArrowStuff generateArrowStuff [] @@ -32,7 +36,7 @@ (let [lastChar (substr content -1) charFreq (dictGet charFrequencies lastChar) chars [] - weights []] + :Array weights []] (doFor =>c weight charFreq (chars.push c) (weights.push weight)) diff --git a/projects/handloose/source/Main.hx b/projects/handloose/source/Main.hx index 1e58528d..f9bb2e6a 100644 --- a/projects/handloose/source/Main.hx +++ b/projects/handloose/source/Main.hx @@ -1,5 +1,6 @@ package; +import flixel.FlxG; import flixel.FlxGame; import openfl.display.Sprite; @@ -9,5 +10,8 @@ class Main extends Sprite { super(); addChild(new FlxGame(0, 0, TypingState, 1, 60, 60, true)); + var t:TypingState = cast FlxG.state; + trace(Sys.args()[0]); + t.setModel(new DocumentModel(Sys.args()[0])); } } diff --git a/projects/handloose/source/TypingState.kiss b/projects/handloose/source/TypingState.kiss index 67bd55c0..6e5315e2 100644 --- a/projects/handloose/source/TypingState.kiss +++ b/projects/handloose/source/TypingState.kiss @@ -31,8 +31,6 @@ add(actionSprites); - var actionSprite = makeTriangleSprite(Right, "a", -SPRITE_SIZE, ()->{trace("a");}); - actionSprite = makeTriangleSprite(Right, "a", -SPRITE_SIZE*4, ()->{trace("a");}); }|#) (prop :Map inputCodes (new Map)) @@ -74,6 +72,14 @@ (prop :FlxTypedGroup actionSprites (new FlxTypedGroup)) +(prop &mut :DocumentModel _model null) +(method setModel [:DocumentModel m] + (set _model m) + (doFor i (range 100) + #|{ + var stuff = m.generateArrowStuff(); + makeTriangleSprite(DIR_ORDER[i % DIR_ORDER.length], stuff.text, -SPRITE_SIZE*i, stuff.action); + }|#)) (method :FlxSprite makeTriangleSprite [:ArrowDir dir :String text :Int y &opt :Void->Void action] #|{ @@ -93,6 +99,14 @@ case Right: 90; }; + + text = switch (text) { + case "\n": ""; + case "\r": ""; + case " ": ""; + case "\t": ""; + default: text; + }; var text = new FlxText(text, TEXT_SIZE); text.angle = -spr.angle; text.color = FlxColor.BLACK;