android遊戲開發自學筆記3-2
原筆記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();
}
}
相關文章
- Android 開發學習筆記Android筆記
- Android開發筆記Android筆記
- 遊戲開發流程與思想學習筆記(持續更新)遊戲開發筆記
- 安卓開發筆記——數獨遊戲安卓筆記遊戲
- Godot Breakeys Godot Beginner Tutorial 遊戲開發筆記Go遊戲開發筆記
- [Lua遊戲AI開發指南] 筆記零 - 框架搭建遊戲AI筆記框架
- Hilo開發H5小遊戲踩坑筆記H5遊戲筆記
- Android開發筆記[17]-桌面小部件Android筆記
- Android開發筆記[10]-關於頁Android筆記
- Android開發筆記——TextView 多行時 ellipsizeAndroid筆記TextView
- 騰訊遊戲學院專家:手遊開發,該如何做好Android記憶體優化?遊戲Android記憶體優化
- Android 學習筆記雜記Android筆記
- C語言0基礎入門遊戲輔助開發—學習筆記01C語言遊戲筆記
- Chrome 自帶開發者工具筆記Chrome筆記
- Java學習筆記(一) 猜數遊戲Java筆記遊戲
- 數字遊戲策劃學習筆記遊戲筆記
- Java自學筆記⑪Java筆記
- Android開發筆記[13]-圖案密碼Android筆記密碼
- Android開發筆記[18]-使用本地模組Android筆記
- Android Studio學習筆記Android筆記
- Android Linker學習筆記Android筆記
- Android學習筆記·ANRAndroid筆記
- Android學習筆記·HandlerAndroid筆記
- Android學習筆記·ADBAndroid筆記
- Android學習筆記一Android筆記
- Android SQLite學習筆記AndroidSQLite筆記
- Android遊戲開發示例——彈幕+戰棋Android遊戲開發
- 博弈論:公平組合遊戲(Nim 遊戲 & SG 定理)學習筆記遊戲筆記
- springboot 開發學習筆記1Spring Boot筆記
- JAVA自學筆記(4)Java筆記
- Android學習過程的Cursor遊標填坑筆記Android筆記
- Android Gradle 學習筆記整理AndroidGradle筆記
- Android 學習筆記思考篇Android筆記
- Android 學習筆記核心篇Android筆記
- 2018.03.06 Android Handler學習筆記Android筆記
- cocos2dx遊戲開發——微信打飛機學習筆記(九)——BulletLayer的搭建遊戲開發筆記
- 遊戲開發雜記(三) 開發及學習過程中的體會遊戲開發
- 微信小遊戲開發小記遊戲開發
- JS開發步驟學習筆記JS筆記