Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Apr 18, 2024
2 parents d5e6bc8 + ad697e6 commit f5c0ad9
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repositories {

dependencies {
include "net.raphimc:NoteBlockLib:2.0.6"
include "com.formdev:flatlaf:3.4"
include "com.formdev:flatlaf:3.4.1"
include "net.lenni0451.commons:swing:1.5.0"
include "org.lwjgl:lwjgl:3.3.3"
include "org.lwjgl:lwjgl-openal:3.3.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,33 @@
package net.raphimc.noteblocktool.elements;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class FastScrollPane extends JScrollPane {

private final Border defaultBorder = this.getBorder();

public FastScrollPane() {
}

public FastScrollPane(final Component view) {
super(view);
}

{
this.setBorder(BorderFactory.createEmptyBorder());
}

public FastScrollPane setDefaultBorder() {
this.setBorder(this.defaultBorder);
return this;
}

public Border getDefaultBorder() {
return this.defaultBorder;
}

@Override
public JScrollBar createVerticalScrollBar() {
JScrollBar scrollBar = super.createVerticalScrollBar();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/raphimc/noteblocktool/elements/NewLineLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* This file is part of NoteBlockTool - https://github.com/RaphiMC/NoteBlockTool
* Copyright (C) 2022-2024 RK_01/RaphiMC and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.noteblocktool.elements;

import javax.swing.*;

public class NewLineLabel extends JLabel {

public NewLineLabel(final String text) {
super("<html>" + text.replace("\n", "<br>") + "</html>");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.raphimc.noteblocklib.format.nbs.model.NbsHeader;
import net.raphimc.noteblocklib.model.Song;
import net.raphimc.noteblocklib.model.SongView;
import net.raphimc.noteblocktool.elements.FastScrollPane;
import net.raphimc.noteblocktool.elements.NoteBlockFileFilter;
import net.raphimc.noteblocktool.elements.TextOverlayPanel;
import net.raphimc.noteblocktool.elements.drag.DragTable;
Expand Down Expand Up @@ -77,7 +78,7 @@ private void initComponents() {
root.setLayout(new BorderLayout());
this.setContentPane(root);

root.add(new JScrollPane(this.table), BorderLayout.CENTER);
root.add(new FastScrollPane(this.table), BorderLayout.CENTER);
this.dropTarget = new DropTarget(this, new DragTableDropTargetListener(this, this::load));
this.table.getSelectionModel().addListSelectionListener(e -> this.refreshButtons());
this.table.addKeyListener(new KeyAdapter() {
Expand Down
63 changes: 42 additions & 21 deletions src/main/java/net/raphimc/noteblocktool/frames/SongPlayerFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package net.raphimc.noteblocktool.frames;

import net.lenni0451.commons.swing.GBC;
import net.lenni0451.commons.swing.components.ScrollPaneSizedPanel;
import net.raphimc.noteblocklib.format.nbs.NbsDefinitions;
import net.raphimc.noteblocklib.format.nbs.NbsSong;
import net.raphimc.noteblocklib.format.nbs.model.NbsNote;
Expand All @@ -32,6 +33,8 @@
import net.raphimc.noteblocklib.util.SongResampler;
import net.raphimc.noteblocktool.audio.JavaxSoundSystem;
import net.raphimc.noteblocktool.audio.OpenALSoundSystem;
import net.raphimc.noteblocktool.elements.FastScrollPane;
import net.raphimc.noteblocktool.elements.NewLineLabel;

import javax.swing.*;
import java.awt.*;
Expand All @@ -44,9 +47,11 @@

public class SongPlayerFrame extends JFrame implements ISongPlayerCallback {

private static final String UNAVAILABLE_MESSAGE = "Your system does not support any sound system.\nPlaying songs is not supported.";
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");
private static SongPlayerFrame instance;
private static SoundSystem forcedSoundSystem;
private static boolean songPlayerUnavailable;
private static Point lastPosition;
private static int lastMaxSounds = 512;
private static int lastVolume = 50;
Expand All @@ -63,6 +68,10 @@ public static void open(final ListFrame.LoadedSong song, final SongView<?> view)
instance.dispose();
}
instance = new SongPlayerFrame(song, view);
if (songPlayerUnavailable) {
JOptionPane.showMessageDialog(instance, UNAVAILABLE_MESSAGE, "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (lastPosition != null) instance.setLocation(lastPosition);
instance.maxSoundsSpinner.setValue(lastMaxSounds);
instance.volumeSlider.setValue(lastVolume);
Expand Down Expand Up @@ -143,47 +152,50 @@ private void initComponents() {
this.volumeSlider.setMajorTickSpacing(25);
this.volumeSlider.setMinorTickSpacing(5);
this.volumeSlider.addChangeListener(e -> {
if (songPlayerUnavailable) return;
this.volume = this.volumeSlider.getValue() / 100F;
if (this.soundSystem.equals(SoundSystem.OPENAL)) OpenALSoundSystem.setMasterVolume(this.volume);
lastVolume = this.volumeSlider.getValue();
});
});
}
{ //Center Panel
final JPanel centerPanel = new JPanel();
final JScrollPane centerScrollPane = new FastScrollPane();
final JPanel centerPanel = new ScrollPaneSizedPanel(centerScrollPane);
centerScrollPane.setViewportView(centerPanel);
centerPanel.setLayout(new GridBagLayout());
root.add(centerPanel, BorderLayout.CENTER);
root.add(centerScrollPane, BorderLayout.CENTER);

int gridy = 0;
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Title:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new JLabel(this.songPlayer.getSongView().getTitle()));
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.NORTHWEST).add(new JLabel("Title:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new NewLineLabel(this.songPlayer.getSongView().getTitle()));

Optional<String> author = this.song.getAuthor();
if (author.isPresent()) {
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Author:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new JLabel(author.get()));
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.NORTHWEST).add(new JLabel("Author:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new NewLineLabel(author.get()));
}

Optional<String> originalAuthor = this.song.getOriginalAuthor();
if (originalAuthor.isPresent()) {
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Original Author:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new JLabel(originalAuthor.get()));
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.NORTHWEST).add(new JLabel("Original Author:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new NewLineLabel(originalAuthor.get()));
}

Optional<String> description = this.song.getDescription();
if (description.isPresent()) {
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Description:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new JLabel(description.get()));
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.NORTHWEST).add(new JLabel("Description:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new NewLineLabel(description.get()));
}

GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Length:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new JLabel(this.song.getLength(this.songPlayer.getSongView())));
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.NORTHWEST).add(new JLabel("Length:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new NewLineLabel(this.song.getLength(this.songPlayer.getSongView())));

GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Note count:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new JLabel(DECIMAL_FORMAT.format(this.song.getNoteCount(this.songPlayer.getSongView()))));
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.NORTHWEST).add(new JLabel("Note count:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new NewLineLabel(DECIMAL_FORMAT.format(this.song.getNoteCount(this.songPlayer.getSongView()))));

GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Speed:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new JLabel(DECIMAL_FORMAT.format(this.songPlayer.getSongView().getSpeed())));
GBC.create(centerPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.NORTHWEST).add(new JLabel("Speed:"));
GBC.create(centerPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(new NewLineLabel(DECIMAL_FORMAT.format(this.songPlayer.getSongView().getSpeed())));

GBC.fillVerticalSpace(centerPanel);
}
Expand All @@ -197,6 +209,7 @@ private void initComponents() {

GBC.create(southPanel).grid(0, gridy++).insets(5, 5, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(this.progressSlider, () -> {
this.progressSlider.addChangeListener(e -> {
if (songPlayerUnavailable) return;
//Skip updates if the value is set directly
if (!this.progressSlider.getValueIsAdjusting()) return;
if (!this.songPlayer.isRunning()) {
Expand All @@ -211,6 +224,7 @@ private void initComponents() {
buttonPanel.setLayout(new GridLayout(1, 3, 5, 0));
buttonPanel.add(this.playStopButton);
this.playStopButton.addActionListener(e -> {
if (songPlayerUnavailable) return;
if (this.songPlayer.isRunning()) {
this.songPlayer.stop();
this.songPlayer.setTick(0);
Expand All @@ -226,11 +240,17 @@ private void initComponents() {
} catch (Throwable t) {
t.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to initialize the " + this.soundSystem.getName() + " sound system:\n" + t.getClass().getSimpleName() + ": " + t.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
this.soundSystem = SoundSystem.values()[(this.soundSystem.ordinal() + 1) % SoundSystem.values().length];
this.soundSystem.init((int) this.maxSoundsSpinner.getValue());
forcedSoundSystem = this.soundSystem;
this.soundSystemComboBox.setEnabled(false);
this.soundSystemComboBox.setSelectedIndex(this.soundSystem.ordinal());
try {
this.soundSystem = SoundSystem.values()[(this.soundSystem.ordinal() + 1) % SoundSystem.values().length];
this.soundSystem.init((int) this.maxSoundsSpinner.getValue());
forcedSoundSystem = this.soundSystem;
this.soundSystemComboBox.setEnabled(false);
this.soundSystemComboBox.setSelectedIndex(this.soundSystem.ordinal());
} catch (Throwable ex) {
ex.printStackTrace();
songPlayerUnavailable = true;
return;
}
}
if (this.soundSystem.equals(SoundSystem.OPENAL)) OpenALSoundSystem.setMasterVolume(this.volume);
this.songPlayer.play();
Expand Down Expand Up @@ -293,6 +313,7 @@ private void tick() {

@Override
public void playNote(Note note) {
if (songPlayerUnavailable) return;
if (note.getInstrument() >= Instrument.values().length) return;

final float volume;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.raphimc.noteblocklib.model.SongView;
import net.raphimc.noteblocklib.util.Instrument;
import net.raphimc.noteblocklib.util.SongUtil;
import net.raphimc.noteblocktool.elements.FastScrollPane;
import net.raphimc.noteblocktool.elements.instruments.InstrumentsTable;
import net.raphimc.noteblocktool.frames.ListFrame;

Expand All @@ -43,7 +44,7 @@ protected void initComponents(JPanel center) {
this.removeAll();

this.table = new InstrumentsTable(true);
this.add(new JScrollPane(this.table));
this.add(new FastScrollPane(this.table));
this.usedInstruments = SongUtil.getUsedCustomInstruments(this.songs.get(0).getSong().getView());
NbsData data = (NbsData) this.songs.get(0).getSong().getData();
for (Integer instrument : this.usedInstruments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public EditTab(final String title, final List<ListFrame.LoadedSong> songs) {
this.songs = songs;

this.setLayout(new BorderLayout());
JScrollPane scrollPane = new FastScrollPane();
JScrollPane scrollPane = new FastScrollPane().setDefaultBorder();
this.center = new ScrollPaneSizedPanel(scrollPane);
this.center.setLayout(new VerticalLayout(5, 5));
scrollPane.setViewportView(this.center);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.raphimc.noteblocklib.model.SongView;
import net.raphimc.noteblocklib.util.Instrument;
import net.raphimc.noteblocklib.util.SongUtil;
import net.raphimc.noteblocktool.elements.FastScrollPane;
import net.raphimc.noteblocktool.elements.instruments.InstrumentsTable;
import net.raphimc.noteblocktool.frames.ListFrame;

Expand All @@ -41,7 +42,7 @@ protected void initComponents(JPanel center) {
this.removeAll();

this.table = new InstrumentsTable(false);
this.add(new JScrollPane(this.table));
this.add(new FastScrollPane(this.table));
this.usedInstruments = this.songs.stream()
.map(song -> SongUtil.getUsedVanillaInstruments(song.getSong().getView()))
.reduce(EnumSet.noneOf(Instrument.class), (a, b) -> {
Expand Down

0 comments on commit f5c0ad9

Please sign in to comment.