Refactor RigidBodyBehaviors
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
286a5e9c00
commit
82eae4971e
1 changed files with 74 additions and 62 deletions
|
@ -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
|
||||
* 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 only works with widths meaning in 1D (with lines)
|
||||
const newUnallocatedWidths = getAvailableWidthsTwoLines(
|
||||
unallocatedSpace.x,
|
||||
unallocatedSpace.x + unallocatedSpace.width,
|
||||
unallocatedSpace,
|
||||
childX,
|
||||
childX + childWidth
|
||||
childWidth
|
||||
);
|
||||
|
||||
// Concat the new list of SizePointer pointing to availables spaces
|
||||
|
@ -240,88 +250,90 @@ function getAvailableWidths(
|
|||
|
||||
/**
|
||||
* Returns the unallocated widths between two lines in 1D
|
||||
* @param unalloctedSpaceLeft left of the first line
|
||||
* @param unallocatedSpaceRight rigth of the first line
|
||||
* @param rectLeft left of the second line
|
||||
* @param rectRight right of the second line
|
||||
* @param unalloctedSpace unallocated space
|
||||
* @param rectX left of the second line
|
||||
* @param rectWidth width of the second line
|
||||
* @returns Available widths
|
||||
*/
|
||||
function getAvailableWidthsTwoLines(
|
||||
unalloctedSpaceLeft: number,
|
||||
unallocatedSpaceRight: number,
|
||||
rectLeft: number,
|
||||
rectRight: number
|
||||
unallocatedSpace: ISizePointer,
|
||||
rectX: number,
|
||||
rectWidth: number
|
||||
): ISizePointer[] {
|
||||
// let object 1 be the unallocated space
|
||||
// and object 2 be the rect
|
||||
const unallocatedSpaceRight = unallocatedSpace.x + unallocatedSpace.width;
|
||||
const rectRight = rectX + rectWidth;
|
||||
|
||||
if (unallocatedSpaceRight < rectLeft ||
|
||||
unalloctedSpaceLeft > rectRight
|
||||
) {
|
||||
// object 1 and 2 are not overlapping
|
||||
return [{
|
||||
x: unalloctedSpaceLeft,
|
||||
width: unallocatedSpaceRight - unalloctedSpaceLeft
|
||||
}];
|
||||
const isNotOverlapping = unallocatedSpaceRight < rectX ||
|
||||
unallocatedSpace.x > rectRight;
|
||||
if (isNotOverlapping) {
|
||||
return [unallocatedSpace];
|
||||
}
|
||||
|
||||
if (rectLeft < unalloctedSpaceLeft && rectRight > unallocatedSpaceRight) {
|
||||
// object 2 is overlapping full width
|
||||
const isOverlappingFullWidth = rectX < unallocatedSpace.x && rectRight > unallocatedSpaceRight;
|
||||
if (isOverlappingFullWidth) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (unalloctedSpaceLeft >= rectLeft) {
|
||||
// object 2 is partially overlapping on the left
|
||||
if (unallocatedSpaceRight - rectRight <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
x: rectRight,
|
||||
width: unallocatedSpaceRight - rectRight
|
||||
}
|
||||
];
|
||||
const isOverlappingOnTheLeft = unallocatedSpace.x >= rectX;
|
||||
if (isOverlappingOnTheLeft) {
|
||||
return getAvailableWidthsLeft(unallocatedSpaceRight, rectRight);
|
||||
}
|
||||
|
||||
if (rectRight >= unallocatedSpaceRight) {
|
||||
// object 2 is partially overlapping on the right
|
||||
if (unalloctedSpaceLeft - rectRight <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
x: unalloctedSpaceLeft,
|
||||
width: rectRight - unalloctedSpaceLeft
|
||||
}
|
||||
];
|
||||
const isOverlappingOnTheRight = rectRight >= unallocatedSpaceRight;
|
||||
if (isOverlappingOnTheRight) {
|
||||
return getAvailableWidthsRight(unallocatedSpace.x, rectRight);
|
||||
}
|
||||
|
||||
// object 2 is overlapping in the middle
|
||||
return getAvailableWidthsMiddle(unallocatedSpace.x, unallocatedSpaceRight, rectX, rectRight);
|
||||
}
|
||||
|
||||
function getAvailableWidthsLeft(unallocatedSpaceRight: number, rectRight: number): ISizePointer[] {
|
||||
if (unallocatedSpaceRight - rectRight <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
x: rectRight,
|
||||
width: unallocatedSpaceRight - rectRight
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function getAvailableWidthsRight(unalloctedSpaceX: number, rectRight: number): ISizePointer[] {
|
||||
if (rectRight - unalloctedSpaceX <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
x: unalloctedSpaceX,
|
||||
width: rectRight - unalloctedSpaceX
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function getAvailableWidthsMiddle(
|
||||
unalloctedSpaceX: number,
|
||||
unallocatedSpaceRight: number,
|
||||
rectX: number,
|
||||
rectRight: number
|
||||
): ISizePointer[] {
|
||||
const sizePointers: ISizePointer[] = [];
|
||||
if (rectLeft - unalloctedSpaceLeft > 0) {
|
||||
|
||||
if (rectX - unalloctedSpaceX > 0) {
|
||||
sizePointers.push({
|
||||
x: unalloctedSpaceLeft,
|
||||
width: rectLeft - unalloctedSpaceLeft
|
||||
x: unalloctedSpaceX,
|
||||
width: rectX - unalloctedSpaceX
|
||||
});
|
||||
}
|
||||
|
||||
if (unallocatedSpaceRight - rectRight > 0) {
|
||||
sizePointers.push({
|
||||
x: rectRight,
|
||||
width: unallocatedSpaceRight - rectRight
|
||||
});
|
||||
}
|
||||
|
||||
return sizePointers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue