-
Notifications
You must be signed in to change notification settings - Fork 18
Add flip selection with H/V keyboard shortcuts #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -580,6 +580,47 @@ function _wireExtraKeyboardShortcuts() { | |
| }, true); | ||
| } | ||
| } | ||
|
|
||
| function flipSelection(horizontal) { | ||
| if (!rectSelection.active) return; | ||
| const c = getFrameCanvas(rectSelection.L, rectSelection.F, rectSelection.key); | ||
| if (!c) return; | ||
| const ctx = c.getContext("2d", { willReadFrequently: true }); | ||
| if (!ctx) return; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here— not sure what would be best but i figure if |
||
|
|
||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -749,6 +790,14 @@ function onWindowKeyDown(e) { | |
| return; | ||
| } | ||
| } | ||
| if (rectSelection.active && (e.key.toLowerCase() === "h" || e.key.toLowerCase() === "v")) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") { | ||
|
|
||
There was a problem hiding this comment.
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
flipSelectionshould only be called when the rectSelection is already active.