Skip to content

Commit

Permalink
Drag continues working when zoomed far out.
Browse files Browse the repository at this point in the history
  • Loading branch information
BF5258 committed Dec 15, 2024
1 parent 8227729 commit 16202b4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/client/scripts/esm/game/chess/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ function update() {
if (promoteTo) makePromotionMove();
return;
}
if (movement.isScaleLess1Pixel_Virtual() || transition.areWeTeleporting() || gamefile.gameConclusion || guipause.areWePaused() || perspective.isLookingUp()) return;
if (movement.isScaleLess1Pixel_Virtual() || transition.areWeTeleporting()) {
if(draggingPiece) handleDragging(undefined, false);
return;
}
if (gamefile.gameConclusion || guipause.areWePaused() || perspective.isLookingUp()) return;

// Calculate if the hover square is legal so we know if we need to render a ghost image...

Expand All @@ -152,16 +156,17 @@ function update() {
// Else we clicked, but there was no piece to select, *shrugs*
}

function handleDragging(pieceHoveredType) {
function handleDragging(pieceHoveredType, allowDrop=true) {
if (input.getTouchHelds().length > 1) {
//Prevents accidental dragging when trying to zoom.
if (didLastClickSelectPiece) return unselectPiece();
return cancelDragging();
}
if (input.getPointerHeld()) { // still dragging.
// Render the piece at the pointer.
draganimation.dragPiece(input.getPointerWorldLocation(), hoverSquare);
draganimation.dragPiece(input.getPointerWorldLocation(), allowDrop ? hoverSquare : null);
} else {
if (!allowDrop) cancelDragging();
handleMovingSelectedPiece(hoverSquare, pieceHoveredType);
const wasCapture = pieceHoveredType || hoverSquare.hasOwnProperty('enpassant');
draganimation.dropPiece(hoverSquareLegal, wasCapture);
Expand Down
27 changes: 21 additions & 6 deletions src/client/scripts/esm/game/rendering/draganimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ let hoveredCoords;
let pieceType;

function renderTransparentSquare() {
if(!startCoords) return;
if (!startCoords) return;
let transparentModel = genTransparentModel();
transparentModel.render();
}

function renderPiece() {
if(perspective.isLookingUp() || !worldLocation) return;
genOutlineModel().render();
if (perspective.isLookingUp() || !worldLocation) return;
let outlineModel;
if (hoveredCoords) outlineModel = genOutlineModel();
else outlineModel = genIntersectingLines();
outlineModel.render();
genPieceModel().render();
}

Expand All @@ -84,7 +87,7 @@ function genTransparentModel() {
* @returns {BufferModel} The buffer model
*/
function genPieceModel() {
if(perspective.isLookingUp()) return;
if (perspective.isLookingUp()) return;
const perspectiveEnabled = perspective.getEnabled();
const touchscreen = input.getPointerIsTouch();
const boardScale = movement.getBoardScale();
Expand Down Expand Up @@ -134,6 +137,18 @@ function genOutlineModel() {
return buffermodel.createModel_Colored(new Float32Array(data), 3, "TRIANGLES");
}

function genIntersectingLines() {
const { left, right, bottom, top } = camera.getScreenBoundingBox(false);
const [ r, g, b, a ] = options.getDefaultOutlineColor();
let data = [
left, worldLocation[1], r, g, b, a,
right, worldLocation[1],r, g, b, a,
worldLocation[0], bottom, r, g, b, a,
worldLocation[0], top, r, g, b, a,
];
return buffermodel.createModel_Colored(new Float32Array(data), 2, "LINES");
}

/**
* Start dragging a piece.
* @param {string} type - The type of piece being dragged
Expand All @@ -147,11 +162,11 @@ function pickUpPiece(type, pieceCoords) {
/**
* Update the location of the piece being dragged.
* @param {number[]} coords - the world coordinates the piece has been dragged to
* @param {number[]} [hoverSquare] - The square to draw a box outline around
* @param {number[]} [hoverSquare] - The square the piece would be moved to if dropped now.
*/
function dragPiece(coords, hoverSquare) {
worldLocation = coords;
hoveredCoords = hoverSquare ?? space.convertWorldSpaceToCoords_Rounded(coords);
hoveredCoords = hoverSquare;
frametracker.onVisualChange();
}

Expand Down

0 comments on commit 16202b4

Please sign in to comment.