Skip to content

Commit 424566a

Browse files
committed
Add 'labelGrid' property for cell-specific labels
1 parent 34acde7 commit 424566a

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ This private NPM module graphically renders game states. This repository houses
66

77
Currently the only supported output format is SVG. The library has been tested and works correctly in Chrome, Firefox, Opera, and Edge.
88

9-
[A playground/demo area is now available.](https://renderer.dev.abstractplay.com)
9+
[A playground/demo area is now available.](https://renderer.dev.abstractplay.com) The [Abstract Play Designer](https://designer.abstractplay.com/) is another place you can interact with the rendering engine.
1010

1111
## Usage
1212

13-
In the browser, simply load `APRender.js` via a `<script>` tag. For a concrete example, [see the playground](https://renderer.dev.abstractplay.com). The [Abstract Play Designer](https://designer.abstractplay.com/) is another place you can interact with the rendering engine.
13+
In the browser, simply load `APRender.js` via a `<script>` tag. For a concrete example, [see the playground](https://renderer.dev.abstractplay.com).
1414

1515
From within Node, simply import the variables and functions you need. Both methods give you access to the same API.
1616

src/renderers/_base.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,7 +2967,6 @@ export abstract class RendererBase {
29672967
offset = 1;
29682968
}
29692969

2970-
29712970
const myHex = defineHex({
29722971
offset,
29732972
orientation,
@@ -3053,6 +3052,11 @@ export abstract class RendererBase {
30533052
rowLabels.reverse();
30543053
}
30553054

3055+
let labelGrid: string[][]|undefined;
3056+
if ("labelGrid" in this.json.board && this.json.board.labelGrid !== undefined && this.json.board.labelGrid !== null && Array.isArray(this.json.board.labelGrid)) {
3057+
labelGrid = this.json.board.labelGrid.map(row => [...row]);
3058+
}
3059+
30563060
let labelColour = this.options.colourContext.labels;
30573061
if ( ("labelColour" in this.json.board) && (this.json.board.labelColour !== undefined) ) {
30583062
labelColour = this.resolveColour(this.json.board.labelColour) as string;
@@ -3073,13 +3077,23 @@ export abstract class RendererBase {
30733077
const { x, y } = hex;
30743078
const used = cells.use(symbolPoly).size(cellsize, cellsize).translate(x, y);
30753079
if ( ( (! this.json.options) || (! this.json.options.includes("hide-labels") ) ) && (labelStyle === "internal") ) {
3076-
const components: string[] = [];
3077-
components.push(columnLabels[hex.col]);
3078-
components.push(rowLabels[hex.row]);
3079-
if (/^\d+$/.test(components[0])) {
3080-
components.reverse();
3080+
let label: string;
3081+
if (labelGrid === undefined) {
3082+
const components: string[] = [];
3083+
components.push(columnLabels[hex.col]);
3084+
components.push(rowLabels[hex.row]);
3085+
if (/^\d+$/.test(components[0])) {
3086+
components.reverse();
3087+
}
3088+
label = components.join("");
3089+
} else {
3090+
label = "?";
3091+
if (hex.row < labelGrid.length) {
3092+
if (hex.col < labelGrid[hex.row].length) {
3093+
label = labelGrid[hex.row][hex.col];
3094+
}
3095+
}
30813096
}
3082-
const label = components.join("");
30833097

30843098
let labelX = corners[5].x;
30853099
let labelY = corners[5].y;
@@ -3118,7 +3132,7 @@ export abstract class RendererBase {
31183132

31193133
// external labels, if requested
31203134
// Add board labels
3121-
if (labelStyle === "external") {
3135+
if (labelStyle === "external" && labelGrid === undefined) {
31223136
let hideHalf = false;
31233137
if (this.json.options?.includes("hide-labels-half")) {
31243138
hideHalf = true;

src/schemas/schema.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export interface BoardBasic {
362362
*/
363363
strokeWeight?: number;
364364
/**
365-
* The opacity of lines drawn to construct the board.
365+
* The opacity of lines drawn for board labels.
366366
*/
367367
labelOpacity?: number;
368368
/**
@@ -432,6 +432,10 @@ export interface BoardBasic {
432432
* Only meaningful for `hex-of-*` boards and generates configurations where the sides alternate between two sizes. Mutually exclusive with `half`.
433433
*/
434434
alternatingSymmetry?: boolean;
435+
/**
436+
* The only way of applying arbitrary labels to cells. Provide the labels in a grid, just as you would the pieces: top to bottom, left to right.
437+
*/
438+
labelGrid?: string[][];
435439
/**
436440
* An optional array of strings to override the default column labeling.
437441
*/

src/schemas/schema.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,7 @@
18481848
"default": 1
18491849
},
18501850
"labelOpacity": {
1851-
"description": "The opacity of lines drawn to construct the board.",
1851+
"description": "The opacity of lines drawn for board labels.",
18521852
"type": "number",
18531853
"minimum": 0,
18541854
"maximum": 1,
@@ -1939,6 +1939,16 @@
19391939
"description": "Only meaningful for `hex-of-*` boards and generates configurations where the sides alternate between two sizes. Mutually exclusive with `half`.",
19401940
"type": "boolean"
19411941
},
1942+
"labelGrid": {
1943+
"description": "The only way of applying arbitrary labels to cells. Provide the labels in a grid, just as you would the pieces: top to bottom, left to right.",
1944+
"type": "array",
1945+
"items": {
1946+
"type": "array",
1947+
"items": {
1948+
"type": "string"
1949+
}
1950+
}
1951+
},
19421952
"columnLabels": {
19431953
"description": "An optional array of strings to override the default column labeling.",
19441954
"type": "array",

0 commit comments

Comments
 (0)