Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions celstomp/js/ui/interaction-shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,47 @@ function _wireExtraKeyboardShortcuts() {
}, true);
}
}

function flipSelection(horizontal) {
if (!rectSelection.active) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we be OK throwing an error in this case? IDK what the defacto standard is for error handling on the web but I figure we should handle this more gracefully, as flipSelection should only be called when the rectSelection is already active.

const c = getFrameCanvas(rectSelection.L, rectSelection.F, rectSelection.key);
if (!c) return;
const ctx = c.getContext("2d", { willReadFrequently: true });
if (!ctx) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here— not sure what would be best but i figure if c or ctx are null then we're encountering anomalous behavior which we want to log instead of returning silently to avoid a crash.


const selSnap = ctx.getImageData(rectSelection.x, rectSelection.y, rectSelection.w, rectSelection.h);
const fullSnap = ctx.getImageData(0, 0, contentW, contentH);

beginGlobalHistoryStep(rectSelection.L, rectSelection.F, rectSelection.key);

ctx.clearRect(rectSelection.x, rectSelection.y, rectSelection.w, rectSelection.h);

const flipped = ctx.createImageData(rectSelection.w, rectSelection.h);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not "mission critical" but at a glance it seems like there should be some more optimal way to snapshot a region of the canvas, flip it, and write it back. Some combination of transformation operations on the dest canvas should work to accomplish the same, I think?

const src = selSnap.data;
const dst = flipped.data;

for (let y = 0; y < rectSelection.h; y++) {
for (let x = 0; x < rectSelection.w; x++) {
const srcIdx = (y * rectSelection.w + x) * 4;
const dstY = horizontal ? y : (rectSelection.h - 1 - y);
const dstX = horizontal ? (rectSelection.w - 1 - x) : x;
const dstIdx = (dstY * rectSelection.w + dstX) * 4;
dst[dstIdx] = src[srcIdx];
dst[dstIdx + 1] = src[srcIdx + 1];
dst[dstIdx + 2] = src[srcIdx + 2];
dst[dstIdx + 3] = src[srcIdx + 3];
}
}

ctx.putImageData(flipped, rectSelection.x, rectSelection.y);

markGlobalHistoryDirty();
commitGlobalHistoryStep();
recomputeHasContent(rectSelection.L, rectSelection.F, rectSelection.key);
queueRenderAll();
updateTimelineHasContent(rectSelection.F);
}

function wireKeyboardShortcuts() {
if (document._celstompKeysWired) return;
document._celstompKeysWired = true;
Expand Down Expand Up @@ -749,6 +790,14 @@ function onWindowKeyDown(e) {
return;
}
}
if (rectSelection.active && (e.key.toLowerCase() === "h" || e.key.toLowerCase() === "v")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not "mission-critical" but in the future we should maybe be factoring this "event handler" code into a different function.

const tag = e.target && e.target.tagName ? e.target.tagName.toUpperCase() : "";
if (tag !== "INPUT" && tag !== "TEXTAREA" && tag !== "SELECT") {
e.preventDefault();
flipSelection(e.key.toLowerCase() === "h");
return;
}
}
if ((e.key === "Delete" || e.key === "Backspace") && rectSelection.active) {
const tag = e.target && e.target.tagName ? e.target.tagName.toUpperCase() : "";
if (tag !== "INPUT" && tag !== "TEXTAREA" && tag !== "SELECT") {
Expand Down
7 changes: 6 additions & 1 deletion celstomp/parts/modals.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ document.getElementById('part-modals').innerHTML = `
<div class="shortcutRow"><kbd>E</kbd><span>Prev Frame</span></div>
<div class="shortcutRow"><kbd>R</kbd><span>Next Frame</span></div>
</div>
<div class="shortcutSection">
<h4>Selection</h4>
<div class="shortcutRow"><kbd>H</kbd><span>Flip Horizontal</span></div>
<div class="shortcutRow"><kbd>V</kbd><span>Flip Vertical</span></div>
<div class="shortcutRow"><kbd>Del</kbd><span>Delete Selection</span></div>
</div>
<div class="shortcutSection">
<h4>Actions</h4>
<div class="shortcutRow"><kbd>Space</kbd><span>Play/Pause</span></div>
<div class="shortcutRow"><kbd>Ctrl+Z</kbd><span>Undo</span></div>
<div class="shortcutRow"><kbd>Ctrl+Y</kbd><span>Redo</span></div>
<div class="shortcutRow"><kbd>Ctrl+Shift+Z</kbd><span>Redo</span></div>
<div class="shortcutRow"><kbd>Del</kbd><span>Delete Selection/Color</span></div>
<div class="shortcutRow"><kbd>F</kbd><span>Fill Current Frame</span></div>
<div class="shortcutRow"><kbd>O</kbd><span>Toggle Onion</span></div>
</div>
Expand Down