Skip to content

Commit

Permalink
Feat: Adds angle API to axis events.
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
doubleactii committed Oct 17, 2023
1 parent 1e54bb3 commit 98ea69b
Show file tree
Hide file tree
Showing 10 changed files with 41,053 additions and 31,174 deletions.
2 changes: 1 addition & 1 deletion dist/gamepad.min.mjs

Large diffs are not rendered by default.

71,372 changes: 40,346 additions & 31,026 deletions docs/ast/source/gamepad.mjs.json

Large diffs are not rendered by default.

299 changes: 259 additions & 40 deletions docs/class/src/gamepad.mjs~Controller.html

Large diffs are not rendered by default.

132 changes: 125 additions & 7 deletions docs/class/src/gamepad.mjs~GamepadManagerSingleton.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,42 @@ <h1 data-ice="name">GamepadManagerSingleton</h1>
</tbody>
</table>
</div>

<div data-ice="staticMethodSummary"><h2>Static Method Summary</h2><table class="summary" data-ice="summary">
<thead><tr><td data-ice="title" colspan="3">Static Public Methods</td></tr></thead>
<tbody>

<tr data-ice="target">
<td>
<span class="access" data-ice="access">public</span>
<span data-ice="static">static</span>


<span class="override" data-ice="override"></span>
</td>
<td>
<div>
<p>



<span class="code" data-ice="name"><span><a href="class/src/gamepad.mjs~GamepadManagerSingleton.html#static-method-getAngle">getAngle</a></span></span><span class="code" data-ice="signature">(pStartPoint: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>, pEndPoint: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>): <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></span></span>
</p>
</div>
<div>


<div data-ice="description"><p>Gets the angle between two points</p>
</div>
</div>
</td>
<td>


</td>
</tr>
</tbody>
</table>
</div>
<div data-ice="constructorSummary"><h2>Constructor Summary</h2><table class="summary" data-ice="summary">
<thead><tr><td data-ice="title" colspan="3">Public Constructor</td></tr></thead>
<tbody>
Expand Down Expand Up @@ -480,7 +515,90 @@ <h3 data-ice="anchor" id="static-member-version">

</div>
</div>
<div data-ice="staticMethodDetails"><h2 data-ice="title">Static Public Methods</h2>

<div class="detail" data-ice="detail">
<h3 data-ice="anchor" id="static-method-getAngle">
<span class="access" data-ice="access">public</span>
<span data-ice="static">static</span>




<span class="code" data-ice="name">getAngle</span><span class="code" data-ice="signature">(pStartPoint: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>, pEndPoint: <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span>): <span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></span></span>
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber60">source</a></span></span>
</span>
</h3>




<div data-ice="description"><p>Gets the angle between two points</p>
</div>



<div data-ice="properties"><div data-ice="properties">
<h4 data-ice="title">Params:</h4>
<table class="params">
<thead>
<tr><td>Name</td><td>Type</td><td>Attribute</td><td>Description</td></tr>
</thead>
<tbody>

<tr data-ice="property" data-depth="0">
<td data-ice="name" class="code" data-depth="0">pStartPoint</td>
<td data-ice="type" class="code"><span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span></td>
<td data-ice="appendix"></td>
<td data-ice="description"><p>The starting point</p>
</td>
</tr>
<tr data-ice="property" data-depth="0">
<td data-ice="name" class="code" data-depth="0">pEndPoint</td>
<td data-ice="type" class="code"><span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></span></td>
<td data-ice="appendix"></td>
<td data-ice="description"><p>The ending point</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>

<div class="return-params" data-ice="returnParams">
<h4>Return:</h4>
<table>
<tbody>
<tr>
<td class="return-type code" data-ice="returnType"><span><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></span></td>
<td class="return-desc" data-ice="returnDescription"><p>The angle between the starting point and the ending point</p>
</td>
</tr>
</tbody>
</table>
<div data-ice="returnProperties">
</div>
</div>















</div>
</div>
<div data-ice="constructorDetails"><h2 data-ice="title">Public Constructors</h2>

<div class="detail" data-ice="detail">
Expand Down Expand Up @@ -677,7 +795,7 @@ <h3 data-ice="anchor" id="instance-method-getControllers">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber64">source</a></span></span>
<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber76">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -733,7 +851,7 @@ <h3 data-ice="anchor" id="instance-method-getMainController">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber58">source</a></span></span>
<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber70">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -790,7 +908,7 @@ <h3 data-ice="anchor" id="instance-method-handleGamepadConnected">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber105">source</a></span></span>
<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber117">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -851,7 +969,7 @@ <h3 data-ice="anchor" id="instance-method-handleGamepadDisconnected">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber116">source</a></span></span>
<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber128">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -912,7 +1030,7 @@ <h3 data-ice="anchor" id="instance-method-on">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber79">source</a></span></span>
<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber91">source</a></span></span>
</span>
</h3>

Expand Down Expand Up @@ -993,7 +1111,7 @@ <h3 data-ice="anchor" id="instance-method-pollGamepadState">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber126">source</a></span></span>
<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber138">source</a></span></span>
</span>
</h3>

Expand Down
51 changes: 50 additions & 1 deletion docs/file/src/gamepad.mjs.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@
console.warn(&apos;Gamepad API not supported in this browser.&apos;);
}
}
/**
* Gets the angle between two points
*
* @param {Object} pStartPoint - The starting point
* @param {Object} pEndPoint - The ending point
* @returns {number} The angle between the starting point and the ending point
*/
static getAngle(pStartPoint, pEndPoint) {
const y = pStartPoint.y - pEndPoint.y;
const x = pStartPoint.x - pEndPoint.x;
return -Math.atan2(y, x) - Math.PI;
}
/**
* This gets the first controller connected. This controller is dominant
*
Expand Down Expand Up @@ -204,6 +216,10 @@
* @type {boolean}
*/
rightAnalogHeld = false;
/**
* The base analogs position when it is not in use
*/
static baseAnalogPos = { x: 0, y: 0 };
/**
* Analog thumb sticks
*
Expand Down Expand Up @@ -357,6 +373,14 @@
previousAxesState: [],
initialAxesStickDrift: []
}
/**
* The left analogs position
*/
leftAnalogPos = { x: 0, y: 0 };
/**
* The right analogs position
*/
rightAnalogPos = { x: 0, y: 0 };
/**
* Object of stored callback that will call when a button is pressed
*
Expand Down Expand Up @@ -633,15 +657,40 @@
*/
handleAxisInput(pAxis, pValue, pRepeat) {
let axisName = pAxis;
// Clamp value to hundreths position just for easier calculations
let clampedValue = Math.floor(pValue * 100) / 100;
// Check if axis is mapped
for (const axes in Controller.AXES) {
if (Controller.AXES[axes] === pAxis) {
axisName = axes;
}
}

// The angle the axis is in
let analogAngle = 0;

if (axisName === &apos;LEFT_X&apos; || axisName === &apos;LEFT_Y&apos;) {
if (axisName === &apos;LEFT_X&apos;) {
this.leftAnalogPos.x = clampedValue;
}
if (axisName === &apos;LEFT_Y&apos;) {
this.leftAnalogPos.y = clampedValue;
}
analogAngle = GamepadManagerSingleton.getAngle(Controller.baseAnalogPos, this.leftAnalogPos);
}

if (axisName === &apos;RIGHT_X&apos; || axisName === &apos;RIGHT_Y&apos;) {
if (axisName === &apos;RIGHT_X&apos;) {
this.rightAnalogPos.x = clampedValue;
}
if (axisName === &apos;RIGHT_Y&apos;) {
this.rightAnalogPos.y = clampedValue;
}
analogAngle = GamepadManagerSingleton.getAngle(Controller.baseAnalogPos, this.rightAnalogPos);
}

if (axisName) {
if (typeof(this.axisHandlers[&apos;axis&apos;]) === &apos;function&apos;) this.axisHandlers[&apos;axis&apos;](axisName, clampedValue, pRepeat);
if (typeof(this.axisHandlers[&apos;axis&apos;]) === &apos;function&apos;) this.axisHandlers[&apos;axis&apos;](axisName, clampedValue, analogAngle, pRepeat);
}
}
/**
Expand Down
286 changes: 193 additions & 93 deletions docs/index.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions docs/script/search_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ window.esdocSearchIndex = [
"src/gamepad.mjs~Controller#leftAnalogHeld",
"member"
],
[
"src/gamepad.mjs~controller#leftanalogpos",
"class/src/gamepad.mjs~Controller.html#instance-member-leftAnalogPos",
"src/gamepad.mjs~Controller#leftAnalogPos",
"member"
],
[
"src/gamepad.mjs~controller#off",
"class/src/gamepad.mjs~Controller.html#instance-method-off",
Expand Down Expand Up @@ -437,6 +443,12 @@ window.esdocSearchIndex = [
"src/gamepad.mjs~Controller#rightAnalogHeld",
"member"
],
[
"src/gamepad.mjs~controller#rightanalogpos",
"class/src/gamepad.mjs~Controller.html#instance-member-rightAnalogPos",
"src/gamepad.mjs~Controller#rightAnalogPos",
"member"
],
[
"src/gamepad.mjs~controller#type",
"class/src/gamepad.mjs~Controller.html#instance-member-type",
Expand Down Expand Up @@ -521,6 +533,12 @@ window.esdocSearchIndex = [
"src/gamepad.mjs~Controller.UNPRESSED",
"member"
],
[
"src/gamepad.mjs~controller.baseanalogpos",
"class/src/gamepad.mjs~Controller.html#static-member-baseAnalogPos",
"src/gamepad.mjs~Controller.baseAnalogPos",
"member"
],
[
"src/gamepad.mjs~gamepadmanagersingleton#connecthandler",
"class/src/gamepad.mjs~GamepadManagerSingleton.html#instance-member-connectHandler",
Expand Down Expand Up @@ -587,6 +605,12 @@ window.esdocSearchIndex = [
"src/gamepad.mjs~GamepadManagerSingleton.Event",
"typedef"
],
[
"src/gamepad.mjs~gamepadmanagersingleton.getangle",
"class/src/gamepad.mjs~GamepadManagerSingleton.html#static-method-getAngle",
"src/gamepad.mjs~GamepadManagerSingleton.getAngle",
"method"
],
[
"src/gamepad.mjs~gamepadmanagersingleton.version",
"class/src/gamepad.mjs~GamepadManagerSingleton.html#static-member-version",
Expand Down
6 changes: 3 additions & 3 deletions docs/source.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
<span><a href="variable/index.html#static-variable-GamepadManager">GamepadManager</a></span>
<span><a href="class/src/gamepad.mjs~GamepadManagerSingleton.html">GamepadManagerSingleton</a></span></td>
<td class="coverage"><span data-ice="coverage">-</span></td>
<td style="display: none;" data-ice="size">24908 byte</td>
<td style="display: none;" data-ice="lines">692</td>
<td style="display: none;" data-ice="updated">2023-07-01 01:05:54 (UTC)</td>
<td style="display: none;" data-ice="size">26382 byte</td>
<td style="display: none;" data-ice="lines">741</td>
<td style="display: none;" data-ice="updated">2023-10-03 14:18:30 (UTC)</td>
</tr>
</tbody>
</table>
Expand Down
4 changes: 2 additions & 2 deletions docs/variable/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ <h3 data-ice="anchor" id="static-variable-GamepadManager">
<span class="right-info">


<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber692">source</a></span></span>
<span data-ice="source"><span><a href="file/src/gamepad.mjs.html#lineNumber741">source</a></span></span>
</span>
</h3>

<div data-ice="importPath" class="import-path"><pre class="prettyprint"><code data-ice="importPathCode">import {GamepadManager} from &apos;<span><a href="file/src/gamepad.mjs.html#lineNumber692">gamepad/src/gamepad.mjs</a></span>&apos;</code></pre></div>
<div data-ice="importPath" class="import-path"><pre class="prettyprint"><code data-ice="importPathCode">import {GamepadManager} from &apos;<span><a href="file/src/gamepad.mjs.html#lineNumber741">gamepad/src/gamepad.mjs</a></span>&apos;</code></pre></div>



Expand Down
Loading

0 comments on commit 98ea69b

Please sign in to comment.