Round dragged points and collision masks vertices of sprites (#5124)

This commit is contained in:
D8H
2023-03-20 10:20:59 +01:00
committed by GitHub
parent 8d1bdfed25
commit 936c75b4ed
3 changed files with 14 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import { mapVector } from '../../../../Utils/MapFor';
import useForceUpdate from '../../../../Utils/UseForceUpdate';
import { dataObjectToProps } from '../../../../Utils/HTMLDataset';
import {
roundVertexToHalfPixel,
findNearestEdgePoint,
getMagnetizedVertexForDeletion,
type NewVertexHintPoint,
@@ -217,6 +218,7 @@ const CollisionMasksPreview = (props: Props) => {
onPolygonsUpdated();
onClickVertice(null);
} else {
roundVertexToHalfPixel(draggedVertex.vertex);
onPolygonsUpdated();
onClickVertice(draggedVertex.vertex.ptr);
}

View File

@@ -3,6 +3,11 @@ import { mapFor, mapVector } from '../../../../Utils/MapFor';
const gd = global.gd;
export const roundVertexToHalfPixel = (vertex: gdVector2f) => {
vertex.set_x(Math.round(vertex.get_x() * 2) / 2);
vertex.set_y(Math.round(vertex.get_y() * 2) / 2);
};
export const addVertexOnLongestEdge = (vertices: gdVectorVector2f) => {
const verticesSize = vertices.size();
if (verticesSize > 0) {

View File

@@ -29,6 +29,11 @@ const getPointName = (kind: PointKind, point: gdPoint): string =>
? 'Center'
: point.getName();
const roundPointToHalfPixel = (point: gdPoint) => {
point.setX(Math.round(point.getX() * 2) / 2);
point.setY(Math.round(point.getY() * 2) / 2);
};
type Props = {|
pointsContainer: gdSprite, // Could potentially be generalized to other things than Sprite in the future.
imageWidth: number,
@@ -101,8 +106,8 @@ const PointsPreview = (props: Props) => {
const onEndDragPoint = React.useCallback(
() => {
const draggingWasDone = !!state.draggedPoint;
if (draggingWasDone) {
if (state.draggedPoint) {
roundPointToHalfPixel(state.draggedPoint);
onPointsUpdated();
// Select point at the end of the drag
if (state.draggedPointKind && state.draggedPoint) {