|
| 1 | +package murphystudio.controllers; |
| 2 | + |
| 3 | +import javafx.collections.FXCollections; |
| 4 | +import javafx.collections.ObservableList; |
| 5 | +import javafx.fxml.Initializable; |
| 6 | +import javafx.scene.control.*; |
| 7 | +import javafx.scene.layout.BorderPane; |
| 8 | +import javafx.scene.layout.Pane; |
| 9 | +import murphystudio.models.MainModel; |
| 10 | +import murphystudio.objects.Accord; |
| 11 | +import murphystudio.objects.Tile; |
| 12 | + |
| 13 | +import java.lang.reflect.InvocationTargetException; |
| 14 | +import java.lang.reflect.Method; |
| 15 | +import java.net.URL; |
| 16 | +import java.util.LinkedHashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.ResourceBundle; |
| 19 | + |
| 20 | +public class ChordMakerController extends Controller implements Initializable { |
| 21 | + |
| 22 | + public Button playChordButton; |
| 23 | + public Button saveChordButton; |
| 24 | + |
| 25 | + public Label chordNameLabel; |
| 26 | + |
| 27 | + public RadioButton doRadio,doDRadio, reRadio,reDRadio, miRadio, faRadio, faDRadio, solRadio, solDRadio, laRadio, laDRadio, siRadio ; |
| 28 | + public BorderPane chordMakerPane; |
| 29 | + private ToggleGroup noteChordGroup = new ToggleGroup(); |
| 30 | + |
| 31 | + private Boolean isSeventh = false, isFifth = false, isMinor = false; |
| 32 | + |
| 33 | + private LinkedHashMap<String, Method> listToFunc; |
| 34 | + public ListView<String> chordListView; |
| 35 | + private ObservableList<String> items; |
| 36 | + |
| 37 | + private MainModel model; |
| 38 | + |
| 39 | + // 0 : Not / 1 : In function / 2 : Function finished |
| 40 | + private int printFromTile = 0; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void initialize(URL location, ResourceBundle resources) { |
| 44 | + listToFunc = new LinkedHashMap<>(); |
| 45 | + |
| 46 | + doRadio.setToggleGroup(noteChordGroup); |
| 47 | + doDRadio.setToggleGroup(noteChordGroup); |
| 48 | + reRadio.setToggleGroup(noteChordGroup); |
| 49 | + reDRadio.setToggleGroup(noteChordGroup); |
| 50 | + miRadio.setToggleGroup(noteChordGroup); |
| 51 | + faRadio.setToggleGroup(noteChordGroup); |
| 52 | + faDRadio.setToggleGroup(noteChordGroup); |
| 53 | + solRadio.setToggleGroup(noteChordGroup); |
| 54 | + solDRadio.setToggleGroup(noteChordGroup); |
| 55 | + laRadio.setToggleGroup(noteChordGroup); |
| 56 | + laDRadio.setToggleGroup(noteChordGroup); |
| 57 | + siRadio.setToggleGroup(noteChordGroup); |
| 58 | + |
| 59 | + doRadio.fire(); |
| 60 | + |
| 61 | + try { |
| 62 | + listToFunc.put(("Minor"), Accord.class.getMethod("setMinor")); |
| 63 | + listToFunc.put(("Major"), Accord.class.getMethod("setMajor")); |
| 64 | + listToFunc.put(("Dominant Seventh"), Accord.class.getMethod("setDominantSeven")); |
| 65 | + listToFunc.put(("Minor Seventh"), Accord.class.getMethod("setMinorSeventh")); |
| 66 | + listToFunc.put(("Major Seventh"), Accord.class.getMethod("setMajorSeventh")); |
| 67 | + listToFunc.put(("Dominant Seventh with Flattened fifth"), Accord.class.getMethod("setDominantSeventhFlattenedFifth")); |
| 68 | + listToFunc.put(("Dominant Seventh with Sharp fifth"), Accord.class.getMethod("setDominantSeventhSharpedFifth")); |
| 69 | + listToFunc.put(("Sixth"), Accord.class.getMethod("setSixth")); |
| 70 | + listToFunc.put(("Minor sixth"), Accord.class.getMethod("setMinorSixth")); |
| 71 | + listToFunc.put(("Minor ninth"), Accord.class.getMethod("setMinorNinth")); |
| 72 | + listToFunc.put(("Major ninth"), Accord.class.getMethod("setMajorNinth")); |
| 73 | + listToFunc.put(("Diminished"), Accord.class.getMethod("setDiminished")); |
| 74 | + listToFunc.put(("Diminished seventh"), Accord.class.getMethod("setDiminishedSeventh")); |
| 75 | + listToFunc.put(("Augmented"), Accord.class.getMethod("setAugmented")); |
| 76 | + listToFunc.put(("Suspended fourth"), Accord.class.getMethod("setSuspendedFourth")); |
| 77 | + listToFunc.put(("Suspended second"), Accord.class.getMethod("setSuspendedSecond")); |
| 78 | + } catch (NoSuchMethodException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + this.items = FXCollections.observableArrayList(); |
| 82 | + |
| 83 | + for(Map.Entry <String, Method> entry : listToFunc.entrySet()) |
| 84 | + items.add(entry.getKey()); |
| 85 | + |
| 86 | + chordListView.setItems(items); |
| 87 | + chordListView.getSelectionModel().select(1); |
| 88 | + |
| 89 | + chordListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { |
| 90 | + if ( newValue == null || printFromTile == 1 ) return; |
| 91 | + |
| 92 | + try { |
| 93 | + listToFunc.get(newValue).invoke(model.selectedChord); |
| 94 | + updtInfos(); |
| 95 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 96 | + e.printStackTrace(); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + noteChordGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> { |
| 101 | + if ( newValue == null ) return; |
| 102 | + |
| 103 | + Method lastFunction = model.selectedChord.getMethodCalled(); |
| 104 | + |
| 105 | + if (newValue == doRadio ) model.selectedChord = new Accord(60, isMinor, isFifth, isSeventh); |
| 106 | + if (newValue == doDRadio ) model.selectedChord = new Accord(61, isMinor, isFifth, isSeventh); |
| 107 | + if (newValue == reRadio ) model.selectedChord = new Accord(62, isMinor, isFifth, isSeventh); |
| 108 | + if (newValue == reDRadio ) model.selectedChord = new Accord(63, isMinor, isFifth, isSeventh); |
| 109 | + if (newValue == miRadio ) model.selectedChord = new Accord(64, isMinor, isFifth, isSeventh); |
| 110 | + if (newValue == faRadio ) model.selectedChord = new Accord(65, isMinor, isFifth, isSeventh); |
| 111 | + if (newValue == faDRadio ) model.selectedChord = new Accord(66, isMinor, isFifth, isSeventh); |
| 112 | + if (newValue == solRadio ) model.selectedChord = new Accord(67, isMinor, isFifth, isSeventh); |
| 113 | + if (newValue == solDRadio) model.selectedChord = new Accord(68, isMinor, isFifth, isSeventh); |
| 114 | + if (newValue == laRadio ) model.selectedChord = new Accord(69, isMinor, isFifth, isSeventh); |
| 115 | + if (newValue == laDRadio ) model.selectedChord = new Accord(70, isMinor, isFifth, isSeventh); |
| 116 | + if (newValue == siRadio ) model.selectedChord = new Accord(71, isMinor, isFifth, isSeventh); |
| 117 | + |
| 118 | + |
| 119 | + try { |
| 120 | + Accord.class.getMethod(lastFunction.getName()).invoke(model.selectedChord); |
| 121 | + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { |
| 122 | + e.printStackTrace(); |
| 123 | + } |
| 124 | + |
| 125 | + updtInfos(); |
| 126 | + }); |
| 127 | + |
| 128 | + playChordButton.setOnMouseClicked(event -> { |
| 129 | + Accord ch = ( printFromTile == 2 ) ? model.selectedTile.accord : model.selectedChord; |
| 130 | + for (int i = 0; i < ch.getNotes().size(); i++) |
| 131 | + this.model.midiInterface.playNote(ch.getNotes().get(i)); |
| 132 | + }); |
| 133 | + |
| 134 | + saveChordButton.setOnMouseClicked(event -> saveChord()); |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + private void updtInfos() |
| 139 | + { |
| 140 | + if ( printFromTile == 2 ) printFromTile = 0; |
| 141 | + chordNameLabel.setText(model.selectedChord.getName()); |
| 142 | + this.model.chordSorterController.resetKeys(); |
| 143 | + this.model.chordSorterController.colorizeKeys(); |
| 144 | + } |
| 145 | + |
| 146 | + private void saveChord() |
| 147 | + { |
| 148 | + if ( model != null && model.selectedChord != null && printFromTile != 1 ) |
| 149 | + model.chordSorterController.changeSelectedTileChord(model.selectedChord); |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + private String getChordNameByMethod(Accord accord) |
| 155 | + { |
| 156 | + if ( accord.getMethodCalled() == null ) return null; |
| 157 | + |
| 158 | + String name = null; |
| 159 | + for ( Map.Entry<String, Method> entry : listToFunc.entrySet() ) |
| 160 | + if (entry.getValue().getName().equals(accord.getMethodCalled().getName())) name = entry.getKey(); |
| 161 | + |
| 162 | + return name; |
| 163 | + } |
| 164 | + |
| 165 | + private void fireRadio(Accord ch) |
| 166 | + { |
| 167 | + switch (ch.getDominantName()) { |
| 168 | + case "A" : laRadio.fire(); break; |
| 169 | + case "A#" : laDRadio.fire(); break; |
| 170 | + case "B" : siRadio.fire(); break; |
| 171 | + case "C" : doRadio.fire(); break; |
| 172 | + case "C#" : doDRadio.fire(); break; |
| 173 | + case "D" : reRadio.fire(); break; |
| 174 | + case "D#" : reDRadio.fire(); break; |
| 175 | + case "E" : miRadio.fire(); break; |
| 176 | + case "F" : faRadio.fire(); break; |
| 177 | + case "F#" : faDRadio.fire(); break; |
| 178 | + case "G" : solRadio.fire(); break; |
| 179 | + case "G#" : solDRadio.fire(); break; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public void updateFromTile(Tile tile) |
| 184 | + { |
| 185 | + printFromTile = 1; |
| 186 | + |
| 187 | + Accord ch = tile.accord; |
| 188 | + chordListView.getSelectionModel().select(getChordNameByMethod(model.selectedTile.accord)); |
| 189 | + fireRadio(ch); |
| 190 | + |
| 191 | + chordNameLabel.setText(ch.getName()); |
| 192 | + for (Pane aNotesPane : this.model.chordSorterController.notesPane) aNotesPane.setStyle(null); |
| 193 | + for (int note : ch.getNotes()) { |
| 194 | + int notePaneIndex = note - 60; |
| 195 | + this.model.chordSorterController.notesPane[notePaneIndex].setStyle("-fx-background-color: red"); |
| 196 | + } |
| 197 | + |
| 198 | + printFromTile = 2; |
| 199 | + } |
| 200 | + |
| 201 | + public void setModel(MainModel model) { |
| 202 | + this.model = model; |
| 203 | + |
| 204 | + model.selectedChord = new Accord(60, false, false, false); |
| 205 | + model.selectedChord.setMajor(); |
| 206 | + updtInfos(); |
| 207 | + |
| 208 | + } |
| 209 | +} |
0 commit comments