Jypeli 10
The simple game programming library
Meter.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2009 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
33namespace Jypeli
34{
38 public enum TriggerDirection
39 {
43 Up,
44
48 Down,
49
54 };
55
60 public abstract class Meter
61 {
65 public abstract double RelativeValue { get; set; }
66
67 internal abstract double GetValue();
68 internal abstract double GetMinValue();
69 internal abstract double GetMaxValue();
70
77 public Meter<T> OfType<T>() where T : struct, IComparable<T>, IEquatable<T>
78 {
79 Type meterType = this.GetType();
80
81#if WINDOWS_STOREAPP
82 Type[] genargs = meterType.GenericTypeArguments;
83#else
84 Type[] genargs = meterType.GetGenericArguments();
85#endif
86
87 if ( genargs.Length < 1 ) throw new ArgumentException( "This meter is not typed" );
88 if ( genargs[0] != typeof( T ) ) throw new ArgumentException( String.Format( "This meter measures {0}, not {1}", genargs[0].Name, typeof( T ).Name ) );
89
90 return (Meter<T>)this;
91 }
92 }
93
98 [Save]
99 public abstract class Meter<ValueType> : Meter where ValueType : struct, IComparable<ValueType>, IEquatable<ValueType>
100 {
101 private struct Trigger
102 {
103 public ValueType value;
105 public Action method;
106
107 public Trigger(ValueType value, TriggerDirection direction, Action method)
108 {
109 this.value = value;
110 this.direction = direction;
111 this.method = method;
112 }
113 }
114
115 private bool valueSet;
116
117 private ValueType val;
118 private ValueType minval;
119 private ValueType maxval;
120 private ValueType defval;
121
122 private List<Trigger> triggers;
123
127 [Save]
128 public ValueType Value
129 {
130 get { return val; }
131 set
132 {
133 if ( value.Equals( val ) )
134 return;
135
136 ValueType oldval = val;
137 valueSet = true;
138
139 if (value.CompareTo(minval) < 0) value = minval;
140 if (value.CompareTo(maxval) > 0) value = maxval;
141
142 val = value;
143 OnChange(oldval, value);
144 CheckLimits( oldval, value );
145 CheckTriggers(oldval, value);
146 }
147 }
148
152 public ValueType DefaultValue
153 {
154 get { return defval; }
155 set
156 {
157 defval = clampValue( value, minval, maxval );
158 if ( !valueSet ) Value = value;
159 }
160 }
161
166 public ValueType MinValue
167 {
168 get { return minval; }
169 set { minval = value; updateBounds(); }
170 }
171
176 public ValueType MaxValue
177 {
178 get { return maxval; }
179 set { maxval = value; updateBounds(); }
180 }
181
182 #region Events
183
187 public delegate void ChangeHandler( ValueType oldValue, ValueType newValue );
188
192 public event ChangeHandler Changed;
193
197 public event Action LowerLimit;
198
202 public event Action UpperLimit;
203
204
205 private void OnChange( ValueType oldValue, ValueType newValue )
206 {
207 if ( Changed != null )
208 Changed( oldValue, newValue );
209 }
210
211 private void CheckLimits( ValueType oldValue, ValueType newValue )
212 {
213 if ( LowerLimit != null && newValue.CompareTo( minval ) <= 0 )
214 {
215 LowerLimit();
216 return;
217 }
218
219 if ( UpperLimit != null && newValue.CompareTo( maxval ) >= 0 )
220 {
221 UpperLimit();
222 return;
223 }
224 }
225
226 private void CheckTriggers( ValueType oldValue, ValueType newValue )
227 {
228 if ( triggers == null ) return;
229
230 foreach ( Trigger t in triggers )
231 {
232 if ( t.direction != TriggerDirection.Down && oldValue.CompareTo( t.value ) < 0 && newValue.CompareTo( t.value ) >= 0 )
233 t.method();
234
235 else if ( t.direction != TriggerDirection.Up && oldValue.CompareTo( t.value ) > 0 && newValue.CompareTo( t.value ) <= 0 )
236 t.method();
237 }
238 }
239
240 #endregion
241
248 public Meter( ValueType defaultVal, ValueType minVal, ValueType maxVal )
249 {
250 minval = minVal;
251 maxval = maxVal;
252 defval = defaultVal;
253 val = defaultVal;
254 updateBounds();
255 }
256
262 {
263 minval = src.minval;
264 maxval = src.maxval;
265 defval = src.defval;
266 val = src.val;
267 updateBounds();
268 }
269
273 public void Reset()
274 {
276 }
277
283 public void SetValue( ValueType value )
284 {
285 Value = value;
286 }
287
294 public void AddTrigger( ValueType value, TriggerDirection direction, Action method )
295 {
296 if ( triggers == null ) triggers = new List<Trigger>();
297 triggers.Add( new Trigger( value, direction, method ) );
298 }
299
306 public void AddTrigger( ValueType value, TriggerDirection direction, Action<ValueType> method )
307 {
308 AddTrigger( value, direction, delegate() { method( this.Value ); } );
309 }
310
315 public void RemoveTriggers( ValueType value )
316 {
317 if ( triggers == null ) return;
318 triggers.RemoveAll( t => t.value.Equals( value ) );
319 }
320
325 public void RemoveTriggers( Action method )
326 {
327 if ( triggers == null ) return;
328 triggers.RemoveAll( t => t.method == method );
329 }
330
334 public void ClearTriggers()
335 {
336 if ( triggers == null ) return;
337 triggers.Clear();
338 }
339
340 private static ValueType clampValue( ValueType v, ValueType min, ValueType max )
341 {
342 if ( v.CompareTo( min ) < 0 )
343 return min;
344
345 if ( v.CompareTo( max ) > 0 )
346 return max;
347
348 return v;
349 }
350
351 private static void clampValue( ref ValueType v, ValueType min, ValueType max )
352 {
353 if ( v.CompareTo( min ) < 0 )
354 v = min;
355
356 else if ( v.CompareTo( max ) > 0 )
357 v = max;
358 }
359
360 private void updateBounds()
361 {
362 clampValue( ref val, minval, maxval );
364 }
365
369 public override String ToString()
370 {
371 return Value.ToString();
372 }
373 }
374}
Mittari, joka mittaa erityyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGauge.
Definition: Meter.cs:100
ValueType maxval
Definition: Meter.cs:119
ValueType minval
Definition: Meter.cs:118
static void clampValue(ref ValueType v, ValueType min, ValueType max)
Definition: Meter.cs:351
bool valueSet
Definition: Meter.cs:115
ChangeHandler Changed
Tapahtuu, kun mittarin arvo muuttuu.
Definition: Meter.cs:192
void CheckLimits(ValueType oldValue, ValueType newValue)
Definition: Meter.cs:211
void CheckTriggers(ValueType oldValue, ValueType newValue)
Definition: Meter.cs:226
void Reset()
Palauttaa mittarin arvon oletusarvoonsa.
Definition: Meter.cs:273
void updateBounds()
Definition: Meter.cs:360
ValueType val
Definition: Meter.cs:117
ValueType MaxValue
Mittarin suurin sallittu arvo. Kun mittari saavuttaa tämän arvon, laukeaa tapahtuma UpperLimit.
Definition: Meter.cs:177
static ValueType clampValue(ValueType v, ValueType min, ValueType max)
Definition: Meter.cs:340
void OnChange(ValueType oldValue, ValueType newValue)
Definition: Meter.cs:205
abstract double RelativeValue
Mittarin suhteellinen arvo (minimi 0, maksimi 1)
Definition: Meter.cs:65
void SetValue(ValueType value)
Asettaa mittarille arvon. Sama kuin Value-ominaisuuteen sijoitus, mutta helpompi käyttää tapahtumakäs...
Definition: Meter.cs:283
void AddTrigger(ValueType value, TriggerDirection direction, Action method)
Lisää mittarille rajan, jonka yli mentäessä laukaistaan aliohjelma.
Definition: Meter.cs:294
Action LowerLimit
Tapahtuu, kun mittari saavuttaa pienimmän sallitun arvonsa.
Definition: Meter.cs:197
Meter(ValueType defaultVal, ValueType minVal, ValueType maxVal)
Luo uuden mittarin.
Definition: Meter.cs:248
void RemoveTriggers(Action method)
Poistaa kaikki raja-arvotapahtumat, jotka kutsuvat tiettyä aliohjelmaa.
Definition: Meter.cs:325
void AddTrigger(ValueType value, TriggerDirection direction, Action< ValueType > method)
Lisää mittarille rajan, jonka yli mentäessä laukaistaan aliohjelma.
Definition: Meter.cs:306
void RemoveTriggers(ValueType value)
Poistaa kaikki tietylle arvolle asetetut raja-arvotapahtumat.
Definition: Meter.cs:315
ValueType Value
Mittarin arvo.
Definition: Meter.cs:129
List< Trigger > triggers
Definition: Meter.cs:122
Meter< T > OfType< T >()
Palauttaa mittarin sen todellisessa muodossa, jotta sen kaikkiin jäseniin pääsee käsiksi.
Definition: Meter.cs:77
ValueType DefaultValue
Mittarin oletusarvo.
Definition: Meter.cs:153
abstract double GetValue()
override String ToString()
Palauttaa mittarin arvon merkkijonona.
Definition: Meter.cs:369
void ClearTriggers()
Poistaa kaikki raja-arvotapahtumat.
Definition: Meter.cs:334
ValueType defval
Definition: Meter.cs:120
Meter(Meter< ValueType > src)
Luo uuden mittarin kopiona parametrina annetusta.
Definition: Meter.cs:261
abstract double GetMaxValue()
delegate void ChangeHandler(ValueType oldValue, ValueType newValue)
Mittarin muutostapahtumankäsittelijä.
ValueType MinValue
Mittarin pienin sallittu arvo. Kun mittari saavuttaa tämän arvon, laukeaa tapahtuma LowerLimit.
Definition: Meter.cs:167
Action UpperLimit
Tapahtuu, kun mittari saavuttaa suurimman sallitun arvonsa.
Definition: Meter.cs:202
abstract double GetMinValue()
@ Irrelevant
Ei väliä.
@ Down
Kallistetaan alas.
@ Up
Kallistetaan ylös.
TriggerDirection
Suunta mittarin muutokselle.
Definition: Meter.cs:39
ValueType value
Definition: Meter.cs:103
TriggerDirection direction
Definition: Meter.cs:104
Trigger(ValueType value, TriggerDirection direction, Action method)
Definition: Meter.cs:107