Jypeli  9
The simple game programming library
Controller.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;
31 using System.Collections.Generic;
32 
33 namespace Jypeli.Controls
34 {
38  public interface Controller
39  {
43  void Clear();
44 
49  void Update();
50 
55  IEnumerable<string> GetHelpTexts();
56  }
57 
63  public abstract class Controller<ControllerState, Control> : Controller where Control : IComparable
64  {
65  protected static readonly ChangePredicate<ControllerState> AlwaysTrigger
66  = delegate { return true; };
67  protected static readonly ChangePredicate<ControllerState> NeverTrigger
68  = delegate { return false; };
69 
72 
76  public ControllerState PrevState { get; protected set; }
77 
81  public ControllerState CurrentState { get; protected set; }
82 
86  internal abstract ControllerState GetState();
87 
92  public void Update()
93  {
96 
97  listeners.Update( Game.Time );
98  disabledListeners.Update(Game.Time);
99  listeners.ForEach( l => l.CheckAndInvoke( PrevState, CurrentState ) );
100  }
101 
102  protected Listener AddListener( ChangePredicate<ControllerState> rule, Control control, string controlName, string helpText, Delegate handler, params object[] args )
103  {
104  var l = new Listener<ControllerState, Control>( rule, Game.Instance.ControlContext, control, controlName, helpText, handler, args );
105  listeners.Add( l );
106  return l;
107  }
108 
109  public void Clear()
110  {
111  listeners.Clear();
112  }
113 
118  public void Disable( Predicate<Listener<ControllerState, Control>> predicate )
119  {
120  foreach ( var l in listeners )
121  {
122  if ( predicate( l ) )
123  {
124  // Note: synchronous list, does not actually change the list while iterating
125  listeners.Remove( l );
126  disabledListeners.Add( l );
127  }
128  }
129  }
130 
135  public void Enable( Predicate<Listener<ControllerState, Control>> predicate )
136  {
137  foreach ( var l in disabledListeners )
138  {
139  if ( predicate( l ) )
140  {
141  // Note: synchronous list, does not actually change the list while iterating
142  listeners.Add( l );
143  disabledListeners.Remove( l );
144  }
145  }
146  }
147 
152  public void Disable( Control c )
153  {
154  Disable( l => l.Control.Equals( c ) );
155  }
156 
161  public void Enable( Control c )
162  {
163  Enable( l => l.Control.Equals( c ) );
164  }
165 
169  public void EnableAll()
170  {
171  Enable( x => true );
172  }
173 
177  public void DisableAll()
178  {
179  Disable( x => true );
180  }
181 
182  public IEnumerable<string> GetHelpTexts()
183  {
184  foreach ( var l in listeners )
185  {
186  if ( l.ControlName != null && l.HelpText != null )
187  yield return String.Format( "{0} - {1}", l.ControlName, l.HelpText );
188  }
189  }
190  }
191 }
Jypeli.Listener
Ohjaintapahtumien kuuntelija.
Definition: Listener.cs:50
Jypeli.Game.Time
static Time Time
Peliaika. Sisältää tiedon siitä, kuinka kauan peliä on pelattu (Time.SinceStartOfGame) ja kuinka kaua...
Definition: Time.cs:25
Jypeli.Controls.Controller.GetHelpTexts
IEnumerable< string > GetHelpTexts()
Palauttaa asetettujen kuuntelijoiden ohjetekstit.
Jypeli.Controls.Controller.Update
void Update()
Lukee uuden tilan laitteelta ja päivittää sen nykyiseksi sekä laukaisee tapahtumia.
Jypeli.Controls.Controller.Clear
void Clear()
Poistaa kaikki kuuntelijat.
Jypeli.SynchronousList.Add
void Add(T item)
Definition: SynchronousList.cs:197
Jypeli.Controls.Controller.NeverTrigger
static readonly ChangePredicate< ControllerState > NeverTrigger
Definition: Controller.cs:68
Jypeli.Controls.Controller.DisableAll
void DisableAll()
Poistaa kaikki kontrollit käytöstä.
Definition: Controller.cs:177
Jypeli.Controls.Controller.PrevState
ControllerState PrevState
Viimeisin tila.
Definition: Controller.cs:76
Jypeli.Controls.Controller.GetState
abstract ControllerState GetState()
Lukee ja palauttaa laitteen viimeisimmän tilan.
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.Controls.Controller.CurrentState
ControllerState CurrentState
Nykyinen tila.
Definition: Controller.cs:81
Jypeli.Controls.Controller.AddListener
Listener AddListener(ChangePredicate< ControllerState > rule, Control control, string controlName, string helpText, Delegate handler, params object[] args)
Definition: Controller.cs:102
Jypeli.Controls.Controller.Enable
void Enable(Control c)
Ottaa kontrollin takaisin käyttöön.
Definition: Controller.cs:161
Jypeli.SynchronousList.Clear
void Clear()
Definition: SynchronousList.cs:207
Jypeli.Controls
Definition: Controller.cs:34
Jypeli.Controls.Controller.Disable
void Disable(Predicate< Listener< ControllerState, Control >> predicate)
Poistaa tietyt kuuntelutapahtumat käytöstä.
Definition: Controller.cs:118
Jypeli.Controls.Controller.Enable
void Enable(Predicate< Listener< ControllerState, Control >> predicate)
Ottaa käytöstä poistetun kontrollin takaisin käyttöön.
Definition: Controller.cs:135
Jypeli.Controls.Controller.Disable
void Disable(Control c)
Poistaa kontrollin käytöstä.
Definition: Controller.cs:152
Jypeli.Controls.Controller.disabledListeners
SynchronousList< Listener< ControllerState, Control > > disabledListeners
Definition: Controller.cs:71
Jypeli.Controls.Controller.EnableAll
void EnableAll()
Ottaa takaisin käyttöön kaikki Disable-metodilla poistetut kontrollit.
Definition: Controller.cs:169
Jypeli.Controls.Controller.AlwaysTrigger
static readonly ChangePredicate< ControllerState > AlwaysTrigger
Definition: Controller.cs:66
System
Definition: CFFauxAttributes.cs:29
Jypeli.Controls.Controller.listeners
SynchronousList< Listener< ControllerState, Control > > listeners
Definition: Controller.cs:70
Jypeli.SynchronousList
Synkroninen lista, eli lista joka päivittyy vasta kun sen Update-metodia kutsutaan....
Definition: SynchronousList.cs:15
Jypeli.SynchronousList.ForEach
void ForEach(Action< T > action)
Suorittaa annetun toimenpiteen kaikille (nykyisille) listan alkioille.
Definition: SynchronousList.cs:320
Jypeli.Game
Definition: Content.cs:46
Jypeli.SynchronousList.Update
void Update(Time time)
Lisää ja poistaa jonossa olevat elementit sekä kutsuu niiden Update-metodia.
Definition: SynchronousList.cs:272
Jypeli.Controls.Controller
Ohjainlaite.
Definition: Controller.cs:39
Jypeli.Game.ControlContext
ListenContext ControlContext
Pelin pääohjainkonteksti.
Definition: Controls.cs:98