diff --git a/.classpath b/.classpath index 628a8ab..91e7afc 100644 --- a/.classpath +++ b/.classpath @@ -2,34 +2,34 @@ - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/src/mightypork/rogue/App.java b/src/mightypork/rogue/App.java index dcf4ea1..8d262d6 100644 --- a/src/mightypork/rogue/App.java +++ b/src/mightypork/rogue/App.java @@ -1,6 +1,5 @@ package mightypork.rogue; - import java.io.File; import java.io.RandomAccessFile; import java.nio.channels.FileLock; @@ -31,40 +30,40 @@ import org.lwjgl.input.Keyboard; * @author MightyPork */ public class App implements Destroyable, AppAccess { - + /** instance pointer */ private static App inst; - + private InputSystem input; private SoundSystem sounds; private DisplaySystem display; private MessageBus events; - + /** current screen */ private Screen screen; - + /** Flag that screenshot is scheduled to be taken next loop */ private boolean scheduledScreenshot = false; - - + + /** * @param args */ public static void main(String[] args) { Thread.setDefaultUncaughtExceptionHandler(new CrashHandler()); - + inst = new App(); - + try { inst.start(); } catch (Throwable t) { onCrash(t); } - + } - - + + /** * Handle a crash * @@ -73,19 +72,19 @@ public class App implements Destroyable, AppAccess { public static void onCrash(Throwable error) { Log.e("The game has crashed.", error); - + if (inst != null) inst.shutdown(); } - - + + @Override public void shutdown() { destroy(); System.exit(0); } - - + + public void initialize() { Log.i("Initializing subsystems"); @@ -96,8 +95,8 @@ public class App implements Destroyable, AppAccess { initSound(); initInput(); } - - + + @Override public void destroy() { @@ -105,15 +104,15 @@ public class App implements Destroyable, AppAccess { if (input != null) input.destroy(); if (display != null) display.destroy(); } - - + + private void initLock() { if (!Config.SINGLE_INSTANCE) return; - + if (!lockInstance()) { System.out.println("Working directory is locked.\nOnly one instance can run at a time."); - + //@formatter:off JOptionPane.showMessageDialog( null, @@ -122,13 +121,13 @@ public class App implements Destroyable, AppAccess { JOptionPane.ERROR_MESSAGE ); //@formatter:on - + shutdown(); return; } } - - + + private boolean lockInstance() { final File lockFile = new File(Paths.WORKDIR, ".lock"); @@ -137,7 +136,7 @@ public class App implements Destroyable, AppAccess { final FileLock fileLock = randomAccessFile.getChannel().tryLock(); if (fileLock != null) { Runtime.getRuntime().addShutdownHook(new Thread() { - + @Override public void run() { @@ -159,8 +158,8 @@ public class App implements Destroyable, AppAccess { } return false; } - - + + /** * initialize inputs */ @@ -168,11 +167,11 @@ public class App implements Destroyable, AppAccess { { events = new MessageBus(); events.subscribe(this); - + events.createChannel(UpdateEvent.class, UpdateEvent.Listener.class); } - - + + /** * initialize sound system */ @@ -181,17 +180,17 @@ public class App implements Destroyable, AppAccess { sounds = new SoundSystem(this); sounds.setMasterVolume(1); } - - + + /** * initialize inputs */ private void initInput() { input = new InputSystem(this); - + input.bindKeyStroke(new KeyStroke(Keyboard.KEY_F2), new Runnable() { - + @Override public void run() { @@ -199,9 +198,9 @@ public class App implements Destroyable, AppAccess { scheduledScreenshot = true; } }); - + input.bindKeyStroke(new KeyStroke(false, Keyboard.KEY_F11), new Runnable() { - + @Override public void run() { @@ -209,9 +208,9 @@ public class App implements Destroyable, AppAccess { display.switchFullscreen(); } }); - + input.bindKeyStroke(new KeyStroke(Keyboard.KEY_LCONTROL, Keyboard.KEY_Q), new Runnable() { - + @Override public void run() { @@ -220,8 +219,8 @@ public class App implements Destroyable, AppAccess { } }); } - - + + /** * initialize display */ @@ -231,8 +230,8 @@ public class App implements Destroyable, AppAccess { display.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.START_IN_FS, Const.TITLEBAR); display.setTargetFps(Const.FPS_RENDER); } - - + + /** * initialize main logger */ @@ -242,41 +241,41 @@ public class App implements Destroyable, AppAccess { li.enable(Config.LOGGING_ENABLED); li.enableSysout(Config.LOG_TO_STDOUT); } - - + + private void start() { initialize(); mainLoop(); shutdown(); } - + /** timer */ private TimerDelta timerRender; - - + + private void mainLoop() { screen = new ScreenTestAnimations(this); screen.setActive(true); - + timerRender = new TimerDelta(); - + while (!display.isCloseRequested()) { display.beginFrame(); - + events.broadcast(new UpdateEvent(timerRender.getDelta())); - + if (scheduledScreenshot) { takeScreenshot(); scheduledScreenshot = false; } - + display.endFrame(); } } - - + + /** * Do take a screenshot */ @@ -285,12 +284,12 @@ public class App implements Destroyable, AppAccess { sounds.getEffect("gui.shutter").play(1); Utils.runAsThread(new TaskTakeScreenshot(display)); } - - + + // - // static accessors + // static accessors // - + /** * @return sound system of the running instance */ @@ -299,8 +298,8 @@ public class App implements Destroyable, AppAccess { { return sounds; } - - + + /** * @return input system of the running instance */ @@ -309,8 +308,8 @@ public class App implements Destroyable, AppAccess { { return input; } - - + + /** * @return display system of the running instance */ @@ -319,8 +318,8 @@ public class App implements Destroyable, AppAccess { { return display; } - - + + /** * @return event bus */ @@ -329,5 +328,5 @@ public class App implements Destroyable, AppAccess { { return events; } - + } diff --git a/src/mightypork/rogue/AppAccess.java b/src/mightypork/rogue/AppAccess.java index 6a7796a..c0ffcc0 100644 --- a/src/mightypork/rogue/AppAccess.java +++ b/src/mightypork/rogue/AppAccess.java @@ -1,6 +1,5 @@ package mightypork.rogue; - import mightypork.rogue.display.DisplaySystem; import mightypork.rogue.input.InputSystem; import mightypork.rogue.sounds.SoundSystem; @@ -13,35 +12,35 @@ import mightypork.utils.patterns.subscription.MessageBus; * @author MightyPork */ public interface AppAccess { - + /** * @return sound system */ abstract SoundSystem snd(); - - + + /** * @return input system */ abstract InputSystem input(); - - + + /** * @return display system */ abstract DisplaySystem disp(); - - + + /** * @return event bus */ abstract MessageBus bus(); - - + + /** * Quit to OS
* Destroy app & exit VM */ abstract void shutdown(); - + } diff --git a/src/mightypork/rogue/AppAdapter.java b/src/mightypork/rogue/AppAdapter.java index 19c847d..0487921 100644 --- a/src/mightypork/rogue/AppAdapter.java +++ b/src/mightypork/rogue/AppAdapter.java @@ -1,6 +1,5 @@ package mightypork.rogue; - import mightypork.rogue.display.DisplaySystem; import mightypork.rogue.input.InputSystem; import mightypork.rogue.sounds.SoundSystem; @@ -13,49 +12,49 @@ import mightypork.utils.patterns.subscription.MessageBus; * @author MightyPork */ public class AppAdapter implements AppAccess { - + private AppAccess app; - - + + public AppAdapter(AppAccess app) { if (app == null) throw new NullPointerException("AppAccess instance cannot be null."); - + this.app = app; } - - + + @Override public final SoundSystem snd() { return app.snd(); } - - + + @Override public final InputSystem input() { return app.input(); } - - + + @Override public final DisplaySystem disp() { return app.disp(); } - - + + @Override public final MessageBus bus() { return app.bus(); } - - + + @Override public void shutdown() { app.shutdown(); } - + } diff --git a/src/mightypork/rogue/Config.java b/src/mightypork/rogue/Config.java index deaf02a..fd9d8a1 100644 --- a/src/mightypork/rogue/Config.java +++ b/src/mightypork/rogue/Config.java @@ -1,6 +1,5 @@ package mightypork.rogue; - import mightypork.utils.files.PropertyManager; import mightypork.utils.logging.Log; @@ -11,42 +10,42 @@ import mightypork.utils.logging.Log; * @author MightyPork */ public class Config { - + private static PropertyManager mgr; - + // opts public static final int def_LAST_RUN_VERSION = 0; public static int LAST_RUN_VERSION; - + public static final boolean def_START_IN_FS = false; public static boolean START_IN_FS; - + // property keys private static final String PK_LAST_RUN_VERSION = "status.last_run_version"; private static final String PK_START_IN_FS = "cfg.start_in_fullscreen"; - - + + /** * Prepare config manager and load user settings */ public static void init() { Log.f2("Initializing configuration manager."); - + String comment = Const.APP_NAME + " config file"; - + mgr = new PropertyManager(Paths.CONFIG, comment); - + mgr.cfgNewlineBeforeComments(true); mgr.cfgSeparateSections(true); - + mgr.putInteger(PK_LAST_RUN_VERSION, def_LAST_RUN_VERSION); mgr.putBoolean(PK_START_IN_FS, def_START_IN_FS); - + load(); // load what has been "put" } - - + + /** * Save changed fields to config file */ @@ -54,28 +53,28 @@ public class Config { { mgr.setValue(PK_LAST_RUN_VERSION, Const.VERSION); mgr.setValue(PK_START_IN_FS, START_IN_FS); - + mgr.apply(); } - - + + /** * Load config file and assign values to fields */ public static void load() { mgr.apply(); - + LAST_RUN_VERSION = mgr.getInteger(PK_LAST_RUN_VERSION); START_IN_FS = mgr.getBoolean(PK_START_IN_FS); } - + // options that can't be configured via config file - + public static boolean LOGGING_ENABLED = true; public static boolean LOG_TO_STDOUT = true; public static boolean SINGLE_INSTANCE = true; - + public static boolean LOG_FONTS = false; - + } diff --git a/src/mightypork/rogue/Const.java b/src/mightypork/rogue/Const.java index 1500e1b..3901f48 100644 --- a/src/mightypork/rogue/Const.java +++ b/src/mightypork/rogue/Const.java @@ -1,23 +1,22 @@ package mightypork.rogue; - /** * Application constants * * @author MightyPork */ public class Const { - + // STRINGS public static final int VERSION = 1; - + public static final String APP_NAME = "Rogue"; public static final String TITLEBAR = APP_NAME + " v." + VERSION; - + // AUDIO public static final int FPS_RENDER = 200; // max public static final long FPS_GUI_UPDATE = 60; - + // INITIAL WINDOW SIZE public static final int WINDOW_W = 1024; public static final int WINDOW_H = 768; diff --git a/src/mightypork/rogue/CrashHandler.java b/src/mightypork/rogue/CrashHandler.java index 147a50c..f12fa4e 100644 --- a/src/mightypork/rogue/CrashHandler.java +++ b/src/mightypork/rogue/CrashHandler.java @@ -1,16 +1,15 @@ package mightypork.rogue; - import java.lang.Thread.UncaughtExceptionHandler; public class CrashHandler implements UncaughtExceptionHandler { - + @Override public void uncaughtException(Thread t, Throwable e) { e.printStackTrace(); App.onCrash(e); } - + } diff --git a/src/mightypork/rogue/Paths.java b/src/mightypork/rogue/Paths.java index a171181..b0bbfb5 100644 --- a/src/mightypork/rogue/Paths.java +++ b/src/mightypork/rogue/Paths.java @@ -1,22 +1,21 @@ package mightypork.rogue; - import java.io.File; import mightypork.utils.files.OsUtils; public class Paths { - + private static final String APPDIR_NAME = "rogue"; - + public static final File WORKDIR = OsUtils.getWorkDir(APPDIR_NAME); public static final File LOGS = OsUtils.getWorkDir(APPDIR_NAME, "logs"); public static final File SCREENSHOTS = OsUtils.getWorkDir(APPDIR_NAME, "screenshots"); public static final File CONFIG = new File(WORKDIR, "config.ini"); - + public static final String DIR_EFFECTS = "res/sounds/effects/"; public static final String DIR_MUSIC = "res/sounds/music/"; public static final String DIR_LOOPS = "res/sounds/loops/"; - + } diff --git a/src/mightypork/rogue/bus/DelegatingBusClient.java b/src/mightypork/rogue/bus/DelegatingBusClient.java index a322e90..b9da5fb 100644 --- a/src/mightypork/rogue/bus/DelegatingBusClient.java +++ b/src/mightypork/rogue/bus/DelegatingBusClient.java @@ -1,6 +1,5 @@ package mightypork.rogue.bus; - import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -23,25 +22,25 @@ import mightypork.utils.time.Updateable; * @author MightyPork */ public abstract class DelegatingBusClient extends AppAdapter implements DelegatingClient, ToggleableClient, Destroyable, Updateable, UpdateEvent.Listener { - + /** Subsystem children subscribing to MessageBus */ private Set childSubscribers = new HashSet(); - + private boolean wantUpdates = true; private boolean eventsEnabled = true; - - + + public DelegatingBusClient(AppAccess app, boolean updates) { super(app); - + bus().subscribe(this); - + enableUpdates(updates); - + init(); } - - + + /** * Add a child subscriber to the {@link MessageBus}.
* @@ -51,40 +50,41 @@ public abstract class DelegatingBusClient extends AppAdapter implements Delegati public final boolean addChildSubscriber(Object client) { if (client == null) return false; - + childSubscribers.add(client); - + return true; } - - + + /** * Remove a child subscriber * - * @param client subscriber to remove + * @param client + * subscriber to remove */ public final void removeChildSubscriber(Object client) { if (client == null) return; - + childSubscribers.remove(client); } - - + + @Override public final Collection getChildClients() { return childSubscribers; } - - + + @Override public final boolean doesDelegate() { return doesSubscribe(); } - - + + /** * Set whether to receive {@link UpdateEvent}s (delta timing, one each * frame).
@@ -95,15 +95,15 @@ public abstract class DelegatingBusClient extends AppAdapter implements Delegati { wantUpdates = enable; } - - + + @Override public final boolean doesSubscribe() { return eventsEnabled; } - - + + /** * Set whether events should be received. * @@ -113,33 +113,33 @@ public abstract class DelegatingBusClient extends AppAdapter implements Delegati { this.eventsEnabled = enable; } - - + + @Override public final void receive(UpdateEvent event) { if (wantUpdates) update(event.getDeltaTime()); } - - + + @Override public final void destroy() { deinit(); - + enableUpdates(false); - + bus().unsubscribe(this); } - - + + @Override public void update(double delta) { Log.w("Client " + getClass().getSimpleName() + " receives updates, but does not override the update() method."); } - - + + /** * Initialize the subsystem
* (called during construction) @@ -148,8 +148,8 @@ public abstract class DelegatingBusClient extends AppAdapter implements Delegati { // no impl } - - + + /** * Deinitialize the subsystem
* (called during destruction) @@ -158,5 +158,5 @@ public abstract class DelegatingBusClient extends AppAdapter implements Delegati { // no impl } - + } diff --git a/src/mightypork/rogue/bus/SimpleBusClient.java b/src/mightypork/rogue/bus/SimpleBusClient.java index f5c40db..bf6fda0 100644 --- a/src/mightypork/rogue/bus/SimpleBusClient.java +++ b/src/mightypork/rogue/bus/SimpleBusClient.java @@ -1,6 +1,5 @@ package mightypork.rogue.bus; - import mightypork.rogue.AppAccess; import mightypork.rogue.AppAdapter; @@ -11,13 +10,14 @@ import mightypork.rogue.AppAdapter; * @author MightyPork */ public class SimpleBusClient extends AppAdapter { - + /** - * @param app app access + * @param app + * app access */ public SimpleBusClient(AppAccess app) { super(app); - + bus().subscribe(this); } } diff --git a/src/mightypork/rogue/bus/UpdateReceiver.java b/src/mightypork/rogue/bus/UpdateReceiver.java index db525a8..2aca86d 100644 --- a/src/mightypork/rogue/bus/UpdateReceiver.java +++ b/src/mightypork/rogue/bus/UpdateReceiver.java @@ -1,26 +1,25 @@ package mightypork.rogue.bus; - import mightypork.rogue.AppAccess; import mightypork.rogue.bus.events.UpdateEvent; import mightypork.utils.time.Updateable; public abstract class UpdateReceiver extends SimpleBusClient implements UpdateEvent.Listener, Updateable { - + public UpdateReceiver(AppAccess app) { super(app); } - - + + @Override public void receive(UpdateEvent event) { update(event.getDeltaTime()); } - - + + @Override public abstract void update(double delta); - + } diff --git a/src/mightypork/rogue/bus/events/KeyboardEvent.java b/src/mightypork/rogue/bus/events/KeyboardEvent.java index a531a4c..61d49c7 100644 --- a/src/mightypork/rogue/bus/events/KeyboardEvent.java +++ b/src/mightypork/rogue/bus/events/KeyboardEvent.java @@ -1,6 +1,5 @@ package mightypork.rogue.bus.events; - import mightypork.utils.patterns.subscription.Handleable; import org.lwjgl.input.Keyboard; @@ -12,19 +11,19 @@ import org.lwjgl.input.Keyboard; * @author MightyPork */ public class KeyboardEvent implements Handleable { - + private int key; private boolean down; private char c; - - + + public KeyboardEvent(int key, char c, boolean down) { this.key = key; this.c = c; this.down = down; } - - + + /** * @return key code (see {@link org.lwjgl.input.Keyboard}) */ @@ -32,8 +31,8 @@ public class KeyboardEvent implements Handleable { { return key; } - - + + /** * @return true if key was just pressed */ @@ -41,8 +40,8 @@ public class KeyboardEvent implements Handleable { { return down; } - - + + /** * @return true if key was just released */ @@ -50,8 +49,8 @@ public class KeyboardEvent implements Handleable { { return !down; } - - + + /** * @return event character (if any) */ @@ -59,29 +58,31 @@ public class KeyboardEvent implements Handleable { { return c; } - - + + @Override public void handleBy(Listener keh) { keh.receive(this); } - + + public interface Listener { - + /** * Handle an event * - * @param event event + * @param event + * event */ public void receive(KeyboardEvent event); } - - + + @Override public String toString() { return Keyboard.getKeyName(key) + ":" + (down ? "DOWN" : "UP"); } - + } diff --git a/src/mightypork/rogue/bus/events/MouseButtonEvent.java b/src/mightypork/rogue/bus/events/MouseButtonEvent.java index a877618..1950464 100644 --- a/src/mightypork/rogue/bus/events/MouseButtonEvent.java +++ b/src/mightypork/rogue/bus/events/MouseButtonEvent.java @@ -1,6 +1,5 @@ package mightypork.rogue.bus.events; - import mightypork.utils.math.coord.Coord; import mightypork.utils.patterns.subscription.Handleable; @@ -11,24 +10,28 @@ import mightypork.utils.patterns.subscription.Handleable; * @author MightyPork */ public class MouseButtonEvent implements Handleable { - + public static final int BUTTON_LEFT = 0; public static final int BUTTON_MIDDLE = 1; public static final int BUTTON_RIGHT = 2; - + private int button; private int wheeld; private Coord pos; private boolean down; - - + + /** * Mouse button event * - * @param pos event position - * @param button button id - * @param down button pressed - * @param wheeld wheel change + * @param pos + * event position + * @param button + * button id + * @param down + * button pressed + * @param wheeld + * wheel change */ public MouseButtonEvent(Coord pos, int button, boolean down, int wheeld) { this.button = button; @@ -36,8 +39,8 @@ public class MouseButtonEvent implements Handleable { this.pos = pos; this.wheeld = wheeld; } - - + + /** * @return true if the event was caused by a button state change */ @@ -45,8 +48,8 @@ public class MouseButtonEvent implements Handleable { { return button != -1; } - - + + /** * @return true if the event was caused by a wheel change */ @@ -54,8 +57,8 @@ public class MouseButtonEvent implements Handleable { { return wheeld != 0; } - - + + /** * @return button id or -1 if none was pressed */ @@ -63,8 +66,8 @@ public class MouseButtonEvent implements Handleable { { return button; } - - + + /** * @return number of steps the wheel changed since last event */ @@ -72,8 +75,8 @@ public class MouseButtonEvent implements Handleable { { return wheeld; } - - + + /** * @return mouse position when the event occurred */ @@ -81,8 +84,8 @@ public class MouseButtonEvent implements Handleable { { return pos; } - - + + /** * @return true if button was just pressed */ @@ -90,8 +93,8 @@ public class MouseButtonEvent implements Handleable { { return button != -1 && down; } - - + + /** * @return true if button was just released */ @@ -99,20 +102,22 @@ public class MouseButtonEvent implements Handleable { { return button != -1 && !down; } - - + + @Override public void handleBy(Listener handler) { handler.receive(this); } - + + public interface Listener { - + /** * Handle an event * - * @param event event + * @param event + * event */ public void receive(MouseButtonEvent event); } diff --git a/src/mightypork/rogue/bus/events/MouseMotionEvent.java b/src/mightypork/rogue/bus/events/MouseMotionEvent.java index b2be021..4f7fad0 100644 --- a/src/mightypork/rogue/bus/events/MouseMotionEvent.java +++ b/src/mightypork/rogue/bus/events/MouseMotionEvent.java @@ -1,22 +1,21 @@ package mightypork.rogue.bus.events; - import mightypork.utils.math.coord.Coord; import mightypork.utils.patterns.subscription.Handleable; public class MouseMotionEvent implements Handleable { - + private Coord move; private Coord pos; - - + + public MouseMotionEvent(Coord pos, Coord move) { this.move = move; this.pos = pos; } - - + + /** * @return movement since last {@link MouseMotionEvent} */ @@ -24,8 +23,8 @@ public class MouseMotionEvent implements Handleable { { return move; } - - + + /** * @return current mouse position */ @@ -33,22 +32,24 @@ public class MouseMotionEvent implements Handleable { { return pos; } - - + + @Override public void handleBy(Listener keh) { keh.receive(this); } - + + public interface Listener { - + /** * Handle an event * - * @param event event + * @param event + * event */ public void receive(MouseMotionEvent event); } - + } diff --git a/src/mightypork/rogue/bus/events/ScreenChangeEvent.java b/src/mightypork/rogue/bus/events/ScreenChangeEvent.java index 65ec112..600f8fc 100644 --- a/src/mightypork/rogue/bus/events/ScreenChangeEvent.java +++ b/src/mightypork/rogue/bus/events/ScreenChangeEvent.java @@ -1,50 +1,50 @@ package mightypork.rogue.bus.events; - import mightypork.utils.math.coord.Coord; import mightypork.utils.patterns.subscription.Handleable; public class ScreenChangeEvent implements Handleable { - + private boolean fullscreen; private Coord screenSize; private boolean fsChanged; - - + + public ScreenChangeEvent(boolean fsChanged, boolean fullscreen, Coord size) { this.fullscreen = fullscreen; this.screenSize = size; this.fsChanged = fsChanged; } - - + + public boolean isFullscreen() { return fullscreen; } - - + + public boolean fullscreenChanged() { return fsChanged; } - - + + public Coord getScreenSize() { return screenSize; } - - + + @Override public void handleBy(Listener handler) { handler.receive(this); } - + + public interface Listener { - + public void receive(ScreenChangeEvent event); } } diff --git a/src/mightypork/rogue/bus/events/UpdateEvent.java b/src/mightypork/rogue/bus/events/UpdateEvent.java index 9a29dd6..63c35e6 100644 --- a/src/mightypork/rogue/bus/events/UpdateEvent.java +++ b/src/mightypork/rogue/bus/events/UpdateEvent.java @@ -1,33 +1,33 @@ package mightypork.rogue.bus.events; - import mightypork.utils.patterns.subscription.Handleable; public class UpdateEvent implements Handleable { - + private final double deltaTime; - - + + public UpdateEvent(double deltaTime) { this.deltaTime = deltaTime; } - - + + public double getDeltaTime() { return deltaTime; } - - + + @Override public void handleBy(Listener handler) { handler.receive(this); } - + + public interface Listener { - + public void receive(UpdateEvent event); } } diff --git a/src/mightypork/rogue/display/DisplaySystem.java b/src/mightypork/rogue/display/DisplaySystem.java index 3d10f64..1481102 100644 --- a/src/mightypork/rogue/display/DisplaySystem.java +++ b/src/mightypork/rogue/display/DisplaySystem.java @@ -1,6 +1,5 @@ package mightypork.rogue.display; - import static org.lwjgl.opengl.GL11.*; import java.awt.image.BufferedImage; @@ -21,31 +20,31 @@ import org.lwjgl.opengl.DisplayMode; public class DisplaySystem extends DelegatingBusClient implements Bounding { - + private DisplayMode windowDisplayMode; private int targetFps; - - + + public DisplaySystem(AppAccess app) { super(app, true); enableUpdates(false); } - - + + @Override protected void init() { initChannels(); } - - + + @Override public void deinit() { Display.destroy(); } - - + + /** * Initialize event channels */ @@ -53,14 +52,14 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { { bus().createChannel(ScreenChangeEvent.class, ScreenChangeEvent.Listener.class); } - - + + public void setTargetFps(int fps) { this.targetFps = fps; } - - + + public void createMainWindow(int width, int height, boolean resizable, boolean fullscreen, String title) { try { @@ -69,7 +68,7 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { Display.setVSyncEnabled(true); Display.setTitle(title); Display.create(); - + if (fullscreen) { switchFullscreen(); Display.update(); @@ -78,20 +77,20 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { throw new RuntimeException("Could not initialize screen", e); } } - - + + /** * Toggle FS if possible */ public void switchFullscreen() { try { - + if (!Display.isFullscreen()) { Log.f3("Entering fullscreen."); // save window resize windowDisplayMode = new DisplayMode(Display.getWidth(), Display.getHeight()); - + Display.setDisplayMode(Display.getDesktopDisplayMode()); Display.setFullscreen(true); Display.update(); @@ -100,9 +99,9 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { Display.setDisplayMode(windowDisplayMode); Display.update(); } - + bus().broadcast(new ScreenChangeEvent(true, Display.isFullscreen(), getSize())); - + } catch (Throwable t) { Log.e("Failed to toggle fullscreen mode.", t); try { @@ -113,19 +112,20 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { } } } - - + + public BufferedImage takeScreenshot() { glReadBuffer(GL_FRONT); int width = Display.getDisplayMode().getWidth(); int height = Display.getDisplayMode().getHeight(); - int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha. + int bpp = 4; // Assuming a 32-bit display with a byte each for red, + // green, blue, and alpha. ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp); glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); - + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - + // convert to a buffered image for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { @@ -136,11 +136,11 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b); } } - + return image; } - - + + /** * @return true if close was requested (i.e. click on cross) */ @@ -148,8 +148,8 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { { return Display.isCloseRequested(); } - - + + /** * Get fullscreen state * @@ -159,8 +159,8 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { { return Display.isFullscreen(); } - - + + /** * Get screen size * @@ -170,8 +170,8 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { { return new Coord(Display.getWidth(), Display.getHeight()); } - - + + /** * Start a OpenGL frame */ @@ -180,12 +180,12 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { if (Display.wasResized()) { bus().broadcast(new ScreenChangeEvent(false, Display.isFullscreen(), getSize())); } - + glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } - - + + /** * End an OpenGL frame, flip buffers, sync to fps. */ @@ -194,8 +194,8 @@ public class DisplaySystem extends DelegatingBusClient implements Bounding { Display.update(false); // don't poll input devices Display.sync(targetFps); } - - + + @Override public Rect getRect() { diff --git a/src/mightypork/rogue/display/Screen.java b/src/mightypork/rogue/display/Screen.java index b8e15fb..2e3df3f 100644 --- a/src/mightypork/rogue/display/Screen.java +++ b/src/mightypork/rogue/display/Screen.java @@ -1,6 +1,5 @@ package mightypork.rogue.display; - import static org.lwjgl.opengl.GL11.*; import mightypork.rogue.AppAccess; import mightypork.rogue.bus.DelegatingBusClient; @@ -21,38 +20,39 @@ import mightypork.utils.math.coord.Rect; * @author MightyPork */ public abstract class Screen extends DelegatingBusClient implements KeyBinder, Bounding, ScreenChangeEvent.Listener { - + private KeyBindingPool keybindings; - + private boolean active; - - + + public Screen(AppAccess app) { super(app, true); - + // disable events initially enableEvents(false); } - - + + @Override public final void bindKeyStroke(KeyStroke stroke, Runnable task) { keybindings.bindKeyStroke(stroke, task); } - - + + @Override public final void unbindKeyStroke(KeyStroke stroke) { keybindings.unbindKeyStroke(stroke); } - - + + /** * Prepare for being shown * - * @param shown true to show, false to hide + * @param shown + * true to show, false to hide */ public final void setActive(boolean shown) { @@ -62,45 +62,45 @@ public abstract class Screen extends DelegatingBusClient implements KeyBinder, B setupViewport(); onSizeChanged(getRect().getSize()); onScreenEnter(); - + // subscribe to event bus enableEvents(true); - + } else { onScreenLeave(); - + active = false; - + // unsusbcribe from event bus enableEvents(false); } } - - + + private void setupGraphics() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - + glDisable(GL_LIGHTING); - + glClearDepth(1f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); - + glEnable(GL_NORMALIZE); - + glShadeModel(GL_SMOOTH); - + glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - + glDisable(GL_TEXTURE_2D); - + setupViewport(); } - - + + private void setupViewport() { // fix projection for changed size @@ -109,80 +109,82 @@ public abstract class Screen extends DelegatingBusClient implements KeyBinder, B Coord s = disp().getSize(); glViewport(0, 0, s.xi(), s.yi()); glOrtho(0, s.x, 0, s.y, -1000, 1000); - + // back to modelview glMatrixMode(GL_MODELVIEW); } - - + + @Override protected final void init() { keybindings = new KeyBindingPool(); - + addChildSubscriber(keybindings); - + initScreen(); } - - + + @Override protected final void deinit() { deinitScreen(); } - - + + /** * Initialize screen layout and key bindings.
* Called during screen construction. */ protected abstract void initScreen(); - - + + /** * Clean up before screen is destroyed. */ protected abstract void deinitScreen(); - - + + /** * Called when the screen becomes active */ protected abstract void onScreenEnter(); - - + + /** * Called when the screen is no longer active */ protected abstract void onScreenLeave(); - - + + /** * Update GUI for new screen size * - * @param size screen size + * @param size + * screen size */ protected void onSizeChanged(Coord size) { // no impl } - - + + /** * Render screen contents (context is ready for 2D rendering) */ protected abstract void renderScreen(); - - + + /** * Update animations and timing * - * @param delta time elapsed + * @param delta + * time elapsed */ protected abstract void updateScreen(double delta); - - + + /** * Render screen */ @@ -191,8 +193,8 @@ public abstract class Screen extends DelegatingBusClient implements KeyBinder, B glPushAttrib(GL_ENABLE_BIT); glPushMatrix(); } - - + + /** * Render screen */ @@ -201,8 +203,8 @@ public abstract class Screen extends DelegatingBusClient implements KeyBinder, B glPopAttrib(); glPopMatrix(); } - - + + /** * @return true if screen is the curretn screen */ @@ -210,19 +212,19 @@ public abstract class Screen extends DelegatingBusClient implements KeyBinder, B { return active; } - - + + @Override public final void receive(ScreenChangeEvent event) { if (!isActive()) return; - + setupViewport(); - + onSizeChanged(event.getScreenSize()); } - - + + /** * Update and render the screen */ @@ -230,17 +232,17 @@ public abstract class Screen extends DelegatingBusClient implements KeyBinder, B public final void update(double delta) { updateScreen(delta); - + renderBegin(); renderScreen(); renderEnd(); } - - + + @Override public final Rect getRect() { return disp().getRect(); } - + } diff --git a/src/mightypork/rogue/display/ScreenTestAnimations.java b/src/mightypork/rogue/display/ScreenTestAnimations.java index 18d2815..d190750 100644 --- a/src/mightypork/rogue/display/ScreenTestAnimations.java +++ b/src/mightypork/rogue/display/ScreenTestAnimations.java @@ -1,6 +1,5 @@ package mightypork.rogue.display; - import java.util.Random; import mightypork.rogue.AppAccess; @@ -19,15 +18,15 @@ import org.lwjgl.opengl.Display; public class ScreenTestAnimations extends Screen implements MouseButtonEvent.Listener { - + public ScreenTestAnimations(AppAccess app) { super(app); } - + private Random rand = new Random(); - + private AnimDoubleDeg degAnim = new AnimDoubleDeg(0, Easing.ELASTIC_OUT); - + //@formatter:off private AnimDouble[] anims = new AnimDouble[] { new AnimDouble(0, Easing.BOUNCE_OUT), @@ -95,12 +94,12 @@ public class ScreenTestAnimations extends Screen implements MouseButtonEvent.Lis // new AnimDouble(0, Easing.ELASTIC_IN_OUT), }; //@formatter:on - + @Override public void initScreen() { bindKeyStroke(new KeyStroke(Keyboard.KEY_RIGHT), new Runnable() { - + @Override public void run() { @@ -109,9 +108,9 @@ public class ScreenTestAnimations extends Screen implements MouseButtonEvent.Lis } } }); - + bindKeyStroke(new KeyStroke(Keyboard.KEY_LEFT), new Runnable() { - + @Override public void run() { @@ -121,40 +120,40 @@ public class ScreenTestAnimations extends Screen implements MouseButtonEvent.Lis } }); } - - + + @Override protected void deinitScreen() { // no impl } - - + + @Override protected void onScreenEnter() { // no impl } - - + + @Override protected void onScreenLeave() { // no impl } - - + + @Override protected void updateScreen(double delta) { degAnim.update(delta); - + for (AnimDouble a : anims) { a.update(delta); } } - - + + @Override protected void renderScreen() { @@ -163,31 +162,31 @@ public class ScreenTestAnimations extends Screen implements MouseButtonEvent.Lis double perBoxH = screenH / anims.length; double padding = perBoxH * 0.1; double boxSide = perBoxH - padding * 2; - + for (int i = 0; i < anims.length; i++) { AnimDouble a = anims[i]; - + RenderUtils.setColor(RGB.GREEN); RenderUtils.quadSize(padding + a.getCurrentValue() * (screenW - perBoxH), screenH - perBoxH * i - perBoxH + padding, boxSide, boxSide); } - + RenderUtils.setColor(RGB.YELLOW); RenderUtils.translate(new Coord(Display.getWidth() / 2, Display.getHeight() / 2)); RenderUtils.rotateZ(degAnim.getCurrentValue()); RenderUtils.quadSize(-10, -10, 20, 200); } - - + + @Override public void receive(MouseButtonEvent event) { if (event.isDown()) { Coord vec = disp().getSize().half().vecTo(event.getPos()); - + Polar p = Polar.fromCoord(vec); - + degAnim.fadeTo(p.getAngleDeg() - 90, 1.5); } } - + } diff --git a/src/mightypork/rogue/display/constraints/Bounding.java b/src/mightypork/rogue/display/constraints/Bounding.java index 4a55b55..a32dc2e 100644 --- a/src/mightypork/rogue/display/constraints/Bounding.java +++ b/src/mightypork/rogue/display/constraints/Bounding.java @@ -1,6 +1,5 @@ package mightypork.rogue.display.constraints; - import mightypork.utils.math.coord.Rect; @@ -10,7 +9,7 @@ import mightypork.utils.math.coord.Rect; * @author MightyPork */ public interface Bounding { - + /** * @return bounding rectangle */ diff --git a/src/mightypork/rogue/display/constraints/Constraint.java b/src/mightypork/rogue/display/constraints/Constraint.java index d03153e..1859608 100644 --- a/src/mightypork/rogue/display/constraints/Constraint.java +++ b/src/mightypork/rogue/display/constraints/Constraint.java @@ -1,20 +1,19 @@ package mightypork.rogue.display.constraints; - import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Rect; public abstract class Constraint implements Bounding { - + protected Bounding context; - - + + public Constraint(Bounding context) { this.context = context; } - - + + /** * Assign a context * @@ -24,8 +23,8 @@ public abstract class Constraint implements Bounding { { this.context = context; } - - + + /** * @return context */ @@ -33,8 +32,8 @@ public abstract class Constraint implements Bounding { { return context; } - - + + /** * @return context rect origin */ @@ -42,8 +41,8 @@ public abstract class Constraint implements Bounding { { return context.getRect().getOrigin(); } - - + + /** * @return context rect size */ @@ -51,9 +50,9 @@ public abstract class Constraint implements Bounding { { return context.getRect().getSize(); } - - + + @Override public abstract Rect getRect(); - + } diff --git a/src/mightypork/rogue/display/rendering/Renderable.java b/src/mightypork/rogue/display/rendering/Renderable.java index 8f20e85..245d257 100644 --- a/src/mightypork/rogue/display/rendering/Renderable.java +++ b/src/mightypork/rogue/display/rendering/Renderable.java @@ -1,8 +1,7 @@ package mightypork.rogue.display.rendering; - public interface Renderable { - + public void render(); - + } diff --git a/src/mightypork/rogue/display/rendering/ScreenLayer.java b/src/mightypork/rogue/display/rendering/ScreenLayer.java index 8254f9f..c064644 100644 --- a/src/mightypork/rogue/display/rendering/ScreenLayer.java +++ b/src/mightypork/rogue/display/rendering/ScreenLayer.java @@ -1,18 +1,17 @@ package mightypork.rogue.display.rendering; - import mightypork.rogue.AppAccess; import mightypork.rogue.bus.DelegatingBusClient; public abstract class ScreenLayer extends DelegatingBusClient implements Renderable { - + public ScreenLayer(AppAccess app) { super(app, true); } - - + + @Override public abstract void render(); - + } diff --git a/src/mightypork/rogue/fonts/Align.java b/src/mightypork/rogue/fonts/Align.java index e9ff742..e4c8e3e 100644 --- a/src/mightypork/rogue/fonts/Align.java +++ b/src/mightypork/rogue/fonts/Align.java @@ -1,13 +1,12 @@ package mightypork.rogue.fonts; - /** * Alignment * * @author MightyPork */ public class Align { - + public static final int LEFT = -1; public static final int RIGHT = 1; public static final int TOP = 1; @@ -16,5 +15,5 @@ public class Align { public static final int DOWN = -1; public static final int CENTER = 0; public static final int MIDDLE = 0; - + } diff --git a/src/mightypork/rogue/fonts/FontManager.java b/src/mightypork/rogue/fonts/FontManager.java index 77bf1f3..e36c0b8 100644 --- a/src/mightypork/rogue/fonts/FontManager.java +++ b/src/mightypork/rogue/fonts/FontManager.java @@ -1,6 +1,5 @@ package mightypork.rogue.fonts; - import java.awt.Font; import java.io.InputStream; import java.util.HashMap; @@ -18,16 +17,17 @@ import org.newdawn.slick.util.ResourceLoader; * @author MightyPork */ public class FontManager { - + private static final boolean DEBUG = Config.LOG_FONTS; - + + /** * Glyph tables. * * @author MightyPork */ public static class Glyphs { - + //@formatter:off /** all glyphs */ public static final String all = @@ -74,14 +74,14 @@ public class FontManager { public static final String basic = alnum + signs; //@formatter:on } - + + /** * Font style * * @author MightyPork */ - public static enum Style - { + public static enum Style { /** Normal */ NORMAL, /** Italic */ @@ -111,98 +111,102 @@ public class FontManager { /** Outline variant of normal */ OUTLINE; } - -// /** -// * Preloaded font identifier [name, size, style] -// * -// * @author MightyPork -// */ -// public static class FontId { -// -// /** font size (pt) */ -// public float size = 24; -// /** font name, registered with registerFile */ -// public String name = ""; -// /** font style. The given style must be in a file. */ -// public Style style; -// -// /** Set of glyphs in this ID */ -// public String glyphs = ""; -// -// /** Index for faster comparision of glyph ids. */ -// public int glyphset_id = 0; -// -// -// /** -// * Preloaded font identifier -// * -// * @param name font name (registerFile) -// * @param size font size (pt) -// * @param style font style -// * @param glyphs glyphs to load -// */ -// public FontId(String name, double size, Style style, String glyphs) { -// this.name = name; -// this.size = (float) size; -// this.style = style; -// -// if (glyphs.equals(Glyphs.basic)) { -// glyphset_id = 1; -// } else if (glyphs.equals(Glyphs.alnum)) { -// glyphset_id = 2; -// } else if (glyphs.equals(Glyphs.basic_text)) { -// glyphset_id = 3; -// } else if (glyphs.equals(Glyphs.numbers)) { -// glyphset_id = 4; -// } else if (glyphs.equals(Glyphs.alpha)) { -// glyphset_id = 5; -// } else if (glyphs.equals(Glyphs.all)) { -// glyphset_id = 6; -// } else if (glyphs.equals(Glyphs.alnum_extra)) { -// glyphset_id = 7; -// } else if (glyphs.equals(Glyphs.signs)) { -// glyphset_id = 8; -// } else if (glyphs.equals(Glyphs.signs_extra)) { -// glyphset_id = 9; -// } else { -// this.glyphs = glyphs; -// } -// } -// -// -// @Override -// public boolean equals(Object obj) -// { -// if (obj == null) return false; -// if (!(obj.getClass().isAssignableFrom(getClass()))) return false; -// if (obj instanceof FontId) { -// if (obj == this) return true; -// FontId id2 = ((FontId) obj); -// boolean flag = true; -// flag &= id2.size == size; -// flag &= id2.name.equals(name); -// flag &= id2.style == style; -// flag &= ((id2.glyphset_id != -1 && id2.glyphset_id == glyphset_id) || id2.glyphs.equals(glyphs)); -// return flag; -// } -// return false; -// } -// -// -// @Override -// public int hashCode() -// { -// return (new Float(size).hashCode()) ^ name.hashCode() ^ style.hashCode() ^ glyphset_id; -// } -// -// -// @Override -// public String toString() -// { -// return "[" + name + ", " + size + ", " + style + (glyphset_id > 0 ? ", g=" + glyphset_id : ", g=custom") + "]"; -// } -// } - + + + // /** + // * Preloaded font identifier [name, size, style] + // * + // * @author MightyPork + // */ + // public static class FontId { + // + // /** font size (pt) */ + // public float size = 24; + // /** font name, registered with registerFile */ + // public String name = ""; + // /** font style. The given style must be in a file. */ + // public Style style; + // + // /** Set of glyphs in this ID */ + // public String glyphs = ""; + // + // /** Index for faster comparision of glyph ids. */ + // public int glyphset_id = 0; + // + // + // /** + // * Preloaded font identifier + // * + // * @param name font name (registerFile) + // * @param size font size (pt) + // * @param style font style + // * @param glyphs glyphs to load + // */ + // public FontId(String name, double size, Style style, String glyphs) { + // this.name = name; + // this.size = (float) size; + // this.style = style; + // + // if (glyphs.equals(Glyphs.basic)) { + // glyphset_id = 1; + // } else if (glyphs.equals(Glyphs.alnum)) { + // glyphset_id = 2; + // } else if (glyphs.equals(Glyphs.basic_text)) { + // glyphset_id = 3; + // } else if (glyphs.equals(Glyphs.numbers)) { + // glyphset_id = 4; + // } else if (glyphs.equals(Glyphs.alpha)) { + // glyphset_id = 5; + // } else if (glyphs.equals(Glyphs.all)) { + // glyphset_id = 6; + // } else if (glyphs.equals(Glyphs.alnum_extra)) { + // glyphset_id = 7; + // } else if (glyphs.equals(Glyphs.signs)) { + // glyphset_id = 8; + // } else if (glyphs.equals(Glyphs.signs_extra)) { + // glyphset_id = 9; + // } else { + // this.glyphs = glyphs; + // } + // } + // + // + // @Override + // public boolean equals(Object obj) + // { + // if (obj == null) return false; + // if (!(obj.getClass().isAssignableFrom(getClass()))) return false; + // if (obj instanceof FontId) { + // if (obj == this) return true; + // FontId id2 = ((FontId) obj); + // boolean flag = true; + // flag &= id2.size == size; + // flag &= id2.name.equals(name); + // flag &= id2.style == style; + // flag &= ((id2.glyphset_id != -1 && id2.glyphset_id == glyphset_id) || + // id2.glyphs.equals(glyphs)); + // return flag; + // } + // return false; + // } + // + // + // @Override + // public int hashCode() + // { + // return (new Float(size).hashCode()) ^ name.hashCode() ^ style.hashCode() + // ^ glyphset_id; + // } + // + // + // @Override + // public String toString() + // { + // return "[" + name + ", " + size + ", " + style + (glyphset_id > 0 ? + // ", g=" + glyphset_id : ", g=custom") + "]"; + // } + // } + /** * Group of styles of one font. * @@ -210,19 +214,22 @@ public class FontManager { */ public static class FontFamily extends HashMap { } - + /** * Table of font files. name → {style:file,style:file,style:file...} */ private static HashMap fontFiles = new HashMap(); - - + + /** * Register font file. * - * @param path resource path (res/fonts/...) - * @param name font name (for binding) - * @param style font style in this file + * @param path + * resource path (res/fonts/...) + * @param name + * font name (for binding) + * @param style + * font style in this file */ public static void registerFile(String path, String name, Style style) { @@ -232,47 +239,59 @@ public class FontManager { return; } } - + // insert new table of styles to font name. FontFamily family = new FontFamily(); family.put(style, path); fontFiles.put(name, family); } - + /** Counter of loaded fonts */ public static int loadedFontCounter = 0; - - + + /** * Preload font if needed, get preloaded font.
* If needed file is not available, throws runtime exception. * - * @param name font name (registerFile) - * @param size font size (pt) - * @param style font style - * @param glyphs glyphs needed + * @param name + * font name (registerFile) + * @param size + * font size (pt) + * @param style + * font style + * @param glyphs + * glyphs needed * @return the loaded font. */ public static LoadedFont loadFont(String name, double size, Style style, String glyphs) { return loadFont(name, size, style, glyphs, 9, 8, Coord.one(), 0, 0); } - - + + /** * Preload font if needed, get preloaded font.
* If needed file is not available, throws runtime exception. * - * @param name font name (registerFile) - * @param size font size (pt) - * @param style font style - * @param glyphs glyphs needed - * @param correctLeft left horizontal correction - * @param correctRight right horizontal correction - * @param scale font scale (changing aspect ratio) - * @param clipTop top clip (0-1) - top part of the font to be cut off - * @param clipBottom bottom clip (0-1) - bottom part of the font to be cut - * off + * @param name + * font name (registerFile) + * @param size + * font size (pt) + * @param style + * font style + * @param glyphs + * glyphs needed + * @param correctLeft + * left horizontal correction + * @param correctRight + * right horizontal correction + * @param scale + * font scale (changing aspect ratio) + * @param clipTop + * top clip (0-1) - top part of the font to be cut off + * @param clipBottom + * bottom clip (0-1) - bottom part of the font to be cut off * @return the loaded font. */ public static LoadedFont loadFont(String name, double size, Style style, String glyphs, int correctLeft, int correctRight, Coord scale, double clipTop, double clipBottom) @@ -290,9 +309,9 @@ public class FontManager { } catch (NullPointerException npe) { throw new RuntimeException("Font loading failed: no font file registered for name \"" + name + "\"."); } - + InputStream in = ResourceLoader.getResourceAsStream(resourcePath); - + Font awtFont; try { awtFont = Font.createFont(Font.TRUETYPE_FONT, in); @@ -300,18 +319,18 @@ public class FontManager { Log.e("Loading of font " + resourcePath + " failed.", e); throw new RuntimeException(e); } - + awtFont = awtFont.deriveFont((float) size); // set font size LoadedFont font = new LoadedFont(awtFont, true, glyphs); - + font.setCorrection(correctLeft, correctRight); font.setClip(clipTop, clipBottom); font.setScale(scale.x, scale.y); - + loadedFontCounter++; - + if (DEBUG) Log.f3("Font from file \"" + resourcePath + "\" preloaded."); - + return font; } } diff --git a/src/mightypork/rogue/fonts/Fonts.java b/src/mightypork/rogue/fonts/Fonts.java index cd9ceeb..d42fe84 100644 --- a/src/mightypork/rogue/fonts/Fonts.java +++ b/src/mightypork/rogue/fonts/Fonts.java @@ -1,6 +1,5 @@ package mightypork.rogue.fonts; - import static mightypork.rogue.fonts.FontManager.Style.*; import mightypork.rogue.fonts.FontManager.Glyphs; import mightypork.utils.logging.Log; @@ -12,9 +11,9 @@ import mightypork.utils.logging.Log; * @author Rapus */ public class Fonts { - + public static LoadedFont splash_info; - + public static LoadedFont tooltip; public static LoadedFont gui; public static LoadedFont gui_title; @@ -22,26 +21,26 @@ public class Fonts { public static LoadedFont menu_button; public static LoadedFont menu_title; public static LoadedFont tiny; - - + + private static void registerFileNames() { FontManager.registerFile("res/fonts/4feb.ttf", "4feb", NORMAL); } - - + + /** * Load fonts needed for splash. */ public static void loadForSplash() { registerFileNames(); - + gui = FontManager.loadFont("4feb", 24, NORMAL, Glyphs.basic).setCorrection(8, 7); splash_info = FontManager.loadFont("4feb", 42, NORMAL, "Loading."); } - - + + /** * Preload all fonts we will use in the game */ @@ -53,8 +52,8 @@ public class Fonts { menu_title = FontManager.loadFont("4feb", 34, NORMAL, Glyphs.basic_text); program_number = FontManager.loadFont("4feb", 28, NORMAL, Glyphs.numbers); tiny = FontManager.loadFont("4feb", 20, NORMAL, Glyphs.basic_text); - + Log.i("Fonts loaded = " + FontManager.loadedFontCounter); - + } } diff --git a/src/mightypork/rogue/fonts/LoadedFont.java b/src/mightypork/rogue/fonts/LoadedFont.java index f459836..112d62c 100644 --- a/src/mightypork/rogue/fonts/LoadedFont.java +++ b/src/mightypork/rogue/fonts/LoadedFont.java @@ -1,6 +1,5 @@ package mightypork.rogue.fonts; - import static mightypork.rogue.fonts.Align.*; import static org.lwjgl.opengl.GL11.*; @@ -39,82 +38,83 @@ import org.lwjgl.util.glu.GLU; * @new version edited by MightyPork */ public class LoadedFont { - + private static final boolean DEBUG = Config.LOG_FONTS; - + /** Map of user defined font characters (Character <-> IntObject) */ private Map chars = new HashMap(100); - + /** Boolean flag on whether AntiAliasing is enabled or not */ private boolean antiAlias; - + /** Font's size */ private int fontSize = 0; - + /** Font's height */ private int fontHeight = 0; - + /** Texture used to cache the font 0-255 characters */ private int fontTextureID; - + /** Default font texture width */ private int textureWidth = 2048; - + /** Default font texture height */ private int textureHeight = 2048; - + /** A reference to Java's AWT Font that we create our font texture from */ private Font font; - + /** The font metrics for our Java AWT font */ private FontMetrics fontMetrics; - + private int correctL = 9, correctR = 8; - + private double defScaleX = 1, defScaleY = 1; private double clipVerticalT = 0; private double clipVerticalB = 0; - + + private class CharStorageEntry { - + /** Character's width */ public int width; - + /** Character's height */ public int height; - + /** Character's stored x position */ public int texPosX; - + /** Character's stored y position */ public int texPosY; } - - + + public LoadedFont(Font font, boolean antiAlias, String charsNeeded) { this.font = font; this.fontSize = font.getSize() + 3; this.antiAlias = antiAlias; - + createSet(charsNeeded.toCharArray()); - + fontHeight -= 1; if (fontHeight <= 0) fontHeight = 1; } - - + + public void setCorrection(boolean on) { if (on) { - correctL = 9;//2 - correctR = 8;//1 + correctL = 9;// 2 + correctR = 8;// 1 } else { correctL = 0; correctR = 0; } } - - + + private BufferedImage getFontImage(char ch) { // Create a temporary image to extract the character's size @@ -126,7 +126,7 @@ public class LoadedFont { g.setFont(font); fontMetrics = g.getFontMetrics(); int charwidth = fontMetrics.charWidth(ch) + 8; - + if (charwidth <= 0) { charwidth = 7; } @@ -134,7 +134,7 @@ public class LoadedFont { if (charheight <= 0) { charheight = fontSize; } - + // Create another image holding the character we are creating BufferedImage fontImage; fontImage = new BufferedImage(charwidth, charheight, BufferedImage.TYPE_INT_ARGB); @@ -143,28 +143,28 @@ public class LoadedFont { gt.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } gt.setFont(font); - + gt.setColor(Color.WHITE); int charx = 3; int chary = 1; gt.drawString(String.valueOf(ch), (charx), (chary) + fontMetrics.getAscent()); - + return fontImage; - + } - - + + private void createSet(char[] charsToLoad) { try { class LoadedGlyph { - + public char c; public BufferedImage image; public int width; public int height; - - + + public LoadedGlyph(char c, BufferedImage image) { this.image = image; this.c = c; @@ -172,7 +172,7 @@ public class LoadedFont { this.height = image.getHeight(); } } - + List glyphs = new ArrayList(); List loaded = new ArrayList(); for (char ch : charsToLoad) { @@ -181,35 +181,35 @@ public class LoadedFont { loaded.add(ch); } } - + Coord canvas = new Coord(128, 128); double lineHeight = 0; Coord begin = new Coord(0, 0); boolean needsLarger = false; - + while (true) { needsLarger = false; - + for (LoadedGlyph glyph : glyphs) { if (begin.x + glyph.width > canvas.x) { begin.y += lineHeight; lineHeight = 0; begin.x = 0; } - + if (lineHeight < glyph.height) { lineHeight = glyph.height; } - + if (begin.y + lineHeight > canvas.y) { needsLarger = true; break; } - + // draw. begin.x += glyph.width; } - + if (needsLarger) { canvas.x *= 2; canvas.y *= 2; @@ -220,62 +220,62 @@ public class LoadedFont { break; } } - + textureWidth = (int) canvas.x; textureHeight = (int) canvas.y; - + BufferedImage imgTemp = new BufferedImage(textureWidth, textureHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) imgTemp.getGraphics(); - + g.setColor(new Color(0, 0, 0, 1)); g.fillRect(0, 0, textureWidth, textureHeight); - + int rowHeight = 0; int positionX = 0; int positionY = 0; - + for (LoadedGlyph glyph : glyphs) { CharStorageEntry storedChar = new CharStorageEntry(); - + storedChar.width = glyph.width; storedChar.height = glyph.height; - + if (positionX + storedChar.width >= textureWidth) { positionX = 0; positionY += rowHeight; rowHeight = 0; } - + storedChar.texPosX = positionX; storedChar.texPosY = positionY; - + if (storedChar.height > fontHeight) { fontHeight = storedChar.height; } - + if (storedChar.height > rowHeight) { rowHeight = storedChar.height; } - + // Draw it here g.drawImage(glyph.image, positionX, positionY, null); - + positionX += storedChar.width; - + chars.put(glyph.c, storedChar); } - + fontTextureID = loadImage(imgTemp); - + imgTemp = null; - + } catch (Exception e) { System.err.println("Failed to create font."); e.printStackTrace(); } } - - + + private void drawQuad(double drawX, double drawY, double drawX2, double drawY2, CharStorageEntry charObj) { double srcX = charObj.texPosX + charObj.width; @@ -290,9 +290,9 @@ public class LoadedFont { double SrcHeight = srcY2 - srcY; double RenderWidth = (SrcWidth / textureWidth); double RenderHeight = (SrcHeight / textureHeight); - + drawY -= DrawHeight * clipVerticalB; - + GL11.glTexCoord2d(TextureSrcX, TextureSrcY); GL11.glVertex2d(drawX, drawY); GL11.glTexCoord2d(TextureSrcX, TextureSrcY + RenderHeight); @@ -302,8 +302,8 @@ public class LoadedFont { GL11.glTexCoord2d(TextureSrcX + RenderWidth, TextureSrcY); GL11.glVertex2d(drawX + DrawWidth, drawY); } - - + + public int getWidth(String whatchars) { if (whatchars == null) whatchars = ""; @@ -312,61 +312,61 @@ public class LoadedFont { char currentChar = 0; for (int i = 0; i < whatchars.length(); i++) { currentChar = whatchars.charAt(i); - + charStorage = chars.get(currentChar); - + if (charStorage != null) { totalwidth += charStorage.width - correctL; } } return (int) (totalwidth * defScaleX); } - - + + public int getHeight() { return (int) (fontHeight * defScaleY * (1 - clipVerticalT - clipVerticalB)); } - - + + public int getLineHeight() { return getHeight(); } - - + + public void drawString(double x, double y, String text, double scaleX, double scaleY, RGB color) { drawString(x, y, text, 0, text.length() - 1, scaleX, scaleY, color, LEFT); } - - + + public void drawString(double x, double y, String text, double scaleX, double scaleY, RGB color, int align) { drawString(x, y, text, 0, text.length() - 1, scaleX, scaleY, color, align); } - - + + private void drawString(double x, double y, String text, int startIndex, int endIndex, double scaleX, double scaleY, RGB color, int align) { x = Math.round(x); y = Math.round(y); - + scaleX *= defScaleX; scaleY *= defScaleY; - + CharStorageEntry charStorage = null; int charCurrent; - + int totalwidth = 0; int i = startIndex, d = 1, c = correctL; float startY = 0; - + switch (align) { case RIGHT: { d = -1; c = correctR; - + while (i < endIndex) { if (text.charAt(i) == '\n') startY -= getHeight(); i++; @@ -377,7 +377,7 @@ public class LoadedFont { for (int l = startIndex; l <= endIndex; l++) { charCurrent = text.charAt(l); if (charCurrent == '\n') break; - + charStorage = chars.get((char) charCurrent); if (charStorage != null) { totalwidth += charStorage.width - correctL; @@ -392,20 +392,20 @@ public class LoadedFont { c = correctL; break; } - + } - + GL11.glPushAttrib(GL_ENABLE_BIT); GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glBindTexture(GL11.GL_TEXTURE_2D, fontTextureID); GL11.glColor4d(color.r, color.g, color.b, color.a); GL11.glBegin(GL11.GL_QUADS); - + while (i >= startIndex && i <= endIndex) { charCurrent = text.charAt(i); - + charStorage = chars.get(new Character((char) charCurrent)); - + if (charStorage != null) { if (d < 0) totalwidth += (charStorage.width - c) * d; if (charCurrent == '\n') { @@ -415,14 +415,14 @@ public class LoadedFont { for (int l = i + 1; l <= endIndex; l++) { charCurrent = text.charAt(l); if (charCurrent == '\n') break; - + charStorage = chars.get((char) charCurrent); - + totalwidth += charStorage.width - correctL; } totalwidth /= -2; } - //if center get next lines total width/2; + // if center get next lines total width/2; } else { //@formatter:off drawQuad( @@ -432,25 +432,26 @@ public class LoadedFont { charStorage ); //@formatter:on - + if (d > 0) totalwidth += (charStorage.width - c) * d; } - + } - + i += d; } GL11.glEnd(); GL11.glPopAttrib(); } - - + + public static int loadImage(BufferedImage bufferedImage) { try { short width = (short) bufferedImage.getWidth(); short height = (short) bufferedImage.getHeight(); - //textureLoader.bpp = bufferedImage.getColorModel().hasAlpha() ? (byte)32 : (byte)24; + // textureLoader.bpp = bufferedImage.getColorModel().hasAlpha() ? + // (byte)32 : (byte)24; int bpp = (byte) bufferedImage.getColorModel().getPixelSize(); ByteBuffer byteBuffer; DataBuffer db = bufferedImage.getData().getDataBuffer(); @@ -460,45 +461,45 @@ public class LoadedFont { for (int i = 0; i < intI.length; i++) { byte b[] = intToByteArray(intI[i]); int newIndex = i * 4; - + newI[newIndex] = b[1]; newI[newIndex + 1] = b[2]; newI[newIndex + 2] = b[3]; newI[newIndex + 3] = b[0]; } - + byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)).order(ByteOrder.nativeOrder()).put(newI); } else { byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)).order(ByteOrder.nativeOrder()).put(((DataBufferByte) (bufferedImage.getData().getDataBuffer())).getData()); } byteBuffer.flip(); - + int internalFormat = GL11.GL_RGBA8, format = GL11.GL_RGBA; IntBuffer textureId = BufferUtils.createIntBuffer(1); - + GL11.glGenTextures(textureId); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId.get(0)); - + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP); - + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); - + GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE); - + GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, internalFormat, width, height, format, GL11.GL_UNSIGNED_BYTE, byteBuffer); return textureId.get(0); - + } catch (Exception e) { e.printStackTrace(); System.exit(-1); } - + return -1; } - - + + public static boolean isSupported(String fontname) { Font font[] = getFonts(); @@ -507,14 +508,14 @@ public class LoadedFont { } return false; } - - + + public static Font[] getFonts() { return GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); } - - + + public static Font getFont(String fontname, int style, float size) { Font result = null; @@ -527,14 +528,14 @@ public class LoadedFont { } return result; } - - + + public static byte[] intToByteArray(int value) { return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value }; } - - + + public void destroy() { IntBuffer scratch = BufferUtils.createIntBuffer(1); @@ -542,82 +543,91 @@ public class LoadedFont { GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); GL11.glDeleteTextures(scratch); } - - + + public LoadedFont setScale(double x, double y) { defScaleX = x; defScaleY = y; return this; } - - + + public LoadedFont setClip(double clipRatioTop, double clipRatioBottom) { clipVerticalT = clipRatioTop; clipVerticalB = clipRatioBottom; return this; } - - + + public LoadedFont setCorrection(int correctionLeft, int correctionRight) { correctL = correctionLeft; correctR = correctionRight; return this; } - - + + /** * Draw string with font. * - * @param x x coord - * @param y y coord - * @param text text to draw - * @param color render color - * @param align (-1,0,1) + * @param x + * x coord + * @param y + * y coord + * @param text + * text to draw + * @param color + * render color + * @param align + * (-1,0,1) */ public void draw(double x, double y, String text, RGB color, int align) { drawString(x, y, text, 1, 1, color, align); } - - + + /** * Draw string with font. * - * @param pos coord - * @param text text to draw - * @param color render color - * @param align (-1,0,1) + * @param pos + * coord + * @param text + * text to draw + * @param color + * render color + * @param align + * (-1,0,1) */ public void draw(Coord pos, String text, RGB color, int align) { drawString(pos.x, pos.y, text, 1, 1, color, align); } - - + + public void drawFuzzy(Coord pos, String text, int align, RGB textColor, RGB blurColor, int blurSize) { drawFuzzy(pos, text, align, textColor, blurColor, blurSize, true); } - - + + public void drawFuzzy(Coord pos, String text, int align, RGB textColor, RGB blurColor, int blurSize, boolean smooth) { glPushMatrix(); - + glTranslated(pos.x, pos.y, pos.z); - - //shadow + + // shadow int sh = blurSize; - + int l = glGenLists(1); - + glNewList(l, GL_COMPILE); draw(0, 0, text, blurColor, align); glEndList(); - + for (int xx = -sh; xx <= sh; xx += (smooth ? 1 : sh)) { for (int yy = -sh; yy <= sh; yy += (smooth ? 1 : sh)) { if (xx == 0 && yy == 0) continue; @@ -627,12 +637,12 @@ public class LoadedFont { glPopMatrix(); } } - + glDeleteLists(l, 1); - + draw(0, 0, text, textColor, align); - + glPopMatrix(); } - + } diff --git a/src/mightypork/rogue/input/InputSystem.java b/src/mightypork/rogue/input/InputSystem.java index 6f6d4cd..8a67214 100644 --- a/src/mightypork/rogue/input/InputSystem.java +++ b/src/mightypork/rogue/input/InputSystem.java @@ -1,6 +1,5 @@ package mightypork.rogue.input; - import mightypork.rogue.AppAccess; import mightypork.rogue.bus.DelegatingBusClient; import mightypork.rogue.bus.events.KeyboardEvent; @@ -15,37 +14,37 @@ import org.lwjgl.opengl.Display; public class InputSystem extends DelegatingBusClient implements KeyBinder { - + // listeners private KeyBindingPool keybindings; - - + + public InputSystem(AppAccess app) { super(app, true); } - - + + @Override protected void init() { initDevices(); - + initChannels(); - + // global keybindings keybindings = new KeyBindingPool(); addChildSubscriber(keybindings); } - - + + @Override public void deinit() { Mouse.destroy(); Keyboard.destroy(); } - - + + private void initDevices() { try { @@ -56,45 +55,45 @@ public class InputSystem extends DelegatingBusClient implements KeyBinder { throw new RuntimeException("Failed to initialize input devices.", e); } } - - + + private void initChannels() { bus().createChannel(KeyboardEvent.class, KeyboardEvent.Listener.class); bus().createChannel(MouseMotionEvent.class, MouseMotionEvent.Listener.class); bus().createChannel(MouseButtonEvent.class, MouseButtonEvent.Listener.class); } - - + + @Override public void bindKeyStroke(KeyStroke stroke, Runnable task) { keybindings.bindKeyStroke(stroke, task); } - - + + @Override public void unbindKeyStroke(KeyStroke stroke) { keybindings.unbindKeyStroke(stroke); } - - + + @Override public void update(double delta) { Display.processMessages(); - + while (Mouse.next()) { onMouseEvent(); } - + while (Keyboard.next()) { onKeyEvent(); } } - - + + private void onMouseEvent() { int button = Mouse.getEventButton(); @@ -102,12 +101,12 @@ public class InputSystem extends DelegatingBusClient implements KeyBinder { Coord pos = new Coord(Mouse.getEventX(), Mouse.getEventY()); Coord move = new Coord(Mouse.getEventDX(), Mouse.getEventDY()); int wheeld = Mouse.getEventDWheel(); - + if (button != -1 || wheeld != 0) bus().broadcast(new MouseButtonEvent(pos, button, down, wheeld)); if (!move.isZero()) bus().broadcast(new MouseMotionEvent(pos, move)); } - - + + private void onKeyEvent() { int key = Keyboard.getEventKey(); diff --git a/src/mightypork/rogue/input/KeyBinder.java b/src/mightypork/rogue/input/KeyBinder.java index 56a1e23..15b36a0 100644 --- a/src/mightypork/rogue/input/KeyBinder.java +++ b/src/mightypork/rogue/input/KeyBinder.java @@ -1,22 +1,24 @@ package mightypork.rogue.input; - public interface KeyBinder { - + /** * Bind handler to a keystroke, replace current handler if any * - * @param stroke trigger keystroke - * @param task handler + * @param stroke + * trigger keystroke + * @param task + * handler */ abstract void bindKeyStroke(KeyStroke stroke, Runnable task); - - + + /** * Remove handler from a keystroke (id any) * - * @param stroke stroke + * @param stroke + * stroke */ abstract void unbindKeyStroke(KeyStroke stroke); - + } diff --git a/src/mightypork/rogue/input/KeyBinding.java b/src/mightypork/rogue/input/KeyBinding.java index d334fe1..a6c9645 100644 --- a/src/mightypork/rogue/input/KeyBinding.java +++ b/src/mightypork/rogue/input/KeyBinding.java @@ -1,48 +1,47 @@ package mightypork.rogue.input; - import mightypork.rogue.bus.events.KeyboardEvent; public class KeyBinding implements KeyboardEvent.Listener { - + private KeyStroke keystroke; private Runnable handler; private boolean wasActive = false; - - + + public KeyBinding(KeyStroke stroke, Runnable handler) { this.keystroke = stroke; this.handler = handler; - + wasActive = keystroke.isActive(); } - - + + public boolean matches(KeyStroke stroke) { return this.keystroke.equals(stroke); } - - + + public void setHandler(Runnable handler) { this.handler = handler; } - - + + @Override public void receive(KeyboardEvent event) { // ignore unrelated events if (!keystroke.getKeys().contains(event.getKey())) return; - + // run handler when event was met if (keystroke.isActive() && !wasActive) { handler.run(); } - + wasActive = keystroke.isActive(); } - + } diff --git a/src/mightypork/rogue/input/KeyBindingPool.java b/src/mightypork/rogue/input/KeyBindingPool.java index cc98703..65834b5 100644 --- a/src/mightypork/rogue/input/KeyBindingPool.java +++ b/src/mightypork/rogue/input/KeyBindingPool.java @@ -1,6 +1,5 @@ package mightypork.rogue.input; - import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -15,15 +14,17 @@ import mightypork.utils.logging.Log; * @author MightyPork */ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener { - + private Set bindings = new HashSet(); - - + + /** * Bind handler to a keystroke, replace current handler if any * - * @param stroke trigger keystroke - * @param task handler + * @param stroke + * trigger keystroke + * @param task + * handler */ @Override public void bindKeyStroke(KeyStroke stroke, Runnable task) @@ -35,21 +36,22 @@ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener { return; } } - + bindings.add(new KeyBinding(stroke, task)); } - - + + /** * Remove handler from keystroke (id any) * - * @param stroke stroke + * @param stroke + * stroke */ @Override public void unbindKeyStroke(KeyStroke stroke) { Iterator iter = bindings.iterator(); - + while (iter.hasNext()) { KeyBinding kb = iter.next(); if (kb.matches(stroke)) { @@ -58,8 +60,8 @@ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener { } } } - - + + @Override public void receive(KeyboardEvent event) { diff --git a/src/mightypork/rogue/input/KeyStroke.java b/src/mightypork/rogue/input/KeyStroke.java index 4faea8e..739d4e1 100644 --- a/src/mightypork/rogue/input/KeyStroke.java +++ b/src/mightypork/rogue/input/KeyStroke.java @@ -1,6 +1,5 @@ package mightypork.rogue.input; - import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; @@ -9,16 +8,18 @@ import org.lwjgl.input.Keyboard; public class KeyStroke { - + private Set keys = new LinkedHashSet(); private boolean down = true; - - + + /** * KeyStroke * - * @param down true for falling edge, up for rising edge - * @param keys keys that must be pressed + * @param down + * true for falling edge, up for rising edge + * @param keys + * keys that must be pressed */ public KeyStroke(boolean down, int... keys) { this.down = down; @@ -26,8 +27,8 @@ public class KeyStroke { this.keys.add(k); } } - - + + /** * Falling edge keystroke * @@ -38,25 +39,25 @@ public class KeyStroke { this.keys.add(k); } } - - + + public boolean isActive() { boolean st = true; for (int k : keys) { st &= Keyboard.isKeyDown(k); } - + return down ? st : !st; } - - + + public void setKeys(Set keys) { this.keys = keys; } - - + + @Override public int hashCode() { @@ -65,8 +66,8 @@ public class KeyStroke { result = prime * result + ((keys == null) ? 0 : keys.hashCode()); return result; } - - + + @Override public boolean equals(Object obj) { @@ -74,39 +75,39 @@ public class KeyStroke { if (obj == null) return false; if (!(obj instanceof KeyStroke)) return false; KeyStroke other = (KeyStroke) obj; - + if (keys == null) { if (other.keys != null) return false; } else if (!keys.equals(other.keys)) { return false; } - + if (down != other.down) return false; - + return true; } - - + + @Override public String toString() { String s = "("; - + int cnt = 0; Iterator i = keys.iterator(); for (; i.hasNext(); cnt++) { if (cnt > 0) s += "+"; s += Keyboard.getKeyName(i.next()); } - + s += down ? ",DOWN" : "UP"; - + s += ")"; - + return s; } - - + + public Set getKeys() { return keys; diff --git a/src/mightypork/rogue/sounds/AudioX.java b/src/mightypork/rogue/sounds/AudioX.java index 5cf31c7..cae7dd4 100644 --- a/src/mightypork/rogue/sounds/AudioX.java +++ b/src/mightypork/rogue/sounds/AudioX.java @@ -1,6 +1,5 @@ package mightypork.rogue.sounds; - import mightypork.utils.files.FileUtils; import mightypork.utils.logging.Log; import mightypork.utils.math.coord.Coord; @@ -16,12 +15,11 @@ import org.newdawn.slick.openal.SoundStore; * @author MightyPork */ public class AudioX implements Destroyable { - - private enum PlayMode - { + + private enum PlayMode { EFFECT, MUSIC; }; - + private Audio audio = null; private double pauseLoopPosition = 0; private boolean looping = false; @@ -29,37 +27,38 @@ public class AudioX implements Destroyable { private PlayMode mode = PlayMode.EFFECT; private double lastPlayPitch = 1; private double lastPlayGain = 1; - + private final String resourcePath; private boolean loadFailed = false; - - + + /** * Create deferred primitive audio player * - * @param resourceName resource to load when needed + * @param resourceName + * resource to load when needed */ public AudioX(String resourceName) { this.audio = null; this.resourcePath = resourceName; } - - + + /** * Pause loop (remember position and stop playing) - if was looping */ public void pauseLoop() { if (!load()) return; - + if (isPlaying() && looping) { pauseLoopPosition = audio.getPosition(); stop(); paused = true; } } - - + + /** * Resume loop (if was paused) * @@ -68,7 +67,7 @@ public class AudioX implements Destroyable { public int resumeLoop() { if (!load()) return -1; - + int source = -1; if (looping && paused) { if (mode == PlayMode.MUSIC) { @@ -81,8 +80,8 @@ public class AudioX implements Destroyable { } return source; } - - + + /** * Check if resource is loaded * @@ -92,8 +91,8 @@ public class AudioX implements Destroyable { { return audio != null; } - - + + /** * Try to load if not loaded already * @@ -102,12 +101,14 @@ public class AudioX implements Destroyable { public boolean load() { if (isLoaded()) return true; // already loaded - if (loadFailed || resourcePath == null) return false; // not loaded, but can't load anyway - + if (loadFailed || resourcePath == null) return false; // not loaded, but + // can't load + // anyway + loadFailed = false; try { String ext = FileUtils.getExtension(resourcePath); - + // java 6 can't use String switch :( if (ext.equalsIgnoreCase("ogg")) { audio = SoundStore.get().getOgg(resourcePath); @@ -121,61 +122,67 @@ public class AudioX implements Destroyable { Log.e("Invalid audio file extension: " + resourcePath); loadFailed = true; // don't try next time } - + } catch (Exception e) { Log.e("Could not load " + resourcePath, e); loadFailed = true; // don't try next time } - + return isLoaded(); } - - + + public void stop() { if (!isLoaded()) return; - + audio.stop(); paused = false; } - - + + public boolean isPlaying() { if (!isLoaded()) return false; - + return audio.isPlaying(); } - - + + public boolean isPaused() { if (!isLoaded()) return false; - + return audio.isPaused(); } - - + + /** * Play as sound effect at listener position * - * @param pitch pitch (1 = default) - * @param gain gain (0-1) - * @param loop looping + * @param pitch + * pitch (1 = default) + * @param gain + * gain (0-1) + * @param loop + * looping * @return source id */ public int playAsEffect(double pitch, double gain, boolean loop) { return playAsEffect(pitch, gain, loop, SoundSystem.getListener()); } - - + + /** * Play as sound effect at given X-Y position * - * @param pitch pitch (1 = default) - * @param gain gain (0-1) - * @param loop looping + * @param pitch + * pitch (1 = default) + * @param gain + * gain (0-1) + * @param loop + * looping * @param x * @param y * @return source id @@ -184,14 +191,17 @@ public class AudioX implements Destroyable { { return playAsEffect(pitch, gain, loop, x, y, SoundSystem.getListener().z); } - - + + /** * Play as sound effect at given position * - * @param pitch pitch (1 = default) - * @param gain gain (0-1) - * @param loop looping + * @param pitch + * pitch (1 = default) + * @param gain + * gain (0-1) + * @param loop + * looping * @param x * @param y * @param z @@ -200,63 +210,70 @@ public class AudioX implements Destroyable { public int playAsEffect(double pitch, double gain, boolean loop, double x, double y, double z) { if (!load()) return -1; - + this.lastPlayPitch = pitch; this.lastPlayGain = gain; looping = loop; mode = PlayMode.EFFECT; return audio.playAsSoundEffect((float) pitch, (float) gain, loop, (float) x, (float) y, (float) z); } - - + + /** * Play as sound effect at given position * - * @param pitch pitch (1 = default) - * @param gain gain (0-1) - * @param loop looping - * @param pos coord + * @param pitch + * pitch (1 = default) + * @param gain + * gain (0-1) + * @param loop + * looping + * @param pos + * coord * @return source id */ public int playAsEffect(double pitch, double gain, boolean loop, Coord pos) { if (!load()) return -1; - + return playAsEffect(pitch, gain, loop, pos.x, pos.y, pos.z); } - - + + /** * Play as music using source 0.
* Discouraged, since this does not allow cross-fading. * - * @param pitch play pitch - * @param gain play gain - * @param loop looping + * @param pitch + * play pitch + * @param gain + * play gain + * @param loop + * looping * @return source */ public int playAsMusic(double pitch, double gain, boolean loop) { if (!load()) return -1; - + this.lastPlayPitch = (float) pitch; this.lastPlayGain = (float) gain; looping = loop; mode = PlayMode.MUSIC; return audio.playAsMusic((float) pitch, (float) gain, loop); } - - + + @Override public void destroy() { if (!isLoaded()) return; - + audio.release(); audio = null; } - - + + @Override public int hashCode() { @@ -265,8 +282,8 @@ public class AudioX implements Destroyable { result = prime * result + ((resourcePath == null) ? 0 : resourcePath.hashCode()); return result; } - - + + @Override public boolean equals(Object obj) { @@ -281,5 +298,5 @@ public class AudioX implements Destroyable { } return true; } - + } diff --git a/src/mightypork/rogue/sounds/BaseAudioPlayer.java b/src/mightypork/rogue/sounds/BaseAudioPlayer.java index f4d81e2..ac477c5 100644 --- a/src/mightypork/rogue/sounds/BaseAudioPlayer.java +++ b/src/mightypork/rogue/sounds/BaseAudioPlayer.java @@ -1,66 +1,65 @@ package mightypork.rogue.sounds; - import mightypork.utils.objects.Mutable; public abstract class BaseAudioPlayer { - + /** the track */ private AudioX audio; - + /** base gain for sfx */ private double baseGain = 1; - + /** base pitch for sfx */ private double basePitch = 1; - + /** dedicated volume control */ private Mutable gainMultiplier = null; - - + + public BaseAudioPlayer(AudioX track, double baseGain, Mutable gainMultiplier) { this(track, 1, baseGain, gainMultiplier); } - - + + public BaseAudioPlayer(AudioX track, double basePitch, double baseGain, Mutable gainMultiplier) { this.audio = track; - + this.baseGain = baseGain; this.basePitch = basePitch; - + if (gainMultiplier == null) gainMultiplier = new Mutable(1D); - + this.gainMultiplier = gainMultiplier; } - - + + public void destroy() { audio.destroy(); audio = null; } - - + + protected AudioX getAudio() { return audio; } - - + + protected double getGain(double multiplier) { return baseGain * gainMultiplier.get() * multiplier; } - - + + protected double getPitch(double multiplier) { return basePitch * multiplier; } - - + + /** * Get if audio is valid * @@ -70,8 +69,8 @@ public abstract class BaseAudioPlayer { { return (audio != null); } - - + + public void load() { if (canPlay()) audio.load(); diff --git a/src/mightypork/rogue/sounds/EffectPlayer.java b/src/mightypork/rogue/sounds/EffectPlayer.java index ee26718..1f51ca9 100644 --- a/src/mightypork/rogue/sounds/EffectPlayer.java +++ b/src/mightypork/rogue/sounds/EffectPlayer.java @@ -1,36 +1,35 @@ package mightypork.rogue.sounds; - import mightypork.utils.math.coord.Coord; import mightypork.utils.objects.Mutable; public class EffectPlayer extends BaseAudioPlayer { - + public EffectPlayer(AudioX track, double basePitch, double baseGain, Mutable gainMultiplier) { super(track, (float) basePitch, (float) baseGain, gainMultiplier); } - - + + public int play(double pitch, double gain) { if (!canPlay()) return -1; - + return getAudio().playAsEffect(getPitch(pitch), getGain(gain), false); } - - + + public int play(double gain) { return play(1, gain); } - - + + public int play(double pitch, double gain, Coord pos) { if (!canPlay()) return -1; - + return getAudio().playAsEffect(getPitch(pitch), getGain(gain), false, pos); } - + } diff --git a/src/mightypork/rogue/sounds/JointVolume.java b/src/mightypork/rogue/sounds/JointVolume.java index a314b3f..c2b1973 100644 --- a/src/mightypork/rogue/sounds/JointVolume.java +++ b/src/mightypork/rogue/sounds/JointVolume.java @@ -1,6 +1,5 @@ package mightypork.rogue.sounds; - import mightypork.utils.math.Calc; import mightypork.utils.objects.Mutable; @@ -11,21 +10,22 @@ import mightypork.utils.objects.Mutable; * @author MightyPork */ public class JointVolume extends Mutable { - + private Mutable[] volumes; - - + + /** * Create joint volume with master gain of 1 * - * @param volumes individual volumes to join + * @param volumes + * individual volumes to join */ public JointVolume(Mutable... volumes) { super(1D); this.volumes = volumes; } - - + + /** * Get combined gain (multiplied) */ @@ -35,11 +35,11 @@ public class JointVolume extends Mutable { double d = super.get(); for (Mutable v : volumes) d *= v.get(); - + return Calc.clampd(d, 0, 1); } - - + + /** * Set master gain */ diff --git a/src/mightypork/rogue/sounds/LoopPlayer.java b/src/mightypork/rogue/sounds/LoopPlayer.java index ef937e4..81f4d19 100644 --- a/src/mightypork/rogue/sounds/LoopPlayer.java +++ b/src/mightypork/rogue/sounds/LoopPlayer.java @@ -1,6 +1,5 @@ package mightypork.rogue.sounds; - import mightypork.utils.objects.Mutable; import mightypork.utils.time.Pauseable; import mightypork.utils.time.Updateable; @@ -10,38 +9,38 @@ import org.lwjgl.openal.AL10; public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable { - + private int sourceID = -1; - + /** animator for fade in and fade out */ private AnimDouble fadeAnim = new AnimDouble(0); - + private double lastUpdateGain = 0; - + /** flag that track is paused */ private boolean paused = true; - + /** Default fadeIn time */ private double inTime = 1; - + /** Default fadeOut time */ private double outTime = 1; - - + + public LoopPlayer(AudioX track, double pitch, double baseGain, Mutable gainMultiplier) { super(track, (float) pitch, (float) baseGain, gainMultiplier); - + paused = true; } - - + + public void setFadeTimes(double in, double out) { inTime = in; outTime = out; } - - + + private void initLoop() { if (!canPlay() && sourceID == -1) { @@ -49,84 +48,84 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable getAudio().pauseLoop(); } } - - + + @Override public void pause() { if (!canPlay() || paused) return; - + initLoop(); - + getAudio().pauseLoop(); paused = true; } - - + + @Override public boolean isPaused() { return paused; } - - + + @Override public void resume() { if (!canPlay() || paused) return; - + initLoop(); - + sourceID = getAudio().resumeLoop(); paused = false; } - - + + @Override public void update(double delta) { if (!canPlay() || paused) return; - + initLoop(); - + fadeAnim.update(delta); - + double gain = getGain(fadeAnim.getCurrentValue()); if (!paused && gain != lastUpdateGain) { AL10.alSourcef(sourceID, AL10.AL_GAIN, (float) gain); lastUpdateGain = gain; } - + if (gain == 0 && !paused) pause(); // pause on zero volume } - - + + public void fadeIn(double secs) { if (!canPlay()) return; - + resume(); fadeAnim.fadeIn(secs); } - - + + public void fadeOut(double secs) { if (!canPlay()) return; - + fadeAnim.fadeOut(secs); } - - + + public void fadeIn() { fadeIn(inTime); } - - + + public void fadeOut() { fadeOut(outTime); } - + } diff --git a/src/mightypork/rogue/sounds/NullAudio.java b/src/mightypork/rogue/sounds/NullAudio.java index e3417ff..5eacb88 100644 --- a/src/mightypork/rogue/sounds/NullAudio.java +++ b/src/mightypork/rogue/sounds/NullAudio.java @@ -1,17 +1,16 @@ package mightypork.rogue.sounds; - public class NullAudio extends AudioX { - + public NullAudio() { super(""); } - - + + @Override public boolean load() { return false; } - + } diff --git a/src/mightypork/rogue/sounds/SoundSystem.java b/src/mightypork/rogue/sounds/SoundSystem.java index c3a2718..7bc4d9e 100644 --- a/src/mightypork/rogue/sounds/SoundSystem.java +++ b/src/mightypork/rogue/sounds/SoundSystem.java @@ -1,6 +1,5 @@ package mightypork.rogue.sounds; - import java.nio.FloatBuffer; import java.util.HashMap; import java.util.HashSet; @@ -26,24 +25,24 @@ import org.newdawn.slick.openal.SoundStore; */ @SuppressWarnings("unchecked") public class SoundSystem extends DelegatingBusClient { - + private static final Coord INITIAL_LISTENER_POS = new Coord(0, 0, 0); private static final int MAX_SOURCES = 256; private static final AudioX NO_SOUND = new NullAudio(); private static final LoopPlayer NULL_LOOP = new LoopPlayer(NO_SOUND, 0, 0, null); private static final EffectPlayer NULL_EFFECT = new EffectPlayer(NO_SOUND, 0, 0, null); - + private static Coord listener = new Coord(); - + static { // initialize sound system SoundStore.get().setMaxSources(MAX_SOURCES); SoundStore.get().init(); - + setListener(INITIAL_LISTENER_POS); } - - + + /** * Set listener pos * @@ -65,42 +64,42 @@ public class SoundSystem extends DelegatingBusClient { AL10.alListener(AL10.AL_ORIENTATION, buf6); buf3 = buf6 = null; } - + // -- instance -- - + public Mutable masterVolume = new Mutable(1D); public Mutable effectsVolume = new JointVolume(masterVolume); public Mutable loopsVolume = new JointVolume(masterVolume); - + private Map effects = new HashMap(); private Map loops = new HashMap(); private Set resources = new HashSet(); - - + + public SoundSystem(AppAccess app) { super(app, true); } - - + + @Override protected void init() { // empty } - - + + @Override public void deinit() { for (AudioX r : resources) { r.destroy(); } - + SoundStore.get().clear(); AL.destroy(); } - - + + @Override public void update(double delta) { @@ -108,38 +107,48 @@ public class SoundSystem extends DelegatingBusClient { lp.update(delta); } } - - + + public static Coord getListener() { return listener; } - - + + /** * Register effect resource * - * @param key sound key - * @param resource resource path - * @param pitch default pitch (1 = unchanged) - * @param gain default gain (0-1) + * @param key + * sound key + * @param resource + * resource path + * @param pitch + * default pitch (1 = unchanged) + * @param gain + * default gain (0-1) */ public void addEffect(String key, String resource, double pitch, double gain) { EffectPlayer p = new EffectPlayer(getResource(resource), pitch, gain, effectsVolume); effects.put(key, p); } - - + + /** * Register loop resource (music / effect loop) * - * @param key sound key - * @param resource resource path - * @param pitch default pitch (1 = unchanged) - * @param gain default gain (0-1) - * @param fadeIn default time for fadeIn - * @param fadeOut default time for fadeOut + * @param key + * sound key + * @param resource + * resource path + * @param pitch + * default pitch (1 = unchanged) + * @param gain + * default gain (0-1) + * @param fadeIn + * default time for fadeIn + * @param fadeOut + * default time for fadeOut */ public void addLoop(String key, String resource, double pitch, double gain, double fadeIn, double fadeOut) { @@ -147,14 +156,16 @@ public class SoundSystem extends DelegatingBusClient { p.setFadeTimes(fadeIn, fadeOut); loops.put(key, p); } - - + + /** * Create {@link AudioX} for a resource * - * @param res a resource name + * @param res + * a resource name * @return the resource - * @throws IllegalArgumentException if resource is already registered + * @throws IllegalArgumentException + * if resource is already registered */ private AudioX getResource(String res) { @@ -163,12 +174,13 @@ public class SoundSystem extends DelegatingBusClient { resources.add(a); return a; } - - + + /** * Get a loop player for key * - * @param key sound key + * @param key + * sound key * @return loop player */ public LoopPlayer getLoop(String key) @@ -180,12 +192,13 @@ public class SoundSystem extends DelegatingBusClient { } return p; } - - + + /** * Get a effect player for key * - * @param key sound key + * @param key + * sound key * @return effect player */ public EffectPlayer getEffect(String key) @@ -197,8 +210,8 @@ public class SoundSystem extends DelegatingBusClient { } return p; } - - + + /** * Fade out all loops (ie. for screen transitions) */ @@ -208,65 +221,72 @@ public class SoundSystem extends DelegatingBusClient { p.fadeOut(); } } - - + + /** * Fade in a loop (with default time) * - * @param key sound key + * @param key + * sound key */ public void fadeInLoop(String key) { getLoop(key).fadeIn(); } - - + + /** * Fade in a loop * - * @param key sound key - * @param seconds fade-in duration + * @param key + * sound key + * @param seconds + * fade-in duration */ public void fadeInLoop(String key, double seconds) { getLoop(key).fadeIn(seconds); } - - + + /** * Fade out a loop (with default time) * - * @param key sound key + * @param key + * sound key */ public void fadeOutLoop(String key) { getLoop(key).fadeOut(); } - - + + /** * Fade out a loop * - * @param key sound key - * @param seconds fade-out duration + * @param key + * sound key + * @param seconds + * fade-out duration */ public void fadeOutLoop(String key, double seconds) { getLoop(key).fadeOut(seconds); } - - + + /** * Pause a loop * - * @param key sound key + * @param key + * sound key */ public void pauseLoop(String key) { getLoop(key).pause(); } - - + + /** * Pause all loops (leave volume unchanged) */ @@ -276,45 +296,49 @@ public class SoundSystem extends DelegatingBusClient { p.pause(); } } - - + + /** * Resume a loop * - * @param key sound key + * @param key + * sound key */ public void resumeLoop(String key) { getLoop(key).resume(); } - - + + /** * Set level of master volume * - * @param d level + * @param d + * level */ public void setMasterVolume(double d) { masterVolume.set(d); } - - + + /** * Set level of effects volume * - * @param d level + * @param d + * level */ public void setEffectsVolume(double d) { effectsVolume.set(d); } - - + + /** * Set level of music volume * - * @param d level + * @param d + * level */ public void setMusicVolume(double d) { diff --git a/src/mightypork/rogue/tasks/TaskTakeScreenshot.java b/src/mightypork/rogue/tasks/TaskTakeScreenshot.java index 1932574..46afb43 100644 --- a/src/mightypork/rogue/tasks/TaskTakeScreenshot.java +++ b/src/mightypork/rogue/tasks/TaskTakeScreenshot.java @@ -1,6 +1,5 @@ package mightypork.rogue.tasks; - import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -16,20 +15,20 @@ import mightypork.utils.logging.Log; public class TaskTakeScreenshot implements Runnable { - + private BufferedImage image; - - + + public TaskTakeScreenshot(DisplaySystem disp) { this.image = disp.takeScreenshot(); } - - + + @Override public void run() { String fname = getUniqueScreenshotName(); - + // generate unique filename File file; int index = 0; @@ -38,11 +37,11 @@ public class TaskTakeScreenshot implements Runnable { if (!file.exists()) break; index++; } - + Log.f3("Saving screenshot to file: " + file); - + String format = "PNG"; - + // save to disk try { ImageIO.write(image, format, file); @@ -50,12 +49,12 @@ public class TaskTakeScreenshot implements Runnable { Log.e("Failed to save screenshot.", e); } } - - + + private String getUniqueScreenshotName() { DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); return df.format(new Date()); } - + } diff --git a/src/mightypork/rogue/testing/TestConstraints.java b/src/mightypork/rogue/testing/TestConstraints.java index d56ad4f..aeddb19 100644 --- a/src/mightypork/rogue/testing/TestConstraints.java +++ b/src/mightypork/rogue/testing/TestConstraints.java @@ -1,6 +1,5 @@ package mightypork.rogue.testing; - import mightypork.rogue.display.constraints.Bounding; import mightypork.rogue.display.constraints.Constraint; import mightypork.utils.math.coord.Coord; @@ -8,56 +7,56 @@ import mightypork.utils.math.coord.Rect; public class TestConstraints { - + public static void main(String[] args) { Bounding context = new Bounding() { - + @Override public Rect getRect() { return Rect.fromSize(new Coord(0, 0), new Coord(400, 300)); } }; - + class Navbar extends Constraint { - + private double height; - - + + public Navbar(Bounding context, double height) { super(context); this.height = height; } - - + + @Override public Rect getRect() { return Rect.fromSize(origin().setY(size().y - height), size().setY(height)); } } - + class TileHorizontal extends Constraint { - + private int count; private int tile; - - + + public TileHorizontal(Bounding context, int tileCount, int aTile) { super(context); this.count = tileCount; setTile(aTile); } - - + + public void setTile(int aTile) { if (aTile > count) throw new IndexOutOfBoundsException("Tile count exceeded: " + aTile + " max: " + count); this.tile = aTile; } - - + + @Override public Rect getRect() { @@ -65,20 +64,20 @@ public class TestConstraints { return Rect.fromSize(origin().add(size.x * tile, 0), size); } } - + Navbar nb = new Navbar(context, 100); - + TileHorizontal tile = new TileHorizontal(nb, 5, 0); - + for (int i = 0; i < 5; i++) { tile.setTile(i); - + System.out.println(tile.getRect()); } - + System.out.println("nb:" + nb.getRect()); - + System.out.println("ctx:" + context.getRect()); } - + } diff --git a/src/mightypork/rogue/testing/TestMsgbus.java b/src/mightypork/rogue/testing/TestMsgbus.java index 540b52d..9ca83ef 100644 --- a/src/mightypork/rogue/testing/TestMsgbus.java +++ b/src/mightypork/rogue/testing/TestMsgbus.java @@ -1,6 +1,5 @@ package mightypork.rogue.testing; - import java.io.File; import java.util.ArrayList; import java.util.Collection; @@ -14,16 +13,16 @@ import mightypork.utils.patterns.subscription.clients.ToggleableClient; public class TestMsgbus { - + public static void main(String[] args) { Log.create("runtime", new File("."), 0); - + MessageBus bus = new MessageBus(); - + bus.createChannel(StringMessage.class, StringMessage.Listener.class); bus.createChannel(IntMessage.class, IntMessage.Listener.class); - + Delegator deleg1 = new Delegator("Deleg1"); Delegator deleg2 = new Delegator("Deleg2"); Toggleable togg1 = new Toggleable("Tog1"); @@ -31,61 +30,61 @@ public class TestMsgbus { Toggleable plain1 = new Toggleable("Plain1"); Toggleable plain2 = new Toggleable("Plain2"); Toggleable plain3 = new Toggleable("Plain3"); - + PlainInt pint = new PlainInt("Ints"); PlainBoth pboth = new PlainBoth("Both"); - + bus.subscribe(deleg1); - + deleg1.clients.add(togg1); deleg1.clients.add(plain2); deleg1.clients.add(deleg2); deleg1.clients.add(pint); - + deleg2.clients.add(deleg1); deleg2.clients.add(togg1); deleg2.clients.add(plain3); deleg2.clients.add(pboth); - + bus.subscribe(plain1); - + bus.subscribe(togg2); - + bus.broadcast(new StringMessage("")); bus.broadcast(new IntMessage(7)); bus.broadcast(new IntMessage(13)); - + deleg2.delegating = false; - + bus.broadcast(new IntMessage(44)); - + deleg2.delegating = true; - + bus.broadcast(new IntMessage(45)); - + } - + } class Delegator extends Plain implements DelegatingClient { - + List clients = new ArrayList(); boolean delegating = true; - - + + public Delegator(String name) { super(name); } - - + + @Override public Collection getChildClients() { return clients; } - - + + @Override public boolean doesDelegate() { @@ -95,15 +94,15 @@ class Delegator extends Plain implements DelegatingClient { class Toggleable extends Plain implements ToggleableClient { - + boolean subscribing = true; - - + + public Toggleable(String name) { super(name); } - - + + @Override public boolean doesSubscribe() { @@ -113,15 +112,15 @@ class Toggleable extends Plain implements ToggleableClient { class Plain implements StringMessage.Listener { - + String name; - - + + public Plain(String name) { this.name = name; } - - + + @Override public void receive(StringMessage message) { @@ -131,15 +130,15 @@ class Plain implements StringMessage.Listener { class PlainInt implements IntMessage.Listener { - + String name; - - + + public PlainInt(String name) { this.name = name; } - - + + @Override public void receive(IntMessage message) { @@ -149,22 +148,22 @@ class PlainInt implements IntMessage.Listener { class PlainBoth implements IntMessage.Listener, StringMessage.Listener { - + String name; - - + + public PlainBoth(String name) { this.name = name; } - - + + @Override public void receive(IntMessage message) { System.out.println(name + " (both-INT) RECV: " + message.i); } - - + + @Override public void receive(StringMessage message) { @@ -174,20 +173,21 @@ class PlainBoth implements IntMessage.Listener, StringMessage.Listener { class StringMessage implements Handleable { - + String s; - - + + StringMessage(String str) { this.s = str; } - + + public interface Listener { - + public void receive(StringMessage message); } - - + + @Override public void handleBy(Listener handler) { @@ -197,20 +197,21 @@ class StringMessage implements Handleable { class IntMessage implements Handleable { - + int i; - - + + IntMessage(int i) { this.i = i; } - + + public interface Listener { - + public void receive(IntMessage message); } - - + + @Override public void handleBy(Listener handler) { diff --git a/src/mightypork/rogue/textures/TextureManager.java b/src/mightypork/rogue/textures/TextureManager.java index de2af43..2decda0 100644 --- a/src/mightypork/rogue/textures/TextureManager.java +++ b/src/mightypork/rogue/textures/TextureManager.java @@ -1,6 +1,5 @@ package mightypork.rogue.textures; - import static org.lwjgl.opengl.GL11.*; import java.io.IOException; @@ -18,10 +17,10 @@ import org.newdawn.slick.util.ResourceLoader; * @author MightyPork */ public class TextureManager { - + private static Texture lastBinded = null; - - + + /** * Load texture * @@ -32,28 +31,30 @@ public class TextureManager { { try { String ext = resourcePath.substring(resourcePath.length() - 4); - + Texture texture = TextureLoader.getTexture(ext.toUpperCase(), ResourceLoader.getResourceAsStream(resourcePath)); - + if (texture != null) { return texture; } - + Log.w("Texture " + resourcePath + " could not be loaded."); return null; } catch (IOException e) { Log.e("Loading of texture " + resourcePath + " failed.", e); throw new RuntimeException(e); } - + } - - + + /** * Bind texture * - * @param texture the texture - * @throws RuntimeException if not loaded yet + * @param texture + * the texture + * @throws RuntimeException + * if not loaded yet */ public static void bind(Texture texture) throws RuntimeException { @@ -63,8 +64,8 @@ public class TextureManager { lastBinded = texture; } } - - + + /** * Unbind all */ diff --git a/src/mightypork/rogue/textures/Textures.java b/src/mightypork/rogue/textures/Textures.java index d67ebb5..d7ca077 100644 --- a/src/mightypork/rogue/textures/Textures.java +++ b/src/mightypork/rogue/textures/Textures.java @@ -1,6 +1,5 @@ package mightypork.rogue.textures; - import static org.lwjgl.opengl.GL11.*; import org.newdawn.slick.opengl.Texture; @@ -14,12 +13,12 @@ import org.newdawn.slick.opengl.Texture; * @author MightyPork */ public class Textures { - + protected static Texture logo; - + private static final String GUI = "res/images/gui/"; - - + + /** * Load what's needed for splash */ @@ -29,12 +28,12 @@ public class Textures { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - + logo = TextureManager.load(GUI + "logo.png"); - + Tx.initForSplash(); glDisable(GL_TEXTURE_2D); Tx.init(); } - + } diff --git a/src/mightypork/rogue/textures/Tx.java b/src/mightypork/rogue/textures/Tx.java index 2f21abd..5c1d9b1 100644 --- a/src/mightypork/rogue/textures/Tx.java +++ b/src/mightypork/rogue/textures/Tx.java @@ -1,6 +1,5 @@ package mightypork.rogue.textures; - // TODO rewrite /** @@ -9,22 +8,22 @@ package mightypork.rogue.textures; * @author MightyPork */ public class Tx { - + // logo public static TxQuad LOGO; - - + + public static void initForSplash() { // splash logo LOGO = TxQuad.fromSize(Textures.logo, 15, 9, 226, 132); } - - + + public static void init() { // title image (word art) - + } - + } diff --git a/src/mightypork/rogue/textures/TxQuad.java b/src/mightypork/rogue/textures/TxQuad.java index 9682c44..e876e38 100644 --- a/src/mightypork/rogue/textures/TxQuad.java +++ b/src/mightypork/rogue/textures/TxQuad.java @@ -1,6 +1,5 @@ package mightypork.rogue.textures; - import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Rect; @@ -13,58 +12,70 @@ import org.newdawn.slick.opengl.Texture; * @author MightyPork */ public class TxQuad { - + /** The texture */ public Texture tx; /** Coords in texture (pixels) */ public Rect uvs; /** Quad size */ public Coord size; - - + + /** * Create TxQuad from left top coord and rect size * - * @param tx texture - * @param x1 left top X - * @param y1 left top Y - * @param width area width - * @param height area height + * @param tx + * texture + * @param x1 + * left top X + * @param y1 + * left top Y + * @param width + * area width + * @param height + * area height * @return new TxQuad */ public static TxQuad fromSize(Texture tx, int x1, int y1, int width, int height) { return new TxQuad(tx, x1, y1, x1 + width, y1 + height); } - - + + /** - * @param tx Texture - * @param uvs Rect of texturwe UVs (pixels - from left top) + * @param tx + * Texture + * @param uvs + * Rect of texturwe UVs (pixels - from left top) */ public TxQuad(Texture tx, Rect uvs) { this.tx = tx; this.uvs = uvs.copy(); this.size = uvs.getSize(); } - - + + /** * Make of coords * - * @param tx texture - * @param x1 x1 - * @param y1 y1 - * @param x2 x2 - * @param y2 y2 + * @param tx + * texture + * @param x1 + * x1 + * @param y1 + * y1 + * @param x2 + * x2 + * @param y2 + * y2 */ public TxQuad(Texture tx, int x1, int y1, int x2, int y2) { this.tx = tx; this.uvs = new Rect(x1, y1, x2, y2); this.size = uvs.getSize(); } - - + + public TxQuad copy() { return new TxQuad(tx, uvs); diff --git a/src/mightypork/rogue/util/RenderUtils.java b/src/mightypork/rogue/util/RenderUtils.java index 8892d5d..e053a12 100644 --- a/src/mightypork/rogue/util/RenderUtils.java +++ b/src/mightypork/rogue/util/RenderUtils.java @@ -1,6 +1,5 @@ package mightypork.rogue.util; - import static org.lwjgl.opengl.GL11.*; import mightypork.rogue.textures.TextureManager; import mightypork.rogue.textures.TxQuad; @@ -18,31 +17,37 @@ import org.newdawn.slick.opengl.Texture; * @author MightyPork */ public class RenderUtils { - + private static final Coord AXIS_X = new Coord(1, 0, 0); private static final Coord AXIS_Y = new Coord(0, 1, 0); private static final Coord AXIS_Z = new Coord(0, 0, 1); - - + + /** * Render quad 2D * - * @param min min coord - * @param max max coord + * @param min + * min coord + * @param max + * max coord */ public static void quadCoord(Coord min, Coord max) { quadCoord(min.x, min.y, max.x, max.y); } - - + + /** * Render quad with absolute coords (from screen bottom) * - * @param left left x - * @param bottom bottom y - * @param right right x - * @param top top y + * @param left + * left x + * @param bottom + * bottom y + * @param right + * right x + * @param top + * top y */ public static void quadCoord(double left, double bottom, double right, double top) { @@ -53,77 +58,99 @@ public class RenderUtils { glVertex2d(left, bottom); glEnd(); } - - + + /** * Render quad with coloured border * - * @param min min - * @param max max - * @param border border width - * @param borderColor border color - * @param insideColor filling color + * @param min + * min + * @param max + * max + * @param border + * border width + * @param borderColor + * border color + * @param insideColor + * filling color */ public static void quadCoordBorder(Coord min, Coord max, double border, RGB borderColor, RGB insideColor) { quadCoordBorder(min.x, min.y, max.x, max.y, border, borderColor, insideColor); } - - + + /** * Render quad with coloured border * - * @param minX min x - * @param minY min y - * @param maxX max x - * @param maxY max y - * @param border border width - * @param borderColor border color - * @param insideColor filling color + * @param minX + * min x + * @param minY + * min y + * @param maxX + * max x + * @param maxY + * max y + * @param border + * border width + * @param borderColor + * border color + * @param insideColor + * filling color */ public static void quadCoordBorder(double minX, double minY, double maxX, double maxY, double border, RGB borderColor, RGB insideColor) { double bdr = border; - + RenderUtils.setColor(borderColor); - + //@formatter:off RenderUtils.quadCoord(minX, minY, minX + bdr, maxY); RenderUtils.quadCoord(maxX - bdr, minY, maxX, maxY); RenderUtils.quadCoord(minX + bdr, maxY - bdr, maxX - bdr, maxY); RenderUtils.quadCoord(minX + bdr, minY, maxX - bdr, minY + bdr); //@formatter:on - + if (insideColor != null) { RenderUtils.setColor(insideColor); RenderUtils.quadCoord(minX + bdr, minY + bdr, maxX - bdr, maxY - bdr); } } - - + + /** * Render quad 2D with gradient horizontal * - * @param min min coord - * @param max max coord - * @param colorLeft color left - * @param colorRight color right + * @param min + * min coord + * @param max + * max coord + * @param colorLeft + * color left + * @param colorRight + * color right */ public static void quadCoordGradH(Coord min, Coord max, RGB colorLeft, RGB colorRight) { quadCoordGradH(min.x, min.y, max.x, max.y, colorLeft, colorRight); } - - + + /** * Render quad 2D with gradient horizontal * - * @param minX units from left - * @param minY units from bottom - * @param maxX quad width - * @param maxY quad height - * @param colorLeft color left - * @param colorRight color right + * @param minX + * units from left + * @param minY + * units from bottom + * @param maxX + * quad width + * @param maxY + * quad height + * @param colorLeft + * color left + * @param colorRight + * color right */ public static void quadCoordGradH(double minX, double minY, double maxX, double maxY, RGB colorLeft, RGB colorRight) { @@ -132,38 +159,48 @@ public class RenderUtils { glVertex2d(minX, maxY); setColor(colorRight); glVertex2d(maxX, maxY); - + setColor(colorRight); glVertex2d(maxX, minY); setColor(colorLeft); glVertex2d(minX, minY); glEnd(); } - - + + /** * Render quad 2D with gradient horizontal * - * @param min min coord - * @param max max coord - * @param colorOuter color outer - * @param colorMiddle color inner + * @param min + * min coord + * @param max + * max coord + * @param colorOuter + * color outer + * @param colorMiddle + * color inner */ public static void quadCoordGradHBilinear(Coord min, Coord max, RGB colorOuter, RGB colorMiddle) { quadCoordGradHBilinear(min.x, min.y, max.x, max.y, colorOuter, colorMiddle); } - - + + /** * Render quad 2D with gradient horizontal * - * @param minX min x - * @param minY min y - * @param maxX max x - * @param maxY max y - * @param colorOuter color outer - * @param colorMiddle color inner + * @param minX + * min x + * @param minY + * min y + * @param maxX + * max x + * @param maxY + * max y + * @param colorOuter + * color outer + * @param colorMiddle + * color inner */ public static void quadCoordGradHBilinear(double minX, double minY, double maxX, double maxY, RGB colorOuter, RGB colorMiddle) { @@ -172,153 +209,185 @@ public class RenderUtils { glVertex2d(minX, maxY); setColor(colorMiddle); glVertex2d((minX + maxX) / 2, maxY); - + setColor(colorMiddle); glVertex2d((minX + maxX) / 2, minY); setColor(colorOuter); glVertex2d(minX, minY); - + setColor(colorMiddle); glVertex2d((minX + maxX) / 2, maxY); setColor(colorOuter); glVertex2d(maxX, maxY); - + setColor(colorOuter); glVertex2d(maxX, minY); setColor(colorMiddle); glVertex2d((minX + maxX) / 2, minY); glEnd(); } - - + + /** * Render quad 2D with gradient vertical * - * @param min min coord - * @param max max coord - * @param colorTop top color - * @param colorBottom bottom color + * @param min + * min coord + * @param max + * max coord + * @param colorTop + * top color + * @param colorBottom + * bottom color */ public static void quadCoordGradV(Coord min, Coord max, RGB colorTop, RGB colorBottom) { quadCoordGradV(min.x, min.y, max.x, max.y, colorTop, colorBottom); } - - + + /** * Render quad 2D with gradient vertical * - * @param minX min X - * @param minY min Y - * @param maxX max X - * @param maxY max Y - * @param colorTop top color - * @param colorBottom bottom color + * @param minX + * min X + * @param minY + * min Y + * @param maxX + * max X + * @param maxY + * max Y + * @param colorTop + * top color + * @param colorBottom + * bottom color */ public static void quadCoordGradV(double minX, double minY, double maxX, double maxY, RGB colorTop, RGB colorBottom) { glBegin(GL_QUADS); - + setColor(colorTop); glVertex2d(minX, maxY); glVertex2d(maxX, maxY); - + setColor(colorBottom); glVertex2d(maxX, minY); glVertex2d(minX, minY); glEnd(); } - - + + /** * Render quad 2D with gradient vertical * - * @param min min coord - * @param max max coord - * @param colorOuter outer color - * @param colorMiddle middle color + * @param min + * min coord + * @param max + * max coord + * @param colorOuter + * outer color + * @param colorMiddle + * middle color */ public static void quadCoordGradVBilinear(Coord min, Coord max, RGB colorOuter, RGB colorMiddle) { quadCoordGradVBilinear(min.x, min.y, max.x, max.y, colorOuter, colorMiddle); } - - + + /** * Render quad 2D with gradient vertical * - * @param minX min X - * @param minY min Y - * @param maxX max X - * @param maxY max Y - * @param colorOuter outer color - * @param colorMiddle middle color + * @param minX + * min X + * @param minY + * min Y + * @param maxX + * max X + * @param maxY + * max Y + * @param colorOuter + * outer color + * @param colorMiddle + * middle color */ public static void quadCoordGradVBilinear(double minX, double minY, double maxX, double maxY, RGB colorOuter, RGB colorMiddle) { glBegin(GL_QUADS); - + setColor(colorOuter); glVertex2d(minX, maxY); glVertex2d(maxX, maxY); - + setColor(colorMiddle); glVertex2d(maxX, (maxY + minY) / 2); glVertex2d(minX, (maxY + minY) / 2); - + setColor(colorMiddle); glVertex2d(minX, (maxY + minY) / 2); glVertex2d(maxX, (maxY + minY) / 2); - + setColor(colorOuter); glVertex2d(maxX, minY); glVertex2d(minX, minY); - + glEnd(); } - - + + /** * Render quad with coloured border with outset effect * - * @param min min coord - * @param max max coord - * @param border border width - * @param fill fill color - * @param inset true for inset, false for outset + * @param min + * min coord + * @param max + * max coord + * @param border + * border width + * @param fill + * fill color + * @param inset + * true for inset, false for outset */ public static void quadCoordOutset(Coord min, Coord max, double border, RGB fill, boolean inset) { quadCoordOutset(min.x, min.y, max.x, max.y, border, fill, inset); } - - + + /** * Render quad with coloured border with outset effect * - * @param minX left X - * @param minY bottom Y - * @param maxX right X - * @param maxY top Y - * @param border border width - * @param fill fill color - * @param inset true for inset, false for outset + * @param minX + * left X + * @param minY + * bottom Y + * @param maxX + * right X + * @param maxY + * top Y + * @param border + * border width + * @param fill + * fill color + * @param inset + * true for inset, false for outset */ public static void quadCoordOutset(double minX, double minY, double maxX, double maxY, double border, RGB fill, boolean inset) { HSV hsv = fill.toHSV(); HSV h; - + h = hsv.copy(); h.s *= 0.6; RGB a = h.toRGB(); - + h = fill.toHSV(); h.v *= 0.6; RGB b = h.toRGB(); - + RGB leftTopC, rightBottomC; - + if (!inset) { leftTopC = a; rightBottomC = b; @@ -326,153 +395,178 @@ public class RenderUtils { leftTopC = b; rightBottomC = a; } - + double bdr = border; - + RenderUtils.setColor(leftTopC); - + // left + top glBegin(GL_QUADS); glVertex2d(minX, minY); glVertex2d(minX + bdr, minY + bdr); glVertex2d(minX + bdr, maxY - bdr); glVertex2d(minX, maxY); - + glVertex2d(minX, maxY); glVertex2d(minX + bdr, maxY - bdr); glVertex2d(maxX - bdr, maxY - bdr); glVertex2d(maxX, maxY); glEnd(); - + RenderUtils.setColor(rightBottomC); - + // right + bottom glBegin(GL_QUADS); glVertex2d(maxX, minY); glVertex2d(maxX, maxY); glVertex2d(maxX - bdr, maxY - bdr); glVertex2d(maxX - bdr, minY + bdr); - + glVertex2d(minX, minY); glVertex2d(maxX, minY); glVertex2d(maxX - bdr, minY + bdr); glVertex2d(minX + bdr, minY + bdr); glEnd(); - + RenderUtils.setColor(fill); RenderUtils.quadCoord(minX + bdr, minY + bdr, maxX - bdr, maxY - bdr); } - - + + /** * Render quad 2D * - * @param rect rectangle + * @param rect + * rectangle */ public static void quadRect(Rect rect) { quadCoord(rect.getOrigin(), rect.getMax()); } - - + + /** * Render quad 2D * - * @param rect rectangle - * @param color draw color + * @param rect + * rectangle + * @param color + * draw color */ public static void quadRect(Rect rect, RGB color) { setColor(color); quadCoord(rect.getOrigin(), rect.getMax()); } - - + + /** * Render quad with coloured border * - * @param rect rect - * @param border border width - * @param borderColor border color - * @param insideColor filling color + * @param rect + * rect + * @param border + * border width + * @param borderColor + * border color + * @param insideColor + * filling color */ public static void quadBorder(Rect rect, double border, RGB borderColor, RGB insideColor) { quadCoordBorder(rect.getOrigin(), rect.getMax(), border, borderColor, insideColor); } - - + + /** * Render quad 2D with gradient horizontal * - * @param rect rect - * @param colorLeft color left - * @param colorRight color right + * @param rect + * rect + * @param colorLeft + * color left + * @param colorRight + * color right */ public static void quadGradH(Rect rect, RGB colorLeft, RGB colorRight) { quadCoordGradH(rect.getOrigin(), rect.getMax(), colorLeft, colorRight); } - - + + /** * Render quad 2D with gradient horizontal * - * @param rect rect - * @param colorOuter color outer - * @param colorMiddle color inner + * @param rect + * rect + * @param colorOuter + * color outer + * @param colorMiddle + * color inner */ public static void quadGradHBilinear(Rect rect, RGB colorOuter, RGB colorMiddle) { quadCoordGradHBilinear(rect.getOrigin(), rect.getMax(), colorOuter, colorMiddle); } - - + + /** * Render quad 2D with gradient vertical * - * @param rect rect - * @param colorTop top color - * @param colorBottom bottom color + * @param rect + * rect + * @param colorTop + * top color + * @param colorBottom + * bottom color */ public static void quadGradV(Rect rect, RGB colorTop, RGB colorBottom) { quadCoordGradV(rect.getOrigin(), rect.getMax(), colorTop, colorBottom); } - - + + /** * Render quad 2D with gradient vertical * - * @param rect rect - * @param colorOuter outer color - * @param colorMiddle middle color + * @param rect + * rect + * @param colorOuter + * outer color + * @param colorMiddle + * middle color */ public static void quadGradVBilinear(Rect rect, RGB colorOuter, RGB colorMiddle) { quadCoordGradVBilinear(rect.getOrigin(), rect.getMax(), colorOuter, colorMiddle); } - - + + /** * Render quad with coloured border with outset effect * - * @param rect rectangle - * @param border border width - * @param fill fill color - * @param inset true for inset, false for outset + * @param rect + * rectangle + * @param border + * border width + * @param fill + * fill color + * @param inset + * true for inset, false for outset */ public static void quadRectOutset(Rect rect, double border, RGB fill, boolean inset) { quadCoordOutset(rect.getOrigin(), rect.getMax(), border, fill, inset); } - - + + /** * Render textured rect (texture must be binded already) * - * @param quad rectangle (px) - * @param textureCoords texture coords (0-1) + * @param quad + * rectangle (px) + * @param textureCoords + * texture coords (0-1) */ public static void quadTexturedAbs(Rect quad, Rect textureCoords) { @@ -480,12 +574,12 @@ public class RenderUtils { double bottom = quad.yMax(); double right = quad.xMax(); double top = quad.yMin(); - + double tleft = textureCoords.xMin(); double tbottom = textureCoords.yMin(); double tright = textureCoords.xMax(); double ttop = textureCoords.yMax(); - + glBegin(GL_QUADS); glTexCoord2d(tleft, ttop); glVertex2d(left, top); @@ -497,386 +591,455 @@ public class RenderUtils { glVertex2d(left, bottom); glEnd(); } - - + + /** * Render textured rect * - * @param quad rectangle (px) - * @param txCoords texture coords rectangle (px) - * @param texture texture instance + * @param quad + * rectangle (px) + * @param txCoords + * texture coords rectangle (px) + * @param texture + * texture instance */ public static void quadTextured(Rect quad, Rect txCoords, Texture texture) { quadTextured(quad, txCoords, texture, RGB.WHITE); } - - + + /** * Render textured rect * - * @param quad rectangle (px) - * @param txCoords texture coords rectangle (px) - * @param texture texture instance - * @param tint color tint + * @param quad + * rectangle (px) + * @param txCoords + * texture coords rectangle (px) + * @param texture + * texture instance + * @param tint + * color tint */ public static void quadTextured(Rect quad, Rect txCoords, Texture texture, RGB tint) { glEnable(GL_TEXTURE_2D); setColor(tint); TextureManager.bind(texture); - + quadTexturedAbs(quad, txCoords.mul(1 / texture.getImageHeight())); - + TextureManager.unbind(); glDisable(GL_TEXTURE_2D); } - - + + /** * Render textured rect * - * @param quad rectangle (px) - * @param txquad texture quad + * @param quad + * rectangle (px) + * @param txquad + * texture quad */ public static void quadTextured(Rect quad, TxQuad txquad) { quadTextured(quad, txquad, RGB.WHITE); } - - + + /** * Render textured rect * - * @param quad rectangle (px) - * @param txquad texture instance - * @param tint color tint + * @param quad + * rectangle (px) + * @param txquad + * texture instance + * @param tint + * color tint */ public static void quadTextured(Rect quad, TxQuad txquad, RGB tint) { quadTextured(quad, txquad.uvs, txquad.tx, tint); } - - + + /** * Render textured frame with borders * - * @param quadRect drawn rectangle (px) - * @param textureRect rectangle in texture with the basic frame (px) - * @param yOffsetTimes offset count (move frame down n times) - * @param texture the texture + * @param quadRect + * drawn rectangle (px) + * @param textureRect + * rectangle in texture with the basic frame (px) + * @param yOffsetTimes + * offset count (move frame down n times) + * @param texture + * the texture */ public static void quadTexturedFrame(Rect quadRect, Rect textureRect, int yOffsetTimes, Texture texture) { quadTexturedFrame(quadRect, textureRect, yOffsetTimes, texture, RGB.WHITE); } - - + + /** * Render textured frame with borders * - * @param quadRect drawn rectangle (px) - * @param textureRect rectangle in texture with the basic frame (px) - * @param yOffsetTimes offset count (move frame down n times) - * @param texture the texture - * @param tint color tint + * @param quadRect + * drawn rectangle (px) + * @param textureRect + * rectangle in texture with the basic frame (px) + * @param yOffsetTimes + * offset count (move frame down n times) + * @param texture + * the texture + * @param tint + * color tint */ public static void quadTexturedFrame(Rect quadRect, Rect textureRect, int yOffsetTimes, Texture texture, RGB tint) { Texture tx = texture; - + int frameHeight = (int) textureRect.getSize().y; - + int yOffset = yOffsetTimes * frameHeight; - + double x1 = quadRect.xMin(); double y1 = quadRect.yMin(); double x2 = quadRect.xMax(); double y2 = quadRect.yMax(); - + double w = x2 - x1; double h = y2 - y1; - + double tx1 = textureRect.xMin(); double ty1 = textureRect.yMin(); double tx2 = textureRect.xMax(); double ty2 = textureRect.yMax(); - + double halfY = h / 2D; double halfX = w / 2D; - + // lt, rt // lb, rb - + Rect verts, uvs; - + // lt verts = new Rect(x1, y2, x1 + halfX, y2 - halfY); uvs = new Rect(tx1, ty1, tx1 + halfX, ty1 + halfY); uvs.add_ip(0, yOffset); quadTextured(verts, uvs, tx, tint); - + // lb verts = new Rect(x1, y2 - halfY, x1 + halfX, y1); uvs = new Rect(tx1, ty2 - halfY, tx1 + halfX, ty2); uvs.add_ip(0, yOffset); quadTextured(verts, uvs, tx, tint); - + // rt verts = new Rect(x1 + halfX, y2, x2, y2 - halfY); uvs = new Rect(tx2 - halfX, ty1, tx2, ty1 + halfY); uvs.add_ip(0, yOffset); quadTextured(verts, uvs, tx, tint); - + // rb verts = new Rect(x1 + halfX, y2 - halfY, x2, y1); uvs = new Rect(tx2 - halfX, ty2 - halfY, tx2, ty2); uvs.add_ip(0, yOffset); quadTextured(verts, uvs, tx, tint); - + } - - + + /** * Render textured frame with borders * - * @param quadRect drawn rectangle (px) - * @param textureRect rectangle in texture with the basic frame (px) - * @param texture the texture + * @param quadRect + * drawn rectangle (px) + * @param textureRect + * rectangle in texture with the basic frame (px) + * @param texture + * the texture */ public static void quadTexturedFrame(Rect quadRect, Rect textureRect, Texture texture) { quadTexturedFrame(quadRect, textureRect, 0, texture, RGB.WHITE); } - - + + /** * Render textured frame with borders * - * @param quadRect drawn rectangle (px) - * @param txquad texture quad + * @param quadRect + * drawn rectangle (px) + * @param txquad + * texture quad */ public static void quadTexturedFrame(Rect quadRect, TxQuad txquad) { quadTexturedFrame(quadRect, txquad, 0, RGB.WHITE); } - - + + /** * Render textured frame with borders * - * @param quadRect drawn rectangle (px) - * @param txquad texture quad - * @param yOffsetTimes offset count (move frame down n times) + * @param quadRect + * drawn rectangle (px) + * @param txquad + * texture quad + * @param yOffsetTimes + * offset count (move frame down n times) */ public static void quadTexturedFrame(Rect quadRect, TxQuad txquad, int yOffsetTimes) { quadTexturedFrame(quadRect, txquad, yOffsetTimes, RGB.WHITE); } - - + + /** * Render textured frame with borders * - * @param quadRect drawn rectangle (px) - * @param txquad texture quad - * @param yOffsetTimes offset count (move frame down n times) - * @param tint color tint + * @param quadRect + * drawn rectangle (px) + * @param txquad + * texture quad + * @param yOffsetTimes + * offset count (move frame down n times) + * @param tint + * color tint */ public static void quadTexturedFrame(Rect quadRect, TxQuad txquad, int yOffsetTimes, RGB tint) { quadTexturedFrame(quadRect, txquad.uvs, yOffsetTimes, txquad.tx, tint); } - - + + /** * Render textured frame stretching horizontally (rect height = texture rect * height) * - * @param quadRect drawn rectangle (px) - * @param textureRect rectangle in texture with the basic frame (px) - * @param borderSize size of the unstretched horizontal border - * @param texture the texture + * @param quadRect + * drawn rectangle (px) + * @param textureRect + * rectangle in texture with the basic frame (px) + * @param borderSize + * size of the unstretched horizontal border + * @param texture + * the texture */ public static void quadTexturedStretchH(Rect quadRect, Rect textureRect, int borderSize, Texture texture) { quadTexturedStretchH(quadRect, textureRect, borderSize, texture, RGB.WHITE); } - - + + /** * Render textured frame stretching horizontally (rect height = texture rect * height) * - * @param quadRect drawn rectangle (px) - * @param textureRect rectangle in texture with the basic frame (px) - * @param borderSize size of the unstretched horizontal border - * @param texture the texture - * @param tint color tint + * @param quadRect + * drawn rectangle (px) + * @param textureRect + * rectangle in texture with the basic frame (px) + * @param borderSize + * size of the unstretched horizontal border + * @param texture + * the texture + * @param tint + * color tint */ public static void quadTexturedStretchH(Rect quadRect, Rect textureRect, int borderSize, Texture texture, RGB tint) { Texture tx = texture; - + double x1 = quadRect.xMin(); double y1 = quadRect.yMin(); double x2 = quadRect.xMax(); double y2 = quadRect.yMax(); - + double tx1 = textureRect.xMin(); double ty1 = textureRect.yMin(); double tx2 = textureRect.xMax(); double ty2 = textureRect.yMax(); - + // lt, mi, rt - + Rect verts, uvs; - + // lt verts = new Rect(x1, y2, x1 + borderSize, y1); uvs = new Rect(tx1, ty1, tx1 + borderSize, ty2); quadTextured(verts, uvs, tx, tint); - -// // mi + + // // mi verts = new Rect(x1 + borderSize, y2, x2 - borderSize, y1); uvs = new Rect(tx1 + borderSize, ty1, tx2 - borderSize, ty2); quadTextured(verts, uvs, tx, tint); - + // rt verts = new Rect(x2 - borderSize, y2, x2, y1); uvs = new Rect(tx2 - borderSize, ty1, tx2, ty2); quadTextured(verts, uvs, tx, tint); } - - + + /** * Render textured frame stretching horizontally (rect height = texture rect * height) * - * @param quadRect drawn rectangle (px) - * @param txquad texture quad - * @param borderSize size of the unstretched horizontal border + * @param quadRect + * drawn rectangle (px) + * @param txquad + * texture quad + * @param borderSize + * size of the unstretched horizontal border */ public static void quadTexturedStretchH(Rect quadRect, TxQuad txquad, int borderSize) { quadTexturedStretchH(quadRect, txquad, borderSize, RGB.WHITE); } - - + + /** * Render textured frame stretching horizontally (rect height = texture rect * height) * - * @param quadRect drawn rectangle (px) - * @param txquad texture quad - * @param borderSize size of the unstretched horizontal border - * @param tint color tint + * @param quadRect + * drawn rectangle (px) + * @param txquad + * texture quad + * @param borderSize + * size of the unstretched horizontal border + * @param tint + * color tint */ public static void quadTexturedStretchH(Rect quadRect, TxQuad txquad, int borderSize, RGB tint) { quadTexturedStretchH(quadRect, txquad.uvs, borderSize, txquad.tx, tint); } - - + + /** * Render textured frame stretching vertically (rect width = texture rect * width) * - * @param quadRect drawn rectangle (px) - * @param textureRect rectangle in texture with the basic frame (px) - * @param borderSize size of the unstretched horizontal border - * @param texture the texture + * @param quadRect + * drawn rectangle (px) + * @param textureRect + * rectangle in texture with the basic frame (px) + * @param borderSize + * size of the unstretched horizontal border + * @param texture + * the texture */ public static void quadTexturedStretchV(Rect quadRect, Rect textureRect, int borderSize, Texture texture) { quadTexturedStretchV(quadRect, textureRect, borderSize, texture, RGB.WHITE); } - - + + /** * Render textured frame stretching vertically (rect width = texture rect * width) * - * @param quadRect drawn rectangle (px) - * @param textureRect rectangle in texture with the basic frame (px) - * @param borderSize size of the unstretched horizontal border - * @param texture the texture - * @param tint color tint + * @param quadRect + * drawn rectangle (px) + * @param textureRect + * rectangle in texture with the basic frame (px) + * @param borderSize + * size of the unstretched horizontal border + * @param texture + * the texture + * @param tint + * color tint */ public static void quadTexturedStretchV(Rect quadRect, Rect textureRect, int borderSize, Texture texture, RGB tint) { Texture tx = texture; - + double x1 = quadRect.xMin(); double y1 = quadRect.yMin(); double x2 = quadRect.xMax(); double y2 = quadRect.yMax(); - + double tx1 = textureRect.xMin(); double ty1 = textureRect.yMin(); double tx2 = textureRect.xMax(); double ty2 = textureRect.yMax(); - + // tp // mi // bn - + Rect verts, uvs; - + // tp verts = new Rect(x1, y2, x2, y2 - borderSize); uvs = new Rect(tx1, ty1, tx2, ty1 + borderSize); quadTextured(verts, uvs, tx, tint); - + // mi verts = new Rect(x1, y2 - borderSize, x2, y1 + borderSize); uvs = new Rect(tx1, ty1 + borderSize, tx2, ty2 - borderSize); quadTextured(verts, uvs, tx, tint); - + // rt verts = new Rect(x1, y1 + borderSize, x2, y1); uvs = new Rect(tx1, ty2 - borderSize, tx2, ty2); quadTextured(verts, uvs, tx, tint); } - - + + /** * Render textured frame stretching vertically (rect width = texture rect * width) * - * @param quadRect drawn rectangle (px) - * @param txquad texture quad - * @param borderSize size of the unstretched horizontal border + * @param quadRect + * drawn rectangle (px) + * @param txquad + * texture quad + * @param borderSize + * size of the unstretched horizontal border */ public static void quadTexturedStretchV(Rect quadRect, TxQuad txquad, int borderSize) { quadTexturedStretchV(quadRect, txquad, borderSize, RGB.WHITE); } - - + + /** * Render textured frame stretching vertically (rect width = texture rect * width) * - * @param quadRect drawn rectangle (px) - * @param txquad texture quad - * @param borderSize size of the unstretched horizontal border - * @param tint color tint + * @param quadRect + * drawn rectangle (px) + * @param txquad + * texture quad + * @param borderSize + * size of the unstretched horizontal border + * @param tint + * color tint */ public static void quadTexturedStretchV(Rect quadRect, TxQuad txquad, int borderSize, RGB tint) { quadTexturedStretchV(quadRect, txquad.uvs, borderSize, txquad.tx, tint); - + } - - + + /** * Render quad 2D * - * @param left units from left - * @param bottom units from bottom - * @param width quad width - * @param height quad height + * @param left + * units from left + * @param bottom + * units from bottom + * @param width + * quad width + * @param height + * quad height */ public static void quadSize(double left, double bottom, double width, double height) { @@ -887,60 +1050,64 @@ public class RenderUtils { glVertex2d(left, bottom); glEnd(); } - - + + /** * Bind GL color * - * @param color RGB color + * @param color + * RGB color */ public static void setColor(RGB color) { if (color != null) glColor4d(color.r, color.g, color.b, color.a); } - - + + /** * Bind GL color * - * @param color RGB color - * @param alpha alpha multiplier + * @param color + * RGB color + * @param alpha + * alpha multiplier */ public static void setColor(RGB color, double alpha) { if (color != null) glColor4d(color.r, color.g, color.b, color.a * alpha); } - - + + /** * Translate with coord * - * @param coord coord + * @param coord + * coord */ public static void translate(Coord coord) { glTranslated(coord.x, coord.y, coord.z); } - - + + public static void rotateX(double angle) { rotate(angle, AXIS_X); } - - + + public static void rotateY(double angle) { rotate(angle, AXIS_Y); } - - + + public static void rotateZ(double angle) { rotate(angle, AXIS_Z); } - - + + public static void rotate(double angle, Coord axis) { Coord vec = axis.norm(1); diff --git a/src/mightypork/rogue/util/Utils.java b/src/mightypork/rogue/util/Utils.java index 9183f53..fa8ece3 100644 --- a/src/mightypork/rogue/util/Utils.java +++ b/src/mightypork/rogue/util/Utils.java @@ -1,13 +1,12 @@ package mightypork.rogue.util; - /** * Utils class * * @author MightyPork */ public class Utils { - + public static Thread runAsThread(Runnable r) { Thread t = new Thread(r); diff --git a/src/mightypork/utils/files/FileTreeDiff.java b/src/mightypork/utils/files/FileTreeDiff.java index 0653f17..5e53388 100644 --- a/src/mightypork/utils/files/FileTreeDiff.java +++ b/src/mightypork/utils/files/FileTreeDiff.java @@ -1,6 +1,5 @@ package mightypork.utils.files; - import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -16,89 +15,90 @@ import mightypork.utils.logging.Log; public class FileTreeDiff { - + private static final byte[] BUFFER = new byte[2048]; private Checksum ck1 = new Adler32(); private Checksum ck2 = new Adler32(); - + private boolean logging = true; - + private List> compared = new ArrayList>(); private Comparator fileFirstSorter = new Comparator() { - + @Override public int compare(File o1, File o2) { if (!o1.isDirectory() && o2.isDirectory()) return -1; if (o1.isDirectory() && !o2.isDirectory()) return 1; - + return o1.getName().compareTo(o2.getName()); } }; - - + + public void enableLogging(boolean state) { logging = state; } - - + + public boolean areEqual(File dir1, File dir2) { if (logging) Log.f3("Comparing directory trees:\n 1. " + dir1 + "\n 2. " + dir2); - + try { compared.clear(); buildList(dir1, dir2); - + calcChecksum(); - + if (logging) Log.f3("No difference found."); - + return true; - + } catch (NotEqualException e) { if (logging) Log.f3("Difference found:\n" + e.getMessage()); - + return false; } } - - + + private void calcChecksum() throws NotEqualException { FileInputStream in1, in2; CheckedInputStream cin1 = null, cin2 = null; - + for (Tuple pair : compared) { try { ck1.reset(); ck2.reset(); - + in1 = new FileInputStream(pair.a); in2 = new FileInputStream(pair.b); - + cin1 = new CheckedInputStream(in1, ck1); cin2 = new CheckedInputStream(in2, ck2); - + while (true) { int read1 = cin1.read(BUFFER); int read2 = cin2.read(BUFFER); - + if (read1 != read2 || ck1.getValue() != ck2.getValue()) { throw new NotEqualException("Bytes differ:\n" + pair.a + "\n" + pair.b); } - + if (read1 == -1) break; } - - } catch (IOException e) {} finally { + + } catch (IOException e) { + } finally { try { if (cin1 != null) cin1.close(); } catch (IOException e) { // ignore } - + try { if (cin1 != null) cin1.close(); } catch (IOException e) { @@ -107,57 +107,59 @@ public class FileTreeDiff { } } } - - + + private void buildList(File f1, File f2) throws NotEqualException { if (f1.isDirectory() != f2.isDirectory()) throw new NotEqualException("isDirectory differs:\n" + f1 + "\n" + f2); - + if (f1.isFile() && f2.isFile()) { if (f1.length() != f2.length()) throw new NotEqualException("Sizes differ:\n" + f1 + "\n" + f2); } - + if (f1.isDirectory()) { File[] children1 = f1.listFiles(); File[] children2 = f2.listFiles(); - + Arrays.sort(children1, fileFirstSorter); Arrays.sort(children2, fileFirstSorter); - + if (children1.length != children2.length) throw new NotEqualException("Child counts differ:\n" + f1 + "\n" + f2); - + for (int i = 0; i < children1.length; i++) { File ch1 = children1[i]; File ch2 = children2[i]; - + if (!ch1.getName().equals(ch2.getName())) throw new NotEqualException("Filenames differ:\n" + ch1 + "\n" + ch2); - + buildList(ch1, ch2); } - + } else { compared.add(new Tuple(f1, f2)); } } - + + private class NotEqualException extends Exception { - + public NotEqualException(String msg) { super(msg); } - + } - + + private class Tuple { - + public T a; public T b; - - + + public Tuple(T a, T b) { this.a = a; this.b = b; } } - + } diff --git a/src/mightypork/utils/files/FileUtils.java b/src/mightypork/utils/files/FileUtils.java index 6db870e..cf21a78 100644 --- a/src/mightypork/utils/files/FileUtils.java +++ b/src/mightypork/utils/files/FileUtils.java @@ -1,6 +1,5 @@ package mightypork.utils.files; - import java.io.*; import java.util.ArrayList; import java.util.List; @@ -11,61 +10,73 @@ import mightypork.utils.string.validation.StringFilter; public class FileUtils { - + /** * Copy directory recursively. * - * @param source source file - * @param target target file - * @throws IOException on error + * @param source + * source file + * @param target + * target file + * @throws IOException + * on error */ public static void copyDirectory(File source, File target) throws IOException { copyDirectory(source, target, null, null); } - - + + /** * Copy directory recursively - advanced variant. * - * @param source source file - * @param target target file - * @param filter filter accepting only files and dirs to be copied - * @param filesCopied list into which all the target files will be added - * @throws IOException on error + * @param source + * source file + * @param target + * target file + * @param filter + * filter accepting only files and dirs to be copied + * @param filesCopied + * list into which all the target files will be added + * @throws IOException + * on error */ public static void copyDirectory(File source, File target, FileFilter filter, List filesCopied) throws IOException { if (!source.exists()) return; - + if (source.isDirectory()) { if (!target.exists()) { target.mkdir(); } - + String[] children = source.list(); for (int i = 0; i < children.length; i++) { copyDirectory(new File(source, children[i]), new File(target, children[i]), filter, filesCopied); } - + } else { if (filter != null && !filter.accept(source)) { return; } - + if (filesCopied != null) filesCopied.add(target); copyFile(source, target); } } - - + + /** * List directory recursively * - * @param source source file - * @param filter filter accepting only files and dirs to be copied (or null) - * @param files list of the found files - * @throws IOException on error + * @param source + * source file + * @param filter + * filter accepting only files and dirs to be copied (or null) + * @param files + * list of the found files + * @throws IOException + * on error */ public static void listDirectoryRecursive(File source, StringFilter filter, List files) throws IOException { @@ -74,33 +85,36 @@ public class FileUtils { for (int i = 0; i < children.length; i++) { listDirectoryRecursive(new File(source, children[i]), filter, files); } - + } else { if (filter != null && !filter.accept(source.getAbsolutePath())) { return; } - + files.add(source); } } - - + + /** * Copy file using streams. Make sure target directory exists! * - * @param source source file - * @param target target file - * @throws IOException on error + * @param source + * source file + * @param target + * target file + * @throws IOException + * on error */ public static void copyFile(File source, File target) throws IOException { InputStream in = null; OutputStream out = null; - + try { in = new FileInputStream(source); out = new FileOutputStream(target); - + copyStream(in, out); } finally { try { @@ -115,38 +129,43 @@ public class FileUtils { } } } - - + + /** * Copy bytes from input to output stream, leaving out stream open * - * @param in input stream - * @param out output stream - * @throws IOException on error + * @param in + * input stream + * @param out + * output stream + * @throws IOException + * on error */ public static void copyStream(InputStream in, OutputStream out) throws IOException { if (in == null) { throw new NullPointerException("Input stream is null"); } - + if (out == null) { throw new NullPointerException("Output stream is null"); } - + byte[] buf = new byte[2048]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } - - + + /** * Improved delete * - * @param path deleted path - * @param recursive recursive delete + * @param path + * deleted path + * @param recursive + * recursive delete * @return success */ public static boolean delete(File path, boolean recursive) @@ -154,50 +173,54 @@ public class FileUtils { if (!path.exists()) { return true; } - + if (!recursive || !path.isDirectory()) return path.delete(); - + String[] list = path.list(); for (int i = 0; i < list.length; i++) { if (!delete(new File(path, list[i]), true)) return false; } - + return path.delete(); } - - + + /** * Read entire file to a string. * - * @param file file + * @param file + * file * @return file contents * @throws IOException */ public static String fileToString(File file) throws IOException { FileInputStream fin = new FileInputStream(file); - + return streamToString(fin); } - - + + /** * Get files in a folder (create folder if needed) * - * @param dir folder + * @param dir + * folder * @return list of files */ public static List listDirectory(File dir) { return FileUtils.listDirectory(dir, null); } - - + + /** * Get files in a folder (create folder if needed) * - * @param dir folder - * @param filter file filter + * @param dir + * folder + * @param filter + * file filter * @return list of files */ public static List listDirectory(File dir, FileFilter filter) @@ -207,9 +230,9 @@ public class FileUtils { } catch (RuntimeException e) { Log.e("Error creating folder " + dir, e); } - + List list = new ArrayList(); - + try { for (File f : dir.listFiles(filter)) { list.add(f); @@ -217,35 +240,36 @@ public class FileUtils { } catch (Exception e) { Log.e("Error listing folder " + dir, e); } - + return list; } - - + + /** * Remove extension. * - * @param file file + * @param file + * file * @return filename without extension */ public static String[] getFilenameParts(File file) { return getFilenameParts(file.getName()); } - - + + public static String getExtension(File file) { return getExtension(file.getName()); } - - + + public static String getExtension(String file) { return StringUtils.fromLastChar(file, '.'); } - - + + /** * Remove extension. * @@ -255,41 +279,44 @@ public class FileUtils { public static String[] getFilenameParts(String filename) { String ext, name; - + try { ext = StringUtils.fromLastDot(filename); } catch (StringIndexOutOfBoundsException e) { ext = ""; } - + try { name = StringUtils.toLastDot(filename); } catch (StringIndexOutOfBoundsException e) { name = ""; Log.w("Error extracting extension from file " + filename); } - + return new String[] { name, ext }; } - - + + /** * Read entire input stream to a string, and close it. * - * @param in input stream + * @param in + * input stream * @return file contents */ public static String streamToString(InputStream in) { return streamToString(in, -1); } - - + + /** * Read input stream to a string, and close it. * - * @param in input stream - * @param lines max number of lines (-1 to disable limit) + * @param in + * input stream + * @param lines + * max number of lines (-1 to disable limit) * @return file contents */ public static String streamToString(InputStream in, int lines) @@ -298,10 +325,10 @@ public class FileUtils { Log.e(new NullPointerException("Null stream to be converted to String.")); return ""; // to avoid NPE's } - + BufferedReader br = null; StringBuilder sb = new StringBuilder(); - + String line; try { int cnt = 0; @@ -310,11 +337,11 @@ public class FileUtils { sb.append(line + "\n"); cnt++; } - + if (cnt == lines && lines > 0) { sb.append("--- end of preview ---\n"); } - + } catch (IOException e) { Log.e(e); } finally { @@ -324,15 +351,15 @@ public class FileUtils { // ignore } } - + return sb.toString(); } - - + + public static InputStream stringToStream(String text) { if (text == null) return null; - + try { return new ByteArrayInputStream(text.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { @@ -340,161 +367,170 @@ public class FileUtils { return null; } } - - + + public static InputStream getResource(String path) { return FileUtils.class.getResourceAsStream(path); } - - + + public static String getResourceAsString(String path) { return streamToString(FileUtils.class.getResourceAsStream(path)); } - - + + /** * Save string to file * - * @param file file - * @param text string - * @throws IOException on error + * @param file + * file + * @param text + * string + * @throws IOException + * on error */ public static void stringToFile(File file, String text) throws IOException { PrintStream out = null; try { out = new PrintStream(new FileOutputStream(file), false, "UTF-8"); - + out.print(text); - + out.flush(); - + } finally { if (out != null) out.close(); } } - - + + public static void deleteEmptyDirs(File base) { for (File f : listDirectory(base)) { if (!f.isDirectory()) continue; - + deleteEmptyDirs(f); - + List children = listDirectory(f); if (children.size() == 0) { f.delete(); continue; } } - + } - - + + /** * Replace special characters with place holders in a filename. * - * @param filestring filename string + * @param filestring + * filename string * @return escaped */ public static String escapeFileString(String filestring) { StringBuilder sb = new StringBuilder(); - + for (char c : filestring.toCharArray()) { switch (c) { case '%': sb.append("%%"); break; - + case '.': sb.append("%d"); break; - + default: sb.append(c); } - + } - + return sb.toString(); } - - + + /** * Unescape filename string obtained by escapeFileString(). * - * @param filestring escaped string + * @param filestring + * escaped string * @return clean string */ public static String unescapeFileString(String filestring) { filestring = filestring.replace("%d", "."); filestring = filestring.replace("%%", "%"); - + return filestring; } - - + + /** * Escape filename, keeping the same extension * - * @param filename filename + * @param filename + * filename * @return escaped */ public static String escapeFilename(String filename) { String[] parts = getFilenameParts(filename); - + return escapeFileString(parts[0]) + "." + parts[1]; } - - + + /** * Unescape filename, keeping the same extension * - * @param filename escaped filename + * @param filename + * escaped filename * @return unesaped */ public static String unescapeFilename(String filename) { String[] parts = getFilenameParts(filename); - + return unescapeFileString(parts[0]) + "." + parts[1]; } - - + + public static String getBasename(String name) { return StringUtils.toLastChar(StringUtils.fromLastChar(name, '/'), '.'); } - - + + public static String getFilename(String name) { return StringUtils.fromLastChar(name, '/'); } - - + + /** * Copy resource to file * - * @param resname resource name - * @param file out file + * @param resname + * resource name + * @param file + * out file * @throws IOException */ public static void resourceToFile(String resname, File file) throws IOException { InputStream in = null; OutputStream out = null; - + try { in = FileUtils.getResource(resname); out = new FileOutputStream(file); - + FileUtils.copyStream(in, out); } finally { try { @@ -502,21 +538,22 @@ public class FileUtils { } catch (IOException e) { // ignore } - + try { if (out != null) out.close(); } catch (IOException e) { // ignore } } - + } - - + + /** * Get resource as string, safely closing streams. * - * @param resname resource name + * @param resname + * resource name * @return resource as string, empty string on failure */ public static String resourceToString(String resname) diff --git a/src/mightypork/utils/files/OsUtils.java b/src/mightypork/utils/files/OsUtils.java index 650c1fd..fb6b33b 100644 --- a/src/mightypork/utils/files/OsUtils.java +++ b/src/mightypork/utils/files/OsUtils.java @@ -1,36 +1,34 @@ package mightypork.utils.files; - import java.io.File; public class OsUtils { - - public static enum EnumOS - { + + public static enum EnumOS { linux, macos, solaris, unknown, windows; - + public boolean isLinux() { return this == linux || this == solaris; } - - + + public boolean isMac() { return this == macos; } - - + + public boolean isWindows() { return this == windows; } } - + private static EnumOS cachedOs; - - + + /** * Get App dir, ensure it exists * @@ -41,8 +39,8 @@ public class OsUtils { { return getWorkDir(dirname, true); } - - + + /** * Get App sub-folder * @@ -54,17 +52,17 @@ public class OsUtils { public static File getWorkDir(String dirname, String subfolderName, boolean create) { File f = new File(getWorkDir(dirname), subfolderName); - + if (!f.exists() && create) { if (!f.mkdirs()) { throw new RuntimeException("Could not create."); } } - + return f; } - - + + /** * Get App sub-folder, create * @@ -76,65 +74,65 @@ public class OsUtils { { return getWorkDir(dirname, subfolderName, true); } - - + + public static EnumOS getOs() { if (cachedOs != null) return cachedOs; - + String s = System.getProperty("os.name").toLowerCase(); - + if (s.contains("win")) { cachedOs = EnumOS.windows; - + } else if (s.contains("mac")) { cachedOs = EnumOS.macos; - + } else if (s.contains("linux") || s.contains("unix")) { cachedOs = EnumOS.linux; - + } else if (s.contains("solaris") || s.contains("sunos")) { cachedOs = EnumOS.solaris; - + } else { cachedOs = EnumOS.unknown; } - + return cachedOs; } - - + + private static File getWorkDir(String dirname, boolean create) { String userhome = System.getProperty("user.home", "."); File file; - + switch (getOs()) { case linux: case solaris: file = new File(userhome, "." + dirname + '/'); break; - + case windows: String appdata = System.getenv("APPDATA"); - + if (appdata != null) { file = new File(appdata, "." + dirname + '/'); } else { file = new File(userhome, "." + dirname + '/'); } - + break; - + case macos: file = new File(userhome, "Library/Application Support/" + dirname); break; - + default: file = new File(userhome, dirname + "/"); break; } - + if (!file.exists() || !file.isDirectory()) { if (create) { if (!file.mkdirs()) { @@ -142,8 +140,8 @@ public class OsUtils { } } } - + return file; } - + } diff --git a/src/mightypork/utils/files/PropertyManager.java b/src/mightypork/utils/files/PropertyManager.java index d0b65fc..8635425 100644 --- a/src/mightypork/utils/files/PropertyManager.java +++ b/src/mightypork/utils/files/PropertyManager.java @@ -1,6 +1,5 @@ package mightypork.utils.files; - import java.io.*; import java.util.*; import java.util.Map.Entry; @@ -16,7 +15,7 @@ import mightypork.utils.math.Calc; * @author MightyPork */ public class PropertyManager { - + /** * Properties stored in file, alphabetically sorted.
* Property file is much cleaner than the normal java.util.Properties, @@ -26,11 +25,11 @@ public class PropertyManager { * @author MightyPork */ private static class SortedProperties extends Properties { - + /** A table of hex digits */ private static final char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - - + + /** * this is here because the original method is private. * @@ -41,12 +40,12 @@ public class PropertyManager { { return hexChars[(nibble & 0xF)]; } - - + + private static void writeComments(BufferedWriter bw, String comm) throws IOException { String comments = comm.replace("\n\n", "\n \n"); - + int len = comments.length(); int current = 0; int last = 0; @@ -59,7 +58,7 @@ public class PropertyManager { if (last != current) { bw.write("# " + comments.substring(last, current)); } - + if (c > '\u00ff') { uu[2] = toHex((c >> 12) & 0xf); uu[3] = toHex((c >> 8) & 0xf); @@ -83,24 +82,24 @@ public class PropertyManager { bw.newLine(); bw.newLine(); } - + /** Option: put empty line before each comment. */ public boolean cfgEmptyLineBeforeComment = true; - + /** * Option: Separate sections by newline
* Section = string before first dot in key. */ public boolean cfgSeparateSectionsByEmptyLine = true; - + private boolean firstEntry = true; - + /** Comments for individual keys */ private Hashtable keyComments = new Hashtable(); - + private String lastSectionBeginning = ""; - - + + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public synchronized Enumeration keys() @@ -113,8 +112,8 @@ public class PropertyManager { Collections.sort(keyList); return keyList.elements(); } - - + + private String saveConvert(String theString, boolean escapeSpace, boolean escapeUnicode) { int len = theString.length(); @@ -123,10 +122,10 @@ public class PropertyManager { bufLen = Integer.MAX_VALUE; } StringBuffer outBuffer = new StringBuffer(bufLen); - + for (int x = 0; x < len; x++) { char aChar = theString.charAt(x); - + // Handle common case first, selecting largest block that // avoids the specials below if ((aChar > 61) && (aChar < 127)) { @@ -138,7 +137,7 @@ public class PropertyManager { outBuffer.append(aChar); continue; } - + switch (aChar) { case ' ': if (x == 0 || escapeSpace) { @@ -146,27 +145,27 @@ public class PropertyManager { } outBuffer.append(' '); break; - + case '\t': outBuffer.append('\\'); outBuffer.append('t'); break; - + case '\n': outBuffer.append('\\'); outBuffer.append('n'); break; - + case '\r': outBuffer.append('\\'); outBuffer.append('r'); break; - + case '\f': outBuffer.append('\\'); outBuffer.append('f'); break; - + case '=': // Fall through case ':': // Fall through case '#': // Fall through @@ -174,7 +173,7 @@ public class PropertyManager { outBuffer.append('\\'); outBuffer.append(aChar); break; - + default: if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode) { outBuffer.append('\\'); @@ -188,62 +187,64 @@ public class PropertyManager { } } } - + return outBuffer.toString(); } - - + + /** * Set additional comment to a key * - * @param key key for comment - * @param comment the comment + * @param key + * key for comment + * @param comment + * the comment */ public void setKeyComment(String key, String comment) { keyComments.put(key, comment); } - - + + @SuppressWarnings("rawtypes") @Override public void store(OutputStream out, String comments) throws IOException { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); - + boolean escUnicode = false; - + if (comments != null) { writeComments(bw, comments); } - + synchronized (this) { for (Enumeration e = keys(); e.hasMoreElements();) { boolean wasNewLine = false; - + String key = (String) e.nextElement(); String val = (String) get(key); key = saveConvert(key, true, escUnicode); val = saveConvert(val, false, escUnicode); - + if (cfgSeparateSectionsByEmptyLine && !lastSectionBeginning.equals(key.split("[.]")[0])) { if (!firstEntry) { bw.newLine(); bw.newLine(); } - + wasNewLine = true; lastSectionBeginning = key.split("[.]")[0]; } - + if (keyComments.containsKey(key)) { String cm = keyComments.get(key); cm = cm.replace("\r", "\n"); cm = cm.replace("\r\n", "\n"); cm = cm.replace("\n\n", "\n \n"); - + String[] cmlines = cm.split("\n"); - + if (!wasNewLine && !firstEntry && cfgEmptyLineBeforeComment) { bw.newLine(); } @@ -252,17 +253,18 @@ public class PropertyManager { bw.newLine(); } } - + bw.write(key + " = " + val); bw.newLine(); - + firstEntry = false; } } bw.flush(); } } - + + /** * Helper class which loads Properties from UTF-8 file (Properties use * "ISO-8859-1" by default) @@ -270,11 +272,11 @@ public class PropertyManager { * @author Itay Maman */ private static class PropertiesLoader { - + private static String escapifyStr(String str) { StringBuilder result = new StringBuilder(); - + int len = str.length(); for (int x = 0; x < len; x++) { char ch = str.charAt(x); @@ -282,7 +284,7 @@ public class PropertyManager { result.append(ch); continue; } - + result.append('\\'); result.append('u'); result.append(hexDigit(ch, 12)); @@ -292,25 +294,25 @@ public class PropertyManager { } return result.toString(); } - - + + private static char hexDigit(char ch, int offset) { int val = (ch >> offset) & 0xF; if (val <= 9) { return (char) ('0' + val); } - + return (char) ('A' + val - 10); } - - + + public static SortedProperties loadProperties(SortedProperties props, InputStream is) throws IOException { return loadProperties(props, is, "utf-8"); } - - + + public static SortedProperties loadProperties(SortedProperties props, InputStream is, String encoding) throws IOException { StringBuilder sb = new StringBuilder(); @@ -320,53 +322,58 @@ public class PropertyManager { if (temp < 0) { break; } - + char c = (char) temp; sb.append(c); } - + String read = sb.toString(); - + String inputString = escapifyStr(read); byte[] bs = inputString.getBytes("ISO-8859-1"); ByteArrayInputStream bais = new ByteArrayInputStream(bs); - + SortedProperties ps = props; ps.load(bais); return ps; } } - + + /** * Property entry in Property manager. * * @author MightyPork */ private class Property { - + public String entryComment; - + public String name; - + public boolean bool = false; public boolean defbool = false; - + public double num = -1; public double defnum = -1; - + public String defstr = ""; public String str = ""; - + public PropertyType type; - - + + /** * Property * - * @param key key - * @param default_value default value - * @param entry_type type - * @param entry_comment entry comment + * @param key + * key + * @param default_value + * default value + * @param entry_type + * type + * @param entry_comment + * entry comment */ public Property(String key, boolean default_value, PropertyType entry_type, String entry_comment) { name = key; @@ -374,15 +381,19 @@ public class PropertyManager { type = entry_type; entryComment = entry_comment; } - - + + /** * Property entry * - * @param key property key - * @param default_value default value - * @param entry_type property type from enum - * @param entry_comment property comment or null + * @param key + * property key + * @param default_value + * default value + * @param entry_type + * property type from enum + * @param entry_comment + * property comment or null */ public Property(String key, double default_value, PropertyType entry_type, String entry_comment) { name = key; @@ -390,15 +401,19 @@ public class PropertyManager { type = entry_type; entryComment = entry_comment; } - - + + /** * Property * - * @param key key - * @param default_value default value - * @param entry_type type - * @param entry_comment entry comment + * @param key + * key + * @param default_value + * default value + * @param entry_type + * type + * @param entry_comment + * entry comment */ public Property(String key, String default_value, PropertyType entry_type, String entry_comment) { name = key; @@ -406,8 +421,8 @@ public class PropertyManager { type = entry_type; entryComment = entry_comment; } - - + + /** * Get boolean * @@ -417,8 +432,8 @@ public class PropertyManager { { return bool; } - - + + /** * Get number * @@ -428,8 +443,8 @@ public class PropertyManager { { return (int) Math.round(num); } - - + + /** * Get number as double * @@ -439,8 +454,8 @@ public class PropertyManager { { return num; } - - + + /** * Get string * @@ -450,8 +465,8 @@ public class PropertyManager { { return str; } - - + + /** * Is this entry valid? * @@ -462,81 +477,82 @@ public class PropertyManager { switch (type) { case STRING: return str != null; - + case BOOLEAN: case INT: case DOUBLE: return true; - + } return false; } - - + + /** * Load property value from a file * - * @param string the string loaded + * @param string + * the string loaded * @return was OK */ public boolean parse(String string) { switch (type) { case INT: - + if (string == null) { num = defnum; return false; } - + try { num = Integer.parseInt(string.trim()); } catch (NumberFormatException e) { num = defnum; } - + break; - + case DOUBLE: - + if (string == null) { num = defnum; return false; } - + try { num = Double.parseDouble(string.trim()); } catch (NumberFormatException e) { num = defnum; } - + break; - + case STRING: - + if (string == null) { str = defstr; return false; } - + str = string; break; - + case BOOLEAN: - + if (string == null) { bool = defbool; return false; } - + String string2 = string.toLowerCase(); bool = string2.equals("yes") || string2.equals("true") || string2.equals("on") || string2.equals("enabled") || string2.equals("enable"); } - + return true; } - - + + /** * prepare the contents for insertion into Properties * @@ -550,24 +566,24 @@ public class PropertyManager { num = defnum; } } - + switch (type) { case INT: return Integer.toString((int) num); - + case DOUBLE: return Calc.floatToString((float) num); - + case STRING: return str; - + case BOOLEAN: return bool ? "True" : "False"; } return null; } - - + + /** * If this entry is not valid, change it to the dafault value. */ @@ -580,15 +596,15 @@ public class PropertyManager { } } } - + + /** * Property types */ - private enum PropertyType - { + private enum PropertyType { BOOLEAN, INT, STRING, DOUBLE; } - + /** put newline before entry comments */ private boolean cfgNewlineBeforeComments = true; /** Disable entry validation */ @@ -597,21 +613,23 @@ public class PropertyManager { private boolean cfgSeparateSections = true; /** Force save, even if nothing changed (used to save changed comments) */ private boolean cfgForceSave; - + private File file; private String fileComment = ""; - + private TreeMap entries; private TreeMap keyRename; private TreeMap setValues; private SortedProperties pr = new SortedProperties(); - - + + /** * Create property manager from file path and an initial comment. * - * @param file file with the props - * @param comment the initial comment. Use \n in it if you want. + * @param file + * file with the props + * @param comment + * the initial comment. Use \n in it if you want. */ public PropertyManager(File file, String comment) { this.file = file; @@ -620,8 +638,8 @@ public class PropertyManager { this.keyRename = new TreeMap(); this.fileComment = comment; } - - + + /** * Load, fix and write to file. */ @@ -633,7 +651,7 @@ public class PropertyManager { new File(file.getParent()).mkdirs(); fis = new FileInputStream(file); pr = PropertiesLoader.loadProperties(pr, fis); - + } catch (IOException e) { needsSave = true; pr = new SortedProperties(); @@ -644,12 +662,12 @@ public class PropertyManager { e.printStackTrace(); } } - + pr.cfgSeparateSectionsByEmptyLine = cfgSeparateSections; pr.cfgEmptyLineBeforeComment = cfgNewlineBeforeComments; - + ArrayList keyList = new ArrayList(); - + // rename keys for (Entry entry : keyRename.entrySet()) { if (pr.getProperty(entry.getKey()) == null) { @@ -659,43 +677,43 @@ public class PropertyManager { pr.remove(entry.getKey()); needsSave = true; } - + // set the override values into the freshly loaded properties file for (Entry entry : setValues.entrySet()) { pr.setProperty(entry.getKey(), entry.getValue()); needsSave = true; } - + // validate entries one by one, replace with default when needed for (Property entry : entries.values()) { keyList.add(entry.name); - + String propOrig = pr.getProperty(entry.name); if (!entry.parse(propOrig)) needsSave = true; if (!cfgNoValidate) { entry.validate(); } - + if (entry.entryComment != null) { pr.setKeyComment(entry.name, entry.entryComment); } - + if (propOrig == null || !entry.toString().equals(propOrig)) { pr.setProperty(entry.name, entry.toString()); - + needsSave = true; } } - + // removed unused props for (String propname : pr.keySet().toArray(new String[pr.size()])) { if (!keyList.contains(propname)) { pr.remove(propname); needsSave = true; } - + } - + // save if needed if (needsSave || cfgForceSave) { try { @@ -704,52 +722,57 @@ public class PropertyManager { ioe.printStackTrace(); } } - + setValues.clear(); keyRename.clear(); } - - + + /** - * @param newlineBeforeComments put newline before comments + * @param newlineBeforeComments + * put newline before comments */ public void cfgNewlineBeforeComments(boolean newlineBeforeComments) { this.cfgNewlineBeforeComments = newlineBeforeComments; } - - + + /** - * @param separateSections do separate sections by newline + * @param separateSections + * do separate sections by newline */ public void cfgSeparateSections(boolean separateSections) { this.cfgSeparateSections = separateSections; } - - + + /** - * @param forceSave save even if unchanged. + * @param forceSave + * save even if unchanged. */ public void cfgForceSave(boolean forceSave) { this.cfgForceSave = forceSave; } - - + + /** - * @param validate enable validation + * @param validate + * enable validation */ public void enableValidation(boolean validate) { this.cfgNoValidate = !validate; } - - + + /** * Get a property entry (rarely used) * - * @param n key + * @param n + * key * @return the entry */ private Property get(String n) @@ -760,12 +783,13 @@ public class PropertyManager { return null; } } - - + + /** * Get boolean property * - * @param n key + * @param n + * key * @return the boolean found, or false */ public Boolean getBoolean(String n) @@ -776,12 +800,13 @@ public class PropertyManager { return false; } } - - + + /** * Get numeric property * - * @param n key + * @param n + * key * @return the int found, or null */ public Integer getInteger(String n) @@ -792,12 +817,13 @@ public class PropertyManager { return -1; } } - - + + /** * Get numeric property as double * - * @param n key + * @param n + * key * @return the double found, or null */ public Double getDouble(String n) @@ -808,12 +834,13 @@ public class PropertyManager { return -1D; } } - - + + /** * Get string property * - * @param n key + * @param n + * key * @return the string found, or null */ public String getString(String n) @@ -824,140 +851,164 @@ public class PropertyManager { return null; } } - - + + /** * Add a boolean property * - * @param n key - * @param d default value + * @param n + * key + * @param d + * default value */ public void putBoolean(String n, boolean d) { entries.put(n, new Property(n, d, PropertyType.BOOLEAN, null)); return; } - - + + /** * Add a boolean property * - * @param n key - * @param d default value - * @param comment the in-file comment + * @param n + * key + * @param d + * default value + * @param comment + * the in-file comment */ public void putBoolean(String n, boolean d, String comment) { entries.put(n, new Property(n, d, PropertyType.BOOLEAN, comment)); return; } - - + + /** * Add a numeric property (double) * - * @param n key - * @param d default value + * @param n + * key + * @param d + * default value */ public void putDouble(String n, int d) { entries.put(n, new Property(n, d, PropertyType.DOUBLE, null)); return; } - - + + /** * Add a numeric property (double) * - * @param n key - * @param d default value - * @param comment the in-file comment + * @param n + * key + * @param d + * default value + * @param comment + * the in-file comment */ public void putDouble(String n, int d, String comment) { entries.put(n, new Property(n, d, PropertyType.DOUBLE, comment)); return; } - - + + /** * Add a numeric property * - * @param n key - * @param d default value + * @param n + * key + * @param d + * default value */ public void putInteger(String n, int d) { entries.put(n, new Property(n, d, PropertyType.INT, null)); return; } - - + + /** * Add a numeric property * - * @param n key - * @param d default value - * @param comment the in-file comment + * @param n + * key + * @param d + * default value + * @param comment + * the in-file comment */ public void putInteger(String n, int d, String comment) { entries.put(n, new Property(n, d, PropertyType.INT, comment)); return; } - - + + /** * Add a string property * - * @param n key - * @param d default value + * @param n + * key + * @param d + * default value */ public void putString(String n, String d) { entries.put(n, new Property(n, d, PropertyType.STRING, null)); return; } - - + + /** * Add a string property * - * @param n key - * @param d default value - * @param comment the in-file comment + * @param n + * key + * @param d + * default value + * @param comment + * the in-file comment */ public void putString(String n, String d, String comment) { entries.put(n, new Property(n, d, PropertyType.STRING, comment)); return; } - - + + /** * Rename key before doing "apply"; value is preserved * - * @param oldKey old key - * @param newKey new key + * @param oldKey + * old key + * @param newKey + * new key */ public void renameKey(String oldKey, String newKey) { keyRename.put(oldKey, newKey); return; } - - + + /** * Set value saved to certain key; use to save runtime-changed configuration * values. * - * @param key key - * @param value the saved value + * @param key + * key + * @param value + * the saved value */ public void setValue(String key, Object value) { setValues.put(key, value.toString()); return; } - + } diff --git a/src/mightypork/utils/files/SimpleConfig.java b/src/mightypork/utils/files/SimpleConfig.java index e265938..86f5ef7 100644 --- a/src/mightypork/utils/files/SimpleConfig.java +++ b/src/mightypork/utils/files/SimpleConfig.java @@ -1,6 +1,5 @@ package mightypork.utils.files; - import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -22,161 +21,166 @@ import mightypork.utils.logging.Log; * @author MightyPork */ public class SimpleConfig { - + /** * Load list from file * - * @param file file + * @param file + * file * @return map of keys and values * @throws IOException */ public static List listFromFile(File file) throws IOException { String fileText = FileUtils.fileToString(file); - + return listFromString(fileText); } - - + + /** * Load map from file * - * @param file file + * @param file + * file * @return map of keys and values * @throws IOException */ public static Map mapFromFile(File file) throws IOException { String fileText = FileUtils.fileToString(file); - + return mapFromString(fileText); } - - + + /** * Load list from string * - * @param text text of the file + * @param text + * text of the file * @return map of keys and values */ public static List listFromString(String text) { List list = new ArrayList(); - + String[] groupsLines = text.split("\n"); - + for (String s : groupsLines) { // ignore invalid lines if (s.length() == 0) continue; if (s.startsWith("#") || s.startsWith("//")) continue; - + // NULL value if (s.equalsIgnoreCase("NULL")) s = null; - + if (s != null) s = s.replace("\\n", "\n"); - + // save extracted key-value pair list.add(s); } - + return list; } - - + + /** * Load map from string * - * @param text text of the file + * @param text + * text of the file * @return map of keys and values */ public static Map mapFromString(String text) { LinkedHashMap pairs = new LinkedHashMap(); - + String[] groupsLines = text.split("\n"); - + for (String s : groupsLines) { // ignore invalid lines if (s.length() == 0) continue; if (s.startsWith("#") || s.startsWith("//")) continue; if (!s.contains("=")) continue; - + // split and trim String[] parts = s.split("="); for (int i = 0; i < parts.length; i++) { parts[i] = parts[i].trim(); } - + // check if both parts are valid if (parts.length == 0) { Log.w("Bad line in config file: " + s); continue; } - + if (parts.length == 1) { parts = new String[] { parts[0], "" }; } - + if (parts.length != 2) { Log.w("Bad line in config file: " + s); continue; } - + // NULL value if (parts[0].equalsIgnoreCase("NULL")) parts[0] = null; if (parts[1].equalsIgnoreCase("NULL")) parts[1] = null; - + if (parts[0] != null) parts[0] = parts[0].replace("\\n", "\n"); if (parts[1] != null) parts[1] = parts[1].replace("\\n", "\n"); - + // save extracted key-value pair pairs.put(parts[0], parts[1]); } - + return pairs; } - - + + /** * Save map to file * * @param target * @param data - * @param allowNulls allow nulls. + * @param allowNulls + * allow nulls. * @throws IOException */ public static void mapToFile(File target, Map data, boolean allowNulls) throws IOException { List lines = new ArrayList(); - + for (Entry e : data.entrySet()) { String key = e.getKey(); String value = e.getValue(); - + if (!allowNulls && (key == null || value == null || key.length() == 0 || value.length() == 0)) continue; - + if (key == null) key = "NULL"; if (value == null) value = "NULL"; - + key = key.replace("\n", "\\n"); value = value.replace("\n", "\\n"); - + lines.add(key + " = " + value); } - - String text = ""; //# File written by SimpleConfig - + + String text = ""; // # File written by SimpleConfig + for (String s : lines) { if (text.length() > 0) text += "\n"; - + text += s; } - + FileUtils.stringToFile(target, text); - + } - - + + /** * Save list to file * @@ -186,19 +190,19 @@ public class SimpleConfig { */ public static void listToFile(File target, List data) throws IOException { - String text = ""; //# File written by SimpleConfig - + String text = ""; // # File written by SimpleConfig + for (String s : data) { if (text.length() > 0) text += "\n"; - + if (s == null) s = "NULL"; - + s = s.replace("\n", "\\n"); - + text += s; } - + FileUtils.stringToFile(target, text); - + } } diff --git a/src/mightypork/utils/files/ZipBuilder.java b/src/mightypork/utils/files/ZipBuilder.java index c088701..a4ad0fe 100644 --- a/src/mightypork/utils/files/ZipBuilder.java +++ b/src/mightypork/utils/files/ZipBuilder.java @@ -1,6 +1,5 @@ package mightypork.utils.files; - import java.io.*; import java.util.HashSet; import java.util.zip.ZipEntry; @@ -15,29 +14,32 @@ import mightypork.utils.logging.Log; * @author MightyPork */ public class ZipBuilder { - + private ZipOutputStream out; private HashSet included = new HashSet(); - - + + /** - * @param target target zip file - * @throws FileNotFoundException if the file is directory or cannot be - * created + * @param target + * target zip file + * @throws FileNotFoundException + * if the file is directory or cannot be created */ public ZipBuilder(File target) throws FileNotFoundException { target.getParentFile().mkdirs(); - + FileOutputStream dest = new FileOutputStream(target); out = new ZipOutputStream(new BufferedOutputStream(dest)); } - - + + /** * Add stream to a path * - * @param path path - * @param in stream + * @param path + * path + * @param in + * stream * @throws IOException */ public void addStream(String path, InputStream in) throws IOException @@ -48,18 +50,20 @@ public class ZipBuilder { return; // ignore } included.add(path); - + out.putNextEntry(new ZipEntry(path)); - + FileUtils.copyStream(in, out); } - - + + /** * Add string as a file * - * @param path path - * @param text text to write + * @param path + * path + * @param text + * text to write * @throws IOException */ public void addString(String path, String text) throws IOException @@ -67,19 +71,21 @@ public class ZipBuilder { path = preparePath(path); if (included.contains(path)) return; // ignore included.add(path); - + out.putNextEntry(new ZipEntry(path)); - + InputStream in = FileUtils.stringToStream(text); FileUtils.copyStream(in, out); } - - + + /** * Add resource obtained via FileUtils.getResource() * - * @param path path - * @param resPath resource path + * @param path + * path + * @param resPath + * resource path * @throws IOException */ public void addResource(String path, String resPath) throws IOException @@ -87,30 +93,31 @@ public class ZipBuilder { path = preparePath(path); if (included.contains(path)) return; // ignore included.add(path); - + out.putNextEntry(new ZipEntry(path)); - + InputStream in = FileUtils.getResource(resPath); FileUtils.copyStream(in, out); } - - + + /** * Normalize path * - * @param path original path + * @param path + * original path * @return normalized path */ private String preparePath(String path) { path = path.replace("\\", "/"); - + if (path.charAt(0) == '/') path = path.substring(1); - + return path; } - - + + /** * Close the zip stream * diff --git a/src/mightypork/utils/files/ZipUtils.java b/src/mightypork/utils/files/ZipUtils.java index 56e86c4..fb3287d 100644 --- a/src/mightypork/utils/files/ZipUtils.java +++ b/src/mightypork/utils/files/ZipUtils.java @@ -1,6 +1,5 @@ package mightypork.utils.files; - import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -21,16 +20,19 @@ import mightypork.utils.string.validation.StringFilter; * @author MightyPork */ public class ZipUtils { - + private static final int BUFFER_SIZE = 2048; - - + + /** * Extract zip file to target directory * - * @param file zip file - * @param outputDir target directory - * @param filter string filter (will be used to test entry names (paths)) + * @param file + * zip file + * @param outputDir + * target directory + * @param filter + * string filter (will be used to test entry names (paths)) * @return list of entries extracted (paths) * @throws IOException */ @@ -39,66 +41,71 @@ public class ZipUtils { ZipFile zip = null; try { zip = new ZipFile(file); - + return extractZip(zip, outputDir, filter); - + } finally { try { if (zip != null) zip.close(); } catch (IOException e) { - //ignore + // ignore } } } - - + + /** * Extract zip file to target directory * - * @param zip open zip file - * @param outputDir target directory - * @param filter string filter (will be used to test entry names (paths)) + * @param zip + * open zip file + * @param outputDir + * target directory + * @param filter + * string filter (will be used to test entry names (paths)) * @return list of entries extracted (paths) * @throws IOException */ public static List extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException { ArrayList files = new ArrayList(); - + outputDir.mkdirs(); - + Enumeration zipFileEntries = zip.entries(); - + // process each entry while (zipFileEntries.hasMoreElements()) { ZipEntry entry = zipFileEntries.nextElement(); - + // parse filename and path String entryPath = entry.getName(); File destFile = new File(outputDir, entryPath); File destinationParent = destFile.getParentFile(); - + if (entry.isDirectory() || (filter != null && !filter.accept(entryPath))) continue; - + // make sure directories exist destinationParent.mkdirs(); - + if (!entry.isDirectory()) { extractZipEntry(zip, entry, destFile); files.add(entryPath); } } - + return files; } - - + + /** * Read zip entries and add their paths to a list * - * @param zipFile open zip file + * @param zipFile + * open zip file * @return list of entry names - * @throws IOException on error + * @throws IOException + * on error */ public static List listZip(File zipFile) throws IOException { @@ -110,84 +117,93 @@ public class ZipUtils { try { if (zip != null) zip.close(); } catch (IOException e) { - //ignore + // ignore } } } - - + + /** * Read zip entries and add their paths to a list * - * @param zip open zip file + * @param zip + * open zip file * @return list of entry names - * @throws IOException on error + * @throws IOException + * on error */ public static List listZip(ZipFile zip) throws IOException { ArrayList files = new ArrayList(); - + Enumeration zipFileEntries = zip.entries(); - + // process each entry while (zipFileEntries.hasMoreElements()) { ZipEntry entry = zipFileEntries.nextElement(); - + if (!entry.isDirectory()) { files.add(entry.getName()); } } - + return files; } - - + + /** * Extract one zip entry to target file * - * @param zip open zip file - * @param entry entry from the zip file - * @param destFile destination file ((NOT directory!) - * @throws IOException on error + * @param zip + * open zip file + * @param entry + * entry from the zip file + * @param destFile + * destination file ((NOT directory!) + * @throws IOException + * on error */ public static void extractZipEntry(ZipFile zip, ZipEntry entry, File destFile) throws IOException { destFile.getParentFile().mkdirs(); - + BufferedInputStream is = null; FileOutputStream fos = null; BufferedOutputStream dest = null; - + try { is = new BufferedInputStream(zip.getInputStream(entry)); fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER_SIZE); - + FileUtils.copyStream(is, dest); } finally { try { if (is != null) is.close(); } catch (IOException e) { - //ignore + // ignore } - + try { if (dest != null) dest.close(); } catch (IOException e) { - //ignore + // ignore } - + } } - - + + /** * Load zip entry to String * - * @param zip open zip file - * @param entry entry from the zip file + * @param zip + * open zip file + * @param entry + * entry from the zip file * @return loaded string - * @throws IOException on error + * @throws IOException + * on error */ public static String zipEntryToString(ZipFile zip, ZipEntry entry) throws IOException { @@ -200,16 +216,16 @@ public class ZipUtils { try { if (is != null) is.close(); } catch (IOException e) { - //ignore + // ignore } } } - - + + public static boolean entryExists(File selectedFile, String string) { ZipFile zf = null; - + try { zf = new ZipFile(selectedFile); return zf.getEntry(string) != null; @@ -219,9 +235,9 @@ public class ZipUtils { try { if (zf != null) zf.close(); } catch (IOException e) { - //ignore + // ignore } } - + } } diff --git a/src/mightypork/utils/files/ion/AbstractIonList.java b/src/mightypork/utils/files/ion/AbstractIonList.java index e336b0d..d9618f3 100644 --- a/src/mightypork/utils/files/ion/AbstractIonList.java +++ b/src/mightypork/utils/files/ion/AbstractIonList.java @@ -1,6 +1,5 @@ package mightypork.utils.files.ion; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -14,15 +13,16 @@ import java.util.ArrayList; * @param */ public abstract class AbstractIonList extends ArrayList implements Ionizable { - + @Override public void ionRead(InputStream in) throws IonException { try { while (true) { byte b = StreamUtils.readByte(in); - + if (b == IonMarks.ENTRY) { + @SuppressWarnings("unchecked") T value = (T) Ion.readObject(in); add(value); } else if (b == IonMarks.END) { @@ -36,8 +36,8 @@ public abstract class AbstractIonList extends ArrayList implements Ionizab throw new IonException("Error reading ion list", e); } } - - + + @Override public void ionWrite(OutputStream out) throws IonException { @@ -53,27 +53,31 @@ public abstract class AbstractIonList extends ArrayList implements Ionizab throw new IonException("Error reading ion map", e); } } - - + + /** * Read custom data of this AbstractIonList implementation * - * @param in input stream + * @param in + * input stream */ public void ionReadCustomData(InputStream in) - {} - - + { + } + + /** * Write custom data of this AbstractIonList implementation * - * @param out output stream + * @param out + * output stream */ public void ionWriteCustomData(OutputStream out) - {} - - + { + } + + @Override public abstract byte ionMark(); - + } diff --git a/src/mightypork/utils/files/ion/AbstractIonMap.java b/src/mightypork/utils/files/ion/AbstractIonMap.java index 107c187..ddafa0b 100644 --- a/src/mightypork/utils/files/ion/AbstractIonMap.java +++ b/src/mightypork/utils/files/ion/AbstractIonMap.java @@ -1,6 +1,5 @@ package mightypork.utils.files.ion; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -14,21 +13,21 @@ import java.util.LinkedHashMap; * @param */ public abstract class AbstractIonMap extends LinkedHashMap implements Ionizable { - + @Override public V get(Object key) { return super.get(key); } - - + + @Override public V put(String key, V value) { return super.put(key, value); } - - + + @Override public void ionRead(InputStream in) throws IonException { @@ -37,13 +36,11 @@ public abstract class AbstractIonMap extends LinkedHashMap impleme byte b = StreamUtils.readByte(in); if (b == IonMarks.ENTRY) { String key = StreamUtils.readStringBytes(in); - V value; - try { - value = (V) Ion.readObject(in); - put(key, value); - } catch (IonException e) { - e.printStackTrace(); - } + + @SuppressWarnings("unchecked") + V value = (V) Ion.readObject(in); + put(key, value); + } else if (b == IonMarks.END) { break; } else { @@ -55,8 +52,8 @@ public abstract class AbstractIonMap extends LinkedHashMap impleme throw new IonException("Error reading ion map", e); } } - - + + @Override public void ionWrite(OutputStream out) throws IonException { @@ -72,30 +69,34 @@ public abstract class AbstractIonMap extends LinkedHashMap impleme throw new IonException("Error reading ion map", e); } } - - + + /** * Read custom data of this AbstractIonMap implementation * - * @param in input stream + * @param in + * input stream */ public void ionReadCustomData(InputStream in) - {} - - + { + } + + /** * Write custom data of this AbstractIonMap implementation * - * @param out output stream + * @param out + * output stream */ public void ionWriteCustomData(OutputStream out) - {} - - + { + } + + @Override public byte ionMark() { return IonMarks.MAP; } - + } diff --git a/src/mightypork/utils/files/ion/Ion.java b/src/mightypork/utils/files/ion/Ion.java index 7149379..8cb805b 100644 --- a/src/mightypork/utils/files/ion/Ion.java +++ b/src/mightypork/utils/files/ion/Ion.java @@ -1,6 +1,5 @@ package mightypork.utils.files.ion; - import java.io.*; import java.util.HashMap; import java.util.Map; @@ -14,10 +13,10 @@ import mightypork.utils.math.Calc; * @author MightyPork */ public class Ion { - + /** Ionizables */ private static Map> customIonizables = new HashMap>(); - + // register default ionizables static { try { @@ -27,13 +26,15 @@ public class Ion { e.printStackTrace(); } } - - + + /** * Register new Ionizable for direct reconstructing. * - * @param mark byte mark to be used, see {@link IonMarks} for reference. - * @param objClass class of the registered Ionizable + * @param mark + * byte mark to be used, see {@link IonMarks} for reference. + * @param objClass + * class of the registered Ionizable * @throws IonException */ public static void registerIonizable(byte mark, Class objClass) throws IonException @@ -43,12 +44,13 @@ public class Ion { } customIonizables.put(mark, objClass); } - - + + /** * Load Ion object from file. * - * @param file file path + * @param file + * file path * @return the loaded object * @throws IonException */ @@ -56,14 +58,16 @@ public class Ion { { return fromFile(new File(file)); } - - + + /** * Load Ion object from file. * - * @param file file + * @param file + * file * @return the loaded object - * @throws IonException on failure + * @throws IonException + * on failure */ public static Object fromFile(File file) throws IonException { @@ -72,7 +76,7 @@ public class Ion { in = new FileInputStream(file); Object obj = fromStream(in); return obj; - + } catch (IOException e) { throw new IonException("Error loading ION file.", e); } finally { @@ -85,12 +89,13 @@ public class Ion { } } } - - + + /** * Load Ion object from stream. * - * @param in input stream + * @param in + * input stream * @return the loaded object * @throws IonException */ @@ -98,26 +103,30 @@ public class Ion { { return readObject(in); } - - + + /** * Store Ion object to file. * - * @param path file path - * @param obj object to store + * @param path + * file path + * @param obj + * object to store * @throws IonException */ public static void toFile(String path, Object obj) throws IonException { toFile(new File(path), obj); } - - + + /** * Store Ion object to file. * - * @param path file path - * @param obj object to store + * @param path + * file path + * @param obj + * object to store * @throws IonException */ public static void toFile(File path, Object obj) throws IonException @@ -126,13 +135,13 @@ public class Ion { try { String f = path.toString(); File dir = new File(f.substring(0, f.lastIndexOf(File.separator))); - + dir.mkdirs(); - + out = new FileOutputStream(path); - + toStream(out, obj); - + out.flush(); out.close(); } catch (Exception e) { @@ -145,28 +154,31 @@ public class Ion { e.printStackTrace(); } } - + } } - - + + /** * Store Ion object to output stream. * - * @param out output stream * - * @param obj object to store + * @param out + * output stream * + * @param obj + * object to store * @throws IonException */ public static void toStream(OutputStream out, Object obj) throws IonException { writeObject(out, obj); } - - + + /** * Read single ionizable or primitive object from input stream * - * @param in input stream + * @param in + * input stream * @return the loaded object * @throws IonException */ @@ -188,7 +200,7 @@ public class Ion { ion.ionRead(in); return ion; } - + switch (b) { case IonMarks.BOOLEAN: return StreamUtils.readBoolean(in); @@ -216,13 +228,15 @@ public class Ion { throw new IonException("Error loading ION file: ", e); } } - - + + /** * Write single ionizable or primitive object to output stream * - * @param out output stream - * @param obj stored object + * @param out + * output stream + * @param obj + * stored object * @throws IonException */ public static void writeObject(OutputStream out, Object obj) throws IonException @@ -233,66 +247,66 @@ public class Ion { ((Ionizable) obj).ionWrite(out); return; } - + if (obj instanceof Boolean) { out.write(IonMarks.BOOLEAN); StreamUtils.writeBoolean(out, (Boolean) obj); return; } - + if (obj instanceof Byte) { out.write(IonMarks.BYTE); StreamUtils.writeByte(out, (Byte) obj); return; } - + if (obj instanceof Character) { out.write(IonMarks.CHAR); StreamUtils.writeChar(out, (Character) obj); return; } - + if (obj instanceof Short) { out.write(IonMarks.SHORT); StreamUtils.writeShort(out, (Short) obj); return; } - + if (obj instanceof Integer) { out.write(IonMarks.INT); StreamUtils.writeInt(out, (Integer) obj); return; } - + if (obj instanceof Long) { out.write(IonMarks.LONG); StreamUtils.writeLong(out, (Long) obj); return; } - + if (obj instanceof Float) { out.write(IonMarks.FLOAT); StreamUtils.writeFloat(out, (Float) obj); return; } - + if (obj instanceof Double) { out.write(IonMarks.DOUBLE); StreamUtils.writeDouble(out, (Double) obj); return; } - + if (obj instanceof String) { out.write(IonMarks.STRING); StreamUtils.writeString(out, (String) obj); return; } - + throw new IonException(Calc.cname(obj) + " can't be stored in Ion storage."); - + } catch (IOException e) { throw new IonException("Could not store " + obj, e); } } - + } diff --git a/src/mightypork/utils/files/ion/IonException.java b/src/mightypork/utils/files/ion/IonException.java index b3bdfd8..26adb8e 100644 --- a/src/mightypork/utils/files/ion/IonException.java +++ b/src/mightypork/utils/files/ion/IonException.java @@ -1,25 +1,24 @@ package mightypork.utils.files.ion; - public class IonException extends Exception { - + public IonException() { super(); } - - + + public IonException(String message, Throwable cause) { super(message, cause); } - - + + public IonException(String message) { super(message); } - - + + public IonException(Throwable cause) { super(cause); } - + } diff --git a/src/mightypork/utils/files/ion/IonList.java b/src/mightypork/utils/files/ion/IonList.java index 4b627e2..d245b7e 100644 --- a/src/mightypork/utils/files/ion/IonList.java +++ b/src/mightypork/utils/files/ion/IonList.java @@ -1,133 +1,132 @@ package mightypork.utils.files.ion; - /** * Ionizable Arraylist * * @author MightyPork */ public class IonList extends AbstractIonList implements Ionizable { - + public Ionizable getIonizable(int index) throws IonException { return (Ionizable) getCheckType(index, Ionizable.class); } - - + + public boolean getBoolean(int index) throws IonException { return (Boolean) getCheckType(index, Boolean.class); } - - + + public byte getByte(int index) throws IonException { return (Byte) getCheckType(index, Byte.class); } - - + + public char getChar(int index) throws IonException { return (Character) getCheckType(index, Character.class); } - - + + public short getShort(int index) throws IonException { return (Short) getCheckType(index, Short.class); } - - + + public int getInt(int index) throws IonException { return (Integer) getCheckType(index, Integer.class); } - - + + public long getLong(int index) throws IonException { return (Long) getCheckType(index, Long.class); } - - + + public float getFloat(int index) throws IonException { return (Float) getCheckType(index, Float.class); } - - + + public double getDouble(int index) throws IonException { return (Double) getCheckType(index, Double.class); } - - + + public String getString(int index) throws IonException { return (String) getCheckType(index, String.class); } - - + + public void addIonizable(Ionizable o) throws IonException { addCheckNull(o); } - - + + public void addBoolean(boolean o) throws IonException { addCheckNull(o); } - - + + public void addByte(byte o) throws IonException { addCheckNull(o); } - - + + public void addChar(char o) throws IonException { addCheckNull(o); } - - + + public void addShort(short o) throws IonException { addCheckNull(o); } - - + + public void addInt(int o) throws IonException { addCheckNull(o); } - - + + public void addLong(long o) throws IonException { addCheckNull(o); } - - + + public void addFloat(float o) throws IonException { addCheckNull(o); } - - + + public void addDouble(double o) throws IonException { addCheckNull(o); } - - + + public void addString(String o) throws IonException { addCheckNull(o); } - - + + public Object getCheckType(int index, Class type) throws IonException { try { @@ -140,18 +139,18 @@ public class IonList extends AbstractIonList implements Ionizable { throw new IonException("Out of bounds"); } } - - + + private void addCheckNull(Object o) throws IonException { if (o == null) throw new IonException("Cannot store null"); } - - + + @Override public byte ionMark() { return IonMarks.LIST; } - + } diff --git a/src/mightypork/utils/files/ion/IonMap.java b/src/mightypork/utils/files/ion/IonMap.java index 7b02f2a..b6c64e9 100644 --- a/src/mightypork/utils/files/ion/IonMap.java +++ b/src/mightypork/utils/files/ion/IonMap.java @@ -1,156 +1,155 @@ package mightypork.utils.files.ion; - /** * Ionizable HashMap * * @author MightyPork */ public class IonMap extends AbstractIonMap implements Ionizable { - + public boolean getBoolean(String key) { return (Boolean) get(key); } - - + + public boolean getBool(String key) { return (Boolean) get(key); } - - + + public byte getByte(String key) { return (Byte) get(key); } - - + + public char getChar(String key) { return (Character) get(key); } - - + + public short getShort(String key) { return (Short) get(key); } - - + + public int getInt(String key) { return (Integer) get(key); } - - + + public long getLong(String key) { return (Long) get(key); } - - + + public float getFloat(String key) { return (Float) get(key); } - - + + public double getDouble(String key) { return (Double) get(key); } - - + + public String getString(String key) { return (String) get(key); } - - + + @Override public Object get(Object arg0) { return super.get(arg0); } - - + + public void putBoolean(String key, boolean num) { put(key, num); } - - + + public void putBool(String key, boolean num) { put(key, num); } - - + + public void putByte(String key, int num) { put(key, (byte) num); } - - + + public void putChar(String key, char num) { put(key, num); } - - + + public void putCharacter(String key, char num) { put(key, num); } - - + + public void putShort(String key, int num) { put(key, num); } - - + + public void putInt(String key, int num) { put(key, num); } - - + + public void putInteger(String key, int num) { put(key, num); } - - + + public void putLong(String key, long num) { put(key, num); } - - + + public void putFloat(String key, double num) { put(key, (float) num); } - - + + public void putDouble(String key, double num) { put(key, num); } - - + + public void putString(String key, String num) { put(key, num); } - - + + @Override public byte ionMark() { return IonMarks.MAP; } - + } diff --git a/src/mightypork/utils/files/ion/IonMarks.java b/src/mightypork/utils/files/ion/IonMarks.java index a518d2b..7dc8809 100644 --- a/src/mightypork/utils/files/ion/IonMarks.java +++ b/src/mightypork/utils/files/ion/IonMarks.java @@ -1,57 +1,56 @@ package mightypork.utils.files.ion; - /** * Byte marks used to structure data in Ion files. * * @author MightyPork */ public class IonMarks { - + /** Null value */ public static final byte NULL = 0; - + /** Boolean value */ public static final byte BOOLEAN = 1; - + /** Byte value */ public static final byte BYTE = 2; - + /** Character value */ public static final byte CHAR = 3; - + /** Short value */ public static final byte SHORT = 4; - + /** Integer value */ public static final byte INT = 5; - + /** Long value */ public static final byte LONG = 6; - + /** Float value */ public static final byte FLOAT = 7; - + /** Double value */ public static final byte DOUBLE = 8; - + /** String value */ public static final byte STRING = 9; - + /** List value (begin) - contains entries, ends with END */ public static final byte LIST = 10; - + /** Map value (begin) - contains entries, ends with END */ public static final byte MAP = 11; - + /** * List / Map entry
* In list directly followed by entry value. In map followed by (string) key * and the entry value. */ public static final byte ENTRY = 12; - + /** End of List / Map */ public static final byte END = 13; - + } diff --git a/src/mightypork/utils/files/ion/Ionizable.java b/src/mightypork/utils/files/ion/Ionizable.java index 37941d2..b3b5f51 100644 --- a/src/mightypork/utils/files/ion/Ionizable.java +++ b/src/mightypork/utils/files/ion/Ionizable.java @@ -1,6 +1,5 @@ package mightypork.utils.files.ion; - import java.io.InputStream; import java.io.OutputStream; @@ -13,27 +12,29 @@ import java.io.OutputStream; * @author MightyPork */ public interface Ionizable { - + /** * Load data from the input stream. Mark has already been read, begin * reading right after it. * - * @param in input stream + * @param in + * input stream * @throws IonException */ public void ionRead(InputStream in) throws IonException; - - + + /** * Store data to output stream. mark has already been written, begin right * after it. * - * @param out Output stream + * @param out + * Output stream * @throws IonException */ public void ionWrite(OutputStream out) throws IonException; - - + + /** * Get Ion mark byte. * diff --git a/src/mightypork/utils/files/ion/IonizableOptional.java b/src/mightypork/utils/files/ion/IonizableOptional.java index 35f9092..9878c1f 100644 --- a/src/mightypork/utils/files/ion/IonizableOptional.java +++ b/src/mightypork/utils/files/ion/IonizableOptional.java @@ -1,13 +1,12 @@ package mightypork.utils.files.ion; - /** * Optional ionizable * * @author MightyPork */ public interface IonizableOptional extends Ionizable { - + /** * Get if this ionizable should be saved to a list * diff --git a/src/mightypork/utils/files/ion/StreamUtils.java b/src/mightypork/utils/files/ion/StreamUtils.java index e5e68fc..68abb0f 100644 --- a/src/mightypork/utils/files/ion/StreamUtils.java +++ b/src/mightypork/utils/files/ion/StreamUtils.java @@ -1,6 +1,5 @@ package mightypork.utils.files.ion; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -13,99 +12,99 @@ import java.nio.ByteBuffer; * @author MightyPork */ public class StreamUtils { - + private static ByteBuffer bi = ByteBuffer.allocate(Integer.SIZE / 8); private static ByteBuffer bd = ByteBuffer.allocate(Double.SIZE / 8); private static ByteBuffer bf = ByteBuffer.allocate(Float.SIZE / 8); private static ByteBuffer bc = ByteBuffer.allocate(Character.SIZE / 8); private static ByteBuffer bl = ByteBuffer.allocate(Long.SIZE / 8); private static ByteBuffer bs = ByteBuffer.allocate(Short.SIZE / 8); - + private static byte[] ai = new byte[Integer.SIZE / 8]; private static byte[] ad = new byte[Double.SIZE / 8]; private static byte[] af = new byte[Float.SIZE / 8]; private static byte[] ac = new byte[Character.SIZE / 8]; private static byte[] al = new byte[Long.SIZE / 8]; private static byte[] as = new byte[Short.SIZE / 8]; - - + + // CONVERSIONS - + private static byte[] convBool(boolean bool) { return new byte[] { (byte) (bool ? 1 : 0) }; } - - + + private static byte[] convByte(byte num) { return new byte[] { num }; } - - + + private static byte[] convChar(char num) { bc.clear(); bc.putChar(num); return bc.array(); } - - + + private static byte[] convShort(short num) { bs.clear(); bs.putShort(num); return bs.array(); } - - + + private static byte[] convInt(int num) { bi.clear(); bi.putInt(num); return bi.array(); } - - + + private static byte[] convLong(long num) { bl.clear(); bl.putLong(num); return bl.array(); } - - + + private static byte[] convFloat(float num) { bf.clear(); bf.putFloat(num); return bf.array(); } - - + + private static byte[] convDouble(double num) { bd.clear(); bd.putDouble(num); return bd.array(); } - - + + private static byte[] convString(String str) { char[] chars = str.toCharArray(); - + ByteBuffer bstr = ByteBuffer.allocate((Character.SIZE / 8) * chars.length + (Character.SIZE / 8)); for (char c : chars) { bstr.putChar(c); } - + bstr.putChar((char) 0); - + return bstr.array(); } - - + + private static byte[] convString_b(String str) { char[] chars = str.toCharArray(); @@ -114,133 +113,133 @@ public class StreamUtils { bstr.put((byte) c); } bstr.put((byte) 0); - + return bstr.array(); } - - + + public static void writeBoolean(OutputStream out, boolean num) throws IOException { out.write(convBool(num)); } - - + + public static void writeByte(OutputStream out, byte num) throws IOException { out.write(convByte(num)); } - - + + public static void writeChar(OutputStream out, char num) throws IOException { out.write(convChar(num)); } - - + + public static void writeShort(OutputStream out, short num) throws IOException { out.write(convShort(num)); } - - + + public static void writeInt(OutputStream out, int num) throws IOException { out.write(convInt(num)); } - - + + public static void writeLong(OutputStream out, long num) throws IOException { out.write(convLong(num)); } - - + + public static void writeFloat(OutputStream out, float num) throws IOException { out.write(convFloat(num)); } - - + + public static void writeDouble(OutputStream out, double num) throws IOException { out.write(convDouble(num)); } - - + + public static void writeString(OutputStream out, String str) throws IOException { out.write(convString(str)); } - - + + public static void writeStringBytes(OutputStream out, String str) throws IOException { out.write(convString_b(str)); } - - + + // READING - + public static boolean readBoolean(InputStream in) throws IOException { return in.read() > 0; } - - + + public static byte readByte(InputStream in) throws IOException { return (byte) in.read(); } - - + + public static char readChar(InputStream in) throws IOException { in.read(ac, 0, ac.length); ByteBuffer buf = ByteBuffer.wrap(ac); return buf.getChar(); } - - + + public static short readShort(InputStream in) throws IOException { in.read(as, 0, as.length); ByteBuffer buf = ByteBuffer.wrap(as); return buf.getShort(); } - - + + public static long readLong(InputStream in) throws IOException { in.read(al, 0, al.length); ByteBuffer buf = ByteBuffer.wrap(al); return buf.getLong(); } - - + + public static int readInt(InputStream in) throws IOException { in.read(ai, 0, ai.length); ByteBuffer buf = ByteBuffer.wrap(ai); return buf.getInt(); } - - + + public static float readFloat(InputStream in) throws IOException { in.read(af, 0, af.length); ByteBuffer buf = ByteBuffer.wrap(af); return buf.getFloat(); } - - + + public static double readDouble(InputStream in) throws IOException { in.read(ad, 0, ad.length); ByteBuffer buf = ByteBuffer.wrap(ad); return buf.getDouble(); } - - + + public static String readString(InputStream in) throws IOException { String s = ""; @@ -250,8 +249,8 @@ public class StreamUtils { } return s; } - - + + public static String readStringBytes(InputStream in) throws IOException { String s = ""; @@ -261,5 +260,5 @@ public class StreamUtils { } return s; } - + } diff --git a/src/mightypork/utils/logging/Log.java b/src/mightypork/utils/logging/Log.java index b8ae96c..a69c5e8 100644 --- a/src/mightypork/utils/logging/Log.java +++ b/src/mightypork/utils/logging/Log.java @@ -1,133 +1,144 @@ package mightypork.utils.logging; - import java.io.File; import java.util.HashMap; public class Log { - + /** enable static logging */ private static boolean esl = true; - - + + /** * Log FINE message in main logger * - * @param msg message + * @param msg + * message */ public static void f1(String msg) { if (esl && main != null) main.f1(msg); } - - + + /** * Log FINER message in main logger * - * @param msg message + * @param msg + * message */ public static void f2(String msg) { if (esl && main != null) main.f2(msg); } - - + + /** * Log FINEST message in main logger * - * @param msg message + * @param msg + * message */ public static void f3(String msg) { if (esl && main != null) main.f3(msg); } - - + + /** * Log INFO message in main logger * - * @param msg message + * @param msg + * message */ public static void i(String msg) { if (esl && main != null) main.i(msg); } - - + + /** * Log WARNING message in main logger * - * @param msg message + * @param msg + * message */ public static void w(String msg) { if (esl && main != null) main.w(msg); } - - + + /** * Log ERROR message in main logger * - * @param msg message + * @param msg + * message */ public static void e(String msg) { if (esl && main != null) main.e(msg); } - - + + /** * Log EXCEPTION and ERROR message in main logger * - * @param msg message - * @param thrown thrown exception + * @param msg + * message + * @param thrown + * thrown exception */ public static void e(String msg, Throwable thrown) { if (esl && main != null) main.e(msg, thrown); } - - + + /** * Log EXCEPTION in main logger * - * @param thrown thrown exception + * @param thrown + * thrown exception */ public static void e(Throwable thrown) { if (esl && main != null) main.e(thrown); } - - + + public static void enable(boolean flag) { if (esl && main != null) main.enable(flag); } - - + + /** * Enable / disable static log delegate methods * - * @param flag enable + * @param flag + * enable */ public static void enableStaticLogging(boolean flag) { esl = flag; } - + private static HashMap logs = new HashMap(); private static LogInstance main = null; - - + + /** * Create a logger. If this is the first logger made, then it'll be made * available via the static methods. * - * @param logName log name (used for filename, must be application-unique) - * @param logsDir directory to store logs in - * @param oldLogsCount number of old logs to keep, -1 for infinite, 0 for - * none. + * @param logName + * log name (used for filename, must be application-unique) + * @param logsDir + * directory to store logs in + * @param oldLogsCount + * number of old logs to keep, -1 for infinite, 0 for none. * @return the created Log instance */ public static synchronized LogInstance create(String logName, File logsDir, int oldLogsCount) @@ -136,18 +147,20 @@ public class Log { LogInstance log = new LogInstance(logName, logsDir, oldLogsCount); if (main == null) main = log; logs.put(logName, log); - + return log; } - - + + /** * Create a logger. If this is the first logger made, then it'll be made * available via the static methods.
* Old logs will be kept. * - * @param logName log name (used for filename, must be application-unique) - * @param logsDir directory to store logs in + * @param logName + * log name (used for filename, must be application-unique) + * @param logsDir + * directory to store logs in * @return the created Log instance */ public static synchronized LogInstance create(String logName, File logsDir) @@ -156,24 +169,24 @@ public class Log { LogInstance log = new LogInstance(logName, logsDir, -1); if (main == null) main = log; logs.put(logName, log); - + return log; } - - + + public int addMonitor(LogMonitor mon) { if (main == null) throw new IllegalStateException("Main logger not initialized."); - + return main.addMonitor(mon); } - - + + public void removeMonitor(int id) { if (main == null) throw new IllegalStateException("Main logger not initialized."); - + main.removeMonitor(id); } - + } diff --git a/src/mightypork/utils/logging/LogInstance.java b/src/mightypork/utils/logging/LogInstance.java index e874424..27c44f3 100644 --- a/src/mightypork/utils/logging/LogInstance.java +++ b/src/mightypork/utils/logging/LogInstance.java @@ -1,6 +1,5 @@ package mightypork.utils.logging; - import java.io.File; import java.io.FileFilter; import java.io.PrintWriter; @@ -27,131 +26,132 @@ import mightypork.utils.files.FileUtils; * @copy (c) 2014 */ public class LogInstance { - + /** log file */ private File file; - + /** Log name */ private String name; - + /** Number of old logs to keep */ private int logs_to_keep; - + /** Logs dir */ private File dir; - + /** Logger instance. */ private Logger logger; - + /** Logging enabled */ private boolean enabled = true; - + private boolean sysout = true; - + private int monitorId = 0; private HashMap monitors = new HashMap(); - + private LogToSysoutMonitor sysoutMonitor; - - + + public LogInstance(String name, File dir, int oldLogCount) { this.name = name; this.file = new File(dir, name + getSuffix()); this.dir = dir; this.logs_to_keep = oldLogCount; - + init(); } - - + + /** * Prepare logs for logging */ private void init() { logger = Logger.getLogger(name); - + cleanup(); - + FileHandler handler = null; - + try { handler = new FileHandler(file.getPath()); } catch (Exception e) { throw new RuntimeException("Failed to init log", e); } - + handler.setFormatter(new LogFormatter()); logger.addHandler(handler); - + enabled = true; - + sysoutMonitor = new LogToSysoutMonitor(); - + addMonitor(sysoutMonitor); - + logger.setUseParentHandlers(false); logger.setLevel(Level.ALL); logger.info("Main logger initialized."); logger.info((new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date())); } - - + + private void cleanup() { if (logs_to_keep == 0) return; // overwrite - + for (File f : FileUtils.listDirectory(file.getParentFile())) { if (!f.isFile()) continue; if (f.equals(file)) { Date d = new Date(f.lastModified()); - + String fbase = name + '_' + (new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")).format(d); String suff = getSuffix(); String cntStr = ""; File f2; - + for (int cnt = 0; (f2 = new File(dir, fbase + cntStr + suff)).exists(); cntStr = "_" + (++cnt)); - + f.renameTo(f2); } } - + if (logs_to_keep == -1) return; // keep all - + List oldLogs = FileUtils.listDirectory(dir, new FileFilter() { - + @Override public boolean accept(File f) { if (f.isDirectory()) return false; if (!f.getName().endsWith(getSuffix())) return false; if (!f.getName().startsWith(name)) return false; - + return true; } - + }); - + Collections.sort(oldLogs, new Comparator() { - + @Override public int compare(File o1, File o2) { return o1.getName().compareTo(o2.getName()); } }); - + for (int i = 0; i < oldLogs.size() - logs_to_keep; i++) { oldLogs.get(i).delete(); } } - - + + /** * Add log monitor * - * @param mon monitor + * @param mon + * monitor * @return assigned ID */ public synchronized int addMonitor(LogMonitor mon) @@ -161,131 +161,143 @@ public class LogInstance { monitors.put(id, mon); return id; } - - + + /** * Remove a monitor by ID * - * @param id monitor ID + * @param id + * monitor ID */ public synchronized void removeMonitor(int id) { monitors.remove(id); } - - + + /** * Enable logging. * - * @param flag do enable logging + * @param flag + * do enable logging */ public void enable(boolean flag) { enabled = flag; } - - + + /** * Enable printing logs to sysout * - * @param flag do enable logging + * @param flag + * do enable logging */ public void enableSysout(boolean flag) { sysout = flag; sysoutMonitor.enable(sysout); } - - + + /** * Log FINE message * - * @param msg message + * @param msg + * message */ public void f1(String msg) { if (enabled) logger.log(Level.FINE, msg); } - - + + /** * Log FINER message * - * @param msg message + * @param msg + * message */ public void f2(String msg) { if (enabled) logger.log(Level.FINER, msg); } - - + + /** * Log FINEST message * - * @param msg message + * @param msg + * message */ public void f3(String msg) { if (enabled) logger.log(Level.FINEST, msg); } - - + + /** * Log INFO message * - * @param msg message + * @param msg + * message */ public void i(String msg) { if (enabled) logger.log(Level.INFO, msg); } - - + + /** * Log WARNING message (less severe than ERROR) * - * @param msg message + * @param msg + * message */ public void w(String msg) { if (enabled) logger.log(Level.WARNING, msg); } - - + + /** * Log ERROR message * - * @param msg message + * @param msg + * message */ public void e(String msg) { if (enabled) logger.log(Level.SEVERE, msg); } - - + + /** * Log THROWING message * - * @param msg message - * @param thrown thrown exception + * @param msg + * message + * @param thrown + * thrown exception */ public void e(String msg, Throwable thrown) { if (enabled) logger.log(Level.SEVERE, msg + "\n" + getStackTrace(thrown)); } - - + + /** * Log exception thrown * - * @param thrown thrown exception + * @param thrown + * thrown exception */ public void e(Throwable thrown) { if (enabled) logger.log(Level.SEVERE, getStackTrace(thrown)); } - - + + /** * Get stack trace from throwable * @@ -301,7 +313,8 @@ public class LogInstance { sw.flush(); return sw.toString(); } - + + /** * PowerCraft Log file formatter. * @@ -309,25 +322,25 @@ public class LogInstance { * @copy (c) 2012 */ private class LogFormatter extends Formatter { - + /** Newline string constant */ private final String nl = System.getProperty("line.separator"); - - + + @Override public String format(LogRecord record) { StringBuffer buf = new StringBuffer(180); - + if (record.getMessage().equals("\n")) { return nl; } - + if (record.getMessage().charAt(0) == '\n') { buf.append(nl); record.setMessage(record.getMessage().substring(1)); } - + Level level = record.getLevel(); String trail = "[ ? ]"; if (level == Level.FINE) { @@ -348,14 +361,14 @@ public class LogInstance { if (level == Level.WARNING) { trail = "[!W!] "; } - + record.setMessage(record.getMessage().replaceAll("\n", nl + trail)); - + buf.append(trail); buf.append(formatMessage(record)); - + buf.append(nl); - + Throwable throwable = record.getThrown(); if (throwable != null) { buf.append("at "); @@ -363,25 +376,25 @@ public class LogInstance { buf.append('.'); buf.append(record.getSourceMethodName()); buf.append(nl); - + StringWriter sink = new StringWriter(); throwable.printStackTrace(new PrintWriter(sink, true)); buf.append(sink.toString()); - + buf.append(nl); } - + String str = buf.toString(); - + for (LogMonitor mon : monitors.values()) { mon.log(level, str); } - + return str; } } - - + + /** * @return log filename suffix (incl. dot) */ diff --git a/src/mightypork/utils/logging/LogMonitor.java b/src/mightypork/utils/logging/LogMonitor.java index b52da11..f629e62 100644 --- a/src/mightypork/utils/logging/LogMonitor.java +++ b/src/mightypork/utils/logging/LogMonitor.java @@ -1,13 +1,12 @@ package mightypork.utils.logging; - import java.util.logging.Level; public interface LogMonitor { - + public void log(Level level, String message); - - + + public void enable(boolean enable); } diff --git a/src/mightypork/utils/logging/LogToSysoutMonitor.java b/src/mightypork/utils/logging/LogToSysoutMonitor.java index 3f2e2c7..06ce5e8 100644 --- a/src/mightypork/utils/logging/LogToSysoutMonitor.java +++ b/src/mightypork/utils/logging/LogToSysoutMonitor.java @@ -1,31 +1,30 @@ package mightypork.utils.logging; - import java.util.logging.Level; public class LogToSysoutMonitor implements LogMonitor { - + private boolean enabled = true; - - + + @Override public void log(Level level, String message) { if (!enabled) return; - + if (level == Level.FINE || level == Level.FINER || level == Level.FINEST || level == Level.INFO) { System.out.print(message); } else if (level == Level.SEVERE || level == Level.WARNING) { System.err.print(message); } } - - + + @Override public void enable(boolean enable) { this.enabled = enable; } - + } diff --git a/src/mightypork/utils/math/Calc.java b/src/mightypork/utils/math/Calc.java index bafe366..c9836fa 100644 --- a/src/mightypork/utils/math/Calc.java +++ b/src/mightypork/utils/math/Calc.java @@ -1,6 +1,5 @@ package mightypork.utils.math; - import java.nio.FloatBuffer; import java.util.ArrayList; import java.util.List; @@ -18,17 +17,20 @@ import org.lwjgl.BufferUtils; * @author MightyPork */ public class Calc { - + /** Square root of two */ public static final double SQ2 = 1.41421356237; - - + + /** * Get distance from 2D line to 2D point [X,Y] * - * @param lineDirVec line directional vector - * @param linePoint point of line - * @param point point coordinate + * @param lineDirVec + * line directional vector + * @param linePoint + * point of line + * @param point + * point coordinate * @return distance */ public static double linePointDist(Coord lineDirVec, Coord linePoint, Coord point) @@ -36,86 +38,96 @@ public class Calc { // line point L[lx,ly] double lx = linePoint.x; double ly = linePoint.y; - + // line equation ax+by+c=0 double a = -lineDirVec.y; double b = lineDirVec.x; double c = -a * lx - b * ly; - + // checked point P[x,y] double x = point.x; double y = point.y; - + // distance return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b); } - - + + /** * Get distance from 2D line to 2D point [X,Z] * - * @param lineDirVec line directional vector - * @param linePoint point of line - * @param point point coordinate + * @param lineDirVec + * line directional vector + * @param linePoint + * point of line + * @param point + * point coordinate * @return distance */ public static double linePointDistXZ(Coord lineDirVec, Coord linePoint, Coord point) { return linePointDist(new Coord(lineDirVec.x, lineDirVec.z), new Coord(linePoint.x, linePoint.z), new Coord(point.x, point.z)); } - - + + /** * Get longest side of a right-angled triangle * - * @param a side a (opposite) - * @param b side b (adjacent) + * @param a + * side a (opposite) + * @param b + * side b (adjacent) * @return longest side (hypotenuse) */ public static double pythC(double a, double b) { return Math.sqrt(square(a) + square(b)); } - - + + /** * Get adjacent side of a right-angled triangle * - * @param a side a (opposite) - * @param c side c (hypotenuse) + * @param a + * side a (opposite) + * @param c + * side c (hypotenuse) * @return side b (adjacent) */ public static double pythB(double a, double c) { return Math.sqrt(square(c) - square(a)); } - - + + /** * Get opposite side of a right-angled triangle * - * @param b side b (adjacent) - * @param c side c (hypotenuse) + * @param b + * side b (adjacent) + * @param c + * side c (hypotenuse) * @return side a (opposite) */ public static double pythA(double b, double c) { return Math.sqrt(square(c) - square(b)); } - + + private static class Angles { - + public static double delta(double alpha, double beta, double a360) { while (Math.abs(alpha - beta) > a360 / 2D) { alpha = norm(alpha + a360 / 2D, a360); beta = norm(beta + a360 / 2D, a360); } - + return beta - alpha; } - - + + public static double norm(double angle, double a360) { while (angle < 0) @@ -125,26 +137,28 @@ public class Calc { return angle; } } - + + /** * Calc subclass with buffer utils. * * @author MightyPork */ public static class Buffers { - + /** * Create java.nio.FloatBuffer of given floats, and flip it. * - * @param obj floats or float array + * @param obj + * floats or float array * @return float buffer */ public static FloatBuffer mkFillBuff(float... obj) { return (FloatBuffer) BufferUtils.createFloatBuffer(obj.length).put(obj).flip(); } - - + + /** * Fill java.nio.FloatBuffer with floats or float array * @@ -156,28 +170,30 @@ public class Calc { buff.put(obj); buff.flip(); } - - + + /** * Create new java.nio.FloatBuffer of given length * - * @param count elements + * @param count + * elements * @return the new java.nio.FloatBuffer */ public static FloatBuffer alloc(int count) { return BufferUtils.createFloatBuffer(count); } - + } - + + /** * Angle calculations for degrees. * * @author MightyPork */ public static class Deg { - + /** 180° in degrees */ public static final double a180 = 180; /** 270° in degrees */ @@ -188,99 +204,110 @@ public class Calc { public static final double a45 = 45; /** 90° in degrees */ public static final double a90 = 90; - - + + /** * Subtract two angles alpha - beta * - * @param alpha first angle - * @param beta second angle + * @param alpha + * first angle + * @param beta + * second angle * @return (alpha - beta) in degrees */ public static double delta(double alpha, double beta) { return Angles.delta(alpha, beta, a360); } - - + + /** * Difference of two angles (absolute value of delta) * - * @param alpha first angle - * @param beta second angle + * @param alpha + * first angle + * @param beta + * second angle * @return difference in radians */ public static double diff(double alpha, double beta) { return Math.abs(Angles.delta(alpha, beta, a360)); } - - + + /** * Cosinus in degrees * - * @param deg angle in degrees + * @param deg + * angle in degrees * @return cosinus */ public static double cos(double deg) { return Math.cos(toRad(deg)); } - - + + /** * Sinus in degrees * - * @param deg angle in degrees + * @param deg + * angle in degrees * @return sinus */ public static double sin(double deg) { return Math.sin(toRad(deg)); } - - + + /** * Tangents in degrees * - * @param deg angle in degrees + * @param deg + * angle in degrees * @return tangents */ public static double tan(double deg) { return Math.tan(toRad(deg)); } - - + + /** * Angle normalized to 0-360 range * - * @param angle angle to normalize + * @param angle + * angle to normalize * @return normalized angle */ public static double norm(double angle) { return Angles.norm(angle, a360); } - - + + /** * Convert to radians * - * @param deg degrees + * @param deg + * degrees * @return radians */ public static double toRad(double deg) { return Math.toRadians(deg); } - - + + /** * Round angle to 0,45,90,135... * - * @param deg angle in deg. to round - * @param x rounding increment (45 - round to 0,45,90...) + * @param deg + * angle in deg. to round + * @param x + * rounding increment (45 - round to 0,45,90...) * @return rounded */ public static int roundX(double deg, double x) @@ -293,36 +320,39 @@ public class Calc { if (a == 360) a = 0; return (int) Math.round(a); } - - + + /** * Round angle to 0,45,90,135... * - * @param deg angle in deg. to round + * @param deg + * angle in deg. to round * @return rounded */ public static int round45(double deg) { return roundX(deg, 45); } - - + + /** * Round angle to 0,90,180,270 * - * @param deg angle in deg. to round + * @param deg + * angle in deg. to round * @return rounded */ public static int round90(double deg) { return roundX(deg, 90); } - - + + /** * Round angle to 0,15,30,45,60,75,90... * - * @param deg angle in deg to round + * @param deg + * angle in deg to round * @return rounded */ public static int round15(double deg) @@ -330,14 +360,15 @@ public class Calc { return roundX(deg, 15); } } - + + /** * Angle calculations for radians. * * @author MightyPork */ public static class Rad { - + /** 180° in radians */ public static final double a180 = Math.PI; /** 270° in radians */ @@ -348,86 +379,95 @@ public class Calc { public static final double a45 = Math.PI / 4D; /** 90° in radians */ public static final double a90 = Math.PI / 2D; - - + + /** * Subtract two angles alpha - beta * - * @param alpha first angle - * @param beta second angle + * @param alpha + * first angle + * @param beta + * second angle * @return (alpha - beta) in radians */ public static double delta(double alpha, double beta) { return Angles.delta(alpha, beta, a360); } - - + + /** * Difference of two angles (absolute value of delta) * - * @param alpha first angle - * @param beta second angle + * @param alpha + * first angle + * @param beta + * second angle * @return difference in radians */ public static double diff(double alpha, double beta) { return Math.abs(Angles.delta(alpha, beta, a360)); } - - + + /** * Cos * - * @param rad angle in rads + * @param rad + * angle in rads * @return cos */ public static double cos(double rad) { return Math.cos(rad); } - - + + /** * Sin * - * @param rad angle in rads + * @param rad + * angle in rads * @return sin */ public static double sin(double rad) { return Math.sin(rad); } - - + + /** * Tan * - * @param rad angle in rads + * @param rad + * angle in rads * @return tan */ public static double tan(double rad) { return Math.tan(rad); } - - + + /** * Angle normalized to 0-2*PI range * - * @param angle angle to normalize + * @param angle + * angle to normalize * @return normalized angle */ public static double norm(double angle) { return Angles.norm(angle, a360); } - - + + /** * Convert to degrees * - * @param rad radians + * @param rad + * radians * @return degrees */ public static double toDeg(double rad) @@ -435,58 +475,62 @@ public class Calc { return Math.toDegrees(rad); } } - + private static Random rand = new Random(); - - + + /** * Get volume of a sphere * - * @param radius sphere radius + * @param radius + * sphere radius * @return volume in cubic units */ public static double sphereGetVolume(double radius) { return (4D / 3D) * Math.PI * cube(radius); } - - + + /** * Get radius of a sphere * - * @param volume sphere volume + * @param volume + * sphere volume * @return radius in units */ public static double sphereGetRadius(double volume) { return Math.cbrt((3D * volume) / (4 * Math.PI)); } - - + + /** * Get surface of a circle * - * @param radius circle radius + * @param radius + * circle radius * @return volume in square units */ public static double circleGetSurface(double radius) { return Math.PI * square(radius); } - - + + /** * Get radius of a circle * - * @param surface circle volume + * @param surface + * circle volume * @return radius in units */ public static double circleGetRadius(double surface) { return Math.sqrt(surface / Math.PI); } - - + + /** * Check if objects are equal (for equals function) * @@ -498,14 +542,17 @@ public class Calc { { return a == null ? b == null : a.equals(b); } - - + + /** * Private clamping helper. * - * @param number number to be clamped - * @param min min value - * @param max max value + * @param number + * number to be clamped + * @param min + * min value + * @param max + * max value * @return clamped double */ private static double clamp_double(Number number, Number min, Number max) @@ -518,13 +565,15 @@ public class Calc { if (Double.isNaN(n)) return mind; return n; } - - + + /** * Private clamping helper. * - * @param number number to be clamped - * @param min min value + * @param number + * number to be clamped + * @param min + * min value * @return clamped double */ private static double clamp_double(Number number, Number min) @@ -534,141 +583,163 @@ public class Calc { if (n < mind) n = mind; return n; } - - + + /** * Clamp number to min and max bounds, inclusive.
* DOUBLE version * - * @param number clamped number - * @param min minimal allowed value - * @param max maximal allowed value + * @param number + * clamped number + * @param min + * minimal allowed value + * @param max + * maximal allowed value * @return result */ public static double clampd(Number number, Number min, Number max) { return clamp_double(number, min, max); } - - + + /** * Clamp number to min and max bounds, inclusive.
* FLOAT version * - * @param number clamped number - * @param min minimal allowed value - * @param max maximal allowed value + * @param number + * clamped number + * @param min + * minimal allowed value + * @param max + * maximal allowed value * @return result */ public static float clampf(Number number, Number min, Number max) { return (float) clamp_double(number, min, max); } - - + + /** * Clamp number to min and max bounds, inclusive.
* INTEGER version * - * @param number clamped number - * @param min minimal allowed value - * @param max maximal allowed value + * @param number + * clamped number + * @param min + * minimal allowed value + * @param max + * maximal allowed value * @return result */ public static int clampi(Number number, Number min, Number max) { return (int) Math.round(clamp_double(number, min, max)); } - - + + /** * Clamp number to min and max bounds, inclusive.
* INTEGER version * - * @param number clamped number - * @param range range + * @param number + * clamped number + * @param range + * range * @return result */ public static int clampi(Number number, Range range) { return (int) Math.round(clamp_double(number, range.getMin(), range.getMax())); } - - + + /** * Clamp number to min and max bounds, inclusive.
* DOUBLE version * - * @param number clamped number - * @param range range + * @param number + * clamped number + * @param range + * range * @return result */ public static double clampd(Number number, Range range) { return clamp_double(number, range.getMin(), range.getMax()); } - - + + /** * Clamp number to min and max bounds, inclusive.
* FLOAT version * - * @param number clamped number - * @param range range + * @param number + * clamped number + * @param range + * range * @return result */ public static float clampf(Number number, Range range) { return (float) clamp_double(number, range.getMin(), range.getMax()); } - - + + /** * Clamp number to min and infinite bounds, inclusive.
* DOUBLE version * - * @param number clamped number - * @param min minimal allowed value + * @param number + * clamped number + * @param min + * minimal allowed value * @return result */ public static double clampd(Number number, Number min) { return clamp_double(number, min); } - - + + /** * Clamp number to min and infinite bounds, inclusive.
* FLOAT version * - * @param number clamped number - * @param min minimal allowed value + * @param number + * clamped number + * @param min + * minimal allowed value * @return result */ public static float clampf(Number number, Number min) { return (float) clamp_double(number, min); } - - + + /** * Clamp number to min and infinite bounds, inclusive.
* INTEGER version * - * @param number clamped number - * @param min minimal allowed value + * @param number + * clamped number + * @param min + * minimal allowed value * @return result */ public static int clampi(Number number, Number min) { return (int) Math.round(clamp_double(number, min)); } - - + + /** * Get class simple name * - * @param obj object + * @param obj + * object * @return simple name */ public static String cname(Object obj) @@ -676,24 +747,26 @@ public class Calc { if (obj == null) return "NULL"; return obj.getClass().getSimpleName(); } - - + + /** * Cube a double * - * @param a squared double + * @param a + * squared double * @return square */ public static double cube(double a) { return a * a * a; } - - + + /** * Convert double to string, remove the mess at the end. * - * @param d double + * @param d + * double * @return string */ public static String doubleToString(double d) @@ -704,12 +777,13 @@ public class Calc { s = s.replaceAll("\\.$", ""); return s; } - - + + /** * Convert float to string, remove the mess at the end. * - * @param f float + * @param f + * float * @return string */ public static String floatToString(float f) @@ -720,71 +794,87 @@ public class Calc { s = s.replaceAll("\\.$", ""); return s; } - - + + /** * Check if number is in range * - * @param number checked - * @param left lower end - * @param right upper end + * @param number + * checked + * @param left + * lower end + * @param right + * upper end * @return is in range */ public static boolean inRange(double number, double left, double right) { return number >= left && number <= right; } - - + + /** * Get number from A to B at delta time (tween A to B) * - * @param from last number - * @param to new number - * @param time time 0..1 - * @param easing easing function + * @param from + * last number + * @param to + * new number + * @param time + * time 0..1 + * @param easing + * easing function * @return current number to render */ public static double interpolate(double from, double to, double time, Easing easing) { return from + (to - from) * easing.get(time); } - - + + /** * Get angle [degrees] from A to B at delta time (tween A to B) * - * @param from last angle - * @param to new angle - * @param time time 0..1 - * @param easing easing function + * @param from + * last angle + * @param to + * new angle + * @param time + * time 0..1 + * @param easing + * easing function * @return current angle to render */ public static double interpolateDeg(double from, double to, double time, Easing easing) { return Deg.norm(from - Deg.delta(to, from) * easing.get(time)); } - - + + /** * Get angle [radians] from A to B at delta time (tween A to B) * - * @param from last angle - * @param to new angle - * @param time time 0..1 - * @param easing easing function + * @param from + * last angle + * @param to + * new angle + * @param time + * time 0..1 + * @param easing + * easing function * @return current angle to render */ public static double interpolateRad(double from, double to, double time, Easing easing) { return Rad.norm(from - Rad.delta(to, from) * easing.get(time)); } - - + + /** * Get highest number of a list * - * @param numbers numbers + * @param numbers + * numbers * @return lowest */ public static double max(double... numbers) @@ -795,12 +885,13 @@ public class Calc { } return highest; } - - + + /** * Get highest number of a list * - * @param numbers numbers + * @param numbers + * numbers * @return lowest */ public static float max(float... numbers) @@ -811,12 +902,13 @@ public class Calc { } return highest; } - - + + /** * Get highest number of a list * - * @param numbers numbers + * @param numbers + * numbers * @return lowest */ public static int max(int... numbers) @@ -827,12 +919,13 @@ public class Calc { } return highest; } - - + + /** * Get lowest number of a list * - * @param numbers numbers + * @param numbers + * numbers * @return lowest */ public static double min(double... numbers) @@ -843,12 +936,13 @@ public class Calc { } return lowest; } - - + + /** * Get lowest number of a list * - * @param numbers numbers + * @param numbers + * numbers * @return lowest */ public static float min(float... numbers) @@ -859,12 +953,13 @@ public class Calc { } return lowest; } - - + + /** * Get lowest number of a list * - * @param numbers numbers + * @param numbers + * numbers * @return lowest */ public static int min(int... numbers) @@ -875,12 +970,13 @@ public class Calc { } return lowest; } - - + + /** * Split comma separated list of integers. * - * @param list String containing the list. + * @param list + * String containing the list. * @return array of integers or null. */ public static List parseIntList(String list) @@ -889,24 +985,26 @@ public class Calc { return null; } String[] parts = list.split(","); - + ArrayList intList = new ArrayList(); - + for (String part : parts) { try { intList.add(Integer.parseInt(part)); - } catch (NumberFormatException e) {} + } catch (NumberFormatException e) { + } } - + return intList; - + } - - + + /** * Pick random element from a given list. * - * @param list list of choices + * @param list + * list of choices * @return picked element */ public static Object pick(List list) @@ -914,20 +1012,21 @@ public class Calc { if (list.size() == 0) return null; return list.get(rand.nextInt(list.size())); } - - + + /** * Square a double * - * @param a squared double + * @param a + * squared double * @return square */ public static double square(double a) { return a * a; } - - + + /** * Signum. * @@ -938,11 +1037,11 @@ public class Calc { { return number > 0 ? 1 : number < 0 ? -1 : 0; } - - + + public static double frag(double d) { return d - Math.floor(d); } - + } diff --git a/src/mightypork/utils/math/Polar.java b/src/mightypork/utils/math/Polar.java index 65f307c..5f5424c 100644 --- a/src/mightypork/utils/math/Polar.java +++ b/src/mightypork/utils/math/Polar.java @@ -1,6 +1,5 @@ package mightypork.utils.math; - import mightypork.utils.math.Calc.Deg; import mightypork.utils.math.Calc.Rad; import mightypork.utils.math.coord.Coord; @@ -12,38 +11,43 @@ import mightypork.utils.math.coord.Coord; * @author MightyPork */ public class Polar { - + /** angle in radians */ private double angle = 0; - + /** distance in units */ private double radius = 0; - - + + /** * Create a polar * - * @param angle angle in RAD - * @param distance distance from origin + * @param angle + * angle in RAD + * @param distance + * distance from origin */ public Polar(double angle, double distance) { this(angle, false, distance); } - - + + /** * Create a polar * - * @param angle angle - * @param deg angle is in DEG - * @param distance radius + * @param angle + * angle + * @param deg + * angle is in DEG + * @param distance + * radius */ public Polar(double angle, boolean deg, double distance) { this.radius = distance; this.angle = deg ? Deg.toRad(angle) : angle; } - - + + /** * @return angle in RAD */ @@ -51,8 +55,8 @@ public class Polar { { return angle; } - - + + /** * @return angle in DEG */ @@ -60,26 +64,28 @@ public class Polar { { return Rad.toDeg(angle); } - - + + /** - * @param angle angle in RAD + * @param angle + * angle in RAD */ public void setAngle(double angle) { this.angle = angle; } - - + + /** - * @param angle angle in DEG + * @param angle + * angle in DEG */ public void setAngleDeg(double angle) { this.angle = Deg.toRad(angle); } - - + + /** * @return radius */ @@ -87,42 +93,46 @@ public class Polar { { return radius; } - - + + /** - * @param r radius + * @param r + * radius */ public void setRadius(double r) { this.radius = r; } - - + + /** * Make polar from coord * - * @param coord coord + * @param coord + * coord * @return polar */ public static Polar fromCoord(Coord coord) { return new Polar(Math.atan2(coord.y, coord.x), Math.sqrt(Calc.square(coord.x) + Calc.square(coord.y))); } - - + + /** * Make polar from coords * - * @param x x coord - * @param y y coord + * @param x + * x coord + * @param y + * y coord * @return polar */ public static Polar fromCoord(double x, double y) { return Polar.fromCoord(new Coord(x, y)); } - - + + /** * Get coord from polar * @@ -132,15 +142,15 @@ public class Polar { { return new Coord(radius * Math.cos(angle), radius * Math.sin(angle)); } - - + + @Override public String toString() { return "Polar(" + angle + "rad, " + radius + ")"; } - - + + @Override public int hashCode() { @@ -153,8 +163,8 @@ public class Polar { result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } - - + + @Override public boolean equals(Object obj) { diff --git a/src/mightypork/utils/math/Range.java b/src/mightypork/utils/math/Range.java index ab241c6..836651d 100644 --- a/src/mightypork/utils/math/Range.java +++ b/src/mightypork/utils/math/Range.java @@ -1,6 +1,5 @@ package mightypork.utils.math; - import java.util.Random; @@ -10,24 +9,27 @@ import java.util.Random; * @author MightyPork */ public class Range { - + private double min = 0; private double max = 1; - + private static Random rand = new Random(); - - + + /** * Implicit range constructor 0-1 */ - public Range() {} - - + public Range() { + } + + /** * Create new range * - * @param min min number - * @param max max number + * @param min + * min number + * @param max + * max number */ public Range(double min, double max) { if (min > max) { @@ -38,19 +40,20 @@ public class Range { this.min = min; this.max = max; } - - + + /** * Create new range * - * @param minmax min = max number + * @param minmax + * min = max number */ public Range(double minmax) { this.min = minmax; this.max = minmax; } - - + + /** * Get random integer from range * @@ -60,8 +63,8 @@ public class Range { { return (int) (Math.round(min) + rand.nextInt((int) (Math.round(max) - Math.round(min)) + 1)); } - - + + /** * Get random double from this range * @@ -71,8 +74,8 @@ public class Range { { return min + rand.nextDouble() * (max - min); } - - + + /** * Get min * @@ -82,8 +85,8 @@ public class Range { { return min; } - - + + /** * Get max * @@ -93,8 +96,8 @@ public class Range { { return max; } - - + + /** * Get min * @@ -104,8 +107,8 @@ public class Range { { return (int) min; } - - + + /** * Get max * @@ -115,37 +118,39 @@ public class Range { { return (int) max; } - - + + /** * Set min * - * @param min min value + * @param min + * min value */ public void setMin(double min) { this.min = min; } - - + + /** * Set max * - * @param max max value + * @param max + * max value */ public void setMax(double max) { this.max = max; } - - + + @Override public String toString() { return "Range(" + min + ";" + max + ")"; } - - + + /** * Get identical copy * @@ -155,32 +160,35 @@ public class Range { { return new Range(min, max); } - - + + /** * Set to value of other range * - * @param other copied range + * @param other + * copied range */ public void setTo(Range other) { if (other == null) return; min = other.min; max = other.max; - + if (min > max) { double t = min; min = max; max = t; } } - - + + /** * Set to min-max values * - * @param min min value - * @param max max value + * @param min + * min value + * @param max + * max value */ public void setTo(double min, double max) { @@ -189,9 +197,9 @@ public class Range { min = max; max = t; } - + this.min = min; this.max = max; } - + } diff --git a/src/mightypork/utils/math/color/HSV.java b/src/mightypork/utils/math/color/HSV.java index feac7d0..3d7e491 100644 --- a/src/mightypork/utils/math/color/HSV.java +++ b/src/mightypork/utils/math/color/HSV.java @@ -1,6 +1,5 @@ package mightypork.utils.math.color; - import java.awt.Color; import mightypork.utils.math.Calc; @@ -12,21 +11,22 @@ import mightypork.utils.math.Calc; * @author MightyPork */ public class HSV { - + /** H */ public double h; /** S */ public double s; /** V */ public double v; - - + + /** * Create black color 0,0,0 */ - public HSV() {} - - + public HSV() { + } + + /** * Color from HSV 0-1 * @@ -40,8 +40,8 @@ public class HSV { this.v = v.doubleValue(); norm(); } - - + + /** * @return hue 0-1 */ @@ -49,8 +49,8 @@ public class HSV { { return h; } - - + + /** * @return saturation 0-1 */ @@ -58,8 +58,8 @@ public class HSV { { return s; } - - + + /** * @return value/brightness 0-1 */ @@ -67,12 +67,13 @@ public class HSV { { return v; } - - + + /** * Set color to other color * - * @param copied copied color + * @param copied + * copied color * @return this */ public HSV setTo(HSV copied) @@ -80,18 +81,21 @@ public class HSV { h = copied.h; s = copied.s; v = copied.v; - + norm(); return this; } - - + + /** * Set to H,S,V 0-1 * - * @param h hue - * @param s saturation - * @param v value + * @param h + * hue + * @param s + * saturation + * @param v + * value * @return this */ public HSV setTo(Number h, Number s, Number v) @@ -102,8 +106,8 @@ public class HSV { norm(); return this; } - - + + /** * Fix numbers out of range 0-1 */ @@ -113,8 +117,8 @@ public class HSV { s = Calc.clampd(s, 0, 1); v = Calc.clampd(v, 0, 1); } - - + + /** * Convert to RGB * @@ -123,32 +127,33 @@ public class HSV { public RGB toRGB() { norm(); - + int rgb = Color.HSBtoRGB((float) h, (float) s, (float) v); - + return RGB.fromHex(rgb); } - - + + /** * Make from RGB * - * @param color RGB + * @param color + * RGB * @return HSV */ public static HSV fromRGB(RGB color) { return color.toHSV(); } - - + + @Override public String toString() { return "HSV[" + h + ";" + s + ";" + v + "]"; } - - + + @Override public boolean equals(Object obj) { @@ -156,15 +161,15 @@ public class HSV { if (!(obj instanceof HSV)) return false; return ((HSV) obj).h == h && ((HSV) obj).s == s && ((HSV) obj).v == v; } - - + + @Override public int hashCode() { return Double.valueOf(h).hashCode() ^ Double.valueOf(s).hashCode() ^ Double.valueOf(v).hashCode(); } - - + + /** * Get a copy * diff --git a/src/mightypork/utils/math/color/RGB.java b/src/mightypork/utils/math/color/RGB.java index fabceb0..bc584fb 100644 --- a/src/mightypork/utils/math/color/RGB.java +++ b/src/mightypork/utils/math/color/RGB.java @@ -1,6 +1,5 @@ package mightypork.utils.math.color; - import java.awt.Color; import mightypork.utils.math.Calc; @@ -12,7 +11,7 @@ import mightypork.utils.math.Calc; * @author MightyPork */ public class RGB { - + /** White */ public static final RGB WHITE = new RGB(1, 1, 1); /** Black */ @@ -33,7 +32,7 @@ public class RGB { public static final RGB ORANGE = new RGB(1, 0.6, 0); /** no color (alpha=0) */ public static final RGB TRANSPARENT = new RGB(0, 0, 0, 0); - + /** R */ public double r; /** G */ @@ -42,30 +41,33 @@ public class RGB { public double b; /** ALPHA */ public double a = 1; - - + + /** * Create black color 0,0,0 */ - public RGB() {} - - + public RGB() { + } + + /** * Get copy with custom alpha * - * @param alpha alpha to set + * @param alpha + * alpha to set * @return copy w/ alpha */ public RGB setAlpha(double alpha) { return copy().setAlpha_ip(alpha); } - - + + /** * set alpha IP * - * @param alpha alpha to set + * @param alpha + * alpha to set * @return this */ public RGB setAlpha_ip(double alpha) @@ -74,8 +76,8 @@ public class RGB { norm(); return this; } - - + + /** * Get copy. * @@ -85,24 +87,26 @@ public class RGB { { return new RGB(r, g, b, a); } - - + + /** * Get copy with alpha multiplied by custom value * - * @param alpha alpha to set + * @param alpha + * alpha to set * @return copy w/ alpha */ public RGB mulAlpha(double alpha) { return copy().mulAlpha_ip(alpha); } - - + + /** * Multiply alpha by given number * - * @param alpha alpha multiplier + * @param alpha + * alpha multiplier * @return this */ public RGB mulAlpha_ip(double alpha) @@ -111,14 +115,17 @@ public class RGB { norm(); return this; } - - + + /** * Color from RGB 0-1 * - * @param r red - * @param g green - * @param b blue + * @param r + * red + * @param g + * green + * @param b + * blue */ public RGB(Number r, Number g, Number b) { this.r = r.doubleValue(); @@ -126,15 +133,19 @@ public class RGB { this.b = b.doubleValue(); norm(); } - - + + /** * Color from RGB 0-1 * - * @param r red - * @param g green - * @param b blue - * @param a alpha + * @param r + * red + * @param g + * green + * @param b + * blue + * @param a + * alpha */ public RGB(Number r, Number g, Number b, Number a) { this.r = r.doubleValue(); @@ -143,44 +154,49 @@ public class RGB { this.a = a.doubleValue(); norm(); } - - + + /** * Color from hex 0xRRGGBB * - * @param hex hex integer + * @param hex + * hex integer */ public RGB(int hex) { setTo(RGB.fromHex(hex)); norm(); } - - + + /** * Color from hex 0xRRGGBB * - * @param hex hex integer - * @param alpha alpha color + * @param hex + * hex integer + * @param alpha + * alpha color */ public RGB(int hex, double alpha) { setTo(RGB.fromHex(hex)); a = alpha; norm(); } - - + + /** * Color from other RGB and alpha channel * - * @param color other RGB color - * @param alpha new alpha channel + * @param color + * other RGB color + * @param alpha + * new alpha channel */ public RGB(RGB color, double alpha) { setTo(color); setAlpha_ip(alpha); } - - + + /** * @return red channel 0-1 */ @@ -188,8 +204,8 @@ public class RGB { { return r; } - - + + /** * @return green channel 0-1 */ @@ -197,8 +213,8 @@ public class RGB { { return g; } - - + + /** * @return blue channel 0-1 */ @@ -206,8 +222,8 @@ public class RGB { { return b; } - - + + /** * @return alpha 0-1 */ @@ -215,12 +231,13 @@ public class RGB { { return a; } - - + + /** * Set color to other color * - * @param copied copied color + * @param copied + * copied color * @return this */ public RGB setTo(RGB copied) @@ -229,16 +246,17 @@ public class RGB { g = copied.g; b = copied.b; a = copied.a; - + norm(); return this; } - - + + /** * Set to represent hex color * - * @param hex hex integer RRGGBB + * @param hex + * hex integer RRGGBB * @return this */ public RGB setTo(int hex) @@ -247,15 +265,19 @@ public class RGB { norm(); return this; } - - + + /** * Set to R,G,B 0-1 * - * @param r red - * @param g green - * @param b blue - * @param a alpha + * @param r + * red + * @param g + * green + * @param b + * blue + * @param a + * alpha * @return this */ public RGB setTo(Number r, Number g, Number b, Number a) @@ -267,14 +289,17 @@ public class RGB { norm(); return this; } - - + + /** * Set to R,G,B 0-1 * - * @param r red - * @param g green - * @param b blue + * @param r + * red + * @param g + * green + * @param b + * blue * @return this */ public RGB setTo(Number r, Number g, Number b) @@ -286,8 +311,8 @@ public class RGB { norm(); return this; } - - + + /** * Fix numbers out of range 0-1 * @@ -301,8 +326,8 @@ public class RGB { a = Calc.clampd(a, 0, 1); return this; } - - + + /** * Get hex value 0xRRGGBB * @@ -315,8 +340,8 @@ public class RGB { int bi = (int) Math.round(b * 255); return (ri << 16) | (gi << 8) | bi; } - - + + /** * Convert to HSV * @@ -328,12 +353,13 @@ public class RGB { Color.RGBtoHSB((int) (r * 255), (int) (g * 255), (int) (b * 255), hsv); return new HSV(hsv[0], hsv[1], hsv[2]); } - - + + /** * Create color from hex 0xRRGGBB * - * @param hex hex RRGGBB + * @param hex + * hex RRGGBB * @return the new color */ public static RGB fromHex(int hex) @@ -343,27 +369,28 @@ public class RGB { int ri = (hex >> 16) & 0xff; return new RGB(ri / 255D, gi / 255D, bi / 255D); } - - + + /** * Make from HSV * - * @param color HSV color + * @param color + * HSV color * @return RGB */ public static RGB fromHSV(HSV color) { return color.toRGB(); } - - + + @Override public String toString() { return "RGB[" + r + ";" + g + ";" + b + ";" + a + "]"; } - - + + @Override public boolean equals(Object obj) { @@ -371,12 +398,12 @@ public class RGB { if (!(obj instanceof RGB)) return false; return ((RGB) obj).r == r && ((RGB) obj).g == g && ((RGB) obj).b == b && ((RGB) obj).a == a; } - - + + @Override public int hashCode() { return Double.valueOf(r).hashCode() ^ Double.valueOf(g).hashCode() ^ Double.valueOf(b).hashCode() ^ Double.valueOf(a).hashCode(); } - + } diff --git a/src/mightypork/utils/math/coord/Coord.java b/src/mightypork/utils/math/coord/Coord.java index 6cc9365..9ab0b92 100644 --- a/src/mightypork/utils/math/coord/Coord.java +++ b/src/mightypork/utils/math/coord/Coord.java @@ -1,6 +1,5 @@ package mightypork.utils.math.coord; - import java.util.Random; import mightypork.utils.math.Calc; @@ -12,143 +11,164 @@ import mightypork.utils.math.Calc; * @author MightyPork */ public class Coord { - + protected static Random rand = new Random(); - - + + /** * Get distance to other point * - * @param a point a - * @param b point b + * @param a + * point a + * @param b + * point b * @return distance in units */ public static double dist(Coord a, Coord b) { return a.distTo(b); } - + /** X coordinate */ public double x = 0; - + /** Y coordinate */ public double y = 0; - + /** Z coordinate */ public double z = 0; - - + + /** * Create zero coord */ - public Coord() {} - - + public Coord() { + } + + /** * Create coord as a copy of another * - * @param copied copied coord + * @param copied + * copied coord */ public Coord(Coord copied) { setTo(copied); } - - + + /** * Create 2D coord * - * @param x x coordinate - * @param y y coordinate + * @param x + * x coordinate + * @param y + * y coordinate */ public Coord(Number x, Number y) { setTo(x, y); } - - + + /** * Create 3D coord * - * @param x x coordinate - * @param y y coordinate - * @param z z coordinate + * @param x + * x coordinate + * @param y + * y coordinate + * @param z + * z coordinate */ public Coord(Number x, Number y, Number z) { setTo(x, y, z); } - - + + /** * Add a vector, in a copy * - * @param vec offset + * @param vec + * offset * @return changed copy */ public Coord add(Coord vec) { return copy().add_ip(vec); } - - + + /** * Add a vector, in place * - * @param vec offset + * @param vec + * offset * @return this */ public Coord add_ip(Coord vec) { return add_ip(vec.x, vec.y, vec.z); } - - + + /** * Add to each component, in a copy.
* Z is unchanged. * - * @param x x offset - * @param y y offset + * @param x + * x offset + * @param y + * y offset * @return changed copy */ public Coord add(Number x, Number y) { return copy().add_ip(x, y); } - - + + /** * Add to each component, in place.
* Z is unchanged. * - * @param x x offset - * @param y y offset + * @param x + * x offset + * @param y + * y offset * @return this */ public Coord add_ip(Number x, Number y) { return add_ip(x, y, 0); } - - + + /** * Add to each component, in a copy. * - * @param x x offset - * @param y y offset - * @param z z offset + * @param x + * x offset + * @param y + * y offset + * @param z + * z offset * @return changed copy */ public Coord add(Number x, Number y, Number z) { return copy().add_ip(x, y, z); } - - + + /** * Add to each component, in place. * - * @param x x offset - * @param y y offset - * @param z z offset + * @param x + * x offset + * @param y + * y offset + * @param z + * z offset * @return this */ public Coord add_ip(Number x, Number y, Number z) @@ -158,8 +178,8 @@ public class Coord { this.z += z.doubleValue(); return this; } - - + + /** * Make a copy * @@ -169,57 +189,62 @@ public class Coord { { return new Coord(x, y, z); } - - + + /** * Get distance to other point * - * @param point other point + * @param point + * other point * @return distance in units */ public double distTo(Coord point) { return Math.sqrt((point.x - x) * (point.x - x) + (point.y - y) * (point.y - y) + (point.z - z) * (point.z - z)); } - - + + /** * Check if this rectangle in inside a rectangular zone * - * @param rect checked rect. + * @param rect + * checked rect. * @return is inside */ public boolean isInRect(Rect rect) { return isInRect(rect.min, rect.max); } - - + + /** * Check if this rectangle in inside a rectangular zone * - * @param min min coord - * @param max max coord + * @param min + * min coord + * @param max + * max coord * @return is inside */ public boolean isInRect(Coord min, Coord max) { return (x >= min.x && x <= max.x) && (y >= min.y && y <= max.y) && (z >= min.z && z <= max.z); } - - + + /** * Get middle of line to other point * - * @param other other point + * @param other + * other point * @return middle */ public Coord midTo(Coord other) { return add(vecTo(other).half_ip()); } - - + + /** * Get copy divided by two * @@ -229,8 +254,8 @@ public class Coord { { return copy().half_ip(); } - - + + /** * Divide in place by two * @@ -241,104 +266,118 @@ public class Coord { mul_ip(0.5); return this; } - - + + /** * Multiply each component, in a copy. * - * @param d multiplier + * @param d + * multiplier * @return changed copy */ public Coord mul(double d) { return copy().mul_ip(d); } - - + + /** * Multiply each component, in place. * - * @param d multiplier + * @param d + * multiplier * @return this */ public Coord mul_ip(double d) { return mul_ip(d, d, d); } - - + + /** * Multiply each component, in a copy. * - * @param vec vector of multipliers + * @param vec + * vector of multipliers * @return changed copy */ public Coord mul(Coord vec) { return copy().mul_ip(vec); } - - + + /** * Multiply each component, in a copy.
* Z is unchanged. * - * @param x x multiplier - * @param y y multiplier + * @param x + * x multiplier + * @param y + * y multiplier * @return changed copy */ public Coord mul(double x, int y) { return copy().mul_ip(x, y); } - - + + /** * Multiply each component, in place.
* Z is unchanged. * - * @param x x multiplier - * @param y y multiplier + * @param x + * x multiplier + * @param y + * y multiplier * @return this */ public Coord mul_ip(double x, double y) { return mul_ip(x, y, 1); } - - + + /** * Multiply each component, in a copy. * - * @param x x multiplier - * @param y y multiplier - * @param z z multiplier + * @param x + * x multiplier + * @param y + * y multiplier + * @param z + * z multiplier * @return changed copy */ public Coord mul(double x, double y, double z) { return copy().mul_ip(x, y, z); } - - + + /** * Multiply each component, in place. * - * @param vec vector of multipliers + * @param vec + * vector of multipliers * @return this */ public Coord mul_ip(Coord vec) { return mul_ip(vec.x, vec.y, vec.z); } - - + + /** * Multiply each component, in place. * - * @param x x multiplier - * @param y y multiplier - * @param z z multiplier + * @param x + * x multiplier + * @param y + * y multiplier + * @param z + * z multiplier * @return this */ public Coord mul_ip(double x, double y, double z) @@ -348,44 +387,49 @@ public class Coord { this.z *= z; return this; } - - + + public Coord random_offset(double max) { return copy().random_offset_ip(max); } - - + + /** * offset randomly in place * - * @param max max +- offset + * @param max + * max +- offset * @return this */ public Coord random_offset_ip(double max) { return add(random(1).norm_ip(rand.nextDouble() * max)); } - - + + /** * offset randomly * - * @param min min offset - * @param max max offset + * @param min + * min offset + * @param max + * max offset * @return offset coord */ public Coord random_offset(double min, double max) { return copy().random_offset_ip(min, max); } - - + + /** * offset randomly in place * - * @param min min offset - * @param max max offset + * @param min + * min offset + * @param max + * max offset * @return this */ public Coord random_offset_ip(double min, double max) @@ -393,8 +437,8 @@ public class Coord { add_ip(random(min, max)); return this; } - - + + /** * Get a copy with rounded coords * @@ -404,8 +448,8 @@ public class Coord { { return copy().round_ip(); } - - + + /** * Round in place * @@ -418,12 +462,13 @@ public class Coord { z = Math.round(z); return this; } - - + + /** * Set to max values of this and other coord * - * @param other other coord + * @param other + * other coord */ public void setToMax(Coord other) { @@ -431,12 +476,13 @@ public class Coord { y = Math.max(y, other.y); z = Math.max(z, other.z); } - - + + /** * Set to min values of this and other coord * - * @param other other coord + * @param other + * other coord */ public void setToMin(Coord other) { @@ -444,39 +490,45 @@ public class Coord { y = Math.min(y, other.y); z = Math.min(z, other.z); } - - + + /** * Set coordinates to match other coord * - * @param copied coord whose coordinates are used + * @param copied + * coord whose coordinates are used * @return this */ public Coord setTo(Coord copied) { return setTo(copied.x, copied.y, copied.z); } - - + + /** * Set 2D coordinates to * - * @param x x coordinate - * @param y y coordinate + * @param x + * x coordinate + * @param y + * y coordinate * @return this */ public Coord setTo(Number x, Number y) { return setTo(x, y, 0); } - - + + /** * Set 3D coordinates to * - * @param x x coordinate - * @param y y coordinate - * @param z z coordinate + * @param x + * x coordinate + * @param y + * y coordinate + * @param z + * z coordinate * @return this */ public Coord setTo(Number x, Number y, Number z) @@ -486,24 +538,26 @@ public class Coord { this.z = z.doubleValue(); return this; } - - + + /** * Set X coordinate in a copy * - * @param x x coordinate + * @param x + * x coordinate * @return copy with set coordinate */ public Coord setX(Number x) { return copy().setX_ip(x); } - - + + /** * Set X coordinate in place * - * @param x x coordinate + * @param x + * x coordinate * @return this */ public Coord setX_ip(Number x) @@ -511,24 +565,26 @@ public class Coord { this.x = x.doubleValue(); return this; } - - + + /** * Set Y coordinate in a copy * - * @param y y coordinate + * @param y + * y coordinate * @return copy with set coordinate */ public Coord setY(Number y) { return copy().setY_ip(y); } - - + + /** * Set Y coordinate in place * - * @param y y coordinate + * @param y + * y coordinate * @return this */ public Coord setY_ip(Number y) @@ -536,24 +592,26 @@ public class Coord { this.y = y.doubleValue(); return this; } - - + + /** * Set Z coordinate in a copy * - * @param z z coordinate + * @param z + * z coordinate * @return copy with set coordinate */ public Coord setZ(Number z) { return copy().setZ_ip(z); } - - + + /** * Set Z coordinate in place * - * @param z z coordinate + * @param z + * z coordinate * @return this */ public Coord setZ_ip(Number z) @@ -561,78 +619,90 @@ public class Coord { this.z = z.doubleValue(); return this; } - - + + /** * Get a copy subtracted by vector * - * @param vec offset + * @param vec + * offset * @return the offset copy */ public Coord sub(Coord vec) { return copy().sub_ip(vec); } - - + + /** * Get a copy subtracted by 2D coordinate * - * @param x x offset - * @param y y offset + * @param x + * x offset + * @param y + * y offset * @return the offset copy */ public Coord sub(Number x, Number y) { return copy().sub_ip(x, y); } - - + + /** * Get a copy subtracted by 3D coordinate * - * @param x x offset - * @param y y offset - * @param z z offset + * @param x + * x offset + * @param y + * y offset + * @param z + * z offset * @return the offset copy */ public Coord sub(Number x, Number y, Number z) { return copy().sub_ip(x, y, z); } - - + + /** * Offset by vector in place * - * @param vec offset + * @param vec + * offset * @return this */ public Coord sub_ip(Coord vec) { return sub_ip(vec.x, vec.y, vec.z); } - - + + /** * Offset by 2D coordinate in place * - * @param x x offset - * @param y y offset + * @param x + * x offset + * @param y + * y offset * @return this */ public Coord sub_ip(Number x, Number y) { return sub_ip(x, y, 0); } - - + + /** * Offset by 3D coordinate in place * - * @param x x offset - * @param y y offset - * @param z z offset + * @param x + * x offset + * @param y + * y offset + * @param z + * z offset * @return this */ public Coord sub_ip(Number x, Number y, Number z) @@ -642,20 +712,21 @@ public class Coord { this.z -= z.doubleValue(); return this; } - - + + /** * Create vector from this point to other point * - * @param point second point + * @param point + * second point * @return vector */ public Coord vecTo(Coord point) { return point.sub(this); } - - + + /** * @return X as double */ @@ -663,8 +734,8 @@ public class Coord { { return x; } - - + + /** * @return X as int */ @@ -672,8 +743,8 @@ public class Coord { { return (int) Math.round(x); } - - + + /** * @return Y as double */ @@ -681,8 +752,8 @@ public class Coord { { return y; } - - + + /** * @return Y as int */ @@ -690,8 +761,8 @@ public class Coord { { return (int) Math.round(y); } - - + + /** * @return Z as double */ @@ -699,8 +770,8 @@ public class Coord { { return z; } - - + + /** * @return Z as int */ @@ -708,50 +779,56 @@ public class Coord { { return (int) Math.round(z); } - - + + /** * Get cross product of two vectors * - * @param a 1st vector - * @param b 2nd vector + * @param a + * 1st vector + * @param b + * 2nd vector * @return cross product */ public static Coord cross(Coord a, Coord b) { return a.cross(b); } - - + + /** * Get dot product of two vectors * - * @param a 1st vector - * @param b 2nd vector + * @param a + * 1st vector + * @param b + * 2nd vector * @return dot product */ public static double dot(Coord a, Coord b) { return a.dot(b); } - - + + /** * Multiply by other vector, vector multiplication * - * @param vec other vector + * @param vec + * other vector * @return changed copy */ public Coord cross(Coord vec) { return copy().cross_ip(vec); } - - + + /** * Multiply by other vector, vector multiplication; in place * - * @param vec other vector + * @param vec + * other vector * @return this */ public Coord cross_ip(Coord vec) @@ -765,20 +842,21 @@ public class Coord { //@formatter:on return this; } - - + + /** * Get dot product * - * @param vec other vector + * @param vec + * other vector * @return dot product */ public double dot(Coord vec) { return x * vec.x + y * vec.y + z * vec.z; } - - + + /** * Negate all coordinates (* -1) * @@ -788,8 +866,8 @@ public class Coord { { return copy().neg_ip(); } - - + + /** * Negate all coordinates (* -1), in place * @@ -800,36 +878,38 @@ public class Coord { mul_ip(-1); return this; } - - + + /** * Scale vector to given size * - * @param size size we need + * @param size + * size we need * @return scaled vector */ public Coord norm(double size) { return copy().norm_ip(size); } - - + + /** * Scale vector to given size, in place.
* Zero vector remains zero. * - * @param size size we need + * @param size + * size we need * @return scaled vector */ public Coord norm_ip(double size) { if (isZero()) return this; - + double k = size / size(); return mul_ip(k); } - - + + /** * Get vector size * @@ -838,11 +918,11 @@ public class Coord { public double size() { if (isZero()) return 0; - + return Math.sqrt(x * x + y * y + z * z); } - - + + /** * @return true if this coord is a zero coord */ @@ -850,15 +930,15 @@ public class Coord { { return x == 0 && y == 0 && z == 0; } - - + + @Override public String toString() { return "[" + x + ", " + y + ", " + z + "]"; } - - + + @Override public int hashCode() { @@ -873,8 +953,8 @@ public class Coord { result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } - - + + @Override public boolean equals(Object obj) { @@ -887,8 +967,8 @@ public class Coord { if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z)) return false; return true; } - - + + /** * Generate a zero coordinate * @@ -898,8 +978,8 @@ public class Coord { { return new Coord(0, 0, 0); } - - + + /** * Generate a unit coordinate * @@ -909,12 +989,13 @@ public class Coord { { return new Coord(1, 1, 1); } - - + + /** * Generate random coord (gaussian) * - * @param max max distance from 0 + * @param max + * max distance from 0 * @return new coord */ public static Coord random(double max) @@ -927,13 +1008,15 @@ public class Coord { ); //@formatter:on } - - + + /** * Generate random coord (min-max) * - * @param min min offset - * @param max max offset + * @param min + * min offset + * @param max + * max offset * @return new coord */ public static Coord random(double min, double max) diff --git a/src/mightypork/utils/math/coord/CoordAnimated.java b/src/mightypork/utils/math/coord/CoordAnimated.java index e8d7fb9..32632e1 100644 --- a/src/mightypork/utils/math/coord/CoordAnimated.java +++ b/src/mightypork/utils/math/coord/CoordAnimated.java @@ -1,6 +1,5 @@ package mightypork.utils.math.coord; - import mightypork.utils.math.Calc; import mightypork.utils.time.Updateable; @@ -11,17 +10,18 @@ import mightypork.utils.time.Updateable; * @author MightyPork */ public class CoordAnimated extends Coord implements Updateable { - + private double animTime = 0; private Coord offs; private Coord start; private double time = 0; - - + + /** * Update delta timing * - * @param delta delta time to add + * @param delta + * delta time to add */ @Override public void update(double delta) @@ -35,8 +35,8 @@ public class CoordAnimated extends Coord implements Updateable { start.setTo(this); } } - - + + /** * Remember position (other changes will be for animation) */ @@ -47,12 +47,13 @@ public class CoordAnimated extends Coord implements Updateable { start.setTo(this); offs = Coord.zero(); } - - + + /** * Start animation * - * @param time anim length + * @param time + * anim length */ public void animStart(double time) { @@ -62,8 +63,8 @@ public class CoordAnimated extends Coord implements Updateable { animTime = 0; offs = start.vecTo(this); } - - + + /** * Stop animation, assign to current value */ @@ -73,8 +74,8 @@ public class CoordAnimated extends Coord implements Updateable { animRemember(); animTime = 0; } - - + + /** * Get if animation is finished * @@ -84,8 +85,8 @@ public class CoordAnimated extends Coord implements Updateable { { return animTime >= time; } - - + + /** * Get current value (animated) * @@ -94,13 +95,13 @@ public class CoordAnimated extends Coord implements Updateable { public Coord animGetCurrent() { if (time == 0) return copy(); // avoid zero division - + if (start == null) start = new Coord(); if (offs == null) offs = new Coord(); - + if (animIsFinished()) return this; - + return start.add(offs.mul(animTime / time)); } - + } diff --git a/src/mightypork/utils/math/coord/Rect.java b/src/mightypork/utils/math/coord/Rect.java index ba6bf50..507b19f 100644 --- a/src/mightypork/utils/math/coord/Rect.java +++ b/src/mightypork/utils/math/coord/Rect.java @@ -1,6 +1,5 @@ package mightypork.utils.math.coord; - import mightypork.utils.math.Calc; @@ -10,167 +9,193 @@ import mightypork.utils.math.Calc; * @author MightyPork */ public class Rect { - + /** Rect [0, 0, 1, 1] */ public static final Rect ONE = new Rect(0, 0, 1, 1); /** Rect all zeros */ public static final Rect ZERO = new Rect(0, 0, 0, 0); - - + + /** * Rectangle from size * - * @param min min coord - * @param size rect size + * @param min + * min coord + * @param size + * rect size * @return the rect */ public static Rect fromSize(Coord min, Coord size) { return fromSize(min, size.xi(), size.yi()); } - - + + /** * Make rect from min coord and size * - * @param min min coord - * @param width size x - * @param height size y + * @param min + * min coord + * @param width + * size x + * @param height + * size y * @return the new rect */ public static Rect fromSize(Coord min, double width, double height) { return new Rect(min, min.add(width, height)); } - - + + /** * Rectangle from size * - * @param x min X - * @param y min Y - * @param size rect size + * @param x + * min X + * @param y + * min Y + * @param size + * rect size * @return the rect */ public static Rect fromSize(int x, int y, Coord size) { return fromSize(x, y, size.x, size.y); } - - + + /** * Make rect from min coord and size * - * @param xMin min x - * @param yMin min y - * @param width size x - * @param height size y + * @param xMin + * min x + * @param yMin + * min y + * @param width + * size x + * @param height + * size y * @return the new rect */ public static Rect fromSize(double xMin, double yMin, double width, double height) { return new Rect(xMin, yMin, xMin + width, yMin + height); } - + /** Lowest coordinates xy */ protected Coord min = new Coord(); - + /** Highest coordinates xy */ protected Coord max = new Coord(); - - + + /** * New Rect [0, 0, 0, 0] */ public Rect() { this(0, 0, 0, 0); } - - + + /** * Rect [0, 0, size.x, size.y] * - * @param size size coord + * @param size + * size coord */ public Rect(Coord size) { this(0, 0, size.x, size.y); } - - + + /** * New rect of two coords * - * @param c1 coord 1 - * @param c2 coord 2 + * @param c1 + * coord 1 + * @param c2 + * coord 2 */ public Rect(Coord c1, Coord c2) { this(c1.x, c1.y, c2.x, c2.y); } - - + + /** * New Rect * - * @param x1 lower x - * @param y1 lower y - * @param x2 upper x - * @param y2 upper y + * @param x1 + * lower x + * @param y1 + * lower y + * @param x2 + * upper x + * @param y2 + * upper y */ public Rect(double x1, double y1, double x2, double y2) { setTo(x1, y1, x2, y2); } - - + + /** * Rect [0, 0, x, y] * - * @param x width - * @param y height + * @param x + * width + * @param y + * height */ public Rect(double x, double y) { this(0, 0, x, y); } - - + + /** * New rect as a copy of other rect * - * @param r other rect + * @param r + * other rect */ public Rect(Rect r) { this(r.min.x, r.min.y, r.max.x, r.max.y); } - - + + /** * Get offset copy (add) * - * @param move offset vector + * @param move + * offset vector * @return offset copy */ public Rect add(Coord move) { return copy().add_ip(move); } - - + + /** * Add X and Y to all coordinates in a copy * - * @param x x to add - * @param y y to add + * @param x + * x to add + * @param y + * y to add * @return copy changed */ public Rect add(double x, double y) { return add(new Coord(x, y)); } - - + + /** * Offset in place (add) * - * @param move offset vector + * @param move + * offset vector * @return this */ public Rect add_ip(Coord move) @@ -179,21 +204,23 @@ public class Rect { max.add_ip(move); return this; } - - + + /** * Add X and Y to all coordinates in place * - * @param x x to add - * @param y y to add + * @param x + * x to add + * @param y + * y to add * @return this */ public Rect add_ip(double x, double y) { return add_ip(new Coord(x, y)); } - - + + /** * Get a copy * @@ -203,8 +230,8 @@ public class Rect { { return new Rect(this); } - - + + /** * Get copy with the same center and height=0 * @@ -214,8 +241,8 @@ public class Rect { { return new Rect(getCenterLeft(), getCenterRight()); } - - + + /** * Get copy with the same center and width=0 * @@ -225,8 +252,8 @@ public class Rect { { return new Rect(getCenterDown(), getCenterTop()); } - - + + /** * Get rect center * @@ -236,8 +263,8 @@ public class Rect { { return min.midTo(max); } - - + + /** * Get center of the lower edge. * @@ -247,8 +274,8 @@ public class Rect { { return new Coord((max.x + min.x) / 2, min.y); } - - + + /** * Get center of the left edge. * @@ -258,8 +285,8 @@ public class Rect { { return new Coord(min.x, (max.y + min.y) / 2); } - - + + /** * Get center of the right edge. * @@ -269,8 +296,8 @@ public class Rect { { return new Coord(max.x, (max.y + min.y) / 2); } - - + + /** * Get center of the top edge. * @@ -280,8 +307,8 @@ public class Rect { { return new Coord((max.x + min.x) / 2, max.y); } - - + + /** * Get bottom edge rect * @@ -291,8 +318,8 @@ public class Rect { { return new Rect(getLeftBottom(), getRightBottom()); } - - + + /** * Get left edge rect * @@ -302,8 +329,8 @@ public class Rect { { return new Rect(getLeftBottom(), getLeftTop()); } - - + + /** * Get right edge rect * @@ -313,8 +340,8 @@ public class Rect { { return new Rect(getRightBottom(), getRightTop()); } - - + + /** * Get top edge rect * @@ -324,8 +351,8 @@ public class Rect { { return new Rect(getLeftTop(), getRightTop()); } - - + + /** * Get left bottom * @@ -335,8 +362,8 @@ public class Rect { { return new Coord(min.x, min.y); } - - + + /** * Get left top * @@ -346,8 +373,8 @@ public class Rect { { return new Coord(min.x, max.y); } - - + + /** * @return highest coordinates xy */ @@ -355,8 +382,8 @@ public class Rect { { return max; } - - + + /** * @return lowest coordinates xy */ @@ -364,8 +391,8 @@ public class Rect { { return min; } - - + + /** * Get right bottom * @@ -375,8 +402,8 @@ public class Rect { { return new Coord(max.x, min.y); } - - + + /** * Get right top * @@ -386,8 +413,8 @@ public class Rect { { return new Coord(max.x, max.y); } - - + + /** * Get size (width, height) as (x,y) * @@ -397,37 +424,41 @@ public class Rect { { return new Coord(Math.abs(min.x - max.x), Math.abs(min.y - max.y)); } - - + + /** * Grow to sides in copy * - * @param grow grow size (added to each side) + * @param grow + * grow size (added to each side) * @return grown copy */ public Rect grow(Coord grow) { return copy().grow_ip(grow); } - - + + /** * Grow to sides in copy * - * @param x x to add - * @param y y to add + * @param x + * x to add + * @param y + * y to add * @return grown copy */ public Rect grow(double x, double y) { return copy().grow_ip(x, y); } - - + + /** * Grow to sides in place * - * @param grow grow size (added to each side) + * @param grow + * grow size (added to each side) * @return this */ public Rect grow_ip(Coord grow) @@ -436,13 +467,15 @@ public class Rect { max.add_ip(grow); return this; } - - + + /** * Grow to sides in place * - * @param x x to add - * @param y y to add + * @param x + * x to add + * @param y + * y to add * @return this */ public Rect grow_ip(double x, double y) @@ -451,24 +484,26 @@ public class Rect { max.add_ip(x, y); return this; } - - + + /** * Grow down in copy * - * @param down added pixels + * @param down + * added pixels * @return grown copy */ public Rect growDown(double down) { return copy().growDown_ip(down); } - - + + /** * Grow down in place * - * @param down added pixels + * @param down + * added pixels * @return this */ public Rect growDown_ip(double down) @@ -476,24 +511,26 @@ public class Rect { min.sub_ip(0, down); return this; } - - + + /** * Grow to left in copy * - * @param left added pixels + * @param left + * added pixels * @return grown copy */ public Rect growLeft(double left) { return copy().growLeft_ip(left); } - - + + /** * Grow to left in place * - * @param left added pixels + * @param left + * added pixels * @return this */ public Rect growLeft_ip(double left) @@ -501,24 +538,26 @@ public class Rect { min.sub_ip(left, 0); return this; } - - + + /** * Grow to right in copy * - * @param right added pixels + * @param right + * added pixels * @return grown copy */ public Rect growRight(double right) { return copy().growRight_ip(right); } - - + + /** * Grow to right in place * - * @param right added pixels + * @param right + * added pixels * @return this */ public Rect growRight_ip(double right) @@ -526,24 +565,26 @@ public class Rect { max.add_ip(right, 0); return this; } - - + + /** * Grow up in copy * - * @param add added pixels + * @param add + * added pixels * @return grown copy */ public Rect growUp(double add) { return copy().growUp_ip(add); } - - + + /** * Grow up in place * - * @param add added pixels + * @param add + * added pixels * @return this */ public Rect growUp_ip(double add) @@ -551,49 +592,54 @@ public class Rect { max.add_ip(0, add); return this; } - - + + /** * Check if point is inside this rectangle * - * @param point point to test + * @param point + * point to test * @return is inside */ public boolean isInside(Coord point) { return Calc.inRange(point.x, min.x, max.x) && Calc.inRange(point.y, min.y, max.y); } - - + + /** * Multiply in copy * - * @param factor multiplier + * @param factor + * multiplier * @return offset copy */ public Rect mul(double factor) { return copy().mul_ip(factor); } - - + + /** * Multiply by number (useful for centered rects) * - * @param x x multiplier - * @param y y multiplier + * @param x + * x multiplier + * @param y + * y multiplier * @return copy multiplied */ public Rect mul(double x, double y) { return copy().mul_ip(x, y); } - - + + /** * Multiply coord in place * - * @param factor multiplier + * @param factor + * multiplier * @return this */ public Rect mul_ip(double factor) @@ -602,13 +648,15 @@ public class Rect { max.mul_ip(factor); return this; } - - + + /** * Multiply coord in place * - * @param x multiplier x - * @param y multiplier y + * @param x + * multiplier x + * @param y + * multiplier y * @return this */ public Rect mul_ip(double x, double y) @@ -617,8 +665,8 @@ public class Rect { max.mul_ip(x, y, 1); return this; } - - + + /** * Round coords in copy * @@ -628,8 +676,8 @@ public class Rect { { return new Rect(min.round(), max.round()); } - - + + /** * Round this in place * @@ -641,26 +689,31 @@ public class Rect { max.round_ip(); return this; } - - + + /** * Set to [0,0,coord.x,coord.y] * - * @param coord size coord + * @param coord + * size coord */ public void setTo(Coord coord) { setTo(0, 0, coord.x, coord.y); } - - + + /** * Set to coordinates * - * @param x1 lower x - * @param y1 lower y - * @param x2 upper x - * @param y2 upper y + * @param x1 + * lower x + * @param y1 + * lower y + * @param x2 + * upper x + * @param y2 + * upper y */ public void setTo(double x1, double y1, double x2, double y2) { @@ -669,62 +722,69 @@ public class Rect { max.x = Calc.max(x1, x2); max.y = Calc.max(y1, y2); } - - + + /** * Set to other rect's coordinates * - * @param r other rect + * @param r + * other rect */ public void setTo(Rect r) { min.setTo(r.min); max.setTo(r.max); } - - + + /** * Subtract X and Y from all coordinates in a copy * - * @param x x to subtract - * @param y y to subtract + * @param x + * x to subtract + * @param y + * y to subtract * @return copy changed */ public Rect sub(double x, double y) { return sub(new Coord(x, y)); } - - + + /** * Get offset copy (subtract) * - * @param move offset vector + * @param move + * offset vector * @return offset copy */ public Rect sub(Coord move) { return copy().sub_ip(move); } - - + + /** * Subtract X and Y from all coordinates in place * - * @param x x to subtract - * @param y y to subtract + * @param x + * x to subtract + * @param y + * y to subtract * @return this */ public Rect sub_ip(double x, double y) { return sub_ip(new Coord(x, y)); } - - + + /** * Offset in place (subtract) * - * @param move offset vector + * @param move + * offset vector * @return this */ public Rect sub_ip(Coord move) @@ -733,15 +793,15 @@ public class Rect { max.sub_ip(move); return this; } - - + + @Override public String toString() { return String.format("[( %4d, %4d )-( %4d, %4d )]", (int) min.x, (int) min.y, (int) max.x, (int) max.y); } - - + + /** * @return lower x */ @@ -749,8 +809,8 @@ public class Rect { { return min.x; } - - + + /** * @return upper x */ @@ -758,8 +818,8 @@ public class Rect { { return max.x; } - - + + /** * @return lower y */ @@ -767,8 +827,8 @@ public class Rect { { return min.y; } - - + + /** * @return upper y */ diff --git a/src/mightypork/utils/math/easing/Easing.java b/src/mightypork/utils/math/easing/Easing.java index 2079a58..e728e73 100644 --- a/src/mightypork/utils/math/easing/Easing.java +++ b/src/mightypork/utils/math/easing/Easing.java @@ -1,106 +1,115 @@ package mightypork.utils.math.easing; - /** * EasingFunction function. * * @author MightyPork */ public abstract class Easing { - + /** * Get value at time t. * - * @param t time parameter (t = 1..1) + * @param t + * time parameter (t = 1..1) * @return value at given t (0..1, can exceed if needed) */ public abstract double get(double t); - - + + /** * Reverse an easing * - * @param original original easing + * @param original + * original easing * @return reversed easing */ public static Easing reverse(Easing original) { return new Reverse(original); } - - + + /** * Combine two easings * - * @param in initial easing - * @param out terminal easing + * @param in + * initial easing + * @param out + * terminal easing * @return product */ public static Easing combine(Easing in, Easing out) { return new Composite(in, out); } - - + + /** * Create "bilinear" easing - compose of straight and reverse. * - * @param in initial easing + * @param in + * initial easing * @return product */ public static Easing inOut(Easing in) { return combine(in, reverse(in)); } - + + /** * Reverse EasingFunction * * @author MightyPork */ private static class Reverse extends Easing { - + private Easing ea; - - + + /** - * @param in Easing to reverse + * @param in + * Easing to reverse */ public Reverse(Easing in) { this.ea = in; } - - + + @Override public double get(double t) { return 1 - ea.get(1 - t); } } - + + /** * Composite EasingFunction (0-0.5 EasingFunction A, 0.5-1 EasingFunction B) * * @author MightyPork */ private static class Composite extends Easing { - + private Easing in; private Easing out; - - + + /** * Create a composite EasingFunction * - * @param in initial EasingFunction - * @param out terminal EasingFunction + * @param in + * initial EasingFunction + * @param out + * terminal EasingFunction */ public Composite(Easing in, Easing out) { this.in = in; this.out = out; } - - + + @Override public double get(double t) { @@ -108,172 +117,172 @@ public abstract class Easing { return 0.5 + out.get(2 * t - 1) * 0.5; } } - + /** No easing; At t=0.5 goes high. */ public static final Easing NONE = new Easing() { - + @Override public double get(double t) { return (t < 0.5 ? 0 : 1); } }; - + /** Linear (y=t) easing */ public static final Easing LINEAR = new Easing() { - + @Override public double get(double t) { return t; } }; - + /** Quadratic (y=t^2) easing in */ public static final Easing QUADRATIC_IN = new Easing() { - + @Override public double get(double t) { return t * t; } }; - + /** Quadratic (y=t^2) easing out */ public static final Easing QUADRATIC_OUT = reverse(QUADRATIC_IN); - + /** Quadratic (y=t^2) easing both */ public static final Easing QUADRATIC_IN_OUT = inOut(QUADRATIC_IN); - + /** Cubic (y=t^3) easing in */ public static final Easing CUBIC_IN = new Easing() { - + @Override public double get(double t) { return t * t * t; } }; - + /** Cubic (y=t^3) easing out */ public static final Easing CUBIC_OUT = reverse(CUBIC_IN); - + /** Cubic (y=t^3) easing both */ public static final Easing CUBIC_IN_OUT = inOut(CUBIC_IN); - + /** Quartic (y=t^4) easing in */ public static final Easing QUARTIC_IN = new Easing() { - + @Override public double get(double t) { return t * t * t * t; } }; - + /** Quartic (y=t^4) easing out */ public static final Easing QUARTIC_OUT = reverse(QUADRATIC_IN); - + /** Quartic (y=t^4) easing both */ public static final Easing QUARTIC_IN_OUT = inOut(QUADRATIC_IN); - + /** Quintic (y=t^5) easing in */ public static final Easing QUINTIC_IN = new Easing() { - + @Override public double get(double t) { return t * t * t * t * t; } }; - + /** Quintic (y=t^5) easing out */ public static final Easing QUINTIC_OUT = reverse(QUINTIC_IN); - + /** Quintic (y=t^5) easing both */ public static final Easing QUINTIC_IN_OUT = inOut(QUINTIC_IN); - + /** Sine easing in */ public static final Easing SINE_IN = new Easing() { - + @Override public double get(double t) { return 1 - Math.cos(t * (Math.PI / 2)); } }; - + /** Sine easing out */ public static final Easing SINE_OUT = reverse(SINE_IN); - + /** Sine easing both */ public static final Easing SINE_IN_OUT = inOut(SINE_IN); - + /** Exponential easing in */ public static final Easing EXPO_IN = new Easing() { - + @Override public double get(double t) { return Math.pow(2, 10 * (t - 1)); } }; - + /** Exponential easing out */ public static final Easing EXPO_OUT = reverse(EXPO_IN); - + /** Exponential easing both */ public static final Easing EXPO_IN_OUT = inOut(EXPO_IN); - + /** Circular easing in */ public static final Easing CIRC_IN = new Easing() { - + @Override public double get(double t) { return 1 - Math.sqrt(1 - t * t); } }; - + /** Circular easing out */ public static final Easing CIRC_OUT = reverse(CIRC_IN); - + /** Circular easing both */ public static final Easing CIRC_IN_OUT = inOut(CIRC_IN); - + /** Bounce easing in */ public static final Easing BOUNCE_OUT = new Easing() { - + @Override public double get(double t) { if (t < (1 / 2.75f)) { return (7.5625f * t * t); - + } else if (t < (2 / 2.75f)) { t -= (1.5f / 2.75f); return (7.5625f * t * t + 0.75f); - + } else if (t < (2.5 / 2.75)) { t -= (2.25f / 2.75f); return (7.5625f * t * t + 0.9375f); - + } else { t -= (2.625f / 2.75f); return (7.5625f * t * t + 0.984375f); } } }; - + /** Bounce easing out */ public static final Easing BOUNCE_IN = reverse(BOUNCE_OUT); - + /** Bounce easing both */ public static final Easing BOUNCE_IN_OUT = inOut(BOUNCE_IN); - + /** Back easing in */ public static final Easing BACK_IN = new Easing() { - + @Override public double get(double t) { @@ -281,31 +290,31 @@ public abstract class Easing { return t * t * ((s + 1) * t - s); } }; - + /** Back easing out */ public static final Easing BACK_OUT = reverse(BACK_IN); - + /** Back easing both */ public static final Easing BACK_IN_OUT = inOut(BACK_IN); - + /** Elastic easing in */ public static final Easing ELASTIC_IN = new Easing() { - + @Override public double get(double t) { if (t == 0) return 0; if (t == 1) return 1; - + double p = .3f; double s = p / 4; return -(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * (2 * Math.PI) / p)); } }; - + /** Elastic easing out */ public static final Easing ELASTIC_OUT = reverse(ELASTIC_IN); - + /** Elastic easing both */ public static final Easing ELASTIC_IN_OUT = inOut(ELASTIC_IN); } diff --git a/src/mightypork/utils/objects/Convertor.java b/src/mightypork/utils/objects/Convertor.java index c8c36f5..52380c4 100644 --- a/src/mightypork/utils/objects/Convertor.java +++ b/src/mightypork/utils/objects/Convertor.java @@ -1,6 +1,5 @@ package mightypork.utils.objects; - import mightypork.utils.logging.Log; import mightypork.utils.math.Range; import mightypork.utils.math.coord.Coord; @@ -13,12 +12,14 @@ import mightypork.utils.math.coord.Coord; * @author MightyPork */ public class Convertor { - + /** * Get INTEGER * - * @param o object - * @param def default value + * @param o + * object + * @param def + * default value * @return integer */ public static int getInteger(Object o, Integer def) @@ -29,17 +30,20 @@ public class Convertor { if (o instanceof Number) return ((Number) o).intValue(); if (o instanceof Range) return ((Range) o).randInt(); if (o instanceof Boolean) return ((Boolean) o) ? 1 : 0; - } catch (NumberFormatException e) {} + } catch (NumberFormatException e) { + } Log.w("Cannot convert " + o + " to Integer."); return def; } - - + + /** * Get DOUBLE * - * @param o object - * @param def default value + * @param o + * object + * @param def + * default value * @return double */ public static double getDouble(Object o, Double def) @@ -50,17 +54,20 @@ public class Convertor { if (o instanceof Number) return ((Number) o).doubleValue(); if (o instanceof Range) return ((Range) o).randDouble(); if (o instanceof Boolean) return ((Boolean) o) ? 1 : 0; - } catch (NumberFormatException e) {} + } catch (NumberFormatException e) { + } Log.w("Cannot convert " + o + " to Double."); return def; } - - + + /** * Get FLOAT * - * @param o object - * @param def default value + * @param o + * object + * @param def + * default value * @return float */ public static double getFloat(Object o, Float def) @@ -68,23 +75,26 @@ public class Convertor { try { if (o == null) return def; if (o instanceof Number) return ((Number) o).floatValue(); - } catch (NumberFormatException e) {} + } catch (NumberFormatException e) { + } Log.w("Cannot convert " + o + " to Float."); return def; } - - + + /** * Get BOOLEAN * - * @param o object - * @param def default value + * @param o + * object + * @param def + * default value * @return boolean */ public static boolean getBoolean(Object o, Boolean def) { if (o == null) return def; - + if (o instanceof String) { String s = ((String) o).trim().toLowerCase(); if (s.equals("0")) return false; @@ -92,31 +102,34 @@ public class Convertor { try { double n = Double.parseDouble(s); return n != 0; - } catch (NumberFormatException e) {} - + } catch (NumberFormatException e) { + } + if (s.equals("true")) return true; if (s.equals("yes")) return true; if (s.equals("y")) return true; if (s.equals("enabled")) return true; - + if (s.equals("false")) return false; if (s.equals("no")) return false; if (s.equals("n")) return false; if (s.equals("disabled")) return true; } - + if (o instanceof Boolean) return ((Boolean) o).booleanValue(); if (o instanceof Number) return ((Number) o).intValue() != 0; Log.w("Cannot convert " + o + " to Boolean."); return def; } - - + + /** * Get STRING * - * @param o object - * @param def default value + * @param o + * object + * @param def + * default value * @return String */ public static String getString(Object o, String def) @@ -126,14 +139,16 @@ public class Convertor { Log.w("Cannot convert " + o + " to String."); return o.toString(); } - - + + /** * Get AI_COORD
* Converts special constants to magic coordinate instances. * - * @param o object - * @param def default value + * @param o + * object + * @param def + * default value * @return AiCoord */ public static Coord getCoord(Object o, Coord def) @@ -142,7 +157,7 @@ public class Convertor { if (o == null) return def; if (o instanceof String) { String s = ((String) o).trim().toUpperCase(); - + // colon to semicolon s = s.replace(':', ';'); // comma to semicolon @@ -159,13 +174,15 @@ public class Convertor { Log.w("Cannot convert " + o + " to Coord."); return def; } - - + + /** * Get RANGE * - * @param o object - * @param def default value + * @param o + * object + * @param def + * default value * @return AiCoord */ public static Range getRange(Object o, Range def) @@ -175,7 +192,7 @@ public class Convertor { if (o instanceof Number) return new Range(((Number) o).doubleValue(), ((Number) o).doubleValue()); if (o instanceof String) { String s = ((String) o).trim(); - + // colon to semicolon s = s.replace(',', ':'); // comma to dot. @@ -187,97 +204,105 @@ public class Convertor { String[] parts = s.split("[:]"); if (parts.length == 2) return new Range(Double.parseDouble(parts[0].trim()), Double.parseDouble(parts[1].trim())); return new Range(Double.parseDouble(parts[0].trim()), Double.parseDouble(parts[0].trim())); - + } if (o instanceof Range) return (Range) o; - } catch (NumberFormatException e) {} + } catch (NumberFormatException e) { + } Log.w("Cannot convert " + o + " to Range."); return def; } - - + + /** * Get INTEGER * - * @param o object + * @param o + * object * @return integer */ public static int getInteger(Object o) { return getInteger(o, 0); } - - + + /** * Get DOUBLE * - * @param o object + * @param o + * object * @return double */ public static double getDouble(Object o) { return getDouble(o, 0d); } - - + + /** * Get FLOAT * - * @param o object + * @param o + * object * @return float */ public static double getFloat(Object o) { return getFloat(o, 0f); } - - + + /** * Get BOOLEAN * - * @param o object + * @param o + * object * @return boolean */ public static boolean getBoolean(Object o) { return getBoolean(o, false); } - - + + /** * Get STRING * - * @param o object + * @param o + * object * @return String */ public static String getString(Object o) { return getString(o, ""); } - - + + /** * Get AI_COORD (if special string constant is present instead, build coord * of it) * - * @param o object + * @param o + * object * @return AiCoord */ public static Coord getCoord(Object o) { return getCoord(o, Coord.zero()); } - - + + /** * Get RANGE * - * @param o object + * @param o + * object * @return AiCoord */ public static Range getRange(Object o) { return getRange(o, new Range()); } - + } diff --git a/src/mightypork/utils/objects/Mutable.java b/src/mightypork/utils/objects/Mutable.java index e965ed0..db40bd8 100644 --- a/src/mightypork/utils/objects/Mutable.java +++ b/src/mightypork/utils/objects/Mutable.java @@ -1,28 +1,29 @@ package mightypork.utils.objects; - /** * Mutable object * * @author MightyPork - * @param type + * @param + * type */ public class Mutable { - + /** The wrapped value */ private T o = null; - - + + /** * New mutable object * - * @param o value + * @param o + * value */ public Mutable(T o) { this.o = o; } - - + + /** * Get the wrapped value * @@ -32,19 +33,20 @@ public class Mutable { { return o; } - - + + /** * Set value * - * @param o new value to set + * @param o + * new value to set */ public void set(T o) { this.o = o; } - - + + @Override public int hashCode() { @@ -53,15 +55,15 @@ public class Mutable { result = prime * result + ((o == null) ? 0 : o.hashCode()); return result; } - - + + @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Mutable)) return false; - + Mutable other = (Mutable) obj; if (o == null) { if (other.o != null) return false; @@ -70,8 +72,8 @@ public class Mutable { } return true; } - - + + @Override public String toString() { diff --git a/src/mightypork/utils/objects/ObjectUtils.java b/src/mightypork/utils/objects/ObjectUtils.java index eaa238e..4145bd5 100644 --- a/src/mightypork/utils/objects/ObjectUtils.java +++ b/src/mightypork/utils/objects/ObjectUtils.java @@ -1,6 +1,5 @@ package mightypork.utils.objects; - import java.util.*; import java.util.Map.Entry; @@ -11,7 +10,7 @@ import java.util.Map.Entry; * @author MightyPork */ public class ObjectUtils { - + public static Object fallback(Object... options) { for (Object o : options) { @@ -19,51 +18,56 @@ public class ObjectUtils { } return null; // error } - - + + /** * Sort a map by keys, maintaining key-value pairs. * - * @param map map to be sorted - * @param comparator a comparator, or null for natural ordering + * @param map + * map to be sorted + * @param comparator + * a comparator, or null for natural ordering * @return linked hash map with sorted entries */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map sortByKeys(Map map, final Comparator comparator) { List keys = new LinkedList(map.keySet()); - + if (comparator == null) { Collections.sort(keys); } else { Collections.sort(keys, comparator); } - + // LinkedHashMap will keep the keys in the order they are inserted // which is currently sorted on natural ordering Map sortedMap = new LinkedHashMap(); for (K key : keys) { sortedMap.put(key, map.get(key)); } - + return sortedMap; } - - + + /** * Sort a map by values, maintaining key-value pairs. * - * @param map map to be sorted - * @param comparator a comparator, or null for natural ordering + * @param map + * map to be sorted + * @param comparator + * a comparator, or null for natural ordering * @return linked hash map with sorted entries */ @SuppressWarnings("rawtypes") public static Map sortByValues(Map map, final Comparator comparator) { List> entries = new LinkedList>(map.entrySet()); - + Collections.sort(entries, new Comparator>() { - + + @SuppressWarnings("unchecked") @Override public int compare(Entry o1, Entry o2) { @@ -71,23 +75,23 @@ public class ObjectUtils { return comparator.compare(o1.getValue(), o2.getValue()); } }); - + // LinkedHashMap will keep the keys in the order they are inserted // which is currently sorted on natural ordering Map sortedMap = new LinkedHashMap(); - + for (Map.Entry entry : entries) { sortedMap.put(entry.getKey(), entry.getValue()); } - + return sortedMap; } - - + + public static String arrayToString(Object[] arr) { StringBuilder sb = new StringBuilder(); - + sb.append('['); boolean first = true; for (Object o : arr) { @@ -95,11 +99,11 @@ public class ObjectUtils { sb.append(o.toString()); } sb.append(']'); - + return sb.toString(); } - - + + public static List arrayToList(T[] objs) { ArrayList list = new ArrayList(); diff --git a/src/mightypork/utils/objects/Pair.java b/src/mightypork/utils/objects/Pair.java index 822d8de..61a733c 100644 --- a/src/mightypork/utils/objects/Pair.java +++ b/src/mightypork/utils/objects/Pair.java @@ -1,6 +1,5 @@ package mightypork.utils.objects; - import mightypork.utils.math.Calc; @@ -9,34 +8,38 @@ import mightypork.utils.math.Calc; * * @author MightyPork * @copy (c) 2012 - * @param 1st object class - * @param 2nd object class + * @param + * 1st object class + * @param + * 2nd object class */ public class Pair { - + /** * 1st object */ public T1 first; - + /** * 2nd object */ public T2 second; - - + + /** * Make structure of 2 objects * - * @param first 1st object - * @param second 2nd object + * @param first + * 1st object + * @param second + * 2nd object */ public Pair(T1 first, T2 second) { this.first = first; this.second = second; } - - + + /** * @return 1st object */ @@ -44,8 +47,8 @@ public class Pair { { return first; } - - + + /** * @return 2nd object */ @@ -53,26 +56,26 @@ public class Pair { { return second; } - - + + @Override public boolean equals(Object obj) { if (obj == null) { return false; } - + if (!this.getClass().equals(obj.getClass())) { return false; } - + Pair t = (Pair) obj; - + return Calc.areObjectsEqual(first, t.first) && Calc.areObjectsEqual(second, t.second); - + } - - + + @Override public int hashCode() { @@ -81,12 +84,12 @@ public class Pair { hash += (second == null ? 0 : second.hashCode()); return hash; } - - + + @Override public String toString() { return "PAIR{" + first + "," + second + "}"; } - + } diff --git a/src/mightypork/utils/objects/Triad.java b/src/mightypork/utils/objects/Triad.java index 027b90a..65df04f 100644 --- a/src/mightypork/utils/objects/Triad.java +++ b/src/mightypork/utils/objects/Triad.java @@ -1,6 +1,5 @@ package mightypork.utils.objects; - import mightypork.utils.math.Calc; @@ -9,31 +8,37 @@ import mightypork.utils.math.Calc; * * @author MightyPork * @copy (c) 2012 - * @param 1st object class - * @param 2nd object class - * @param 3rd object class + * @param + * 1st object class + * @param + * 2nd object class + * @param + * 3rd object class */ public class Triad extends Pair { - + /** * 3rd object */ public T3 third; - - + + /** * Make structure of 3 objects * - * @param objA 1st object - * @param objB 2nd object - * @param objC 3rd object + * @param objA + * 1st object + * @param objB + * 2nd object + * @param objC + * 3rd object */ public Triad(T1 objA, T2 objB, T3 objC) { super(objA, objB); third = objC; } - - + + /** * @return 3rd object */ @@ -41,40 +46,41 @@ public class Triad extends Pair { { return third; } - - + + /** * Set 1st object * - * @param obj 1st object + * @param obj + * 1st object */ public void setThird(T3 obj) { third = obj; } - - + + @Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; - + return Calc.areObjectsEqual(third, ((Triad) obj).third); - + } - - + + @Override public int hashCode() { return super.hashCode() + (third == null ? 0 : third.hashCode()); } - - + + @Override public String toString() { return "TRIAD{" + first + "," + second + "," + third + "}"; } - + } diff --git a/src/mightypork/utils/objects/VarargsParser.java b/src/mightypork/utils/objects/VarargsParser.java index 96c0ec1..2450a72 100644 --- a/src/mightypork/utils/objects/VarargsParser.java +++ b/src/mightypork/utils/objects/VarargsParser.java @@ -1,6 +1,5 @@ package mightypork.utils.objects; - import java.util.LinkedHashMap; import java.util.Map; @@ -26,28 +25,33 @@ import java.util.Map; * * * @author MightyPork - * @param Type for Map keys - * @param Type for Map values + * @param + * Type for Map keys + * @param + * Type for Map values */ public class VarargsParser { - + /** * Parse array of vararg key, value pairs to a LinkedHashMap. * - * @param args varargs + * @param args + * varargs * @return LinkedHashMap - * @throws ClassCastException in case of incompatible type in the array - * @throws IllegalArgumentException in case of invalid array length (odd) + * @throws ClassCastException + * in case of incompatible type in the array + * @throws IllegalArgumentException + * in case of invalid array length (odd) */ @SuppressWarnings("unchecked") public Map parse(Object... args) throws ClassCastException, IllegalArgumentException { LinkedHashMap attrs = new LinkedHashMap(); - + if (args.length % 2 != 0) { throw new IllegalArgumentException("Odd number of elements in varargs map!"); } - + K key = null; for (Object o : args) { if (key == null) { @@ -58,7 +62,7 @@ public class VarargsParser { key = null; } } - + return attrs; } } diff --git a/src/mightypork/utils/patterns/Destroyable.java b/src/mightypork/utils/patterns/Destroyable.java index 51caf15..bdecebc 100644 --- a/src/mightypork/utils/patterns/Destroyable.java +++ b/src/mightypork/utils/patterns/Destroyable.java @@ -1,13 +1,12 @@ package mightypork.utils.patterns; - /** * Object that can be destroyed (free resources etc) * * @author MightyPork */ public interface Destroyable { - + /** * Destroy this object */ diff --git a/src/mightypork/utils/patterns/subscription/Handleable.java b/src/mightypork/utils/patterns/subscription/Handleable.java index 815b323..96fb772 100644 --- a/src/mightypork/utils/patterns/subscription/Handleable.java +++ b/src/mightypork/utils/patterns/subscription/Handleable.java @@ -1,18 +1,19 @@ package mightypork.utils.patterns.subscription; - /** * Something that can be handled by HANDLER. * * @author MightyPork - * @param handler type + * @param + * handler type */ public interface Handleable { - + /** * Ask handler to handle this message. * - * @param handler handler instance + * @param handler + * handler instance */ public void handleBy(HANDLER handler); } diff --git a/src/mightypork/utils/patterns/subscription/MessageBus.java b/src/mightypork/utils/patterns/subscription/MessageBus.java index aa43699..aede3e7 100644 --- a/src/mightypork/utils/patterns/subscription/MessageBus.java +++ b/src/mightypork/utils/patterns/subscription/MessageBus.java @@ -1,6 +1,5 @@ package mightypork.utils.patterns.subscription; - import java.util.Collection; import java.util.LinkedHashSet; @@ -13,19 +12,20 @@ import mightypork.utils.logging.Log; * @author MightyPork */ final public class MessageBus { - + private Collection> channels = new LinkedHashSet>(); - + private Collection clients = new LinkedHashSet(); - + private boolean warn_unsent = true; - - + + /** * Add a {@link MessageChannel} to this bus.
* If a channel of matching types is already added, it is returned instead. * - * @param channel channel to be added + * @param channel + * channel to be added * @return the channel that's now in the bus */ public MessageChannel addChannel(MessageChannel channel) @@ -37,72 +37,76 @@ final public class MessageBus { return ch; } } - + channels.add(channel); - + return channel; } - - + + /** * Remove a {@link MessageChannel} from this bus * - * @param channel true if channel was removed + * @param channel + * true if channel was removed */ public void removeChannel(MessageChannel channel) { channels.remove(channel); } - - + + /** * Broadcast a message * - * @param message message + * @param message + * message * @return true if message was accepted by at least one channel */ public boolean broadcast(Object message) { boolean sent = false; - + for (MessageChannel b : channels) { sent |= b.broadcast(message, clients); } - + if (!sent && warn_unsent) Log.w("Message not accepted by any channel: " + message); - + return sent; } - - + + /** * Connect a client to the bus. The client will be connected to all current * and future channels, until removed from the bus. * - * @param client the client + * @param client + * the client * @return true on success */ public boolean subscribe(Object client) { if (client == null) return false; - + clients.add(client); - + return true; } - - + + /** * Disconnect a client from the bus. * - * @param client the client + * @param client + * the client */ public void unsubscribe(Object client) { clients.remove(client); } - - + + /** * Enable logging of unsent messages * @@ -112,13 +116,15 @@ final public class MessageBus { { this.warn_unsent = enable; } - - + + /** * Add a channel for given message and client type. * - * @param messageClass message type - * @param clientClass client type + * @param messageClass + * message type + * @param clientClass + * client type * @return the created channel instance */ public , F_CLIENT> MessageChannel createChannel(Class messageClass, Class clientClass) @@ -126,5 +132,5 @@ final public class MessageBus { MessageChannel bc = new MessageChannel(messageClass, clientClass); return addChannel(bc); } - + } diff --git a/src/mightypork/utils/patterns/subscription/MessageChannel.java b/src/mightypork/utils/patterns/subscription/MessageChannel.java index 3a94993..2d02289 100644 --- a/src/mightypork/utils/patterns/subscription/MessageChannel.java +++ b/src/mightypork/utils/patterns/subscription/MessageChannel.java @@ -1,6 +1,5 @@ package mightypork.utils.patterns.subscription; - import java.util.Collection; import java.util.HashSet; @@ -13,64 +12,68 @@ import mightypork.utils.patterns.subscription.clients.ToggleableClient; * Message channel, module of {@link MessageBus} * * @author MightyPork - * @param message type - * @param client (subscriber) type + * @param + * message type + * @param + * client (subscriber) type */ final public class MessageChannel, CLIENT> { - + private Class clientClass; private Class messageClass; - - + + public MessageChannel(Class messageClass, Class clientClass) { - + if (messageClass == null || clientClass == null) throw new NullPointerException("Null Message or Client class."); - + this.clientClass = clientClass; this.messageClass = messageClass; } - - + + /** * Try to broadcast a message.
* If message is of wrong type, false is returned. * - * @param message a message to be sent - * @param clients collection of clients + * @param message + * a message to be sent + * @param clients + * collection of clients * @return true if message was accepted by this channel */ public boolean broadcast(Object message, Collection clients) { if (!canBroadcast(message)) return false; - + MESSAGE evt = messageClass.cast(message); - + doBroadcast(evt, clients, new HashSet()); - + return true; } - - + + private void doBroadcast(MESSAGE message, Collection clients, Collection processed) { for (Object client : clients) { - + // circular reference check if (processed.contains(client)) { Log.w("Client already served (subscribing twice?)"); continue; } processed.add(client); - + // opt-out if (client instanceof ToggleableClient) { if (!((ToggleableClient) client).doesSubscribe()) { continue; } } - + sendTo(client, message); - + if (client instanceof DelegatingClient) { if (((DelegatingClient) client).doesDelegate()) { doBroadcast(message, ((DelegatingClient) client).getChildClients(), processed); @@ -78,13 +81,15 @@ final public class MessageChannel, CLIENT> { } } } - - + + /** * Send a message to a client. * - * @param client target client - * @param message message to send + * @param client + * target client + * @param message + * message to send */ @SuppressWarnings("unchecked") private void sendTo(Object client, MESSAGE message) @@ -93,21 +98,22 @@ final public class MessageChannel, CLIENT> { ((Handleable) message).handleBy((CLIENT) client); } } - - + + /** * Check if the given message can be broadcasted by this * {@link MessageChannel} * - * @param message event object + * @param message + * event object * @return can be broadcasted */ public boolean canBroadcast(Object message) { return message != null && messageClass.isInstance(message); } - - + + @Override public int hashCode() { @@ -117,8 +123,8 @@ final public class MessageChannel, CLIENT> { result = prime * result + ((messageClass == null) ? 0 : messageClass.hashCode()); return result; } - - + + @Override public boolean equals(Object obj) { @@ -134,25 +140,27 @@ final public class MessageChannel, CLIENT> { } else if (!messageClass.equals(other.messageClass)) return false; return true; } - - + + @Override public String toString() { return "CHANNEL( " + messageClass.getSimpleName() + " -> " + clientClass.getSimpleName() + " )"; } - - + + /** * Create an instance for given types * - * @param messageClass event class - * @param clientClass client class + * @param messageClass + * event class + * @param clientClass + * client class * @return the broadcaster */ public static , F_CLIENT> MessageChannel create(Class messageClass, Class clientClass) { return new MessageChannel(messageClass, clientClass); } - + } diff --git a/src/mightypork/utils/patterns/subscription/clients/DelegatingClient.java b/src/mightypork/utils/patterns/subscription/clients/DelegatingClient.java index 266b523..48431a3 100644 --- a/src/mightypork/utils/patterns/subscription/clients/DelegatingClient.java +++ b/src/mightypork/utils/patterns/subscription/clients/DelegatingClient.java @@ -1,6 +1,5 @@ package mightypork.utils.patterns.subscription.clients; - import java.util.Collection; @@ -10,16 +9,16 @@ import java.util.Collection; * @author MightyPork */ public interface DelegatingClient { - + /** * @return collection of child clients. Can not be null. */ public Collection getChildClients(); - - + + /** * @return true if delegating is active */ public boolean doesDelegate(); - + } diff --git a/src/mightypork/utils/patterns/subscription/clients/ToggleableClient.java b/src/mightypork/utils/patterns/subscription/clients/ToggleableClient.java index 198556d..e115ddf 100644 --- a/src/mightypork/utils/patterns/subscription/clients/ToggleableClient.java +++ b/src/mightypork/utils/patterns/subscription/clients/ToggleableClient.java @@ -1,16 +1,15 @@ package mightypork.utils.patterns.subscription.clients; - /** * Client that can toggle receiving messages. * * @author MightyPork */ public interface ToggleableClient { - + /** * @return true if the client wants to receive messages */ public boolean doesSubscribe(); - + } diff --git a/src/mightypork/utils/string/StringUtils.java b/src/mightypork/utils/string/StringUtils.java index 4221b64..f74469d 100644 --- a/src/mightypork/utils/string/StringUtils.java +++ b/src/mightypork/utils/string/StringUtils.java @@ -1,19 +1,21 @@ package mightypork.utils.string; - /** * General purpose string utilities * * @author MightyPork */ public class StringUtils { - + /** * Get if string is in array * - * @param needle checked string - * @param case_sensitive case sensitive comparision - * @param haystack array of possible values + * @param needle + * checked string + * @param case_sensitive + * case sensitive comparision + * @param haystack + * array of possible values * @return is in array */ public static boolean isInArray(String needle, boolean case_sensitive, String... haystack) @@ -30,38 +32,39 @@ public class StringUtils { return false; } } - - + + public static String fromLastDot(String s) { return fromLastChar(s, '.'); } - - + + public static String toLastDot(String s) { return toLastChar(s, '.'); } - - + + public static String fromLastChar(String s, char c) { if (s == null) return null; return s.substring(s.lastIndexOf(c) + 1, s.length()); } - - + + public static String toLastChar(String s, char c) { if (s == null) return null; return s.substring(0, s.lastIndexOf(c)); } - - + + /** * Repeat a string * - * @param repeated string + * @param repeated + * string * @param count * @return output */ @@ -72,37 +75,41 @@ public class StringUtils { s += repeated; return s; } - - + + /** * convert string to a same-length sequence of # marks * - * @param password password + * @param password + * password * @return encoded */ public static String passwordify(String password) { return passwordify(password, "*"); } - - + + /** * convert string to a same-length sequence of chars * - * @param password password - * @param replacing character used in output + * @param password + * password + * @param replacing + * character used in output * @return encoded */ public static String passwordify(String password, String replacing) { return repeat(replacing, password.length()); } - - + + /** * Get ordinal version of numbers (1 = 1st, 5 = 5th etc.) * - * @param number number + * @param number + * number * @return ordinal, string */ public static String numberToOrdinal(int number) @@ -114,12 +121,13 @@ public class StringUtils { } return number + "th"; } - - + + /** * Format number with thousands separated by a dot. * - * @param number number + * @param number + * number * @return string 12.004.225 */ public static String formatInt(long number) @@ -133,35 +141,35 @@ public class StringUtils { if (cnt % 3 == 0 && i > 0) out = dot + out; cnt++; } - + return out; } - - + + public static boolean isValidFilenameChar(char ch) { return isValidFilenameString(Character.toString(ch)); } - - + + public static boolean isValidFilenameString(String filename) { return filename.matches("[a-zA-Z0-9 +\\-.,_%@#!]+"); } - - + + public static boolean isValidIdentifierChar(char ch) { return isValidIdentifierString(Character.toString(ch)); } - - + + public static boolean isValidIdentifierString(String filename) { return filename.matches("[a-zA-Z0-9._]+"); } - - + + public static String ellipsisStart(String orig, int length) { if (orig.length() > length) { @@ -169,8 +177,8 @@ public class StringUtils { } return orig; } - - + + public static String ellipsisEnd(String orig, int length) { if (orig.length() > length) { diff --git a/src/mightypork/utils/string/validation/CharValidator.java b/src/mightypork/utils/string/validation/CharValidator.java index 83fe898..abeb676 100644 --- a/src/mightypork/utils/string/validation/CharValidator.java +++ b/src/mightypork/utils/string/validation/CharValidator.java @@ -1,7 +1,6 @@ package mightypork.utils.string.validation; - public interface CharValidator { - + public boolean isValid(char c); } diff --git a/src/mightypork/utils/string/validation/CharValidatorRegex.java b/src/mightypork/utils/string/validation/CharValidatorRegex.java index 27b128f..cd608e9 100644 --- a/src/mightypork/utils/string/validation/CharValidatorRegex.java +++ b/src/mightypork/utils/string/validation/CharValidatorRegex.java @@ -1,20 +1,19 @@ package mightypork.utils.string.validation; - public class CharValidatorRegex implements CharValidator { - + private String formula; - - + + public CharValidatorRegex(String regex) { this.formula = regex; } - - + + @Override public boolean isValid(char c) { return Character.toString(c).matches(formula); } - + } diff --git a/src/mightypork/utils/string/validation/CharValidatorWhitelist.java b/src/mightypork/utils/string/validation/CharValidatorWhitelist.java index d270479..68a4bf3 100644 --- a/src/mightypork/utils/string/validation/CharValidatorWhitelist.java +++ b/src/mightypork/utils/string/validation/CharValidatorWhitelist.java @@ -1,20 +1,19 @@ package mightypork.utils.string.validation; - public class CharValidatorWhitelist implements CharValidator { - + private String whitelist; - - + + public CharValidatorWhitelist(String allowed) { this.whitelist = allowed; } - - + + @Override public boolean isValid(char c) { return whitelist.contains(Character.toString(c)); } - + } diff --git a/src/mightypork/utils/string/validation/FileSuffixFilter.java b/src/mightypork/utils/string/validation/FileSuffixFilter.java index ccb4713..afaeb5c 100644 --- a/src/mightypork/utils/string/validation/FileSuffixFilter.java +++ b/src/mightypork/utils/string/validation/FileSuffixFilter.java @@ -1,6 +1,5 @@ package mightypork.utils.string.validation; - import java.io.File; import java.io.FileFilter; @@ -11,35 +10,36 @@ import java.io.FileFilter; * @author MightyPork */ public class FileSuffixFilter implements FileFilter { - + /** Array of allowed suffixes */ private String[] suffixes = null; - - + + /** * Suffix filter * - * @param suffixes var-args allowed suffixes, case insensitive + * @param suffixes + * var-args allowed suffixes, case insensitive */ public FileSuffixFilter(String... suffixes) { this.suffixes = suffixes; } - - + + @Override public boolean accept(File pathname) { if (!pathname.isFile()) return false; - + String fname = pathname.getName().toLowerCase().trim(); - + for (String suffix : suffixes) { if (fname.endsWith(suffix.toLowerCase().trim())) { return true; } } - + return false; } - + } diff --git a/src/mightypork/utils/string/validation/FilenameCharValidator.java b/src/mightypork/utils/string/validation/FilenameCharValidator.java index 6907264..03e32e1 100644 --- a/src/mightypork/utils/string/validation/FilenameCharValidator.java +++ b/src/mightypork/utils/string/validation/FilenameCharValidator.java @@ -1,10 +1,9 @@ package mightypork.utils.string.validation; - public class FilenameCharValidator extends CharValidatorRegex { - + public FilenameCharValidator() { super("[a-zA-Z0-9 +\\-.,_%@#$!'\"]"); } - + } diff --git a/src/mightypork/utils/string/validation/StringFilter.java b/src/mightypork/utils/string/validation/StringFilter.java index e06e3f4..4d2140f 100644 --- a/src/mightypork/utils/string/validation/StringFilter.java +++ b/src/mightypork/utils/string/validation/StringFilter.java @@ -1,12 +1,11 @@ package mightypork.utils.string.validation; - /** * Utility interface for string filters (accepting filepaths and similar) * * @author MightyPork */ public interface StringFilter { - + public boolean accept(String entry); } diff --git a/src/mightypork/utils/time/FpsMeter.java b/src/mightypork/utils/time/FpsMeter.java index 90d0431..58f2a4f 100644 --- a/src/mightypork/utils/time/FpsMeter.java +++ b/src/mightypork/utils/time/FpsMeter.java @@ -1,6 +1,5 @@ package mightypork.utils.time; - /** * Class for counting FPS in games.
* This class can be used also as a simple frequency meter - output is in Hz. @@ -8,14 +7,14 @@ package mightypork.utils.time; * @author MightyPork */ public class FpsMeter { - + private long frames = 0; private long drops = 0; private long lastTimeMillis = System.currentTimeMillis(); private long lastSecFPS = 0; private long lastSecDrop = 0; - - + + /** * @return current second's FPS */ @@ -23,8 +22,8 @@ public class FpsMeter { { return lastSecFPS; } - - + + /** * Notification that frame was rendered */ @@ -39,19 +38,20 @@ public class FpsMeter { } frames++; } - - + + /** * Notification that some frames have been dropped * - * @param dropped dropped frames + * @param dropped + * dropped frames */ public void drop(int dropped) { drops += dropped; } - - + + /** * @return current second's dropped frames */ diff --git a/src/mightypork/utils/time/Pauseable.java b/src/mightypork/utils/time/Pauseable.java index 99d3101..8b984a3 100644 --- a/src/mightypork/utils/time/Pauseable.java +++ b/src/mightypork/utils/time/Pauseable.java @@ -1,23 +1,22 @@ package mightypork.utils.time; - public interface Pauseable { - + /** * Pause operation */ public void pause(); - - + + /** * Resume operation */ public void resume(); - - + + /** * @return paused state */ public boolean isPaused(); - + } diff --git a/src/mightypork/utils/time/TimerDelta.java b/src/mightypork/utils/time/TimerDelta.java index 862dca9..8c0bdc5 100644 --- a/src/mightypork/utils/time/TimerDelta.java +++ b/src/mightypork/utils/time/TimerDelta.java @@ -1,26 +1,25 @@ package mightypork.utils.time; - /** * Timer for delta timing * * @author MightyPork */ public class TimerDelta { - + private long lastFrame; - + private static final long SECOND = 1000000000; // a million nanoseconds - - + + /** * New delta timer */ public TimerDelta() { lastFrame = System.nanoTime(); } - - + + /** * Get current time in NS * @@ -30,8 +29,8 @@ public class TimerDelta { { return System.nanoTime(); } - - + + /** * Get time since the last "getDelta()" call. * diff --git a/src/mightypork/utils/time/TimerInterpolating.java b/src/mightypork/utils/time/TimerInterpolating.java index 3cfee74..ab987d7 100644 --- a/src/mightypork/utils/time/TimerInterpolating.java +++ b/src/mightypork/utils/time/TimerInterpolating.java @@ -1,35 +1,35 @@ package mightypork.utils.time; - /** * Timer for interpolated timing * * @author MightyPork */ public class TimerInterpolating { - + private long lastFrame = 0; private long nextFrame = 0; private long skipped = 0; private long lastSkipped = 0; - + private static final long SECOND = 1000000000; // a million nanoseconds private long FRAME; // a time of one frame in nanoseconds - - + + /** * New interpolated timer * - * @param fps target FPS + * @param fps + * target FPS */ public TimerInterpolating(long fps) { FRAME = Math.round(SECOND / fps); - + lastFrame = System.nanoTime(); nextFrame = System.nanoTime() + FRAME; } - - + + /** * Sync and calculate dropped frames etc. */ @@ -38,14 +38,14 @@ public class TimerInterpolating { long time = getTime(); if (time >= nextFrame) { long skippedNow = (long) Math.floor((time - nextFrame) / (double) FRAME) + 1; - //System.out.println("Skipping: "+skippedNow); + // System.out.println("Skipping: "+skippedNow); skipped += skippedNow; lastFrame = nextFrame + (1 - skippedNow) * FRAME; nextFrame += skippedNow * FRAME; } } - - + + /** * Get nanotime * @@ -55,8 +55,8 @@ public class TimerInterpolating { { return System.nanoTime(); } - - + + /** * Get fraction of next frame * @@ -67,17 +67,17 @@ public class TimerInterpolating { if (getSkipped() >= 1) { return 1; } - + long time = getTime(); - + if (time <= nextFrame) { return (double) (time - lastFrame) / (double) FRAME; } - + return 1; } - - + + /** * Get number of elapsed ticks * @@ -89,14 +89,14 @@ public class TimerInterpolating { lastSkipped = skipped; return (int) change; } - - + + /** * Clear timer and start counting new tick. */ public void startNewFrame() { - //System.out.println("! start new frame !"); + // System.out.println("! start new frame !"); long time = getTime(); lastFrame = time; nextFrame = time + FRAME; diff --git a/src/mightypork/utils/time/Updateable.java b/src/mightypork/utils/time/Updateable.java index 6512069..983f08b 100644 --- a/src/mightypork/utils/time/Updateable.java +++ b/src/mightypork/utils/time/Updateable.java @@ -1,17 +1,17 @@ package mightypork.utils.time; - /** * object supporting delta timing * * @author MightyPork */ public interface Updateable { - + /** * Update item state based on elapsed time * - * @param delta time elapsed since last update, in seconds + * @param delta + * time elapsed since last update, in seconds */ public void update(double delta); } diff --git a/src/mightypork/utils/time/animation/AnimDouble.java b/src/mightypork/utils/time/animation/AnimDouble.java index 0860c42..e47e338 100644 --- a/src/mightypork/utils/time/animation/AnimDouble.java +++ b/src/mightypork/utils/time/animation/AnimDouble.java @@ -1,6 +1,5 @@ package mightypork.utils.time.animation; - import mightypork.utils.math.Calc; import mightypork.utils.math.easing.Easing; import mightypork.utils.time.Pauseable; @@ -13,58 +12,62 @@ import mightypork.utils.time.Updateable; * @author MightyPork */ public class AnimDouble implements Updateable, Pauseable { - + /** target double */ protected double to = 0; - + /** last tick double */ protected double from = 0; - + /** how long the transition should last */ protected double duration = 0; - + /** current anim time */ protected double elapsedTime = 0; - + /** True if this animator is paused */ protected boolean paused = false; - + /** Easing fn */ protected Easing easing = Easing.LINEAR; - - + + /** * Create linear animator * - * @param value initial value + * @param value + * initial value */ public AnimDouble(double value) { setTo(value); } - - + + /** * Create animator with easing * - * @param value initial value - * @param easing easing function + * @param value + * initial value + * @param easing + * easing function */ public AnimDouble(double value, Easing easing) { setTo(value); setEasing(easing); } - - + + /** * Create as copy of another * - * @param other other animator + * @param other + * other animator */ public AnimDouble(AnimDouble other) { setTo(other); } - - + + /** * @return easing function */ @@ -72,17 +75,18 @@ public class AnimDouble implements Updateable, Pauseable { { return easing; } - - + + /** - * @param easing easing function + * @param easing + * easing function */ public void setEasing(Easing easing) { this.easing = easing; } - - + + /** * Get start value * @@ -92,8 +96,8 @@ public class AnimDouble implements Updateable, Pauseable { { return from; } - - + + /** * Get end value * @@ -103,8 +107,8 @@ public class AnimDouble implements Updateable, Pauseable { { return to; } - - + + /** * Get value at delta time * @@ -115,8 +119,8 @@ public class AnimDouble implements Updateable, Pauseable { if (duration == 0) return to; return Calc.interpolate(from, to, (elapsedTime / duration), easing); } - - + + /** * Get how much of the animation is already finished * @@ -127,13 +131,13 @@ public class AnimDouble implements Updateable, Pauseable { if (duration == 0) return 1; return elapsedTime / duration; } - - + + @Override public void update(double delta) { if (paused) return; - + elapsedTime = Calc.clampd(elapsedTime + delta, 0, duration); if (isFinished()) { duration = 0; @@ -141,8 +145,8 @@ public class AnimDouble implements Updateable, Pauseable { from = to; } } - - + + /** * Get if animation is finished * @@ -152,8 +156,8 @@ public class AnimDouble implements Updateable, Pauseable { { return duration == 0 || elapsedTime >= duration; } - - + + /** * Set to a value (without animation) * @@ -165,8 +169,8 @@ public class AnimDouble implements Updateable, Pauseable { elapsedTime = 0; duration = 0; } - - + + /** * Copy other * @@ -181,86 +185,93 @@ public class AnimDouble implements Updateable, Pauseable { this.paused = other.paused; this.easing = other.easing; } - - + + /** * Animate between two states, start from current value (if it's in between) * - * @param from start value - * @param to target state - * @param time animation time (secs) + * @param from + * start value + * @param to + * target state + * @param time + * animation time (secs) */ public void animate(double from, double to, double time) { double current = getCurrentValue(); - + this.from = from; this.to = to; - + double progress = getProgressFromValue(current); - + this.from = (progress > 0 ? current : from); - + this.duration = time * (1 - progress); this.elapsedTime = 0; } - - + + protected double getProgressFromValue(double value) { double p = 0; - + if (from == to) return 0; - + if (value >= from && value <= to) { // up p = ((value - from) / (to - from)); } else if (value >= to && value <= from) { // down p = ((from - value) / (from - to)); } - + return p; } - - + + /** * Animate to a value from curretn value * - * @param to target state - * @param time animation time (secs) + * @param to + * target state + * @param time + * animation time (secs) */ public void fadeTo(double to, double time) { double current = getCurrentValue(); - + this.from = current; this.to = to; this.duration = time; this.elapsedTime = 0; } - - + + /** * Animate 0 to 1 * - * @param time animation time (secs) + * @param time + * animation time (secs) */ public void fadeIn(double time) { animate(0, 1, time); } - - + + /** * Animate 1 to 0 * - * @param time animation time (secs) + * @param time + * animation time (secs) */ public void fadeOut(double time) { animate(1, 0, time); } - - + + /** * Make a copy * @@ -270,15 +281,15 @@ public class AnimDouble implements Updateable, Pauseable { { return new AnimDouble(this); } - - + + @Override public String toString() { return "Animation(" + from + " -> " + to + ", t=" + duration + "s, elapsed=" + elapsedTime + "s)"; } - - + + /** * Set to zero and stop animation */ @@ -289,8 +300,8 @@ public class AnimDouble implements Updateable, Pauseable { duration = 0; paused = false; } - - + + /** * Stop animation, keep current value */ @@ -300,22 +311,22 @@ public class AnimDouble implements Updateable, Pauseable { elapsedTime = 0; duration = 0; } - - + + @Override public void pause() { paused = true; } - - + + @Override public void resume() { paused = false; } - - + + @Override public boolean isPaused() { diff --git a/src/mightypork/utils/time/animation/AnimDoubleDeg.java b/src/mightypork/utils/time/animation/AnimDoubleDeg.java index d281d5e..5c86cf0 100644 --- a/src/mightypork/utils/time/animation/AnimDoubleDeg.java +++ b/src/mightypork/utils/time/animation/AnimDoubleDeg.java @@ -1,6 +1,5 @@ package mightypork.utils.time.animation; - import mightypork.utils.math.Calc; import mightypork.utils.math.Calc.Deg; import mightypork.utils.math.easing.Easing; @@ -12,30 +11,30 @@ import mightypork.utils.math.easing.Easing; * @author MightyPork */ public class AnimDoubleDeg extends AnimDouble { - + public AnimDoubleDeg(AnimDouble other) { super(other); } - - + + public AnimDoubleDeg(double value) { super(value); } - - + + public AnimDoubleDeg(double value, Easing easing) { super(value, easing); } - - + + @Override public double getCurrentValue() { if (duration == 0) return Deg.norm(to); return Calc.interpolateDeg(from, to, (elapsedTime / duration), easing); } - - + + @Override protected double getProgressFromValue(double value) { @@ -44,7 +43,7 @@ public class AnimDoubleDeg extends AnimDouble { double partial = Deg.diff(from, value); return partial / whole; } - + return 0; } } diff --git a/src/mightypork/utils/time/animation/AnimDoubleRad.java b/src/mightypork/utils/time/animation/AnimDoubleRad.java index 3787571..73c73ba 100644 --- a/src/mightypork/utils/time/animation/AnimDoubleRad.java +++ b/src/mightypork/utils/time/animation/AnimDoubleRad.java @@ -1,6 +1,5 @@ package mightypork.utils.time.animation; - import mightypork.utils.math.Calc; import mightypork.utils.math.Calc.Rad; import mightypork.utils.math.easing.Easing; @@ -12,30 +11,30 @@ import mightypork.utils.math.easing.Easing; * @author MightyPork */ public class AnimDoubleRad extends AnimDouble { - + public AnimDoubleRad(AnimDouble other) { super(other); } - - + + public AnimDoubleRad(double value) { super(value); } - - + + public AnimDoubleRad(double value, Easing easing) { super(value, easing); } - - + + @Override public double getCurrentValue() { if (duration == 0) return Rad.norm(to); return Calc.interpolateRad(from, to, (elapsedTime / duration), easing); } - - + + @Override protected double getProgressFromValue(double value) { @@ -44,7 +43,7 @@ public class AnimDoubleRad extends AnimDouble { double partial = Rad.diff(from, value); return partial / whole; } - + return 0; } }