Jypeli  9
The simple game programming library
Controls.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2013 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28  */
29 
30 using System.Collections.Generic;
31 using Jypeli.Controls;
32 using Microsoft.Xna.Framework;
33 
34 namespace Jypeli
35 {
36  public partial class Game : ControlContexted
37  {
39  private List<Controller> _controllers;
40 
44  public Keyboard Keyboard { get; private set; }
45 
49  public Mouse Mouse { get; private set; }
50 
55  {
56  get { return Device.Accelerometer; }
57  }
58 
62  public TouchPanel TouchPanel { get; private set; }
63 
67  public BackButton PhoneBackButton { get; private set; }
68 
72  public List<GamePad> GameControllers { get; private set; }
73 
77  public GamePad ControllerOne { get { return GameControllers[0]; } }
78 
82  public GamePad ControllerTwo { get { return GameControllers[1]; } }
83 
87  public GamePad ControllerThree { get { return GameControllers[2]; } }
88 
92  public GamePad ControllerFour { get { return GameControllers[3]; } }
93 
98  {
99  get { return Instance._context; }
100  }
101 
102  public bool IsModal
103  {
104  get { return false; }
105  }
106 
107 #if WINDOWS_PHONE || ANDROID
108  public new bool IsMouseVisible
109  {
110  get { return false; }
111  set { }
112  }
113 #endif
114 
115  private void InitControls()
116  {
117  _context = new ListenContext() { Active = true };
118 
119  Keyboard = new Keyboard();
120  Mouse = new Mouse( Screen );
121  PhoneBackButton = new BackButton();
122  TouchPanel = new TouchPanel( Screen );
123 
124  GameControllers = new List<GamePad>( 4 );
125  GameControllers.Add( new GamePad( PlayerIndex.One ) );
126  GameControllers.Add( new GamePad( PlayerIndex.Two ) );
127  GameControllers.Add( new GamePad( PlayerIndex.Three ) );
128  GameControllers.Add( new GamePad( PlayerIndex.Four ) );
129 
130  _controllers = new List<Controller>();
131  _controllers.Add( Keyboard );
132 #if !WINDOWS_PHONE && !ANDROID
133  _controllers.Add( Mouse );
134 #endif
136  _controllers.Add( TouchPanel );
137 #if WINDOWS_PHONE || ANDROID
139 #endif
140 #if NETCOREAPP
141  _controllers.AddRange( GameControllers );
142 #endif
143 
144  IsMouseVisible = true;
145  }
146 
147  private void UpdateControls( Time gameTime )
148  {
149  _controllers.ForEach( c => c.Update() );
150  //_gamePads.ForEach( g => g.UpdateVibrations( gameTime ) );
151  }
152 
156  public void ClearControls()
157  {
158  _controllers.ForEach( c => c.Clear() );
159  }
160 
164  public void ShowControlHelp()
165  {
166  _controllers.ForEach( c => MessageDisplay.Add( c.GetHelpTexts() ) );
167  }
168 
172  public void ShowControlHelp( Controller controller )
173  {
174  MessageDisplay.Add( controller.GetHelpTexts() );
175  }
176 
177  private void ActivateObject( ControlContexted obj )
178  {
179  obj.ControlContext.Active = true;
180 
181  if ( obj.IsModal )
182  {
185 
186  foreach ( Layer l in Layers )
187  {
188  foreach ( IGameObject lo in l.Objects )
189  {
191  if ( lo == obj || co == null )
192  continue;
193 
195  co.ControlContext.Active = false;
196  }
197  }
198  }
199  }
200 
201  private void DeactivateObject( ControlContexted obj )
202  {
203  obj.ControlContext.Active = false;
204 
205  if ( obj.IsModal )
206  {
208 
209  foreach ( Layer l in Layers )
210  {
211  foreach ( IGameObject lo in l.Objects )
212  {
214  if ( lo == obj || co == null )
215  continue;
216 
218  }
219  }
220  }
221  }
222  }
223 }
Jypeli.Controls.ControlContexted.ControlContext
ListenContext ControlContext
Definition: ListenContext.cs:116
Jypeli.Controls.ControlContexted
Definition: ListenContext.cs:115
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.Game.IsModal
bool IsModal
Definition: Controls.cs:103
Jypeli
Definition: Automobile.cs:5
Jypeli.Game.Device
static Device Device
Laite jolla peliä pelataan.
Definition: Game.cs:95
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.Game.ControllerTwo
GamePad ControllerTwo
Toinen peliohjain.
Definition: Controls.cs:82
Jypeli.MessageDisplay.Add
void Add(string message)
Lisää uuden viestin näkymään.
Definition: MessageDisplay.cs:208
Jypeli.IGameObject
Yhteinen rajapinta kaikille peliolioille.
Definition: IGameObject.cs:11
Jypeli.Game.PhoneBackButton
BackButton PhoneBackButton
Puhelimen takaisin-näppäin.
Definition: Controls.cs:67
Jypeli.Game.ControllerFour
GamePad ControllerFour
Neljäs peliohjain.
Definition: Controls.cs:92
Jypeli.Game.Mouse
Mouse Mouse
Hiiri.
Definition: Controls.cs:49
Jypeli.Mouse
Hiiri.
Definition: Mouse.cs:46
Jypeli.MessageDisplay
Definition: MessageDisplay.cs:39
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.TouchPanel
Kosketusnäyttö.
Definition: TouchPanel.cs:20
Jypeli.Controls.ListenContext.SaveFocus
void SaveFocus()
Definition: ListenContext.cs:78
Jypeli.Game.ControllerOne
GamePad ControllerOne
Ensimmäinen peliohjain.
Definition: Controls.cs:77
Jypeli.Layer
Kerros. Vastaa olioiden piirtämisestä.
Definition: Layer.cs:32
Jypeli.Game._context
ListenContext _context
Definition: Controls.cs:38
Jypeli.Controls.ListenContext.Active
bool Active
Definition: ListenContext.cs:13
Jypeli.GamePad
Definition: GamePad.cs:41
Jypeli.Time
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
Jypeli.Game.Screen
static ScreenView Screen
Näytön dimensiot, eli koko ja reunat.
Definition: Graphics.cs:90
Jypeli.BackButton
Puhelimen (tai peliohjaimen) takaisin-näppäin.
Definition: BackButton.cs:15
Jypeli.Controls
Definition: Controller.cs:34
Jypeli.Game.GameControllers
List< GamePad > GameControllers
Lista kaikista peliohjaimista järjestyksessä.
Definition: Controls.cs:72
Jypeli.Game.ShowControlHelp
void ShowControlHelp(Controller controller)
Näyttää kontrollien ohjetekstit tietylle ohjaimelle.
Definition: Controls.cs:172
Jypeli.Controls.ListenContext.RestoreFocus
void RestoreFocus()
Definition: ListenContext.cs:84
Jypeli.Game.ClearControls
void ClearControls()
Poistaa kaikki ohjainkuuntelijat.
Definition: Controls.cs:156
Jypeli.Game.DeactivateObject
void DeactivateObject(ControlContexted obj)
Definition: Controls.cs:201
Jypeli.Game.ActivateObject
void ActivateObject(ControlContexted obj)
Definition: Controls.cs:177
Jypeli.Game.Keyboard
Keyboard Keyboard
Näppäimistö.
Definition: Controls.cs:44
System
Definition: CFFauxAttributes.cs:29
Jypeli.Accelerometer
Definition: Accelerometer.cs:12
Jypeli.Game.InitControls
void InitControls()
Definition: Controls.cs:115
Jypeli.Game.ShowControlHelp
void ShowControlHelp()
Näyttää kontrollien ohjetekstit.
Definition: Controls.cs:164
Jypeli.Game.TouchPanel
TouchPanel TouchPanel
Kosketusnäyttö
Definition: Controls.cs:62
Jypeli.Game.ControllerThree
GamePad ControllerThree
Kolmas peliohjain.
Definition: Controls.cs:87
Jypeli.Controls.ListenContext
Definition: ListenContext.cs:7
Jypeli.Keyboard
Näppäimistö.
Definition: Keyboard.cs:41
Jypeli.Controls.ControlContexted.IsModal
bool IsModal
Definition: ListenContext.cs:117
Jypeli.Game._controllers
List< Controller > _controllers
Definition: Controls.cs:39
Jypeli.Game.Layers
SynchronousList< Layer > Layers
Kerrokset, joilla pelioliot viihtyvät.
Definition: Layers.cs:14
Jypeli.Game
Definition: Content.cs:46
Jypeli.Controls.Controller
Ohjainlaite.
Definition: Controller.cs:39
Jypeli.Game.UpdateControls
void UpdateControls(Time gameTime)
Definition: Controls.cs:147
Jypeli.Layer.Objects
SynchronousList< IGameObject > Objects
Definition: Layer.cs:76
Jypeli.Game.ControlContext
ListenContext ControlContext
Pelin pääohjainkonteksti.
Definition: Controls.cs:98