Skip to content

Commit

Permalink
nicer behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
fohristiwhirl committed Jul 5, 2019
1 parent 6fea0e9 commit 52c2d31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
18 changes: 9 additions & 9 deletions 80_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function NewInfoHandler() {
}
};

ih.draw_infobox = function(mouse_point, active_square, leela_should_go, active_colour, searchmoves, hoverdraw_line) {
ih.draw_infobox = function(mouse_point, active_square, leela_should_go, active_colour, searchmoves, hoverdraw_index) {

ih.draw_statusbox(leela_should_go, searchmoves);

Expand All @@ -296,6 +296,8 @@ function NewInfoHandler() {
return;
}

let info_list = this.sorted();

// We might be highlighting some div...

let highlight_move = null;
Expand All @@ -306,10 +308,8 @@ function NewInfoHandler() {
highlight_class = "ocm_highlight";
}

let hoverdraw_move = null;

if (Array.isArray(hoverdraw_line) && hoverdraw_line.length > 0) {
highlight_move = hoverdraw_line[0];
if (typeof hoverdraw_index === "number" && hoverdraw_index >= 0 && hoverdraw_index < info_list.length) {
highlight_move = info_list[hoverdraw_index].move;
highlight_class = "hover_highlight";
}

Expand Down Expand Up @@ -337,9 +337,9 @@ function NewInfoHandler() {

this.info_clickers = [];

let info_list = this.sorted();
let substrings = [];
let n = 0;
let clicker_index = 0;
let div_index = 0;

for (let info of info_list) {

Expand All @@ -351,7 +351,7 @@ function NewInfoHandler() {
divclass += " " + highlight_class;
}

substrings.push(`<div id="infoline_${info.move}" class="${divclass}">`);
substrings.push(`<div id="infoline_${div_index++}" class="${divclass}">`);

// The "focus" button...

Expand Down Expand Up @@ -391,7 +391,7 @@ function NewInfoHandler() {
if (nice_pv[i].includes("O-O")) {
spanclass += " nobr";
}
substrings.push(`<span id="infobox_${n++}" class="${spanclass}">${nice_pv[i]} </span>`);
substrings.push(`<span id="infobox_${clicker_index++}" class="${spanclass}">${nice_pv[i]} </span>`);
this.info_clickers.push({
move: info.pv[i],
is_start: i === 0,
Expand Down
44 changes: 19 additions & 25 deletions 95_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function NewRenderer() {
renderer.pgn_choices = null; // All games found when opening a PGN file.
renderer.friendly_draws = New2DArray(8, 8); // What pieces are drawn in boardfriends. Used to skip redraws.
renderer.active_square = null; // Clicked square.
renderer.hoverdraw_line = []; // The fantasy line drawn so far.
renderer.hoverdraw_index = -1;
renderer.hoverdraw_depth = 0;
renderer.tick = 0; // How many draw loops we've been through.
renderer.position_change_time = performance.now(); // Time of the last position change. Used for cooldown on hover draw.

Expand All @@ -38,7 +39,7 @@ function NewRenderer() {
this.position_change_time = performance.now();

this.searchmoves = [];
this.hoverdraw_line = [];
this.hoverdraw_index = -1;
this.position_changed_clear_info_handler(new_game_flag);
this.escape();

Expand Down Expand Up @@ -1012,59 +1013,52 @@ function NewRenderer() {
renderer.hoverdraw = function() {

if (!config.hover_draw) {
this.hoverdraw_line = [];
this.hoverdraw_index = -1;
return false;
}

if (performance.now() - this.position_change_time < 1000) {
this.hoverdraw_line = [];
this.hoverdraw_index = -1;
return false;
}

let overlist = document.querySelectorAll(":hover");

let firstmove = null;
let sorted_index = null;

for (let item of overlist) {
if (typeof item.id === "string" && item.id.startsWith("infoline_")) {
firstmove = item.id.slice("infoline_".length);
sorted_index = parseInt(item.id.slice("infoline_".length), 10);
break;
}
}

if (!firstmove) {
this.hoverdraw_line = [];
if (typeof sorted_index !== "number" || Number.isNaN(sorted_index)) {
this.hoverdraw_index = -1;
return false;
}

let info = this.info_handler.table[firstmove];

// It is entirely possible that the firstmove we've detected is illegal
// due to the HTML being stale - because we call hoverdraw() before
// updating the HTML to prevent flicker.
let info = this.info_handler.sorted()[sorted_index]; // Possibly undefined

if (!info || Array.isArray(info.pv) === false || info.pv.length === 0) {
this.hoverdraw_line = [];
this.hoverdraw_index = -1;
return false;
}

// At this point, info.pv represents the full line to animate. We need
// to check if it's compatible with the moves we've already drawn, if
// there are any...
// If the user is hovering over a different div index in the infobox, reset depth...

if (ArrayStartsWith(info.pv, this.hoverdraw_line) === false) {
this.hoverdraw_line = [];
if (sorted_index !== this.hoverdraw_index) {
this.hoverdraw_index = sorted_index;
this.hoverdraw_depth = 0;
}

// And now, sometimes add a move to the drawn line...
// Sometimes increase depth...

if (this.tick % config.animate_delay_multiplier === 0) {
if (this.hoverdraw_line.length < info.pv.length) {
this.hoverdraw_line.push(info.pv[this.hoverdraw_line.length]);
}
this.hoverdraw_depth++;
}

return this.draw_fantasy_from_moves(this.hoverdraw_line);
return this.draw_fantasy_from_moves(info.pv.slice(0, this.hoverdraw_depth)); // Relies on slice() being safe if depth > length
};

renderer.draw_fantasy_from_moves = function(moves) {
Expand Down Expand Up @@ -1136,7 +1130,7 @@ function NewRenderer() {
this.leela_should_go(),
this.node.get_board().active,
this.searchmoves,
this.hoverdraw_line);
this.hoverdraw_index);
};

renderer.draw_loop = function() {
Expand Down

0 comments on commit 52c2d31

Please sign in to comment.