android遊戲開發自學筆記3-2

fener10289發表於2012-11-19

原筆記3寫不下了,就接著這個下吧:

package com.gamestudy.mrnom.framework.impl;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.View.OnKeyListener;

import com.gamestudy.mrnom.framework.Input.KeyEvent;
import com.gamestudy.mrnom.framework.impl.Pool.PoolObjectFactory;


public class KeyboardHandler implements OnKeyListener {
		boolean[] pressedKeys = new boolean[128];
		Pool<KeyEvent> keyEventPool;
		List<KeyEvent> keyEventsBuffer = new ArrayList<KeyEvent>();
		List<KeyEvent> keyEvents = new ArrayList<KeyEvent>();
		
		public KeyboardHandler(View view) {
			PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {
			@Override
			public KeyEvent createObject() {
				return new KeyEvent();
			}
			};
			keyEventPool = new Pool<KeyEvent>(factory, 100);
			view.setOnKeyListener(this);
			view.setFocusableInTouchMode(true);
			view.requestFocus();
		}
		
		@Override
		public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
			if (event.getAction() == android.view.KeyEvent.ACTION_MULTIPLE)
				return false;
			synchronized (this) {
				KeyEvent keyEvent = keyEventPool.newObject();
				keyEvent.keyCode = keyCode;
				keyEvent.keyChar = (char) event.getUnicodeChar();
				if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) {
					keyEvent.type = KeyEvent.KEY_DOWN;
					if(keyCode > 0 && keyCode < 127)
						pressedKeys[keyCode] = true;
				}
				if (event.getAction() == android.view.KeyEvent.ACTION_UP) {
						keyEvent.type = KeyEvent.KEY_UP;
						if(keyCode > 0 && keyCode < 127)
						pressedKeys[keyCode] = false;
						}
						keyEventsBuffer.add(keyEvent);
			}
			return false;
		}

		public boolean isKeyPressed(int keyCode) {
			if (keyCode < 0 || keyCode > 127)
				return false;
			return pressedKeys[keyCode];
		}

		public List<KeyEvent> getKeyEvents() {
			synchronized (this) {
				int len = keyEvents.size();
				for (int i = 0; i < len; i++)
					keyEventPool.free(keyEvents.get(i));
					keyEvents.clear();
					keyEvents.addAll(keyEventsBuffer);
					keyEventsBuffer.clear();
					return keyEvents;
			}
		}

}


package com.gamestudy.mrnom.framework.impl;

import java.util.List;

import android.view.View.OnTouchListener;

import com.gamestudy.mrnom.framework.Input.TouchEvent;

public interface TouchHandler extends OnTouchListener {
    public boolean isTouchDown(int pointer);
    
    public int getTouchX(int pointer);
    
    public int getTouchY(int pointer);
    
    public List<TouchEvent> getTouchEvents();
}


package com.gamestudy.mrnom.framework.impl;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;
import android.view.View;

import com.gamestudy.mrnom.framework.Input.TouchEvent;
import com.gamestudy.mrnom.framework.impl.Pool.PoolObjectFactory;

public class SingleTouchHandler implements TouchHandler {
	boolean isTouched;
	int touchX;
	int touchY;
	Pool<TouchEvent> touchEventPool;
	List<TouchEvent> touchEvents = new ArrayList<TouchEvent>();
	List<TouchEvent> touchEventsBuffer = new ArrayList<TouchEvent>();
	float scaleX;
	float scaleY;

	public SingleTouchHandler(View view, float scaleX, float scaleY) {
		PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>() {
		@Override
		public TouchEvent createObject() {
		return new TouchEvent();
		}
		};
		touchEventPool = new Pool<TouchEvent>(factory, 100);
		view.setOnTouchListener(this);
		this.scaleX = scaleX;
		this.scaleY = scaleY;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		synchronized(this) {
			TouchEvent touchEvent = touchEventPool.newObject();
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					touchEvent.type = TouchEvent.TOUCH_DOWN;
					isTouched = true;
					break;
				case MotionEvent.ACTION_MOVE:
					touchEvent.type = TouchEvent.TOUCH_DRAGGED;
					isTouched = true;
					break;
				case MotionEvent.ACTION_CANCEL:
				case MotionEvent.ACTION_UP:
					touchEvent.type = TouchEvent.TOUCH_UP;
					isTouched = false;
					break;
			}
			touchEvent.x = touchX = (int)(event.getX() * scaleX);
			touchEvent.y = touchY = (int)(event.getY() * scaleY);
			touchEventsBuffer.add(touchEvent);
			return true;
		}
	}
	
	@Override
	public boolean isTouchDown(int pointer) {
		synchronized(this) {
			if(pointer == 0)
				return isTouched;
			else
				return false;
		}
	}
	@Override
	public int getTouchX(int pointer) {
		synchronized(this) {
			return touchX;
		}
	}

	@Override
	public int getTouchY(int pointer) {
		synchronized(this) {
			return touchY;
		}
	}

	@Override
	public List<TouchEvent> getTouchEvents() {
		synchronized(this) {
			int len = touchEvents.size();
			for( int i = 0; i < len; i++ )
			touchEventPool.free(touchEvents.get(i));
			touchEvents.clear();
			touchEvents.addAll(touchEventsBuffer);
			touchEventsBuffer.clear();
			return touchEvents;
		}
	}

}


package com.gamestudy.mrnom.framework.impl;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;
import android.view.View;

import com.gamestudy.mrnom.framework.Input.TouchEvent;
import com.gamestudy.mrnom.framework.impl.Pool.PoolObjectFactory;

public class MultiTouchHandler implements TouchHandler {
	boolean[] isTouched = new boolean[20];
	int[] touchX = new int[20];
	int[] touchY = new int[20];
	Pool<TouchEvent> touchEventPool;
	List<TouchEvent> touchEvents = new ArrayList<TouchEvent>();
	List<TouchEvent> touchEventsBuffer = new ArrayList<TouchEvent>();
	float scaleX;
	float scaleY;
	
	public MultiTouchHandler(View view, float scaleX, float scaleY) {
		PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>() {
		@Override
		public TouchEvent createObject() {
		return new TouchEvent();
		}
		};
		touchEventPool = new Pool<TouchEvent>(factory, 100);
		view.setOnTouchListener(this);
		this.scaleX = scaleX;
		this.scaleY = scaleY;
	}
	
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
	synchronized (this) {
	int action = event.getAction() & MotionEvent.ACTION_MASK;
	int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
	>> MotionEvent.ACTION_POINTER_ID_SHIFT;
	int pointerId = event.getPointerId(pointerIndex);
	TouchEvent touchEvent;
	switch (action) {
	case MotionEvent.ACTION_DOWN:
	case MotionEvent.ACTION_POINTER_DOWN:
	touchEvent = touchEventPool.newObject();
	touchEvent.type = TouchEvent.TOUCH_DOWN;
	touchEvent.pointer = pointerId;
	touchEvent.x = touchX[pointerId] = (int) (event
	.getX(pointerIndex) * scaleX);
	touchEvent.y = touchY[pointerId] = (int) (event
	.getY(pointerIndex) * scaleY);
	isTouched[pointerId] = true;
	touchEventsBuffer.add(touchEvent);
	break;
	case MotionEvent.ACTION_UP:
	case MotionEvent.ACTION_POINTER_UP:
	case MotionEvent.ACTION_CANCEL:
	touchEvent = touchEventPool.newObject();
	touchEvent.type = TouchEvent.TOUCH_UP;
	touchEvent.pointer = pointerId;
	touchEvent.x = touchX[pointerId] = (int) (event
	.getX(pointerIndex) * scaleX);
	touchEvent.y = touchY[pointerId] = (int) (event
	.getY(pointerIndex) * scaleY);
	isTouched[pointerId] = false;
	touchEventsBuffer.add(touchEvent);
	break;
	case MotionEvent.ACTION_MOVE:
	int pointerCount = event.getPointerCount();
	for (int i = 0; i < pointerCount; i++) {
	pointerIndex = i;
	pointerId = event.getPointerId(pointerIndex);
	touchEvent = touchEventPool.newObject();
	touchEvent.type = TouchEvent.TOUCH_DRAGGED;
	touchEvent.pointer = pointerId;
	touchEvent.x = touchX[pointerId] = (int) (event
	.getX(pointerIndex) * scaleX);
	touchEvent.y = touchY[pointerId] = (int) (event
	.getY(pointerIndex) * scaleY);
	touchEventsBuffer.add(touchEvent);
	}
	break;
	}
	return true;
	}
	}

	@Override
	public boolean isTouchDown(int pointer) {
		synchronized (this) {
			if (pointer < 0 || pointer >= 20)
				return false;
			else
				return isTouched[pointer];
		}
	}

	@Override
	public int getTouchX(int pointer) {
		synchronized (this) {
			if (pointer < 0 || pointer >= 20)
				return 0;
			else
				return touchX[pointer];
		}
	}
	
	@Override
	public int getTouchY(int pointer) {
		synchronized (this) {
			if (pointer < 0 || pointer >= 20)
				return 0;
			else
				return touchY[pointer];
		}
	}
	
	@Override
	public List<TouchEvent> getTouchEvents() {
		synchronized (this) {
			int len = touchEvents.size();
			for (int i = 0; i < len; i++)
				touchEventPool.free(touchEvents.get(i));
				touchEvents.clear();
				touchEvents.addAll(touchEventsBuffer);
				touchEventsBuffer.clear();
			return touchEvents;
		}
	}

}

package com.gamestudy.mrnom.framework.impl;

import java.util.List;

import android.content.Context;
import android.os.Build.VERSION;
import android.view.View;

import com.gamestudy.mrnom.framework.Input;

public class AndroidInput implements Input {
	AccelerometerHandler accelHandler;
	KeyboardHandler keyHandler;
	TouchHandler touchHandler;

	public AndroidInput(Context context, View view, float scaleX, float scaleY) {
		accelHandler = new AccelerometerHandler(context);
		keyHandler = new KeyboardHandler(view);
		if(Integer.parseInt(VERSION.SDK) < 5)
		touchHandler = new SingleTouchHandler(view, scaleX, scaleY);
		else
		touchHandler = new MultiTouchHandler(view, scaleX, scaleY);
	}

	@Override
	public boolean isKeyPressed(int keyCode) {
		return keyHandler.isKeyPressed(keyCode);
	}
	@Override
	public boolean isTouchDown(int pointer) {
		return touchHandler.isTouchDown(pointer);
	}
	@Override
	public int getTouchX(int pointer) {
		return touchHandler.getTouchX(pointer);
	}
	@Override
	public int getTouchY(int pointer) {
		return touchHandler.getTouchY(pointer);
	}
	@Override
	public float getAccelX() {
		return accelHandler.getAccelX();
	}
	@Override
	public float getAccelY() {
		return accelHandler.getAccelY();
	}
	@Override
	public float getAccelZ() {
		return accelHandler.getAccelZ();
	}
	@Override
	public List<TouchEvent> getTouchEvents() {
		return touchHandler.getTouchEvents();
	}
	@Override
	public List<KeyEvent> getKeyEvents() {
		return keyHandler.getKeyEvents();
	}

}

相關文章