scratch-paint/src/helper/hover.js

50 lines
1.4 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-11-03 17:55:02 -04:00
// sort items by index
const items = [];
for (const hitResult of hitResults) {
items.push(hitResult.item);
}
items.sort(sortItemsByZIndex);
2017-09-11 10:52:00 -04:00
let hitResult;
for (const result of hitResults) {
2017-09-13 15:17:59 -04:00
if (!(result.item.data && result.item.data.noHover) && !result.item.selected) {
2017-09-11 10:52:00 -04:00
hitResult = result;
break;
}
}
if (!hitResult) {
return null;
}
2017-09-11 14:23:30 -04:00
if (isBoundsItem(hitResult.item)) {
return hoverBounds(hitResult.item);
2017-09-21 10:36:26 -04:00
} else if (!subselect && isGroupChild(hitResult.item)) {
2017-09-11 14:23:30 -04:00
return hoverBounds(getRootItem(hitResult.item));
2017-09-11 10:52:00 -04:00
}
2017-09-11 14:23:30 -04:00
return hoverItem(hitResult);
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
};