Click here to Skip to main content
13,668,134 members
Click here to Skip to main content

Tagged as

Stats

33.1K views
442 downloads
31 bookmarked
Posted 14 Jan 2013
Licenced CPOL

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

, 6 Dec 2013
A DSL for creating touch gestures in Android.
AndroidGestureDSL Version 1.1
AndroidGestureDSL
.classpath
.project
res
drawable-hdpi
icon.png
drawable-ldpi
icon.png
drawable-mdpi
icon.png
layout
values
src
com
hfk
android
gestures
dsl
AndroidGestureDSLSample
.classpath
.project
res
drawable-hdpi
icon.png
drawable-ldpi
icon.png
drawable-mdpi
icon.png
layout
values
src
com
hfk
android
sample
actions
conditions
gestures
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)

Share

About the Author

Serge Desmedt
Software Developer (Senior)
Belgium Belgium
No Biography provided

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180820.1 | Last Updated 6 Dec 2013
Article Copyright 2013 by Serge Desmedt
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid