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

Gamepad scale and move #43

Open
TheNetStriker opened this issue Feb 18, 2013 · 0 comments
Open

Gamepad scale and move #43

TheNetStriker opened this issue Feb 18, 2013 · 0 comments

Comments

@TheNetStriker
Copy link

I wrote some code to support scaling and moving in an image in gesture-imageview using a gamepad. It works great with XBOX gamepad. Maybe it will also work for the Ouya gamepad, but I have to wait for mine before I can test this. :)

Those two methods belong in GestureImageView.java:

public PointF getTopLeft() {
    PointF topLeft = new PointF();
    int effectiveWidth = Math.round( (float) getImageWidth() * getScale() );
    int effectiveHeight = Math.round( (float) getImageHeight() * getScale() );
    float diff = (float)(effectiveWidth - displayWidth) / 2.0f;
    topLeft.x = centerX - diff;
    float diff2 = (float)(effectiveHeight - displayHeight) / 2.0f;
    topLeft.y = centerY - diff2;
    return topLeft;
}

public void AxisMoveAndScale(float scaleAxisValue, float xAxisValue, float yAxisValue) {
     float scale = startingScale + (scaleAxisValue * (15 - startingScale));          
     setScale(scale);

     PointF topLeft = getTopLeft();

     float x = centerX + (topLeft.x - centerX) * xAxisValue;
     float y = centerY + (topLeft.y - centerY) * yAxisValue;

     setPosition(x, y);
     redraw();
}

After that call the method from any activity like this:

public boolean onGenericMotionEvent(MotionEvent event) {
    if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
         if (event.getAction() == MotionEvent.ACTION_MOVE) {
             float scaleAxisValue = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
             float xAxisValue = event.getAxisValue(MotionEvent.AXIS_Z);
             float yAxisValue = event.getAxisValue(MotionEvent.AXIS_RZ);
             currentGestureImageView.AxisMoveAndScale(scaleAxisValue, xAxisValue, yAxisValue);
             return true;
         }
     }
    return super.onGenericMotionEvent(event);
}
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

No branches or pull requests

1 participant