-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateManager.cs
More file actions
60 lines (43 loc) · 1.22 KB
/
StateManager.cs
File metadata and controls
60 lines (43 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Bakera.RedFace{
public class StateManager<T> : KeyedByTypeCollection<T> where T : RedFaceParserState{
public T CurrentState{get; private set;}
public T PreviousState{get; private set;}
public void SetState<U>() where U : T, new(){
PreviousState = CurrentState;
CurrentState = GetState<U>();
}
public void SetState(T state){
if(!this.Contains(state)){
this.Add(state);
}
PreviousState = CurrentState;
CurrentState = state;
}
public T GetState<U>() where U : T, new(){
Type t = typeof(U);
if(!this.Contains(t)){
U state = new U();
state.ParserEventRaised += OnParserEventRaised;
this.Add(state);
}
return this[t];
}
// ひとつ前のステータスに戻します。
public void BackState(){
T temp = CurrentState;
CurrentState = PreviousState;
PreviousState = temp;
}
// イベント
public event EventHandler<ParserEventArgs> ParserEventRaised;
// Stateが発生させたイベントを伝達します。
protected virtual void OnParserEventRaised(Object sender, ParserEventArgs e){
if(ParserEventRaised != null){
ParserEventRaised(sender, e);
}
}
}
}