Ondřej Hruška 11 years ago
commit d8530f2f1a
  1. 24
      .classpath
  2. 133
      src/mightypork/rogue/App.java
  3. 21
      src/mightypork/rogue/AppAccess.java
  4. 31
      src/mightypork/rogue/AppAdapter.java
  5. 43
      src/mightypork/rogue/Config.java
  6. 9
      src/mightypork/rogue/Const.java
  7. 5
      src/mightypork/rogue/CrashHandler.java
  8. 9
      src/mightypork/rogue/Paths.java
  9. 78
      src/mightypork/rogue/bus/DelegatingBusClient.java
  10. 8
      src/mightypork/rogue/bus/SimpleBusClient.java
  11. 13
      src/mightypork/rogue/bus/UpdateReceiver.java
  12. 41
      src/mightypork/rogue/bus/events/KeyboardEvent.java
  13. 61
      src/mightypork/rogue/bus/events/MouseButtonEvent.java
  14. 29
      src/mightypork/rogue/bus/events/MouseMotionEvent.java
  15. 28
      src/mightypork/rogue/bus/events/ScreenChangeEvent.java
  16. 20
      src/mightypork/rogue/bus/events/UpdateEvent.java
  17. 80
      src/mightypork/rogue/display/DisplaySystem.java
  18. 136
      src/mightypork/rogue/display/Screen.java
  19. 55
      src/mightypork/rogue/display/ScreenTestAnimations.java
  20. 3
      src/mightypork/rogue/display/constraints/Bounding.java
  21. 29
      src/mightypork/rogue/display/constraints/Constraint.java
  22. 5
      src/mightypork/rogue/display/rendering/Renderable.java
  23. 9
      src/mightypork/rogue/display/rendering/ScreenLayer.java
  24. 5
      src/mightypork/rogue/fonts/Align.java
  25. 283
      src/mightypork/rogue/fonts/FontManager.java
  26. 23
      src/mightypork/rogue/fonts/Fonts.java
  27. 316
      src/mightypork/rogue/fonts/LoadedFont.java
  28. 53
      src/mightypork/rogue/input/InputSystem.java
  29. 18
      src/mightypork/rogue/input/KeyBinder.java
  30. 27
      src/mightypork/rogue/input/KeyBinding.java
  31. 28
      src/mightypork/rogue/input/KeyBindingPool.java
  32. 57
      src/mightypork/rogue/input/KeyStroke.java
  33. 157
      src/mightypork/rogue/sounds/AudioX.java
  34. 47
      src/mightypork/rogue/sounds/BaseAudioPlayer.java
  35. 21
      src/mightypork/rogue/sounds/EffectPlayer.java
  36. 20
      src/mightypork/rogue/sounds/JointVolume.java
  37. 81
      src/mightypork/rogue/sounds/LoopPlayer.java
  38. 9
      src/mightypork/rogue/sounds/NullAudio.java
  39. 180
      src/mightypork/rogue/sounds/SoundSystem.java
  40. 25
      src/mightypork/rogue/tasks/TaskTakeScreenshot.java
  41. 47
      src/mightypork/rogue/testing/TestConstraints.java
  42. 123
      src/mightypork/rogue/testing/TestMsgbus.java
  43. 29
      src/mightypork/rogue/textures/TextureManager.java
  44. 15
      src/mightypork/rogue/textures/Textures.java
  45. 15
      src/mightypork/rogue/textures/Tx.java
  46. 55
      src/mightypork/rogue/textures/TxQuad.java
  47. 821
      src/mightypork/rogue/util/RenderUtils.java
  48. 3
      src/mightypork/rogue/util/Utils.java
  49. 92
      src/mightypork/utils/files/FileTreeDiff.java
  50. 313
      src/mightypork/utils/files/FileUtils.java
  51. 74
      src/mightypork/utils/files/OsUtils.java
  52. 491
      src/mightypork/utils/files/PropertyManager.java
  53. 110
      src/mightypork/utils/files/SimpleConfig.java
  54. 73
      src/mightypork/utils/files/ZipBuilder.java
  55. 136
      src/mightypork/utils/files/ZipUtils.java
  56. 36
      src/mightypork/utils/files/ion/AbstractIonList.java
  57. 53
      src/mightypork/utils/files/ion/AbstractIonMap.java
  58. 124
      src/mightypork/utils/files/ion/Ion.java
  59. 17
      src/mightypork/utils/files/ion/IonException.java
  60. 93
      src/mightypork/utils/files/ion/IonList.java
  61. 97
      src/mightypork/utils/files/ion/IonMap.java
  62. 31
      src/mightypork/utils/files/ion/IonMarks.java
  63. 17
      src/mightypork/utils/files/ion/Ionizable.java
  64. 3
      src/mightypork/utils/files/ion/IonizableOptional.java
  65. 139
      src/mightypork/utils/files/ion/StreamUtils.java
  66. 117
      src/mightypork/utils/logging/Log.java
  67. 201
      src/mightypork/utils/logging/LogInstance.java
  68. 7
      src/mightypork/utils/logging/LogMonitor.java
  69. 15
      src/mightypork/utils/logging/LogToSysoutMonitor.java
  70. 577
      src/mightypork/utils/math/Calc.java
  71. 94
      src/mightypork/utils/math/Polar.java
  72. 98
      src/mightypork/utils/math/Range.java
  73. 83
      src/mightypork/utils/math/color/HSV.java
  74. 201
      src/mightypork/utils/math/color/RGB.java
  75. 561
      src/mightypork/utils/math/coord/Coord.java
  76. 41
      src/mightypork/utils/math/coord/CoordAnimated.java
  77. 446
      src/mightypork/utils/math/coord/Rect.java
  78. 161
      src/mightypork/utils/math/easing/Easing.java
  79. 149
      src/mightypork/utils/objects/Convertor.java
  80. 38
      src/mightypork/utils/objects/Mutable.java
  81. 52
      src/mightypork/utils/objects/ObjectUtils.java
  82. 51
      src/mightypork/utils/objects/Pair.java
  83. 54
      src/mightypork/utils/objects/Triad.java
  84. 24
      src/mightypork/utils/objects/VarargsParser.java
  85. 3
      src/mightypork/utils/patterns/Destroyable.java
  86. 9
      src/mightypork/utils/patterns/subscription/Handleable.java
  87. 72
      src/mightypork/utils/patterns/subscription/MessageBus.java
  88. 86
      src/mightypork/utils/patterns/subscription/MessageChannel.java
  89. 9
      src/mightypork/utils/patterns/subscription/clients/DelegatingClient.java
  90. 5
      src/mightypork/utils/patterns/subscription/clients/ToggleableClient.java
  91. 92
      src/mightypork/utils/string/StringUtils.java
  92. 3
      src/mightypork/utils/string/validation/CharValidator.java
  93. 13
      src/mightypork/utils/string/validation/CharValidatorRegex.java
  94. 13
      src/mightypork/utils/string/validation/CharValidatorWhitelist.java
  95. 22
      src/mightypork/utils/string/validation/FileSuffixFilter.java
  96. 5
      src/mightypork/utils/string/validation/FilenameCharValidator.java
  97. 3
      src/mightypork/utils/string/validation/StringFilter.java
  98. 22
      src/mightypork/utils/time/FpsMeter.java
  99. 13
      src/mightypork/utils/time/Pauseable.java
  100. 17
      src/mightypork/utils/time/TimerDelta.java
  101. Some files were not shown because too many files have changed in this diff Show More

@ -2,34 +2,34 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/jinput.jar" sourcepath="lib/lwjgl-source-2.8.4.zip">
<classpathentry kind="lib" path="lib/jinput.jar" sourcepath="lib">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="PR2-game/lib"/>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="Rogue/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/jogg-0.0.7.jar">
<classpathentry kind="lib" path="lib/jogg-0.0.7.jar" sourcepath="lib">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="PR2-game/lib"/>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="Rogue/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/jorbis-0.0.15.jar" sourcepath="lib/lwjgl-source-2.8.4.zip">
<classpathentry kind="lib" path="lib/jorbis-0.0.15.jar" sourcepath="lib">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="PR2-game/lib"/>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="Rogue/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/lwjgl_util.jar" sourcepath="lib/lwjgl-source-2.8.4.zip">
<classpathentry kind="lib" path="lib/lwjgl_util.jar" sourcepath="lib">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="PR2-game/lib"/>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="Rogue/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/lwjgl.jar" sourcepath="lib/lwjgl-source-2.8.4.zip">
<classpathentry kind="lib" path="lib/lwjgl.jar" sourcepath="lib">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="PR2-game/lib"/>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="Rogue/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/slick-util.jar" sourcepath="lib/slick-util-src.zip">
<classpathentry kind="lib" path="lib/slick-util.jar" sourcepath="lib">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="PR2-game/lib"/>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="Rogue/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>

@ -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;
}
}

@ -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<br>
* Destroy app & exit VM
*/
abstract void shutdown();
}

@ -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();
}
}

@ -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;
}

@ -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;

@ -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);
}
}

@ -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/";
}

@ -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<Object> childSubscribers = new HashSet<Object>();
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}.<br>
*
@ -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<Object> getChildClients()
{
return childSubscribers;
}
@Override
public final boolean doesDelegate()
{
return doesSubscribe();
}
/**
* Set whether to receive {@link UpdateEvent}s (delta timing, one each
* frame).<br>
@ -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<br>
* (called during construction)
@ -148,8 +148,8 @@ public abstract class DelegatingBusClient extends AppAdapter implements Delegati
{
// no impl
}
/**
* Deinitialize the subsystem<br>
* (called during destruction)
@ -158,5 +158,5 @@ public abstract class DelegatingBusClient extends AppAdapter implements Delegati
{
// no impl
}
}

@ -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);
}
}

@ -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);
}

@ -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<KeyboardEvent.Listener> {
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<KeyboardEvent.Listener> {
{
return key;
}
/**
* @return true if key was just pressed
*/
@ -41,8 +40,8 @@ public class KeyboardEvent implements Handleable<KeyboardEvent.Listener> {
{
return down;
}
/**
* @return true if key was just released
*/
@ -50,8 +49,8 @@ public class KeyboardEvent implements Handleable<KeyboardEvent.Listener> {
{
return !down;
}
/**
* @return event character (if any)
*/
@ -59,29 +58,31 @@ public class KeyboardEvent implements Handleable<KeyboardEvent.Listener> {
{
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");
}
}

@ -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<MouseButtonEvent.Listener> {
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<MouseButtonEvent.Listener> {
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<MouseButtonEvent.Listener> {
{
return button != -1;
}
/**
* @return true if the event was caused by a wheel change
*/
@ -54,8 +57,8 @@ public class MouseButtonEvent implements Handleable<MouseButtonEvent.Listener> {
{
return wheeld != 0;
}
/**
* @return button id or -1 if none was pressed
*/
@ -63,8 +66,8 @@ public class MouseButtonEvent implements Handleable<MouseButtonEvent.Listener> {
{
return button;
}
/**
* @return number of steps the wheel changed since last event
*/
@ -72,8 +75,8 @@ public class MouseButtonEvent implements Handleable<MouseButtonEvent.Listener> {
{
return wheeld;
}
/**
* @return mouse position when the event occurred
*/
@ -81,8 +84,8 @@ public class MouseButtonEvent implements Handleable<MouseButtonEvent.Listener> {
{
return pos;
}
/**
* @return true if button was just pressed
*/
@ -90,8 +93,8 @@ public class MouseButtonEvent implements Handleable<MouseButtonEvent.Listener> {
{
return button != -1 && down;
}
/**
* @return true if button was just released
*/
@ -99,20 +102,22 @@ public class MouseButtonEvent implements Handleable<MouseButtonEvent.Listener> {
{
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);
}

@ -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<MouseMotionEvent.Listener> {
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<MouseMotionEvent.Listener> {
{
return move;
}
/**
* @return current mouse position
*/
@ -33,22 +32,24 @@ public class MouseMotionEvent implements Handleable<MouseMotionEvent.Listener> {
{
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);
}
}

@ -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<ScreenChangeEvent.Listener> {
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);
}
}

@ -1,33 +1,33 @@
package mightypork.rogue.bus.events;
import mightypork.utils.patterns.subscription.Handleable;
public class UpdateEvent implements Handleable<UpdateEvent.Listener> {
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);
}
}

@ -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()
{

@ -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.<br>
* 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();
}
}

@ -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);
}
}
}

@ -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
*/

@ -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();
}

@ -1,8 +1,7 @@
package mightypork.rogue.display.rendering;
public interface Renderable {
public void render();
}

@ -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();
}

@ -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;
}

@ -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<Style, String> {
}
/**
* Table of font files. name {style:file,style:file,style:file...}
*/
private static HashMap<String, FontFamily> fontFiles = new HashMap<String, FontFamily>();
/**
* 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.<br>
* 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.<br>
* 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;
}
}

@ -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);
}
}

@ -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<Character, CharStorageEntry> chars = new HashMap<Character, CharStorageEntry>(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<LoadedGlyph> glyphs = new ArrayList<LoadedGlyph>();
List<Character> loaded = new ArrayList<Character>();
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();
}
}

@ -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();

@ -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);
}

@ -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();
}
}

@ -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<KeyBinding> bindings = new HashSet<KeyBinding>();
/**
* 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<KeyBinding> 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)
{

@ -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<Integer> keys = new LinkedHashSet<Integer>();
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<Integer> 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<Integer> 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<Integer> getKeys()
{
return keys;

@ -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.<br>
* 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;
}
}

@ -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<Double> gainMultiplier = null;
public BaseAudioPlayer(AudioX track, double baseGain, Mutable<Double> gainMultiplier) {
this(track, 1, baseGain, gainMultiplier);
}
public BaseAudioPlayer(AudioX track, double basePitch, double baseGain, Mutable<Double> gainMultiplier) {
this.audio = track;
this.baseGain = baseGain;
this.basePitch = basePitch;
if (gainMultiplier == null) gainMultiplier = new Mutable<Double>(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();

@ -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<Double> 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);
}
}

@ -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<Double> {
private Mutable<Double>[] volumes;
/**
* Create joint volume with master gain of 1
*
* @param volumes individual volumes to join
* @param volumes
* individual volumes to join
*/
public JointVolume(Mutable<Double>... volumes) {
super(1D);
this.volumes = volumes;
}
/**
* Get combined gain (multiplied)
*/
@ -35,11 +35,11 @@ public class JointVolume extends Mutable<Double> {
double d = super.get();
for (Mutable<Double> v : volumes)
d *= v.get();
return Calc.clampd(d, 0, 1);
}
/**
* Set master gain
*/

@ -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<Double> 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);
}
}

@ -1,17 +1,16 @@
package mightypork.rogue.sounds;
public class NullAudio extends AudioX {
public NullAudio() {
super("");
}
@Override
public boolean load()
{
return false;
}
}

@ -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<Double> masterVolume = new Mutable<Double>(1D);
public Mutable<Double> effectsVolume = new JointVolume(masterVolume);
public Mutable<Double> loopsVolume = new JointVolume(masterVolume);
private Map<String, EffectPlayer> effects = new HashMap<String, EffectPlayer>();
private Map<String, LoopPlayer> loops = new HashMap<String, LoopPlayer>();
private Set<AudioX> resources = new HashSet<AudioX>();
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)
{

@ -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());
}
}

@ -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());
}
}

@ -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("<MSG>"));
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<Object> clients = new ArrayList<Object>();
boolean delegating = true;
public Delegator(String name) {
super(name);
}
@Override
public Collection<Object> 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<StringMessage.Listener> {
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<StringMessage.Listener> {
class IntMessage implements Handleable<IntMessage.Listener> {
int i;
IntMessage(int i) {
this.i = i;
}
public interface Listener {
public void receive(IntMessage message);
}
@Override
public void handleBy(Listener handler)
{

@ -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
*/

@ -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();
}
}

@ -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)
}
}

@ -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);

File diff suppressed because it is too large Load Diff

@ -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);

@ -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<Tuple<File>> compared = new ArrayList<Tuple<File>>();
private Comparator<File> fileFirstSorter = new Comparator<File>() {
@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<File> 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<File>(f1, f2));
}
}
private class NotEqualException extends Exception {
public NotEqualException(String msg) {
super(msg);
}
}
private class Tuple<T> {
public T a;
public T b;
public Tuple(T a, T b) {
this.a = a;
this.b = b;
}
}
}

@ -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<File> 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<File> 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<File> 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<File> listDirectory(File dir, FileFilter filter)
@ -207,9 +230,9 @@ public class FileUtils {
} catch (RuntimeException e) {
Log.e("Error creating folder " + dir, e);
}
List<File> list = new ArrayList<File>();
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<File> 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)

@ -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;
}
}

File diff suppressed because it is too large Load Diff

@ -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<String> 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<String, String> 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<String> listFromString(String text)
{
List<String> list = new ArrayList<String>();
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<String, String> mapFromString(String text)
{
LinkedHashMap<String, String> pairs = new LinkedHashMap<String, String>();
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<String, String> data, boolean allowNulls) throws IOException
{
List<String> lines = new ArrayList<String>();
for (Entry<String, String> 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<String> 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);
}
}

@ -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<String> included = new HashSet<String>();
/**
* @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
*

@ -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<String> extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException
{
ArrayList<String> files = new ArrayList<String>();
outputDir.mkdirs();
Enumeration<? extends ZipEntry> 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<String> 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<String> listZip(ZipFile zip) throws IOException
{
ArrayList<String> files = new ArrayList<String>();
Enumeration<? extends ZipEntry> 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
}
}
}
}

@ -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 <T>
*/
public abstract class AbstractIonList<T> extends ArrayList<T> 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<T> extends ArrayList<T> 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<T> extends ArrayList<T> 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();
}

@ -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 <V>
*/
public abstract class AbstractIonMap<V> extends LinkedHashMap<String, V> 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<V> extends LinkedHashMap<String, V> 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<V> extends LinkedHashMap<String, V> 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<V> extends LinkedHashMap<String, V> 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;
}
}

@ -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<Mark, Class> */
private static Map<Byte, Class<?>> customIonizables = new HashMap<Byte, Class<?>>();
// 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);
}
}
}

@ -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);
}
}

@ -1,133 +1,132 @@
package mightypork.utils.files.ion;
/**
* Ionizable Arraylist
*
* @author MightyPork
*/
public class IonList extends AbstractIonList<Object> 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<Object> 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;
}
}

@ -1,156 +1,155 @@
package mightypork.utils.files.ion;
/**
* Ionizable HashMap
*
* @author MightyPork
*/
public class IonMap extends AbstractIonMap<Object> 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;
}
}

@ -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<br>
* 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;
}

@ -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.
*

@ -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
*

@ -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;
}
}

@ -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<String, LogInstance> logs = new HashMap<String, LogInstance>();
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.<br>
* 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);
}
}

@ -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<Integer, LogMonitor> monitors = new HashMap<Integer, LogMonitor>();
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<File> 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<File>() {
@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)
*/

@ -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);
}

@ -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;
}
}

File diff suppressed because it is too large Load Diff

@ -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)
{

@ -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;
}
}

@ -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
*

@ -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();
}
}

File diff suppressed because it is too large Load Diff

@ -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));
}
}

@ -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
*/

@ -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);
}

@ -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<br>
* 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());
}
}

@ -1,28 +1,29 @@
package mightypork.utils.objects;
/**
* Mutable object
*
* @author MightyPork
* @param <T> type
* @param <T>
* type
*/
public class Mutable<T> {
/** 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<T> {
{
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<T> {
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<T> {
}
return true;
}
@Override
public String toString()
{

@ -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 <K extends Comparable, V extends Comparable> Map<K, V> sortByKeys(Map<K, V> map, final Comparator<K> comparator)
{
List<K> keys = new LinkedList<K>(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<K, V> sortedMap = new LinkedHashMap<K, V>();
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 <K extends Comparable, V extends Comparable> Map<K, V> sortByValues(Map<K, V> map, final Comparator<V> comparator)
{
List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {
@SuppressWarnings("unchecked")
@Override
public int compare(Entry<K, V> o1, Entry<K, V> 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<K, V> sortedMap = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> 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 <T extends Object> List<T> arrayToList(T[] objs)
{
ArrayList<T> list = new ArrayList<T>();

@ -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 <T1> 1st object class
* @param <T2> 2nd object class
* @param <T1>
* 1st object class
* @param <T2>
* 2nd object class
*/
public class Pair<T1, T2> {
/**
* 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<T1, T2> {
{
return first;
}
/**
* @return 2nd object
*/
@ -53,26 +56,26 @@ public class Pair<T1, T2> {
{
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<T1, T2> {
hash += (second == null ? 0 : second.hashCode());
return hash;
}
@Override
public String toString()
{
return "PAIR{" + first + "," + second + "}";
}
}

@ -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 <T1> 1st object class
* @param <T2> 2nd object class
* @param <T3> 3rd object class
* @param <T1>
* 1st object class
* @param <T2>
* 2nd object class
* @param <T3>
* 3rd object class
*/
public class Triad<T1, T2, T3> extends Pair<T1, T2> {
/**
* 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<T1, T2, T3> extends Pair<T1, T2> {
{
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 + "}";
}
}

@ -1,6 +1,5 @@
package mightypork.utils.objects;
import java.util.LinkedHashMap;
import java.util.Map;
@ -26,28 +25,33 @@ import java.util.Map;
* </pre>
*
* @author MightyPork
* @param <K> Type for Map keys
* @param <V> Type for Map values
* @param <K>
* Type for Map keys
* @param <V>
* Type for Map values
*/
public class VarargsParser<K, V> {
/**
* 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<K, V> parse(Object... args) throws ClassCastException, IllegalArgumentException
{
LinkedHashMap<K, V> attrs = new LinkedHashMap<K, V>();
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<K, V> {
key = null;
}
}
return attrs;
}
}

@ -1,13 +1,12 @@
package mightypork.utils.patterns;
/**
* Object that can be destroyed (free resources etc)
*
* @author MightyPork
*/
public interface Destroyable {
/**
* Destroy this object
*/

@ -1,18 +1,19 @@
package mightypork.utils.patterns.subscription;
/**
* Something that can be handled by HANDLER.
*
* @author MightyPork
* @param <HANDLER> handler type
* @param <HANDLER>
* handler type
*/
public interface Handleable<HANDLER> {
/**
* Ask handler to handle this message.
*
* @param handler handler instance
* @param handler
* handler instance
*/
public void handleBy(HANDLER handler);
}

@ -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<MessageChannel<?, ?>> channels = new LinkedHashSet<MessageChannel<?, ?>>();
private Collection<Object> clients = new LinkedHashSet<Object>();
private boolean warn_unsent = true;
/**
* Add a {@link MessageChannel} to this bus.<br>
* 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_MESSAGE extends Handleable<F_CLIENT>, F_CLIENT> MessageChannel<?, ?> createChannel(Class<F_MESSAGE> messageClass, Class<F_CLIENT> clientClass)
@ -126,5 +132,5 @@ final public class MessageBus {
MessageChannel<F_MESSAGE, F_CLIENT> bc = new MessageChannel<F_MESSAGE, F_CLIENT>(messageClass, clientClass);
return addChannel(bc);
}
}

@ -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> message type
* @param <CLIENT> client (subscriber) type
* @param <MESSAGE>
* message type
* @param <CLIENT>
* client (subscriber) type
*/
final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
private Class<CLIENT> clientClass;
private Class<MESSAGE> messageClass;
public MessageChannel(Class<MESSAGE> messageClass, Class<CLIENT> 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.<br>
* If message is of wrong type, <code>false</code> 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<Object> clients)
{
if (!canBroadcast(message)) return false;
MESSAGE evt = messageClass.cast(message);
doBroadcast(evt, clients, new HashSet<Object>());
return true;
}
private void doBroadcast(MESSAGE message, Collection<Object> clients, Collection<Object> 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<MESSAGE extends Handleable<CLIENT>, 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<MESSAGE extends Handleable<CLIENT>, CLIENT> {
((Handleable<CLIENT>) 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<MESSAGE extends Handleable<CLIENT>, 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<MESSAGE extends Handleable<CLIENT>, 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_MESSAGE extends Handleable<F_CLIENT>, F_CLIENT> MessageChannel<F_MESSAGE, F_CLIENT> create(Class<F_MESSAGE> messageClass, Class<F_CLIENT> clientClass)
{
return new MessageChannel<F_MESSAGE, F_CLIENT>(messageClass, clientClass);
}
}

@ -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<Object> getChildClients();
/**
* @return true if delegating is active
*/
public boolean doesDelegate();
}

@ -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();
}

@ -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) {

@ -1,7 +1,6 @@
package mightypork.utils.string.validation;
public interface CharValidator {
public boolean isValid(char c);
}

@ -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);
}
}

@ -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));
}
}

@ -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;
}
}

@ -1,10 +1,9 @@
package mightypork.utils.string.validation;
public class FilenameCharValidator extends CharValidatorRegex {
public FilenameCharValidator() {
super("[a-zA-Z0-9 +\\-.,_%@#$!'\"]");
}
}

@ -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);
}

@ -1,6 +1,5 @@
package mightypork.utils.time;
/**
* Class for counting FPS in games.<br>
* 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
*/

@ -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();
}

@ -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.
*

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save