Skip to content

Commit

Permalink
Script: Add Observable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MonsterDruide1 committed Oct 7, 2022
1 parent 1b5364b commit 3238cfc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 70 deletions.
105 changes: 51 additions & 54 deletions src/main/java/io/github/jadefalke2/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -23,36 +22,40 @@ public class Script {
* @return the created script
*/
public static Script getEmptyScript (int amount){
Script tmp = new Script();
Script tmp;
try {
tmp = new Script();
} catch (IOException | CorruptedScriptException e) {
throw new RuntimeException(e);
}

for (int i = 0; i < amount; i++){
tmp.insertRow(i,InputLine.getEmpty());
}
tmp.dirty.set(false);
tmp.dirty = false;

return tmp;
}


private final ObservableProperty<File> file;
private File file;
private DefaultTableModel table;
private final ArrayList<InputLine> inputLines;
private final ObservableProperty<Boolean> dirty;
private final List<ObservableProperty.PropertyChangeListener<Integer>> lengthListeners;
private boolean dirty;

public Script() {
inputLines = new ArrayList<>();
file = new ObservableProperty<>(null);
dirty = new ObservableProperty<>(false);
lengthListeners = new ArrayList<>();
}
public Script(String script) throws CorruptedScriptException {
this();
prepareScript(script);
private final ArrayList<ScriptObserver> observers;

public Script() throws IOException, CorruptedScriptException {
this(null);
}
public Script (File file) throws CorruptedScriptException, IOException {
this(Util.fileToString(file));
this.file.set(file);
this.file = file;
inputLines = new ArrayList<>();
dirty = false;
observers = new ArrayList<>();

if(file != null)
prepareScript(Util.fileToString(file));
}

/**
Expand All @@ -66,7 +69,6 @@ private void prepareScript (String script) throws CorruptedScriptException {
int currentFrame = 0;

for (String line : lines) {

InputLine currentInputLine = new InputLine(line);
int frame = Integer.parseInt(line.split(" ")[0]);

Expand All @@ -80,14 +82,13 @@ private void prepareScript (String script) throws CorruptedScriptException {
}

inputLines.add(currentInputLine);

currentFrame++;
}
updateLength(0);
updateLength();
}

public boolean closeScript(){
if(!dirty.get()){
if(!dirty){
return true; //just close without issue if no changes happened
}

Expand All @@ -100,7 +101,7 @@ public boolean closeScript(){
JOptionPane.showMessageDialog(null, "Failed to save file!\nError: " + ioe.getMessage(), "Saving failed", JOptionPane.ERROR_MESSAGE);
return false;
}
return dirty.get();
return dirty;
}
return result == JOptionPane.NO_OPTION; //otherwise return false -> cancel
}
Expand All @@ -117,13 +118,13 @@ public String getFull (){
* Saves the script (itself) to that last saved/opened file
*/
public void saveFile() throws IOException {
if(file.get() == null){
if(file == null){
saveFileAs();
return;
}

writeToFile(file.get());
dirty.set(false);
writeToFile(file);
setDirty(false);
}

private void writeToFile(File dest) throws IOException {
Expand All @@ -139,7 +140,7 @@ public void saveFileAs() throws IOException {
File savedFile = new TxtFileChooser(Settings.INSTANCE.directory.get()).getFile(false);
if(savedFile != null){
Logger.log("saving file as " + savedFile.getAbsolutePath());
file.set(savedFile);
setFile(savedFile);
saveFile();
}
}
Expand Down Expand Up @@ -188,23 +189,23 @@ public void replaceRow(int row, InputLine replacement) {
for(int i=0;i<tableArray.length;i++){
table.setValueAt(tableArray[i], row, i);
}
dirty.set(true);
setDirty(true);
}

public void removeRow(int row){
inputLines.remove(row);
table.removeRow(row);
adjustLines(row);
dirty.set(true);
updateLength(inputLines.size()+1);
setDirty(true);
updateLength();
}

public void insertRow(int row, InputLine line) {
inputLines.add(row, line);
if(table != null) table.insertRow(row, line.getArray(row));
adjustLines(row);
dirty.set(true);
updateLength(inputLines.size()-1);
setDirty(true);
updateLength();
}

public void appendRow(InputLine line) {
Expand All @@ -231,7 +232,7 @@ public void setButton(int row, Button button, boolean enabled) {
inputLines.get(row).buttons.remove(button);
table.setValueAt("", row, col);
}
dirty.set(true);
setDirty(true);
}

public void setStickPos(int row, JoystickPanel.StickType stickType, StickPosition position) {
Expand All @@ -240,39 +241,35 @@ public void setStickPos(int row, JoystickPanel.StickType stickType, StickPositio
else
inputLines.get(row).setStickR(position);
table.setValueAt(position.toCartString(), row, stickType == JoystickPanel.StickType.L_STICK ? 1 : 2); //TODO find a better way to differentiate sticks?
dirty.set(true);
setDirty(true);
}

public String getName() {
return file.get() == null ? "unnamed script" : file.get().getName();
return file == null ? "unnamed script" : file.getName();
}

public boolean isDirty() {
return dirty.get();
return dirty;
}

public void attachDirtyListener(ObservableProperty.PropertyChangeListener<Boolean> listener) {
dirty.attachListener(listener);
private void setDirty(boolean dirty) {
this.dirty = dirty;
observers.forEach(c -> c.onDirtyChange(dirty));
}
public void detachDirtyListener(ObservableProperty.PropertyChangeListener<Boolean> listener) {
dirty.detachListener(listener);
private void setFile(File file) {
this.file = file;
observers.forEach(c -> c.onFileChange(file));
}
public void attachLengthListener(ObservableProperty.PropertyChangeListener<Integer> listener) {
lengthListeners.add(listener);
}
public void detachLengthListener(ObservableProperty.PropertyChangeListener<Integer> listener) {
lengthListeners.remove(listener);
}
public void attachFileListener(ObservableProperty.PropertyChangeListener<File> listener) {
file.attachListener(listener);

public void updateLength() {
int after = inputLines.size();
observers.forEach(c -> c.onLengthChange(after));
}
public void detachFileListener(ObservableProperty.PropertyChangeListener<File> listener) {
file.detachListener(listener);

public void attachObserver(ScriptObserver observer) {
observers.add(observer);
}
public void updateLength(int before) {
int after = inputLines.size();
for(ObservableProperty.PropertyChangeListener<Integer> listener : lengthListeners) {
listener.onChange(after, before);
}
public void detachObserver(ScriptObserver observer) {
observers.remove(observer);
}
}
34 changes: 18 additions & 16 deletions src/main/java/io/github/jadefalke2/components/ScriptTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.jadefalke2.actions.Action;
import io.github.jadefalke2.util.Logger;
import io.github.jadefalke2.util.ObservableProperty;
import io.github.jadefalke2.util.ScriptObserver;
import io.github.jadefalke2.util.Settings;

import javax.swing.JPanel;
Expand All @@ -15,7 +16,7 @@
import java.io.IOException;
import java.util.Stack;

public class ScriptTab extends JPanel {
public class ScriptTab extends JPanel implements ScriptObserver {

private final MainEditorWindow mainEditorWindow;
private final Script script;
Expand Down Expand Up @@ -60,10 +61,11 @@ public ScriptTab(MainEditorWindow mainEditorWindow, Script script, ObservablePro
mainEditorWindow.enableUndoRedo(false, false);

this.lengthChangeListener = listener;
script.attachLengthListener(listener);
listener.onChange(script.getLines().length, -1);

Settings.INSTANCE.joystickPanelPosition.attachListener(ignored -> refreshLayout());

script.attachObserver(this);
}

public void refreshLayout() {
Expand Down Expand Up @@ -182,26 +184,26 @@ public void previewAction(Action action) {
}

public void setDirtyListener(ObservableProperty.PropertyChangeListener<Boolean> listener) {
if(dirtyChangeListener != null)
script.detachDirtyListener(dirtyChangeListener);

this.dirtyChangeListener = listener;
script.attachDirtyListener(listener);
}
public void setFileListener(ObservableProperty.PropertyChangeListener<File> listener) {
if(fileChangeListener != null)
script.detachFileListener(fileChangeListener);

this.fileChangeListener = listener;
script.attachFileListener(listener);
}
public void cleanup() {
script.detachDirtyListener(dirtyChangeListener);
dirtyChangeListener = null;
script.detachLengthListener(lengthChangeListener);
lengthChangeListener = null;
script.detachFileListener(fileChangeListener);
fileChangeListener = null;
script.detachObserver(this);
}

@Override
public void onFileChange(File file) {
fileChangeListener.onChange(file);
}
@Override
public void onLengthChange(int length) {
lengthChangeListener.onChange(length);
}
@Override
public void onDirtyChange(boolean dirty) {
dirtyChangeListener.onChange(dirty);
}

public Script getScript() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/jadefalke2/util/ScriptObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.jadefalke2.util;

import java.io.File;

public interface ScriptObserver {

default void onFileChange(File file) {}
default void onLengthChange(int length) {}
default void onDirtyChange(boolean dirty) {}

}

0 comments on commit 3238cfc

Please sign in to comment.