scratch-paint/src/helper/hover.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-10-12 18:35:30 -04:00
import paper from '@scratch/paper';
2017-09-11 14:23:30 -04:00
import {isBoundsItem, getRootItem} from './item';
import {hoverBounds, hoverItem} from './guides';
import {isGroupChild} from './group';
2017-11-03 17:55:02 -04:00
import {sortItemsByZIndex} from './math';
2017-09-11 10:52:00 -04:00
/**
2017-09-11 14:23:30 -04:00
* @param {!MouseEvent} event mouse event
* @param {?object} hitOptions hit options to use
2017-09-21 10:36:26 -04:00
* @param {?boolean} subselect Whether items within groups can be hovered. If false, the
* entire group should be hovered.
2017-09-11 14:23:30 -04:00
* @return {paper.Item} the hovered item or null if there is none
2017-09-11 10:52:00 -04:00
*/
2017-09-21 10:36:26 -04:00
const getHoveredItem = function (event, hitOptions, subselect) {
2017-12-12 12:23:02 -05:00
// @todo make hit test only hit painting layer
2017-09-11 10:52:00 -04:00
const hitResults = paper.project.hitTestAll(event.point, hitOptions);
if (hitResults.length === 0) {
return null;
}
2017-12-08 18:27:23 -05:00
// sort items by z-index
2017-11-03 17:55:02 -04:00
const items = [];
for (const hitResult of hitResults) {
2017-12-08 18:27:23 -05:00
if (!(hitResult.item.data && hitResult.item.data.noHover) && !hitResult.item.selected) {
items.push(hitResult.item);
}
2017-11-03 17:55:02 -04:00
}
items.sort(sortItemsByZIndex);
2017-12-08 18:27:23 -05:00
const item = items[items.length - 1];
if (!item) {
2017-09-11 10:52:00 -04:00
return null;
}
2017-12-08 18:27:23 -05:00
if (isBoundsItem(item)) {
return hoverBounds(item);
} else if (!subselect && isGroupChild(item)) {
return hoverBounds(getRootItem(item));
2017-09-11 10:52:00 -04:00
}
2017-12-08 18:27:23 -05:00
return hoverItem(item);
2017-09-11 10:52:00 -04:00
};
export {
2017-09-11 14:23:30 -04:00
getHoveredItem
2017-09-11 10:52:00 -04:00
};