Advertisement
FALSkills

Untitled

May 15th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. package scripts.lanapi.core.gui;
  2.  
  3. import javafx.application.Application;
  4. import javafx.application.Platform;
  5. import javafx.embed.swing.JFXPanel;
  6. import javafx.fxml.FXMLLoader;
  7. import javafx.scene.Parent;
  8. import javafx.scene.Scene;
  9. import javafx.scene.paint.Color;
  10. import javafx.stage.Stage;
  11. import javafx.stage.StageStyle;
  12. import org.tribot.api.General;
  13. import org.tribot.api.Timing;
  14. import org.tribot.api.types.generic.Condition;
  15. import scripts.lanapi.core.logging.LogProxy;
  16.  
  17. import javax.swing.*;
  18. import java.net.URL;
  19.  
  20. /**
  21. * @author Laniax
  22. */
  23. public class GUI extends Application {
  24.  
  25. private LogProxy log = new LogProxy("GUI");
  26.  
  27. private final URL fxml;
  28. private final URL stylesheet;
  29.  
  30. private Stage stage;
  31. private Scene scene;
  32. private AbstractGUIController controller;
  33. private boolean decorated;
  34.  
  35. private boolean isOpen = false;
  36.  
  37. public GUI(URL fxml) {
  38. this(fxml, (URL)null);
  39. }
  40. public GUI(URL fxml,AbstractGUIController controller) {
  41. this(fxml, null, true, controller);
  42. }
  43.  
  44. public GUI(URL fxml, boolean decorated) {
  45. this(fxml, null, decorated);
  46. }
  47.  
  48. public GUI(URL fxml, URL stylesheet) {
  49. this(fxml, stylesheet, true);
  50. }
  51.  
  52. public GUI(URL fxml, URL stylesheet, boolean decorated) {
  53. this(fxml,stylesheet,decorated,null);
  54. }
  55.  
  56. public GUI(URL fxml, URL stylesheet, boolean decorated, AbstractGUIController controller) {
  57.  
  58. this.fxml = fxml;
  59. this.stylesheet = stylesheet;
  60. this.decorated = decorated;
  61.  
  62. // We have to start the JFX thread from the EDT otherwise tribot will end it.
  63. SwingUtilities.invokeLater(() -> {
  64.  
  65. new JFXPanel(); // we have to init the toolkit
  66.  
  67. Platform.runLater(() -> {
  68. try {
  69. final Stage stage = new Stage();
  70.  
  71. if (!this.decorated) {
  72. stage.initStyle(StageStyle.TRANSPARENT);
  73. }
  74.  
  75. start(stage,controller);
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. });
  80. });
  81.  
  82. waitForInit();
  83. }
  84.  
  85. public Scene getScene() {
  86. return this.scene;
  87. }
  88.  
  89. public Stage getStage() {
  90. return this.stage;
  91. }
  92.  
  93. /**
  94. * The main entry point for all JavaFX applications.
  95. * The start method is called after the init method has returned,
  96. * and after the system is ready for the application to begin running.
  97. * <p>
  98. * <p>
  99. * NOTE: This method is called on the JavaFX Application Thread.
  100. * </p>
  101. *
  102. * @param stage the primary stage for this application, onto which
  103. * the application scene can be set. The primary stage will be embedded in
  104. * the browser if the application was launched as an applet.
  105. * Applications may create other stages, if needed, but they will not be
  106. * primary stages and will not be embedded in the browser.
  107. */
  108. @Override
  109. public void start(Stage stage) throws Exception {
  110. start(stage,null);
  111. }
  112.  
  113. public void start(Stage stage,AbstractGUIController controller) throws Exception {
  114.  
  115. if (fxml == null) {
  116. log.error("fxml is null. aborting");
  117. return;
  118. }
  119.  
  120.  
  121. this.stage = stage;
  122.  
  123. // stage.setAlwaysOnTop(true);
  124.  
  125. Platform.setImplicitExit(false);
  126.  
  127. FXMLLoader loader = new FXMLLoader(fxml);
  128.  
  129. // By default FXMLLoader uses a different classloader, this caused issues with upcasting
  130. loader.setClassLoader(this.getClass().getClassLoader());
  131.  
  132. if(controller != null){
  133. loader.setController(controller);
  134. }
  135.  
  136.  
  137. Parent box = loader.load();
  138.  
  139. this.controller = loader.getController();
  140.  
  141. if (this.controller == null) {
  142. log.error("Please add a controller to your fxml!");
  143. return;
  144. }
  145.  
  146. this.controller.setGUI(this);
  147.  
  148. scene = new Scene(box);
  149.  
  150. if (!this.decorated) {
  151. scene.setFill(Color.TRANSPARENT);
  152. }
  153.  
  154. if (this.stylesheet != null)
  155. scene.getStylesheets().add(this.stylesheet.toExternalForm());
  156.  
  157. stage.setScene(scene);
  158.  
  159. }
  160.  
  161. public <T extends AbstractGUIController> T getController() {
  162.  
  163. return (T) this.controller;
  164.  
  165. }
  166.  
  167. public void show() {
  168.  
  169. if (stage == null)
  170. return;
  171.  
  172. isOpen = true;
  173.  
  174. Platform.runLater(() -> stage.show());
  175. }
  176.  
  177. public void close() {
  178.  
  179. if (stage == null)
  180. return;
  181.  
  182. isOpen = false;
  183.  
  184. Platform.runLater(() -> stage.close());
  185. }
  186.  
  187. public boolean isOpen() {
  188. return isOpen;
  189. }
  190.  
  191. private void waitForInit() {
  192.  
  193. Timing.waitCondition(new Condition() {
  194. @Override
  195. public boolean active() {
  196. General.sleep(250);
  197. return stage != null;
  198. }
  199. }, 5000);
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement