Compare commits

...

2 commits

Author SHA1 Message Date
Eric NGUYEN
82eae4971e Refactor RigidBodyBehaviors
All checks were successful
continuous-integration/drone/push Build is passing
2022-08-17 15:39:37 +02:00
Eric NGUYEN
286a5e9c00 Fix rigid body in which children thinks there was enough space
All checks were successful
continuous-integration/drone/push Build is passing
2022-08-17 15:12:38 +02:00

View file

@ -181,6 +181,17 @@ export function constraintBodyInsideUnallocatedWidth(
); );
} }
/**
* Check if a container can fit inside a size space
* @param container Container to check
* @param sizePointer Size space to check
* @returns
*/
const isFitting = (
container: IContainerModel,
sizePointer: ISizePointer
): boolean => container.properties.width <= sizePointer.width;
/** /**
* Get the unallocated widths inside a container * Get the unallocated widths inside a container
* An allocated width is defined by its the widths of the children that are rigid bodies. * An allocated width is defined by its the widths of the children that are rigid bodies.
@ -222,10 +233,9 @@ function getAvailableWidths(
// We need to calculate the overlap between the two containers // We need to calculate the overlap between the two containers
// We only works with widths meaning in 1D (with lines) // We only works with widths meaning in 1D (with lines)
const newUnallocatedWidths = getAvailableWidthsTwoLines( const newUnallocatedWidths = getAvailableWidthsTwoLines(
unallocatedSpace.x, unallocatedSpace,
unallocatedSpace.x + unallocatedSpace.width,
childX, childX,
childX + childWidth childWidth
); );
// Concat the new list of SizePointer pointing to availables spaces // Concat the new list of SizePointer pointing to availables spaces
@ -240,59 +250,49 @@ function getAvailableWidths(
/** /**
* Returns the unallocated widths between two lines in 1D * Returns the unallocated widths between two lines in 1D
* @param unalloctedSpaceLeft left of the first line * @param unalloctedSpace unallocated space
* @param unallocatedSpaceRight rigth of the first line * @param rectX left of the second line
* @param rectLeft left of the second line * @param rectWidth width of the second line
* @param rectRight right of the second line
* @returns Available widths * @returns Available widths
*/ */
function getAvailableWidthsTwoLines( function getAvailableWidthsTwoLines(
unalloctedSpaceLeft: number, unallocatedSpace: ISizePointer,
unallocatedSpaceRight: number, rectX: number,
rectLeft: number, rectWidth: number
rectRight: number
): ISizePointer[] { ): ISizePointer[] {
if (unallocatedSpaceRight < rectLeft || const unallocatedSpaceRight = unallocatedSpace.x + unallocatedSpace.width;
unalloctedSpaceLeft > rectRight const rectRight = rectX + rectWidth;
) {
// object 1 and 2 are not overlapping const isNotOverlapping = unallocatedSpaceRight < rectX ||
return [{ unallocatedSpace.x > rectRight;
x: unalloctedSpaceLeft, if (isNotOverlapping) {
width: unallocatedSpaceRight - unalloctedSpaceLeft return [unallocatedSpace];
}];
} }
if (rectLeft < unalloctedSpaceLeft && rectRight > unallocatedSpaceRight) { const isOverlappingFullWidth = rectX < unallocatedSpace.x && rectRight > unallocatedSpaceRight;
// object 2 is overlapping full width if (isOverlappingFullWidth) {
return []; return [];
} }
if (unalloctedSpaceLeft >= rectLeft) { const isOverlappingOnTheLeft = unallocatedSpace.x >= rectX;
// object 2 is partially overlapping on the left if (isOverlappingOnTheLeft) {
return [ return getAvailableWidthsLeft(unallocatedSpaceRight, rectRight);
{
x: rectRight,
width: unallocatedSpaceRight - rectRight
}
];
} }
if (rectRight >= unallocatedSpaceRight) { const isOverlappingOnTheRight = rectRight >= unallocatedSpaceRight;
// object 2 is partially overlapping on the right if (isOverlappingOnTheRight) {
return [ return getAvailableWidthsRight(unallocatedSpace.x, rectRight);
{ }
x: unalloctedSpaceLeft,
width: rectRight - unalloctedSpaceLeft return getAvailableWidthsMiddle(unallocatedSpace.x, unallocatedSpaceRight, rectX, rectRight);
} }
];
function getAvailableWidthsLeft(unallocatedSpaceRight: number, rectRight: number): ISizePointer[] {
if (unallocatedSpaceRight - rectRight <= 0) {
return [];
} }
// object 2 is overlapping in the middle
return [ return [
{
x: unalloctedSpaceLeft,
width: rectLeft - unalloctedSpaceLeft
},
{ {
x: rectRight, x: rectRight,
width: unallocatedSpaceRight - rectRight width: unallocatedSpaceRight - rectRight
@ -300,13 +300,40 @@ function getAvailableWidthsTwoLines(
]; ];
} }
/** function getAvailableWidthsRight(unalloctedSpaceX: number, rectRight: number): ISizePointer[] {
* Check if a container can fit inside a size space if (rectRight - unalloctedSpaceX <= 0) {
* @param container Container to check return [];
* @param sizePointer Size space to check }
* @returns
*/ return [
const isFitting = ( {
container: IContainerModel, x: unalloctedSpaceX,
sizePointer: ISizePointer width: rectRight - unalloctedSpaceX
): boolean => container.properties.width <= sizePointer.width; }
];
}
function getAvailableWidthsMiddle(
unalloctedSpaceX: number,
unallocatedSpaceRight: number,
rectX: number,
rectRight: number
): ISizePointer[] {
const sizePointers: ISizePointer[] = [];
if (rectX - unalloctedSpaceX > 0) {
sizePointers.push({
x: unalloctedSpaceX,
width: rectX - unalloctedSpaceX
});
}
if (unallocatedSpaceRight - rectRight > 0) {
sizePointers.push({
x: rectRight,
width: unallocatedSpaceRight - rectRight
});
}
return sizePointers;
}