Jypeli 10
The simple game programming library
Accelerometer.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using Jypeli.Controls;
3using Microsoft.Xna.Framework;
4
5namespace Jypeli
6{
8 {
9 }
10
11 public abstract class Accelerometer : Controller<Vector3, AccelerometerDirection>
12 {
13 protected bool started = false;
14
19
24 {
25 get
26 {
27 return Project2d( CurrentState );
28 }
29 }
30
35 {
36 get
37 {
38 return Project2d( PrevState );
39 }
40 }
41
42 public Vector3 Reading3d
43 {
44 get
45 {
46 return CurrentState;
47 }
48 }
49
50 public Vector3 PreviousReading3d
51 {
52 get
53 {
54 return PrevState;
55 }
56 }
57
62 {
63 get { return Game.Device.DisplayOrientation; }
64 }
65
69 public double DefaultSensitivity { get; private set; }
70
74 public double DefaultAnalogSensitivity { get; private set; }
75
79 public bool GesturesEnabled { get; set; }
80
84 public int TimeBetweenTaps { get; set; }
85
89 public int TimeBetweenShakes { get; set; }
90
92 {
96 GesturesEnabled = true;
97 TimeBetweenTaps = 300;
99 }
100
101 internal static Accelerometer Create()
102 {
103#if ANDROID
104 return new Jypeli.Android.AndroidAccelerometer();
105#else
106 return new DummyAccelerometer();
107#endif
108 }
109
110 internal virtual Vector Project2d( Vector3 spaceVector )
111 {
112 switch ( Calibration )
113 {
114 case AccelerometerCalibration.ZeroAngle:
115 return new Vector( spaceVector.X, spaceVector.Y );
116 case AccelerometerCalibration.HalfRightAngle:
117 return new Vector( spaceVector.X, spaceVector.Y - spaceVector.Z );
118 case AccelerometerCalibration.RightAngle:
119 return new Vector( spaceVector.X, -spaceVector.Z );
120 default:
121 return new Vector( spaceVector.X, spaceVector.Y );
122 }
123 }
124
128 public virtual void Start()
129 {
130 if ( started )
131 return;
132
133 started = true;
134 }
135
139 public virtual void Stop()
140 {
141 if ( !started )
142 return;
143
144 started = false;
145 }
146
147 internal override Vector3 GetState()
148 {
149 return Vector3.Zero;
150 }
151
156 public void PauseForDuration( double seconds )
157 {
158 Stop();
159 Timer.SingleShot( seconds, Start );
160 }
161
162 private string GetDirectionName( AccelerometerDirection direction )
163 {
164 switch (direction)
165 {
167 return "Tilt up";
168 case AccelerometerDirection.Down:
169 return "Tilt down";
170 case AccelerometerDirection.Left:
171 return "Tilt left";
172 case AccelerometerDirection.Right:
173 return "Tilt right";
174 case AccelerometerDirection.Tap:
175 return "Tap device";
176 case AccelerometerDirection.Shake:
177 return "Shake";
178 default:
179 return "Accelerometer";
180 }
181 }
182
183 private ChangePredicate<Vector3> MakeTriggerRule( AccelerometerDirection direction, double trigger )
184 {
185 switch ( direction )
186 {
187 case AccelerometerDirection.Any:
188 return ( Vector3 prev, Vector3 curr ) => Project2d(curr).Magnitude > trigger;
189 case AccelerometerDirection.Left:
190 return ( Vector3 prev, Vector3 curr ) => Project2d( curr ).X < -trigger;
191 case AccelerometerDirection.Right:
192 return ( Vector3 prev, Vector3 curr ) => Project2d( curr ).X > trigger;
194 return ( Vector3 prev, Vector3 curr ) => Project2d( curr ).Y > trigger;
195 case AccelerometerDirection.Down:
196 return ( Vector3 prev, Vector3 curr ) => Project2d( curr ).Y < -trigger;
197 /*case AccelerometerDirection.Shake:
198 return GestureTriggered( direction, trigger, 3 );
199 case AccelerometerDirection.Tap:
200 return GestureTriggered( direction, trigger, 1 );*/
201 default:
202 return NeverTrigger;
203 }
204 }
205
206#region Listen with no parameters
207
208 public void Listen( AccelerometerDirection direction, Action handler, string helpText )
209 {
210 this.Listen( direction, DefaultSensitivity, handler, helpText );
211 }
212
213 public void ListenAnalog( Action<AnalogState> handler, string helpText )
214 {
215 this.ListenAnalog( DefaultAnalogSensitivity, handler, helpText );
216 }
217
218 public void Listen( AccelerometerDirection direction, AccelerometerSensitivity sensitivity, Action handler, string helpText )
219 {
220 this.Listen( direction, (int)sensitivity / 100, handler, helpText );
221 }
222
223 public void ListenAnalog( AccelerometerSensitivity sensitivity, Action<AnalogState> handler, string helpText )
224 {
225 this.ListenAnalog( (int)sensitivity / 100, handler, helpText );
226 }
227
228 public Listener Listen( AccelerometerDirection direction, double trigger, Action handler, string helpText )
229 {
230 Start();
231 ChangePredicate<Vector3> rule = MakeTriggerRule(direction, trigger);
232 return AddListener( rule, direction, GetDirectionName( direction ), helpText, handler );
233 }
234
235 public Listener ListenAnalog( double trigger, Action<AnalogState> handler, string helpText )
236 {
237 Start();
238 ChangePredicate<Vector3> rule = ( Vector3 prev, Vector3 curr ) => Project2d(curr).Magnitude >= trigger;
239 Action analogHandler = delegate { handler( new AccelerometerAnalogState( this.Reading, this.PreviousReading ) ); };
240 return AddListener( rule, AccelerometerDirection.Any, "Accelerometer", helpText, analogHandler );
241 }
242
243#endregion
244 }
245}
void ListenAnalog(AccelerometerSensitivity sensitivity, Action< AnalogState > handler, string helpText)
Listener ListenAnalog(double trigger, Action< AnalogState > handler, string helpText)
Listener Listen(AccelerometerDirection direction, double trigger, Action handler, string helpText)
virtual void Stop()
Pysäyttää kiihtyvyysanturin.
Vector Reading
Kallistuksen tämänhetkinen suunta.
void PauseForDuration(double seconds)
Pysäyttää kiihtyvyysanturin annetuksi ajaksi.
virtual Vector Project2d(Vector3 spaceVector)
string GetDirectionName(AccelerometerDirection direction)
bool GesturesEnabled
Määrittää onko Shake ja Tap käytössä.
int TimeBetweenTaps
Aika millisekunteina joka pitää kulua napautusten välissä.
override Vector3 GetState()
Lukee ja palauttaa laitteen viimeisimmän tilan.
double DefaultAnalogSensitivity
Herkkyys jos kuunnellaan suuntia analogisesti ilman erikseen annettua herkkyyttä.
Vector PreviousReading
Kallistuksen edellinen suunta.
virtual void Start()
Käynnistää kiihtyvyysanturin.
double DefaultSensitivity
Herkkyys jos kuunnellaan suuntia ja eleitä ilman erikseen annettua herkkyyttä.
void ListenAnalog(Action< AnalogState > handler, string helpText)
static Accelerometer Create()
int TimeBetweenShakes
Aika millisekunteina joka pitää kulua ravistusten välissä.
void Listen(AccelerometerDirection direction, AccelerometerSensitivity sensitivity, Action handler, string helpText)
void Listen(AccelerometerDirection direction, Action handler, string helpText)
AccelerometerCalibration Calibration
Puhelimen kallistuksen nollakohta.
ChangePredicate< Vector3 > MakeTriggerRule(AccelerometerDirection direction, double trigger)
static Device Device
Laite jolla peliä pelataan.
Definition: Game.cs:101
Ajastin, joka voidaan asettaa laukaisemaan tapahtumia tietyin väliajoin.
Definition: Timer.cs:38
static void SingleShot(double seconds, Action onTimeout)
Kutsuu aliohjelmaa onTimeout annetun ajan kuluttua. Ajastin luodaan automaattisesti.
Definition: Timer.cs:220
static readonly ChangePredicate< ControllerState > NeverTrigger
Definition: Controller.cs:68
Listener AddListener(ChangePredicate< ControllerState > rule, Control control, string controlName, string helpText, Delegate handler, params object[] args)
Definition: Controller.cs:102
Ohjaintapahtumien kuuntelija.
Definition: Listener.cs:72
AccelerometerDirection
Suunta/ele joka tunnistetaan.
AccelerometerSensitivity
Herkkyys jolla kallistus/ele halutaan tunnistaa.
AccelerometerCalibration
Kalibrointi puhelimen kallistuksen nollakohdalle. (Asento missä puhelinta ei ole kallistettu yhtää...
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
double X
Vektorin X-komponentti.
Definition: Vector.cs:334
double Magnitude
Vektorin pituus.
Definition: Vector.cs:345