Skip to content
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

Merged
merged 21 commits into from
Sep 6, 2020
Merged

feat: Add shape option to joystick #107

merged 21 commits into from
Sep 6, 2020

Conversation

htshah
Copy link
Contributor

@htshah htshah commented May 25, 2019

Feature add for issue #104.

Copy link
Owner

@yoannmoinet yoannmoinet left a 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.

@htshah
Copy link
Contributor Author

htshah commented Jun 2, 2019

Changes done:

  • Reverted package-lock.json
  • Reverted all unrelated style changes

@darkworks

This comment has been minimized.

@Nek-
Copy link

Nek- commented Aug 10, 2020

Hello guys. Any update on this? Should this work be finish? (it seems that only a rebase is required)

@htshah
Copy link
Contributor Author

htshah commented Aug 15, 2020

@yoannmoinet
Just a follow up for this PR, is there anything else on which you would like to comment?

@yoannmoinet
Copy link
Owner

I'm off with no computer.
It will have to wait next week end.
I'll add a reminder.

Copy link
Owner

@yoannmoinet yoannmoinet left a 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
Comment on lines 136 to 139
u.extend(
styles.back,
this.options.shape === 'circle' ? circleBorderStyle : squareBorderStyle
);
Copy link
Owner

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.

Suggested change
u.extend(
styles.back,
this.options.shape === 'circle' ? circleBorderStyle : squareBorderStyle
);
if (this.options.shape === 'circle') {
u.extend(styles.back, borderStyle);
}

Copy link
Contributor Author

@htshah htshah Aug 30, 2020

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!

Comment on lines 420 to 443
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;
}
Copy link
Owner

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') {

Copy link
Owner

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?

Copy link
Contributor Author

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!

@htshah htshah requested a review from yoannmoinet August 30, 2020 11:54
Copy link
Owner

@yoannmoinet yoannmoinet left a 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.

@yoannmoinet
Copy link
Owner

There are conflicts.
Once they are fixed I'll merge the PR.

@yoannmoinet
Copy link
Owner

NVM, I'll squash and merge.

@yoannmoinet yoannmoinet merged commit 5a8f8ba into yoannmoinet:master Sep 6, 2020
@yoannmoinet
Copy link
Owner

Published in v0.8.6

@htshah
Copy link
Contributor Author

htshah commented Sep 7, 2020

Thank you soo much for accepting this PR. It is my first ever open source contribution.😁

@yoannmoinet
Copy link
Owner

No problem @htshah that was a good contribution.
Well done, and thank YOU!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants