Skip to content

Commit

Permalink
refactor: Use common handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
miyanokomiya committed Apr 7, 2024
1 parent f8860f8 commit 650a280
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 258 deletions.
15 changes: 15 additions & 0 deletions src/composables/shapeHandlers/simplePolygonHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getExpansionFn,
getMigrateRelativePointFn,
getNextDirection2,
getNextDirection4,
getNormalizedSimplePolygonShape,
getShapeDetransform,
getShapeDirection,
Expand Down Expand Up @@ -380,3 +381,17 @@ export function handleSwitchDirection2(
ctx.patchShapes(layoutPatch);
return newSelectionHubState;
}

export function handleSwitchDirection4(
ctx: Pick<AppCanvasStateContext, "patchShapes" | "getShapeComposite">,
shape: SimplePolygonShape,
) {
const patch = {
direction: getNextDirection4(getShapeDirection(shape)),
} as Partial<SimplePolygonShape>;
const layoutPatch = getPatchByLayouts(ctx.getShapeComposite(), {
update: { [shape.id]: patch },
});
ctx.patchShapes(layoutPatch);
return newSelectionHubState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ import { movingShapeControlState } from "../movingShapeControlState";
import { DocumentSymbolShape } from "../../../../shapes/polygons/documentSymbol";
import { applyAffine, clamp } from "okageo";
import {
SimplePolygonShape,
getDirectionalLocalAbsolutePoints,
getExpansionFn,
getMigrateRelativePointFn,
getNextDirection4,
getNormalizedSimplePolygonShape,
getShapeDetransform,
getShapeDirection,
getShapeTransform,
} from "../../../../shapes/simplePolygon";
import { renderValueLabel } from "../../../../utils/renderer";
import {
SimplePolygonHandler,
getResizeByState,
handleSwitchDirection4,
newSimplePolygonHandler,
renderShapeBounds,
} from "../../../shapeHandlers/simplePolygonHandler";
import { newSelectionHubState } from "../selectionHubState";
import { getPatchByLayouts } from "../../../shapeLayoutHandler";

export const newDocumentSymbolSelectedState = defineSingleSelectedHandlerState<
DocumentSymbolShape,
Expand Down Expand Up @@ -82,64 +76,11 @@ export const newDocumentSymbolSelectedState = defineSingleSelectedHandlerState<
});
};
case "top":
return () => {
return movingShapeControlState<DocumentSymbolShape>({
targetId: targetShape.id,
patchFn: (shape, p) => {
const resized = shapeComposite.transformShape(shape, getExpansionFn(shape, 0)(shape, p));
const migrateFn = getMigrateRelativePointFn(shape, resized);
return {
...resized,
c0: migrateFn(shape.c0, { x: 0.75, y: 1 }),
};
},
getControlFn: (shape) => {
const s = getNormalizedSimplePolygonShape(shape);
return applyAffine(getShapeTransform(s), { x: s.width / 2, y: 0 });
},
renderFn: (ctx, renderCtx, shape) => {
renderShapeBounds(
renderCtx,
ctx.getStyleScheme(),
shapeComposite.getLocalRectPolygon(shape),
);
},
});
};
return () => getResizeByState(0, shapeComposite, targetShape, [["c0", { x: 0.75, y: 1 }]]);
case "bottom":
return () => {
return movingShapeControlState<DocumentSymbolShape>({
targetId: targetShape.id,
patchFn: (shape, p) => {
const resized = shapeComposite.transformShape(shape, getExpansionFn(shape, 2)(shape, p));
const migrateFn = getMigrateRelativePointFn(shape, resized);
return {
...resized,
c0: migrateFn(shape.c0, { x: 0.75, y: 1 }),
};
},
getControlFn: (shape) => {
const s = getNormalizedSimplePolygonShape(shape);
return applyAffine(getShapeTransform(s), { x: s.width / 2, y: s.height });
},
renderFn: (ctx, renderCtx, shape) => {
renderShapeBounds(
renderCtx,
ctx.getStyleScheme(),
shapeComposite.getLocalRectPolygon(shape),
);
},
});
};
return () => getResizeByState(2, shapeComposite, targetShape, [["c0", { x: 0.75, y: 1 }]]);
case "direction4": {
const patch = {
direction: getNextDirection4(getShapeDirection(targetShape)),
} as Partial<SimplePolygonShape>;
const layoutPatch = getPatchByLayouts(shapeComposite, {
update: { [targetShape.id]: patch },
});
ctx.patchShapes(layoutPatch);
return newSelectionHubState;
return handleSwitchDirection4(ctx, targetShape);
}
}
}
Expand Down
69 changes: 5 additions & 64 deletions src/composables/states/appCanvas/hexagon/hexagonSelectedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@ import { movingShapeControlState } from "../movingShapeControlState";
import { HexagonShape } from "../../../../shapes/polygons/hexagon";
import { add, applyAffine, clamp, getRadian, rotate } from "okageo";
import {
SimplePolygonShape,
getDirectionalLocalAbsolutePoints,
getExpansionFn,
getMigrateRelativePointFn,
getNextDirection2,
getNormalizedSimplePolygonShape,
getShapeDetransform,
getShapeDirection,
getShapeTransform,
} from "../../../../shapes/simplePolygon";
import { getCrossLineAndLine, snapRadianByAngle } from "../../../../utils/geometry";
import { COMMAND_EXAM_SRC } from "../commandExams";
import { renderValueLabel } from "../../../../utils/renderer";
import {
SimplePolygonHandler,
getResizeByState,
handleSwitchDirection2,
newSimplePolygonHandler,
renderShapeBounds,
} from "../../../shapeHandlers/simplePolygonHandler";
import { newSelectionHubState } from "../selectionHubState";
import { getPatchByLayouts } from "../../../shapeLayoutHandler";
import { AppCanvasStateContext } from "../core";

export const newHexagonSelectedState = defineSingleSelectedHandlerState<HexagonShape, SimplePolygonHandler, never>(
Expand Down Expand Up @@ -126,64 +120,11 @@ export const newHexagonSelectedState = defineSingleSelectedHandlerState<HexagonS
});
};
case "bottom":
return () => {
return movingShapeControlState<HexagonShape>({
targetId: targetShape.id,
patchFn: (shape, p) => {
const resized = shapeComposite.transformShape(shape, getExpansionFn(shape, 2)(shape, p));
const migrateFn = getMigrateRelativePointFn(shape, resized);
return {
...resized,
c0: migrateFn(shape.c0, { x: 0, y: 0 }),
};
},
getControlFn: (shape) => {
const s = getNormalizedSimplePolygonShape(shape);
return applyAffine(getShapeTransform(s), { x: s.width / 2, y: s.height });
},
renderFn: (ctx, renderCtx, shape) => {
renderShapeBounds(
renderCtx,
ctx.getStyleScheme(),
shapeComposite.getLocalRectPolygon(shape),
);
},
});
};
return () => getResizeByState(2, shapeComposite, targetShape, [["c0", { x: 0, y: 0 }]]);
case "right":
return () => {
return movingShapeControlState<HexagonShape>({
targetId: targetShape.id,
patchFn: (shape, p) => {
const resized = shapeComposite.transformShape(shape, getExpansionFn(shape, 1)(shape, p));
const migrateFn = getMigrateRelativePointFn(shape, resized);
return {
...resized,
c0: migrateFn(shape.c0, { x: 0, y: 0 }),
};
},
getControlFn: (shape) => {
const s = getNormalizedSimplePolygonShape(shape);
return applyAffine(getShapeTransform(s), { x: s.width, y: s.height / 2 });
},
renderFn: (ctx, renderCtx, shape) => {
renderShapeBounds(
renderCtx,
ctx.getStyleScheme(),
shapeComposite.getLocalRectPolygon(shape),
);
},
});
};
return () => getResizeByState(1, shapeComposite, targetShape, [["c0", { x: 0, y: 0 }]]);
case "direction4": {
const patch = {
direction: getNextDirection2(getShapeDirection(targetShape)),
} as Partial<SimplePolygonShape>;
const layoutPatch = getPatchByLayouts(shapeComposite, {
update: { [targetShape.id]: patch },
});
ctx.patchShapes(layoutPatch);
return newSelectionHubState;
return handleSwitchDirection2(ctx, targetShape);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { ParallelogramShape, getMaxParallelogramCornerRadius } from "../../../../shapes/polygons/parallelogram";
import { movingShapeControlState } from "../movingShapeControlState";
import {
SimplePolygonShape,
getDirectionalLocalAbsolutePoints,
getExpansionFn,
getMigrateRelativePointFn,
getNextDirection2,
getNormalizedSimplePolygonShape,
getShapeDetransform,
getShapeDirection,
getShapeTransform,
} from "../../../../shapes/simplePolygon";
import { add, applyAffine, clamp, getRadian, rotate } from "okageo";
Expand All @@ -18,11 +13,10 @@ import { renderValueLabel } from "../../../../utils/renderer";
import {
EDGE_ANCHOR_MARGIN,
SimplePolygonHandler,
getResizeByState,
handleSwitchDirection2,
newSimplePolygonHandler,
renderShapeBounds,
} from "../../../shapeHandlers/simplePolygonHandler";
import { getPatchByLayouts } from "../../../shapeLayoutHandler";
import { newSelectionHubState } from "../selectionHubState";

export const newParallelogramSelectedState = defineSingleSelectedHandlerState<
ParallelogramShape,
Expand Down Expand Up @@ -140,64 +134,11 @@ export const newParallelogramSelectedState = defineSingleSelectedHandlerState<
});
};
case "left":
return () => {
return movingShapeControlState<ParallelogramShape>({
targetId: targetShape.id,
patchFn: (shape, p) => {
const resized = shapeComposite.transformShape(shape, getExpansionFn(shape, 3)(shape, p));
const migrateFn = getMigrateRelativePointFn(shape, resized);
return {
...resized,
c0: migrateFn(targetShape.c0, { x: 0.5, y: 0 }),
};
},
getControlFn: (shape) => {
const s = getNormalizedSimplePolygonShape(shape);
return applyAffine(getShapeTransform(s), { x: 0, y: s.height / 2 });
},
renderFn: (ctx, renderCtx, shape) => {
renderShapeBounds(
renderCtx,
ctx.getStyleScheme(),
shapeComposite.getLocalRectPolygon(shape),
);
},
});
};
return () => getResizeByState(3, shapeComposite, targetShape, [["c0", { x: 0.5, y: 0 }]]);
case "right":
return () => {
return movingShapeControlState<ParallelogramShape>({
targetId: targetShape.id,
patchFn: (shape, p) => {
const resized = shapeComposite.transformShape(shape, getExpansionFn(shape, 1)(shape, p));
const migrateFn = getMigrateRelativePointFn(shape, resized);
return {
...resized,
c0: migrateFn(targetShape.c0, { x: 0.5, y: 0 }),
};
},
getControlFn: (shape) => {
const s = getNormalizedSimplePolygonShape(shape);
return applyAffine(getShapeTransform(s), { x: s.width, y: s.height / 2 });
},
renderFn: (ctx, renderCtx, shape) => {
renderShapeBounds(
renderCtx,
ctx.getStyleScheme(),
shapeComposite.getLocalRectPolygon(shape),
);
},
});
};
return () => getResizeByState(1, shapeComposite, targetShape, [["c0", { x: 0.5, y: 0 }]]);
case "direction4": {
const patch = {
direction: getNextDirection2(getShapeDirection(targetShape)),
} as Partial<SimplePolygonShape>;
const layoutPatch = getPatchByLayouts(shapeComposite, {
update: { [targetShape.id]: patch },
});
ctx.patchShapes(layoutPatch);
return newSelectionHubState;
return handleSwitchDirection2(ctx, targetShape);
}
}
}
Expand Down
Loading

0 comments on commit 650a280

Please sign in to comment.