Some more cleaning of modules and App shutdown method.

master
Ondřej Hruška 11 years ago
parent 3987b234bb
commit ae57315df9
  1. 33
      src/mightypork/gamecore/backends/lwjgl/LwjglInputModule.java
  2. 1
      src/mightypork/gamecore/backends/lwjgl/audio/SlickAudioModule.java
  3. 7
      src/mightypork/gamecore/backends/lwjgl/graphics/LwjglGraphicsModule.java
  4. 44
      src/mightypork/gamecore/core/App.java
  5. 2
      src/mightypork/gamecore/core/config/Config.java
  6. 4
      src/mightypork/gamecore/core/config/KeyStrokeProperty.java
  7. 33
      src/mightypork/gamecore/core/events/ShudownRequest.java
  8. 47
      src/mightypork/gamecore/core/events/ShutdownEvent.java
  9. 4
      src/mightypork/gamecore/core/events/ShutdownListener.java
  10. 32
      src/mightypork/gamecore/core/events/UserQuitRequest.java
  11. 8
      src/mightypork/gamecore/graphics/GraphicsModule.java
  12. 1
      src/mightypork/gamecore/gui/components/BaseComponent.java
  13. 1
      src/mightypork/gamecore/gui/components/input/TextButton.java
  14. 3
      src/mightypork/gamecore/gui/screens/Overlay.java
  15. 2
      src/mightypork/gamecore/gui/screens/Screen.java
  16. 3
      src/mightypork/gamecore/gui/screens/impl/CrossfadeOverlay.java
  17. 6
      src/mightypork/gamecore/input/InputModule.java
  18. 12
      src/mightypork/gamecore/input/KeyStroke.java
  19. 7
      src/mightypork/rogue/RogueApp.java
  20. 8
      src/mightypork/rogue/screens/RogueScreen.java
  21. 2
      src/mightypork/rogue/screens/game/LayerInv.java
  22. 4
      src/mightypork/rogue/screens/game/ScreenGame.java
  23. 6
      src/mightypork/rogue/screens/menu/ScreenMainMenu.java
  24. 2
      src/mightypork/rogue/screens/story/ScreenStory.java
  25. 2
      src/mightypork/rogue/world/gui/interaction/MIPKeyboard.java

@ -2,7 +2,6 @@ package mightypork.gamecore.backends.lwjgl;
import mightypork.gamecore.core.App;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.input.InputModule;
import mightypork.gamecore.input.Key;
import mightypork.gamecore.input.Keys;
@ -20,12 +19,11 @@ import org.lwjgl.opengl.Display;
/**
* Input system
* Lwjgl Input Module.
*
* @author Ondřej Hruška (MightyPork)
*/
public class LwjglInputModule extends InputModule implements Updateable {
/** Current mouse position */
private static final Vect mousePos = new Vect() {
@ -61,11 +59,12 @@ public class LwjglInputModule extends InputModule implements Updateable {
}
}
@Override
protected void initKeyCodes()
{
Keys.NONE.setCode(Keyboard.KEY_NONE);
Keys.NUM_1.setCode(Keyboard.KEY_1);
Keys.NUM_2.setCode(Keyboard.KEY_2);
Keys.NUM_3.setCode(Keyboard.KEY_3);
@ -76,7 +75,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.NUM_8.setCode(Keyboard.KEY_8);
Keys.NUM_9.setCode(Keyboard.KEY_9);
Keys.NUM_0.setCode(Keyboard.KEY_0);
Keys.Q.setCode(Keyboard.KEY_Q);
Keys.W.setCode(Keyboard.KEY_W);
Keys.E.setCode(Keyboard.KEY_E);
@ -103,7 +102,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.B.setCode(Keyboard.KEY_B);
Keys.N.setCode(Keyboard.KEY_N);
Keys.M.setCode(Keyboard.KEY_M);
Keys.MINUS.setCode(Keyboard.KEY_MINUS);
Keys.EQUALS.setCode(Keyboard.KEY_EQUALS);
Keys.SLASH.setCode(Keyboard.KEY_SLASH);
@ -115,7 +114,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.GRAVE.setCode(Keyboard.KEY_GRAVE);
Keys.COMMA.setCode(Keyboard.KEY_COMMA);
Keys.PERIOD.setCode(Keyboard.KEY_PERIOD);
Keys.SPACE.setCode(Keyboard.KEY_SPACE);
Keys.BACKSPACE.setCode(Keyboard.KEY_BACK);
Keys.TAB.setCode(Keyboard.KEY_TAB);
@ -125,7 +124,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.POWER.setCode(Keyboard.KEY_POWER);
Keys.SLEEP.setCode(Keyboard.KEY_SLEEP);
//Keys.MENU.setCode(Keyboard.KEY_MENU); // not defined
Keys.F1.setCode(Keyboard.KEY_F1);
Keys.F2.setCode(Keyboard.KEY_F2);
Keys.F3.setCode(Keyboard.KEY_F3);
@ -141,11 +140,11 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.F13.setCode(Keyboard.KEY_F13);
Keys.F14.setCode(Keyboard.KEY_F14);
Keys.F15.setCode(Keyboard.KEY_F15);
Keys.CAPS_LOCK.setCode(Keyboard.KEY_CAPITAL);
Keys.SCROLL_LOCK.setCode(Keyboard.KEY_SCROLL);
Keys.SCROLL_LOCK.setCode(Keyboard.KEY_SCROLL);
Keys.NUM_LOCK.setCode(Keyboard.KEY_NUMLOCK);
Keys.NUMPAD_MINUS.setCode(Keyboard.KEY_SUBTRACT);
Keys.NUMPAD_PLUSS.setCode(Keyboard.KEY_ADD);
Keys.NUMPAD_0.setCode(Keyboard.KEY_NUMPAD0);
@ -162,7 +161,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.NUMPAD_ENTER.setCode(Keyboard.KEY_NUMPADENTER);
Keys.NUMPAD_DIVIDE.setCode(Keyboard.KEY_DIVIDE);
Keys.NUMPAD_MULTIPLY.setCode(Keyboard.KEY_MULTIPLY);
Keys.CONTROL_LEFT.setCode(Keyboard.KEY_LCONTROL);
Keys.CONTROL_RIGHT.setCode(Keyboard.KEY_RCONTROL);
Keys.ALT_LEFT.setCode(Keyboard.KEY_LMENU);
@ -171,18 +170,18 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.SHIFT_RIGHT.setCode(Keyboard.KEY_RSHIFT);
Keys.META_LEFT.setCode(Keyboard.KEY_LMETA);
Keys.META_RIGHT.setCode(Keyboard.KEY_RMETA);
Keys.UP.setCode(Keyboard.KEY_UP);
Keys.DOWN.setCode(Keyboard.KEY_DOWN);
Keys.LEFT.setCode(Keyboard.KEY_LEFT);
Keys.RIGHT.setCode(Keyboard.KEY_RIGHT);
Keys.HOME.setCode(Keyboard.KEY_HOME);
Keys.END.setCode(Keyboard.KEY_END);
Keys.PAGE_UP.setCode(Keyboard.KEY_PRIOR);
Keys.PAGE_DOWN.setCode(Keyboard.KEY_NEXT);
Keys.RETURN.setCode(Keyboard.KEY_RETURN);
Keys.PAUSE.setCode(Keyboard.KEY_PAUSE);
Keys.INSERT.setCode(Keyboard.KEY_INSERT);
@ -230,7 +229,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
}
if (Display.isCloseRequested()) {
App.bus().send(new UserQuitRequest());
App.shutdown();
}
}

@ -6,7 +6,6 @@ import java.nio.FloatBuffer;
import mightypork.gamecore.audio.AudioModule;
import mightypork.gamecore.audio.DeferredAudio;
import mightypork.gamecore.backends.lwjgl.BufferHelper;
import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.vect.Vect;
import mightypork.utils.math.constraints.vect.var.VectVar;

@ -512,13 +512,6 @@ public class LwjglGraphicsModule extends GraphicsModule {
}
@Override
public boolean isCloseRequested()
{
return Display.isCloseRequested();
}
@Override
public void beginFrame()
{

@ -6,6 +6,7 @@ import java.util.List;
import mightypork.gamecore.audio.AudioModule;
import mightypork.gamecore.core.config.Config;
import mightypork.gamecore.core.events.ShutdownEvent;
import mightypork.gamecore.graphics.GraphicsModule;
import mightypork.gamecore.input.InputModule;
import mightypork.utils.annotations.Stub;
@ -165,24 +166,33 @@ public class App extends BusNode {
*/
public static void shutdown()
{
if (instance == null) throw new IllegalStateException("App is not running.");
Log.i("Shutting down subsystems...");
// TODO send some shutdown notify event
try {
final EventBus bus = bus();
if (bus != null) {
bus.send(new DestroyEvent());
bus.destroy();
}
} catch (final Throwable e) {
Log.e(e);
if (instance != null) {
Log.i("Dispatching Shutdown event...");
bus().send(new ShutdownEvent(new Runnable() {
@Override
public void run()
{
try {
final EventBus bus = bus();
if (bus != null) {
bus.send(new DestroyEvent());
bus.destroy();
}
} catch (final Throwable e) {
Log.e(e);
}
Log.i("Shutdown completed.");
System.exit(0);
}
}));
} else {
Log.w("App is not running.");
System.exit(0);
}
Log.i("App terminated.");
System.exit(0);
}

@ -96,7 +96,7 @@ public class Config {
*/
public void addKeyStroke(String key, String defval, String comment)
{
final KeyStrokeProperty kprop = new KeyStrokeProperty(prefixKeyStroke(key), KeyStroke.createFromDataString(defval), comment);
final KeyStrokeProperty kprop = new KeyStrokeProperty(prefixKeyStroke(key), KeyStroke.createFromString(defval), comment);
strokes.put(prefixKeyStroke(key), kprop);
propertyManager.addProperty(kprop);
}

@ -31,7 +31,7 @@ public class KeyStrokeProperty extends Property<KeyStroke> {
final Key backup_key = value.getKey();
final int backup_mod = value.getMod();
value.fromDataString(string);
value.loadFromString(string);
if (value.getKey() == Keys.NONE) {
value.setTo(backup_key, backup_mod);
}
@ -42,7 +42,7 @@ public class KeyStrokeProperty extends Property<KeyStroke> {
@Override
public String toString()
{
return value.toDataString();
return value.saveToString();
}

@ -1,33 +0,0 @@
package mightypork.gamecore.core.events;
import mightypork.gamecore.core.App;
import mightypork.gamecore.core.MainLoop;
import mightypork.utils.eventbus.BusEvent;
import mightypork.utils.eventbus.events.flags.NonConsumableEvent;
import mightypork.utils.eventbus.events.flags.SingleReceiverEvent;
/**
* Shutdown request, non-interactive. Shutdown needs to execute on GL thread for
* display to deinit properly.
*
* @author Ondřej Hruška (MightyPork)
*/
@SingleReceiverEvent
@NonConsumableEvent
public class ShudownRequest extends BusEvent<MainLoop> {
@Override
public void handleBy(final MainLoop handler)
{
handler.queueTask(new Runnable() {
@Override
public void run()
{
App.shutdown();
}
}, true);
}
}

@ -0,0 +1,47 @@
package mightypork.gamecore.core.events;
import mightypork.utils.eventbus.BusEvent;
import mightypork.utils.eventbus.EventBus;
import mightypork.utils.logging.Log;
/**
* Shutdown event.<br>
* This event is dispatched when the <code>App.shutdown()</code> method is
* called. If no client consumes it, the shutdown will immediately follow.<br>
* This is a way to allow clients to abort the shutdown (ie. ask user to save
* game). After the game is saved, the <code>App.shutdown()</code> method can be
* called again.
*
* @author Ondřej Hruška (MightyPork)
*/
public class ShutdownEvent extends BusEvent<ShutdownListener> {
private Runnable shutdownTask;
public ShutdownEvent(Runnable doShutdown) {
this.shutdownTask = doShutdown;
}
@Override
protected void handleBy(ShutdownListener handler)
{
handler.onShutdown(this);
}
@Override
public void onDispatchComplete(EventBus bus)
{
if (!isConsumed()) {
Log.i("Shutting down...");
shutdownTask.run();
} else {
Log.i("Shutdown aborted.");
}
}
}

@ -6,7 +6,7 @@ package mightypork.gamecore.core.events;
*
* @author Ondřej Hruška (MightyPork)
*/
public interface UserQuitRequestListener {
public interface ShutdownListener {
/**
* Intercept quit request.<br>
@ -14,5 +14,5 @@ public interface UserQuitRequestListener {
*
* @param event quit request event.
*/
void onQuitRequest(UserQuitRequest event);
void onShutdown(ShutdownEvent event);
}

@ -1,32 +0,0 @@
package mightypork.gamecore.core.events;
import mightypork.utils.eventbus.BusEvent;
import mightypork.utils.eventbus.EventBus;
/**
* User quit request. This event is triggered when user clicks the "close"
* titlebar button, and if no client consumes it, the application will be shut
* down.
*
* @author Ondřej Hruška (MightyPork)
*/
public class UserQuitRequest extends BusEvent<UserQuitRequestListener> {
@Override
protected void handleBy(UserQuitRequestListener handler)
{
handler.onQuitRequest(this);
}
@Override
public void onDispatchComplete(EventBus bus)
{
if (!isConsumed()) {
bus.send(new ShudownRequest());
}
}
}

@ -316,14 +316,6 @@ public abstract class GraphicsModule extends BackendModule {
public abstract Screenshot takeScreenshot();
/**
* FIXME This should probably be moved to input module
*
* @return true if close was requested recently (i.e. click on cross)
*/
public abstract boolean isCloseRequested();
/**
* Start a render frame - clear buffers, prepare rendering context etc.
*/

@ -1,7 +1,6 @@
package mightypork.gamecore.gui.components;
import mightypork.gamecore.backends.lwjgl.LwjglInputModule;
import mightypork.gamecore.core.App;
import mightypork.gamecore.graphics.Renderable;
import mightypork.gamecore.gui.events.LayoutChangeEvent;

@ -1,7 +1,6 @@
package mightypork.gamecore.gui.components.input;
import mightypork.gamecore.backends.lwjgl.LwjglInputModule;
import mightypork.gamecore.core.App;
import mightypork.gamecore.graphics.fonts.IFont;
import mightypork.gamecore.gui.components.DynamicWidthComponent;

@ -4,15 +4,14 @@ package mightypork.gamecore.gui.screens;
import java.util.ArrayList;
import java.util.Collection;
import mightypork.gamecore.backends.lwjgl.LwjglInputModule;
import mightypork.gamecore.core.App;
import mightypork.gamecore.graphics.Renderable;
import mightypork.gamecore.gui.components.layout.ConstraintLayout;
import mightypork.gamecore.gui.events.LayoutChangeListener;
import mightypork.gamecore.input.Trigger;
import mightypork.gamecore.input.KeyBinder;
import mightypork.gamecore.input.KeyBindingPool;
import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Trigger;
import mightypork.utils.annotations.Stub;
import mightypork.utils.eventbus.clients.BusNode;
import mightypork.utils.interfaces.Enableable;

@ -5,10 +5,10 @@ import mightypork.gamecore.core.App;
import mightypork.gamecore.graphics.Renderable;
import mightypork.gamecore.gui.events.LayoutChangeEvent;
import mightypork.gamecore.gui.events.LayoutChangeListener;
import mightypork.gamecore.input.Trigger;
import mightypork.gamecore.input.KeyBinder;
import mightypork.gamecore.input.KeyBindingPool;
import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Trigger;
import mightypork.utils.annotations.Stub;
import mightypork.utils.eventbus.clients.BusNode;
import mightypork.utils.math.constraints.rect.Rect;

@ -2,7 +2,6 @@ package mightypork.gamecore.gui.screens.impl;
import mightypork.gamecore.core.App;
import mightypork.gamecore.core.events.ShudownRequest;
import mightypork.gamecore.gui.components.painters.QuadPainter;
import mightypork.gamecore.gui.events.ScreenRequest;
import mightypork.gamecore.gui.screens.Overlay;
@ -31,7 +30,7 @@ public class CrossfadeOverlay extends Overlay {
public void run()
{
if (requestedScreenName == null) {
App.bus().send(new ShudownRequest());
App.shutdown();
} else {
App.bus().send(new ScreenRequest(requestedScreenName));
}

@ -6,7 +6,11 @@ import mightypork.utils.math.constraints.vect.Vect;
/**
* Abstract input module
* Abstract input module.<br>
* An input module takes care of dispatching mouse and keyboard events, provides
* access to mouse position, key states etc.<br>
* The input module also takes care of calling App.shutdown() when the user
* requests exit (eg. clicks the titlebar close button)
*
* @author Ondřej Hruška (MightyPork)
*/

@ -5,7 +5,7 @@ import mightypork.utils.string.StringUtil;
/**
* Key stroke description
* Key stroke
*
* @author Ondřej Hruška (MightyPork)
*/
@ -62,21 +62,21 @@ public class KeyStroke {
}
public String toDataString()
public String saveToString()
{
return Keys.modToString(mod) + "+" + key.getName();
}
public static KeyStroke createFromDataString(String dataString)
public static KeyStroke createFromString(String dataString)
{
final KeyStroke ks = new KeyStroke(Keys.NONE, Keys.MOD_NONE);
ks.fromDataString(dataString);
ks.loadFromString(dataString);
return ks;
}
public void fromDataString(String dataString)
public void loadFromString(String dataString)
{
final String dataString1 = dataString.toUpperCase().replace('-', '+').replaceAll("[^A-Z0-9_+]", "");
@ -108,7 +108,7 @@ public class KeyStroke {
@Override
public String toString()
{
return toDataString();
return saveToString();
}

@ -7,8 +7,7 @@ import mightypork.gamecore.backends.lwjgl.LwjglBackend;
import mightypork.gamecore.backends.lwjgl.LwjglInputModule;
import mightypork.gamecore.core.config.Config;
import mightypork.gamecore.core.events.MainLoopRequest;
import mightypork.gamecore.core.events.ShudownRequest;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.core.events.ShutdownEvent;
import mightypork.gamecore.core.plugins.screenshot.ScreenshotRequest;
import mightypork.gamecore.graphics.FullscreenToggleRequest;
import mightypork.gamecore.graphics.GraphicsModule;
@ -110,8 +109,8 @@ public final class RogueApp extends BaseApp implements ViewportChangeListener, S
bindEventToKey(new FullscreenToggleRequest(), "global.fullscreen");
bindEventToKey(new ScreenshotRequest(), "global.screenshot");
bindEventToKey(new UserQuitRequest(), "global.quit");
bindEventToKey(new ShudownRequest(), "global.quit_force");
bindEventToKey(new ShutdownEvent(), "global.quit");
bindEventToKey(new ShutdownEvent(), "global.quit_force");
}

@ -1,15 +1,15 @@
package mightypork.rogue.screens;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.core.events.UserQuitRequestListener;
import mightypork.gamecore.core.events.ShutdownEvent;
import mightypork.gamecore.core.events.ShutdownListener;
import mightypork.gamecore.gui.screens.LayeredScreen;
import mightypork.rogue.RogueStateManager.RogueState;
import mightypork.rogue.events.RogueStateRequest;
import mightypork.utils.annotations.Stub;
public class RogueScreen extends LayeredScreen implements UserQuitRequestListener {
public class RogueScreen extends LayeredScreen implements ShutdownListener {
public RogueScreen(AppAccess app) {
super(app);
@ -18,7 +18,7 @@ public class RogueScreen extends LayeredScreen implements UserQuitRequestListene
@Override
@Stub
public void onQuitRequest(UserQuitRequest event)
public void onShutdown(ShutdownEvent event)
{
getEventBus().send(new RogueStateRequest(RogueState.EXIT));
event.consume();

@ -8,8 +8,8 @@ import mightypork.gamecore.gui.components.layout.GridLayout;
import mightypork.gamecore.gui.components.painters.QuadPainter;
import mightypork.gamecore.gui.components.painters.TextPainter;
import mightypork.gamecore.gui.screens.impl.FadingLayer;
import mightypork.gamecore.input.Trigger;
import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Trigger;
import mightypork.gamecore.resources.Res;
import mightypork.rogue.screens.game.ScreenGame.GScrState;
import mightypork.rogue.world.PlayerFacade;

@ -4,7 +4,7 @@ package mightypork.rogue.screens.game;
import java.io.File;
import mightypork.gamecore.core.config.Config;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.core.events.ShutdownEvent;
import mightypork.gamecore.gui.Action;
import mightypork.gamecore.gui.ActionGroup;
import mightypork.gamecore.input.Trigger;
@ -343,7 +343,7 @@ public class ScreenGame extends RogueScreen implements PlayerDeathHandler, GameW
@Override
public void onQuitRequest(UserQuitRequest event)
public void onShutdown(ShutdownEvent event)
{
// if player is dead, don't ask don't ask for save
final PlayerFacade pl = WorldProvider.get().getPlayer();

@ -2,7 +2,7 @@ package mightypork.rogue.screens.menu;
import mightypork.gamecore.core.config.Config;
import mightypork.gamecore.core.events.UserQuitRequest;
import mightypork.gamecore.core.events.ShutdownEvent;
import mightypork.gamecore.graphics.fonts.IFont;
import mightypork.gamecore.gui.Action;
import mightypork.gamecore.gui.components.input.TextButton;
@ -98,7 +98,7 @@ public class ScreenMainMenu extends RogueScreen {
@Override
protected void execute()
{
getEventBus().send(new UserQuitRequest());
getEventBus().send(new ShutdownEvent());
}
});
rows.add(btn, 2);
@ -108,7 +108,7 @@ public class ScreenMainMenu extends RogueScreen {
@Override
public void run()
{
getEventBus().send(new UserQuitRequest());
getEventBus().send(new ShutdownEvent());
}
});
}

@ -10,9 +10,9 @@ import mightypork.gamecore.gui.components.painters.TextPainter;
import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.gui.screens.ScreenLayer;
import mightypork.gamecore.gui.screens.impl.LayerColor;
import mightypork.gamecore.input.Trigger;
import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys;
import mightypork.gamecore.input.Trigger;
import mightypork.gamecore.input.events.MouseButtonEvent;
import mightypork.gamecore.input.events.MouseButtonHandler;
import mightypork.gamecore.resources.Res;

@ -7,10 +7,10 @@ import java.util.List;
import mightypork.gamecore.backends.lwjgl.LwjglInputModule;
import mightypork.gamecore.core.config.Config;
import mightypork.gamecore.input.Trigger;
import mightypork.gamecore.input.KeyBindingPool;
import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys;
import mightypork.gamecore.input.Trigger;
import mightypork.rogue.world.entity.impl.EntityPlayer;
import mightypork.rogue.world.events.PlayerStepEndListener;
import mightypork.rogue.world.gui.MapView;

Loading…
Cancel
Save