My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.replica.replicaisland;

/**
* Manages input from a roller wheel and touch screen. Reduces frequent UI messages to
* an average direction over a short period of time.
*/
public class InputSystem extends BaseObject {
private InputTouchScreen mTouchScreen = new InputTouchScreen();
private InputXY mOrientationSensor = new InputXY();
private InputXY mTrackball = new InputXY();
private InputKeyboard mKeyboard = new InputKeyboard();
private int mScreenRotation = 0;
private float mOrientationInput[] = new float[3];
private float mOrientationOutput[] = new float[3];

public InputSystem() {
super();
reset();
}

@Override
public void reset() {
mTrackball.reset();
mTouchScreen.reset();
mKeyboard.resetAll();
mOrientationSensor.reset();
}

public void roll(float x, float y) {
TimeSystem time = sSystemRegistry.timeSystem;
mTrackball.press(time.getGameTime(), mTrackball.getX() + x, mTrackball.getY() + y);
}

public void touchDown(int index, float x, float y) {
ContextParameters params = sSystemRegistry.contextParameters;
TimeSystem time = sSystemRegistry.timeSystem;
// Change the origin of the touch location from the top-left to the bottom-left to match
// OpenGL space.
// TODO: UNIFY THIS SHIT
mTouchScreen.press(index, time.getGameTime(), x, params.gameHeight - y);
}

public void touchUp(int index, float x, float y) {
// TODO: record up location?
mTouchScreen.release(index);
}


public void setOrientation(float x, float y, float z) {
// The order of orientation axes changes depending on the rotation of the screen.
// Some devices call landscape "ROTAION_90" (e.g. phones), while others call it
// "ROTATION_0" (e.g. tablets). So we need to adjust the axes from canonical
// space into screen space depending on the rotation of the screen from
// whatever this device calls "default."
mOrientationInput[0] = x;
mOrientationInput[1] = y;
mOrientationInput[2] = z;

canonicalOrientationToScreenOrientation(mScreenRotation, mOrientationInput, mOrientationOutput);

// Now we have screen space rotations around xyz.
final float horizontalMotion = mOrientationOutput[1] / 90.0f;
final float verticalMotion = mOrientationOutput[0] / 90.0f;

TimeSystem time = sSystemRegistry.timeSystem;
mOrientationSensor.press(time.getGameTime(), horizontalMotion, verticalMotion);

}

public void keyDown(int keycode) {
TimeSystem time = sSystemRegistry.timeSystem;
final float gameTime = time.getGameTime();
mKeyboard.press(gameTime, keycode);
}

public void keyUp(int keycode) {
mKeyboard.release(keycode);
}

public void releaseAllKeys() {
mTrackball.releaseX();
mTrackball.releaseY();
mTouchScreen.resetAll();
mKeyboard.releaseAll();
mOrientationSensor.release();
}

public InputTouchScreen getTouchScreen() {
return mTouchScreen;
}

public InputXY getOrientationSensor() {
return mOrientationSensor;
}

public InputXY getTrackball() {
return mTrackball;
}

public InputKeyboard getKeyboard() {
return mKeyboard;
}

public void setScreenRotation(int rotation) {
mScreenRotation = rotation;
}

// Thanks to NVIDIA for this useful canonical-to-screen orientation function.
// More here: http://developer.download.nvidia.com/tegra/docs/tegra_android_accelerometer_v5f.pdf
static void canonicalOrientationToScreenOrientation(
int displayRotation, float[] canVec, float[] screenVec) {
final int axisSwap[][] = {
{ 1, -1, 0, 1 }, // ROTATION_0
{-1, -1, 1, 0 }, // ROTATION_90
{-1, 1, 0, 1 }, // ROTATION_180
{ 1, 1, 1, 0 } }; // ROTATION_270

final int[] as = axisSwap[displayRotation];
screenVec[0] = (float)as[0] * canVec[ as[2] ];
screenVec[1] = (float)as[1] * canVec[ as[3] ];
screenVec[2] = canVec[2];
}



}

Change log

r7 by sm0a9f4 on Jan 15, 2011   Diff
+ Fixed lots of bugs.  Most notably a
divide-by-zero case in the collision
system.
+ Add fades to all activity transitions
(seems unreliable, though; race
condition?)
+ Add Extras Menu, unlockable Linear Mode
and Level Select
+ Add difficulty settings: Baby, Kids, and
Adult
+ Add on-screen control interface; add
support for muli-touch input events
...
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by sm0a9f4 on May 2, 2010   Diff
Public release v1.3.
+ Rewrote input system to better
abstract different hardware from game.
Major rework to this section.
+ Added nefarious hacks via "Safe
...
r4 by sm0a9f4 on Apr 2, 2010   Diff
- Stop using VBOs when rendering with
software (no speed improvement, and
there are bugs in 1.5 and 1.6 software
renderers related to freeing VBOs).
This should fix the Cliq and other
...
r2 by sm0a9f4 on Mar 18, 2010   Diff
Initial submission of Replica Island
1.1 source and assets.
All revisions of this file

File info

Size: 4705 bytes, 142 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting