Reputation: 1079
I'm building a Desktop only application in LibGDX. The game has a map editor built in that you can switch to and make changes to the game maps. I want to add a feature where if the user clicks to close the window and there are unsaved edits it prompts you whether or not you want to save changes before closing down. What's the best way to achieve this effect?
EDIT: I'm using Lwjgl3 in this LibGDX project.
Upvotes: 3
Views: 685
Reputation: 4093
You simply need to override the exit method of the LwjglApplication
class to intercept any call to Gdx.app.exit
, including the call done by the app when window is closed via Lwjgl Display.
//desktop
public class DesktopLauncher
{
public static void main(String[] arg)
{
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "YourGame";
config.width = 800;
config.height = 400;
new LwjglApplication(new YourGame(), config) {
@Override
public void exit()
{
if(((YourGame)getApplicationListener()).canClose())
super.exit();
}
};
}
}
//core
public class YourGame implements ApplicationListener
{
//...
public boolean canClose()
{
//close logic here
return true; //return true if you want the close to happen
}
}
Of course, in your case your map editor should be a screen so your game should call a canClose()
method of the current screen instead of checking in the game class.
Upvotes: 4
Reputation: 1593
I can think of three methods:
Copy LwjglApplication
and modify the mainLoop()
according to your needs. Exspecially the line with if (Display.isCloseRequested()) exit();
Then use your own LwjglApplication
implementation in DesktopLauncher
.
Always safe any change in your editor in a temporary file and mark it as unfinished. So when the user restarts the game he can continue working on an unfinished level.
Check Display.isCloseRequested()
in one of your clases which implement render()
and then implement the saving. Like this:
void render() {
if(Gdx.app.getType() == Application.Desktop && Display.isCloseRequested()) {
// ask for saving
}
}
Upvotes: 0