fastjengine / FastJ

An open-source, Java-based 2D game engine.
https://fastj.tech
MIT License
82 stars 22 forks source link

[Bug] Unusable Quit Button and Resizing when initialized on New window #57

Closed lines-of-codes closed 2 years ago

lines-of-codes commented 3 years ago

Platform Info

Place an x in the box(es) [x] that the issue occurs on (or that you've found the issue on)

Describe the Bug

A clear and concise description of what the bug is. I'm making a Swing UI for adjusting window resolution and Other settings. But when I run the Code, I pressed play, The game window appears properly. But I can't exit out of the Game using the x button on the Window border. And it messed up the Window resizing as well.

Expected Behavior

A clear and concise description of what you expected to happen. Exit the Java program peacefully. And can resize like normal.

How to Reproduce the Bug

Steps to reproduce the behavior:

  1. Use my code (Available to the public soon)
  2. Build and run it
  3. Press play
  4. The game window will popup
  5. Try resizing and Press x on the Window border.

Media Aid

If applicable, add screenshots/GIFs/videos to help explain your problem.

Messed up resizing

https://user-images.githubusercontent.com/49134472/125572509-6f037792-153b-41ca-818f-8eab00b2759d.mp4

(Video provided by: lucasstarsz)

Additional context

Additional context not provided above.

lucasstarsz commented 3 years ago

I'll have to leave this to a future update of FastJ, as 1.5.0 is right around the corner. With that said, this is a peculiar issue for sure -- displays and display management will definitely need to be a major focus sometime soon.

lucasstarsz commented 2 years ago

Hey there! With the release of FastJ 1.6.0-SNAPSHOT-2, a new dialog system has been added. Rather than messing around with an external usage of Swing, I think we can circumvent the possible issues entirely by using this built-in dialog system. We can use the dialogs provided to get whatever user input you need, and then move on from there.

Here's a bit of code that should provide enough of an example of what I mean. (Said code would be in the init method of a SimpleManager or equivalent in a SceneManager.) The program, when run, should get user input, format it, and turn it into a display resolution that is applied to the window before it is displayed.

// close the display so that only the dialog will show
FastJEngine.getDisplay().close();

/* Dialogs in FastJ make use of a DialogConfig, which has quite a few useful parameters.
 * For now, we'll just change the prompt text to explain how the user can input the resolution. */
DialogConfig resolutionDialogConfig = DialogConfig.create()
        .withPromptText("Enter Resolution (format: number,number e.g. 1920,1080)")
        .build();

// open the dialog, then split the result and parse it into out resolution point.
String[] resolution = DialogUtil.showInputDialog(resolutionDialogConfig).split(",");
Point resolutionPoint = new Point(Integer.parseInt(resolution[0]), Integer.parseInt(resolution[1]));

// and now, we can resize our display!
FastJEngine.<SimpleDisplay>getDisplay().resizeDisplay(resolutionPoint);
// somewhere along the line, you'll want to re-open the display. It's still hidden here!

When put into action, this is the result.

You are still left with the issue of the window flashing open at the start, but we might be able to avoid this in a later issue related to how the window should start when the engine is run.

In order to make use of this, you'll need to update your FastJ version to 1.6.0-SNAPSHOT-2. Hope that helps!

lines-of-codes commented 2 years ago

Seems interesting... But I think just a text input field is not enough...

lucasstarsz commented 2 years ago

There's far more than the text field -- that was just one option! By default it is the text field, but if you need to go deeper there is already plans to have a fully configurable dialog system. Beyond that, you can interface directly with swing -- perhaps using JOptionPane or by constructing the frame yourself.

lucasstarsz commented 2 years ago

Beyond that, you can interface directly with swing

For example, this is a custom dialog with a JComboBox and JLabel for picking resolution.

public class CustomDialogExample extends SimpleManager {

    private static Point chosenResolution = FastJEngine.DefaultWindowResolution;

    private static JPanel customDialog() {
        JPanel customDialog = new JPanel();

        Point[] resolutionOptions = {
                new Point(1920, 1080),
                FastJEngine.DefaultWindowResolution,
                new Point(640, 360)
        };
        JComboBox<Point> optionsBox = new JComboBox<>(resolutionOptions);
        optionsBox.setEditable(true);
        optionsBox.setSelectedItem(FastJEngine.DefaultWindowResolution);
        optionsBox.addActionListener(e -> {
            JComboBox<Point> sourceOptionsBox = (JComboBox<Point>) e.getSource();
            chosenResolution = (Point) sourceOptionsBox.getSelectedItem();
        });
        JLabel resolutionMessage = new JLabel("Pick a resolution.");

        customDialog.setLayout(new BorderLayout());
        customDialog.add(resolutionMessage, BorderLayout.CENTER);
        customDialog.add(optionsBox, BorderLayout.SOUTH);

        return customDialog;
    }

    public static void main(String[] args) {
        DialogUtil.showConfirmationDialog(
                DialogConfig.create()
                        .withTitle("Custom Dialog")
                        .withPrompt(customDialog())
                        .build()
        );

        FastJEngine.init("Customized Resolution", new CustomDialogExample());
        FastJEngine.configureWindowResolution(chosenResolution);
        FastJEngine.run();
    }

    @Override
    public void init(FastJCanvas canvas) {}

    @Override
    public void update(FastJCanvas canvas) {}
}

Here's the result: https://user-images.githubusercontent.com/64715411/139172732-1c68e0cc-d706-4ce6-a074-e34d2f244f21.mp4

Hope that helps!

lucasstarsz commented 2 years ago

As a side note, this feature (prompts in DialogConfig as any type) will be available in the next snapshot of FastJ.

lucasstarsz commented 2 years ago

This feature is properly available in FastJ 1.6.0-SNAPSHOT-3. Please give it a try and see if that helps out your issue!

lines-of-codes commented 2 years ago

Yeah, I've tried it and it's pretty cool \:p

lucasstarsz commented 2 years ago

Glad to hear it! 😄 On that note, if you don't have any more issues feel free to close the issue. Otherwise, it'll be closed when 1.6.0 is released.