Skip to content

Commit 2a8a45b

Browse files
committed
Fix palette color undo/repo
1 parent 1cb6017 commit 2a8a45b

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/ide/pixeleditor.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,22 @@ export class PaletteFormatToRGB extends PixNode {
698698
}
699699
}
700700

701+
export class PaletteEditorView extends PixNode {
702+
updateCells: () => void;
703+
704+
constructor(updateCells: () => void) {
705+
super();
706+
this.updateCells = updateCells;
707+
}
708+
updateLeft() {
709+
return true;
710+
}
711+
updateRight() {
712+
this.updateCells();
713+
return true;
714+
}
715+
}
716+
701717
export abstract class Compositor extends PixNode {
702718

703719
tilemap: Uint8Array[]; // tilemap images

src/ide/views/asseteditor.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,9 @@ export class AssetEditorView implements ProjectView, pixed.EditorContext {
206206
}
207207

208208
// TODO: move to pixeleditor.ts?
209-
addPaletteEditorViews(parentdiv: JQuery, pal2rgb: pixed.PaletteFormatToRGB, callback) {
209+
addPaletteEditorViews(parentdiv: JQuery, pal2rgb: pixed.PaletteFormatToRGB, callback): () => void {
210210
var adual = $('<div class="asset_dual"/>').appendTo(parentdiv);
211211
var aeditor = $('<div class="asset_editor"/>').hide(); // contains editor, when selected
212-
// TODO: they need to update when refreshed from right
213212
var allrgbimgs = [];
214213
pal2rgb.getAllColors().forEach((rgba) => { allrgbimgs.push(new Uint32Array([rgba])); }); // array of array of 1 rgb color (for picker)
215214
var atable = $('<table/>').appendTo(adual);
@@ -224,13 +223,17 @@ export class AssetEditorView implements ProjectView, pixed.EditorContext {
224223
layout.push(["", i, Math.min(len - i, imgsperline)]);
225224
}
226225
}
226+
var cells: { cell: JQuery, index: number }[] = [];
227227
function updateCell(cell, j) {
228228
var val = pal2rgb.words[j];
229229
var rgb = pal2rgb.palette[j];
230230
var hexcol = '#' + hex(rgb2bgr(rgb), 6);
231231
var textcol = (rgb & 0x008000) ? 'black' : 'white';
232232
cell.text(hex(val, 2)).css('background-color', hexcol).css('color', textcol);
233233
}
234+
function updateAllCells() {
235+
cells.forEach(({ cell, index }) => updateCell(cell, index));
236+
}
234237
// iterate over each row of the layout
235238
layout.forEach(([name, start, len]) => {
236239
if (start < pal2rgb.palette.length) { // skip row if out of range
@@ -243,6 +246,7 @@ export class AssetEditorView implements ProjectView, pixed.EditorContext {
243246
inds.reverse();
244247
inds.forEach((i) => {
245248
var cell = $('<td/>').addClass('asset_cell asset_editable').appendTo(arow);
249+
cells.push({ cell, index: i });
246250
updateCell(cell, i);
247251
cell.click((e) => {
248252
var chooser = new pixed.ImageChooser();
@@ -258,6 +262,7 @@ export class AssetEditorView implements ProjectView, pixed.EditorContext {
258262
});
259263
}
260264
});
265+
return updateAllCells;
261266
}
262267

263268
addPixelEditor(parentdiv: JQuery, firstnode: pixed.PixNode, fmt: pixed.PixelEditorImageFormat) {
@@ -279,9 +284,7 @@ export class AssetEditorView implements ProjectView, pixed.EditorContext {
279284
firstnode.addRight(pal2rgb);
280285
// TODO: refresh twice?
281286
firstnode.refreshRight();
282-
// TODO: add view objects
283-
// TODO: show which one is selected?
284-
this.addPaletteEditorViews(parentdiv, pal2rgb,
287+
var updateAllCells = this.addPaletteEditorViews(parentdiv, pal2rgb,
285288
(index, newvalue) => {
286289
console.log('set entry', index, '=', newvalue);
287290
// TODO: this forces update of palette rgb colors and file data
@@ -290,6 +293,8 @@ export class AssetEditorView implements ProjectView, pixed.EditorContext {
290293
pal2rgb.updateRight();
291294
pal2rgb.refreshLeft();
292295
});
296+
// add view node to repaint cells when data changes (e.g. undo)
297+
pal2rgb.addRight(new pixed.PaletteEditorView(updateAllCells));
293298
}
294299

295300
ensureFileDiv(fileid: string): JQuery<HTMLElement> {

0 commit comments

Comments
 (0)