-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add shape option to joystick #107
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an awesome new feature, thanks a lot.
Can you clean your PR though?
There are a lot of changes that are not part of the feature.
The package-lock.json shouldn't change, neither should the code style.
NOTE: You can change the code style, but it should be done in another PR and enforced by something automatic. Here, it seems to be just a local configuration of yours. Feel free to add any linter, as long as it's automatically enforced.
Changes done:
|
This comment has been minimized.
This comment has been minimized.
Hello guys. Any update on this? Should this work be finish? (it seems that only a rebase is required) |
@yoannmoinet |
I'm off with no computer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for taking the time to implement this.
I have a few suggestions though.
Also, you're missing the types, you need to update ./types/index.d.ts to add the new option.
src/nipple.js
Outdated
u.extend( | ||
styles.back, | ||
this.options.shape === 'circle' ? circleBorderStyle : squareBorderStyle | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to set it up in case it's a square
.
Simply put it in a condition and get rid of this dual variable declaration.
u.extend( | |
styles.back, | |
this.options.shape === 'circle' ? circleBorderStyle : squareBorderStyle | |
); | |
if (this.options.shape === 'circle') { | |
u.extend(styles.back, borderStyle); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the necessary changes for this suggestion!
src/collection.js
Outdated
switch (nipple.options.shape) { | ||
case 'square': | ||
// Clamping for x-axis | ||
xPosition = pos.x - nipple.position.x; | ||
yPosition = pos.y - nipple.position.y; | ||
if (Math.abs(xPosition) >= size) { | ||
xPosition = Math.sign(xPosition) * size; | ||
} | ||
|
||
var xPosition = pos.x - nipple.position.x; | ||
var yPosition = pos.y - nipple.position.y; | ||
//Clamping for y-axis | ||
if (Math.abs(yPosition) >= size) { | ||
yPosition = Math.sign(yPosition) * size; | ||
} | ||
break; | ||
default: | ||
if (dist > size) { | ||
dist = size; | ||
pos = u.findCoord(nipple.position, dist, angle); | ||
} | ||
|
||
xPosition = pos.x - nipple.position.x; | ||
yPosition = pos.y - nipple.position.y; | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of switch
.
I'd prefer a simple if - else
statement.
Starting with the default behavior, aka if (nipple.options.shape === 'circle') {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I think this process should be simplified.
Something like:
/* utils.js */
export const clamp = (pos, min, max) => ({
x: Math.min(Math.max(pos.x, min), max),
y: Math.min(Math.max(pos.y, min), max)
});
/* collection.js */
// Clamp the position.
if (nipple.options.shape === 'circle') {
dist = Math.min(dist, size);
pos = u.findCoord(nipple.position, dist, angle);
} else {
// Clamp to a square
pos = u.clamp(nipple.position, -size, size);
dist = u.distance(pos, nipple.position);
}
var xPosition = pos.x - nipple.position.x;
var yPosition = pos.y - nipple.position.y;
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yoannmoinet
Thank you for this great suggestion! I've implemented it but had to make some minor changes to the suggested clamp()
function. Here's the updated function:
/* utils.js */
// Clamp position within the range
export const clamp = (pos, nipplePos, size) => ({
// left-clamping right-clamping
x: Math.min(Math.max(pos.x, nipplePos.x - size), nipplePos.x + size),
// top-clamping bottom-clamping
y: Math.min(Math.max(pos.y, nipplePos.y - size), nipplePos.y + size)
});
Also, I've replaced switch statements with if-else statements as you requested.
Do let me know if there are any further changes required!
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done, way simpler now.
LGTM.
There are conflicts. |
NVM, I'll squash and merge. |
Published in |
Thank you soo much for accepting this PR. It is my first ever open source contribution.😁 |
No problem @htshah that was a good contribution. |
Feature add for issue #104.