|
| 1 | +package org.ruru.ffta2editor; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.ByteOrder; |
| 5 | +import java.util.List; |
| 6 | +import java.util.logging.Level; |
| 7 | +import java.util.logging.Logger; |
| 8 | + |
| 9 | +import org.ruru.ffta2editor.model.item.ItemData; |
| 10 | +import org.ruru.ffta2editor.model.item.LawBonus; |
| 11 | +import org.ruru.ffta2editor.utility.AutoCompleteComboBox; |
| 12 | +import org.ruru.ffta2editor.utility.ShortChangeListener; |
| 13 | + |
| 14 | +import javafx.beans.property.ObjectProperty; |
| 15 | +import javafx.beans.property.SimpleObjectProperty; |
| 16 | +import javafx.collections.FXCollections; |
| 17 | +import javafx.collections.ObservableList; |
| 18 | +import javafx.fxml.FXML; |
| 19 | +import javafx.scene.control.ListView; |
| 20 | +import javafx.scene.control.TextField; |
| 21 | + |
| 22 | +public class LawBonusController { |
| 23 | + |
| 24 | + private static Logger logger = Logger.getLogger("org.ruru.ffta2editor"); |
| 25 | + |
| 26 | + private static final int storyRequirementOffset = 0x0011b650; |
| 27 | + |
| 28 | + @FXML ListView<LawBonus> lawBonusList; |
| 29 | + |
| 30 | + @FXML AutoCompleteComboBox<ItemData> item1; |
| 31 | + @FXML AutoCompleteComboBox<ItemData> item2; |
| 32 | + @FXML AutoCompleteComboBox<ItemData> item3; |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + @FXML TextField storyRequirement1; |
| 37 | + @FXML TextField storyRequirement2; |
| 38 | + @FXML TextField storyRequirement3; |
| 39 | + @FXML TextField storyRequirement4; |
| 40 | + |
| 41 | + |
| 42 | + private ObjectProperty<LawBonus> lawBonusProperty = new SimpleObjectProperty<>(); |
| 43 | + |
| 44 | + @FXML |
| 45 | + public void initialize() { |
| 46 | + |
| 47 | + lawBonusList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { |
| 48 | + if (oldValue != null) unbindLawBonus(); |
| 49 | + lawBonusProperty.setValue(newValue); |
| 50 | + if (newValue != null) bindLawBonus(); |
| 51 | + }); |
| 52 | + storyRequirement1.textProperty().addListener(new ShortChangeListener(storyRequirement1)); |
| 53 | + storyRequirement2.textProperty().addListener(new ShortChangeListener(storyRequirement2)); |
| 54 | + storyRequirement3.textProperty().addListener(new ShortChangeListener(storyRequirement3)); |
| 55 | + storyRequirement4.textProperty().addListener(new ShortChangeListener(storyRequirement4)); |
| 56 | + } |
| 57 | + |
| 58 | + private void unbindLawBonus() { |
| 59 | + item1.valueProperty().unbindBidirectional(lawBonusProperty.getValue().item1); |
| 60 | + item2.valueProperty().unbindBidirectional(lawBonusProperty.getValue().item2); |
| 61 | + item3.valueProperty().unbindBidirectional(lawBonusProperty.getValue().item3); |
| 62 | + } |
| 63 | + |
| 64 | + private void bindLawBonus() { |
| 65 | + item1.valueProperty().bindBidirectional(lawBonusProperty.getValue().item1); |
| 66 | + item2.valueProperty().bindBidirectional(lawBonusProperty.getValue().item2); |
| 67 | + item3.valueProperty().bindBidirectional(lawBonusProperty.getValue().item3); |
| 68 | + } |
| 69 | + |
| 70 | + public void loadLawBonus() throws Exception { |
| 71 | + if (App.archive != null) { |
| 72 | + |
| 73 | + ByteBuffer lawBonusDataBytes = App.sysdata.getFile(45); |
| 74 | + |
| 75 | + if (lawBonusDataBytes == null) { |
| 76 | + System.err.println("IdxAndPak null file error"); |
| 77 | + throw new Exception("Law Bonus data is null"); |
| 78 | + } |
| 79 | + lawBonusDataBytes.rewind(); |
| 80 | + |
| 81 | + ObservableList<LawBonus> lawBonusDataList = FXCollections.observableArrayList(); |
| 82 | + |
| 83 | + logger.info("Loading Law Bonuses"); |
| 84 | + int numLawBonus = Short.toUnsignedInt(App.arm9.getShort(0x000cb958))+1; |
| 85 | + for (int i = 0; i < numLawBonus; i++) { |
| 86 | + try { |
| 87 | + LawBonus lawBonus = new LawBonus(lawBonusDataBytes, i); |
| 88 | + lawBonusDataList.add(lawBonus); |
| 89 | + } catch (Exception e) { |
| 90 | + logger.log(Level.SEVERE, String.format("Failed to load Law Bonus %d", i)); |
| 91 | + throw e; |
| 92 | + } |
| 93 | + } |
| 94 | + App.lawBonusList = lawBonusDataList; |
| 95 | + lawBonusList.setItems(lawBonusDataList); |
| 96 | + |
| 97 | + lawBonusDataBytes.rewind(); |
| 98 | + |
| 99 | + storyRequirement1.setText(Integer.toString(Short.toUnsignedInt(App.arm9.getShort(storyRequirementOffset+0)))); |
| 100 | + storyRequirement2.setText(Integer.toString(Short.toUnsignedInt(App.arm9.getShort(storyRequirementOffset+2)))); |
| 101 | + storyRequirement3.setText(Integer.toString(Short.toUnsignedInt(App.arm9.getShort(storyRequirementOffset+4)))); |
| 102 | + storyRequirement4.setText(Integer.toString(Short.toUnsignedInt(App.arm9.getShort(storyRequirementOffset+6)))); |
| 103 | + |
| 104 | + item1.setData(App.itemList); |
| 105 | + item2.setData(App.itemList); |
| 106 | + item3.setData(App.itemList); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public void saveLawBonus() { |
| 111 | + if (App.archive != null) { |
| 112 | + List<LawBonus> lawBonus = lawBonusList.getItems(); |
| 113 | + ByteBuffer newLawBonusDatabytes = ByteBuffer.allocate(lawBonus.size()*0x8).order(ByteOrder.LITTLE_ENDIAN); |
| 114 | + |
| 115 | + logger.info("Saving Item Tables"); |
| 116 | + for (int i = 0; i < lawBonus.size(); i++) { |
| 117 | + try { |
| 118 | + newLawBonusDatabytes.put(lawBonus.get(i).toBytes()); |
| 119 | + } catch (Exception e) { |
| 120 | + logger.log(Level.SEVERE, String.format("Failed to save Law Bonus %d", i)); |
| 121 | + throw e; |
| 122 | + } |
| 123 | + } |
| 124 | + newLawBonusDatabytes.rewind(); |
| 125 | + App.sysdata.setFile(45, newLawBonusDatabytes); |
| 126 | + |
| 127 | + App.arm9.putShort(storyRequirementOffset+0, Short.parseShort(storyRequirement1.getText())); |
| 128 | + App.arm9.putShort(storyRequirementOffset+2, Short.parseShort(storyRequirement2.getText())); |
| 129 | + App.arm9.putShort(storyRequirementOffset+4, Short.parseShort(storyRequirement3.getText())); |
| 130 | + App.arm9.putShort(storyRequirementOffset+6, Short.parseShort(storyRequirement4.getText())); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments