checkArea()

in client/client/modules/render/utilities/zonesManager.js [68:168]


    checkArea(zoneName, area, translationVector) {
        const {key, rect, global} = area || {};
        let {margin} = area;
        if (rect === null || rect === undefined)
            return null;
        const {translateX, translateY} = translationVector;
        margin = margin ? margin : {
            marginX: 0,
            marginY: 0
        };
        const zone = this.zones.get(zoneName);
        if (zone !== undefined && zone !== null) {
            const {cachedAreas = []} = zone;
            const desiredArea = {
                height: rect.y2 - rect.y1,
                width: rect.x2 - rect.x1,
                x1: rect.x1 + global.x,
                x2: rect.x2 + global.x,
                y1: rect.y1 + global.y,
                y2: rect.y2 + global.y
            };
            const [cache] = this.useCaches
                ? cachedAreas
                    .filter(a => !!key && a.key === key &&
                        a.width === desiredArea.width &&
                        a.height === desiredArea.height
                    )
                : [];
            let conflicts = true;
            if (cache && (translateY || translateX)) {
                conflicts = false;
                if (translateY) {
                    desiredArea.y1 = cache.y1;
                    desiredArea.y2 = cache.y2;
                }
                if (translateX) {
                    desiredArea.x1 = cache.x1;
                    desiredArea.x2 = cache.x2;
                }
                cachedAreas.splice(cachedAreas.indexOf(cache), 1);
            } else {
                const checkZone = {
                    height: translateY !== 0 ? Infinity : desiredArea.height,
                    y1: translateY !== 0 ? -Infinity : desiredArea.y1,
                    y2: translateY !== 0 ? Infinity : desiredArea.y2,
                    width: translateX !== 0 ? Infinity : desiredArea.width,
                    x1: translateX !== 0 ? -Infinity : desiredArea.x1,
                    x2: translateX !== 0 ? Infinity : desiredArea.x2
                };
                const areasToCheck = zone.areas.filter(area => ZonesManager._rectanglesConflict(area, checkZone, margin));
                const testXZones = ZonesManager._getEmptyZones(zone.boundaries, areasToCheck, 'x', desiredArea.width + 2 * margin.marginX);
                const testYZones = ZonesManager._getEmptyZones(zone.boundaries, areasToCheck, 'y', desiredArea.height + 2 * margin.marginY);
                const yCandidate = ZonesManager._getZonePlacement(
                    testYZones,
                    translateY,
                    desiredArea.y1,
                    desiredArea.y2,
                    margin.marginX
                );
                const xCandidate = ZonesManager._getZonePlacement(
                    testXZones,
                    translateX,
                    desiredArea.x1,
                    desiredArea.x2,
                    margin.marginX
                );
                if (!xCandidate || !yCandidate) {
                    conflicts = true;
                } else {
                    desiredArea.x1 = xCandidate.start;
                    desiredArea.x2 = xCandidate.end;
                    desiredArea.y1 = yCandidate.start;
                    desiredArea.y2 = yCandidate.end;
                    if (xCandidate.conflicts && yCandidate.conflicts) {
                        conflicts = areasToCheck.filter(a => ZonesManager._rectanglesConflict(a, desiredArea, margin)).length > 0;
                    } else {
                        conflicts = false;
                    }
                }
            }
            conflicts = conflicts || !ZonesManager._rectangleFitBoundaries(desiredArea, zone.boundaries);
            if (conflicts) {
                desiredArea.x1 = rect.x1;
                desiredArea.x2 = rect.x2;
                desiredArea.y1 = rect.y1;
                desiredArea.y2 = rect.y2;
            }
            desiredArea.x1 -= global.x;
            desiredArea.x2 -= global.x;
            desiredArea.y1 -= global.y;
            desiredArea.y2 -= global.y;
            return {
                key,
                conflicts: conflicts,
                global: global,
                margin: margin,
                rect: desiredArea
            };
        }
        return null;
    }