Sfoglia il codice sorgente

Adding the ability to apply selection of prepared spells.
The button is only available if all levels of spells are selected.
Updating the GUI is done in a heavy-handed manner, by reloading the entire model.
TODO: precision reload

Sam Jaffe 8 anni fa
parent
commit
8eef309ef4

+ 3 - 0
resources/Potato.json

@@ -15,6 +15,9 @@
               "Create Water"
             ],
             "spellsPreparedPreviously":[
+              "Create Water",
+              "Create Water",
+              "Create Water",
               "Create Water"
             ]
           },

+ 1 - 1
src/org/leumasjaffe/charsheet/model/magic/DDSpell.java

@@ -80,7 +80,7 @@ public class DDSpell {
 		return classToLevel.get(clas);
 	}
 	
-	@JsonCreator static DDSpell fromString(String str) { 
+	@JsonCreator public static DDSpell fromString(String str) { 
 		return DDSpellFactory.loadSpell(str);
 	}
 }

+ 5 - 0
src/org/leumasjaffe/charsheet/view/D20Sheet.java

@@ -134,6 +134,7 @@ public class D20Sheet extends JFrame {
 				});
 			}
 			// Step N: regenerate spellbooks
+			reload();
 		});
 		mnSession.add(mntmTakeRest);
 		
@@ -179,6 +180,10 @@ public class D20Sheet extends JFrame {
 		}
 	}
 	
+	public void reload() {
+		setModel(this.model); // TODO: make this not awful
+	}
+	
 	private void setModel(DDCharacter model) {
 		classTabs.clear();
 		this.model = model;

+ 57 - 5
src/org/leumasjaffe/charsheet/view/magic/PrepareSpellsDialog.java

@@ -5,39 +5,91 @@ import javax.swing.JPanel;
 import org.jdesktop.swingx.VerticalLayout;
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import org.leumasjaffe.charsheet.model.magic.impl.Prepared;
+
+import lombok.AccessLevel;
+import lombok.experimental.FieldDefaults;
+
 import java.awt.GridBagLayout;
 import javax.swing.JScrollPane;
 import java.awt.GridBagConstraints;
+import java.awt.Insets;
+import java.util.ArrayList;
+import java.util.List;
 
+import javax.swing.JButton;
+import javax.swing.JDialog;
+
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class PrepareSpellsDialog extends JPanel {
 
 	/**
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
+	
+	int[] ready = {0};
+	int highestSpellLevel;
 
 	public PrepareSpellsDialog(DDCharacterClass dclass) {
+		highestSpellLevel = dclass.getHighestSpellLevel();
+		ready[0] = highestSpellLevel;
+		
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0};
-		gridBagLayout.rowHeights = new int[]{0, 0};
+		gridBagLayout.rowHeights = new int[]{0, 0, 0};
 		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
-		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
+		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
 		setLayout(gridBagLayout);
 		
+		JPanel panel_1 = new JPanel();
+		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
+		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
+		gbc_panel_1.fill = GridBagConstraints.BOTH;
+		gbc_panel_1.gridx = 0;
+		gbc_panel_1.gridy = 0;
+		add(panel_1, gbc_panel_1);
+		GridBagLayout gbl_panel_1 = new GridBagLayout();
+		gbl_panel_1.columnWidths = new int[]{0, 0};
+		gbl_panel_1.rowHeights = new int[]{0, 0};
+		gbl_panel_1.columnWeights = new double[]{0.0, Double.MIN_VALUE};
+		gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
+		panel_1.setLayout(gbl_panel_1);
+		
+		JButton btnPrepareTheseSpells = new JButton("Prepare These Spells");
+		GridBagConstraints gbc_btnPrepareTheseSpells = new GridBagConstraints();
+		gbc_btnPrepareTheseSpells.gridx = 0;
+		gbc_btnPrepareTheseSpells.gridy = 0;
+		panel_1.add(btnPrepareTheseSpells, gbc_btnPrepareTheseSpells);
+		
 		JScrollPane scrollPane = new JScrollPane();
 		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
 		gbc_scrollPane.fill = GridBagConstraints.BOTH;
 		gbc_scrollPane.gridx = 0;
-		gbc_scrollPane.gridy = 0;
+		gbc_scrollPane.gridy = 1;
 		add(scrollPane, gbc_scrollPane);
 		
 		JPanel panel = new JPanel(new VerticalLayout(5));
 		scrollPane.setViewportView(panel);
 		
 		final Prepared spellBook = (Prepared) dclass.getSpellBook().get();
-		for (int i = 0; i < dclass.getHighestSpellLevel(); ++i) {
-			panel.add(new SelectPreparedSpellsPanel(spellBook.getSpellsPreparedPreviouslyForLevel(i), spellBook.spellsKnownAtLevel(i)));
+		List<SelectPreparedSpellsPanel> panels = new ArrayList<>();
+		for (int i = 0; i < highestSpellLevel; ++i) {
+			SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(spellBook.getSpellsPreparedPreviouslyForLevel(i), spellBook.spellsKnownAtLevel(i));
+			panels.add(lvl);
+			lvl.addPropertyChangeListener(SelectPreparedSpellsPanel.READY, e -> {
+				if ((Boolean) e.getNewValue()) ++ready[0];
+				else --ready[0];
+				btnPrepareTheseSpells.setEnabled(ready[0] == highestSpellLevel);
+			});
+			panel.add(lvl);
 		}
+		
+		btnPrepareTheseSpells.addActionListener(e -> {
+			((JDialog) this.getParent().getParent().getParent()).dispose();
+			for (int i = 0; i < highestSpellLevel; ++i) {
+				spellBook.prepareSpells(i, panels.get(i).getPrepared());
+			}
+		});
 	}
 
 }

+ 15 - 0
src/org/leumasjaffe/charsheet/view/magic/SelectPreparedSpellsPanel.java

@@ -1,6 +1,7 @@
 package org.leumasjaffe.charsheet.view.magic;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
@@ -64,12 +65,15 @@ class SelectPreparedSpellsPanel extends JPanel {
 	 */
 	private static final long serialVersionUID = 1L;
 	
+	public static final String READY = "Is Filled Out";
+	
 	@Getter List<DDSpell> prepared;
 	
 	SelectSpellModel modelPrepared, modelKnown;
 
 	public SelectPreparedSpellsPanel(List<DDSpell> prepared,
 			Collection<DDSpell> known) {
+		putClientProperty(READY, true);
 		this.prepared = new ArrayList<>(prepared);
 		this.modelPrepared = new SelectSpellModel(prepared.stream().map(DDSpell::getName).toArray());
 		this.modelKnown = new SelectSpellModel(known.stream().map(DDSpell::getName).toArray());
@@ -137,6 +141,9 @@ class SelectPreparedSpellsPanel extends JPanel {
 			}
 			tablePrepared.getSelectionModel().clearSelection();
 			tablePrepared.repaint();
+			if ((Boolean) getClientProperty(READY)) {
+				putClientProperty(READY, false);
+			}
 		});
 		
 		JButton button_1 = new JButton("<<");
@@ -163,6 +170,14 @@ class SelectPreparedSpellsPanel extends JPanel {
 			tableKnown.getSelectionModel().clearSelection();
 			tablePrepared.getSelectionModel().clearSelection();
 			tablePrepared.repaint();
+			
+			if (!(Boolean) getClientProperty(READY) && !Arrays.asList(modelPrepared.data).contains("<none>")) {
+				this.prepared.clear();
+				for (Object o : modelPrepared.data) {
+					this.prepared.add(DDSpell.fromString((String) o)); // TODO
+				}
+				putClientProperty(READY, true);
+			}
 		});
 	}