Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronny12301 committed Feb 4, 2023
2 parents 432513f + 1397583 commit 898ed16
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 35 deletions.
102 changes: 92 additions & 10 deletions src/main/java/controllers/MediaPlayerFXMLController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ResourceBundle;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
Expand All @@ -22,11 +23,13 @@
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
Expand All @@ -45,6 +48,9 @@
*/
public class MediaPlayerFXMLController implements Initializable {

@FXML
private BorderPane mainScene;

@FXML
private Text fileNameText;

Expand Down Expand Up @@ -96,6 +102,8 @@ public class MediaPlayerFXMLController implements Initializable {
private ListView playList;
@FXML
private Label lblPlaylist;
@FXML
private TextField searchPlaylist;

@FXML
private ToggleButton nowPlayingButton;
Expand All @@ -120,31 +128,32 @@ public class MediaPlayerFXMLController implements Initializable {
public void initialize(URL url, ResourceBundle rb) {
visualSyncProgressSliderBar(progressSlider, progressBar);
visualSyncProgressSliderBar(volumeSlider, volumeBar);
searchPlaylistItems();

smallAlbumArt.fitHeightProperty().bind(smallAlbumArtBackground.heightProperty());
smallAlbumArt.fitWidthProperty().bind(smallAlbumArtBackground.widthProperty());

// Play tracks in the playlist List using the mouse
playList.setOnMouseClicked((MouseEvent click) -> {

if (click.getClickCount() == 2 && !playList.getItems().isEmpty()) {
//Use ListView's getSelected Item
fileNumber = playList.getSelectionModel().getSelectedIndex();

updateNextPreviousButtonsState();
initiateMediaPlayer(filePlayList.keySet().toArray()[fileNumber].toString());
playSelectedItem();
}
});
// same but with Enter key
playList.setOnKeyPressed((key) -> {
if (key.getCode() == KeyCode.ENTER && !playList.getItems().isEmpty()) {
fileNumber = playList.getSelectionModel().getSelectedIndex();

updateNextPreviousButtonsState();
initiateMediaPlayer(filePlayList.keySet().toArray()[fileNumber].toString());

playSelectedItem();
}
});
}
private void playSelectedItem() {
//Use ListView's getSelected Item
fileNumber = playList.getSelectionModel().getSelectedIndex();

updateNextPreviousButtonsState();
initiateMediaPlayer(filePlayList.keySet().toArray()[fileNumber].toString());
}


public void nextButtonClicked(ActionEvent e) {
Expand Down Expand Up @@ -177,6 +186,7 @@ public void previousButtonClicked(ActionEvent e) {
}
initiateMediaPlayer(filePlayList.keySet().toArray()[fileNumber].toString());
playList.getSelectionModel().select(fileNumber);
playList.scrollTo(fileNumber);
playList.getFocusModel().focus(oldValue);

}
Expand Down Expand Up @@ -297,6 +307,57 @@ public void nowPlayingButtonClicked(ActionEvent e) {
}
}

public void searchPlaylistItems() {

mainScene.setOnKeyTyped((t) -> {
// escape, backspace, enter
if (t.getCharacter().equals("\u001B") || t.getCharacter().equals("\b") || t.getCharacter().equals("\n") || t.getCharacter().equals("\r")) {
return;
}

if (searchPlaylist.getText().isBlank()) {
searchPlaylist.setVisible(true);
}

searchPlaylist.setText(t.getCharacter());
searchPlaylist.requestFocus();
searchPlaylist.positionCaret(searchPlaylist.getLength());
});

searchPlaylist.setOnKeyPressed((t) -> {
if (t.getCode().equals(KeyCode.ESCAPE) || t.getCode().equals(KeyCode.ENTER)) {
searchPlaylist.clear();
searchPlaylist.setVisible(false);
}


// not being backspace avoids lag when removing al text, holding backspace
if (!playList.getItems().isEmpty() && !t.getCode().equals(KeyCode.BACK_SPACE)) {
int index = findKeyIndex(filePlayList, searchPlaylist.getText());
if (index != -1) {
playList.requestFocus();
playList.getSelectionModel().select(index);
playList.scrollTo(index);
searchPlaylist.requestFocus();
searchPlaylist.positionCaret(searchPlaylist.getLength());
}

if (t.getCode().equals(KeyCode.ENTER)) {
playSelectedItem();
searchPlaylist.setVisible(false);
}
}


});
}







public void getAllFiles(File directory) {
files = directory.listFiles();

Expand Down Expand Up @@ -579,8 +640,10 @@ private void playNextMedia() {

if (randomButton.isSelected()) {
fileNumber = new Random().nextInt(filePlayList.size());
playList.scrollTo(fileNumber);
}
else {
playList.scrollTo(oldValue);
fileNumber++;
}
initiateMediaPlayer(filePlayList.keySet().toArray()[fileNumber].toString());
Expand All @@ -589,6 +652,25 @@ private void playNextMedia() {
}


public static int findKeyIndex(Map<File, String> map, String searchString) {
ArrayList<File> keys = new ArrayList<>(map.keySet());
if (searchString == null || searchString.isEmpty()) {
return -1;
}
System.out.println("Keys list size: " + keys.size());
for (File key : keys) {
System.out.println("Checking key: " + key.toString());
System.out.println("Search string: " + searchString);
if (key.toString().toLowerCase().contains(searchString.toLowerCase())) {
System.out.println("Found match: " + key.toString());
return keys.indexOf(key);
}
}
System.out.println("No match found");
return -1;
}


private void setStageName(String track) {
wmediaplayer.WMediaPlayer.getStage().setTitle(track + " - Media Player 11");
}
Expand Down
56 changes: 31 additions & 25 deletions src/main/resources/wmediaplayer/MediaPlayerFXML.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
Expand All @@ -22,18 +23,18 @@
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="450.0" prefWidth="700.0" style="-fx-background-color: #000000;" styleClass="root" stylesheets="@../styles/MediaPlayerStyles.css" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.MediaPlayerFXMLController">
<BorderPane fx:id="mainScene" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="450.0" prefWidth="700.0" style="-fx-background-color: #000000;" styleClass="root" stylesheets="@../styles/MediaPlayerStyles.css" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.MediaPlayerFXMLController">
<top>
<HBox alignment="CENTER_LEFT" styleClass="nav-bar" stylesheets="@../styles/MediaPlayerStyles.css" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="36.0" prefWidth="71.0" styleClass="background-btns" stylesheets="@../styles/MediaPlayerStyles.css" HBox.hgrow="NEVER">
<children>
<Button id="arrow-back" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" styleClass="circular-buttons" stylesheets="@../styles/MediaPlayerStyles.css" textAlignment="CENTER" textFill="WHITE" HBox.hgrow="NEVER">
<Button id="arrow-back" alignment="CENTER" contentDisplay="CENTER" disable="true" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" styleClass="circular-buttons" stylesheets="@../styles/MediaPlayerStyles.css" textAlignment="CENTER" textFill="WHITE" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="1.0" />
</HBox.margin>
</Button>
<Button id="arrow-forward" alignment="CENTER" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" styleClass="circular-buttons" stylesheets="@../styles/MediaPlayerStyles.css" textFill="WHITE">
<Button id="arrow-forward" alignment="CENTER" disable="true" mnemonicParsing="false" prefHeight="30.0" prefWidth="30.0" styleClass="circular-buttons" stylesheets="@../styles/MediaPlayerStyles.css" textFill="WHITE">
<HBox.margin>
<Insets bottom="1.0" left="4.0" />
</HBox.margin>
Expand Down Expand Up @@ -77,27 +78,23 @@
<center>
<SplitPane dividerPositions="0.7217956019061831" minHeight="0.0" prefHeight="160.0" prefWidth="200.0" style="-fx-background-color: black;" BorderPane.alignment="CENTER">
<items>
<StackPane SplitPane.resizableWithParent="false">
<VBox fx:id="screenBackground" alignment="CENTER" fillWidth="false" minHeight="0.0" SplitPane.resizableWithParent="false">
<children>
<VBox fx:id="screenBackground" alignment="CENTER" fillWidth="false" minHeight="0.0">
<StackPane>
<VBox.margin>
<Insets />
</VBox.margin>
<children>
<StackPane>
<VBox.margin>
<Insets />
</VBox.margin>
<children>
<MediaView fx:id="mediaView" fitHeight="1.0" fitWidth="1.0" rotate="90.0">
<viewport>
<Rectangle2D />
</viewport>
</MediaView>
<ImageView fx:id="imageView" fitHeight="1.0" fitWidth="1.0" pickOnBounds="true" preserveRatio="true" />
</children>
</StackPane>
<MediaView fx:id="mediaView" fitHeight="1.0" fitWidth="1.0" rotate="90.0">
<viewport>
<Rectangle2D />
</viewport>
</MediaView>
<ImageView fx:id="imageView" fitHeight="1.0" fitWidth="1.0" pickOnBounds="true" preserveRatio="true" />
</children>
</VBox>
</StackPane>
</children>
</StackPane>
</VBox>
<SplitPane dividerPositions="0.35" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0" styleClass="split-pane-divider" stylesheets="@../styles/MediaPlayerStyles.css" SplitPane.resizableWithParent="false">
<items>
<VBox alignment="BOTTOM_CENTER" minWidth="0.0" style="-fx-background-color: #F3F0FA;" SplitPane.resizableWithParent="false">
Expand Down Expand Up @@ -130,11 +127,20 @@
<Insets bottom="3.0" />
</VBox.margin>
</Label>
<ListView fx:id="playList" minWidth="0.0" style="-fx-background-color: #F3F0FA;">
<padding>
<Insets bottom="4.0" />
</padding>
</ListView>
<StackPane>
<children>
<ListView fx:id="playList" minWidth="0.0" style="-fx-background-color: #F3F0FA;">
<padding>
<Insets bottom="4.0" />
</padding>
</ListView>
<TextField fx:id="searchPlaylist" promptText="search" visible="false" StackPane.alignment="BOTTOM_RIGHT">
<StackPane.margin>
<Insets bottom="3.0" />
</StackPane.margin>
</TextField>
</children>
</StackPane>
</children>
</VBox>
</items>
Expand Down

0 comments on commit 898ed16

Please sign in to comment.