WIP: solution to bringing active element to top?

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 22:41:33 -07:00
parent 08cdbe0565
commit b8fc681e53
2 changed files with 59 additions and 38 deletions

View File

@ -173,7 +173,7 @@ polygon.firing-arc[data-allegiance="attacker"] {
/* r: 5px;*/ /* r: 5px;*/
/*}*/ /*}*/
g.counter { g.counter, g.grid-top {
r: 5px; r: 5px;
} }
@ -196,6 +196,7 @@ g.counter.selected {
g.counter use.primary-weapon { g.counter use.primary-weapon {
r: inherit; r: inherit;
pointer-events: none;
} }
g.counter use.troop-number, g.counter use.squad-number { g.counter use.troop-number, g.counter use.squad-number {
@ -264,12 +265,14 @@ g[data-y]:nth-child(odd) {
transform: scale(var(--scale)); transform: scale(var(--scale));
} }
[data-x]:hover use[href="#hex"], [data-q][data-r][data-s][data-t]:hover use[href="#hex"] { [data-x]:hover use[href="#hex"],
[data-q][data-r][data-s][data-t]:hover use[href="#hex"],
[data-q][data-r][data-s][data-t].hover use[href="#hex"] {
opacity: 1; opacity: 1;
fill: orange; fill: orange;
stroke: black; stroke: black;
} }
/**/
/*[data-q][data-r][data-s][data-t] g.counter,*/ /*[data-q][data-r][data-s][data-t] g.counter,*/
/*[data-q][data-r][data-s][data-t] g.counter .troop-number,*/ /*[data-q][data-r][data-s][data-t] g.counter .troop-number,*/
/*[data-q][data-r][data-s][data-t] g.counter .squad-number {*/ /*[data-q][data-r][data-s][data-t] g.counter .squad-number {*/
@ -497,20 +500,6 @@ text.elevation {
fill: black; fill: black;
} }
.radial-icon {
transition: cx 0.25s, cy 0.25s;
}
[data-q][data-r][data-s][data-t] .radial-icon {
cx: calc(var(--cx) / 20);
cy: calc(var(--cy) / 20);
}
[data-q][data-r][data-s][data-t]:hover .radial-icon {
cx: var(--cx);
cy: var(--cy);
}
use[class^="counter-"] { use[class^="counter-"] {
--scale: 1; --scale: 1;
--translateX: -5px; --translateX: -5px;
@ -541,14 +530,18 @@ g.counter use[class^="counter-"] {
} }
/* Counters placed on a hex */ /* Counters placed on a hex */
[data-q][data-r][data-s][data-t]:hover > g.counter ~ use[class^="counter-"] { [data-q][data-r][data-s][data-t]:hover > g.counter ~ use[class^="counter-"],
[data-q][data-r][data-s][data-t].hover > g.counter ~ use[class^="counter-"],
.grid-top use[class^="counter-"] {
x: calc(var(--x) * 2); x: calc(var(--x) * 2);
y: calc(var(--y) * 2); y: calc(var(--y) * 2);
--translateY: -5px; --translateY: -5px;
} }
[data-q][data-r][data-s][data-t]:hover > use[class^="counter-"] { [data-q][data-r][data-s][data-t]:hover > use[class^="counter-"],
[data-q][data-r][data-s][data-t].hover > use[class^="counter-"] {
x: calc(var(--x) * 1.5); x: calc(var(--x) * 1.5);
y: calc(var(--y) * 1.5); y: calc(var(--y) * 1.5);
--translateY: -5px; --translateY: -5px;
} }

View File

@ -6,6 +6,10 @@ import { Observable } from "./observable";
let svg, let svg,
placing = []; placing = [];
const top = {
collection: new Map()
};
function getCellContents(cell) { function getCellContents(cell) {
return cell.querySelectorAll('*:not(use[href="#hex"])'); return cell.querySelectorAll('*:not(use[href="#hex"])');
} }
@ -19,7 +23,7 @@ function getHex(cell) {
} }
function getCellOccupant(cell) { function getCellOccupant(cell) {
return cell.querySelector('.counter'); return cell.querySelector('.counter') || svg.querySelector('.grid-top .counter');
} }
function getCells(svg) { function getCells(svg) {
@ -203,23 +207,22 @@ function endMove() {
} }
} }
function returnPieces(top) { async function returnToParent(top) {
[...top.container.children].forEach(piece => { //[...top.container.children].forEach(child => {
top.collection.get(piece).parent.append(piece); top.container.querySelectorAll('*:not(use[href="#hex"]').forEach(child => {
top.collection.delete(piece); top.collection.get(child).parent.append(child);
top.collection.delete(child);
}); });
} }
export function start(el) { export function start(el) {
svg = el; svg = el;
top.container = svg.querySelector('.grid-top');
const startingLocations = svg.querySelector('.start-locations'); const startingLocations = svg.querySelector('.start-locations');
startingLocations && getUnits(startingLocations).forEach(unit => unit.addEventListener('click', selectOffBoard)); startingLocations && getUnits(startingLocations).forEach(unit => unit.addEventListener('click', selectOffBoard));
const top = { svg.addEventListener('pointerout', e => console.log('pointerout svg'));
container: svg.querySelector('.grid-top'),
collection: new Map()
};
getCells(svg).forEach(cell => { getCells(svg).forEach(cell => {
cell.addEventListener('click', e => { cell.addEventListener('click', e => {
@ -227,8 +230,11 @@ export function start(el) {
let toPlace = placing.pop(); let toPlace = placing.pop();
if (isCounter(toPlace) || isMechTemplate(toPlace)) { if (isCounter(toPlace) || isMechTemplate(toPlace)) {
getHex(cell).after(toPlace); //getHex(cell).after(toPlace);
if (isCounter(toPlace)) arrangeCounters(cell); top.collection.set(toPlace, { parent: cell });
top.container.append(toPlace);
//if (isCounter(toPlace)) arrangeCounters(cell);
if (isCounter(toPlace)) arrangeCounters(top.container);
removeEventListener("keydown", handleMechTemplateRotation); removeEventListener("keydown", handleMechTemplateRotation);
} else if (toPlace && !occupant) { } else if (toPlace && !occupant) {
soldier.place(svg, toPlace, cell); soldier.place(svg, toPlace, cell);
@ -289,10 +295,24 @@ export function start(el) {
}); });
cell.addEventListener('pointerover', () => { cell.addEventListener('pointerover', () => {
if (!top.container.contains(cell)) { if (!cell.classList.contains('hover')) {
returnPieces(top); [...top.container.children].forEach(child => {
top.collection.set(cell, { parent: cell.parentElement }); if (!cell.contains(child)) {
top.container.append(cell); top.collection.get(child).parent.append(child);
top.collection.delete(child);
}
});
top.container.setAttributeNS(null, 'transform', cell.getAttributeNS(null, 'transform'));
[...cell.children].filter(c => c.getAttributeNS(null, 'href') !== '#hex').forEach(child => {
if (!top.container.contains(child)) {
top.collection.set(child, { parent: cell });
top.container.append(child);
}
});
svg.querySelectorAll('g.hover').forEach(el => el.classList.remove('hover'));
cell.classList.add('hover');
} }
const selected = getSelected(); const selected = getSelected();
@ -314,6 +334,7 @@ export function start(el) {
}); });
cell.addEventListener('pointerout', () => { cell.addEventListener('pointerout', () => {
console.log('pointerout cell', cell);
getActiveSightLine(svg) && clearSightLine(); getActiveSightLine(svg) && clearSightLine();
const occupant = getCellOccupant(cell); const occupant = getCellOccupant(cell);
@ -324,16 +345,22 @@ export function start(el) {
}); });
}); });
//const cell = document.querySelector('[data-q="0"][data-r="0"][data-s="0"][data-t="0"]');
//const povr = new PointerEvent('pointerover');
//const pout = new PointerEvent('pointerout');
//cell.dispatchEvent(povr);
//cell.dispatchEvent(pout);
// debug // // debug //
// Add a trooper counter // Add a trooper counter
//const attacker = { dataset: { allegiance: 'attacker', number: 1, squad: 1 }}; const attacker = { dataset: { allegiance: 'attacker', number: 1, squad: 1 }};
// const defender = { dataset: { allegiance: 'defender', number: 1, squad: 2 }}; // const defender = { dataset: { allegiance: 'defender', number: 1, squad: 2 }};
//const cell = getCell(0, 0, 0, 0); const cell = getCell(0, 0, 0, 0);
//const trooper = soldier.createCounter(attacker, 'blazer'); const trooper = soldier.createCounter(attacker, 'blazer');
//const trooperSack = document.createElementNS(svgns, 'g'); //const trooperSack = document.createElementNS(svgns, 'g');
//trooperSack.classList.add('cell-contents'); //trooperSack.classList.add('cell-contents');
//trooperSack.append(trooper); //trooperSack.append(trooper);
//soldier.place(svg, trooperSack, cell); soldier.place(svg, trooper, cell);
// Add some counters in an unoccupied cell // Add some counters in an unoccupied cell
//const countersCell = getCell(-1, 1, 0, 0); //const countersCell = getCell(-1, 1, 0, 0);
@ -379,6 +406,7 @@ export function setFiringArc() {
isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-q'); isOnBoard = counter => counter && counter.parentElement.hasAttribute('data-q');
if (isOnBoard(counter)) { if (isOnBoard(counter)) {
returnToParent(top);
firingArc.set(svg, this.dataset.size, counter, getCellPosition(counter.parentElement)); firingArc.set(svg, this.dataset.size, counter, getCellPosition(counter.parentElement));
} }
} }