From e0fb59b19d212e96586ef8901cf0a283b09844c4 Mon Sep 17 00:00:00 2001 From: Benoit Casoetto Date: Fri, 18 Feb 2022 18:53:57 +0100 Subject: [PATCH] Add order enforcement in stack process includes creation of group parameter shouldStackEnforceOrder --- src/lib/utility/calendar.js | 47 ++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/lib/utility/calendar.js b/src/lib/utility/calendar.js index be501951b..379174355 100644 --- a/src/lib/utility/calendar.js +++ b/src/lib/utility/calendar.js @@ -287,15 +287,34 @@ export function getVisibleItems(items, canvasTimeStart, canvasTimeEnd, keys) { const EPSILON = 0.001 -export function collision(a, b, lineHeight, collisionPadding = EPSILON) { +export function simpleCollision(a, b, lineHeight, collisionPadding = EPSILON) { // 2d collisions detection - https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection var verticalMargin = 0 return ( a.collisionLeft + collisionPadding < b.collisionLeft + b.collisionWidth && a.collisionLeft + a.collisionWidth - collisionPadding > b.collisionLeft && - a.top - verticalMargin + collisionPadding < b.top + b.height && - a.top + a.height + verticalMargin - collisionPadding > b.top + a.top - verticalMargin + collisionPadding < b.top + b.height && + a.top + a.height + verticalMargin - collisionPadding > b.top + ) +} + +export function collisionOrOrderMismatch(a, b, lineHeight, collisionPadding = EPSILON) { + // 2d collisions detection - https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection + var verticalMargin = 0 + + return ( + a.collisionLeft + collisionPadding < b.collisionLeft + b.collisionWidth && + a.collisionLeft + a.collisionWidth - collisionPadding > b.collisionLeft && + (( + a.top - verticalMargin + collisionPadding < b.top + b.height && + a.top + a.height + verticalMargin - collisionPadding > b.top + ) + || + ( + a.top < b.top + ) + ) ) } @@ -309,7 +328,8 @@ export function groupStack( group, groupHeight, groupTop, - itemIndex + itemIndex, + collision ) { // calculate non-overlapping positions let curHeight = groupHeight @@ -387,9 +407,12 @@ export function stackAll(itemsDimensions, groupOrders, lineHeight, stackItems) { // Is group being stacked? const isGroupStacked = group.stackItems !== undefined ? group.stackItems : stackItems + const shouldStackEnforceOrder = + group.shouldStackEnforceOrder !== undefined ? group.shouldStackEnforceOrder : false const { groupHeight, verticalMargin } = stackGroup( itemsDimensions, isGroupStacked, + shouldStackEnforceOrder, lineHeight, groupTop ) @@ -411,15 +434,16 @@ export function stackAll(itemsDimensions, groupOrders, lineHeight, stackItems) { } /** - * - * @param {*} itemsDimensions - * @param {*} isGroupStacked - * @param {*} lineHeight - * @param {*} groupTop + * + * @param {*} itemsDimensions + * @param {*} isGroupStacked + * @param {*} lineHeight + * @param {*} groupTop */ -export function stackGroup(itemsDimensions, isGroupStacked, lineHeight, groupTop) { +export function stackGroup(itemsDimensions, isGroupStacked, shouldStackEnforceOrder, lineHeight, groupTop) { var groupHeight = 0 var verticalMargin = 0 + const collision = shouldStackEnforceOrder ? collisionOrOrderMismatch : simpleCollision // Find positions for each item in group for (let itemIndex = 0; itemIndex < itemsDimensions.length; itemIndex++) { let r = {} @@ -430,7 +454,8 @@ export function stackGroup(itemsDimensions, isGroupStacked, lineHeight, groupTop itemsDimensions, groupHeight, groupTop, - itemIndex + itemIndex, + collision ) } else { r = groupNoStack(lineHeight, itemsDimensions[itemIndex], groupHeight, groupTop)