Click here to Skip to main content
15,884,628 members
Articles / Mobile Apps / Android

A Domain Specific Language for Android touch events: Part 1: Description and usage of the DSL

Rate me:
Please Sign up or sign in to vote.
4.98/5 (14 votes)
6 Dec 2013CPOL9 min read 46.6K   467   31  
A DSL for creating touch gestures in Android.
package com.hfk.android.sample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.hfk.android.ScreenVector;
import com.hfk.android.gestures.TouchHandler;
import com.hfk.android.sample.gestures.ClickAndDoubleClickOnRectangleGesture;
import com.hfk.android.sample.gestures.ClickOnRectangleGesture;
import com.hfk.android.sample.gestures.DoubleClickOnRectangleGesture;
import com.hfk.android.sample.gestures.DragRectangleGesture;
import com.hfk.android.sample.gestures.DragRectangleOrShowMessageGesture;
import com.hfk.android.sample.gestures.LongClickOutsideRectangle;
import com.hfk.android.sample.gestures.SwipeRectangleGesture;

public class AndroidGestureDSLSampleView extends View {

	public static final String ENABLED_GESTURES = "EnabledGestures";

	public static final String CLICK_GESTURE = "ClickGesture";
	public static final String DBLCLICK_GESTURE = "DblClickGesture";
	public static final String CLICKDBLCLICK_GESTURE = "ClickDblClickGesture";
	public static final String DRAG_GESTURE = "DragGesture";
	public static final String LONGTOUCH_GESTURE = "LongTouchGesture";
	public static final String DRAGLONGTOUCH_GESTURE = "DragLongTouchGesture";
	
	public AndroidGestureDSLSampleView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	    touchHandler = new TouchHandler();
	    
	    ClickOnRectangleGesture clickOnRectangleBuilder = new ClickOnRectangleGesture(this);
	    DoubleClickOnRectangleGesture doubleClickOnRectangleBuilder = new DoubleClickOnRectangleGesture(this);
	    ClickAndDoubleClickOnRectangleGesture clickAndDoubleClickOnRectangleBuilder = new ClickAndDoubleClickOnRectangleGesture(this);
	    LongClickOutsideRectangle longClickOutsideRectangleBuilder = new LongClickOutsideRectangle(this);
	    DragRectangleGesture dragRectangleGestureBuilder = new DragRectangleGesture(this);
	    DragRectangleOrShowMessageGesture dragRectangleOrShowMessageGestureBuilder = new DragRectangleOrShowMessageGesture(this);
	    SwipeRectangleGesture swipeRectangleGesture = new SwipeRectangleGesture(this);
	    
		//touchHandler.addGesture(clickOnRectangleBuilder.create());
	    //touchHandler.addGesture(doubleClickOnRectangleBuilder.create());
		//touchHandler.addGesture(clickAndDoubleClickOnRectangleBuilder.create());
		//touchHandler.addGesture(longClickOutsideRectangleBuilder.create());
		//touchHandler.addGesture(dragRectangleGestureBuilder.create());
		//touchHandler.addGesture(dragRectangleOrShowMessageGestureBuilder.create());
		touchHandler.addGesture(swipeRectangleGesture.create());
	}
    
    public boolean onTouchEvent(MotionEvent motion)   { 
    	
    	touchHandler.onTouchEvent(motion);
    	
    	invalidate();
    	return true;  	
    }   

	protected void onDraw(Canvas canvas) {
    	Paint paint = new Paint();
    	paint.setColor(isOnRectangleResult ? Color.GREEN : Color.RED);
		if(rect == null)
			rect = getRect();
		
//    	Paint textPaint = new Paint();
//    	textPaint.setColor(isInvalidated ? Color.YELLOW : Color.GREEN);
//		canvas.drawText(drawMessage, 10, 10, textPaint);
//
//		canvas.drawText(isOnRectangleXCoord, 10, 25, paint);
//		canvas.drawText(isOnRectangleYCoord, 10, 35, paint);
		
		canvas.drawRect(rect, paint);
	}
	
	public boolean IsOnRectangle(ScreenVector position)
	{
//		float centerX = this.getWidth()/2;
//		float centerY = this.getHeight()/2;
//		
//		float size = this.getWidth()/4;
		
		if(rect == null)
			rect = getRect();

		isOnRectangleResult = false;
		if(rect.contains(position.x, position.y) )
		{
			isOnRectangleResult = true;
		}
		
		isOnRectangleXCoord = "X: l:" + ((Integer)(rect.left)).toString() + ",x:" + ((Integer)(position.x)).toString() + ",r:" +((Integer)(rect.right)).toString() ;
		
		return isOnRectangleResult;
	}
	
	public void showMessage(String message)
	{
		Toast msg = Toast.makeText(AndroidGestureDSLSampleView.this.getContext(), message, Toast.LENGTH_SHORT);
		msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
		msg.show();		
	}
	
	public void drawMessage(String message)
	{
		drawMessage = message;
		invalidate();	
	}
	
	public void isInvalidated(boolean value)
	{
		isInvalidated = value;
	}
	
	public void setRectangleCenter(Point center)
	{
		rect = getRect(center.x, center.y);
		invalidate();
	}
	
	public Point getRectangleCenter()
	{
		if(rect == null)
			rect = getRect();
		
		return new Point(rect.centerX(), rect.centerY());
	}
	
	public String getRectangleDimensions()
	{
		Integer size = getSize();
		return "width:" + size.toString() + ", height:" + size.toString();
	}
	
	private Rect getRect()
	{
		int centerX = this.getWidth()/2;
		int centerY = this.getHeight()/2;
		
		return getRect(centerX, centerY);
	}
	
	private Rect getRect(int centerX, int centerY)
	{
		int size = getSize();
		
		return new Rect(centerX - (size/2), centerY - (size/2), centerX + (size/2), centerY + (size/2));
	}
	
	private int getSize()
	{
		return this.getWidth()/4;
	}
    
	Rect rect = null;
    TouchHandler touchHandler;
    
    boolean isOnRectangleResult = true;
    String isOnRectangleXCoord = "X: none";
    String isOnRectangleYCoord = "Y: none";
    
    String drawMessage = "None";
    boolean isInvalidated = false;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions