Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'index.js')
-rw-r--r--index.js57
1 files changed, 36 insertions, 21 deletions
diff --git a/index.js b/index.js
index a0f63a3..33d78c3 100644
--- a/index.js
+++ b/index.js
@@ -99,6 +99,8 @@ if (recVis == 'false') {
recordSheetVisibility.checked = false;
}
+let info = document.getElementById('status');
+
// Object.values(settingsPanel.querySelectorAll('fieldset')).forEach(fieldset => {
[].forEach(fieldset => {
const target = document.getElementById(fieldset.name);
@@ -166,8 +168,10 @@ POINTS.forEach((row, index) => row.forEach(([x, y]) => {
point.addEventListener('click', e => {
let selectedSoldier = document.querySelector('.soldier-record.selected');
+ let existingOccupant =
+ svg.querySelector(`.counter[data-x="${point.dataset.x}"][data-y="${point.dataset.y}"]`);
- if (selectedSoldier) {
+ if (selectedSoldier && !existingOccupant) {
let counter = document.createElementNS(svgns, 'circle'),
text = document.createElementNS(svgns, 'text'),
{troopNumber, troopAllegiance} = selectedSoldier.dataset,
@@ -178,6 +182,11 @@ POINTS.forEach((row, index) => row.forEach(([x, y]) => {
pt = new DOMPoint(point.x.baseVal.value, point.y.baseVal.value),
svgP = pt.matrixTransform(mtx);
+ info.querySelector('#hex-count').textContent = '-';
+ info.style.display = 'none';
+ ptGrp.querySelectorAll('.active').forEach(el => el.removeAttribute('class'));
+ svg.querySelectorAll('.sight-line').forEach(el => el.remove());
+
counter.setAttributeNS(null, 'cx', svgP.x);
counter.setAttributeNS(null, 'cy', svgP.y);
counter.setAttributeNS(null, 'r', '0.25in');
@@ -264,37 +273,43 @@ POINTS.forEach((row, index) => row.forEach(([x, y]) => {
let source = ptGrp.querySelector(`use[data-x="${counter.dataset.x}"][data-y="${counter.dataset.y}"]`);
let [x1, y1] = [source.x.baseVal.value, source.y.baseVal.value];
let [x2, y2] = [e.target.x.baseVal.value, e.target.y.baseVal.value];
- let { x: svgX1, y: svgY1 } = ptGrpToSvgPt(x1, y1);
- let { x: svgX2, y: svgY2 } = ptGrpToSvgPt(x2, y2);
- let sightLine = document.createElementNS(svgns, 'line');
+ if (x1 !== x2 || y1 !== y2) {
+ let { x: svgX1, y: svgY1 } = ptGrpToSvgPt(x1, y1);
+ let { x: svgX2, y: svgY2 } = ptGrpToSvgPt(x2, y2);
- sightLine.classList.add('sight-line');
- sightLine.setAttributeNS(null, 'x1', svgX1);
- sightLine.setAttributeNS(null, 'y1', svgY1);
- sightLine.setAttributeNS(null, 'x2', svgX2);
- sightLine.setAttributeNS(null, 'y2', svgY2);
+ let sightLine = document.createElementNS(svgns, 'line');
- svg.appendChild(sightLine);
+ sightLine.classList.add('sight-line');
+ sightLine.setAttributeNS(null, 'x1', svgX1);
+ sightLine.setAttributeNS(null, 'y1', svgY1);
+ sightLine.setAttributeNS(null, 'x2', svgX2);
+ sightLine.setAttributeNS(null, 'y2', svgY2);
- let coords = [
- counter.dataset.x,
- counter.dataset.y,
- e.target.dataset.x,
- e.target.dataset.y
- ].map(n => parseInt(n));
+ svg.insertBefore(sightLine, ptGrp);
- console.log(offset_distance(...coords))
+ let coords = [
+ counter.dataset.x,
+ counter.dataset.y,
+ e.target.dataset.x,
+ e.target.dataset.y
+ ].map(n => parseInt(n));
- let lineCoords = linedraw(...coords);
- lineCoords.shift();
- let s = lineCoords.map(([x, y]) => `use[data-x="${x}"][data-y="${y}"]`).join(', ');
- ptGrp.querySelectorAll(s).forEach(p => p.classList.add('active'));
+ info.querySelector('#hex-count').textContent = offset_distance(...coords);
+ info.style.display = 'block';
+
+ let lineCoords = linedraw(...coords);
+ lineCoords.shift();
+ let s = lineCoords.map(([x, y]) => `use[data-x="${x}"][data-y="${y}"]`).join(', ');
+ ptGrp.querySelectorAll(s).forEach(p => p.classList.add('active'));
+ }
}
}
});
point.addEventListener('mouseout', e => {
+ info.querySelector('#hex-count').textContent = '-';
+ info.style.display = 'none';
ptGrp.querySelectorAll('.active').forEach(el => el.removeAttribute('class'));
svg.querySelectorAll('.sight-line').forEach(el => el.remove());
});