Skip to content

Commit f2dbe7a

Browse files
committed
Rewriting component hooks.
1 parent 61e29cc commit f2dbe7a

12 files changed

Lines changed: 165 additions & 13 deletions

File tree

src/components/pg/button/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ import PgButton from '@pictogrammers/components/pg/button';
3838
| `--pg-button-active-color` | `#fff` | `active` Text color |
3939
| `--pg-button-active-background-color` | `#453C4F` | `active` Background color |
4040
| `--pg-button-active-border-color` | `#453C4F` | `active` Border color |
41-
| `--pg-button-padding` | `0.25rem 0.5rem` | Padding. |
41+
| `--pg-button-padding-inline` | `0.5rem` | Padding inline. |
42+
| `--pg-button-padding-inline-start` | `0.5rem` | Padding inline start. |
43+
| `--pg-button-padding-inline-end` | `0.5rem` | Padding inline end. |
44+
| `--pg-button-padding-block` | `0.25rem` | Padding block. |
45+
| `--pg-button-padding-block-start` | `0.25rem` | Padding block start. |
46+
| `--pg-button-padding-block-end` | `0.25rem` | Padding block end. |
4247
| `--pg-button-font-size` | `1rem` | Font size. |
4348
| `--pg-button-line-height` | `1.5rem` | Line height. |
4449

src/components/pg/button/button.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
:host {
22
display: inline-flex;
3+
width: fit-content;
34
}
45

56
[part=button] {
@@ -12,7 +13,10 @@
1213
border: var(--pg-button-border-width, 1px) solid var(--pg-button-border-color, #453C4F);
1314
background-color: var(--pg-button-background-color, #FFFFFF);
1415
color: var(--pg-button-color, #453C4F);
15-
padding: var(--pg-button-padding, 0.25rem 0.5rem);
16+
padding-inline-start: var(--pg-button-padding-inline, var(--pg-button-padding-inline-start, 0.5rem));
17+
padding-inline-end: var(--pg-button-padding-inline, var(--pg-button-padding-inline-end, 0.5rem));
18+
padding-block-start: var(--pg-button-padding-block, var(--pg-button-padding-block-start, 0.25rem));
19+
padding-block-end: var(--pg-button-padding-block, var(--pg-button-padding-block-start, 0.25rem));
1620
border-radius: var(--pg-button-border-radius, 0.25rem);
1721
outline: none;
1822
--pg-icon-color: var(--pg-button-color, #453C4F);

src/components/pg/inputPixelEditor/README.md

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
The `pg-input-pixel-editor` component is used to edit images. Tailored for pen or mouse input the editor can be used for various image editing tasks.
44

55
```typescript
6-
import '@pictogrammers/components/pgInputPixelEditor';
6+
import '@pictogrammers/components/pg/inputPixelEditor';
77
import PgInputPixelEditor, {
88
InputMode,
99
LayerType,
10-
} from '@pictogrammers/components/pgInputPixelEditor';
10+
} from '@pictogrammers/components/pg/inputPixelEditor';
1111
```
1212

1313
```html
@@ -50,8 +50,10 @@ See usage for each method below.
5050
| `setData()` | - | Set layer data. |
5151
| `setJson(json)` | - | Set JSON file. |
5252
| `reset()` | - | Reset canvas and data. |
53-
| `getExportCanvas()` | - | Get new canvas of export. |
54-
| `await getExportPng(options, meta)` | - | Get image. |
53+
| `getExport()` | - | 2d array of pixel color indexes. Already calculated. |
54+
| `getExport(id)` | - | 2d array of pixel color indexes by export size. Minimal perf. |
55+
| `getExportCanvas()` | - | Get new prepopulated HTML canvas, ideal for advanced export screens. |
56+
| `await getExportPng(options, meta)` | - | Get png image with optional metadata. |
5557
| `undo()` | - | Undo. |
5658
| `hasUndo()` | - | Has undo |
5759
| `hasRedo()` | - | Has redo |
@@ -179,6 +181,11 @@ A complete JSON storage for a 10x10 image.
179181
[{ "color": 2, "path": "M...Z" }]
180182
]
181183
],
184+
"grid": {
185+
"vertical": [],
186+
"horizontal": []
187+
},
188+
"exports": [],
182189
"history": []
183190
}
184191
```
@@ -291,6 +298,85 @@ this.$input.addEventListener('reference', (e: any) => {
291298
});
292299
```
293300

301+
## Grid
302+
303+
Grids seperate the frame into regions. The vertical and horizontal lines can be assigned a unique color often used in fonts.
304+
305+
Grid lines on 0 or when equal or greater than to width/height are deleted. Applying a grid or resizing a frame will result in an error. Errors are handled by the consumer via the `error` event. `errorType: 'grid'`.
306+
307+
```text
308+
0, 0 | 1, 0 | 2, 0
309+
0, 1 | 1, 1 | 2, 1
310+
0, 2 | 1, 2 | 2, 2
311+
```
312+
313+
An example of a 9 segment grid for instance. Negative values offset from the right or bottom.
314+
315+
> Note: When configuring exports all grid colors with `color: 0` will be visible.
316+
317+
```json
318+
{
319+
"grid": [{
320+
"vertical": [
321+
{ "start": 10, "color": 0 },
322+
{ "start": -10, "color": 0 }
323+
],
324+
"horizontal": [
325+
{ "start": 10, "color": 0 },
326+
{ "start": -10, "color": 0 }
327+
]
328+
}]
329+
}
330+
```
331+
332+
Specific grid colors are configured on the editor level and do not persist in this input component. In a font the first 5 colors are reserved for the below lines.
333+
334+
- `1` - Ascender Line
335+
- `2` - Cap Line
336+
- `3` - Median / Mean line
337+
- `4` - Baseline
338+
- `5` - Decender Line
339+
340+
### Repeating Grids
341+
342+
A repeating grid is useful for tilesets and can be represented as.
343+
- `start` can be optional or null when `step` is defined.
344+
- `limit` can be optional or null. Null repeats for as many times as it can fit.
345+
- Similar to regular grids an error will throw if the first line is outside the `width` or `height`.
346+
347+
```json
348+
{ "step": 10, "limit": null, "color": 0 },
349+
{ "start": 5, "step": 10, "limit": 10, "color": 0 },
350+
```
351+
352+
## Export
353+
354+
Exports by default include the entire canvas, but can be resized to a specific pixel size or grid. The `x`, `y`, `width`, and `height` are optional.
355+
356+
Use `getExport(id)` with a `id` value to get the data grid. This is ideal for export previews.
357+
358+
```json
359+
{
360+
"exports": [{
361+
"id": "uuid",
362+
"unit": "pixel",
363+
"path": ["file.png"],
364+
"x": 0,
365+
"y": 0,
366+
"width": null,
367+
"height": null,
368+
}, {
369+
"id": "uuid",
370+
"unit": "grid",
371+
"path": ["folder", "bottomRight.png"],
372+
"x": 2,
373+
"y": 2,
374+
"width": 1,
375+
"height": 1,
376+
}]
377+
}
378+
```
379+
294380
## History
295381

296382
The history list contains every change made to the canvas. All feature insert an entry into history. Pens and pattern tools will group as to cut down history entries.

src/components/pg/inputPixelEditor/inputPixelEditor.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ interface Export {
9696
height?: number
9797
}
9898

99+
interface GridItem {
100+
value: number
101+
color: number
102+
}
103+
104+
interface Grid {
105+
vertical: GridItem[]
106+
horizontal: GridItem[]
107+
}
108+
99109
function toColor([r, g, b, a]: Color) {
100110
return `rgba(${r}, ${g}, ${b}, ${a})`;
101111
}
@@ -139,6 +149,7 @@ export default class PgInputPixelEditor extends HTMLElement {
139149
#selectionPixels = new Map<string, number[]>(); // 'x,y', [x, y]
140150
#selection: number[][] = [];
141151
#export: number[][] = [];
152+
#grid: Grid = { vertical: [], horizontal: [] };
142153
#undoHistory: History[] = [];
143154
#redoHistory: History[] = [];
144155
#context: CanvasRenderingContext2D;
@@ -1412,6 +1423,28 @@ export default class PgInputPixelEditor extends HTMLElement {
14121423
});
14131424
}
14141425

1426+
/**
1427+
* Draw pixel.
1428+
* @param grid 2d grid of colors
1429+
* @param layer Layer to apply the pixel.
1430+
*/
1431+
drawGrid(grid: number[][], layer = this.#layer) {
1432+
grid.forEach((row, y) => {
1433+
row.forEach((color, x) => {
1434+
this.#setPixel(x, y, color, layer);
1435+
});
1436+
})
1437+
}
1438+
1439+
/**
1440+
* Draw a single pixel.
1441+
*/
1442+
drawPixel(x: number, y: number, color: number, layer = this.#layer) {
1443+
this.#setPixel(x, y, color, layer);
1444+
}
1445+
1446+
// todo: add other draw commands
1447+
14151448
/**
14161449
* Flatten layers, always merges to lowest layer
14171450
* Note 0 is the lowest layer.
@@ -1496,6 +1529,10 @@ export default class PgInputPixelEditor extends HTMLElement {
14961529
});
14971530
}
14981531

1532+
getExport() {
1533+
return this.#export;
1534+
}
1535+
14991536
getExportPath() {
15001537
return bitmaskToPath(this.#export, { scale: 1 });
15011538
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type ExportType = 'pixel' | 'grid';

src/components/pg/inputText/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ import { PgInputText } from '@pictogrammers/components/pgInputText';
2525
| ---------- | -------- | ----------- |
2626
| change | | `{ detail: { value }` |
2727
| input | | `{ detail: { value }` |
28+
29+
## CSS Variables
30+
31+
| CSS Variables | Default | Description |
32+
| ------------------- | --------- | ----------- |
33+
| `--pg-button-padding-inline` | `0.5rem` | Padding inline. |
34+
| `--pg-button-padding-inline-start` | `0.5rem` | Padding inline start. |
35+
| `--pg-button-padding-inline-end` | `0.5rem` | Padding inline end. |
36+
| `--pg-button-padding-block` | `0.25rem` | Padding block. |
37+
| `--pg-button-padding-block-start` | `0.25rem` | Padding block start. |
38+
| `--pg-button-padding-block-end` | `0.25rem` | Padding block end. |

src/components/pg/inputText/inputText.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
[part=input] {
77
border: 1px solid var(--pg-input-text-border-color, #453C4F);
88
border-radius: 0.125rem;
9-
padding: var(--pg-input-text-padding, calc(0.5rem - 1px) 0.75rem);
9+
padding-inline-start: var(--pg-input-text-padding-inline, var(--pg-input-text-padding-inline-start, 0.5rem));
10+
padding-inline-end: var(--pg-input-text-padding-inline, var(--pg-input-text-padding-inline-end, 0.5rem));
11+
padding-block-start: var(--pg-input-text-padding-block, var(--pg-input-text-padding-block-start, 0.25rem));
12+
padding-block-end: var(--pg-input-text-padding-block, var(--pg-input-text-padding-block-start, 0.25rem));
1013
font-family: var(--pg-input-text-font-family, var(--pg-font-family));
1114
font-size: 1rem;
1215
outline: none;
1316
field-sizing: content;
14-
min-width: calc(100% - 1.5rem - 2px);
17+
min-width: calc(100% - 1rem - 2px);
1518
max-width: calc(var(--pg-input-text-max-width, 100%) - 1.5rem - 2px);
1619
background-color: transparent;
1720
}

src/components/pg/jsonNumber/jsonNumber.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[part=value] {
1212
user-select: text;
13-
--pg-input-text-padding: 0 0.25rem;
13+
--pg-input-text-padding-inline: 0.25rem;
1414
--pg-input-text-border-color: transparent;
1515
--pg-input-number-font-family: var(--pg-json-font-family);
1616
display: inline-flex;

src/components/pg/jsonString/jsonString.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[part=value] {
1212
user-select: text;
13-
--pg-input-text-padding: 0 0.25rem;
13+
--pg-input-text-padding-inline: 0.25rem;
1414
--pg-input-text-border-color: transparent;
1515
--pg-input-text-font-family: var(--pg-json-font-family);
1616
display: inline-flex;

src/components/pg/tableCellButtonIcon/tableCellButtonIcon.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
vertical-align: middle;
1212
justify-items: center;
1313
--pg-button-background-color: transparent;
14-
--pg-button-padding: 0;
14+
--pg-button-inline-start: 0;
15+
--pg-button-inline-end: 0;
16+
--pg-button-block-start: 0;
17+
--pg-button-block-end: 0;
1518
--pg-button-border-width: 0;
1619
}

0 commit comments

Comments
 (0)