Pārlūkot izejas kodu

Began construction of functionality to equipping an item

Sam Jaffe 9 gadi atpakaļ
vecāks
revīzija
0d4da6d131

+ 0 - 3
src/org/leumasjaffe/charsheet/Test.java

@@ -2,11 +2,8 @@ package org.leumasjaffe.charsheet;
 
 import org.leumasjaffe.charsheet.view.D20Sheet;
 
-import lombok.SneakyThrows;
-
 public class Test {
 
-	@SneakyThrows
 	public static void main(String[] args) {
 		D20Sheet frame = new D20Sheet();
 		frame.pack();

+ 24 - 0
src/org/leumasjaffe/charsheet/model/equip/DDInventory.java

@@ -96,4 +96,28 @@ public class DDInventory {
 			equipment.remove(slot);
 		}
 	}
+
+	public void equipNext( final DDItem item ) {
+		switch ( item.getSlot() ) {
+		case NONE:
+			throw new IllegalArgumentException("Cannot equip unequippable item");
+		case ONE_HAND:
+			if ( canEquip( EquipmentSlot.MAIN_HAND ) ) { 
+				equip( EquipmentSlot.MAIN_HAND, item );
+			} else {
+				equip( EquipmentSlot.OFF_HAND, item );
+			}
+			break;
+		case RING:
+			if ( canEquip( EquipmentSlot.RING1 ) ) { 
+				equip( EquipmentSlot.RING1, item );
+			} else {
+				equip( EquipmentSlot.RING2, item );
+			}
+			break;
+		default:
+			equip( item.getSlot(), item );
+			break;
+		}
+	}
 }

+ 9 - 18
src/org/leumasjaffe/charsheet/view/inventory/ArmorPanel.java

@@ -24,15 +24,6 @@ public class ArmorPanel extends JPanel {
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
-	private JTextField armorNameField;
-	private JTextField armorTypeField;
-	private JTextField armorBonusField;
-	private JTextField armorDexField;
-	private JTextField checkField;
-	private JTextField spellField;
-	private JTextField speedField;
-	private JTextField weightField;
-	private JTextField propField;
 
 	public ArmorPanel(DDItem item) {
 		final DDArmor armor = item.getArmor();
@@ -122,7 +113,7 @@ public class ArmorPanel extends JPanel {
 		gbc_lblMaxDex.gridy = 1;
 		panel.add(lblMaxDex, gbc_lblMaxDex);
 		
-		armorNameField = new JTextField(item.getName());
+		JTextField armorNameField = new JTextField(item.getName());
 		GridBagConstraints gbc_armorNameField = new GridBagConstraints();
 		gbc_armorNameField.insets = new Insets(0, 0, 0, 0);
 		gbc_armorNameField.fill = GridBagConstraints.HORIZONTAL;
@@ -131,7 +122,7 @@ public class ArmorPanel extends JPanel {
 		panel.add(armorNameField, gbc_armorNameField);
 		armorNameField.setColumns(10);
 		
-		armorTypeField = new JTextField(armor.getType().toString());
+		JTextField armorTypeField = new JTextField(armor.getType().toString());
 		GridBagConstraints gbc_armorTypeField = new GridBagConstraints();
 		gbc_armorTypeField.insets = new Insets(0, 0, 0, 0);
 		gbc_armorTypeField.fill = GridBagConstraints.HORIZONTAL;
@@ -140,7 +131,7 @@ public class ArmorPanel extends JPanel {
 		panel.add(armorTypeField, gbc_armorTypeField);
 		armorTypeField.setColumns(10);
 		
-		armorBonusField = new JTextField(StringHelper.toSignedString(item.getArmor().getBonus()));
+		JTextField armorBonusField = new JTextField(StringHelper.toSignedString(item.getArmor().getBonus()));
 		armorBonusField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_armorBonusField = new GridBagConstraints();
 		gbc_armorBonusField.insets = new Insets(0, 0, 0, 0);
@@ -150,7 +141,7 @@ public class ArmorPanel extends JPanel {
 		panel.add(armorBonusField, gbc_armorBonusField);
 		armorBonusField.setColumns(10);
 		
-		armorDexField = new JTextField(StringHelper.toSignedString(armor.getMaxDex()));
+		JTextField armorDexField = new JTextField(StringHelper.toSignedString(armor.getMaxDex()));
 		armorDexField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_armorDexField = new GridBagConstraints();
 		gbc_armorDexField.fill = GridBagConstraints.HORIZONTAL;
@@ -237,7 +228,7 @@ public class ArmorPanel extends JPanel {
 		gbc_lblSpecialProperties.gridy = 0;
 		panel_1.add(lblSpecialProperties, gbc_lblSpecialProperties);
 		
-		checkField = new JTextField(StringHelper.toString(armor.getCheckPenalty()));
+		JTextField checkField = new JTextField(StringHelper.toString(armor.getCheckPenalty()));
 		checkField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_checkField = new GridBagConstraints();
 		gbc_checkField.insets = new Insets(0, 0, 0, 0);
@@ -247,7 +238,7 @@ public class ArmorPanel extends JPanel {
 		panel_1.add(checkField, gbc_checkField);
 		checkField.setColumns(10);
 		
-		spellField = new JTextField(StringHelper.toString(armor.getSpellFailure()) + "%");
+		JTextField spellField = new JTextField(StringHelper.toString(armor.getSpellFailure()) + "%");
 		spellField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_spellField = new GridBagConstraints();
 		gbc_spellField.insets = new Insets(0, 0, 0, 0);
@@ -257,7 +248,7 @@ public class ArmorPanel extends JPanel {
 		panel_1.add(spellField, gbc_spellField);
 		spellField.setColumns(10);
 		
-		speedField = new JTextField(StringHelper.toString(armor.getSpeed()));
+		JTextField speedField = new JTextField(StringHelper.toString(armor.getSpeed()));
 		speedField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_speedField = new GridBagConstraints();
 		gbc_speedField.insets = new Insets(0, 0, 0, 0);
@@ -267,7 +258,7 @@ public class ArmorPanel extends JPanel {
 		panel_1.add(speedField, gbc_speedField);
 		speedField.setColumns(10);
 		
-		weightField = new JTextField();
+		JTextField weightField = new JTextField();
 		GridBagConstraints gbc_weightField = new GridBagConstraints();
 		gbc_weightField.insets = new Insets(0, 0, 0, 0);
 		gbc_weightField.fill = GridBagConstraints.HORIZONTAL;
@@ -276,7 +267,7 @@ public class ArmorPanel extends JPanel {
 		panel_1.add(weightField, gbc_weightField);
 		weightField.setColumns(10);
 		
-		propField = new JTextField();
+		JTextField propField = new JTextField();
 		GridBagConstraints gbc_propField = new GridBagConstraints();
 		gbc_propField.fill = GridBagConstraints.HORIZONTAL;
 		gbc_propField.gridx = 4;

+ 82 - 2
src/org/leumasjaffe/charsheet/view/inventory/InventoryPanel.java

@@ -2,15 +2,18 @@ package org.leumasjaffe.charsheet.view.inventory;
 
 import javax.swing.JComponent;
 import javax.swing.JLabel;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.SwingConstants;
 import javax.swing.border.MatteBorder;
 
 import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.charsheet.controller.inventory.PopClickListener;
 import org.leumasjaffe.charsheet.model.DDCharacter;
 import org.leumasjaffe.charsheet.model.equip.DDInventory;
 import org.leumasjaffe.charsheet.model.equip.DDItem;
+import org.leumasjaffe.charsheet.model.equip.EquipmentSlot;
 
 import lombok.AccessLevel;
 import lombok.experimental.FieldDefaults;
@@ -20,6 +23,8 @@ import java.awt.Font;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 
+import static org.leumasjaffe.charsheet.model.equip.EquipmentSlot.*;
+
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class InventoryPanel extends JPanel {
 	/**
@@ -63,8 +68,83 @@ public class InventoryPanel extends JPanel {
 		inventory.removeAll();
 		
 		final DDInventory inv = model.getInventory();
-		for ( final DDItem item : inv.getItems() ) {
-			inventory.add(new ItemPanel(item));
+		inv.getItems().stream().forEach( 
+				item -> createWithRightClickMenu(inv, item) );
+	}
+
+	private void createWithRightClickMenu(final DDInventory inv, final DDItem item) {
+		final ItemPanel panel = new ItemPanel(item);
+		panel.addMouseListener(new PopClickListener(
+				new ItemInfoMenu(item)));
+		inventory.add(panel);
+	}
+	
+	void example_equipItem(final DDInventory inv, final DDItem item) {
+		if ( getUnequippedCount(item) == 0 ) { return; }
+		if ( inv.canEquip( item ) || getReplaceItem( inv, item.getSlot() ) ) {
+			inv.equipNext( item );
+		}
+	}
+
+	private boolean getReplaceItem(final DDInventory inv, 
+			final EquipmentSlot slot) {
+		switch ( slot ) {
+		case TWO_HANDS: return selectToReplaceAllOf( inv, MAIN_HAND, OFF_HAND );
+		case ONE_HAND: return selectToReplaceOneOf( inv, MAIN_HAND, OFF_HAND );
+		case RING: return selectToReplaceOneOf( inv, RING1, RING2 );
+		default: return selectToReplace( inv, slot );
+		}
+	}
+
+	private boolean selectToReplaceAllOf(final DDInventory inv, 
+			final EquipmentSlot slot1, final EquipmentSlot slot2) {
+		if ( inv.getEquipment().get(slot1) == inv.getEquipment().get(slot2) ) {
+			
+		} else {
+			
 		}
+		if ( JOptionPane.showConfirmDialog(null, 
+				"TODO", 
+				"Replace Equipped Item", JOptionPane.YES_NO_OPTION) 
+				== JOptionPane.YES_OPTION ) {
+			inv.unequip( slot1 );
+			inv.unequip( slot2 );
+			return true;
+		}
+		return false;
+	}
+
+	private boolean selectToReplaceOneOf(final DDInventory inv, 
+			final EquipmentSlot slot1, final EquipmentSlot slot2) {
+		final int choice = JOptionPane.showOptionDialog(null, 
+				"TODO", 
+				"Replace Equipped Item", JOptionPane.YES_NO_CANCEL_OPTION,
+				JOptionPane.QUESTION_MESSAGE, null, 
+				new String[] {"Cancel", slot1.toString(), slot2.toString()}, null );
+		if ( choice == JOptionPane.YES_OPTION ) {
+			inv.unequip( slot1 );
+			return true;
+		} else if ( choice == JOptionPane.NO_OPTION ) {
+			inv.unequip( slot2 );
+			return true;
+		}
+		return false;
+	}
+
+	private boolean selectToReplace(final DDInventory inv, 
+			final EquipmentSlot slot) {
+		if ( JOptionPane.showConfirmDialog(null, 
+				"TODO", 
+				"Replace Equipped Item", JOptionPane.YES_NO_OPTION) 
+				== JOptionPane.YES_OPTION ) {
+			inv.unequip( slot );
+			return true;
+		}
+		return false;
+	}
+
+	private int getUnequippedCount(DDItem item) {
+		// TODO Auto-generated method stub
+		return 0;
 	}
 }

+ 4 - 11
src/org/leumasjaffe/charsheet/view/inventory/ItemPanel.java

@@ -12,7 +12,6 @@ import java.awt.Font;
 import java.awt.Color;
 import javax.swing.SwingConstants;
 
-import org.leumasjaffe.charsheet.controller.inventory.PopClickListener;
 import org.leumasjaffe.charsheet.model.equip.DDItem;
 
 import java.awt.Component;
@@ -23,10 +22,6 @@ public class ItemPanel extends JPanel {
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
-	private JTextField nameField;
-	private JTextField weightField;
-	private JTextField countField;
-	private JTextField valueField;
 
 	public ItemPanel(DDItem item) {
 		setPreferredSize(new Dimension(280, 40));
@@ -100,7 +95,7 @@ public class ItemPanel extends JPanel {
 		gbc_lblValue.gridy = 1;
 		add(lblValue, gbc_lblValue);
 		
-		nameField = new JTextField(item.getName());
+		JTextField nameField = new JTextField(item.getName());
 		GridBagConstraints gbc_nameField = new GridBagConstraints();
 		gbc_nameField.insets = new Insets(0, 0, 0, 0);
 		gbc_nameField.fill = GridBagConstraints.HORIZONTAL;
@@ -109,7 +104,7 @@ public class ItemPanel extends JPanel {
 		add(nameField, gbc_nameField);
 		nameField.setColumns(10);
 		
-		countField = new JTextField(Integer.toString(item.getCount().value()));
+		JTextField countField = new JTextField(Integer.toString(item.getCount().value()));
 		countField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_countField = new GridBagConstraints();
 		gbc_countField.insets = new Insets(0, 0, 0, 0);
@@ -119,7 +114,7 @@ public class ItemPanel extends JPanel {
 		add(countField, gbc_countField);
 		countField.setColumns(10);
 		
-		weightField = new JTextField();
+		JTextField weightField = new JTextField();
 		weightField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_weightField = new GridBagConstraints();
 		gbc_weightField.insets = new Insets(0, 0, 0, 0);
@@ -129,7 +124,7 @@ public class ItemPanel extends JPanel {
 		add(weightField, gbc_weightField);
 		weightField.setColumns(10);
 		
-		valueField = new JTextField(item.getValue().toString());
+		JTextField valueField = new JTextField(item.getValue().toString());
 		valueField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_valueField = new GridBagConstraints();
 		gbc_valueField.insets = new Insets(0, 0, 0, 0);
@@ -138,8 +133,6 @@ public class ItemPanel extends JPanel {
 		gbc_valueField.gridy = 2;
 		add(valueField, gbc_valueField);
 		valueField.setColumns(10);
-		
-		addMouseListener(new PopClickListener(new ItemInfoMenu(item)));
 	}
 
 }

+ 6 - 12
src/org/leumasjaffe/charsheet/view/inventory/ShieldPanel.java

@@ -24,12 +24,6 @@ public class ShieldPanel extends JPanel {
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
-	private JTextField nameField;
-	private JTextField armorBonusField;
-	private JTextField weightField;
-	private JTextField checkField;
-	private JTextField spellField;
-	private JTextField propField;
 
 	public ShieldPanel(DDItem item) {
 		final DDArmor armor = item.getArmor();
@@ -119,7 +113,7 @@ public class ShieldPanel extends JPanel {
 		lblCheckPenalty.setBackground(Color.BLACK);
 		lblCheckPenalty.setFont(new Font("Tahoma", Font.BOLD, 8));
 		
-		nameField = new JTextField(item.getName());
+		JTextField nameField = new JTextField(item.getName());
 		GridBagConstraints gbc_nameField = new GridBagConstraints();
 		gbc_nameField.insets = new Insets(0, 0, 0, 0);
 		gbc_nameField.fill = GridBagConstraints.HORIZONTAL;
@@ -128,7 +122,7 @@ public class ShieldPanel extends JPanel {
 		panel.add(nameField, gbc_nameField);
 		nameField.setColumns(10);
 		
-		armorBonusField = new JTextField(StringHelper.toSignedString(armor.getBonus()));
+		JTextField armorBonusField = new JTextField(StringHelper.toSignedString(armor.getBonus()));
 		armorBonusField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_armorBonusField = new GridBagConstraints();
 		gbc_armorBonusField.insets = new Insets(0, 0, 0, 0);
@@ -138,7 +132,7 @@ public class ShieldPanel extends JPanel {
 		panel.add(armorBonusField, gbc_armorBonusField);
 		armorBonusField.setColumns(10);
 		
-		weightField = new JTextField();
+		JTextField weightField = new JTextField();
 		weightField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_weightField = new GridBagConstraints();
 		gbc_weightField.insets = new Insets(0, 0, 0, 0);
@@ -148,7 +142,7 @@ public class ShieldPanel extends JPanel {
 		panel.add(weightField, gbc_weightField);
 		weightField.setColumns(10);
 		
-		checkField = new JTextField(StringHelper.toString(armor.getCheckPenalty()));
+		JTextField checkField = new JTextField(StringHelper.toString(armor.getCheckPenalty()));
 		checkField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_checkField = new GridBagConstraints();
 		gbc_checkField.fill = GridBagConstraints.HORIZONTAL;
@@ -196,7 +190,7 @@ public class ShieldPanel extends JPanel {
 		gbc_lblSpecialProperties.gridy = 0;
 		panel_1.add(lblSpecialProperties, gbc_lblSpecialProperties);
 		
-		spellField = new JTextField(StringHelper.toString(armor.getSpellFailure()) + "%");
+		JTextField spellField = new JTextField(StringHelper.toString(armor.getSpellFailure()) + "%");
 		spellField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_spellField = new GridBagConstraints();
 		gbc_spellField.insets = new Insets(0, 0, 0, 0);
@@ -206,7 +200,7 @@ public class ShieldPanel extends JPanel {
 		panel_1.add(spellField, gbc_spellField);
 		spellField.setColumns(10);
 		
-		propField = new JTextField();
+		JTextField propField = new JTextField();
 		GridBagConstraints gbc_propField = new GridBagConstraints();
 		gbc_propField.fill = GridBagConstraints.HORIZONTAL;
 		gbc_propField.gridx = 1;

+ 7 - 14
src/org/leumasjaffe/charsheet/view/inventory/WeaponPanel.java

@@ -24,13 +24,6 @@ public class WeaponPanel extends JPanel {
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
-	private JTextField nameField;
-	private JTextField attackBonusField;
-	private JTextField damageField;
-	private JTextField criticalField;
-	private JTextField rangeField;
-	private JTextField propField;
-	private JTextField typeField;
 
 	public WeaponPanel(final DDItem item) {
 		final DDWeapon weapon = item.getWeapon();
@@ -120,7 +113,7 @@ public class WeaponPanel extends JPanel {
 		lblCritical.setBackground(Color.BLACK);
 		lblCritical.setFont(new Font("Tahoma", Font.BOLD, 8));
 		
-		nameField = new JTextField(item.getName());
+		JTextField nameField = new JTextField(item.getName());
 		GridBagConstraints gbc_nameField = new GridBagConstraints();
 		gbc_nameField.insets = new Insets(0, 0, 0, 0);
 		gbc_nameField.fill = GridBagConstraints.HORIZONTAL;
@@ -129,7 +122,7 @@ public class WeaponPanel extends JPanel {
 		panel.add(nameField, gbc_nameField);
 		nameField.setColumns(10);
 		
-		attackBonusField = new JTextField(StringHelper.toSignedString(weapon.getAttackBonus()));
+		JTextField attackBonusField = new JTextField(StringHelper.toSignedString(weapon.getAttackBonus()));
 		attackBonusField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_attackBonusField = new GridBagConstraints();
 		gbc_attackBonusField.insets = new Insets(0, 0, 0, 0);
@@ -139,7 +132,7 @@ public class WeaponPanel extends JPanel {
 		panel.add(attackBonusField, gbc_attackBonusField);
 		attackBonusField.setColumns(10);
 		
-		damageField = new JTextField(weapon.getDamage() + StringHelper.toSignedString(weapon.getDamageBonus(), 0));
+		JTextField damageField = new JTextField(weapon.getDamage() + StringHelper.toSignedString(weapon.getDamageBonus(), 0));
 		damageField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_damageField = new GridBagConstraints();
 		gbc_damageField.insets = new Insets(0, 0, 0, 0);
@@ -152,7 +145,7 @@ public class WeaponPanel extends JPanel {
 		final StringBuilder critStr = new StringBuilder();
 		if (weapon.hasCriticalThreat()) { critStr.append(weapon.getCriticalThreat()).append("-20/"); }
 		critStr.append('x').append(weapon.getCriticalDamage());
-		criticalField = new JTextField(critStr.toString());
+		JTextField criticalField = new JTextField(critStr.toString());
 		criticalField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_criticalField = new GridBagConstraints();
 		gbc_criticalField.fill = GridBagConstraints.HORIZONTAL;
@@ -213,7 +206,7 @@ public class WeaponPanel extends JPanel {
 		gbc_lblSpecialProperties.gridy = 0;
 		panel_1.add(lblSpecialProperties, gbc_lblSpecialProperties);
 		
-		rangeField = new JTextField(StringHelper.toString(weapon.getRange().toString()));
+		JTextField rangeField = new JTextField(StringHelper.toString(weapon.getRange().toString()));
 		rangeField.setHorizontalAlignment(SwingConstants.CENTER);
 		GridBagConstraints gbc_rangeField = new GridBagConstraints();
 		gbc_rangeField.insets = new Insets(0, 0, 0, 0);
@@ -223,7 +216,7 @@ public class WeaponPanel extends JPanel {
 		panel_1.add(rangeField, gbc_rangeField);
 		rangeField.setColumns(10);
 		
-		typeField = new JTextField(item.getWeapon().getType().toString());
+		JTextField typeField = new JTextField(item.getWeapon().getType().toString());
 		typeField.setHorizontalAlignment(SwingConstants.CENTER);
 		typeField.setColumns(10);
 		GridBagConstraints gbc_typeField = new GridBagConstraints();
@@ -233,7 +226,7 @@ public class WeaponPanel extends JPanel {
 		gbc_typeField.gridy = 1;
 		panel_1.add(typeField, gbc_typeField);
 		
-		propField = new JTextField();
+		JTextField propField = new JTextField();
 		GridBagConstraints gbc_propField = new GridBagConstraints();
 		gbc_propField.fill = GridBagConstraints.HORIZONTAL;
 		gbc_propField.gridx = 2;