mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(ui): r to center & fit stage on document
This commit is contained in:
parent
cafc1839e2
commit
21e814d766
@ -9,6 +9,7 @@ import { debouncedRenderers, renderers as normalRenderers } from 'features/contr
|
|||||||
import {
|
import {
|
||||||
$bbox,
|
$bbox,
|
||||||
$currentFill,
|
$currentFill,
|
||||||
|
$document,
|
||||||
$isDrawing,
|
$isDrawing,
|
||||||
$isMouseDown,
|
$isMouseDown,
|
||||||
$lastAddedPoint,
|
$lastAddedPoint,
|
||||||
@ -72,6 +73,7 @@ const useStageRenderer = (stage: Konva.Stage, container: HTMLDivElement | null,
|
|||||||
const selectedEntityIdentifier = useAppSelector((s) => s.canvasV2.selectedEntityIdentifier);
|
const selectedEntityIdentifier = useAppSelector((s) => s.canvasV2.selectedEntityIdentifier);
|
||||||
const maskOpacity = useAppSelector((s) => s.canvasV2.settings.maskOpacity);
|
const maskOpacity = useAppSelector((s) => s.canvasV2.settings.maskOpacity);
|
||||||
const bbox = useAppSelector((s) => s.canvasV2.bbox);
|
const bbox = useAppSelector((s) => s.canvasV2.bbox);
|
||||||
|
const document = useAppSelector((s) => s.canvasV2.document);
|
||||||
const lastCursorPos = useStore($lastCursorPos);
|
const lastCursorPos = useStore($lastCursorPos);
|
||||||
const lastMouseDownPos = useStore($lastMouseDownPos);
|
const lastMouseDownPos = useStore($lastMouseDownPos);
|
||||||
const isMouseDown = useStore($isMouseDown);
|
const isMouseDown = useStore($isMouseDown);
|
||||||
@ -108,7 +110,8 @@ const useStageRenderer = (stage: Konva.Stage, container: HTMLDivElement | null,
|
|||||||
$selectedEntity.set(selectedEntity);
|
$selectedEntity.set(selectedEntity);
|
||||||
$bbox.set({ x: bbox.x, y: bbox.y, width: bbox.width, height: bbox.height });
|
$bbox.set({ x: bbox.x, y: bbox.y, width: bbox.width, height: bbox.height });
|
||||||
$currentFill.set(currentFill);
|
$currentFill.set(currentFill);
|
||||||
}, [selectedEntity, tool, bbox, currentFill]);
|
$document.set(document);
|
||||||
|
}, [selectedEntity, tool, bbox, currentFill, document]);
|
||||||
|
|
||||||
const onPosChanged = useCallback(
|
const onPosChanged = useCallback(
|
||||||
(arg: PosChangedArg, entityType: CanvasEntity['type']) => {
|
(arg: PosChangedArg, entityType: CanvasEntity['type']) => {
|
||||||
@ -243,6 +246,7 @@ const useStageRenderer = (stage: Konva.Stage, container: HTMLDivElement | null,
|
|||||||
setLastMouseDownPos: $lastMouseDownPos.set,
|
setLastMouseDownPos: $lastMouseDownPos.set,
|
||||||
getSpaceKey: $spaceKey.get,
|
getSpaceKey: $spaceKey.get,
|
||||||
setStageAttrs: $stageAttrs.set,
|
setStageAttrs: $stageAttrs.set,
|
||||||
|
getDocument: $document.get,
|
||||||
onBrushLineAdded,
|
onBrushLineAdded,
|
||||||
onEraserLineAdded,
|
onEraserLineAdded,
|
||||||
onPointAddedToLine,
|
onPointAddedToLine,
|
||||||
|
@ -45,6 +45,7 @@ type Arg = {
|
|||||||
setStageAttrs: (attrs: StageAttrs) => void;
|
setStageAttrs: (attrs: StageAttrs) => void;
|
||||||
getSelectedEntity: () => CanvasEntity | null;
|
getSelectedEntity: () => CanvasEntity | null;
|
||||||
getSpaceKey: () => boolean;
|
getSpaceKey: () => boolean;
|
||||||
|
getDocument: () => CanvasV2State['document'];
|
||||||
onBrushLineAdded: (arg: BrushLineAddedArg, entityType: CanvasEntity['type']) => void;
|
onBrushLineAdded: (arg: BrushLineAddedArg, entityType: CanvasEntity['type']) => void;
|
||||||
onEraserLineAdded: (arg: EraserLineAddedArg, entityType: CanvasEntity['type']) => void;
|
onEraserLineAdded: (arg: EraserLineAddedArg, entityType: CanvasEntity['type']) => void;
|
||||||
onPointAddedToLine: (arg: PointAddedToLineArg, entityType: CanvasEntity['type']) => void;
|
onPointAddedToLine: (arg: PointAddedToLineArg, entityType: CanvasEntity['type']) => void;
|
||||||
@ -144,6 +145,7 @@ export const setStageEventHandlers = ({
|
|||||||
setStageAttrs,
|
setStageAttrs,
|
||||||
getSelectedEntity,
|
getSelectedEntity,
|
||||||
getSpaceKey,
|
getSpaceKey,
|
||||||
|
getDocument,
|
||||||
onBrushLineAdded,
|
onBrushLineAdded,
|
||||||
onEraserLineAdded,
|
onEraserLineAdded,
|
||||||
onPointAddedToLine,
|
onPointAddedToLine,
|
||||||
@ -506,6 +508,20 @@ export const setStageEventHandlers = ({
|
|||||||
} else if (e.key === ' ') {
|
} else if (e.key === ' ') {
|
||||||
setToolBuffer(getToolState().selected);
|
setToolBuffer(getToolState().selected);
|
||||||
setTool('view');
|
setTool('view');
|
||||||
|
} else if (e.key === 'r') {
|
||||||
|
// Fit & center the document on the stage
|
||||||
|
const width = stage.width();
|
||||||
|
const height = stage.height();
|
||||||
|
const document = getDocument();
|
||||||
|
const docWidthWithBuffer = document.width + 20;
|
||||||
|
const docHeightWithBuffer = document.height + 20;
|
||||||
|
const scale = Math.min(Math.min(width / docWidthWithBuffer, height / docHeightWithBuffer), 1);
|
||||||
|
const x = (width - docWidthWithBuffer * scale) / 2;
|
||||||
|
const y = (height - docHeightWithBuffer * scale) / 2;
|
||||||
|
stage.setAttrs({ x, y, width, height, scaleX: scale, scaleY: scale });
|
||||||
|
setStageAttrs({ x, y, width, height, scale });
|
||||||
|
scaleToolPreview(stage, getToolState());
|
||||||
|
renderBackgroundLayer(stage);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
window.addEventListener('keydown', onKeyDown);
|
window.addEventListener('keydown', onKeyDown);
|
||||||
|
@ -318,6 +318,7 @@ export const $toolState = atom<CanvasV2State['tool']>(deepClone(initialState.too
|
|||||||
export const $currentFill = atom<RgbaColor>(DEFAULT_RGBA_COLOR);
|
export const $currentFill = atom<RgbaColor>(DEFAULT_RGBA_COLOR);
|
||||||
export const $selectedEntity = atom<CanvasEntity | null>(null);
|
export const $selectedEntity = atom<CanvasEntity | null>(null);
|
||||||
export const $bbox = atom<IRect>({ x: 0, y: 0, width: 0, height: 0 });
|
export const $bbox = atom<IRect>({ x: 0, y: 0, width: 0, height: 0 });
|
||||||
|
export const $document = atom<CanvasV2State['document']>(deepClone(initialState.document));
|
||||||
|
|
||||||
export const canvasV2PersistConfig: PersistConfig<CanvasV2State> = {
|
export const canvasV2PersistConfig: PersistConfig<CanvasV2State> = {
|
||||||
name: canvasV2Slice.name,
|
name: canvasV2Slice.name,
|
||||||
|
Loading…
Reference in New Issue
Block a user