Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit c2d478c

Browse files
author
Andrew Jackson
authored
Show numbers on moves branched from main line (#852)
* add hacking info to README * Remove annotation enum, make annotation layer draw annotations as alabels. Position::addChild() will now add annotations to non-mainline children. * add show-divergence handlers to study mode * fix type error caught by ts; null not a valid value to assign. * add duplicate annotation checking to position.ts * fix null property for pointer style, nits
1 parent 2a4fe41 commit c2d478c

File tree

9 files changed

+160
-77
lines changed

9 files changed

+160
-77
lines changed

minigui/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,16 @@ stdout: = D4
310310
- *fails*: If the id doesn't correspond to a position played in the game or
311311
one of its variations.
312312
- *comments*: Required only for Minigui's study mode.
313+
314+
315+
### Hacking
316+
317+
318+
e.g. in study.ts, change the last line to something like
319+
320+
```
321+
(window as any)['app'] = new ExploreApp();
322+
```
323+
324+
This will put the application in a global variable named "app"; you need to cast
325+
the window object to the "any" type to silence the TS compiler.

minigui/board.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class ClickableBoard extends Board {
284284
drawImpl() {
285285
super.drawImpl();
286286
let p = this.enabled ? this.p : null;
287-
this.ctx.canvas.style.cursor = p ? 'pointer' : null;
287+
this.ctx.canvas.style.cursor = p ? 'pointer' : '';
288288
if (p) {
289289
this.drawStones([p], this.position.toPlay, 0.6);
290290
}

minigui/layer.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,24 @@ namespace Variation {
458458

459459

460460
class Annotations extends Layer {
461-
private annotations = new Map<Annotation.Shape, Annotation[]>();
461+
private annotations: Annotation[] = [];
462+
private _showDivergence = false;
463+
464+
get showDivergence() : boolean {
465+
return this._showDivergence;
466+
}
467+
set showDivergence(x: boolean) {
468+
if (x == this._showDivergence) {
469+
return;
470+
}
471+
this._showDivergence = x;
472+
this.update(new Set<string>(["annotations"]));
473+
this.board.draw();
474+
}
462475

463476
clear() {
464-
if (this.annotations.size > 0) {
465-
this.annotations.clear();
477+
if (this.annotations.length > 0) {
478+
this.annotations = [];
466479
this.board.draw();
467480
}
468481
}
@@ -473,41 +486,34 @@ class Annotations extends Layer {
473486
}
474487

475488
let position = this.board.position;
476-
this.annotations.clear();
477-
for (let annotation of position.annotations) {
478-
let byShape = this.annotations.get(annotation.shape);
479-
if (byShape === undefined) {
480-
byShape = [];
481-
this.annotations.set(annotation.shape, byShape);
482-
}
483-
byShape.push(annotation);
484-
}
489+
// TODO this.annotations could be a map keyed by coordinate.
490+
this.annotations = position.annotations;
485491
return true;
486492
}
487493

488494
draw() {
489-
if (this.annotations.size == 0) {
495+
if (this.annotations.length == 0) {
490496
return;
491497
}
492498

493499
let sr = this.board.stoneRadius;
494-
let pr = pixelRatio();
495500

496501
let ctx = this.board.ctx;
497-
ctx.lineCap = 'round';
498-
this.annotations.forEach((annotations: Annotation[], shape: Annotation.Shape) => {
499-
switch (shape) {
500-
case Annotation.Shape.Dot:
501-
for (let annotation of annotations) {
502-
let c = this.boardToCanvas(annotation.p.row, annotation.p.col);
503-
ctx.fillStyle = annotation.colors[0];
504-
ctx.beginPath();
505-
ctx.arc(c.x + 0.5, c.y + 0.5, 0.16 * sr, 0, 2 * Math.PI);
506-
ctx.fill();
507-
}
508-
break;
502+
let textHeight = Math.floor(0.8 * sr);
503+
ctx.font = `${textHeight}px sans-serif`;
504+
ctx.textAlign = 'center';
505+
ctx.textBaseline = 'middle';
506+
507+
for (let annotation of this.annotations) {
508+
ctx.fillStyle = annotation.colors[0];
509+
let c = this.boardToCanvas(annotation.p.row, annotation.p.col);
510+
if (annotation.label == "●"){
511+
ctx.fillText(annotation.label, c.x, c.y);
509512
}
510-
});
513+
else if (this.showDivergence) {
514+
ctx.fillText(annotation.label, c.x, c.y);
515+
}
516+
}
511517
}
512518
}
513519

minigui/position.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@
1515
import {Color, Move, N, Nullable, Point, moveIsPoint, movesEqual, otherColor, stonesEqual, toGtp} from './base'
1616
import * as util from './util'
1717

18-
namespace Annotation {
19-
export enum Shape {
20-
Dot,
21-
}
22-
}
23-
2418
interface Annotation {
2519
p: Point;
26-
shape: Annotation.Shape;
20+
label: string;
2721
colors: string[];
2822
}
2923

@@ -100,10 +94,10 @@ class Position {
10094
if (moveIsPoint(this.lastMove)) {
10195
this.annotations.push({
10296
p: this.lastMove,
103-
shape: Annotation.Shape.Dot,
97+
label: "●",
10498
colors: ['#ef6c02'],
10599
});
106-
}
100+
}
107101
}
108102

109103
addChild(p: Position) {
@@ -125,6 +119,34 @@ class Position {
125119
// Create a new child.
126120
p.isMainLine = this.isMainLine && this.children.length == 0;
127121
p.parent = this;
122+
123+
// If it is not on the main line, prepare the move number annotations.
124+
if (! p.isMainLine) {
125+
let result: Move[] = [];
126+
let node: Nullable<Position>
127+
for (node = p; node && !node.isMainLine; node = node.parent) {
128+
if (node.lastMove == null) { break; }
129+
result.push(node.lastMove);
130+
}
131+
result.reverse();
132+
let playedCount = new Uint16Array(N * N);
133+
for (let i=0; i < result.length - 1; ++i) {
134+
let move = result[i];
135+
136+
if (moveIsPoint(move)) {
137+
let idx = move.row * N + move.col;
138+
let count = ++playedCount[idx];
139+
if (count != 1) {
140+
continue;
141+
}
142+
p.annotations.push({
143+
p: move, // 'p' here for 'point'.
144+
label: (i+1).toString(),
145+
colors: ['#999999'],
146+
});
147+
}
148+
}
149+
}
128150
this.children.push(p);
129151
}
130152

minigui/static/board.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minigui/static/layer.js

Lines changed: 31 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minigui/static/position.js

Lines changed: 28 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minigui/static/study.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)