Jypeli 10
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
30using System;
31using System.Collections.Generic;
32
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 );
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
110 public void Clear()
111 {
112 listeners.Clear();
113 }
114
119 public void Disable( Predicate<Listener<ControllerState, Control>> predicate )
120 {
121 foreach ( var l in listeners )
122 {
123 if ( predicate( l ) )
124 {
125 // Note: synchronous list, does not actually change the list while iterating
126 listeners.Remove( l );
127 disabledListeners.Add( l );
128 }
129 }
130 }
131
136 public void Enable( Predicate<Listener<ControllerState, Control>> predicate )
137 {
138 foreach ( var l in disabledListeners )
139 {
140 if ( predicate( l ) )
141 {
142 // Note: synchronous list, does not actually change the list while iterating
143 listeners.Add( l );
144 disabledListeners.Remove( l );
145 }
146 }
147 }
148
153 public void Disable( Control c )
154 {
155 Disable( l => l.Control.Equals( c ) );
156 }
157
162 public void Enable( Control c )
163 {
164 Enable( l => l.Control.Equals( c ) );
165 }
166
170 public void EnableAll()
171 {
172 Enable( x => true );
173 }
174
178 public void DisableAll()
179 {
180 Disable( x => true );
181 }
182
183 public IEnumerable<string> GetHelpTexts()
184 {
185 foreach ( var l in listeners )
186 {
187 if ( l.ControlName != null && l.HelpText != null )
188 yield return String.Format( "{0} - {1}", l.ControlName, l.HelpText );
189 }
190 }
191 }
192}
ListenContext ControlContext
Pelin pääohjainkonteksti.
Definition: Controls.cs:98
static Time Time
Peliaika. Sisältää tiedon siitä, kuinka kauan peliä on pelattu (Time.SinceStartOfGame) ja kuinka kaua...
Definition: Time.cs:25
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:96
Synkroninen lista, eli lista joka päivittyy vasta kun sen Update-metodia kutsutaan....
void Enable(Predicate< Listener< ControllerState, Control > > predicate)
Ottaa käytöstä poistetun kontrollin takaisin käyttöön.
Definition: Controller.cs:136
ControllerState CurrentState
Nykyinen tila.
Definition: Controller.cs:81
void Enable(Control c)
Ottaa kontrollin takaisin käyttöön.
Definition: Controller.cs:162
ControllerState PrevState
Viimeisin tila.
Definition: Controller.cs:76
void DisableAll()
Poistaa kaikki kontrollit käytöstä.
Definition: Controller.cs:178
IEnumerable< string > GetHelpTexts()
Palauttaa asetettujen kuuntelijoiden ohjetekstit.
static readonly ChangePredicate< ControllerState > NeverTrigger
Definition: Controller.cs:68
void EnableAll()
Ottaa takaisin käyttöön kaikki Disable-metodilla poistetut kontrollit.
Definition: Controller.cs:170
SynchronousList< Listener< ControllerState, Control > > listeners
Definition: Controller.cs:70
void Clear()
Poistaa kaikki kuuntelijat.
Listener AddListener(ChangePredicate< ControllerState > rule, Control control, string controlName, string helpText, Delegate handler, params object[] args)
Definition: Controller.cs:102
void Disable(Predicate< Listener< ControllerState, Control > > predicate)
Poistaa tietyt kuuntelutapahtumat käytöstä.
Definition: Controller.cs:119
void Disable(Control c)
Poistaa kontrollin käytöstä.
Definition: Controller.cs:153
abstract ControllerState GetState()
Lukee ja palauttaa laitteen viimeisimmän tilan.
void Update()
Lukee uuden tilan laitteelta ja päivittää sen nykyiseksi sekä laukaisee tapahtumia.
static readonly ChangePredicate< ControllerState > AlwaysTrigger
Definition: Controller.cs:66
SynchronousList< Listener< ControllerState, Control > > disabledListeners
Definition: Controller.cs:71
Ohjaintapahtumien kuuntelija.
Definition: Listener.cs:72