Jypeli 10
The simple game programming library
Listener.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.Reflection;
32using Jypeli.Controls;
33
34namespace Jypeli
35{
44 public delegate bool ChangePredicate<T>( T prev, T curr );
45
49 public interface Listener : Destroyable
50 {
56
64 }
65
71 public class Listener<State, Ctrl> : Listener
72 {
73 private ChangePredicate<State> isTriggered;
74 private Delegate handler;
75 private object[] handlerParams;
76 private bool isDestroyed;
77
78 private string _controlName;
79 private string _helpText;
80
81 private bool dynamicContext;
84
90 {
92 }
93
98 public Ctrl Control { get; private set; }
99
103 public string ControlName
104 {
105 get { return _controlName; }
106 set { _controlName = value; }
107 }
108
112 public string HelpText
113 {
114 get { return _helpText; }
115 set { _helpText = value; }
116 }
117
118 public Listener( ChangePredicate<State> triggerRule, ListenContext context, Ctrl ctrl, string controlName, string helpText, Delegate handler, params object[] args )
119 {
120 this.isDestroyed = false;
121 this.isTriggered = triggerRule;
122 this.handler = handler;
123 this.Control = ctrl;
124 this._controlName = controlName;
125 this._helpText = helpText;
126 this.handlerParams = args;
127 this.Destroyed = null;
128 this.dynamicContext = false;
129 this.context = context;
130 this.contextedObject = null;
131 }
132
133 public Listener( ChangePredicate<State> triggerRule, ControlContexted contexted, Ctrl ctrl, string controlName, string helpText, Delegate handler, params object[] args )
134 {
135 this.isDestroyed = false;
136 this.isTriggered = triggerRule;
137 this.handler = handler;
138 this.Control = ctrl;
139 this._controlName = controlName;
140 this._helpText = helpText;
141 this.handlerParams = args;
142 this.Destroyed = null;
143 this.dynamicContext = true;
144 this.context = null;
145 this.contextedObject = contexted;
146 }
147
148 public void Invoke()
149 {
150#if WINDOWS_STOREAPP
151 // Win8
152 MethodInfo handlerMethod = handler.GetMethodInfo();
153#else
154 MethodInfo handlerMethod = handler.Method;
155#endif
156 handlerMethod.Invoke( handler.Target, handlerParams );
157 }
158
159 public void CheckAndInvoke( State oldState, State newState )
160 {
161 if ( !IsDestroyed && Context != null && !Context.IsDestroyed && Context.Active && isTriggered( oldState, newState ) )
162 Invoke();
163 }
164
170 {
171 this.dynamicContext = false;
172 this.context = context;
173 return this;
174 }
175
183 {
184 this.dynamicContext = true;
185 this.contextedObject = obj;
186 return this;
187 }
188
189 #region Destroyable Members
190
195 public bool IsDestroyed
196 {
197 get { return isDestroyed; }
198 }
199
203 public void Destroy()
204 {
205 isDestroyed = true;
206 OnDestroyed();
207 }
208
212 public event Action Destroyed;
213
214 private void OnDestroyed()
215 {
216 if ( Destroyed != null )
217 Destroyed();
218 }
219
220 #endregion
221 }
222}
Kuuntelukonteksti ohjaimia varten
bool Active
Onko tämä konteksti tällä hetkellä aktiivinen
bool IsDestroyed
Onko konteksti tuhottu
Rajapinta olioille, jotka ovat tuhottavissa.
Definition: Destroyable.cs:9
Ohjaintapahtumien kuuntelija.
Definition: Listener.cs:72
void CheckAndInvoke(State oldState, State newState)
Definition: Listener.cs:159
void Invoke()
Definition: Listener.cs:148
Listener(ChangePredicate< State > triggerRule, ControlContexted contexted, Ctrl ctrl, string controlName, string helpText, Delegate handler, params object[] args)
Definition: Listener.cs:133
bool IsDestroyed
Onko olio tuhottu.
Definition: Listener.cs:196
string _controlName
Definition: Listener.cs:78
bool isDestroyed
Definition: Listener.cs:76
Ctrl Control
Kontrolli, jota kuunnellaan.
Definition: Listener.cs:98
ListenContext context
Definition: Listener.cs:82
Delegate handler
Definition: Listener.cs:74
object[] handlerParams
Definition: Listener.cs:75
Listener InContext(ControlContexted obj)
Kuuntelee tapahtumaa vain tietyssä kontekstissa. Esim. Keyboard.Listen(parametrit)....
string _helpText
Definition: Listener.cs:79
Listener(ChangePredicate< State > triggerRule, ListenContext context, Ctrl ctrl, string controlName, string helpText, Delegate handler, params object[] args)
Definition: Listener.cs:118
Action Destroyed
Tapahtuu, kun olio tuhotaan.
Definition: Listener.cs:212
Listener InContext(ListenContext context)
Kuuntelee tapahtumaa vain tietyssä kontekstissa.
ListenContext? Context
Konteksti, jossa kontrolleja kuunnellaan.
Definition: Listener.cs:90
string HelpText
Ohjeteksti.
Definition: Listener.cs:113
ControlContexted contextedObject
Definition: Listener.cs:83
void Destroy()
Tuhoaa kuuntelijan
Definition: Listener.cs:203
bool dynamicContext
Definition: Listener.cs:81
void OnDestroyed()
Definition: Listener.cs:214
string ControlName
Kontrollin nimi jota kuunnellaan. Käytetään vain ohjeen yhteydessä.
Definition: Listener.cs:104
ChangePredicate< State > isTriggered
Definition: Listener.cs:73
delegate bool ChangePredicate< T >(T prev, T curr)
Metodityyppi, joka ottaa parametrikseen entisen ja nykyisen muuttujan arvon ja palauttaa totuusarvon....