@ -50,11 +50,21 @@ public class AppStateManager {
private final ArrayList < AppState > states = new ArrayList < AppState > ( ) ;
private final ArrayList < AppState > states = new ArrayList < AppState > ( ) ;
private final Application app ;
private final Application app ;
private AppState [ ] stateArray ;
public AppStateManager ( Application app ) {
public AppStateManager ( Application app ) {
this . app = app ;
this . app = app ;
}
}
protected AppState [ ] getArray ( ) {
synchronized ( states ) {
if ( stateArray = = null ) {
stateArray = states . toArray ( new AppState [ states . size ( ) ] ) ;
}
return stateArray ;
}
}
/ * *
/ * *
* Attach a state to the AppStateManager , the same state cannot be attached
* Attach a state to the AppStateManager , the same state cannot be attached
* twice .
* twice .
@ -68,6 +78,7 @@ public class AppStateManager {
if ( ! states . contains ( state ) ) {
if ( ! states . contains ( state ) ) {
state . stateAttached ( this ) ;
state . stateAttached ( this ) ;
states . add ( state ) ;
states . add ( state ) ;
stateArray = null ;
return true ;
return true ;
} else {
} else {
return false ;
return false ;
@ -87,6 +98,7 @@ public class AppStateManager {
if ( states . contains ( state ) ) {
if ( states . contains ( state ) ) {
state . stateDetached ( this ) ;
state . stateDetached ( this ) ;
states . remove ( state ) ;
states . remove ( state ) ;
stateArray = null ;
return true ;
return true ;
} else {
} else {
return false ;
return false ;
@ -132,18 +144,15 @@ public class AppStateManager {
* @param tpf Time per frame .
* @param tpf Time per frame .
* /
* /
public void update ( float tpf ) {
public void update ( float tpf ) {
synchronized ( states ) {
AppState [ ] array = getArray ( ) ;
int num = states . size ( ) ;
for ( AppState state : array ) {
for ( int i = 0 ; i < num ; i + + ) {
if ( ! state . isInitialized ( ) )
AppState state = states . get ( i ) ;
state . initialize ( this , app ) ;
if ( ! state . isInitialized ( ) )
state . initialize ( this , app ) ;
if ( state . isEnabled ( ) ) {
if ( state . isEnabled ( ) ) {
state . update ( tpf ) ;
state . update ( tpf ) ;
}
}
}
}
}
}
}
/ * *
/ * *
@ -151,16 +160,13 @@ public class AppStateManager {
* @param rm The RenderManager
* @param rm The RenderManager
* /
* /
public void render ( RenderManager rm ) {
public void render ( RenderManager rm ) {
synchronized ( states ) {
AppState [ ] array = getArray ( ) ;
int num = states . size ( ) ;
for ( AppState state : array ) {
for ( int i = 0 ; i < num ; i + + ) {
if ( ! state . isInitialized ( ) )
AppState state = states . get ( i ) ;
state . initialize ( this , app ) ;
if ( ! state . isInitialized ( ) )
state . initialize ( this , app ) ;
if ( state . isEnabled ( ) ) {
if ( state . isEnabled ( ) ) {
state . render ( rm ) ;
state . render ( rm ) ;
}
}
}
}
}
}
}
@ -170,16 +176,13 @@ public class AppStateManager {
* @param rm The RenderManager
* @param rm The RenderManager
* /
* /
public void postRender ( ) {
public void postRender ( ) {
synchronized ( states ) {
AppState [ ] array = getArray ( ) ;
int num = states . size ( ) ;
for ( AppState state : array ) {
for ( int i = 0 ; i < num ; i + + ) {
if ( ! state . isInitialized ( ) )
AppState state = states . get ( i ) ;
state . initialize ( this , app ) ;
if ( ! state . isInitialized ( ) )
state . initialize ( this , app ) ;
if ( state . isEnabled ( ) ) {
if ( state . isEnabled ( ) ) {
state . postRender ( ) ;
state . postRender ( ) ;
}
}
}
}
}
}
}
@ -188,11 +191,9 @@ public class AppStateManager {
* Calls cleanup on attached states , do not call directly .
* Calls cleanup on attached states , do not call directly .
* /
* /
public void cleanup ( ) {
public void cleanup ( ) {
synchronized ( states ) {
AppState [ ] array = getArray ( ) ;
for ( int i = 0 ; i < states . size ( ) ; i + + ) {
for ( AppState state : array ) {
AppState state = states . get ( i ) ;
state . cleanup ( ) ;
state . cleanup ( ) ;
}
}
}
}
}
}
}