Guest User

Untitled

a guest
May 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.74 KB | None | 0 0
  1. package niftyworkarounds;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.newdawn.slick.GameContainer;
  7. import org.newdawn.slick.Graphics;
  8. import org.newdawn.slick.Input;
  9. import org.newdawn.slick.SlickException;
  10. import org.newdawn.slick.state.BasicGameState;
  11. import org.newdawn.slick.state.StateBasedGame;
  12.  
  13. import de.lessvoid.nifty.Nifty;
  14. import de.lessvoid.nifty.slick2d.input.SlickInputSystem;
  15. import de.lessvoid.nifty.slick2d.input.SlickSlickInputSystem;
  16. import de.lessvoid.nifty.slick2d.render.SlickRenderDevice;
  17. import de.lessvoid.nifty.slick2d.sound.SlickSoundDevice;
  18. import de.lessvoid.nifty.slick2d.time.LWJGLTimeProvider;
  19. import de.lessvoid.nifty.spi.time.TimeProvider;
  20.  
  21. public abstract class NiftyOverlayBasicGameStateImproved extends BasicGameState {
  22.  
  23.     /**
  24.      *  Used to determine what is rendered first.
  25.      *
  26.      *  NiftyFirst: First Nifty and then the application
  27.      *  NiftyLast: Frist the application and then Nifty
  28.      */
  29.  
  30.     public enum RenderOrder {
  31.         NiftyFirst, NiftyLast
  32.     }
  33.  
  34.     /**
  35.      *  Used to determine what is updated first.
  36.      *
  37.      *  NiftyFirst: First Nifty and then the application
  38.      *  NiftyLast: Frist the application and then Nifty
  39.      */
  40.  
  41.     public enum UpdateOrder {
  42.         NiftyFirst, NiftyLast
  43.     }
  44.  
  45.     /**
  46.      *  Holds the mapping for the enum and its renderer object
  47.      */
  48.  
  49.     private final Map<RenderOrder, OrderedRenderer> renderOrderMap = new HashMap<RenderOrder, OrderedRenderer>();
  50.  
  51.     /**
  52.      *  Holds the mapping for the enum and its updater object
  53.      */
  54.  
  55.     private final Map<UpdateOrder, OrderedUpdater> updateOrderMap = new HashMap<UpdateOrder, OrderedUpdater>();
  56.  
  57.     /**
  58.      *  Current rendering order
  59.      */
  60.  
  61.     private RenderOrder renderOrder = RenderOrder.NiftyLast;
  62.  
  63.     /**
  64.      *  Current updating order
  65.      */
  66.  
  67.     private UpdateOrder updateOrder = UpdateOrder.NiftyLast;
  68.  
  69.     /**
  70.      * The used input system.
  71.      */
  72.     private SlickInputSystem inputSystem;
  73.  
  74.     /**
  75.      * The one and only Nifty GUI.
  76.      */
  77.     private Nifty niftyGUI;
  78.  
  79.     public NiftyOverlayBasicGameStateImproved() {
  80.         initializeInstance();
  81.     }
  82.  
  83.     /**
  84.      * Used to initialize the Maps
  85.      */
  86.  
  87.     private void initializeInstance() {
  88.         updateOrderMap.put(UpdateOrder.NiftyFirst, new UpdateNiftyFirst());
  89.         updateOrderMap.put(UpdateOrder.NiftyLast, new UpdateNiftyLast());
  90.         renderOrderMap.put(RenderOrder.NiftyFirst, new RenderNiftyFirst());
  91.         renderOrderMap.put(RenderOrder.NiftyLast, new RenderNiftyLast());
  92.     }
  93.  
  94.     /**
  95.      * Sets the current rendering order
  96.      *
  97.      * @param renderOrder
  98.      *          the new RenderOrder value
  99.      */
  100.  
  101.     public void setRenderOrder(final RenderOrder renderOrder) {
  102.         this.renderOrder = renderOrder;
  103.     }
  104.  
  105.     /**
  106.      * Sets the current updating order
  107.      *
  108.      * @param updateOrder
  109.      *          the new UpdateOrder value
  110.      */
  111.  
  112.     public void setUpdateOrder(final UpdateOrder updateOrder) {
  113.         this.updateOrder = updateOrder;
  114.     }
  115.  
  116.     /**
  117.      * Gets the current RenderOrder
  118.      *
  119.      * @return the current RenderOrder value
  120.      */
  121.  
  122.     public RenderOrder getRenderOrder() {
  123.         return renderOrder;
  124.     }
  125.  
  126.     /**
  127.      * Gets the current UpdateOrder
  128.      *
  129.      * @return the current UpdateOrder value
  130.      */
  131.  
  132.     public UpdateOrder getUpdateOrder() {
  133.         return updateOrder;
  134.     }
  135.  
  136.     /**
  137.      * Enter the game state.
  138.      */
  139.     @Override
  140.     public final void enter(final GameContainer container, final StateBasedGame game) throws SlickException {
  141.         final Input input = container.getInput();
  142.         input.removeListener(inputSystem);
  143.         input.addListener(inputSystem);
  144.  
  145.         enterState(container, game);
  146.     }
  147.  
  148.     /**
  149.      * Enter the game state. This function is called during the default
  150.      * {@link #enter(GameContainer, StateBasedGame)} function call.
  151.      *
  152.      * @param container
  153.      *          the container that displays the game
  154.      * @param game
  155.      *          the state based game this state is a part of
  156.      * @throws SlickException
  157.      *           in case entering the state fails
  158.      */
  159.     protected abstract void enterState(final GameContainer container, final StateBasedGame game) throws SlickException;
  160.  
  161.     /**
  162.      * Get the instance of the NiftyGUI that is used to render this screen.
  163.      *
  164.      * @return the instance of the NiftyGUI
  165.      */
  166.     public final Nifty getNifty() {
  167.         return niftyGUI;
  168.     }
  169.  
  170.     /**
  171.      * Initialize the game and the GUI.
  172.      */
  173.     @Override
  174.     public final void init(final GameContainer container, final StateBasedGame game) throws SlickException {
  175.         initGameAndGUI(container, game);
  176.  
  177.         if (niftyGUI == null) {
  178.             throw new IllegalStateException("NiftyGUI was not initialized.");
  179.         }
  180.  
  181.         container.getInput().removeListener(game);
  182.     }
  183.  
  184.     /**
  185.      * Initialize the game. This function is called during
  186.      * {@link #init(GameContainer)}. During this call its needed to initialize the
  187.      * Nifty GUI with own options by calling
  188.      * {@link #initNifty(GameContainer, SlickRenderDevice, SlickSoundDevice, TimeProvider)}
  189.      * .
  190.      *
  191.      * @param container
  192.      *          the game container that displays the game
  193.      * @throws SlickException
  194.      *           in case initializing the game goes wrong
  195.      */
  196.     protected abstract void initGameAndGUI(final GameContainer container, StateBasedGame game) throws SlickException;
  197.  
  198.     /**
  199.      * Initialize the Nifty GUI for this game.
  200.      *
  201.      * @param container
  202.      *          the container used to display the game
  203.      * @param game
  204.      *          the state based game this state is part of
  205.      * @param renderDevice
  206.      *          the render device that is supposed to be used to render the GUI
  207.      * @param soundDevice
  208.      *          the sound device that is supposed to be used
  209.      * @param inputSystem
  210.      *          the input system that is supposed to be used
  211.      * @param timeProvider
  212.      *          the time provider that is supposed to be used
  213.      * @throws IllegalStateException
  214.      *           in case this function was called before
  215.      */
  216.     protected final void initNifty(
  217.             final GameContainer container,
  218.             final StateBasedGame game,
  219.             final SlickRenderDevice renderDevice,
  220.             final SlickSoundDevice soundDevice,
  221.             final SlickInputSystem inputSystem,
  222.             final TimeProvider timeProvider) {
  223.         if (niftyGUI != null) {
  224.             throw new IllegalStateException("The NiftyGUI was already initialized. Its illegal to do so twice.");
  225.         }
  226.  
  227.         inputSystem.setInput(container.getInput());
  228.  
  229.         niftyGUI = new Nifty(renderDevice, soundDevice, inputSystem, timeProvider);
  230.  
  231.         this.inputSystem = inputSystem;
  232.  
  233.         prepareNifty(niftyGUI, game);
  234.     }
  235.  
  236.     /**
  237.      * Initialize the Nifty GUI for this game. This function will use the default
  238.      * {@link de.lessvoid.nifty.tools.TimeProvider}.
  239.      *
  240.      * @param container
  241.      *          the container used to display the game
  242.      * @param game
  243.      *          the state based game this state is part of
  244.      * @param renderDevice
  245.      *          the render device that is supposed to be used to render the GUI
  246.      * @param soundDevice
  247.      *          the sound device that is supposed to be used
  248.      * @param inputSystem
  249.      *          the input system that is supposed to be used
  250.      * @throws IllegalStateException
  251.      *           in case this function was called before
  252.      */
  253.     protected final void initNifty(
  254.             final GameContainer container,
  255.             final StateBasedGame game,
  256.             final SlickRenderDevice renderDevice,
  257.             final SlickSoundDevice soundDevice,
  258.             final SlickInputSystem inputSystem) {
  259.         initNifty(container, game, renderDevice, soundDevice, inputSystem, new LWJGLTimeProvider());
  260.     }
  261.  
  262.     /**
  263.      * Initialize the Nifty GUI for this game. This function will use the default
  264.      * {@link de.lessvoid.nifty.tools.TimeProvider}. Also it will use the render
  265.      * and sound devices that are provided with this library.
  266.      *
  267.      * @param container
  268.      *          the container used to display the game
  269.      * @param game
  270.      *          the state based game this state is part of
  271.      * @param inputSystem
  272.      *          the input system that is supposed to be used
  273.      * @throws IllegalStateException
  274.      *           in case this function was called before
  275.      * @see de.lessvoid.nifty.slick2d.render.SlickRenderDevice
  276.      * @see de.lessvoid.nifty.slick2d.sound.SlickSoundDevice
  277.      */
  278.     protected final void initNifty(
  279.             final GameContainer container,
  280.             final StateBasedGame game,
  281.             final SlickInputSystem inputSystem) {
  282.         initNifty(container, game, new SlickRenderDevice(container), new SlickSoundDevice(), inputSystem);
  283.     }
  284.  
  285.     /**
  286.      * Initialize the Nifty GUI for this game. This function will use the default
  287.      * {@link de.lessvoid.nifty.tools.TimeProvider}. Also it will use the render
  288.      * and sound devices that are provided with this library. As for the input it
  289.      * will forward all input to the Slick {@link org.newdawn.slick.InputListener}
  290.      * that is implemented in this class.
  291.      *
  292.      * @param container
  293.      *          the container used to display the game
  294.      * @param game
  295.      *          the state based game this state is part of
  296.      * @throws IllegalStateException
  297.      *           in case this function was called before
  298.      * @see de.lessvoid.nifty.slick2d.render.SlickRenderDevice
  299.      * @see de.lessvoid.nifty.slick2d.sound.SlickSoundDevice
  300.      * @see de.lessvoid.nifty.slick2d.input.SlickSlickInputSystem
  301.      */
  302.     protected final void initNifty(final GameContainer container, final StateBasedGame game) {
  303.         initNifty(container, game, new SlickSlickInputSystem(this));
  304.     }
  305.  
  306.     /**
  307.      * Leave this game state.
  308.      */
  309.     @Override
  310.     public final void leave(final GameContainer container, final StateBasedGame game) throws SlickException {
  311.         final Input input = container.getInput();
  312.         input.removeListener(inputSystem);
  313.  
  314.         leaveState(container, game);
  315.     }
  316.  
  317.     /**
  318.      * Leave the game state. This function is called during the default
  319.      * {@link #leave(GameContainer, StateBasedGame)} function call.
  320.      *
  321.      * @param container
  322.      *          the container that displays the game
  323.      * @param game
  324.      *          the state based game this state is a part of
  325.      * @throws SlickException
  326.      *           in case entering the state fails
  327.      */
  328.     protected abstract void leaveState(final GameContainer container, final StateBasedGame game) throws SlickException;
  329.  
  330.     /**
  331.      * This function should be used to prepare the actual GUI and the controllers
  332.      * of the Nifty GUI. It is called right after the Nifty GUI got initialized.
  333.      *
  334.      * @param nifty
  335.      *          the Nifty GUI that got initialized
  336.      * @param game
  337.      *          the state based game this state is part of
  338.      */
  339.     protected abstract void prepareNifty(Nifty nifty, StateBasedGame game);
  340.  
  341.     /**
  342.      * Render the game.
  343.      */
  344.     @Override
  345.     public final void render(final GameContainer container, final StateBasedGame game, final Graphics g)
  346.             throws SlickException {
  347.         renderOrderMap.get(renderOrder).render(container, game, g);
  348.     }
  349.  
  350.     /**
  351.      * This function is supposed to be used to render the game. It is called
  352.      * during the call of the {@link #render(GameContainer, Graphics)} function.
  353.      *
  354.      * @param container
  355.      *          the container that displays the game
  356.      * @param g
  357.      *          the graphics instance that is used to draw the game
  358.      * @throws SlickException
  359.      *           in case anything goes wrong during the rendering
  360.      */
  361.     protected abstract void renderGame(GameContainer container, StateBasedGame game, Graphics g) throws SlickException;
  362.  
  363.     /**
  364.      * Update the game.
  365.      */
  366.     @Override
  367.     public final void update(final GameContainer container, final StateBasedGame game, final int delta)
  368.             throws SlickException {
  369.         updateOrderMap.get(updateOrder).update(container, game, delta);
  370.     }
  371.  
  372.     /**
  373.      * This function is supposed to be used to update the state of the game. It
  374.      * called during the call of the {@link #update(GameContainer, int)} function.
  375.      *
  376.      * @param container
  377.      *          the container that displays the game
  378.      * @param delta
  379.      *          the time since the last update
  380.      * @throws SlickException
  381.      *           in case anything goes wrong during the update
  382.      */
  383.     protected abstract void updateGame(GameContainer container, StateBasedGame game, int delta) throws SlickException;
  384.  
  385.     /**
  386.      * Only render Nifty
  387.      */
  388.     private void renderNifty() {
  389.         if (niftyGUI != null) {
  390.             niftyGUI.render(false);
  391.         }
  392.     }
  393.  
  394.     /**
  395.      * Only update Nifty
  396.      */
  397.     private void updateNifty() {
  398.         if (niftyGUI != null) {
  399.             niftyGUI.update();
  400.         }
  401.     }
  402.  
  403.     private abstract class OrderedRenderer {
  404.         public abstract void render(final GameContainer container, final StateBasedGame game, final Graphics g)
  405.                 throws SlickException;
  406.     }
  407.  
  408.     /**
  409.      * This class will render Nifty first and then the application
  410.      */
  411.     private class RenderNiftyFirst extends OrderedRenderer {
  412.  
  413.         @Override
  414.         public void render(final GameContainer container, final StateBasedGame game, final Graphics g)
  415.                 throws SlickException {
  416.             renderNifty();
  417.             renderGame(container, game, g);
  418.         }
  419.  
  420.     }
  421.  
  422.     /**
  423.      * This class will render the application first and then Nifty
  424.      */
  425.     private class RenderNiftyLast extends OrderedRenderer {
  426.  
  427.         @Override
  428.         public void render(final GameContainer container, final StateBasedGame game, final Graphics g)
  429.                 throws SlickException {
  430.             renderGame(container, game, g);
  431.             renderNifty();
  432.         }
  433.  
  434.     }
  435.  
  436.     private abstract class OrderedUpdater {
  437.         public abstract void update(final GameContainer container, final StateBasedGame game, final int delta)
  438.                 throws SlickException;
  439.     }
  440.  
  441.     /**
  442.      * This class will update Nifty first and then the application
  443.      */
  444.     private class UpdateNiftyFirst extends OrderedUpdater {
  445.  
  446.         @Override
  447.         public void update(final GameContainer container, final StateBasedGame game, final int delta)
  448.                 throws SlickException {
  449.             updateNifty();
  450.             updateGame(container, game, delta);
  451.         }
  452.  
  453.     }
  454.  
  455.     /**
  456.      * This class will update the application first and then Nifty
  457.      */
  458.     private class UpdateNiftyLast extends OrderedUpdater {
  459.  
  460.         @Override
  461.         public void update(final GameContainer container, final StateBasedGame game, final int delta)
  462.                 throws SlickException {
  463.             updateGame(container, game, delta);
  464.             updateNifty();
  465.         }
  466.  
  467.     }
  468. }
Add Comment
Please, Sign In to add comment