Advertisement
Guest User

Menu.java

a guest
Sep 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.98 KB | None | 0 0
  1. package com.cottonlesergal.nubot.app.controllers;
  2.  
  3. import com.cottonlesergal.nubot.bot.BotRunner;
  4. import com.cottonlesergal.nubot.bot.modules.PropertyChange;
  5. import com.cottonlesergal.nubot.main.modules.DebugPrinter;
  6. import javafx.application.Platform;
  7. import javafx.beans.property.SimpleStringProperty;
  8. import javafx.beans.property.StringProperty;
  9. import javafx.collections.ObservableList;
  10. import javafx.concurrent.Task;
  11. import javafx.geometry.Rectangle2D;
  12. import javafx.scene.control.Label;
  13. import javafx.scene.control.ProgressBar;
  14. import javafx.scene.effect.BlurType;
  15. import javafx.scene.effect.DropShadow;
  16. import javafx.scene.image.Image;
  17. import javafx.scene.image.ImageView;
  18. import javafx.scene.input.MouseEvent;
  19. import javafx.scene.layout.AnchorPane;
  20. import javafx.scene.paint.Color;
  21. import javafx.scene.paint.ImagePattern;
  22. import javafx.scene.shape.Circle;
  23. import javafx.stage.Screen;
  24. import javafx.stage.Stage;
  25. import net.dv8tion.jda.api.JDA;
  26. import net.dv8tion.jda.api.events.ReadyEvent;
  27. import net.dv8tion.jda.api.hooks.EventListener;
  28. import net.dv8tion.jda.api.hooks.ListenerAdapter;
  29. import com.jfoenix.controls.JFXButton;
  30.  
  31. import java.beans.PropertyChangeListener;
  32. import java.beans.PropertyChangeSupport;
  33. import java.net.URL;
  34. import java.util.ArrayList;
  35. import java.util.ResourceBundle;
  36. import java.util.concurrent.Executors;
  37. import java.util.concurrent.ScheduledExecutorService;
  38. import java.util.concurrent.TimeUnit;
  39.  
  40. import javafx.fxml.FXML;
  41.  
  42. public class Menu extends ListenerAdapter {
  43.  
  44.     @FXML
  45.     private ResourceBundle resources;
  46.  
  47.     @FXML
  48.     private URL location;
  49.  
  50.     @FXML
  51.     private AnchorPane moveablePane;
  52.  
  53.     @FXML
  54.     private AnchorPane profileCard;
  55.  
  56.     @FXML
  57.     private JFXButton loginManageButton;
  58.  
  59.     @FXML
  60.     private AnchorPane usernameAndDiscriminatorPane;
  61.  
  62.     @FXML
  63.     private Label discriminatorLabel;
  64.  
  65.     @FXML
  66.     private Label usernameLabel;
  67.  
  68.     @FXML
  69.     private Circle userAvatar;
  70.  
  71.     @FXML
  72.     private AnchorPane userStats;
  73.  
  74.     @FXML
  75.     private AnchorPane signInText;
  76.  
  77.     @FXML
  78.     private JFXButton profileSettingButton;
  79.  
  80.     @FXML
  81.     private ImageView close;
  82.  
  83.     @FXML
  84.     private ImageView maximize;
  85.  
  86.     @FXML
  87.     private ImageView minimize;
  88.  
  89.     @FXML
  90.     private Label ShardInfoLabel;
  91.  
  92.     @FXML
  93.     private Label PingLabel;
  94.  
  95.     private DebugPrinter debug = new DebugPrinter();
  96.  
  97.     private double[] scale = new double[4];
  98.  
  99.     private boolean avatarDynamicBackdropIsOn = true;
  100.  
  101.     private double xOffset = 0;
  102.  
  103.     private double yOffset = 0;
  104.  
  105.     @FXML
  106.     private StringProperty ShardStatus = new SimpleStringProperty();
  107.  
  108.     private String TestText = "";
  109.  
  110.     PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
  111.  
  112.     @FXML
  113.     void initialize() {
  114.  
  115.         assert moveablePane != null : "fx:id=\"moveablePane\" was not injected: check your FXML file 'Menu.fxml'.";
  116.         assert profileCard != null : "fx:id=\"profileCard\" was not injected: check your FXML file 'Menu.fxml'.";
  117.         assert loginManageButton != null : "fx:id=\"loginManageButton\" was not injected: check your FXML file 'Menu.fxml'.";
  118.         assert usernameAndDiscriminatorPane != null : "fx:id=\"usernameAndDiscriminatorPane\" was not injected: check your FXML file 'Menu.fxml'.";
  119.         assert discriminatorLabel != null : "fx:id=\"discriminatorLabel\" was not injected: check your FXML file 'Menu.fxml'.";
  120.         assert usernameLabel != null : "fx:id=\"usernameLabel\" was not injected: check your FXML file 'Menu.fxml'.";
  121.         assert userAvatar != null : "fx:id=\"userAvatar\" was not injected: check your FXML file 'Menu.fxml'.";
  122.         assert userStats != null : "fx:id=\"userStats\" was not injected: check your FXML file 'Menu.fxml'.";
  123.         assert signInText != null : "fx:id=\"signInText\" was not injected: check your FXML file 'Menu.fxml'.";
  124.         assert profileSettingButton != null : "fx:id=\"profileSettingButton\" was not injected: check your FXML file 'Menu.fxml'.";
  125.         assert close != null : "fx:id=\"close\" was not injected: check your FXML file 'Menu.fxml'.";
  126.         assert maximize != null : "fx:id=\"maximize\" was not injected: check your FXML file 'Menu.fxml'.";
  127.         assert minimize != null : "fx:id=\"minimize\" was not injected: check your FXML file 'Menu.fxml'.";
  128.         assert ShardInfoLabel != null : "fx:id=\"ShardInfoLabel\" was not injected: check your FXML file 'Menu.fxml'.";
  129.         assert PingLabel != null : "fx:id=\"PingLabel\" was not injected: check your FXML file 'Menu.fxml'.";
  130.         ShardInfoLabel.setText("Loading...");
  131.         //ShardInfoLabel.textProperty().bind(ShardStatus);
  132.         setUserAvatar("/media/images/CottonIconFixed.png");
  133.         //userStats.setVisible(false);
  134.         //profileSettingButton.setVisible(false);
  135.         loginManageButton.setText("Log Out");
  136.         usernameAndDiscriminatorPane.setEffect(new DropShadow(+15d, 0d, +1d, Color.rgb(22, 30, 45, 0.46)));
  137.         /*setShardInfoLabelText("Yo, wtf");*/
  138.  
  139.  
  140.         ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
  141.         exec.scheduleAtFixedRate(() -> {
  142.             System.out.println("BoundEntity2: " + TestText);
  143.             ShardInfoLabel.setText(ShardStatus.toString().replace("StringProperty [value:", "").replace("]", ""));
  144.  
  145.         }, 0, 5, TimeUnit.SECONDS);
  146.     }
  147.  
  148.     @FXML
  149.     @Override
  150.     public void onReady(ReadyEvent event) {
  151.         super.onReady(event);
  152.         //System.out.println((event.getJDA().getShardInfo().getShardId() + 1) + " of " + event.getJDA().getShardInfo().getShardTotal() + " shards online");
  153.         new Thread(() -> {
  154.             System.out.println("Running a new Thread!!!!");
  155.             Platform.runLater(() -> {
  156.                 //ShardInfoLabel.setText((event.getJDA().getShardInfo().getShardId() + 1) + " of " + event.getJDA().getShardInfo().getShardTotal() + " shards online");
  157.                 if(!((event.getJDA().getShardInfo().getShardId() + 1) + " of " + event.getJDA().getShardInfo().getShardTotal() + " shards online").toLowerCase().equals("StringProperty [value: null]")) {
  158.                     ShardStatus.set((event.getJDA().getShardInfo().getShardId() + 1) + " of " + event.getJDA().getShardInfo().getShardTotal() + " shards online");
  159.                     TestText = ShardStatus.toString().replace("StringProperty [value:", "").replace("]", "");
  160.                     System.out.println("THIS IS WHAT TEXT SHOULD BE: " + TestText);
  161.                 }
  162.                 //System.out.println("Running a new Thread!!!! 222222222222222222");
  163.             });
  164.         }).start();
  165.  
  166.         System.out.println("BoundEntity1: " + ShardStatus);
  167.     }
  168.  
  169.     @FXML
  170.     void setShardInfoLabelText(ReadyEvent event){
  171.         ShardInfoLabel.setText((event.getJDA().getShardInfo().getShardId() + 1) + " of " + event.getJDA().getShardInfo().getShardTotal() + " shards online");
  172.     }
  173.  
  174.     @FXML
  175.     void closeApplication(MouseEvent event) {
  176.         System.exit(0);
  177.         Stage stage = (Stage) close.getScene().getWindow();
  178.         stage.close();
  179.     }
  180.  
  181.     @FXML
  182.     void minimizeApplication(MouseEvent event) {
  183.         Stage stage = (Stage) minimize.getScene().getWindow();
  184.         stage.setIconified(true);
  185.     }
  186.  
  187.     @FXML
  188.     void maximizeApplication(MouseEvent event){
  189.         Stage stage = (Stage) maximize.getScene().getWindow();
  190.         ObservableList<Screen> screens = Screen.getScreensForRectangle(new Rectangle2D(stage.getX(), stage.getY(), stage.getWidth(), stage.getHeight()));
  191.         Rectangle2D bounds = screens.get(0).getVisualBounds();
  192.         if((stage.getX() != bounds.getMinX()) && (stage.getY() != bounds.getMinY()) && (stage.getWidth() != bounds.getWidth()) && (stage.getHeight() != bounds.getHeight())) {
  193.             this.scale[0] = stage.getX();
  194.             this.scale[1] = stage.getY();
  195.             this.scale[2] = stage.getWidth();
  196.             this.scale[3] = stage.getHeight();
  197.  
  198.             stage.setX(bounds.getMinX());
  199.             stage.setY(bounds.getMinY());
  200.             stage.setWidth(bounds.getWidth());
  201.             stage.setHeight(bounds.getHeight());
  202.         }else {
  203.             stage.setX(this.scale[0]);
  204.             stage.setY(this.scale[1]);
  205.             stage.setWidth(this.scale[2]);
  206.             stage.setHeight(this.scale[3]);
  207.         }
  208.  
  209.     }
  210.  
  211.     @FXML
  212.     void getScene(MouseEvent event) {
  213.         xOffset = event.getSceneX();
  214.         yOffset = event.getSceneY();
  215.     }
  216.  
  217.     @FXML
  218.     void moveScene(MouseEvent event) {
  219.         Stage stage = (Stage) moveablePane.getScene().getWindow();
  220.         stage.setX(event.getScreenX() - xOffset);
  221.         stage.setY(event.getScreenY() - yOffset);
  222.     }
  223.  
  224.     public String getShardInfoLabelText() {
  225.         return this.ShardInfoLabel.getText();
  226.     }
  227.  
  228.     public void setShardInfoLabelText(String text) {
  229.         changeSupport.firePropertyChange("ShardInfo", this.ShardInfoLabel.getText(), text);
  230.         this.ShardInfoLabel.setText(text);
  231.     }
  232.  
  233.     /*public String getCity() {
  234.         return city;
  235.     }
  236.  
  237.     public void setCity(String city) {
  238.         changeSupport.firePropertyChange("city", this.city, city);
  239.         this.city = city;
  240.     }*/
  241.  
  242.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  243.         changeSupport.addPropertyChangeListener(listener);
  244.     }
  245.  
  246.     public void toggleDynamicBackground(){
  247.         avatarDynamicBackdropIsOn = !avatarDynamicBackdropIsOn;
  248.     }
  249.  
  250.     public void setUserAvatar(String url){
  251.         userAvatar.setStroke(Color.rgb(240, 64, 192));
  252.         String image = getClass().getResource(url).toExternalForm();
  253.         Image avatar = new Image(image);
  254.         double size = (avatar.getHeight() > avatar.getWidth()) ? avatar.getHeight() : avatar.getWidth();
  255.         double newHeight = avatar.getHeight();
  256.         double newWidth = avatar.getWidth();
  257.         debug.print("Image size is: " + avatar.getHeight() + "x" + avatar.getWidth());
  258.         debug.println(" with the " + ((avatar.getHeight() > avatar.getWidth()) ? "height " : "width ") + "being the largest at: " + size + "px.");
  259.         debug.println("Image is " + ((size/100)) + " times the size it should be... " + ((size/100 > 0) ? "resizing" : "not resizing"));
  260.         if(avatar.getWidth()/100 > 0 && avatar.getHeight()/100 > 0){
  261.             newHeight = avatar.getHeight()/((avatar.getHeight()/100));
  262.             newWidth = avatar.getWidth()/((avatar.getWidth()/100));
  263.             debug.println("Image has been resized to the following: " + Math.floor(newHeight) + "x" + Math.floor(newWidth) + "px.");
  264.         }
  265.         Image im = new Image(image, Math.floor(newWidth), Math.floor(newHeight), true, true, false);
  266.         debug.println("Actual image size is: " + im.getHeight() + "x" + im.getWidth() + "px.");
  267.         userAvatar.styleProperty().setValue("-fx-background-size: cover;");
  268.         userAvatar.setFill(new ImagePattern(im));
  269.  
  270.  
  271.  
  272.         if(avatarDynamicBackdropIsOn) {
  273.             ArrayList<ArrayList<Double>> rgbValues = new ArrayList<>();
  274.             for (int heightIndex = 0; heightIndex < avatar.getHeight(); heightIndex++) {
  275.                 for (int widthIndex = 0; widthIndex < avatar.getWidth(); widthIndex++) {
  276.                     double red = avatar.getPixelReader().getColor(widthIndex, heightIndex).getRed();
  277.                     double green = avatar.getPixelReader().getColor(widthIndex, heightIndex).getGreen();
  278.                     double blue = avatar.getPixelReader().getColor(widthIndex, heightIndex).getBlue();
  279.                     ArrayList<Double> colorVals = new ArrayList<>();
  280.                     colorVals.add(red*255);
  281.                     colorVals.add(green*255);
  282.                     colorVals.add(blue*255);
  283.                     rgbValues.add(colorVals);
  284.                 }
  285.             }
  286.             int newRed = 0;
  287.             int newGreen = 0;
  288.             int newBlue = 0;
  289.             int sumOfColors = 0;
  290.             for (ArrayList<Double> rgbValue : rgbValues) {
  291.                 if(!((rgbValue.get(0) > 220 || rgbValue.get(0) < 30) && (rgbValue.get(1) > 220 || rgbValue.get(1) < 30) && (rgbValue.get(2) > 220 || rgbValue.get(2) < 30))){
  292.                     sumOfColors++;
  293.                     newRed += rgbValue.get(0);
  294.                     newGreen += rgbValue.get(1);
  295.                     newBlue += rgbValue.get(2);
  296.                 }
  297.             }
  298.             newRed = newRed/((sumOfColors != 0)? sumOfColors : 1);
  299.             newGreen = newGreen/((sumOfColors != 0)? sumOfColors : 1);
  300.             newBlue = newBlue/((sumOfColors != 0)? sumOfColors : 1);
  301.             userAvatar.setStroke(Color.rgb(newRed, newGreen, newBlue));
  302.             userAvatar.setEffect(new DropShadow(+15d, 0d, +1d, Color.rgb(newRed, newGreen, newBlue)));
  303.             userAvatar.setEffect(new DropShadow(BlurType.GAUSSIAN, Color.rgb(newRed, newGreen, newBlue), +18d, 0.5, 0d, 0d));
  304.             /*userAvatar.setEffect(new DropShadow(+45d, +15d, 0d, Color.rgb(newRed, newGreen, newBlue)));*/
  305.         }
  306.     }
  307.  
  308.     public void setShardStatus(String shardStatus) {
  309.         this.ShardStatus.set(shardStatus);
  310.     }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement