Skip to content

Commit

Permalink
Add tests; fix default threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
nsfm committed Jun 25, 2022
1 parent 8aeab4d commit f9daa4b
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"prebuild": "yarn barrels && yarn docs",
"prebuild": "yarn barrels",
"build": "tsc --project tsconfig.build.json",
"build:watch": "tsc --project tsconfig.build.json --pretty --watch",
"lint": "eslint src",
Expand Down
17 changes: 17 additions & 0 deletions src/elements/analog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ describe("Analog", () => {
expect(analog.active).toEqual(true);
analog.x[InputSet](0);
expect(analog.active).toEqual(false);
analog.x[InputSet](-0.6);
expect(analog.active).toEqual(true);
analog.x[InputSet](-0.4);
expect(analog.active).toEqual(false);
});

it("children inherit `threshold`", () => {
const analog = new Analog({ threshold: 0.5 });
expect(analog.x.active).toEqual(false);
analog.x[InputSet](0.4);
expect(analog.x.active).toEqual(false);
analog.x[InputSet](0.5);
expect(analog.x.active).toEqual(false);
analog.x[InputSet](0.6);
expect(analog.x.active).toEqual(true);
analog.x[InputSet](0);
expect(analog.x.active).toEqual(false);
});

it("should return good directions", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/elements/analog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export class Analog extends Input<Analog> {

this.button = new Momentary(button || { icon: "3", name: "Button" });
this.x = new Axis(
x || { icon: "↔", name: "X", threshold: threshold || (1 / 128) * 6 }
x || { icon: "↔", name: "X", threshold: threshold || 0.07 }
);
this.y = new Axis(
y || { icon: "↕", name: "Y", threshold: threshold || (1 / 128) * 6 }
y || { icon: "↕", name: "Y", threshold: threshold || 0.07 }
);
}

Expand Down
31 changes: 31 additions & 0 deletions src/elements/axis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
import { Axis } from "./axis";
import { InputSet } from "../input";

describe("Axis", () => {
it("should construct", () => {
expect(new Axis()).toBeInstanceOf(Axis);
});

it("should implement `active`", () => {
const axis = new Axis({ threshold: (1 / 128) * 10 });
expect(axis.active).toEqual(false);
axis[InputSet](1);
expect(axis.active).toEqual(true);
axis[InputSet](0);
expect(axis.active).toEqual(false);
axis[InputSet](-1);
expect(axis.active).toEqual(true);
axis[InputSet](0);
expect(axis.active).toEqual(false);
axis[InputSet]((1 / 128) * 2);
expect(axis.active).toEqual(false);
});

it("should implement `force`", () => {
const axis = new Axis({ threshold: (1 / 128) * 10 });
expect(axis.force).toEqual(0);
axis[InputSet](1);
expect(axis.force).toEqual(1);
axis[InputSet](0);
expect(axis.force).toEqual(0);
axis[InputSet](-1);
expect(axis.force).toEqual(-1);
axis[InputSet](0);
expect(axis.force).toEqual(0);
axis[InputSet]((1 / 128) * 2);
expect(axis.force).toEqual(0);
});
});
2 changes: 1 addition & 1 deletion src/elements/unisense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Unisense extends Input<Unisense> {
params?.bumper || { icon: "1", name: "Bumper" }
);
this.analog = new Analog(
params?.analog || { icon: "⨁", name: "Analog", threshold: (1 / 128) * 3 }
params?.analog || { icon: "⨁", name: "Analog", threshold: 0.07 }
);
this.haptic = new Haptic();
}
Expand Down
36 changes: 27 additions & 9 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,29 @@ export abstract class Input<Type>
{
public readonly id: symbol;

// Timestamp of the last received input that changed the state.
/**
* Timestamp of the last received input that changed the state.
*/
public lastChange: number = Date.now();

// Timestamp of the last received input, even if it didn't change the state.
/**
* Timestamp of the last received input, even if it didn't change the state.
*/
public lastInput: number = Date.now();

// For numeric inputs, ignore state changes smaller than this threshold.
/**
* For numeric inputs, ignore state changes smaller than this threshold.
*/
public threshold: number = 0;

// Provide the type and default value for the input.
/**
* Provide the type and default value for the input.
*/
public abstract state: Type;

// Implement a function that returns true if the user is actively engaged with the input.
/**
* Implement a function that returns true if the user is actively engaged with the input.
*/
public abstract get active(): boolean;

/**
Expand Down Expand Up @@ -124,7 +134,9 @@ export abstract class Input<Type>
});
}

// Optionally, implement a function that returns true if the provided state is worth an event
/**
* Optionally, implement a function that returns true if the provided state is worth an event
*/
[InputChanged]: (state: Type, newState: Type) => boolean;

// TODO Support params for nested inputs
Expand All @@ -150,13 +162,19 @@ export abstract class Input<Type>
return this.toString();
}

// A name for this input
/**
* The name of this input.
*/
readonly [InputName]: string;

// A short name for this input
/**
* A short name for this input
*/
readonly [InputIcon]: string;

// The Input's parent, if any
/**
* The Input's parent, if any
*/
[InputParent]?: Input<unknown>;

[InputChildless]: boolean = true;
Expand Down
2 changes: 2 additions & 0 deletions util/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function main() {
if (Math.random() > 0.05) return;
console.clear();
console.log(controller.hid?.state);
console.log(controller.left.analog.x.force);
console.log(controller.left.analog.y.force);
});
} catch (err) {
console.log(err);
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1211,9 +1211,9 @@ camelcase@^6.2.0:
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==

caniuse-lite@^1.0.30001358:
version "1.0.30001358"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001358.tgz#473d35dabf5e448b463095cab7924e96ccfb8c00"
integrity sha512-hvp8PSRymk85R20bsDra7ZTCpSVGN/PAz9pSAjPSjKC+rNmnUk5vCRgJwiTT/O4feQ/yu/drvZYpKxxhbFuChw==
version "1.0.30001359"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001359.tgz#a1c1cbe1c2da9e689638813618b4219acbd4925e"
integrity sha512-Xln/BAsPzEuiVLgJ2/45IaqD9jShtk3Y33anKb4+yLwQzws3+v6odKfpgES/cDEaZMLzSChpIGdbOYtH9MyuHw==

chalk@^2.0.0, chalk@^2.3.2:
version "2.4.2"
Expand Down Expand Up @@ -1567,9 +1567,9 @@ duplexer3@^0.1.4:
integrity sha512-CEj8FwwNA4cVH2uFCoHUrmojhYh1vmCdOaneKJXwkeY1i9jnlslVo9dx+hQ5Hl9GnH/Bwy/IjxAyOePyPKYnzA==

electron-to-chromium@^1.4.164:
version "1.4.165"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.165.tgz#a1ae079a4412b0c2d3bf6908e8db54511fb0bbac"
integrity sha512-DKQW1lqUSAYQvn9dnpK7mWaDpWbNOXQLXhfCi7Iwx0BKxdZOxkKcCyKw1l3ihWWW5iWSxKKbhEUoNRoHvl/hbA==
version "1.4.170"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.170.tgz#0415fc489402e09bfbe1f0c99bbf4d73f31d48d4"
integrity sha512-rZ8PZLhK4ORPjFqLp9aqC4/S1j4qWFsPPz13xmWdrbBkU/LlxMcok+f+6f8YnQ57MiZwKtOaW15biZZsY5Igvw==

emittery@^0.8.1:
version "0.8.1"
Expand Down Expand Up @@ -4363,9 +4363,9 @@ typedoc-plugin-markdown@^3.12.1:
handlebars "^4.7.7"

typedoc@^0.22.15:
version "0.22.17"
resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.22.17.tgz#bc51cc95f569040112504300831cdac4f8089b7b"
integrity sha512-h6+uXHVVCPDaANzjwzdsj9aePBjZiBTpiMpBBeyh1zcN2odVsDCNajz8zyKnixF93HJeGpl34j/70yoEE5BfNg==
version "0.22.18"
resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.22.18.tgz#1d000c33b66b88fd8cdfea14a26113a83b7e6591"
integrity sha512-NK9RlLhRUGMvc6Rw5USEYgT4DVAUFk7IF7Q6MYfpJ88KnTZP7EneEa4RcP+tX1auAcz7QT1Iy0bUSZBYYHdoyA==
dependencies:
glob "^8.0.3"
lunr "^2.3.9"
Expand Down Expand Up @@ -4411,9 +4411,9 @@ universalify@^0.1.2:
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==

update-browserslist-db@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.3.tgz#6c47cb996f34afb363e924748e2f6e4d983c6fc1"
integrity sha512-ufSazemeh9Gty0qiWtoRpJ9F5Q5W3xdIPm1UZQqYQv/q0Nyb9EMHUB2lu+O9x1re9WsorpMAUu4Y6Lxcs5n+XQ==
version "1.0.4"
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.4.tgz#dbfc5a789caa26b1db8990796c2c8ebbce304824"
integrity sha512-jnmO2BEGUjsMOe/Fg9u0oczOe/ppIDZPebzccl1yDWGLFP16Pa1/RM5wEoKYPG2zstNcDuAStejyxsOuKINdGA==
dependencies:
escalade "^3.1.1"
picocolors "^1.0.0"
Expand Down

0 comments on commit f9daa4b

Please sign in to comment.