bring handloose and nat-globelet-playground into CI
This commit is contained in:
9
projects/flixel-desktop-handloose/source/ActionSprite.hx
Normal file
9
projects/flixel-desktop-handloose/source/ActionSprite.hx
Normal file
@@ -0,0 +1,9 @@
|
||||
package;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
import flixel.FlxSprite;
|
||||
import TypingState;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class ActionSprite extends FlxSprite {}
|
||||
@@ -0,0 +1,3 @@
|
||||
(defNew [&prop :Void->Void action
|
||||
&prop :ArrowDir dir]
|
||||
(super))
|
||||
16
projects/flixel-desktop-handloose/source/DocumentModel.hx
Normal file
16
projects/flixel-desktop-handloose/source/DocumentModel.hx
Normal file
@@ -0,0 +1,16 @@
|
||||
package;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
import sys.io.File;
|
||||
import sys.FileSystem;
|
||||
import flixel.math.FlxRandom;
|
||||
import haxe.io.Path;
|
||||
|
||||
typedef ArrowStuff = {
|
||||
text:String,
|
||||
action:Void->Void
|
||||
};
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class DocumentModel {}
|
||||
64
projects/flixel-desktop-handloose/source/DocumentModel.kiss
Normal file
64
projects/flixel-desktop-handloose/source/DocumentModel.kiss
Normal file
@@ -0,0 +1,64 @@
|
||||
(defNew [&prop :String path]
|
||||
(when (FileSystem.exists path) (set content (File.getContent path)))
|
||||
(type "") // If the file can't be written to, throw the error right away
|
||||
(learnAllFrequencies))
|
||||
|
||||
(prop &mut :String content "")
|
||||
(method :Void type [:String str]
|
||||
(+= content str)
|
||||
(File.saveContent path content)
|
||||
(print content))
|
||||
|
||||
(prop :Map<String,Map<String,Float>> charFrequencies (new Map))
|
||||
(prop :Map<String,Map<String,Float>> wordFrequencies (new Map))
|
||||
|
||||
(method incFrequency [:Map<String,Map<String,Float>> m :String c :String following]
|
||||
(let [&mut weight 1.0]
|
||||
(when c
|
||||
(when (= c "\r") (set c "\n"))
|
||||
(when (= c following " ") (set c "\t") (set weight 0.25))
|
||||
(unless (m.exists following) (dictSet m following (new Map)))
|
||||
(let [followingMap (dictGet m following)]
|
||||
(dictSet followingMap c (+ weight (or (dictGet followingMap c) 0)))))))
|
||||
|
||||
(defMacro prelearnAllFrequencies []
|
||||
/* TODO - add Context.definedValues to macro variables
|
||||
- walk LEARN_FROM, adding to a frequency map in prelearnFrequencies() below
|
||||
- stringify the frequency map with "" specially added around the keys
|
||||
- inject a Context.parse of the stringified frequency map
|
||||
*/
|
||||
(throw "not implemented"))
|
||||
|
||||
(defMacroFunction prelearnFrequencies [str]
|
||||
(throw "not implemented"))
|
||||
|
||||
(method learnAllFrequencies []
|
||||
(#if LEARN_FROM
|
||||
(prelearnAllFrequencies)
|
||||
// Use files with the same extension in the current working directory to determine letter frequencies
|
||||
(walkDirectory "" (FileSystem.absolutePath "")
|
||||
->file (when (= (Path.extension file) (Path.extension path))
|
||||
(learnFrequencies (File.getContent file))))))
|
||||
|
||||
(method learnFrequencies [:String str]
|
||||
(let [chars (str.split "")]
|
||||
(when chars
|
||||
(incFrequency charFrequencies (first chars) "")
|
||||
(doFor [following c] (pairs chars)
|
||||
(incFrequency charFrequencies c following)))
|
||||
// TODO learn word frequencies
|
||||
))
|
||||
|
||||
(prop :FlxRandom r (new FlxRandom))
|
||||
(var ANY_CHANCE 25) // percent
|
||||
(method :ArrowStuff generateArrowStuff []
|
||||
// TODO also generate word arrows if lastChar is a space
|
||||
(let [lastChar (substr content -1)
|
||||
charFreq (dictGet charFrequencies lastChar)
|
||||
chars []
|
||||
:Array<Float> weights []]
|
||||
(doFor =>c weight charFreq
|
||||
(chars.push c)
|
||||
(weights.push weight))
|
||||
(let [c (r.getObject chars (if (r.bool ANY_CHANCE) null weights))]
|
||||
(object text c action ->(type c)))))
|
||||
16
projects/flixel-desktop-handloose/source/Main.hx
Normal file
16
projects/flixel-desktop-handloose/source/Main.hx
Normal file
@@ -0,0 +1,16 @@
|
||||
package;
|
||||
|
||||
import flixel.FlxG;
|
||||
import flixel.FlxGame;
|
||||
import openfl.display.Sprite;
|
||||
|
||||
class Main extends Sprite
|
||||
{
|
||||
public function new()
|
||||
{
|
||||
super();
|
||||
addChild(new FlxGame(0, 0, TypingState, 1, 60, 60, true));
|
||||
var t:TypingState = cast FlxG.state;
|
||||
t.setModel(new DocumentModel(Sys.args()[0]));
|
||||
}
|
||||
}
|
||||
62
projects/flixel-desktop-handloose/source/TypingState.hx
Normal file
62
projects/flixel-desktop-handloose/source/TypingState.hx
Normal file
@@ -0,0 +1,62 @@
|
||||
package;
|
||||
|
||||
import flixel.FlxG;
|
||||
import flixel.FlxState;
|
||||
import flixel.FlxSprite;
|
||||
import flixel.util.FlxColor;
|
||||
import flixel.util.FlxTimer;
|
||||
import flixel.group.FlxGroup;
|
||||
import flixel.input.gamepad.FlxGamepad;
|
||||
import flixel.text.FlxText;
|
||||
import flixel.tweens.FlxTween;
|
||||
import haxe.ds.Option;
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
|
||||
using flixel.util.FlxSpriteUtil;
|
||||
|
||||
// In DDR order:
|
||||
enum ArrowDir
|
||||
{
|
||||
Left;
|
||||
Down;
|
||||
Up;
|
||||
Right;
|
||||
}
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class TypingState extends FlxState
|
||||
{
|
||||
function makeCircleSprite(text:String, x:Int, y:Int):FlxSprite
|
||||
{
|
||||
var spr = new FlxSprite();
|
||||
spr.makeGraphic(100, 100, FlxColor.TRANSPARENT, true);
|
||||
FlxSpriteUtil.beginDraw(FlxColor.WHITE);
|
||||
spr.drawCircle();
|
||||
var text = new FlxText(text, 24);
|
||||
text.color = FlxColor.BLACK;
|
||||
spr.stamp(text, 50 - Math.floor(text.width / 2), 50 - Math.floor(text.height / 2));
|
||||
spr.x = x;
|
||||
spr.y = y;
|
||||
return spr;
|
||||
}
|
||||
|
||||
function getFirstInputId():Option<Int> {
|
||||
// Important: can be null if there's no active gamepad yet!
|
||||
var gamepad:FlxGamepad = FlxG.gamepads.lastActive;
|
||||
if (gamepad != null)
|
||||
{
|
||||
var firstGamepadId = gamepad.firstJustPressedRawID();
|
||||
if (firstGamepadId != -1) {
|
||||
return Some(firstGamepadId);
|
||||
}
|
||||
}
|
||||
|
||||
var firstKeyId = FlxG.keys.firstJustPressed();
|
||||
if (firstKeyId != -1) {
|
||||
return Some(firstKeyId);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
150
projects/flixel-desktop-handloose/source/TypingState.kiss
Normal file
150
projects/flixel-desktop-handloose/source/TypingState.kiss
Normal file
@@ -0,0 +1,150 @@
|
||||
(var SPRITE_SPACING 20)
|
||||
(var SPRITE_SIZE 100)
|
||||
(var TEXT_SIZE 24)
|
||||
(prop &mut :FlxText docText null)
|
||||
(method &override :Void create []
|
||||
#{
|
||||
super.create();
|
||||
|
||||
var background = new FlxSprite();
|
||||
background.makeGraphic(1280, 720, FlxColor.BLACK);
|
||||
// background.x = 0;
|
||||
// background.y = 0;
|
||||
|
||||
FlxSpriteUtil.beginDraw(FlxColor.WHITE);
|
||||
|
||||
var x = SPRITE_SPACING;
|
||||
var y = FlxG.height - SPRITE_SIZE - SPRITE_SPACING;
|
||||
|
||||
// Split the screen into text area and dance area:
|
||||
var splitX = SPRITE_SPACING * 5 + SPRITE_SIZE * 4;
|
||||
background.drawLine(splitX, 0, splitX, FlxG.height);
|
||||
docText = new FlxText(splitX, 0, "", TEXT_SIZE);
|
||||
docText.setFormat(null, 8, FlxColor.WHITE);
|
||||
|
||||
add(background);
|
||||
add(docText);
|
||||
|
||||
// this is actually just distracting:
|
||||
// Split the left side into upper/lower:
|
||||
//background.drawLine(0, FlxG.height - SPRITE_SIZE - SPRITE_SPACING * 2, splitX, FlxG.height - SPRITE_SIZE - SPRITE_SPACING * 2);
|
||||
|
||||
/*
|
||||
makeTriangleSprite(Left, "", y);
|
||||
makeTriangleSprite(Down, "", y);
|
||||
makeTriangleSprite(Up, "", y);
|
||||
makeTriangleSprite(Right, "", y);
|
||||
*/
|
||||
|
||||
add(actionSprites);
|
||||
|
||||
}#)
|
||||
|
||||
(prop :Map<Int,ArrowDir> inputCodes (new Map))
|
||||
(var DIR_ORDER [ Left Down Up Right ])
|
||||
|
||||
(prop &mut :FlxText instructionText null)
|
||||
(method &override :Void update [:Float elapsed]
|
||||
(super.update elapsed)
|
||||
|
||||
(let [mappedCodes (count inputCodes)
|
||||
nextToMap (nth DIR_ORDER mappedCodes)]
|
||||
(if (= DIR_ORDER.length mappedCodes)
|
||||
// Handle an arrow press
|
||||
{
|
||||
(set instructionText.text "")
|
||||
(unless timer
|
||||
(set timer (new FlxTimer))
|
||||
(let [&mut i 0]
|
||||
(timer.start ARROW_DELAY ->_ {
|
||||
#{
|
||||
var stuff = _model.generateArrowStuff();
|
||||
makeTriangleSprite(DIR_ORDER[i++ % DIR_ORDER.length], stuff.text, -SPRITE_SIZE, stuff.action);
|
||||
}# } 0)))
|
||||
(whenLet [(Some id) (getFirstInputId)
|
||||
dir (dictGet inputCodes id)]
|
||||
(let [&mut :ActionSprite lowestMatching null
|
||||
&mut :Float highestY -SPRITE_SIZE-1]
|
||||
(actionSprites.forEachAlive
|
||||
->spr (when (= dir spr.dir)
|
||||
(when (> spr.y highestY)
|
||||
(set highestY spr.y)
|
||||
(set lowestMatching spr))))
|
||||
(when lowestMatching
|
||||
(lowestMatching.action)
|
||||
(updateDocText)
|
||||
(lowestMatching.kill))))
|
||||
}
|
||||
// Prompt to map the arrows
|
||||
{
|
||||
(unless instructionText
|
||||
(set instructionText (new FlxText "" TEXT_SIZE))
|
||||
(add instructionText))
|
||||
(set instructionText.text "Press $nextToMap")
|
||||
(instructionText.screenCenter)
|
||||
(whenLet [(Some id) (getFirstInputId)]
|
||||
(dictSet inputCodes id nextToMap))
|
||||
})))
|
||||
|
||||
(method updateDocText []
|
||||
(set docText.text (StringTools.replace _model.content "\t" " ")))
|
||||
|
||||
(prop :FlxTypedGroup<ActionSprite> actionSprites (new FlxTypedGroup))
|
||||
(prop &mut :DocumentModel _model null)
|
||||
(prop &mut :FlxTimer timer null)
|
||||
(var ARROW_DELAY 0.25)
|
||||
(method setModel [:DocumentModel m]
|
||||
(set _model m)
|
||||
(updateDocText))
|
||||
|
||||
(method :FlxSprite makeTriangleSprite [:ArrowDir dir :String text :Int y &opt :Void->Void action]
|
||||
#{
|
||||
var spr = new ActionSprite(action, dir);
|
||||
spr.makeGraphic(SPRITE_SIZE, SPRITE_SIZE, FlxColor.TRANSPARENT, true);
|
||||
FlxSpriteUtil.beginDraw(FlxColor.WHITE);
|
||||
spr.drawTriangle(0, 0, SPRITE_SIZE);
|
||||
|
||||
spr.angle = switch (dir)
|
||||
{
|
||||
case Left:
|
||||
-90;
|
||||
case Down:
|
||||
180;
|
||||
case Up:
|
||||
0;
|
||||
case Right:
|
||||
90;
|
||||
};
|
||||
|
||||
text = switch (text) {
|
||||
case "\n": "<newline>";
|
||||
case "\r": "<bad newline>";
|
||||
case " ": "<space>";
|
||||
case "\t": "<tab>";
|
||||
default: text;
|
||||
};
|
||||
var ftext = new FlxText(text, TEXT_SIZE);
|
||||
ftext.angle = -spr.angle;
|
||||
ftext.color = FlxColor.BLACK;
|
||||
|
||||
if (text.length > 1) {
|
||||
ftext.size = 12;
|
||||
}
|
||||
spr.stamp(ftext, Math.floor(SPRITE_SIZE/2) - Math.floor(ftext.width / 2), Math.floor(SPRITE_SIZE/2) - Math.floor(ftext.height / 2));
|
||||
|
||||
spr.x = SPRITE_SPACING + DIR_ORDER.indexOf(dir) * (SPRITE_SIZE + SPRITE_SPACING);
|
||||
spr.y = y;
|
||||
|
||||
if (action != null) {
|
||||
actionSprites.add(spr);
|
||||
FlxTween.linearMotion(spr, spr.x, spr.y, spr.x, FlxG.height, 200, false, {
|
||||
onComplete: (_) -> {
|
||||
spr.kill();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
add(spr);
|
||||
}
|
||||
|
||||
return spr;
|
||||
}#)
|
||||
Reference in New Issue
Block a user