Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Tutorial Making your GUI match the client styling

Joined
Nov 3, 2013
Messages
609
Hi there,
Recently @Arbiter told me that he wants more script GUIs to look like the one I used on the BatMineAIO. The GUI on that script is styled after the script selector on the client, and looks like this:
cpCDASi.png

So I thought I'd go over how I made this GUI step by step and touch on the key parts that make it look the way it does and how to make it match the currently set theme. Follow step by step as I build a very similar GUI. Fee free to copy this all you want.

Step 1 - GUI basics:
First off, let's get a basic GUI class going.
Code:
public class StyledGUI extends Stage{
    public StyledGUI(String title, final AbstractScript script){
        super(StageStyle.UNDECORATED);
        this.setTitle(title);
        JFrame frame = ClientUI.getFrame();
        initStyle(StageStyle.TRANSPARENT);
    }
}
Explanation by line:
1. We extend Stage because that is what JFX uses, (this is similar but also very different from a JFrame)
2. This is our constructor declaration: It takes in a String for the title, and reference back to the calling script (used later)
3. This is is a call to the Stage constructor. We use StageStyle.UNDECORATED since we are going to add our own close button and don't want the default OS title bar
4. Set the title of the window. This will change the name of window displayed on your task bar.
5. Grab the main client's frame for use later.
6. Initialize the window so that it will allow transparency.

Step 2 - Adding Open and Close functionality:
Let's add a bit more to this constructor
Code:
public class StyledGUI extends Stage{
    public StyledGUI(String title, final AbstractScript script){
        super(StageStyle.UNDECORATED);
        this.setTitle(title);
        JFrame frame = ClientUI.getFrame();
        initStyle(StageStyle.TRANSPARENT);

       setOnCloseRequest(new EventHandler<WindowEvent>(){
            @Override
            public void handle(WindowEvent event) {
                script.stop();
            }
        });

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                getIcons().add(SwingFXUtils.toFXImage((BufferedImage)frame.getIconImage(), null));
                setX(frame.getX() + (frame.getWidth()/2) - 290);
                setY(frame.getY() + (frame.getHeight()/2) - 176  + 40);
                setScene(createScene());
            }
        });
    }
}
Explanation by line:
8-13. This will add a listener to the window so that when it is closed, it will automatically stop your script, pretty dandy right?
15-17. This is what will actually allow your GUI to "open" and be displayed
18. This will grab and set your window icon to the fun little RuneMate ClapTrap robot.
19-20. These lines set the x,y location of the window to be centered on the Runemate Client. the +/- mystery numbers at the end are to account for the header on the client and the height/width or our window
21. This is what will cause the actual scene/window to be generated. We push this into a different method so the code doesn't get cluster fucked.

Step 3 - Adding our basic scene:
Add this method below the constructor.
Code:
public int dispose = 0;
BorderPane root = new BorderPane();
private Scene createScene(){
    Scene scene = new Scene(root);
    scene.setFill(null);
    root.setMinWidth(580);
    root.setMinHeight(349);

    FadeTransition ft = new FadeTransition(Duration.millis(500), root);
    ft.setFromValue(0.0f);
    ft.setToValue(1.0f);
    ft.play();
    return scene;
}
Explanation by line:
1. A variable that will be used to track the state of the GUI later
2. Create a BorderPane, this will be our top level container.
3. Now the method declaration
4. Create a new Scene, this will be needed later when we want to add some styling.
5. This is also needed to make the GUI transparent.
6-7. Set the minimum width and height for the window
9-12. This will make the window fade in when it is opened, the same way that the script selector does.
13. Now of course we need a return silly, it wouldn't work otherwise.

At this point we have a GUI that is now openable! If you do follow the next step right away, just remember that it may be difficult to close the window since it is Undercoated and currently completely invisible.

Step 4 - Open the GUI in the script:
Add this to the field variables of your script
Code:
StyledGUI gui;

Add this snippet to the onStart of you script.
Code:
AbstractScript THIS = this;
Platform.runLater(new Runnable() {
    @Override
    public void run() {
        gui = new StyledGUI("Sample GUI v1.0", THIS);
        gui.show();
        }
    });

    while(gui == null || gui.dispose == 0)Execution.delay(500);
    if(gui.dispose == 2){
        stop();
        return;
    }
1. We need a final copy of the current script in order to pass it in an anonymous function.
2-3. We need to open the GUI on the JFX window thread, Note this will be non-blocking
4. Create a new GUI object, passing it our title, and the final copy of the local script.
5. This will actually open the window! Yay!
7. Now we wait for the gui to close, before we advance. The gui will set the dispose variable accordingly, 0 = start script, 2 = end
8-11. This will make sure we stop if the window was closed, but the start button was not clicked.

More to Come...
 
Last edited:
Top