Skip to content

Commit 3f1e80c

Browse files
committed
Progress for multi-color support.
1 parent cf88f0a commit 3f1e80c

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

src/pg/inputPixelEditor/__examples__/basic/basic.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { patterns } from './constants';
88
import template from './basic.html';
99
import style from './basic.css';
1010

11+
const IconPicker = 'M19.35,11.72L17.22,13.85L15.81,12.43L8.1,20.14L3.5,22L2,20.5L3.86,15.9L11.57,8.19L10.15,6.78L12.28,4.65L19.35,11.72M16.76,3C17.93,1.83 19.83,1.83 21,3C22.17,4.17 22.17,6.07 21,7.24L19.08,9.16L14.84,4.92L16.76,3M5.56,17.03L4.5,19.5L6.97,18.44L14.4,11L13,9.6L5.56,17.03Z';
1112
const IconTrash = 'M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z';
1213
const IconLayerEdit = 'M4.63 10.27L3 9L12 2L19.94 8.17L12.5 15.61L12 16L4.63 10.27M10 18.94V18.11L10.59 17.53L10.63 17.5L4.62 12.81L3 14.07L10 19.5V18.94M21.7 12.58L20.42 11.3C20.21 11.09 19.86 11.09 19.65 11.3L18.65 12.3L20.7 14.35L21.7 13.35C21.91 13.14 21.91 12.79 21.7 12.58M12 21H14.06L20.11 14.93L18.06 12.88L12 18.94V21Z';
1314

@@ -221,17 +222,62 @@ export default class XPgInputPixelEditorBasic extends HTMLElement {
221222
label: 'Alpha',
222223
key: 'a',
223224
editable: true,
225+
}, {
226+
label: 'Selected',
227+
key: 'selected',
228+
hideLabel: true,
229+
}, {
230+
label: 'Select',
231+
key: 'select',
232+
hideLabel: true,
224233
}, {
225234
label: 'Delete',
226235
key: 'delete',
227236
hideLabel: true,
228237
}];
238+
this.$colors.data.push(...this.$input.getColors().map(([r, g, b, a], i) => {
239+
return createTableItem({
240+
r,
241+
g,
242+
b,
243+
a,
244+
selected: i === 1,
245+
select: {
246+
type: PgTableCellButtonIcon,
247+
icon: IconPicker,
248+
value: i,
249+
},
250+
delete: {
251+
type: PgTableCellButtonIcon,
252+
icon: IconTrash,
253+
value: i,
254+
}
255+
});
256+
}));
257+
this.$colors.addEventListener('action', (e: any) => {
258+
const { getColumn, getRows, key } = e.detail;
259+
switch(key) {
260+
case 'select':
261+
getRows().forEach(({ getColumn }) => {
262+
getColumn('selected').value = false;
263+
});
264+
getColumn('selected').value = true;
265+
this.$input.selectColor(getColumn('select').value);
266+
break;
267+
}
268+
});
229269
this.$addColor.addEventListener('click', () => {
230270
this.$colors.data.push(createTableItem({
231271
r: 5,
232272
g: 5,
233273
b: 5,
234274
a: 1,
275+
selected: false,
276+
select: {
277+
type: PgTableCellButtonIcon,
278+
icon: IconPicker,
279+
value: this.$colors.data.length,
280+
},
235281
delete: {
236282
type: PgTableCellButtonIcon,
237283
icon: IconTrash

src/pg/inputPixelEditor/inputPixelEditor.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ export default class PgInputPixelEditor extends HTMLElement {
116116
#undoHistory: History[] = [];
117117
#redoHistory: History[] = [];
118118
#context: CanvasRenderingContext2D;
119+
#color: number = 1;
119120
#colors: Color[] = [
120121
[0, 0, 0, 0],
121-
[0, 0, 0, 1]
122+
[0, 0, 0, 1],
123+
[255, 0, 255, 1]
122124
];
123125
#baseLayer: HTMLCanvasElement;
124126
#baseLayerContext: CanvasRenderingContext2D;
@@ -535,7 +537,7 @@ export default class PgInputPixelEditor extends HTMLElement {
535537
this.#startY = newY;
536538
this.#x = newX;
537539
this.#y = newY;
538-
const color = event.buttons === 32 ? 0 : 1;
540+
const color = event.buttons === 32 ? 0 : this.#color;
539541
switch (this.#inputMode) {
540542
case InputMode.Pixel:
541543
this.#setPixel(newX, newY, color);
@@ -660,7 +662,7 @@ export default class PgInputPixelEditor extends HTMLElement {
660662
points.push([newX, newY]);
661663
}
662664
// Is Eraser
663-
const color = event.buttons === 32 ? 0 : 1;
665+
const color = event.buttons === 32 ? 0 : this.#color;
664666
// Shape tools only care about the last point
665667
if (points.length === 0) { return; }
666668
const [lastX, lastY] = points.at(-1) as [number, number];
@@ -1023,6 +1025,14 @@ export default class PgInputPixelEditor extends HTMLElement {
10231025
this.#init();
10241026
}
10251027

1028+
selectColor(index) {
1029+
this.#color = index;
1030+
}
1031+
1032+
getColors() {
1033+
return this.#colors;
1034+
}
1035+
10261036
addColor(r, g, b, a) {
10271037
this.#colors.push([r, g, b, a]);
10281038
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function getOutline(grid) {
2+
const outline: number[][] = [];
3+
const rows = grid.length;
4+
const cols = grid[0].length;
5+
6+
// Directions: [dx, dy]
7+
const dirs = [
8+
[0, -1], // up
9+
[1, 0], // right
10+
[0, 1], // down
11+
[-1, 0] // left
12+
];
13+
14+
for (let y = 0; y < rows; y++) {
15+
for (let x = 0; x < cols; x++) {
16+
if (grid[y][x] !== 1) continue;
17+
18+
// Check if this cell touches the boundary of the shape
19+
let isBoundary = false;
20+
21+
for (const [dx, dy] of dirs) {
22+
const nx = x + dx;
23+
const ny = y + dy;
24+
25+
// If neighbor is out of bounds OR is zero, this is an outline cell
26+
if (ny < 0 || ny >= rows || nx < 0 || nx >= cols || grid[ny][nx] === 0) {
27+
isBoundary = true;
28+
break;
29+
}
30+
}
31+
32+
if (isBoundary) {
33+
outline.push([x, y]);
34+
}
35+
}
36+
}
37+
38+
return outline;
39+
}

0 commit comments

Comments
 (0)