Class BaseAppState

java.lang.Object
com.jme3.app.state.BaseAppState
All Implemented Interfaces:
AppState
Direct Known Subclasses:
ArmatureDebugAppState, AudioListenerState, BasicProfilerState, ConstantVerifierState, DetailedProfilerState, EnvironmentCamera, LightsDebugState

public abstract class BaseAppState extends Object implements AppState
A base app state implementation the provides more built-in management convenience than AbstractAppState, including methods for enable/disable/initialize state management. The abstract onEnable() and onDisable() methods are called appropriately during initialize(), cleanup(), or setEnabled() depending on the mutual state of "initialized" and "enabled".

initialize() and cleanup() can be used by subclasses to manage resources that should exist the entire time that the app state is attached. This is useful for resources that might be expensive to create or load.

onEnable()/onDisable() can be used for managing things that should only exist while the state is enabled. Prime examples would be scene graph attachment or input listener attachment.

The base class logic is such that onDisable() will always be called before cleanup() if the state is enabled. Likewise, enable() will always be called after initialize() if the state is enable(). onEnable()/onDisable() are also called appropriate when setEnabled() is called that changes the enabled state AND if the state is attached. In other words, onEnable()/onDisable() are only ever called on an already attached state.

It is technically safe to do all initialization and cleanup in the onEnable()/onDisable() methods. Choosing to use initialize() and cleanup() for this is a matter of performance specifics for the implementor.

  • Constructor Details

    • BaseAppState

      protected BaseAppState()
    • BaseAppState

      protected BaseAppState(String id)
  • Method Details

    • initialize

      protected abstract void initialize(Application app)
      Called during initialization once the app state is attached and before onEnable() is called.
      Parameters:
      app - the application
    • cleanup

      protected abstract void cleanup(Application app)
      Called after the app state is detached or during application shutdown if the state is still attached. onDisable() is called before this cleanup() method if the state is enabled at the time of cleanup.
      Parameters:
      app - the application
    • onEnable

      protected abstract void onEnable()
      Called when the state is fully enabled, ie: is attached and isEnabled() is true or when the setEnabled() status changes after the state is attached.
    • onDisable

      protected abstract void onDisable()
      Called when the state was previously enabled but is now disabled either because setEnabled(false) was called or the state is being cleaned up.
    • initialize

      public final void initialize(AppStateManager stateManager, Application app)
      Do not call directly: Called by the state manager to initialize this state post-attachment. This implementation calls initialize(app) and then onEnable() if the state is enabled.
      Specified by:
      initialize in interface AppState
      Parameters:
      stateManager - The state manager
      app - The application
    • isInitialized

      public final boolean isInitialized()
      Specified by:
      isInitialized in interface AppState
      Returns:
      True if initialize() was called on the state, false otherwise.
    • setId

      protected void setId(String id)
      Sets the unique ID of this app state. Note: that setting this while an app state is attached to the state manager will have no effect on ID-based lookups.
      Parameters:
      id - the desired ID
    • getId

      public String getId()
      Description copied from interface: AppState
      Returns the unique ID for this AppState or null if it has no unique ID.
      Specified by:
      getId in interface AppState
      Returns:
      the ID, or null if none
    • getApplication

      public final Application getApplication()
    • getStateManager

      public final AppStateManager getStateManager()
    • getState

      public final <T extends AppState> T getState(Class<T> type)
    • getState

      public final <T extends AppState> T getState(Class<T> type, boolean failOnMiss)
    • getState

      public final <T extends AppState> T getState(String id, Class<T> type)
    • getState

      public final <T extends AppState> T getState(String id, Class<T> type, boolean failOnMiss)
    • setEnabled

      public final void setEnabled(boolean enabled)
      Description copied from interface: AppState
      Enable or disable the functionality of the AppState. The effect of this call depends on implementation. An AppState starts as being enabled by default. A disabled AppStates does not get calls to AppState.update(float), AppState.render(RenderManager), or AppState.postRender() from its AppStateManager.
      Specified by:
      setEnabled in interface AppState
      Parameters:
      enabled - activate the AppState or not.
    • isEnabled

      public final boolean isEnabled()
      Specified by:
      isEnabled in interface AppState
      Returns:
      True if the AppState is enabled, false otherwise.
      See Also:
    • stateAttached

      public void stateAttached(AppStateManager stateManager)
      Description copied from interface: AppState
      Called by AppStateManager.attach(com.jme3.app.state.AppState) when transitioning this AppState from detached to initializing.

      There is no assumption about the thread from which this function is called, therefore it is unsafe to modify the scene graph from this method. Please use AppState.initialize(com.jme3.app.state.AppStateManager, com.jme3.app.Application) instead.

      Specified by:
      stateAttached in interface AppState
      Parameters:
      stateManager - State manager to which the state was attached to.
    • stateDetached

      public void stateDetached(AppStateManager stateManager)
      Description copied from interface: AppState
      Called by AppStateManager.detach(com.jme3.app.state.AppState) when transitioning this AppState from running to terminating.

      There is no assumption about the thread from which this function is called, therefore it is unsafe to modify the scene graph from this method. Please use AppState.cleanup() instead.

      Specified by:
      stateDetached in interface AppState
      Parameters:
      stateManager - The state manager from which the state was detached from.
    • update

      public void update(float tpf)
      Description copied from interface: AppState
      Called to update the AppState. This method will be called every render pass if the AppState is both attached and enabled.
      Specified by:
      update in interface AppState
      Parameters:
      tpf - Time since the last call to update(), in seconds.
    • render

      public void render(RenderManager rm)
      Description copied from interface: AppState
      Render the state. This method will be called every render pass if the AppState is both attached and enabled.
      Specified by:
      render in interface AppState
      Parameters:
      rm - RenderManager
    • postRender

      public void postRender()
      Description copied from interface: AppState
      Called after all rendering commands are flushed. This method will be called every render pass if the AppState is both attached and enabled.
      Specified by:
      postRender in interface AppState
    • cleanup

      public final void cleanup()
      Do not call directly: Called by the state manager to terminate this state post-detachment or during state manager termination. This implementation calls onDisable() if the state is enabled and then cleanup(app).
      Specified by:
      cleanup in interface AppState