LevelUpSpellPanel.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package org.leumasjaffe.charsheet.view.level;
  2. import javax.swing.JPanel;
  3. import org.jdesktop.swingx.VerticalLayout;
  4. import org.leumasjaffe.charsheet.model.magic.DDSpell;
  5. import org.leumasjaffe.charsheet.view.level.UpdateClassWithLevelPanel.BoolArray;
  6. import org.leumasjaffe.charsheet.view.magic.SelectSpellsPanel;
  7. import org.leumasjaffe.charsheet.view.magic.SelectSpellsPanel.Info;
  8. import org.leumasjaffe.observer.ObserverDispatch;
  9. import lombok.AccessLevel;
  10. import lombok.experimental.FieldDefaults;
  11. import lombok.experimental.NonFinal;
  12. import java.awt.GridBagLayout;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.Collections;
  16. import java.util.LinkedHashSet;
  17. import java.util.List;
  18. import javax.swing.JScrollPane;
  19. import java.awt.GridBagConstraints;
  20. @SuppressWarnings("serial")
  21. @FieldDefaults(level=AccessLevel.PUBLIC, makeFinal=true)
  22. class LevelUpSpellPanel extends JPanel {
  23. public static enum SpellPickType {
  24. LEARN {
  25. @Override
  26. public List<List<Integer>> getSpellCounts(SelectSpellsPanel.Info info) {
  27. return info.dclass.getProto().getSpells().get().getKnown();
  28. }
  29. @Override
  30. public Collection<DDSpell> getAvailableSpells(Info info, int i) {
  31. Collection<DDSpell> spells = new ArrayList<>(info.dclass.getBase().getSpellList(i));
  32. spells.removeAll(info.dclass.getSpellBook().get().spellsKnownAtLevel(i));
  33. return spells;
  34. }
  35. }, PREPARE {
  36. @Override
  37. public List<List<Integer>> getSpellCounts(SelectSpellsPanel.Info info) {
  38. // TODO: Bonus spells for high ability scores
  39. return info.dclass.getProto().getSpells().get().getPerDay();
  40. }
  41. @Override
  42. public Collection<DDSpell> getAvailableSpells(Info info, int i) {
  43. return info.dclass.getSpellBook().get().spellsKnownAtLevel(i);
  44. }
  45. };
  46. public abstract List<List<Integer>> getSpellCounts(SelectSpellsPanel.Info info);
  47. public abstract Collection<DDSpell> getAvailableSpells(SelectSpellsPanel.Info info, int i);
  48. }
  49. int[] ready = {0};
  50. @NonFinal int spellLevelsGrown = 0;
  51. int oldHighestSpellLevel, newHighestSpellLevel;
  52. public LevelUpSpellPanel(SpellPickType pick, SelectSpellsPanel.Info info, int toLevel, BoolArray readyCount) {
  53. newHighestSpellLevel = info.dclass.getHighestSpellLevel(toLevel);
  54. oldHighestSpellLevel = info.dclass.getHighestSpellLevel();
  55. GridBagLayout gridBagLayout = new GridBagLayout();
  56. gridBagLayout.columnWidths = new int[]{0, 0};
  57. gridBagLayout.rowHeights = new int[]{0, 0};
  58. gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
  59. gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
  60. setLayout(gridBagLayout);
  61. JScrollPane scrollPane = new JScrollPane();
  62. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  63. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  64. gbc_scrollPane.gridx = 0;
  65. gbc_scrollPane.gridy = 0;
  66. add(scrollPane, gbc_scrollPane);
  67. JPanel panel = new JPanel(new VerticalLayout(5));
  68. scrollPane.setViewportView(panel);
  69. final List<SelectSpellsPanel> panels = new ArrayList<>();
  70. final List<List<Integer>> spellList = pick.getSpellCounts(info);
  71. final List<Integer> spellsAtPreviousLevel = toLevel == 1 ? Collections.emptyList() :
  72. spellList.get(toLevel-2);
  73. final List<Integer> spellsAtCurrentLevel = spellList.get(toLevel-1);
  74. for (int i = 0; i < newHighestSpellLevel; ++i) {
  75. ++spellLevelsGrown;
  76. final int newSpells = diff(spellsAtCurrentLevel, spellsAtPreviousLevel, i,
  77. isNewSpellCircle(i));
  78. SelectSpellsPanel lvl = new SelectSpellsPanel(info, i,
  79. new LinkedHashSet<>(), newSpells, pick.getAvailableSpells(info, i),
  80. pick != SpellPickType.LEARN);
  81. panels.add(lvl);
  82. lvl.addPropertyChangeListener(SelectSpellsPanel.READY, e -> {
  83. if ((Boolean) e.getNewValue()) ++ready[0];
  84. else --ready[0];
  85. readyCount.data[1] = (ready[0] == spellLevelsGrown);
  86. ObserverDispatch.notifySubscribers(readyCount, this);
  87. });
  88. panel.add(lvl);
  89. }
  90. }
  91. private boolean isNewSpellCircle(int i) {
  92. return i == (newHighestSpellLevel - 1) && oldHighestSpellLevel != newHighestSpellLevel;
  93. }
  94. private int diff(List<Integer> current, List<Integer> previous, int level, boolean newSpellLevel) {
  95. return current.get(level) - (newSpellLevel || previous.size() <= level ? 0 : previous.get(level));
  96. }
  97. }