Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
p4535992 committed Jun 8, 2022
1 parent 8c7cd91 commit 9a4b4f9
Show file tree
Hide file tree
Showing 14 changed files with 484 additions and 339 deletions.
3 changes: 1 addition & 2 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@
"foundryvtt-arms-reach.settingNameRangeOverlayFeature": "[Experimental] Enable/Disable Range Overlay integration",
"foundryvtt-arms-reach.settingHintRangeOverlayFeature": "",


"foundryvtt-arms-reach.no-good-tiles": "No tiles are within movement range AND attack range",
"foundryvtt-arms-reach.controlButton": "Toggle combat range overlay",
"foundryvtt-arms-reach.is-active": "Active?",
Expand Down Expand Up @@ -205,7 +204,7 @@
"foundryvtt-arms-reach.diagonals.tenFiveTen": "Ten / Five / Ten / ...",
"foundryvtt-arms-reach.diagonals.five": "Five / Five / ...",
"foundryvtt-arms-reach.diagonals.ten": "Ten / Ten / ...",

"foundryvtt-arms-reach.quick-settings.title": "Combat Range Overlay Quick Settings",
"foundryvtt-arms-reach.quick-settings.title-hint": " ",
"foundryvtt-arms-reach.quick-settings.weapon-range-header": "Weapon Range:",
Expand Down
22 changes: 11 additions & 11 deletions src/module/ArmsReachModels.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Overlay } from "./apps/range_overlay/overlay";
import type { Overlay } from './apps/range_overlay/overlay';

export class DoorData {
/// door data of the source door (WARNING: this data may change in the future)
Expand Down Expand Up @@ -74,18 +74,18 @@ export class ArmsreachData {
}

export class combatRangeOverlay {
instance:Overlay;
instance: Overlay;
showNumericMovementCost: boolean;
showPathLines: boolean;
roundNumericMovementCost: boolean;
};
}

export class overlaysData {
distanceTexts:PIXI.Text[];
tokenOverlays:PIXI.Graphics[];
distanceOverlay:PIXI.Graphics|undefined;
pathOverlay:PIXI.Graphics|undefined;
turnOrderTexts:any[];
potentialTargetOverlay:PIXI.Graphics|undefined;
wallsOverlay:PIXI.Graphics|undefined;
}
distanceTexts: PIXI.Text[];
tokenOverlays: PIXI.Graphics[];
distanceOverlay: PIXI.Graphics | undefined;
pathOverlay: PIXI.Graphics | undefined;
turnOrderTexts: any[];
potentialTargetOverlay: PIXI.Graphics | undefined;
wallsOverlay: PIXI.Graphics | undefined;
}
2 changes: 1 addition & 1 deletion src/module/DoorsReach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export const DoorsReach = {
// DEPRECATED AND REMOVED
// maxDistance = <number>game.settings.get(CONSTANTS.MODULE_NAME, 'doorInteractionDistance');
// if (maxDistance <= 0) {
if(<number>game.settings.get(CONSTANTS.MODULE_NAME, 'doorInteractionMeasurement') > 0){
if (<number>game.settings.get(CONSTANTS.MODULE_NAME, 'doorInteractionMeasurement') > 0) {
maxDistance = <number>game.settings.get(CONSTANTS.MODULE_NAME, 'doorInteractionMeasurement');
}
// }
Expand Down
3 changes: 1 addition & 2 deletions src/module/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import type { Overlay } from './apps/range_overlay/overlay';
import type { combatRangeOverlay } from './ArmsReachModels';

const API = {

combatRangeOverlay:<combatRangeOverlay>{},
combatRangeOverlay: <combatRangeOverlay>{},

async isReachableArr(...inAttributes: any[]) {
if (!Array.isArray(inAttributes)) {
Expand Down
102 changes: 62 additions & 40 deletions src/module/apps/range_overlay/controls.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,76 @@
import {getCurrentToken, getWeaponRanges} from "./utility.js"
import {keyboard} from "./keyboard.js";
import {TokenInfo} from "./tokenInfo.js";
import { debug, i18n, warn } from "../../lib/lib.js";
import CONSTANTS from "../../constants.js";
import API from "../../api.js";
import { getCurrentToken, getWeaponRanges } from './utility.js';
import { keyboard } from './keyboard.js';
import { TokenInfo } from './tokenInfo.js';
import { debug, i18n, warn } from '../../lib/lib.js';
import CONSTANTS from '../../constants.js';
import API from '../../api.js';

export const TOGGLE_BUTTON = "combatRangeOverlayButton";
export const TOGGLE_BUTTON = 'combatRangeOverlayButton';

// noinspection JSUnusedLocalSymbols
async function _submitDialog(i, html) {
debug("_submitDialog")// , i, html);
const updateActor = html.find("[name=update-actor]")[0]?.checked;
const speedOverride = html.find("[name=speed-override]")[0]?.value;
const ignoreTerrain = html.find("[name=ignore-difficult-terrain]")[0]?.checked;
debug('_submitDialog'); // , i, html);
const updateActor = html.find('[name=update-actor]')[0]?.checked;
const speedOverride = html.find('[name=speed-override]')[0]?.value;
const ignoreTerrain = html.find('[name=ignore-difficult-terrain]')[0]?.checked;
await TokenInfo.current.setWeaponRange(i, updateActor);
await TokenInfo.current.setSpeedOverride(speedOverride, updateActor);
await TokenInfo.current.setIgnoreDifficultTerrain(ignoreTerrain, updateActor);
}

function _showRangeDialog() {
const buttons = Object.fromEntries(getWeaponRanges()
.map((i) =>
[
String(i),
{
icon: '',
label: String(i),
callback: (html) => _submitDialog(i, html)
}
])
const buttons = Object.fromEntries(
getWeaponRanges().map((i) => [
String(i),
{
icon: '',
label: String(i),
callback: (html) => _submitDialog(i, html),
},
]),
);
const defaultValue = String(getWeaponRanges()[0]);

const speedOverride = TokenInfo.current.speedOverride ?? "";
const ignoreDifficultTerrainChecked = TokenInfo.current.ignoreDifficultTerrain ? "checked" : "";
const content:string[] = [];
const speedOverride = TokenInfo.current.speedOverride ?? '';
const ignoreDifficultTerrainChecked = TokenInfo.current.ignoreDifficultTerrain ? 'checked' : '';
const content: string[] = [];
if (game.user?.isGM) {
content.push(`<p>${i18n(`${CONSTANTS.MODULE_NAME}.quick-settings.update-actor-checkbox`)} <input name="update-actor" type="checkbox"/></p>`);
content.push(
`<p>${i18n(
`${CONSTANTS.MODULE_NAME}.quick-settings.update-actor-checkbox`,
)} <input name="update-actor" type="checkbox"/></p>`,
);
}
content.push(`<p>${i18n(`${CONSTANTS.MODULE_NAME}.quick-settings.ignore-difficult-terrain`)} <input name="ignore-difficult-terrain" type="checkbox" ${ignoreDifficultTerrainChecked}/></p>`);
content.push(`<p>${i18n(`${CONSTANTS.MODULE_NAME}.quick-settings.speed-override`)} <input name="speed-override" type="text" value="${speedOverride}" size="3" style="width: 40px" maxlength="3"/>`);
content.push(
`<p>${i18n(
`${CONSTANTS.MODULE_NAME}.quick-settings.ignore-difficult-terrain`,
)} <input name="ignore-difficult-terrain" type="checkbox" ${ignoreDifficultTerrainChecked}/></p>`,
);
content.push(
`<p>${i18n(
`${CONSTANTS.MODULE_NAME}.quick-settings.speed-override`,
)} <input name="speed-override" type="text" value="${speedOverride}" size="3" style="width: 40px" maxlength="3"/>`,
);
content.push(`<p>${i18n(`${CONSTANTS.MODULE_NAME}.quick-settings.weapon-range-header`)}</p>`);

const d = new Dialog({
title: i18n(`${CONSTANTS.MODULE_NAME}.quick-settings.title`),
content: content.join("\n"),
buttons: buttons,
default: defaultValue
}, {id: "croQuickSettingsDialog"});
const d = new Dialog(
{
title: i18n(`${CONSTANTS.MODULE_NAME}.quick-settings.title`),
content: content.join('\n'),
buttons: buttons,
default: defaultValue,
},
{ id: 'croQuickSettingsDialog' },
);
d.render(true);
}

export async function _toggleButtonClick(toggled, controls) {
let isActive = game.settings.get(CONSTANTS.MODULE_NAME,'is-active');
const wasActive = game.settings.get(CONSTANTS.MODULE_NAME,'is-active');
let isActive = game.settings.get(CONSTANTS.MODULE_NAME, 'is-active');
const wasActive = game.settings.get(CONSTANTS.MODULE_NAME, 'is-active');

if (keyboard.isDown("Shift")) { // Pop quick settings
if (keyboard.isDown('Shift')) {
// Pop quick settings
const token = getCurrentToken();
if (!token) {
warn(i18n(`${CONSTANTS.MODULE_NAME}.controls.cant-open-no-selected-token`), true);
Expand All @@ -65,7 +80,8 @@ export async function _toggleButtonClick(toggled, controls) {

_showRangeDialog();
}
} else if (keyboard.isDown("Control")) { // Reset measureFrom
} else if (keyboard.isDown('Control')) {
// Reset measureFrom
const token = getCurrentToken();
if (!token) {
warn(i18n(`${CONSTANTS.MODULE_NAME}.controls.cant-reset-no-token`), true);
Expand All @@ -82,10 +98,16 @@ export async function _toggleButtonClick(toggled, controls) {

// Ensure button matches active state
// We _must_ set .active _before_ using await or the button will be drawn and we'll be too late
controls.find(group => group.name === "token").tools.find(t => t.name === TOGGLE_BUTTON).active = isActive;
await game.settings.set(CONSTANTS.MODULE_NAME,'is-active',isActive);
controls.find((group) => group.name === 'token').tools.find((t) => t.name === TOGGLE_BUTTON).active = isActive;
await game.settings.set(CONSTANTS.MODULE_NAME, 'is-active', isActive);

if (!wasActive && isActive && TokenInfo.current && TokenInfo.current.speed === 0 && TokenInfo.current.getSpeedFromAttributes() == 0) {
if (
!wasActive &&
isActive &&
TokenInfo.current &&
TokenInfo.current.speed === 0 &&
TokenInfo.current.getSpeedFromAttributes() == 0
) {
if (game.user?.isGM) {
warn(i18n(`${CONSTANTS.MODULE_NAME}.token-speed-warning-gm`), true);
} else {
Expand Down
25 changes: 12 additions & 13 deletions src/module/apps/range_overlay/gridTile.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import CONSTANTS from "../../constants.js";
import {TokenInfo} from "./tokenInfo.js"
import CONSTANTS from '../../constants.js';
import { TokenInfo } from './tokenInfo.js';

export class GridTile {

gx:number;
gy:number;
distance:number;
gx: number;
gy: number;
distance: number;
visited = false;
upstreams:GridTile[];
_upstreamCache:Map<string,GridTile>;
upstreams: GridTile[];
_upstreamCache: Map<string, GridTile>;

constructor(gx, gy) {
this.gx = gx;
this.gy = gy;
this.distance = CONSTANTS.MAX_DIST;
this.visited = false;
this.upstreams = [];
this._upstreamCache = new Map<string,GridTile>();
this._upstreamCache = new Map<string, GridTile>();
}

get centerPt() {
const pixels = <PointArray>canvas.grid?.grid?.getPixelsFromGridPosition(this.gx, this.gy);
return { x: pixels[0] + <number>canvas.grid?.size/2, y: pixels[1] + <number>canvas.grid?.size/2 };
return { x: pixels[0] + <number>canvas.grid?.size / 2, y: pixels[1] + <number>canvas.grid?.size / 2 };
}

get pt() {
Expand All @@ -38,13 +37,13 @@ export class GridTile {
return 1;
} else {
//@ts-ignore
return canvas.terrain?.cost({x: this.gy, y: this.gx}) ?? 1;
return canvas.terrain?.cost({ x: this.gy, y: this.gx }) ?? 1;
}
}

get allUpstreams():Map<string,GridTile> {
get allUpstreams(): Map<string, GridTile> {
if (this._upstreamCache === undefined) {
this._upstreamCache = new Map<string,GridTile>();
this._upstreamCache = new Map<string, GridTile>();
if (this.upstreams !== undefined) {
for (const upstream of this.upstreams) {
this._upstreamCache.set(upstream.key, upstream);
Expand Down
80 changes: 40 additions & 40 deletions src/module/apps/range_overlay/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
export const states = {
DOWN: 'down',
UP: 'up'
DOWN: 'down',
UP: 'up',
};

class Keyboard {
_keyStates:Map<string,string>;
_hooks:Map<string,Function[]>;
_hookList:Map<string,Function[]>;

constructor() {
this._keyStates = new Map<string,string>();
this._hooks = new Map<string,any[]>();
}

addHook(key:string, func:Function) {
const hookList = <any[]>(this._hooks.has(key) ? this._hooks.get(key) : []);
hookList.push(func);
this._hooks.set(key, hookList);
}

clearHooks() {
this._hookList = new Map<string,any[]>();
_keyStates: Map<string, string>;
_hooks: Map<string, Function[]>;
_hookList: Map<string, Function[]>;

constructor() {
this._keyStates = new Map<string, string>();
this._hooks = new Map<string, any[]>();
}

addHook(key: string, func: Function) {
const hookList = <any[]>(this._hooks.has(key) ? this._hooks.get(key) : []);
hookList.push(func);
this._hooks.set(key, hookList);
}

clearHooks() {
this._hookList = new Map<string, any[]>();
}

isDown(key) {
return this._keyStates.has(key) && this._keyStates.get(key) === states.DOWN;
}

_keyDownListener(event) {
this._keyStates.set(event.key, states.DOWN);
if (this._hooks.has(event.key)) {
for (const func of <Function[]>this._hooks.get(event.key)) {
func(event, states.DOWN);
}
}

isDown(key) {
return this._keyStates.has(key) && this._keyStates.get(key) === states.DOWN;
}

_keyDownListener(event) {
this._keyStates.set(event.key, states.DOWN);
if (this._hooks.has(event.key)) {
for (const func of <Function[]>this._hooks.get(event.key)) {
func(event, states.DOWN);
}
}
}

_keyUpListener(event) {
this._keyStates.set(event.key, states.UP);
if (this._hooks.has(event.key)) {
for (const func of <Function[]>this._hooks.get(event.key)) {
func(event, states.UP);
}
}
}

_keyUpListener(event) {
this._keyStates.set(event.key, states.UP);
if (this._hooks.has(event.key)) {
for (const func of <Function[]>this._hooks.get(event.key)) {
func(event, states.UP);
}
}
}
}

export const keyboard = new Keyboard();
Expand Down
Loading

0 comments on commit 9a4b4f9

Please sign in to comment.