Skip to content

Instantly share code, notes, and snippets.

@postite
Created October 19, 2018 08:41
Show Gist options
  • Save postite/635b8fbc5c55712ff96798b0668d0751 to your computer and use it in GitHub Desktop.
Save postite/635b8fbc5c55712ff96798b0668d0751 to your computer and use it in GitHub Desktop.
minimal hl/heaps timer
import haxe.Timer;
using DateTools;
class Main extends hxd.App {
var txt:h2d.Text;
var fui:h2d.Flow;
var paused:Bool;
var music:hxd.snd.Channel;
var times = [[0, 20], [1, 20], [20, 0], [30, 0]];
var targetTime:Float = 30;
var targetTimeString:String;
var timer:Timer;
var date:Float;
override function init() {
paused = false;
fui = new h2d.Flow(s2d);
fui.isVertical = true;
fui.verticalSpacing = 5;
fui.horizontalAlign = Middle;
fui.verticalAlign = Middle;
fui.padding = 10;
txt = inittext();
createbutton();
}
public function stort(time:Date) {
if (timer != null)
timer.stop();
targetTimeString = '${time.getMinutes()}:${time.getSeconds()}';
targetTime = time.getTime();
date = new Date(2018, 0, 0, 0, 0, 0).getTime();
timer = new Timer(1000);
timer.run = onRun;
}
function playSound() {
var res = if (hxd.res.Sound.supportedFormat(Mp3) || hxd.res.Sound.supportedFormat(OggVorbis)) hxd.Res.music_loop else null;
if (res != null) {
trace("Playing " + res);
music = res.play(false);
// music.queueSound(...);
music.onEnd = function() trace("LOOP");
}
}
function addButton(label:String, onClick:Void->Void, parent:h2d.Flow) {
var f = new h2d.Flow(parent);
f.padding = 5;
f.paddingBottom = 7;
f.backgroundTile = h2d.Tile.fromColor(0x404040);
var tf = new h2d.Text(getFont(), f);
tf.text = label;
f.enableInteractive = true;
f.interactive.cursor = Button;
f.interactive.onClick = function(_) onClick();
f.interactive.onOver = function(_) f.backgroundTile = h2d.Tile.fromColor(0x606060);
f.interactive.onOut = function(_) f.backgroundTile = h2d.Tile.fromColor(0x404040);
return f;
}
function addTimeButton(flow:h2d.Flow, _date:Date, fun) {
var _text = _date.getTime().parse();
var text = '${_text.minutes}:${_text.seconds}';
var f = addButton(text, fun, flow);
f.paddingLeft = 7;
flow.isInline = true;
}
function getFont() {
return hxd.res.DefaultFont.get();
}
function inittext():h2d.Text {
var font = hxd.Res.customFont.toFont();
// creates another text field with this font
var tf = new h2d.Text(font, fui);
tf.textColor = 0xFFFFFF;
tf.dropShadow = {
dx: 0.5,
dy: 0.5,
color: 0xFF0000,
alpha: 0.8
};
tf.text = "ready ?";
tf.y = 20;
tf.x = 20;
tf.scale(7);
return tf;
}
function onRun() {
trace(paused);
if (!paused) {
var parsed = date.parse();
date += 1000;
if (date > targetTime)
return togStop();
var timetxt = '${parsed.minutes}:${parsed.seconds} on $targetTimeString';
txt.text = timetxt;
}
}
function createbutton() {
var timebuts = new h2d.Flow(fui);
for (a in times) {
var d = new Date(2018, 0, 0, 0, a[0], a[1]);
addTimeButton(timebuts, d, stort.bind(d));
}
addButton('pause', togPause, fui);
addButton('stop', togStop, fui);
}
function togPause() {
paused = !paused;
}
function togStop() {
if (timer != null) {
txt.text = "fini!";
timer.stop();
playSound();
trace("fini" + txt.text);
}
}
static function main() {
new Main();
hxd.Res.initEmbed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment