Add random rotate action

This commit is contained in:
Jeremie Pardou-Piquemal 2020-11-21 22:15:59 +01:00 committed by Jérémie Pardou-Piquemal
parent 83d11c1418
commit bbcf6351f3
3 changed files with 56 additions and 1 deletions

View file

@ -123,6 +123,10 @@ export const itemMap = {
"rotate90", "rotate90",
"stack", "stack",
"shuffle", "shuffle",
"randomlyRotate30",
"randomlyRotate45",
"randomlyRotate90",
"randomlyRotate180",
"clone", "clone",
"lock", "lock",
"remove", "remove",
@ -136,6 +140,10 @@ export const itemMap = {
"rotate90", "rotate90",
"stack", "stack",
"shuffle", "shuffle",
"randomlyRotate30",
"randomlyRotate45",
"randomlyRotate90",
"randomlyRotate180",
"clone", "clone",
"lock", "lock",
"remove", "remove",

View file

@ -13,7 +13,7 @@ import { getActionsFromItem } from "./";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { shuffle as shuffleArray } from "../../utils"; import { shuffle as shuffleArray, randInt } from "../../utils";
import deleteIcon from "../../images/delete.svg"; import deleteIcon from "../../images/delete.svg";
import stackIcon from "../../images/stack.svg"; import stackIcon from "../../images/stack.svg";
@ -129,6 +129,19 @@ export const useItemActions = () => {
swapItems(selectedItems, shuffledItems); swapItems(selectedItems, shuffledItems);
}, [selectedItems, swapItems]); }, [selectedItems, swapItems]);
const randomlyRotateSelectedItems = React.useCallback(
(angle, maxRotateCount) => {
batchUpdateItems(selectedItems, (item) => {
console.log(randInt(0, 3));
const rotation =
((item.rotation || 0) + angle * randInt(0, maxRotateCount)) % 360;
console.log(item.rotation, rotation);
return { ...item, rotation };
});
},
[selectedItems, batchUpdateItems]
);
// Tap/Untap elements // Tap/Untap elements
const toggleTap = useRecoilCallback( const toggleTap = useRecoilCallback(
({ snapshot }) => async () => { ({ snapshot }) => async () => {
@ -281,6 +294,34 @@ export const useItemActions = () => {
multiple: true, multiple: true,
icon: shuffleIcon, icon: shuffleIcon,
}, },
randomlyRotate30: {
action: randomlyRotateSelectedItems.bind(null, 30, 11),
label: t("Rotate randomly 30"),
shortcut: "",
multiple: false,
icon: rotateIcon,
},
randomlyRotate45: {
action: randomlyRotateSelectedItems.bind(null, 90, 7),
label: t("Rotate randomly 45"),
shortcut: "",
multiple: false,
icon: rotateIcon,
},
randomlyRotate90: {
action: randomlyRotateSelectedItems.bind(null, 90, 3),
label: t("Rotate randomly 90"),
shortcut: "",
multiple: false,
icon: rotateIcon,
},
randomlyRotate180: {
action: randomlyRotateSelectedItems.bind(null, 180, 1),
label: t("Rotate randomly 180"),
shortcut: "",
multiple: false,
icon: rotateIcon,
},
rotate30: { rotate30: {
action: rotate.bind(null, 30), action: rotate.bind(null, 30),
label: t("Rotate 30"), label: t("Rotate 30"),
@ -335,6 +376,7 @@ export const useItemActions = () => {
cloneItem, cloneItem,
toggleLock, toggleLock,
removeSelectedItems, removeSelectedItems,
randomlyRotateSelectedItems,
] ]
); );
@ -349,6 +391,7 @@ export const useItemActions = () => {
rotate, rotate,
actionMap, actionMap,
availableActions, availableActions,
randomlyRotate: randomlyRotateSelectedItems,
}; };
}; };

View file

@ -56,3 +56,7 @@ export const getPointerState = (e) => {
}; };
} }
}; };
export const randInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};