Implement impose position
This commit is contained in:
parent
0b41f7ac2c
commit
10d13b246d
3 changed files with 50 additions and 4 deletions
|
@ -13,11 +13,52 @@
|
|||
* or make them lose their property as a rigid body
|
||||
*/
|
||||
|
||||
import { IContainerModel } from '../../../Interfaces/IContainerModel';
|
||||
import { constraintBodyInsideUnallocatedWidth } from './RigidBodyBehaviors';
|
||||
|
||||
/**
|
||||
* Impose the container position
|
||||
* Apply the following modification to the overlapping rigid body container :
|
||||
*
|
||||
*/
|
||||
export function ImposePosition(){
|
||||
export function ImposePosition(container: IContainerModel): IContainerModel {
|
||||
if (container.parent === undefined ||
|
||||
container.parent === null) {
|
||||
return container;
|
||||
}
|
||||
|
||||
// Get the closest one
|
||||
const rigidBodies = container.parent.children.filter(
|
||||
child => child.properties.isRigidBody && !child.properties.isAnchor
|
||||
);
|
||||
|
||||
const overlappingContainers = getOverlappingContainers(container, rigidBodies);
|
||||
for (const overlappingContainer of overlappingContainers) {
|
||||
constraintBodyInsideUnallocatedWidth(overlappingContainer);
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
function getOverlappingContainers(
|
||||
container: IContainerModel,
|
||||
siblings: IContainerModel[]
|
||||
): IContainerModel[] {
|
||||
const min1 = container.properties.x;
|
||||
const max1 = container.properties.x + Number(container.properties.width);
|
||||
const overlappingContainers: IContainerModel[] = [];
|
||||
for (const sibling of siblings) {
|
||||
if (sibling === container) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const min2 = sibling.properties.x;
|
||||
const max2 = sibling.properties.x + Number(sibling.properties.width);
|
||||
const isOverlapping = Math.min(max1, max2) - Math.max(min1, min2) > 0;
|
||||
|
||||
if (!isOverlapping) {
|
||||
continue;
|
||||
}
|
||||
|
||||
overlappingContainers.push(sibling);
|
||||
}
|
||||
return overlappingContainers;
|
||||
}
|
|
@ -112,7 +112,7 @@ function constraintBodyInsideSpace(
|
|||
* @param container
|
||||
* @returns Updated container
|
||||
*/
|
||||
function constraintBodyInsideUnallocatedWidth(
|
||||
export function constraintBodyInsideUnallocatedWidth(
|
||||
container: IContainerModel
|
||||
): IContainerModel {
|
||||
if (container.parent === null) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import { findContainerById } from '../../utils/itertools';
|
|||
import { getCurrentHistory } from './Editor';
|
||||
import { RecalculatePhysics } from './Behaviors/RigidBodyBehaviors';
|
||||
import { INPUT_TYPES } from '../Properties/PropertiesInputTypes';
|
||||
import { ImposePosition } from './Behaviors/AnchorBehaviors';
|
||||
|
||||
/**
|
||||
* Handled the property change event in the properties form
|
||||
|
@ -42,6 +43,10 @@ export function OnPropertyChange(
|
|||
(container.properties as any)[key] = value;
|
||||
}
|
||||
|
||||
if (container.properties.isAnchor) {
|
||||
ImposePosition(container);
|
||||
}
|
||||
|
||||
if (container.properties.isRigidBody) {
|
||||
RecalculatePhysics(container);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue