-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCompositeAppState.java
240 lines (213 loc) · 7.9 KB
/
CompositeAppState.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* $Id$
*
* Copyright (c) 2015, Simsilica, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.simsilica.state;
import com.jme3.app.Application;
import com.jme3.app.state.AppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.app.state.BaseAppState;
import com.jme3.util.SafeArrayList;
/**
* An AppState that manages a set of child app states, making sure
* they are attached/detached and optional enabled/disabled with the
* parent state.
*
* @author Paul Speed
*/
public class CompositeAppState extends BaseAppState {
private final SafeArrayList<AppStateEntry> states = new SafeArrayList<>(AppStateEntry.class);
private boolean childrenEnabled;
/**
* Since we manage attachmend/detachment possibly before
* initialization, we need to keep track of the stateManager we
* were given in stateAttached() in case we have to attach another
* child prior to initialization (but after we're attached).
* It's possible that we should actually be waiting for initialize
* to add these but I feel like there was some reason I did it this
* way originally. Past-me did not leave any clues.
*/
private AppStateManager stateManager;
private boolean attached;
public CompositeAppState( AppState... states ) {
for( AppState a : states ) {
this.states.add(new AppStateEntry(a, false));
}
}
private int indexOf( AppState state ) {
for( int i = 0; i < states.size(); i++ ) {
AppStateEntry e = states.get(i);
if( e.state == state ) {
return i;
}
}
return -1;
}
private AppStateEntry entry( AppState state ) {
for( AppStateEntry e : states.getArray() ) {
if( e.state == state ) {
return e;
}
}
return null;
}
protected <T extends AppState> T addChild( T state ) {
return addChild(state, false);
}
protected <T extends AppState> T addChild( T state, boolean overrideEnable ) {
if( indexOf(state) >= 0 ) {
return state;
}
states.add(new AppStateEntry(state, overrideEnable));
if( attached ) {
stateManager.attach(state);
}
return state;
}
protected void removeChild( AppState state ) {
int index = indexOf(state);
if( index < 0 ) {
return;
}
states.remove(index);
if( attached ) {
stateManager.detach(state);
}
}
protected <T extends AppState> T getChild( Class<T> stateType ) {
for( AppStateEntry e : states.getArray() ) {
if( stateType.isInstance(e.state) ) {
return stateType.cast(e.state);
}
}
return null;
}
protected void clearChildren() {
for( AppStateEntry e : states.getArray() ) {
removeChild(e.state);
}
}
@Override
public void stateAttached( AppStateManager stateManager ) {
this.stateManager = stateManager;
for( AppStateEntry e : states.getArray() ) {
stateManager.attach(e.state);
}
this.attached = true;
}
@Override
public void stateDetached( AppStateManager stateManager ) {
// Reverse order
for( int i = states.size() - 1; i >= 0; i-- ) {
stateManager.detach(states.get(i).state);
}
this.attached = false;
this.stateManager = null;
}
protected void setChildrenEnabled( boolean b ) {
if( childrenEnabled == b ) {
return;
}
childrenEnabled = b;
for( AppStateEntry e : states.getArray() ) {
e.setEnabled(b);
}
}
/**
* Overrides the automatic synching of a child's enabled state.
* When override is true, a child will remember its old state when
* the parent's enabled state is false so that when the parent is
* re-enabled the child can resume its previous enabled state. This
* is useful for the cases where a child may want to be disabled
* independent of the parent... and then not automatically become
* enabled just because the parent does.
* Currently, the parent's disabled state always disables the children,
* too. Override is about remembering the child's state before that
* happened and restoring it when the 'family' is enabled again as a whole.
*/
public void setOverrideEnabled( AppState state, boolean override ) {
AppStateEntry e = entry(state);
if( e == null ) {
throw new IllegalArgumentException("State not managed:" + state);
}
if( override ) {
e.override = true;
} else {
e.override = false;
e.state.setEnabled(isEnabled());
}
}
@Override
protected void initialize(Application app) {
}
@Override
protected void cleanup(Application app) {
}
@Override
protected void onEnable() {
setChildrenEnabled(true);
}
@Override
protected void onDisable() {
setChildrenEnabled(false);
}
private class AppStateEntry {
AppState state;
boolean enabled;
boolean override;
public AppStateEntry( AppState state, boolean overrideEnable ) {
this.state = state;
this.override = overrideEnable;
this.enabled = state.isEnabled();
}
public void setEnabled( boolean b ) {
if( override ) {
if( b ) {
// Set it to whatever its enabled state
// was before going disabled last time.
state.setEnabled(enabled);
} else {
// We are going to set enabled to false
// but keep track of what it was before we did
// that
this.enabled = state.isEnabled();
state.setEnabled(false);
}
} else {
// Just synch it always
state.setEnabled(b);
}
}
}
}