-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
templates: Added square rotation correction
Resolves #327
- Loading branch information
1 parent
da75b03
commit 2074690
Showing
12 changed files
with
149 additions
and
60 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import SETTINGS from "../../common/Settings"; | ||
|
||
export default class SquareTemplate { | ||
static readonly FIX_ROTATION_PREF = 'fix-square-rotation'; | ||
|
||
static init() { | ||
SETTINGS.register(SquareTemplate.FIX_ROTATION_PREF, { | ||
config: true, | ||
scope: 'world', | ||
name: 'DF_TEMPLATES.SquareRotateName', | ||
hint: 'DF_TEMPLATES.SquareRotateHint', | ||
type: Boolean, | ||
default: true, | ||
onChange: (toggled) => toggled ? SquareTemplate.patch() : SquareTemplate.unpatch() | ||
}); | ||
if (SETTINGS.get(SquareTemplate.FIX_ROTATION_PREF)) | ||
SquareTemplate.patch(); | ||
} | ||
|
||
private static patch() { | ||
libWrapper.register(SETTINGS.MOD_NAME, 'MeasuredTemplate.prototype._getRectShape', SquareTemplate.MeasuredTemplate_getRectShape, 'OVERRIDE'); | ||
libWrapper.register(SETTINGS.MOD_NAME, 'MeasuredTemplate.prototype._refreshRulerText', SquareTemplate.MeasuredTemplate_refreshRulerText, 'WRAPPER'); | ||
} | ||
private static unpatch() { | ||
libWrapper.unregister(SETTINGS.MOD_NAME, 'MeasuredTemplate.prototype._getRectShape', false); | ||
libWrapper.unregister(SETTINGS.MOD_NAME, 'MeasuredTemplate.prototype._refreshRulerText', false); | ||
} | ||
|
||
static MeasuredTemplate_getRectShape(this: MeasuredTemplate, direction: number, distance: number, adjustForRoundingError = false): PIXI.Polygon { | ||
// Generate a rotation matrix to apply the rect against. The base rotation must be rotated | ||
// CCW by 45° before applying the real direction rotation. | ||
const matrix = PIXI.Matrix.IDENTITY.rotate((-45 * (Math.PI / 180)) + direction); | ||
// If the shape will be used for collision, shrink the rectangle by a fixed EPSILON amount to account for rounding errors | ||
const EPSILON = adjustForRoundingError ? 0.0001 : 0; | ||
// Use simple Pythagoras to calculate the square's size from the diagonal "distance". | ||
const size = Math.sqrt((distance * distance) / 2) - EPSILON; | ||
// Create the square's 4 corners with origin being the Top-Left corner and apply the | ||
// rotation matrix against each. | ||
const topLeft = matrix.apply(new PIXI.Point(EPSILON, EPSILON)); | ||
const topRight = matrix.apply(new PIXI.Point(size, EPSILON)); | ||
const botLeft = matrix.apply(new PIXI.Point(EPSILON, size)); | ||
const botRight = matrix.apply(new PIXI.Point(size, size)); | ||
// Inject the vector data into a Polygon object to create a closed shape. | ||
return new PIXI.Polygon([topLeft.x, topLeft.y, topRight.x, topRight.y, botRight.x, botRight.y, botLeft.x, botLeft.y, topLeft.x, topLeft.y]); | ||
} | ||
|
||
private static MeasuredTemplate_refreshRulerText(this: MeasuredTemplate, wrapped: () => void): void { | ||
wrapped(); | ||
// Overwrite the text for the "rect" type | ||
if (this.data.t === "rect") { | ||
// Use simple Pythagoras to calculate the square's size from the diagonal "distance". | ||
const size = Math.sqrt((this.data.distance * this.data.distance) / 2).toFixed(1); | ||
const text = `${size}${canvas.scene.data.gridUnits}`; | ||
(<any>this).hud.ruler.text = text; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters