• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.replica.replicaisland;
18 
19 /**
20  * Manages input from a roller wheel and touch screen.  Reduces frequent UI messages to
21  * an average direction over a short period of time.
22  */
23 public class InputSystem extends BaseObject {
24 	private InputTouchScreen mTouchScreen = new InputTouchScreen();
25 	private InputXY mOrientationSensor = new InputXY();
26 	private InputXY mTrackball = new InputXY();
27     private InputKeyboard mKeyboard = new InputKeyboard();
28     private int mScreenRotation = 0;
29     private float mOrientationInput[] = new float[3];
30     private float mOrientationOutput[] = new float[3];
31 
InputSystem()32     public InputSystem() {
33         super();
34         reset();
35     }
36 
37     @Override
reset()38     public void reset() {
39     	mTrackball.reset();
40     	mTouchScreen.reset();
41     	mKeyboard.resetAll();
42     	mOrientationSensor.reset();
43     }
44 
roll(float x, float y)45     public void roll(float x, float y) {
46         TimeSystem time = sSystemRegistry.timeSystem;
47     	mTrackball.press(time.getGameTime(), mTrackball.getX() + x, mTrackball.getY() + y);
48     }
49 
touchDown(int index, float x, float y)50     public void touchDown(int index, float x, float y) {
51 	   ContextParameters params = sSystemRegistry.contextParameters;
52 	   TimeSystem time = sSystemRegistry.timeSystem;
53 	   // Change the origin of the touch location from the top-left to the bottom-left to match
54 	   // OpenGL space.
55 	   // TODO: UNIFY THIS SHIT
56 	   mTouchScreen.press(index, time.getGameTime(), x, params.gameHeight - y);
57     }
58 
touchUp(int index, float x, float y)59     public void touchUp(int index, float x, float y) {
60     	// TODO: record up location?
61     	mTouchScreen.release(index);
62     }
63 
64 
setOrientation(float x, float y, float z)65     public void setOrientation(float x, float y, float z) {
66     	// The order of orientation axes changes depending on the rotation of the screen.
67     	// Some devices call landscape "ROTAION_90" (e.g. phones), while others call it
68     	// "ROTATION_0" (e.g. tablets).  So we need to adjust the axes from canonical
69     	// space into screen space depending on the rotation of the screen from
70     	// whatever this device calls "default."
71     	mOrientationInput[0] = x;
72     	mOrientationInput[1] = y;
73     	mOrientationInput[2] = z;
74 
75     	canonicalOrientationToScreenOrientation(mScreenRotation, mOrientationInput, mOrientationOutput);
76 
77     	// Now we have screen space rotations around xyz.
78     	final float horizontalMotion = mOrientationOutput[1] / 90.0f;
79         final float verticalMotion = mOrientationOutput[0] / 90.0f;
80 
81         TimeSystem time = sSystemRegistry.timeSystem;
82         mOrientationSensor.press(time.getGameTime(), horizontalMotion, verticalMotion);
83 
84     }
85 
keyDown(int keycode)86     public void keyDown(int keycode) {
87     	TimeSystem time = sSystemRegistry.timeSystem;
88         final float gameTime = time.getGameTime();
89         mKeyboard.press(gameTime, keycode);
90     }
91 
keyUp(int keycode)92     public void keyUp(int keycode) {
93     	mKeyboard.release(keycode);
94     }
95 
releaseAllKeys()96     public void releaseAllKeys() {
97     	mTrackball.releaseX();
98     	mTrackball.releaseY();
99     	mTouchScreen.resetAll();
100     	mKeyboard.releaseAll();
101     	mOrientationSensor.release();
102     }
103 
getTouchScreen()104 	public InputTouchScreen getTouchScreen() {
105 		return mTouchScreen;
106 	}
107 
getOrientationSensor()108 	public InputXY getOrientationSensor() {
109 		return mOrientationSensor;
110 	}
111 
getTrackball()112 	public InputXY getTrackball() {
113 		return mTrackball;
114 	}
115 
getKeyboard()116 	public InputKeyboard getKeyboard() {
117 		return mKeyboard;
118 	}
119 
setScreenRotation(int rotation)120 	public void setScreenRotation(int rotation) {
121 		mScreenRotation = rotation;
122 	}
123 
124 	// Thanks to NVIDIA for this useful canonical-to-screen orientation function.
125 	// More here: http://developer.download.nvidia.com/tegra/docs/tegra_android_accelerometer_v5f.pdf
canonicalOrientationToScreenOrientation( int displayRotation, float[] canVec, float[] screenVec)126 	static void canonicalOrientationToScreenOrientation(
127 			int displayRotation, float[] canVec, float[] screenVec) {
128 		final int axisSwap[][] = {
129 			{ 1, -1, 0, 1 },   // ROTATION_0
130 			{-1, -1, 1, 0 },   // ROTATION_90
131 			{-1,  1, 0, 1 },   // ROTATION_180
132 			{ 1,  1, 1, 0 } }; // ROTATION_270
133 
134 		final int[] as = axisSwap[displayRotation];
135 		screenVec[0] = (float)as[0] * canVec[ as[2] ];
136 		screenVec[1] = (float)as[1] * canVec[ as[3] ];
137 		screenVec[2] = canVec[2];
138 	}
139 
140 
141 
142 }
143