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(improvement): restJoystick now accepts an object #162

Merged
merged 2 commits into from
Jun 28, 2021

Conversation

htshah
Copy link
Contributor

@htshah htshah commented May 30, 2021

Currently the restJoystick option only accepts boolean values. This means that I can't specify which axis should be re-centered on rest state. For example, in my use case I only want the Y-axis to be reset on rest state while the X-axis should remain in the same position.

This improvement will add this capability to the library. This is how we'll be able to use restJoystick option:

var joystick = nipple.create({
    restJoystick: true,
    // This is converted to {x: true, y: true}

    // OR
    restJoystick: false,
    // This is converted to {x: false, y: false}

    // OR
    restJoystick: {x: false},
    // This is converted to {x: false, y: true}

    // OR
    restJoystick: {x: false, y: true},
});

Although a small improvement, it'll be useful of many of us.

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.

Some formatting issues and I don't understand why you had to duplicate the transformation logic.

Although, I wouldn't keep that logic randomly placed in the code, I'd just do it where we actually use the option.

src/nipple.js Outdated
Comment on lines 41 to 48
if(typeof restJoystick === 'boolean'){
this.options.restJoystick = {
x: restJoystick,
y: restJoystick,
};
}else if(typeof restJoystick === 'object'){
this.options.restJoystick = u.safeExtend({x:true,y:true},restJoystick);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(typeof restJoystick === 'boolean'){
this.options.restJoystick = {
x: restJoystick,
y: restJoystick,
};
}else if(typeof restJoystick === 'object'){
this.options.restJoystick = u.safeExtend({x:true,y:true},restJoystick);
}
if (typeof restJoystick === 'boolean'){
this.options.restJoystick = {
x: restJoystick,
y: restJoystick,
};
} else if (typeof restJoystick === 'object') {
this.options.restJoystick = u.safeExtend({x:true, y:true}, restJoystick);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.. (removed this duplicated logic)

Comment on lines 55 to 64
const restJoystick = this.options.restJoystick;

if(typeof restJoystick === 'boolean'){
this.options.restJoystick = {
x: restJoystick,
y: restJoystick,
};
}else if(typeof restJoystick === 'object'){
this.options.restJoystick = u.safeExtend({x:true,y:true},restJoystick);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const restJoystick = this.options.restJoystick;
if(typeof restJoystick === 'boolean'){
this.options.restJoystick = {
x: restJoystick,
y: restJoystick,
};
}else if(typeof restJoystick === 'object'){
this.options.restJoystick = u.safeExtend({x:true,y:true},restJoystick);
}
const restJoystick = this.options.restJoystick;
if (typeof restJoystick === 'boolean') {
this.options.restJoystick = {
x: restJoystick,
y: restJoystick,
};
} else if (typeof restJoystick === 'object') {
this.options.restJoystick = u.safeExtend({x:true, y:true}, restJoystick);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.. (removed this duplicated logic)

src/nipple.js Show resolved Hide resolved
src/nipple.js Outdated
Comment on lines 258 to 261
self.setPosition(cb, {
x: self.options.restJoystick.x ?0 :self.instance.frontPosition.x,
y: self.options.restJoystick.y ?0 :self.instance.frontPosition.y
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to have this logic just once where we do actually use it.
You can remove the other logics that you duplicated.

Suggested change
self.setPosition(cb, {
x: self.options.restJoystick.x ?0 :self.instance.frontPosition.x,
y: self.options.restJoystick.y ?0 :self.instance.frontPosition.y
});
if (self.options.restJoystick) {
const rest = self.options.restJoystick;
const newPosition = {};
newPosition.x = rest === true || rest.x ? 0 : self.instance.frontPosition.x;
newPosition.y = rest === true || rest.y ? 0 : self.instance.frontPosition.y;
self.setPosition(cb, newPosition);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored it as you request..

@htshah htshah requested a review from yoannmoinet June 4, 2021 10:05
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.

Awesome, LGTM.
Sorry for the delay, I'll make a release later today.

@yoannmoinet yoannmoinet merged commit ce3a67d into yoannmoinet:master Jun 28, 2021
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.

2 participants