Skip to content

Commit

Permalink
Add Select Lines functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
MonsterDruide1 committed Sep 21, 2022
1 parent 0cfb042 commit bdc0eeb
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,14 @@ public void setAllTabsClosed(boolean closed) {
public void setCurrentScriptLength(int newLength) {
scriptLengthLabel.setText("Length: "+newLength);
}

public void selectLines() {
ScriptTab tab = getActiveScriptTab();
SelectLinesDialog dialog = new SelectLinesDialog(this, tab.getScript().getLines().length);
dialog.setVisible(true);
if(dialog.isAccepted()) {
int[] selectedLines = dialog.getSelectedLines();
tab.setSelectedLines(selectedLines);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class MainJMenuBar extends JMenuBar {

private JMenuItem newScript, newWindow, openScript, save, saveAs, exit;
private JMenuItem undo, redo, cut, copy, paste, deleteLines, addLine, settings;
private JMenuItem undo, redo, cut, copy, paste, deleteLines, selectLines, addLine, settings;
private JCheckBoxMenuItem darkTheme;
private JMenuItem discord, about;
private final MainEditorWindow mainEditorWindow;
Expand Down Expand Up @@ -124,6 +124,10 @@ private JMenu createEditMenu(TAS parent, MainEditorWindow mainEditorWindow){
deleteLines.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
deleteLines.addActionListener(e -> getActiveScriptTab().getPianoRoll().deleteSelectedRows());

selectLines = editJMenu.add("Select lines");
selectLines.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK));
selectLines.addActionListener(e -> mainEditorWindow.selectLines());

addLine = editJMenu.add("Add line");
addLine.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
addLine.addActionListener(e -> mainEditorWindow.addEmptyRow());
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/github/jadefalke2/components/PianoRoll.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,13 @@ public void cut(){
deleteSelectedRows();
}

public void setSelectedRows(int[] selectedLines) {
clearSelection();
for (int selectedLine : selectedLines) {
addRowSelectionInterval(selectedLine, selectedLine);
}

if(selectedLines.length != 0)
scrollRectToVisible(getCellRect(selectedLines[0], 0, false));
}
}
7 changes: 6 additions & 1 deletion src/main/java/io/github/jadefalke2/components/ScriptTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ScriptTab extends JPanel {

private final MainEditorWindow mainEditorWindow;
private final Script script;
private final JScrollPane scrollPane;
private final PianoRoll pianoRoll;
private final SideJoystickPanel sideJoystickPanel;

Expand Down Expand Up @@ -47,7 +48,7 @@ public ScriptTab(MainEditorWindow mainEditorWindow, Script script, ObservablePro
GridBagConstraints c = new GridBagConstraints();

//Components
JScrollPane scrollPane = new JScrollPane(pianoRoll);
scrollPane = new JScrollPane(pianoRoll);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

c.fill = GridBagConstraints.BOTH;
Expand Down Expand Up @@ -203,4 +204,8 @@ public void cleanup() {
public Script getScript() {
return script;
}

public void setSelectedLines(int[] selectedLines) {
pianoRoll.setSelectedRows(selectedLines);
}
}
116 changes: 116 additions & 0 deletions src/main/java/io/github/jadefalke2/components/SelectLinesDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package io.github.jadefalke2.components;

import io.github.jadefalke2.util.SimpleDocumentListener;

import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SelectLinesDialog extends JDialog {
private static final Pattern pattern = Pattern.compile("(\\d+)(?:-(\\d+))?(?:,\\s*|$)");

private final int scriptLength;

private final JTextField input;
private boolean accepted = false;
public SelectLinesDialog(Window owner, int scriptLength) {
super(owner, "Select Lines", ModalityType.APPLICATION_MODAL);
this.scriptLength = scriptLength;

getRootPane().registerKeyboardAction(e -> dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)), KeyStroke.getKeyStroke(
KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

JButton okButton = new JButton("OK");
ActionListener accept = e -> {
if(!isValidInput()) return;
accepted = true;
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
};

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 15, 10, 15));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipadx = 15;
c.insets = new Insets(5, 0, 0, 0);
c.weighty = 1;
c.weightx = 1;
c.gridx = 0;
c.gridy = 0;

input = new JTextField();
input.getDocument().addDocumentListener((SimpleDocumentListener) e -> {
okButton.setEnabled(isValidInput());
});
input.addActionListener(accept);
input.setToolTipText("Examples: \"34\", \"120, 35\", \"5-20\" or \"120-150, 155\"");
mainPanel.add(input, c);

c.gridy++;
okButton.addActionListener(accept);
mainPanel.add(okButton, c);

add(mainPanel);
setLocationRelativeTo(null);
pack();
}

public boolean isAccepted() {
return accepted;
}

private boolean isValidInput() {
String labelText = input.getText();
Matcher matcher = pattern.matcher(labelText);
StringBuilder collected = new StringBuilder();
try {
while(matcher.find()) {
collected.append(matcher.group());

int start = Integer.parseInt(matcher.group(1));
if(start >= scriptLength)
return false;

if(matcher.group(2) != null) {
int end = Integer.parseInt(matcher.group(2));
if(end >= scriptLength)
return false;
if(start > end)
return false;
}
}
} catch(NumberFormatException e) {
return false;
}
return collected.toString().equals(labelText);
}

public int[] getSelectedLines() {
if(!isValidInput()) return null;

Matcher matcher = pattern.matcher(input.getText());
ArrayList<Integer> list = new ArrayList<>();
while(matcher.find()) {
if(matcher.group(2) != null) {
int start = Integer.parseInt(matcher.group(1));
int end = Integer.parseInt(matcher.group(2));
list.addAll(IntStream.rangeClosed(start, end).boxed().collect(Collectors.toList()));
} else {
list.add(Integer.parseInt(matcher.group(1)));
}
}

return list.stream().mapToInt(i -> i).toArray();
}
}

0 comments on commit bdc0eeb

Please sign in to comment.