From 43be986a989968aa3691f857628a77f8130ab460 Mon Sep 17 00:00:00 2001 From: Alyssa Milburn Date: Wed, 30 Jul 2014 19:44:27 +0200 Subject: [PATCH] add a seek bar to round playback --- style.css | 7 ++++++ tanks.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/style.css b/style.css index 97ea65b..5b526ae 100644 --- a/style.css +++ b/style.css @@ -191,3 +191,10 @@ table.pollster thead { .swatch { color: #000000; } + +.ui-slider-tick-mark { + display: inline-block; + width: 2px; + height: 100%; + position: absolute; +} diff --git a/tanks.js b/tanks.js index 05b2b15..45fb827 100644 --- a/tanks.js +++ b/tanks.js @@ -181,7 +181,6 @@ function Tank(ctx, width, height, color, sensors) { var loop_id; var updateFunc = null; function togglePlayback() { - console.log($("#playing").prop("checked")); if ($("#playing").prop("checked")) { loop_id = setInterval(updateFunc, 66); } else { @@ -216,15 +215,7 @@ function start(id, game) { lastframe = frame; } - function update() { - var idx = frame % (turns.length + 20); - var turn; - - frame += 1; - if (idx >= turns.length) { - return; - } - + function drawFrame(idx) { canvas.width = canvas.width; turn = turns[idx]; @@ -248,6 +239,35 @@ function start(id, game) { for (i in turn) { tanks[i].draw_tank() } + + document.getElementById('frameid').innerHTML = idx; + } + + function update() { + var idx = frame % (turns.length + 20); + var turn; + + frame += 1; + if (idx >= turns.length) { + return; + } + + drawFrame(idx); + + $('#seekslider').slider('value', idx); + } + + function seekToFrame(newidx) { + var idx = frame % (turns.length + 20); + if (idx !== newidx) { + frame = newidx; + drawFrame(newidx); + } + // make sure we're paused + if ($("#playing").prop("checked")) { + $("#playing").prop("checked", false); + togglePlayback(); + } } updateFunc = update; @@ -258,8 +278,33 @@ function start(id, game) { } if (id === "battlefield") { - $("#game_box").append('

'); + $("#game_box").append('

0

'); $('#playing').button(); + var slider = $('#seekslider'); + slider.slider({ max: turns.length-1, slide: function(event, ui) { seekToFrame(ui.value); } }); + + var spacing = 100 / turns.length; + var deaths = []; + for (i in turns[0]) { + deaths.push(false); + } + var percent = 0; + for (var f = 0; f < turns.length; f++) { + var turn = turns[f]; + if (percent < (spacing * f)) { + percent = spacing * f; + } + for (var i = 0; i < turn.length; i++) { + if (deaths[i]) { continue; } + if (!turn[i] || (turn[i][4] & 4)) { + deaths[i] = true; + // http://stackoverflow.com/questions/8648963/add-tick-marks-to-jquery-slider + $('').css('left', percent + '%').css('background-color', game[1][i][0]).appendTo(slider); + percent++; + break; + } + } + } } }