Skip to content

Commit

Permalink
Optimise Analog control
Browse files Browse the repository at this point in the history
- check if new position is different from the previous one before
dispatching it and before updating the animation because it was bandwith
consuming and not performant
  • Loading branch information
experiment322 committed Mar 22, 2019
1 parent 2e64a60 commit ef1de6c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions js/lib/controller/Analog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ export default class Analog extends TouchReceiverMixin(React.PureComponent) {
x: Math.min(halfSize, Math.max(-halfSize, position.x - centerX)),
y: Math.min(halfSize, Math.max(-halfSize, position.y - centerY)),
};
// noinspection JSSuspiciousNameCombination
if (Math.abs(clampedPosition.x) >= (analogDeadZone / 100) * halfSize
|| Math.abs(clampedPosition.y) >= (analogDeadZone / 100) * halfSize) {
dispatch({
[emitX]: Math.round((clampedPosition.x / halfSize) * analogStickMax),
[emitY]: Math.round((clampedPosition.y / halfSize) * analogStickMax),
}, false);
this.translation.setValue(clampedPosition);
if (clampedPosition.x !== this.translation.x._value // eslint-disable-line max-len, no-underscore-dangle
|| clampedPosition.y !== this.translation.y._value) { // eslint-disable-line max-len, no-underscore-dangle
dispatch({
[emitX]: Math.round((clampedPosition.x / halfSize) * analogStickMax),
[emitY]: Math.round((clampedPosition.y / halfSize) * analogStickMax),
}, false);
this.translation.setValue(clampedPosition);
}
} else {
this.analogReset();
}
Expand Down

0 comments on commit ef1de6c

Please sign in to comment.