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(android): Add rotate event #11411

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Expand Up @@ -554,6 +554,11 @@ public class TiC
*/
public static final String EVENT_RETURN = "return";

/**
* @module.api
*/
public static final String EVENT_ROTATE = "rotate";

/**
* @module.api
*/
Expand Down
Expand Up @@ -6,30 +6,6 @@
*/
package org.appcelerator.titanium.view;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollObject;
import org.appcelerator.kroll.KrollPropertyChange;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.KrollProxyListener;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiAnimationBuilder;
import org.appcelerator.titanium.util.TiAnimationBuilder.TiMatrixAnimation;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutParams;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
Expand Down Expand Up @@ -69,8 +45,29 @@
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;

import com.nineoldandroids.view.ViewHelper;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollObject;
import org.appcelerator.kroll.KrollPropertyChange;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.KrollProxyListener;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiAnimationBuilder;
import org.appcelerator.titanium.util.TiAnimationBuilder.TiMatrixAnimation;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutParams;

/**
* This class is for Titanium View implementations, that correspond with TiViewProxy.
Expand Down Expand Up @@ -1722,6 +1719,34 @@ public void onLongPress(MotionEvent e)
touchable.setOnTouchListener(new OnTouchListener() {
int pointersDown = 0;

private boolean doRotationEvent(MotionEvent event)
{
// Calculate the angle between the two fingers
float deltaX = event.getX(0) - event.getX(1);
float deltaY = event.getY(0) - event.getY(1);
double radians = Math.atan(deltaY / deltaX);
// Convert to degrees
int degrees = (int) (radians * 180 / Math.PI);
m1ga marked this conversation as resolved.
Show resolved Hide resolved

switch (event.getActionMasked()) {
m1ga marked this conversation as resolved.
Show resolved Hide resolved
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
// start
break;
case MotionEvent.ACTION_MOVE:
// rotate
KrollDict data = new KrollDict();
data.put(TiC.PROPERTY_ROTATE, degrees);
data.put(TiC.EVENT_PROPERTY_SOURCE, proxy);
fireEvent(TiC.EVENT_ROTATE, data);

break;
}

return true;
m1ga marked this conversation as resolved.
Show resolved Hide resolved
}

public boolean onTouch(View view, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP) {
Expand All @@ -1731,6 +1756,10 @@ public boolean onTouch(View view, MotionEvent event)
lastUpEvent.put(TiC.EVENT_PROPERTY_Y, yDimension.getAsDefault(view));
}

if (proxy != null && proxy.hierarchyHasListener(TiC.EVENT_ROTATE) && event.getPointerCount() == 2) {
doRotationEvent(event);
}

if (proxy != null && proxy.hierarchyHasListener(TiC.EVENT_PINCH)) {
scaleDetector.onTouchEvent(event);
if (scaleDetector.isInProgress()) {
Expand Down
12 changes: 12 additions & 0 deletions apidoc/Titanium/UI/View.yml
Expand Up @@ -425,6 +425,18 @@ events:
may result in an endless loop.
since: "2.0.0"

- name: rotate
summary: Fired when the device detects a two finger rotation.
description: |
This event is fired when doing a two finger rotation and returning the angle.
The event occurs continuously until a finger is lifted again.
platforms: [android]
properties:
- name: rotate
summary: Rotation in degrees.
type: Number
since: "9.0.0"

- name: singletap
summary: Fired when the device detects a single tap against the view.
properties:
Expand Down