OptionMenu.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package pathgame.gameplay.menu;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import me.hammerle.snuviengine.api.KeyBinding;
  7. import me.hammerle.snuviengine.api.KeyHandler;
  8. import pathgame.gameplay.Keys;
  9. /**
  10. * A container for holding all options of the OptionMenu
  11. *
  12. * @author julia
  13. */
  14. public class OptionMenu extends BaseMenu
  15. {
  16. private final MenuButton[] options;
  17. /**
  18. * Contructor generating and initializing the OptionMenu
  19. *
  20. * @param id the id of the OptionMenu
  21. * @param mainId the id to the MainMenu
  22. */
  23. public OptionMenu(int id, int mainId)
  24. {
  25. super(id);
  26. int menuLength = Keys.KEYS.length;
  27. options = new MenuButton[menuLength + 1];
  28. for(int i = 0; i < menuLength; ++i)
  29. {
  30. options[i] = getButton(Keys.KEYNAMES[i], Keys.KEYS[i]);
  31. }
  32. options[menuLength] = new MenuButton("Back to Main Menu", (gamestate) ->
  33. {
  34. File f = new File("resources/config.bin");
  35. try(DataOutputStream writer = new DataOutputStream(new FileOutputStream(f)))
  36. {
  37. for(int i = 0; i < Keys.KEYS.length; ++i)
  38. {
  39. writer.writeInt(Keys.KEYS[i].getKey());
  40. }
  41. }
  42. catch(IOException ex)
  43. {
  44. }
  45. setReturnId(mainId);
  46. });
  47. }
  48. private MenuButton getButton(String name, KeyBinding key)
  49. {
  50. return new MenuButton(name, (gamestate) ->
  51. {
  52. KeyHandler.rebind(key);
  53. });
  54. }
  55. /**
  56. * Returns the options of the OptionMenu
  57. *
  58. * @return the options of the OptionMenu
  59. */
  60. @Override
  61. public MenuButton[] getOptions()
  62. {
  63. return options;
  64. }
  65. /**
  66. * Returns if this is the OptionMenu
  67. *
  68. * @return if this is the OptionMenu
  69. */
  70. @Override
  71. public boolean isOptionMenu()
  72. {
  73. return true;
  74. }
  75. }