@@ -115,7 +115,6 @@ public interface OnTriggerListener {
115115 private int mMaxTargetWidth ;
116116
117117 private float mOuterRadius = 0.0f ;
118- private float mHitRadius = 0.0f ;
119118 private float mSnapMargin = 0.0f ;
120119 private boolean mDragging ;
121120 private int mNewTargetResources ;
@@ -211,7 +210,6 @@ public GlowPadView(Context context, AttributeSet attrs) {
211210 TypedArray a = context .obtainStyledAttributes (attrs , R .styleable .GlowPadView );
212211 mInnerRadius = a .getDimension (R .styleable .GlowPadView_innerRadius , mInnerRadius );
213212 mOuterRadius = a .getDimension (R .styleable .GlowPadView_outerRadius , mOuterRadius );
214- mHitRadius = a .getDimension (R .styleable .GlowPadView_hitRadius , mHitRadius );
215213 mSnapMargin = a .getDimension (R .styleable .GlowPadView_snapMargin , mSnapMargin );
216214 mVibrationDuration = a .getInt (R .styleable .GlowPadView_vibrationDuration ,
217215 mVibrationDuration );
@@ -280,7 +278,6 @@ private int getResourceId(TypedArray a, int id) {
280278
281279 private void dump () {
282280 Log .v (TAG , "Outer Radius = " + mOuterRadius );
283- Log .v (TAG , "HitRadius = " + mHitRadius );
284281 Log .v (TAG , "SnapMargin = " + mSnapMargin );
285282 Log .v (TAG , "FeedbackCount = " + mFeedbackCount );
286283 Log .v (TAG , "VibrationDuration = " + mVibrationDuration );
@@ -799,7 +796,6 @@ private void handleMove(MotionEvent event) {
799796 final int historySize = event .getHistorySize ();
800797 ArrayList <TargetDrawable > targets = mTargetDrawables ;
801798 int ntargets = targets .size ();
802- final boolean singleTarget = ntargets == 1 ;
803799 float x = 0.0f ;
804800 float y = 0.0f ;
805801 for (int k = 0 ; k < historySize + 1 ; k ++) {
@@ -812,31 +808,29 @@ private void handleMove(MotionEvent event) {
812808 final float scale = touchRadius > mOuterRadius ? mOuterRadius / touchRadius : 1.0f ;
813809 float limitX = tx * scale ;
814810 float limitY = ty * scale ;
811+ double angleRad = Math .atan2 (-ty , tx );
815812
816813 if (!mDragging ) {
817814 trySwitchToFirstTouchState (eventX , eventY );
818815 }
819816
820817 if (mDragging ) {
821- if (singleTarget ) {
822- // Snap to outer ring if there's only one target
823- float snapRadius = mOuterRadius - mSnapMargin ;
824- if (touchRadius > snapRadius ) {
825- activeTarget = 0 ;
826- }
827- } else {
828- // For more than one target, snap to the closest one less than hitRadius away.
829- float best = Float .MAX_VALUE ;
830- final float hitRadius2 = mHitRadius * mHitRadius ;
831- // Find first target in range
832- for (int i = 0 ; i < ntargets ; i ++) {
833- TargetDrawable target = targets .get (i );
834- float dx = limitX - target .getX ();
835- float dy = limitY - target .getY ();
836- float dist2 = dx *dx + dy *dy ;
837- if (target .isEnabled () && dist2 < hitRadius2 && dist2 < best ) {
818+ // For multiple targets, snap to the one that matches
819+ final float snapRadius = mOuterRadius - mSnapMargin ;
820+ final float snapDistance2 = snapRadius * snapRadius ;
821+ // Find first target in range
822+ for (int i = 0 ; i < ntargets ; i ++) {
823+ TargetDrawable target = targets .get (i );
824+
825+ double targetMinRad = (i - 0.5 ) * 2 * Math .PI / ntargets ;
826+ double targetMaxRad = (i + 0.5 ) * 2 * Math .PI / ntargets ;
827+ if (target .isEnabled ()) {
828+ boolean angleMatches =
829+ (angleRad > targetMinRad && angleRad <= targetMaxRad ) ||
830+ (angleRad + 2 * Math .PI > targetMinRad &&
831+ angleRad + 2 * Math .PI <= targetMaxRad );
832+ if (angleMatches && (dist2 (tx , ty ) > snapDistance2 )) {
838833 activeTarget = i ;
839- best = dist2 ;
840834 }
841835 }
842836 }
@@ -851,10 +845,7 @@ private void handleMove(MotionEvent event) {
851845
852846 if (activeTarget != -1 ) {
853847 switchToState (STATE_SNAP , x ,y );
854- TargetDrawable target = targets .get (activeTarget );
855- final float newX = singleTarget ? x : target .getX ();
856- final float newY = singleTarget ? y : target .getY ();
857- updateGlowPosition (newX , newY );
848+ updateGlowPosition (x , y );
858849 } else {
859850 switchToState (STATE_TRACKING , x , y );
860851 updateGlowPosition (x , y );
@@ -942,10 +933,6 @@ private void assignDefaultsIfNeeded() {
942933 if (mOuterRadius == 0.0f ) {
943934 mOuterRadius = Math .max (mOuterRing .getWidth (), mOuterRing .getHeight ())/2.0f ;
944935 }
945- if (mHitRadius == 0.0f ) {
946- // Use the radius of inscribed circle of the first target.
947- mHitRadius = mTargetDrawables .get (0 ).getWidth () / 2.0f ;
948- }
949936 if (mSnapMargin == 0.0f ) {
950937 mSnapMargin = TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP ,
951938 SNAP_MARGIN_DEFAULT , getContext ().getResources ().getDisplayMetrics ());
0 commit comments