ISSUE-017 - Implemented drag bounds that consider the dimensions of dragged element.
This is bugged as the resize observer I'm using doesn't update itself when I reveal the OverlayItem from hiding.
This commit is contained in:
1 parent
861d702a27
commit
aba5cc20be
5 files changed
+51
-25
No files matched your search
@@ -23,15 +23,6 @@ export function OverlayControl() {
|
|||||||
/* For prototyping use only */
|
/* For prototyping use only */
|
||||||
const [idNum, setIdNum] = useState(0);
|
const [idNum, setIdNum] = useState(0);
|
||||||
|
|
||||||
const getRandomArbitrary = (min: number, max: number) =>
|
|
||||||
Math.random() * (max - min) + min;
|
|
||||||
|
|
||||||
const posRandomizer = () => {
|
|
||||||
dispatch(setPosition({
|
|
||||||
x: Math.floor(getRandomArbitrary(-50, 850)),
|
|
||||||
y: Math.floor(getRandomArbitrary(-50, 650)),
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
const nextIdNum = () => {
|
const nextIdNum = () => {
|
||||||
const nextIdNum = (idNum + 1) % overlayIds.length;
|
const nextIdNum = (idNum + 1) % overlayIds.length;
|
||||||
setIdNum(nextIdNum);
|
setIdNum(nextIdNum);
|
||||||
@@ -45,7 +36,7 @@ export function OverlayControl() {
|
|||||||
if (currentOverlayItem == null) {
|
if (currentOverlayItem == null) {
|
||||||
dispatch(initNewOverlayItem({
|
dispatch(initNewOverlayItem({
|
||||||
overlayId: overlayIds[idNum],
|
overlayId: overlayIds[idNum],
|
||||||
initialPosition: { x: 0, y: 300 },
|
initialPosition: { x: 400, y: 300 },
|
||||||
setVisible: true,
|
setVisible: true,
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
@@ -63,12 +54,6 @@ export function OverlayControl() {
|
|||||||
|
|
||||||
{/* For prototyping use only */}
|
{/* For prototyping use only */}
|
||||||
<br />
|
<br />
|
||||||
<a
|
|
||||||
onClick={posRandomizer}
|
|
||||||
>
|
|
||||||
Random Location
|
|
||||||
</a>
|
|
||||||
<br />
|
|
||||||
<a
|
<a
|
||||||
onClick={nextIdNum}
|
onClick={nextIdNum}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -2,24 +2,47 @@ import { useSelector, useDispatch } from "react-redux";
|
|||||||
import { useTransition, animated } from "@react-spring/web";
|
import { useTransition, animated } from "@react-spring/web";
|
||||||
import { useDrag } from "@use-gesture/react";
|
import { useDrag } from "@use-gesture/react";
|
||||||
|
|
||||||
|
import { useDimensions } from "../../../utils/hooks";
|
||||||
import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector";
|
import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector";
|
||||||
import { setPosition } from "../../../features/overlayTool/overlayToolSlice";
|
import { setPosition } from "../../../features/overlayTool/overlayToolSlice";
|
||||||
|
|
||||||
import { OverlayItemContainer } from "./styles";
|
import { OverlayItemContainer } from "./styles";
|
||||||
|
|
||||||
export function OverlayDisplay() {
|
type OverlayDisplayProps = {
|
||||||
|
containerDimensions: {
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
},
|
||||||
|
bleedover?: number,
|
||||||
|
};
|
||||||
|
export function OverlayDisplay({
|
||||||
|
containerDimensions: { width: containerWidth, height: containerHeight },
|
||||||
|
bleedover = 0,
|
||||||
|
}: OverlayDisplayProps) {
|
||||||
|
const dispatch = useDispatch();
|
||||||
const {
|
const {
|
||||||
currentOverlayId,
|
currentOverlayId,
|
||||||
currentOverlayItem,
|
currentOverlayItem,
|
||||||
visible,
|
visible,
|
||||||
} = useSelector(overlayToolSelector);
|
} = useSelector(overlayToolSelector);
|
||||||
|
|
||||||
const dispatch = useDispatch();
|
// Monitor element dimensions to calculate min/max drag coordinates.
|
||||||
|
const [overlayItemRef, {
|
||||||
|
width: overlayItemWidth,
|
||||||
|
height: overlayItemHeight,
|
||||||
|
}] = useDimensions<HTMLDivElement>();
|
||||||
|
const
|
||||||
|
minX = -bleedover,
|
||||||
|
minY = -bleedover,
|
||||||
|
maxX = containerWidth - overlayItemWidth + bleedover,
|
||||||
|
maxY = containerHeight - overlayItemHeight + bleedover;
|
||||||
|
|
||||||
const bindOverlayItem = useDrag(({ args, delta }) => {
|
const bindOverlayItem = useDrag(({ args, delta }) => {
|
||||||
|
const newX = args[0].xPos + delta[0];
|
||||||
|
const newY = args[0].yPos + delta[1];
|
||||||
dispatch(setPosition({
|
dispatch(setPosition({
|
||||||
x: args[0].xPos + delta[0],
|
x: newX >= minX && newX <= maxX ? newX : args[0].xPos,
|
||||||
y: args[0].yPos + delta[1],
|
y: newY >= minY && newY <= maxY ? newY : args[0].yPos,
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -54,7 +77,11 @@ export function OverlayDisplay() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<animated.div style={{ ...style, position: "absolute" }}>
|
<animated.div style={{ ...style, position: "absolute" }}>
|
||||||
<OverlayItemContainer visible={visible} tempColor={currentOverlayId}>
|
<OverlayItemContainer
|
||||||
|
ref={overlayItemRef}
|
||||||
|
visible={visible}
|
||||||
|
tempColor={currentOverlayId}
|
||||||
|
>
|
||||||
{currentOverlayId}
|
{currentOverlayId}
|
||||||
<br />
|
<br />
|
||||||
(x:{Math.floor(position.x)}px, y:{Math.floor(position.y)}px)
|
(x:{Math.floor(position.x)}px, y:{Math.floor(position.y)}px)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export const OverlayItemContainer = styled.div.attrs<OverlayItemContainerProps>(
|
|||||||
},
|
},
|
||||||
}))<OverlayItemContainerProps>`
|
}))<OverlayItemContainerProps>`
|
||||||
/* Temporary. Overlay will be transparent and based on an image. */
|
/* Temporary. Overlay will be transparent and based on an image. */
|
||||||
|
/* width: ${({ tempColor }) => tempColor == "red" ? "150px" : "100px"}; */
|
||||||
width: 150px;
|
width: 150px;
|
||||||
height: 150px;
|
height: 150px;
|
||||||
background-color: ${({ tempColor }) => tempColor};
|
background-color: ${({ tempColor }) => tempColor};
|
||||||
|
|||||||
@@ -16,11 +16,14 @@ import { OverlayDisplay } from "../../OverlayTool";
|
|||||||
|
|
||||||
import { workspaceObject } from "../workspaces";
|
import { workspaceObject } from "../workspaces";
|
||||||
|
|
||||||
|
const PREVIEW_WINDOW_HEIGHT = 600;
|
||||||
|
const PREVIEW_WINDOW_WIDTH = 800;
|
||||||
|
|
||||||
export const Workspace = () => {
|
export const Workspace = () => {
|
||||||
|
const dispatch = useDispatch();
|
||||||
const { workspaceId } = useParams();
|
const { workspaceId } = useParams();
|
||||||
const { animatedBG } = useSelector(workspaceSelector);
|
const { animatedBG } = useSelector(workspaceSelector);
|
||||||
const { currentWorkspaceId: overlayWorkspaceId } = useSelector(overlayToolSelector);
|
const { currentWorkspaceId: overlayWorkspaceId } = useSelector(overlayToolSelector);
|
||||||
const dispatch = useDispatch();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (workspaceId == null) return;
|
if (workspaceId == null) return;
|
||||||
@@ -45,13 +48,23 @@ export const Workspace = () => {
|
|||||||
centerChildren
|
centerChildren
|
||||||
>
|
>
|
||||||
<H2>{workspaceId ? workspaceObject[workspaceId].name : "Unknown"}</H2>
|
<H2>{workspaceId ? workspaceObject[workspaceId].name : "Unknown"}</H2>
|
||||||
<PreviewWindow height={600} width={800} animatedBG={animatedBG}>
|
<PreviewWindow
|
||||||
|
height={PREVIEW_WINDOW_HEIGHT}
|
||||||
|
width={PREVIEW_WINDOW_WIDTH}
|
||||||
|
animatedBG={animatedBG}
|
||||||
|
>
|
||||||
<MockupBlock
|
<MockupBlock
|
||||||
color="#415bad"
|
color="#415bad"
|
||||||
height="300px"
|
height="300px"
|
||||||
width="400px"
|
width="400px"
|
||||||
>Preview Item</MockupBlock>
|
>Preview Item</MockupBlock>
|
||||||
<OverlayDisplay />
|
<OverlayDisplay
|
||||||
|
containerDimensions={{
|
||||||
|
height: PREVIEW_WINDOW_HEIGHT,
|
||||||
|
width: PREVIEW_WINDOW_WIDTH,
|
||||||
|
}}
|
||||||
|
bleedover={50}
|
||||||
|
/>
|
||||||
</PreviewWindow>
|
</PreviewWindow>
|
||||||
<ControlsContainer>
|
<ControlsContainer>
|
||||||
<WorkspaceControls />
|
<WorkspaceControls />
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export const WorkspaceList = ({ headerOffset }: WorkspaceListProps) => {
|
|||||||
return (
|
return (
|
||||||
<WorkspaceListBox headerOffset={headerOffset}>
|
<WorkspaceListBox headerOffset={headerOffset}>
|
||||||
<Stack as="ul" gutter="md">
|
<Stack as="ul" gutter="md">
|
||||||
{workspaceList.map((id, i) => (
|
{workspaceList.map((id) => (
|
||||||
<WorkspaceListNavLink
|
<WorkspaceListNavLink
|
||||||
key={id}
|
key={id}
|
||||||
to={id}
|
to={id}
|
||||||
|
|||||||
Reference in new issue
Block a user