Jypeli  5
The simple game programming library
ListenContext.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 
6 namespace Jypeli.Controls
7 {
8  public class ListenContext : Destroyable
9  {
10  internal static ListenContext Null = new ListenContext() { Active = false };
11 
12  private bool _active = false;
13 
14  public bool Active
15  {
16  //get { return _active && !destroyed; }
17  get
18  {
19  if ( destroyed ) return false;
20  if ( Parent != null && !Parent.Active ) return false;
21  return _active;
22  }
23 
24  set
25  {
26  if ( value != _active )
27  {
28  _active = value;
29  if ( value && Activated != null ) Activated();
30  if ( !value && Deactivated != null ) Deactivated();
31  }
32  }
33  }
34 
35  internal bool dynamicParent = false;
36  internal ListenContext parentContext = null;
37  internal ControlContexted parentObject = null;
38 
39  public ListenContext Parent
40  {
41  get
42  {
43  if ( dynamicParent && parentObject == null ) return null;
44  return ( dynamicParent ? parentObject.ControlContext : parentContext );
45  }
46  }
47 
48  private Stack<bool> savedStates = null;
49 
53  public event Action Activated;
54 
58  public event Action Deactivated;
59 
60  public ListenContext()
61  {
62  }
63 
64  private ListenContext( ListenContext parent )
65  {
66  this.parentContext = parent;
67  }
68 
69  private ListenContext( ControlContexted parentObj )
70  {
71  this.dynamicParent = true;
72  this.parentObject = parentObj;
73  }
74 
76  {
77  return new ListenContext( this );
78  }
79 
80  internal void SaveFocus()
81  {
82  if ( savedStates == null ) savedStates = new Stack<bool>();
83  savedStates.Push( Active );
84  }
85 
86  internal void RestoreFocus()
87  {
88  if ( savedStates == null || savedStates.Count == 0 ) return;
89  Active = savedStates.Pop();
90  }
91 
92  public void Enable() { Active = true; }
93  public void Disable() { Active = false; }
94 
95  #region Destroyable Members
96 
97  bool destroyed = false;
98 
99  public bool IsDestroyed
100  {
101  get { return destroyed; }
102  }
103 
104  public event Action Destroyed;
105 
106  public void Destroy()
107  {
108  destroyed = true;
109  savedStates = null;
110  if ( Destroyed != null ) Destroyed();
111  }
112 
113  #endregion
114  }
115 
116  public interface ControlContexted
117  {
118  ListenContext ControlContext { get; }
119  bool IsModal { get; }
120  }
121 }
ListenContext CreateSubcontext()
Action Deactivated
Tapahtuu kun konteksti passivoidaan.
Action Activated
Tapahtuu kun konteksti aktivoidaan.
Rajapinta olioille, jotka ovat tuhottavissa.
Definition: Destroyable.cs:8