AppController.java (2245B)
1 package nl.isygameclient.controllers; 2 3 import javafx.event.ActionEvent; 4 import javafx.fxml.FXML; 5 import javafx.fxml.FXMLLoader; 6 import javafx.fxml.Initializable; 7 import javafx.scene.layout.BorderPane; 8 import javafx.scene.layout.VBox; 9 import nl.isygameclient.util.StageHandler; 10 import nl.isygameclient.views.NavButtonControl; 11 12 import java.io.IOException; 13 import java.net.URL; 14 import java.util.Map; 15 import java.util.ResourceBundle; 16 import java.util.TreeMap; 17 18 public class AppController implements Initializable { 19 20 private final TreeMap<Integer, NavButtonControl> navButtons = new TreeMap<>(Map.ofEntries( 21 Map.entry(0, new NavButtonControl("Store", "LOCAL_MALL", null)), 22 Map.entry(1, new NavButtonControl("Library", "GAMES", "/nl/isygameclient/views/game-library.fxml")), 23 Map.entry(2, new NavButtonControl("Community", "QUESTION_ANSWER", null)), 24 Map.entry(3, new NavButtonControl("Servers", "FORMAT_LIST_BULLETED", null)) 25 )); 26 private NavButtonControl activeButton; 27 @FXML 28 public BorderPane app; 29 @FXML 30 private VBox navBar; 31 32 @Override 33 public void initialize(URL url, ResourceBundle resourceBundle) { 34 navButtons.forEach((index, button) -> { 35 button.setOnAction(this::onNavButtonPress); 36 navBar.getChildren().add(button); 37 }); 38 39 activeButton = navButtons.get(1); 40 activeButton.activate(); 41 loadScene(activeButton.getSource()); 42 } 43 44 @FXML 45 public void onNavButtonPress(ActionEvent e) { 46 var newButton = (NavButtonControl) e.getSource(); 47 activeButton.deactivate(); 48 activeButton = newButton; 49 newButton.activate(); 50 51 loadScene(activeButton.getSource()); 52 } 53 54 @FXML 55 public void onSettingsButtonPressed(){ 56 loadScene("/nl/isygameclient/views/app-settings.fxml"); 57 } 58 59 private void loadScene(String path) { 60 app.getChildren().remove(app.getCenter()); 61 62 if (path == null) return; 63 64 URL url = StageHandler.class.getResource(path); 65 FXMLLoader fxmlLoader = new FXMLLoader(url); 66 try { 67 app.setCenter(fxmlLoader.load()); 68 } catch (IOException e) { 69 e.printStackTrace(); 70 } 71 } 72 }