Cleaned up coord, rect and constraint system.

v5stable
Ondřej Hruška 10 years ago
parent 65bfbbd16a
commit 4221d5430a
  1. 18
      src/mightypork/gamecore/audio/SoundSystem.java
  2. 2
      src/mightypork/gamecore/control/bus/EventBus.java
  3. 12
      src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java
  4. 2
      src/mightypork/gamecore/control/timing/TimerFps.java
  5. 8
      src/mightypork/gamecore/gui/components/PluggableRenderable.java
  6. 4
      src/mightypork/gamecore/gui/components/PluggableRenderer.java
  7. 4
      src/mightypork/gamecore/gui/components/layout/ElementHolder.java
  8. 7
      src/mightypork/gamecore/gui/components/painters/TextPainter.java
  9. 4
      src/mightypork/gamecore/gui/screens/Screen.java
  10. 6
      src/mightypork/gamecore/gui/screens/ScreenLayer.java
  11. 29
      src/mightypork/gamecore/input/InputSystem.java
  12. 17
      src/mightypork/gamecore/render/DisplaySystem.java
  13. 8
      src/mightypork/gamecore/render/Render.java
  14. 12
      src/mightypork/gamecore/render/fonts/FontRenderer.java
  15. 4
      src/mightypork/gamecore/render/fonts/GLFont.java
  16. 7
      src/mightypork/gamecore/render/fonts/impl/CachedFont.java
  17. 31
      src/mightypork/gamecore/render/fonts/impl/DeferredFont.java
  18. 4
      src/mightypork/gamecore/render/fonts/impl/NullFont.java
  19. 4
      src/mightypork/gamecore/render/textures/TxQuad.java
  20. 4
      src/mightypork/rogue/screens/test_bouncyboxes/LayerBouncyBoxes.java
  21. 16
      src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java
  22. 7
      src/mightypork/test/TestConstr.java
  23. 4
      src/mightypork/utils/config/PropertyManager.java
  24. 8
      src/mightypork/utils/files/FileUtils.java
  25. 2
      src/mightypork/utils/files/InstanceLock.java
  26. 14
      src/mightypork/utils/files/ZipBuilder.java
  27. 12
      src/mightypork/utils/files/ZipUtils.java
  28. 13
      src/mightypork/utils/files/ion/BinaryUtils.java
  29. 2
      src/mightypork/utils/files/ion/Ion.java
  30. 6
      src/mightypork/utils/logging/ArchivingLog.java
  31. 27
      src/mightypork/utils/logging/Log.java
  32. 91
      src/mightypork/utils/logging/SimpleLog.java
  33. 14
      src/mightypork/utils/math/Polar.java
  34. 135
      src/mightypork/utils/math/constraints/Constraints.java
  35. 6
      src/mightypork/utils/math/constraints/ContextAdapter.java
  36. 25
      src/mightypork/utils/math/constraints/FixedNumberConstraint.java
  37. 19
      src/mightypork/utils/math/constraints/NumberConstraint.java
  38. 6
      src/mightypork/utils/math/constraints/PluggableRect.java
  39. 12
      src/mightypork/utils/math/constraints/RectCache.java
  40. 4
      src/mightypork/utils/math/constraints/RectConstraint.java
  41. 33
      src/mightypork/utils/math/constraints/VecConstraintSynth.java
  42. 188
      src/mightypork/utils/math/coord/AbstractVec.java
  43. 68
      src/mightypork/utils/math/coord/AbstractVecProxy.java
  44. 155
      src/mightypork/utils/math/coord/AnimCoord.java
  45. 18
      src/mightypork/utils/math/coord/ConstVec.java
  46. 58
      src/mightypork/utils/math/coord/CoordProxy.java
  47. 86
      src/mightypork/utils/math/coord/MutableCoord.java
  48. 9
      src/mightypork/utils/math/coord/NumConstrVec.java
  49. 25
      src/mightypork/utils/math/coord/SynthCoord2D.java
  50. 23
      src/mightypork/utils/math/coord/SynthCoord3D.java
  51. 153
      src/mightypork/utils/math/coord/Synths.java
  52. 90
      src/mightypork/utils/math/coord/Vec.java
  53. 217
      src/mightypork/utils/math/coord/VecMath.java
  54. 398
      src/mightypork/utils/math/coord/VecMathImpl.java
  55. 144
      src/mightypork/utils/math/coord/VecMutable.java
  56. 192
      src/mightypork/utils/math/coord/VecMutableAnim.java
  57. 46
      src/mightypork/utils/math/coord/VecMutableImpl.java
  58. 32
      src/mightypork/utils/math/coord/VecProxy.java
  59. 161
      src/mightypork/utils/math/coord/VecView.java
  60. 93
      src/mightypork/utils/math/rect/AbstractRect.java
  61. 47
      src/mightypork/utils/math/rect/ConstRect.java
  62. 105
      src/mightypork/utils/math/rect/FixedRect.java
  63. 233
      src/mightypork/utils/math/rect/MutableRect.java
  64. 28
      src/mightypork/utils/math/rect/Rect.java
  65. 51
      src/mightypork/utils/math/rect/RectMath.java
  66. 158
      src/mightypork/utils/math/rect/RectMutable.java
  67. 120
      src/mightypork/utils/math/rect/RectMutableImpl.java
  68. 2
      src/mightypork/utils/math/rect/RectProxy.java
  69. 138
      src/mightypork/utils/math/rect/RectValue.java
  70. 69
      src/mightypork/utils/math/rect/RectView.java
  71. 13
      src/mightypork/utils/objects/Convert.java
  72. 21
      src/mightypork/utils/objects/Mutable.java

@ -12,7 +12,6 @@ import mightypork.gamecore.control.bus.clients.RootBusNode;
import mightypork.gamecore.control.bus.events.ResourceLoadRequest; import mightypork.gamecore.control.bus.events.ResourceLoadRequest;
import mightypork.gamecore.control.timing.Updateable; import mightypork.gamecore.control.timing.Updateable;
import mightypork.utils.math.Calc.Buffers; import mightypork.utils.math.Calc.Buffers;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable; import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
@ -32,9 +31,8 @@ public class SoundSystem extends RootBusNode implements Updateable {
private static final Vec INITIAL_LISTENER_POS = Vec.ZERO; private static final Vec INITIAL_LISTENER_POS = Vec.ZERO;
private static final int MAX_SOURCES = 256; private static final int MAX_SOURCES = 256;
private static VecMutable listener = new MutableCoord(0, 0, 0); private static VecMutable listener = VecMutable.zero();
private static boolean soundSystemInited = false;
private static boolean inited;
/** /**
@ -45,10 +43,10 @@ public class SoundSystem extends RootBusNode implements Updateable {
public static void setListener(Vec pos) public static void setListener(Vec pos)
{ {
listener.setTo(pos); listener.setTo(pos);
FloatBuffer buf3 = Buffers.alloc(3); final FloatBuffer buf3 = Buffers.alloc(3);
FloatBuffer buf6 = Buffers.alloc(6); final FloatBuffer buf6 = Buffers.alloc(6);
buf3.clear(); buf3.clear();
Buffers.fill(buf3, pos.xf(), pos.yf(), pos.zf()); Buffers.fill(buf3, (float) pos.x(), (float) pos.y(), (float) pos.z());
AL10.alListener(AL10.AL_POSITION, buf3); AL10.alListener(AL10.AL_POSITION, buf3);
buf3.clear(); buf3.clear();
Buffers.fill(buf3, 0, 0, 0); Buffers.fill(buf3, 0, 0, 0);
@ -56,7 +54,6 @@ public class SoundSystem extends RootBusNode implements Updateable {
buf6.clear(); buf6.clear();
Buffers.fill(buf6, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f); Buffers.fill(buf6, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
AL10.alListener(AL10.AL_ORIENTATION, buf6); AL10.alListener(AL10.AL_ORIENTATION, buf6);
buf3 = buf6 = null;
} }
@ -84,13 +81,12 @@ public class SoundSystem extends RootBusNode implements Updateable {
public SoundSystem(AppAccess app) { public SoundSystem(AppAccess app) {
super(app); super(app);
if (!inited) { if (!soundSystemInited) {
SoundStore.get().setMaxSources(MAX_SOURCES); SoundStore.get().setMaxSources(MAX_SOURCES);
SoundStore.get().init(); SoundStore.get().init();
setListener(INITIAL_LISTENER_POS); setListener(INITIAL_LISTENER_POS);
inited = true; soundSystemInited = true;
} }
} }

@ -293,7 +293,7 @@ final public class EventBus implements Destroyable {
@Override @Override
public int compareTo(Delayed o) public int compareTo(Delayed o)
{ {
return -Long.valueOf(o.getDelay(TimeUnit.MILLISECONDS)).compareTo(getDelay(TimeUnit.MILLISECONDS)); return Long.valueOf(getDelay(TimeUnit.MILLISECONDS)).compareTo(o.getDelay(TimeUnit.MILLISECONDS));
} }

@ -14,8 +14,8 @@ import mightypork.utils.math.coord.VecView;
@UnloggedEvent @UnloggedEvent
public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> { public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
private final Vec move; private final VecView move;
private final Vec pos; private final VecView pos;
/** /**
@ -23,8 +23,8 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
* @param move move vector * @param move move vector
*/ */
public MouseMotionEvent(Vec pos, Vec move) { public MouseMotionEvent(Vec pos, Vec move) {
this.move = move; this.move = move.value();
this.pos = pos; this.pos = pos.value();
} }
@ -33,7 +33,7 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
*/ */
public VecView getMove() public VecView getMove()
{ {
return move.view(); return move;
} }
@ -42,7 +42,7 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
*/ */
public VecView getPos() public VecView getPos()
{ {
return pos.view(); return pos;
} }

@ -23,7 +23,7 @@ public class TimerFps {
* @param fps target FPS * @param fps target FPS
*/ */
public TimerFps(long fps) { public TimerFps(long fps) {
FRAME = Math.round(SECOND / fps); FRAME = Math.round(SECOND / (double) fps);
lastFrame = System.nanoTime(); lastFrame = System.nanoTime();
nextFrame = System.nanoTime() + FRAME; nextFrame = System.nanoTime() + FRAME;

@ -1,9 +1,9 @@
package mightypork.gamecore.gui.components; package mightypork.gamecore.gui.components;
import mightypork.utils.math.constraints.PluggableContext; import mightypork.utils.math.constraints.PluggableRect;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -11,14 +11,14 @@ import mightypork.utils.math.rect.RectView;
* *
* @author MightyPork * @author MightyPork
*/ */
public interface PluggableRenderable extends Renderable, PluggableContext { public interface PluggableRenderable extends Renderable, PluggableRect {
@Override @Override
void render(); void render();
@Override @Override
RectView getRect(); RectValue getRect();
@Override @Override

@ -3,7 +3,7 @@ package mightypork.gamecore.gui.components;
import mightypork.utils.math.constraints.ContextAdapter; import mightypork.utils.math.constraints.ContextAdapter;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -18,7 +18,7 @@ public abstract class PluggableRenderer extends ContextAdapter implements Plugga
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return super.getRect(); return super.getRect();
} }

@ -10,7 +10,7 @@ import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.gui.components.PluggableRenderer;
import mightypork.gamecore.gui.components.Renderable; import mightypork.gamecore.gui.components.Renderable;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -60,7 +60,7 @@ public abstract class ElementHolder extends BusNode implements PluggableRenderab
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return context.getRect(); return context.getRect();
} }

@ -6,10 +6,9 @@ import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.fonts.FontRenderer.Align; import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.gamecore.render.fonts.GLFont; import mightypork.gamecore.render.fonts.GLFont;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable; import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
import mightypork.utils.string.StringProvider; import mightypork.utils.string.StringProvider;
import mightypork.utils.string.StringProvider.StringWrapper; import mightypork.utils.string.StringProvider.StringWrapper;
@ -30,7 +29,7 @@ public class TextPainter extends PluggableRenderer {
private boolean shadow; private boolean shadow;
private RGB shadowColor = RGB.BLACK; private RGB shadowColor = RGB.BLACK;
private final VecMutable shadowOffset = new MutableCoord(1, 1); private final VecMutable shadowOffset = VecMutable.make(1, 1);
/** /**
@ -86,7 +85,7 @@ public class TextPainter extends PluggableRenderer {
if (text == null) return; if (text == null) return;
final String str = text.getString(); final String str = text.getString();
final RectView rect = getRect(); final RectValue rect = getRect();
if (shadow) { if (shadow) {
font.draw(str, rect.move(shadowOffset), align, shadowColor); font.draw(str, rect.move(shadowOffset), align, shadowColor);

@ -12,7 +12,7 @@ import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.render.Render; import mightypork.gamecore.render.Render;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -104,7 +104,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return getDisplay().getRect(); return getDisplay().getRect();
} }

@ -10,7 +10,7 @@ import mightypork.gamecore.input.KeyStroke;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -26,7 +26,9 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
private final KeyBindingPool keybindings = new KeyBindingPool(); private final KeyBindingPool keybindings = new KeyBindingPool();
/** Mouse position constraint */
protected final VecView cMousePos = getInput().getMousePos(); protected final VecView cMousePos = getInput().getMousePos();
/** Screen size constraint */
protected final VecView cScreenSize = getDisplay().getSize(); protected final VecView cScreenSize = getDisplay().getSize();
@ -65,7 +67,7 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return screen.getRect(); return screen.getRect();
} }

@ -9,8 +9,6 @@ import mightypork.gamecore.control.bus.events.MouseMotionEvent;
import mightypork.gamecore.control.timing.Updateable; import mightypork.gamecore.control.timing.Updateable;
import mightypork.rogue.events.ActionRequest; import mightypork.rogue.events.ActionRequest;
import mightypork.rogue.events.ActionRequest.RequestType; import mightypork.rogue.events.ActionRequest.RequestType;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.SynthCoord2D;
import mightypork.utils.math.coord.VecMutable; import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
@ -30,7 +28,8 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
// listeners // listeners
private final KeyBindingPool keybindings; private final KeyBindingPool keybindings;
private final VecView mousePos = new SynthCoord2D() { /** Current mouse position */
private final VecView mousePos = new VecView() {
@Override @Override
public double x() public double x()
@ -103,9 +102,13 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
keybindings.unbindKeyStroke(stroke); keybindings.unbindKeyStroke(stroke);
} }
// counters as fields to save memory.
private final VecMutable mouseMove = VecMutable.zero();
private final VecMutable mouseLastPos = VecMutable.zero();
@Override @Override
public void update(double delta) public synchronized void update(double delta)
{ {
// was destroyed // was destroyed
if (!Display.isCreated()) return; if (!Display.isCreated()) return;
@ -114,17 +117,17 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
Display.processMessages(); Display.processMessages();
final VecMutable moveSum = new MutableCoord(); // sum the moves
final VecMutable lastPos = new MutableCoord(); mouseMove.reset();
mouseLastPos.reset();
boolean wasMouse = false; boolean wasMouse = false;
while (Mouse.next()) { while (Mouse.next()) {
onMouseEvent(moveSum, lastPos); onMouseEvent(mouseMove, mouseLastPos);
wasMouse = true; wasMouse = true;
} }
if (wasMouse && !moveSum.isZero()) { if (wasMouse && !mouseMove.isZero()) {
getEventBus().send(new MouseMotionEvent(lastPos, moveSum)); getEventBus().send(new MouseMotionEvent(mouseLastPos, mouseMove));
} }
while (Keyboard.next()) { while (Keyboard.next()) {
@ -142,8 +145,8 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
final int button = Mouse.getEventButton(); final int button = Mouse.getEventButton();
final boolean down = Mouse.getEventButtonState(); final boolean down = Mouse.getEventButtonState();
final VecMutable pos = new MutableCoord(Mouse.getEventX(), Mouse.getEventY()); final VecMutable pos = VecMutable.make(Mouse.getEventX(), Mouse.getEventY());
final VecMutable move = new MutableCoord(Mouse.getEventDX(), Mouse.getEventDY()); final VecMutable move = VecMutable.make(Mouse.getEventDX(), Mouse.getEventDY());
final int wheeld = Mouse.getEventDWheel(); final int wheeld = Mouse.getEventDWheel();
@ -152,7 +155,7 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
move.mul(1, -1, 1); move.mul(1, -1, 1);
if (button != -1 || wheeld != 0) { if (button != -1 || wheeld != 0) {
getEventBus().send(new MouseButtonEvent(pos.copy(), button, down, wheeld)); getEventBus().send(new MouseButtonEvent(pos.value(), button, down, wheeld));
} }
moveSum.add(move); moveSum.add(move);

@ -11,10 +11,8 @@ import mightypork.gamecore.control.bus.events.ScreenChangeEvent;
import mightypork.gamecore.control.timing.FpsMeter; import mightypork.gamecore.control.timing.FpsMeter;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.SynthCoord2D;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.FixedRect; import mightypork.utils.math.rect.RectValue;
import mightypork.utils.math.rect.RectView;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
@ -33,7 +31,8 @@ public class DisplaySystem extends AppModule implements RectConstraint {
private int targetFps; private int targetFps;
private FpsMeter fpsMeter; private FpsMeter fpsMeter;
private final VecView cScreenSize = new SynthCoord2D() { /** Current screen size */
private final VecView screenSize = new VecView() {
@Override @Override
public double y() public double y()
@ -191,7 +190,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
*/ */
public VecView getSize() public VecView getSize()
{ {
return cScreenSize; return screenSize;
} }
@ -200,7 +199,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
*/ */
public int getWidth() public int getWidth()
{ {
return cScreenSize.xi(); return screenSize.xi();
} }
@ -209,7 +208,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
*/ */
public int getHeight() public int getHeight()
{ {
return cScreenSize.yi(); return screenSize.yi();
} }
@ -240,9 +239,9 @@ public class DisplaySystem extends AppModule implements RectConstraint {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return new FixedRect(getSize()); return RectValue.make(getSize());
} }

@ -9,7 +9,6 @@ import mightypork.gamecore.render.textures.TxQuad;
import mightypork.utils.files.FileUtils; import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.FixedCoord;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.Rect; import mightypork.utils.math.rect.Rect;
@ -28,9 +27,9 @@ import org.newdawn.slick.util.ResourceLoader;
*/ */
public class Render { public class Render {
public static final Vec AXIS_X = new FixedCoord(1, 0, 0); public static final VecView AXIS_X = VecView.make(1, 0, 0);
public static final Vec AXIS_Y = new FixedCoord(0, 1, 0); public static final VecView AXIS_Y = VecView.make(0, 1, 0);
public static final Vec AXIS_Z = new FixedCoord(0, 0, 1); public static final VecView AXIS_Z = VecView.make(0, 0, 1);
/** /**
@ -541,6 +540,7 @@ public class Render {
/** /**
* Setup Ortho projection for 2D graphics * Setup Ortho projection for 2D graphics
*
* @param size viewport size (screen size) * @param size viewport size (screen size)
*/ */
public static void setupOrtho(VecView size) public static void setupOrtho(VecView size)

@ -4,6 +4,7 @@ package mightypork.gamecore.render.fonts;
import mightypork.gamecore.render.Render; import mightypork.gamecore.render.Render;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.Rect; import mightypork.utils.math.rect.Rect;
@ -71,7 +72,7 @@ public class FontRenderer {
private double getScale(double height) private double getScale(double height)
{ {
return height / font.getHeight(); return height / font.getLineHeight();
} }
@ -109,7 +110,7 @@ public class FontRenderer {
{ {
Render.pushMatrix(); Render.pushMatrix();
Render.translate(pos.view().round()); Render.translate(pos.value().round());
Render.scaleXY(getScale(height)); Render.scaleXY(getScale(height));
font.draw(text, color); font.draw(text, color);
@ -192,20 +193,19 @@ public class FontRenderer {
final double w = getWidth(text, height); final double w = getWidth(text, height);
final Vec start; final VecMutable start = pos.mutable();
switch (align) { switch (align) {
case LEFT: case LEFT:
start = pos.view();
break; break;
case CENTER: case CENTER:
start = pos.view().sub(w / 2D, 0); start.sub(w / 2D, 0);
break; break;
case RIGHT: case RIGHT:
default: default:
start = pos.view().sub(w, 0); start.sub(w, 0);
break; break;
} }

@ -33,7 +33,7 @@ public interface GLFont {
/** /**
* @return font height * @return font height
*/ */
int getHeight(); int getLineHeight();
/** /**
@ -46,5 +46,5 @@ public interface GLFont {
/** /**
* @return specified font size * @return specified font size
*/ */
int getSize(); int getFontSize();
} }

@ -23,7 +23,6 @@ import mightypork.gamecore.render.fonts.GLFont;
import mightypork.gamecore.render.textures.FilterMode; import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.FixedCoord;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
@ -376,14 +375,14 @@ public class CachedFont implements GLFont {
@Override @Override
public int getHeight() public int getLineHeight()
{ {
return fontHeight; return fontHeight;
} }
@Override @Override
public int getSize() public int getFontSize()
{ {
return fontSize; return fontSize;
} }
@ -425,7 +424,7 @@ public class CachedFont implements GLFont {
@Override @Override
public VecView getNeededSpace(String text) public VecView getNeededSpace(String text)
{ {
return new FixedCoord(getWidth(text), getHeight()); return VecView.make(getWidth(text), getLineHeight());
} }
} }

@ -79,37 +79,31 @@ public class DeferredFont extends DeferredResource implements GLFont {
} }
public void setFont(GLFont font) public synchronized void setSize(double size)
{
this.font = font;
}
public void setSize(double size)
{ {
this.size = size; this.size = size;
} }
public void setStyle(FontStyle style) public synchronized void setStyle(FontStyle style)
{ {
this.style = style; this.style = style;
} }
public void setChars(String chars) public synchronized void setChars(String chars)
{ {
this.chars = chars; this.chars = chars;
} }
public void setFilter(FilterMode filter) public synchronized void setFilter(FilterMode filter)
{ {
this.filter = filter; this.filter = filter;
} }
public void setAntialias(boolean antialias) public synchronized void setAntialias(boolean antialias)
{ {
this.antialias = antialias; this.antialias = antialias;
} }
@ -182,20 +176,20 @@ public class DeferredFont extends DeferredResource implements GLFont {
* @return font height * @return font height
*/ */
@Override @Override
public int getHeight() public int getLineHeight()
{ {
if (!ensureLoaded()) return 0; if (!ensureLoaded()) return 0;
return font.getHeight(); return font.getLineHeight();
} }
@Override @Override
public int getSize() public int getFontSize()
{ {
if (!ensureLoaded()) return 0; if (!ensureLoaded()) return 0;
return font.getSize(); return font.getFontSize();
} }
@ -212,11 +206,4 @@ public class DeferredFont extends DeferredResource implements GLFont {
// this will have to suffice // this will have to suffice
font = null; font = null;
} }
public void setFiltering(FilterMode filter)
{
this.filter = filter;
}
} }

@ -30,7 +30,7 @@ public class NullFont implements GLFont {
@Override @Override
public int getHeight() public int getLineHeight()
{ {
return 0; return 0;
} }
@ -44,7 +44,7 @@ public class NullFont implements GLFont {
@Override @Override
public int getSize() public int getFontSize()
{ {
return 0; return 0;
} }

@ -1,8 +1,8 @@
package mightypork.gamecore.render.textures; package mightypork.gamecore.render.textures;
import mightypork.utils.math.rect.FixedRect;
import mightypork.utils.math.rect.Rect; import mightypork.utils.math.rect.Rect;
import mightypork.utils.math.rect.RectValue;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
@ -65,7 +65,7 @@ public class TxQuad {
* @param y2 right bottom Y (0-1) * @param y2 right bottom Y (0-1)
*/ */
public TxQuad(Texture tx, double x1, double y1, double x2, double y2) { public TxQuad(Texture tx, double x1, double y1, double x2, double y2) {
this(tx, new FixedRect(x1, y1, x2, y2)); this(tx, RectValue.make(x1, y1, x2, y2));
} }

@ -16,7 +16,7 @@ import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.FixedCoord; import mightypork.utils.math.coord.VecView;
public class LayerBouncyBoxes extends ScreenLayer { public class LayerBouncyBoxes extends ScreenLayer {
@ -59,7 +59,7 @@ public class LayerBouncyBoxes extends ScreenLayer {
final TextPainter tp = new TextPainter(Res.getFont("default"), Align.LEFT, RGB.WHITE); final TextPainter tp = new TextPainter(Res.getFont("default"), Align.LEFT, RGB.WHITE);
tp.setText("Press \"C\" for \"Cat\" screen."); tp.setText("Press \"C\" for \"Cat\" screen.");
tp.setShadow(RGB.RED, new FixedCoord(2, 2)); tp.setShadow(RGB.RED, VecView.make(2, 2));
layout.add(tp); layout.add(tp);
} }

@ -19,15 +19,16 @@ import mightypork.rogue.Res;
import mightypork.utils.math.animation.AnimDouble; import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing; import mightypork.utils.math.animation.Easing;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.AnimCoord;
import mightypork.utils.math.coord.FixedCoord;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecMutableAnim;
import mightypork.utils.math.coord.VecView;
public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButtonEvent.Listener { public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButtonEvent.Listener {
private final AnimDouble size = new AnimDouble(400, Easing.SINE_BOTH); private final AnimDouble size = new AnimDouble(400, Easing.SINE_BOTH);
private final AnimCoord pos = new AnimCoord(Vec.ZERO, Easing.ELASTIC_OUT); private final VecMutableAnim pos = VecMutable.makeAnim(Easing.ELASTIC_OUT);
private final Random rand = new Random(); private final Random rand = new Random();
@ -40,6 +41,7 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
super(screen); super(screen);
pos.setTo(getDisplay().getCenter()); pos.setTo(getDisplay().getCenter());
pos.setDefaultDuration(3);
cat = new ImagePainter(Res.getTxQuad("test.kitten")); cat = new ImagePainter(Res.getTxQuad("test.kitten"));
@ -49,7 +51,7 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
tp.setAlign(Align.CENTER); tp.setAlign(Align.CENTER);
tp.setColor(RGB.YELLOW); tp.setColor(RGB.YELLOW);
tp.setText("Meow!"); tp.setText("Meow!");
tp.setShadow(RGB.dark(0.8), new FixedCoord(2, 2)); tp.setShadow(RGB.dark(0.8), VecView.make(2, 2));
tp.setContext(cCenterTo(cBox(64, 64), cMousePos)); tp.setContext(cCenterTo(cBox(64, 64), cMousePos));
@ -64,7 +66,7 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
@Override @Override
public void run() public void run()
{ {
pos.animateWithSpeed(getDisplay().getCenter(), 300); pos.setTo(getDisplay().getCenter());
} }
}); });
} }
@ -85,9 +87,9 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
final Vec pos = event.getPos(); final Vec pos = event.getPos();
this.pos.animateWithSpeed(pos, 160); this.pos.setTo(pos);
size.animate(200 + rand.nextInt(600), Math.max(0.2, this.pos.getDuration() / 2)); size.animate(200 + rand.nextInt(600), 1);
} }

@ -1,8 +1,7 @@
package mightypork.test; package mightypork.test;
import mightypork.utils.math.rect.FixedRect; import mightypork.utils.math.rect.RectValue;
import mightypork.utils.math.rect.RectView;
public class TestConstr { public class TestConstr {
@ -10,10 +9,10 @@ public class TestConstr {
public static void main(String[] args) public static void main(String[] args)
{ {
final RectView rm = new FixedRect(0, 0, 100, 100); final RectValue rm = RectValue.make(0, 0, 100, 100);
System.out.println(rm); System.out.println(rm);
final RectView added = rm.move(10, 10); final RectValue added = rm.move(10, 10);
System.out.println(added); System.out.println(added);
System.out.println(added.getOrigin()); System.out.println(added.getOrigin());

@ -173,7 +173,9 @@ public class PropertyManager {
public void apply() public void apply()
{ {
boolean needsSave = false; boolean needsSave = false;
new File(file.getParent()).mkdirs(); if (!new File(file.getParent()).mkdirs()) {
throw new RuntimeException("Cound not create config file.");
}
try(FileInputStream fis = new FileInputStream(file)) { try(FileInputStream fis = new FileInputStream(file)) {
props.load(fis); props.load(fis);

@ -39,8 +39,8 @@ public class FileUtils {
if (!source.exists()) return; if (!source.exists()) return;
if (source.isDirectory()) { if (source.isDirectory()) {
if (!target.exists()) { if (!target.exists() && !target.mkdir()) {
target.mkdir(); throw new IOException("Could not open destination directory.");
} }
final String[] children = source.list(); final String[] children = source.list();
@ -371,7 +371,7 @@ public class FileUtils {
} }
public static void deleteEmptyDirs(File base) public static void deleteEmptyDirs(File base) throws IOException
{ {
for (final File f : listDirectory(base)) { for (final File f : listDirectory(base)) {
if (!f.isDirectory()) continue; if (!f.isDirectory()) continue;
@ -380,7 +380,7 @@ public class FileUtils {
final List<File> children = listDirectory(f); final List<File> children = listDirectory(f);
if (children.size() == 0) { if (children.size() == 0) {
f.delete(); if (!f.delete()) throw new IOException("Could not delete a directory: " + f);
continue; continue;
} }
} }

@ -31,7 +31,7 @@ public class InstanceLock {
try { try {
fileLock.release(); fileLock.release();
randomAccessFile.close(); randomAccessFile.close();
lockFile.delete(); if (!lockFile.delete()) throw new IOException();
} catch (final Exception e) { } catch (final Exception e) {
System.err.println("Unable to remove lock file."); System.err.println("Unable to remove lock file.");
e.printStackTrace(); e.printStackTrace();

@ -1,7 +1,11 @@
package mightypork.utils.files; package mightypork.utils.files;
import java.io.*; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet; import java.util.HashSet;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -22,11 +26,11 @@ public class ZipBuilder {
/** /**
* @param target target zip file * @param target target zip file
* @throws FileNotFoundException if the file is directory or cannot be * @throws IOException if the file is directory or cannot be created
* created
*/ */
public ZipBuilder(File target) throws FileNotFoundException { public ZipBuilder(File target) throws IOException {
target.getParentFile().mkdirs();
if (!target.getParentFile().mkdirs()) throw new IOException("Could not create output directory.");
final FileOutputStream dest = new FileOutputStream(target); final FileOutputStream dest = new FileOutputStream(target);
out = new ZipOutputStream(new BufferedOutputStream(dest)); out = new ZipOutputStream(new BufferedOutputStream(dest));

@ -8,6 +8,7 @@ import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import mightypork.utils.logging.Log;
import mightypork.utils.string.validation.StringFilter; import mightypork.utils.string.validation.StringFilter;
@ -45,13 +46,13 @@ public class ZipUtils {
* @param outputDir target directory * @param outputDir target directory
* @param filter string filter (will be used to test entry names (paths)) * @param filter string filter (will be used to test entry names (paths))
* @return list of entries extracted (paths) * @return list of entries extracted (paths)
* @throws IOException * @throws IOException if the file)s) cannot be created
*/ */
public static List<String> extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException public static List<String> extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException
{ {
final ArrayList<String> files = new ArrayList<>(); final ArrayList<String> files = new ArrayList<>();
outputDir.mkdirs(); if (!outputDir.mkdirs()) throw new IOException("Could not create output directory.");
final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
@ -67,7 +68,7 @@ public class ZipUtils {
if (entry.isDirectory() || (filter != null && !filter.accept(entryPath))) continue; if (entry.isDirectory() || (filter != null && !filter.accept(entryPath))) continue;
// make sure directories exist // make sure directories exist
destinationParent.mkdirs(); if (!destinationParent.mkdirs()) throw new IOException("Could not create directory.");
if (!entry.isDirectory()) { if (!entry.isDirectory()) {
extractZipEntry(zip, entry, destFile); extractZipEntry(zip, entry, destFile);
@ -130,7 +131,7 @@ public class ZipUtils {
*/ */
public static void extractZipEntry(ZipFile zip, ZipEntry entry, File destFile) throws IOException public static void extractZipEntry(ZipFile zip, ZipEntry entry, File destFile) throws IOException
{ {
destFile.getParentFile().mkdirs(); if (!destFile.getParentFile().mkdirs()) throw new IOException("Could not create output directory.");
try(InputStream in = zip.getInputStream(entry); try(InputStream in = zip.getInputStream(entry);
BufferedInputStream is = new BufferedInputStream(in); BufferedInputStream is = new BufferedInputStream(in);
@ -171,7 +172,8 @@ public class ZipUtils {
{ {
try(ZipFile zf = new ZipFile(selectedFile)) { try(ZipFile zf = new ZipFile(selectedFile)) {
return zf.getEntry(string) != null; return zf.getEntry(string) != null;
} catch (final Exception e) { } catch (final IOException | RuntimeException e) {
Log.w("Error reading zip.", e);
return false; return false;
} }

@ -176,7 +176,7 @@ public class BinaryUtils {
public static char readChar(InputStream in) throws IOException public static char readChar(InputStream in) throws IOException
{ {
in.read(ac, 0, ac.length); if (-1 == in.read(ac, 0, ac.length)) throw new IOException("End of stream.");
final ByteBuffer buf = ByteBuffer.wrap(ac); final ByteBuffer buf = ByteBuffer.wrap(ac);
return buf.getChar(); return buf.getChar();
} }
@ -184,7 +184,7 @@ public class BinaryUtils {
public static short readShort(InputStream in) throws IOException public static short readShort(InputStream in) throws IOException
{ {
in.read(as, 0, as.length); if (-1 == in.read(as, 0, as.length)) throw new IOException("End of stream.");
final ByteBuffer buf = ByteBuffer.wrap(as); final ByteBuffer buf = ByteBuffer.wrap(as);
return buf.getShort(); return buf.getShort();
} }
@ -192,7 +192,8 @@ public class BinaryUtils {
public static long readLong(InputStream in) throws IOException public static long readLong(InputStream in) throws IOException
{ {
in.read(al, 0, al.length); if (-1 == in.read(al, 0, al.length)) throw new IOException("End of stream.");
final ByteBuffer buf = ByteBuffer.wrap(al); final ByteBuffer buf = ByteBuffer.wrap(al);
return buf.getLong(); return buf.getLong();
} }
@ -200,7 +201,7 @@ public class BinaryUtils {
public static int readInt(InputStream in) throws IOException public static int readInt(InputStream in) throws IOException
{ {
in.read(ai, 0, ai.length); if (-1 == in.read(ai, 0, ai.length)) throw new IOException("End of stream.");
final ByteBuffer buf = ByteBuffer.wrap(ai); final ByteBuffer buf = ByteBuffer.wrap(ai);
return buf.getInt(); return buf.getInt();
} }
@ -208,7 +209,7 @@ public class BinaryUtils {
public static float readFloat(InputStream in) throws IOException public static float readFloat(InputStream in) throws IOException
{ {
in.read(af, 0, af.length); if (-1 == in.read(af, 0, af.length)) throw new IOException("End of stream.");
final ByteBuffer buf = ByteBuffer.wrap(af); final ByteBuffer buf = ByteBuffer.wrap(af);
return buf.getFloat(); return buf.getFloat();
} }
@ -216,7 +217,7 @@ public class BinaryUtils {
public static double readDouble(InputStream in) throws IOException public static double readDouble(InputStream in) throws IOException
{ {
in.read(ad, 0, ad.length); if (-1 == in.read(ad, 0, ad.length)) throw new IOException("End of stream.");
final ByteBuffer buf = ByteBuffer.wrap(ad); final ByteBuffer buf = ByteBuffer.wrap(ad);
return buf.getDouble(); return buf.getDouble();
} }

@ -117,7 +117,7 @@ public class Ion {
final String f = path.toString(); final String f = path.toString();
final File dir = new File(f.substring(0, f.lastIndexOf(File.separator))); final File dir = new File(f.substring(0, f.lastIndexOf(File.separator)));
dir.mkdirs(); if (!dir.mkdirs()) throw new IOException("Could not create file.");
toStream(out, obj); toStream(out, obj);

@ -79,7 +79,7 @@ public class ArchivingLog extends SimpleLog {
for (int cnt = 0; (f2 = new File(log_dir, fbase + cntStr + suff)).exists(); cntStr = "_" + (++cnt)) {} for (int cnt = 0; (f2 = new File(log_dir, fbase + cntStr + suff)).exists(); cntStr = "_" + (++cnt)) {}
f.renameTo(f2); if (!f.renameTo(f2)) throw new RuntimeException("Could not move log file.");
} }
} }
@ -110,7 +110,9 @@ public class ArchivingLog extends SimpleLog {
// playing with fireee // playing with fireee
for (int i = 0; i < oldLogs.size() - logs_to_keep; i++) { for (int i = 0; i < oldLogs.size() - logs_to_keep; i++) {
oldLogs.get(i).delete(); if (!oldLogs.get(i).delete()) {
throw new RuntimeException("Could not delete old log file.");
}
} }
} }

@ -200,7 +200,30 @@ public class Log {
/** /**
* Log THROWING message * Log warning message with exception
*
* @param msg message
* @param thrown thrown exception
*/
public static void w(String msg, Throwable thrown)
{
log(Level.WARNING, msg, thrown);
}
/**
* Log exception thrown as warning
*
* @param thrown thrown exception
*/
public static void w(Throwable thrown)
{
log(Level.WARNING, null, thrown);
}
/**
* Log error message
* *
* @param msg message * @param msg message
* @param thrown thrown exception * @param thrown thrown exception
@ -212,7 +235,7 @@ public class Log {
/** /**
* Log exception thrown * Log exception thrown as error
* *
* @param thrown thrown exception * @param thrown thrown exception
*/ */

@ -76,7 +76,7 @@ public class SimpleLog implements LogWriter {
protected void printHeader() protected void printHeader()
{ {
final String stamp = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date()); final String stamp = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date());
i("Logger \"" + getName() + "\" initialized.\n" + stamp); log(Level.INFO, "Logger \"" + getName() + "\" initialized.\n" + stamp);
} }
@ -159,93 +159,4 @@ public class SimpleLog implements LogWriter {
} }
} }
/**
* Log FINE message
*
* @param msg message
*/
public void f1(String msg)
{
log(Level.FINE, msg);
}
/**
* Log FINER message
*
* @param msg message
*/
public void f2(String msg)
{
log(Level.FINER, msg);
}
/**
* Log FINEST message
*
* @param msg message
*/
public void f3(String msg)
{
log(Level.FINEST, msg);
}
/**
* Log INFO message
*
* @param msg message
*/
public void i(String msg)
{
log(Level.INFO, msg);
}
/**
* Log WARNING message (less severe than ERROR)
*
* @param msg message
*/
public void w(String msg)
{
log(Level.WARNING, msg);
}
/**
* Log ERROR message
*
* @param msg message
*/
public void e(String msg)
{
log(Level.SEVERE, msg);
}
/**
* Log THROWING message
*
* @param msg message
* @param thrown thrown exception
*/
public void e(String msg, Throwable thrown)
{
log(Level.SEVERE, msg, thrown);
}
/**
* Log exception thrown
*
* @param thrown thrown exception
*/
public void e(Throwable thrown)
{
log(Level.SEVERE, null, thrown);
}
} }

@ -3,8 +3,6 @@ package mightypork.utils.math;
import mightypork.utils.math.Calc.Deg; import mightypork.utils.math.Calc.Deg;
import mightypork.utils.math.Calc.Rad; import mightypork.utils.math.Calc.Rad;
import mightypork.utils.math.constraints.NumberConstraint;
import mightypork.utils.math.coord.ConstraintCoord;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
@ -22,7 +20,7 @@ public class Polar {
/** distance in units */ /** distance in units */
private double radius = 0; private double radius = 0;
private ConstraintCoord coord = null; private VecView coord = null;
/** /**
@ -141,21 +139,21 @@ public class Polar {
{ {
// lazy init // lazy init
if (coord == null) { if (coord == null) {
coord = new ConstraintCoord(new NumberConstraint() { coord = new VecView() {
@Override @Override
public double getValue() public double x()
{ {
return radius * Math.cos(angle); return radius * Math.cos(angle);
} }
}, new NumberConstraint() {
@Override @Override
public double getValue() public double y()
{ {
return radius * Math.sin(angle); return radius * Math.sin(angle);
} }
}); };
} }
return coord; return coord;

@ -2,12 +2,10 @@ package mightypork.utils.math.constraints;
import mightypork.gamecore.control.timing.Poller; import mightypork.gamecore.control.timing.Poller;
import mightypork.utils.math.coord.FixedCoord; import mightypork.utils.math.coord.AbstractVecProxy;
import mightypork.utils.math.coord.SynthCoord3D;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.FixedRect; import mightypork.utils.math.rect.RectValue;
import mightypork.utils.math.rect.RectView;
/** /**
@ -132,7 +130,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().round(); return r.getRect().round();
} }
@ -255,7 +253,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final double height = r.getRect().getHeight(); final double height = r.getRect().getHeight();
final double perRow = height / rows; final double perRow = height / rows;
@ -263,7 +261,7 @@ public class Constraints {
final Vec origin = r.getRect().getOrigin().add(0, perRow * index); final Vec origin = r.getRect().getOrigin().add(0, perRow * index);
final Vec size = r.getRect().getSize().setY(perRow); final Vec size = r.getRect().getSize().setY(perRow);
return new FixedRect(origin, size); return RectValue.make(origin, size);
} }
}; };
} }
@ -274,7 +272,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final double width = r.getRect().getWidth(); final double width = r.getRect().getWidth();
final double perCol = width / columns; final double perCol = width / columns;
@ -282,7 +280,7 @@ public class Constraints {
final Vec origin = r.getRect().getOrigin().add(perCol * index, 0); final Vec origin = r.getRect().getOrigin().add(perCol * index, 0);
final Vec size = r.getRect().getSize().setX(perCol); final Vec size = r.getRect().getSize().setX(perCol);
return new FixedRect(origin, size); return RectValue.make(origin, size);
} }
}; };
} }
@ -293,7 +291,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final double height = r.getRect().getHeight(); final double height = r.getRect().getHeight();
final double width = r.getRect().getHeight(); final double width = r.getRect().getHeight();
@ -302,7 +300,7 @@ public class Constraints {
final Vec origin = r.getRect().getOrigin().add(perCol * left, perRow * (rows - top - 1)); final Vec origin = r.getRect().getOrigin().add(perCol * left, perRow * (rows - top - 1));
return new FixedRect(origin, perCol, perRow); return RectValue.make(origin, perCol, perRow);
} }
}; };
} }
@ -328,7 +326,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(toDouble(left), toDouble(top), toDouble(right), toDouble(bottom)); return r.getRect().shrink(toDouble(left), toDouble(top), toDouble(right), toDouble(bottom));
} }
@ -341,7 +339,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(0, toDouble(shrink), 0, 0); return r.getRect().shrink(0, toDouble(shrink), 0, 0);
} }
@ -354,7 +352,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(0, 0, 0, toDouble(shrink)); return r.getRect().shrink(0, 0, 0, toDouble(shrink));
} }
@ -367,7 +365,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(toDouble(shrink), 0, 0, 0); return r.getRect().shrink(toDouble(shrink), 0, 0, 0);
} }
@ -380,7 +378,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(0, 0, toDouble(shrink), 0); return r.getRect().shrink(0, 0, toDouble(shrink), 0);
} }
@ -406,7 +404,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().grow(toDouble(left), toDouble(right), toDouble(top), toDouble(bottom)); return r.getRect().grow(toDouble(left), toDouble(right), toDouble(top), toDouble(bottom));
} }
@ -419,7 +417,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().grow(0, toDouble(grow), 0, 0); return r.getRect().grow(0, toDouble(grow), 0, 0);
} }
@ -432,7 +430,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().grow(0, 0, 0, toDouble(grow)); return r.getRect().grow(0, 0, 0, toDouble(grow));
} }
@ -445,7 +443,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().grow(toDouble(grow), 0, 0, 0); return r.getRect().grow(toDouble(grow), 0, 0, 0);
} }
@ -458,7 +456,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().grow(0, 0, toDouble(grow), 0); return r.getRect().grow(0, 0, toDouble(grow), 0);
} }
@ -479,9 +477,9 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return new FixedRect(origin.getVec(), toDouble(width), toDouble(height)); return RectValue.make(origin.getVec(), toDouble(width), toDouble(height));
} }
}; };
} }
@ -492,9 +490,9 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return new FixedRect(0, 0, toDouble(width), toDouble(height)); return RectValue.make(0, 0, toDouble(width), toDouble(height));
} }
}; };
} }
@ -505,11 +503,11 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final Vec origin = r.getRect().getOrigin(); final Vec origin = r.getRect().getOrigin();
return new FixedRect(origin.x(), origin.y(), toDouble(width), toDouble(height)); return RectValue.make(origin.x(), origin.y(), toDouble(width), toDouble(height));
} }
}; };
} }
@ -520,11 +518,11 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final Vec origin = r.getRect().getOrigin(); final Vec origin = r.getRect().getOrigin();
return new FixedRect(origin.x() + toDouble(x), origin.y() + toDouble(y), toDouble(width), toDouble(height)); return RectValue.make(origin.x() + toDouble(x), origin.y() + toDouble(y), toDouble(width), toDouble(height));
} }
}; };
} }
@ -535,12 +533,12 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final VecView size = r.getRect().getSize(); final VecView size = r.getRect().getSize();
final VecView center = centerTo.getRect().getCenter(); final VecView center = centerTo.getRect().getCenter();
return new FixedRect(center.sub(size.half()), size); return RectValue.make(center.sub(size.half()), size);
} }
}; };
} }
@ -551,11 +549,11 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final VecView size = r.getRect().getSize(); final VecView size = r.getRect().getSize();
return new FixedRect(centerTo.getVec().sub(size.half()), size); return RectValue.make(centerTo.getVec().sub(size.half()), size);
} }
}; };
} }
@ -566,12 +564,12 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final VecView size = r.getRect().getSize(); final VecView size = r.getRect().getSize();
final VecView v = new FixedCoord(toDouble(x), toDouble(y)); final VecView v = VecView.make(toDouble(x), toDouble(y));
return new FixedRect(v.sub(size.half()), size); return RectValue.make(v.sub(size.half()), size);
} }
}; };
} }
@ -582,7 +580,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().move(move.getVec()); return r.getRect().move(move.getVec());
} }
@ -595,7 +593,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().move(toDouble(x), toDouble(y)); return r.getRect().move(toDouble(x), toDouble(y));
} }
@ -645,7 +643,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final double t = toDouble(top); final double t = toDouble(top);
final double r = toDouble(right); final double r = toDouble(right);
@ -655,7 +653,7 @@ public class Constraints {
final double x = c.getVec().x(); final double x = c.getVec().x();
final double y = c.getVec().y(); final double y = c.getVec().y();
return new FixedRect(x - l, y - t, l + r, t + b); return RectValue.make(x - l, y - t, l + r, t + b);
} }
}; };
} }
@ -668,7 +666,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(0, 0, r.getRect().getWidth(), 0); return r.getRect().shrink(0, 0, r.getRect().getWidth(), 0);
} }
@ -681,7 +679,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(0, 0, 0, r.getRect().getHeight()); return r.getRect().shrink(0, 0, 0, r.getRect().getHeight());
} }
@ -694,7 +692,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(r.getRect().getWidth(), 0, 0, 0); return r.getRect().shrink(r.getRect().getWidth(), 0, 0, 0);
} }
@ -707,7 +705,7 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return r.getRect().shrink(0, r.getRect().getHeight(), 0, 0); return r.getRect().shrink(0, r.getRect().getHeight(), 0, 0);
} }
@ -770,10 +768,10 @@ public class Constraints {
public static VecConstraint cAdd(final VecConstraint c1, final VecConstraint c2) public static VecConstraint cAdd(final VecConstraint c1, final VecConstraint c2)
{ {
return new VecConstraintSynth() { return new AbstractVecProxy() {
@Override @Override
public VecView getVec() public VecView getSource()
{ {
return c1.getVec().add(c2.getVec()); return c1.getVec().add(c2.getVec());
} }
@ -789,28 +787,13 @@ public class Constraints {
public static VecConstraint cAdd(final VecConstraint c, final Object x, final Object y, final Object z) public static VecConstraint cAdd(final VecConstraint c, final Object x, final Object y, final Object z)
{ {
return new SynthCoord3D() { return new AbstractVecProxy() {
@Override @Override
public double x() public VecView getSource()
{ {
return c.getVec().x() + toDouble(x); return c.getVec().add(toDouble(x), toDouble(y), toDouble(z));
} }
@Override
public double y()
{
return c.getVec().y() + toDouble(y);
}
@Override
public double z()
{
return c.getVec().z() + toDouble(z);
}
}; };
} }
@ -819,10 +802,10 @@ public class Constraints {
public static VecConstraint cSub(final VecConstraint c1, final VecConstraint c2) public static VecConstraint cSub(final VecConstraint c1, final VecConstraint c2)
{ {
return new VecConstraintSynth() { return new AbstractVecProxy() {
@Override @Override
public VecView getVec() public VecView getSource()
{ {
return c1.getVec().sub(c2.getVec()); return c1.getVec().sub(c2.getVec());
} }
@ -838,10 +821,10 @@ public class Constraints {
public static VecConstraint cSub(final VecConstraint c, final Object x, final Object y, final Object z) public static VecConstraint cSub(final VecConstraint c, final Object x, final Object y, final Object z)
{ {
return new VecConstraintSynth() { return new AbstractVecProxy() {
@Override @Override
public VecView getVec() public VecView getSource()
{ {
return c.getVec().sub(toDouble(x), toDouble(y), toDouble(z)); return c.getVec().sub(toDouble(x), toDouble(y), toDouble(z));
} }
@ -854,10 +837,10 @@ public class Constraints {
public static VecConstraint cMul(final VecConstraint c, final Object mul) public static VecConstraint cMul(final VecConstraint c, final Object mul)
{ {
return new VecConstraintSynth() { return new AbstractVecProxy() {
@Override @Override
public VecView getVec() public VecView getSource()
{ {
return c.getVec().mul(toDouble(mul)); return c.getVec().mul(toDouble(mul));
} }
@ -870,10 +853,10 @@ public class Constraints {
public static VecConstraint cOrigin(final RectConstraint r) public static VecConstraint cOrigin(final RectConstraint r)
{ {
return new VecConstraintSynth() { return new AbstractVecProxy() {
@Override @Override
public VecView getVec() public VecView getSource()
{ {
return r.getRect().getOrigin(); return r.getRect().getOrigin();
} }
@ -883,10 +866,10 @@ public class Constraints {
public static VecConstraint cSize(final RectConstraint r) public static VecConstraint cSize(final RectConstraint r)
{ {
return new VecConstraintSynth() { return new AbstractVecProxy() {
@Override @Override
public VecView getVec() public VecView getSource()
{ {
return r.getRect().getSize(); return r.getRect().getSize();
} }
@ -971,11 +954,11 @@ public class Constraints {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
final Vec v = c.getVec(); final Vec v = c.getVec();
return new FixedRect(v.x(), v.y(), 0, 0); return RectValue.make(v.x(), v.y(), 0, 0);
} }
}; };
} }

@ -1,7 +1,7 @@
package mightypork.utils.math.constraints; package mightypork.utils.math.constraints;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -9,7 +9,7 @@ import mightypork.utils.math.rect.RectView;
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class ContextAdapter implements PluggableContext { public abstract class ContextAdapter implements PluggableRect {
private RectConstraint backing = null; private RectConstraint backing = null;
@ -22,7 +22,7 @@ public abstract class ContextAdapter implements PluggableContext {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return backing.getRect(); return backing.getRect();
} }

@ -0,0 +1,25 @@
package mightypork.utils.math.constraints;
/**
* Constant number {@link NumberConstraint}
*
* @author MightyPork
*/
public class FixedNumberConstraint implements NumberConstraint {
private final double value;
public FixedNumberConstraint(double value) {
this.value = value;
}
@Override
public double getValue()
{
return value;
}
}

@ -8,23 +8,8 @@ package mightypork.utils.math.constraints;
*/ */
public interface NumberConstraint { public interface NumberConstraint {
public static final NumberConstraint ZERO = new NumberConstraint() { public static final NumberConstraint ZERO = new FixedNumberConstraint(0);
public static final NumberConstraint ONE = new FixedNumberConstraint(1);
@Override
public double getValue()
{
return 0;
}
};
public static final NumberConstraint ONE = new NumberConstraint() {
@Override
public double getValue()
{
return 0;
}
};
/** /**

@ -1,7 +1,7 @@
package mightypork.utils.math.constraints; package mightypork.utils.math.constraints;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -9,7 +9,7 @@ import mightypork.utils.math.rect.RectView;
* *
* @author MightyPork * @author MightyPork
*/ */
public interface PluggableContext extends RectConstraint { public interface PluggableRect extends RectConstraint {
/** /**
* @param rect context to set * @param rect context to set
@ -18,6 +18,6 @@ public interface PluggableContext extends RectConstraint {
@Override @Override
abstract RectView getRect(); abstract RectValue getRect();
} }

@ -3,13 +3,13 @@ package mightypork.utils.math.constraints;
import mightypork.gamecore.control.timing.Pollable; import mightypork.gamecore.control.timing.Pollable;
import mightypork.gamecore.control.timing.Poller; import mightypork.gamecore.control.timing.Poller;
import mightypork.utils.math.rect.MutableRect; import mightypork.utils.math.rect.RectMutable;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
* {@link RectConstraint} cache, used to reduce CPU load with very complex * {@link RectConstraint} cache, used for caching computed Rect from a complex
* constraints.<br> * {@link RectConstraint}.<br>
* Calculates only when polled. * Calculates only when polled.
* *
* @author MightyPork * @author MightyPork
@ -17,7 +17,7 @@ import mightypork.utils.math.rect.RectView;
public class RectCache implements RectConstraint, Pollable { public class RectCache implements RectConstraint, Pollable {
private final RectConstraint observed; private final RectConstraint observed;
private final MutableRect cached = new MutableRect(); private final RectMutable cached = RectMutable.zero();
/** /**
@ -42,7 +42,7 @@ public class RectCache implements RectConstraint, Pollable {
@Override @Override
public RectView getRect() public RectValue getRect()
{ {
return cached.view(); return cached.view();
} }

@ -1,7 +1,7 @@
package mightypork.utils.math.constraints; package mightypork.utils.math.constraints;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.RectValue;
/** /**
@ -14,5 +14,5 @@ public interface RectConstraint {
/** /**
* @return rect region * @return rect region
*/ */
RectView getRect(); RectValue getRect();
} }

@ -1,33 +0,0 @@
package mightypork.utils.math.constraints;
import mightypork.utils.math.coord.VecView;
public abstract class VecConstraintSynth implements VecConstraint {
@Override
public abstract VecView getVec();
@Override
public NumberConstraint xc()
{
return Constraints.cX(getVec());
}
@Override
public NumberConstraint yc()
{
return Constraints.cY(getVec());
}
@Override
public NumberConstraint zc()
{
return Constraints.cZ(getVec());
}
}

@ -0,0 +1,188 @@
package mightypork.utils.math.coord;
import mightypork.utils.math.constraints.NumberConstraint;
public abstract class AbstractVec implements Vec {
private AbstractVecProxy view;
private NumberConstraint constraintX;
private NumberConstraint constraintY;
private NumberConstraint constraintZ;
@Override
public VecView getVec()
{
return view();
}
@Override
public abstract double x();
@Override
public abstract double y();
@Override
public abstract double z();
@Override
public int xi()
{
return (int) Math.round(x());
}
@Override
public int yi()
{
return (int) Math.round(y());
}
@Override
public int zi()
{
return (int) Math.round(z());
}
@Override
public NumberConstraint xc()
{
if (constraintX == null) constraintX = new NumberConstraint() {
@Override
public double getValue()
{
return x();
}
};
return constraintX;
}
@Override
public NumberConstraint yc()
{
if (constraintY == null) constraintY = new NumberConstraint() {
@Override
public double getValue()
{
return y();
}
};
return constraintY;
}
@Override
public NumberConstraint zc()
{
if (constraintZ == null) constraintZ = new NumberConstraint() {
@Override
public double getValue()
{
return z();
}
};
return constraintZ;
}
@Override
public double size()
{
final double x = x(), y = y(), z = z();
return Math.sqrt(x * x + y * y + z * z);
}
@Override
public boolean isZero()
{
return x() == 0 && y() == 0 && z() == 0;
}
@Override
public VecView value()
{
return new ConstVec(this);
}
@Override
public double distTo(Vec point)
{
final double dx = x() - point.x();
final double dy = y() - point.y();
final double dz = z() - point.z();
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
@Override
public VecView midTo(Vec point)
{
final double dx = (point.x() - x()) * 0.5;
final double dy = (point.y() - y()) * 0.5;
final double dz = (point.z() - z()) * 0.5;
return VecView.make(dx, dy, dz);
}
@Override
public VecView vecTo(Vec point)
{
return VecView.make(point.x() - x(), point.y() - y(), point.z() - z());
}
@Override
public VecView cross(Vec vec)
{
//@formatter:off
return VecView.make(
y() * vec.z() - z() * vec.y(),
z() * vec.x() - x() * vec.z(),
x() * vec.y() - y() * vec.x());
//@formatter:on
}
@Override
public double dot(Vec vec)
{
return x() * vec.x() + y() * vec.y() + z() * vec.z();
}
@Override
public VecMutable mutable()
{
return VecMutable.make(this);
}
@Override
public VecView view()
{
if (view == null) view = new VecProxy(this);
return view;
}
}

@ -0,0 +1,68 @@
package mightypork.utils.math.coord;
public abstract class AbstractVecProxy extends VecView {
/**
* @return the proxied coord
*/
protected abstract Vec getSource();
@Override
public double x()
{
return processX(getSource().x());
}
@Override
public double y()
{
return processY(getSource().y());
}
@Override
public double z()
{
return processZ(getSource().z());
}
/**
* Process X before it's returned by x()
*
* @param x original X
* @return output X
*/
protected double processX(double x)
{
return x;
}
/**
* Process Y before it's returned by y()
*
* @param y original Y
* @return output Y
*/
protected double processY(double y)
{
return y;
}
/**
* Process Z before it's returned by z()
*
* @param z original Z
* @return output Z
*/
protected double processZ(double z)
{
return z;
}
}

@ -1,155 +0,0 @@
package mightypork.utils.math.coord;
import mightypork.gamecore.control.timing.Pauseable;
import mightypork.gamecore.control.timing.Updateable;
import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing;
/**
* 3D coordinated with support for transitions, mutable.
*
* @author MightyPork
*/
public class AnimCoord extends VecMutableImpl implements Pauseable, Updateable {
private final AnimDouble x, y, z;
public AnimCoord(AnimDouble x, AnimDouble y, AnimDouble z) {
this.x = x;
this.y = y;
this.z = z;
}
public AnimCoord(Vec start, Easing easing) {
x = new AnimDouble(start.x(), easing);
y = new AnimDouble(start.y(), easing);
z = new AnimDouble(start.z(), easing);
}
@Override
public double x()
{
return x.now();
}
@Override
public double y()
{
return y.now();
}
@Override
public double z()
{
return z.now();
}
@Override
public AnimCoord result(double x, double y, double z)
{
this.x.setTo(x);
this.y.setTo(y);
this.z.setTo(z);
return this;
}
public AnimCoord add(Vec offset, double speed)
{
animate(offset.x() - x(), offset.y() - y(), offset.z() - z(), speed);
return this;
}
public AnimCoord animate(double x, double y, double z, double duration)
{
this.x.animate(x, duration);
this.y.animate(y, duration);
this.z.animate(z, duration);
return this;
}
public AnimCoord animate(Vec target, double duration)
{
x.animate(target.x(), duration);
y.animate(target.y(), duration);
z.animate(target.z(), duration);
return this;
}
public void animateWithSpeed(Vec target, double unitsPerSecond)
{
final double dist = distTo(target);
final double duration = dist / unitsPerSecond;
animate(target, duration);
}
@Override
public void update(double delta)
{
x.update(delta);
y.update(delta);
z.update(delta);
}
@Override
public void pause()
{
x.pause();
y.pause();
z.pause();
}
@Override
public void resume()
{
x.resume();
y.resume();
z.resume();
}
@Override
public boolean isPaused()
{
return x.isPaused(); // BUNO
}
public boolean isFinished()
{
return x.isFinished(); // BUNO
}
public double getDuration()
{
return x.getDuration(); // BUNO
}
public double getElapsed()
{
return x.getElapsed(); // BUNO
}
public double getProgress()
{
return x.getProgress(); // BUNO
}
}

@ -2,27 +2,26 @@ package mightypork.utils.math.coord;
/** /**
* Coordinate with immutable numeric values.<br> * Coordinate with immutable numeric values.
* Operations yield a new {@link MutableCoord} with the result.
* *
* @author MightyPork * @author MightyPork
*/ */
public class FixedCoord extends VecView { class ConstVec extends VecView {
private final double x, y, z; private final double x, y, z;
public FixedCoord(Vec other) { public ConstVec(Vec other) {
this(other.x(), other.y(), other.z()); this(other.x(), other.y(), other.z());
} }
public FixedCoord(double x, double y) { public ConstVec(double x, double y) {
this(x, y, 0); this(x, y, 0);
} }
public FixedCoord(double x, double y, double z) { public ConstVec(double x, double y, double z) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
@ -49,4 +48,11 @@ public class FixedCoord extends VecView {
return z; return z;
} }
@Override
public VecView value()
{
return this; // it's constant already
}
} }

@ -1,58 +0,0 @@
package mightypork.utils.math.coord;
/**
* <p>
* <b>[ Use Vec.view() method to make a proxy! ]</b>
* </p>
* <p>
* View of another coordinate, immutable.<br>
* Operations yield a new {@link MutableCoord} with the result.
* </p>
*
* @author MightyPork
*/
public class CoordProxy extends VecView {
private final Vec observed;
/**
* Protected, in order to enforce the use of view() method on Vec, which
* uses caching.
*
* @param observed
*/
public CoordProxy(Vec observed) {
this.observed = observed;
}
@Override
public CoordProxy view()
{
return this; // no need to make another
}
@Override
public double x()
{
return observed.x();
}
@Override
public double y()
{
return observed.y();
}
@Override
public double z()
{
return observed.z();
}
}

@ -1,86 +0,0 @@
package mightypork.utils.math.coord;
/**
* Mutable coordinate.<br>
* All Vec methods (except copy) alter data values and return this instance.
*
* @author MightyPork
*/
public class MutableCoord extends VecMutableImpl {
private double x, y, z;
/**
* Zero coord
*/
public MutableCoord() {
this(0, 0, 0);
}
/**
* @param copied other coord to vopy
*/
public MutableCoord(Vec copied) {
this(copied.x(), copied.y(), copied.z());
}
/**
* @param x X coordinate
* @param y Y coordinate
*/
public MutableCoord(double x, double y) {
super();
this.x = x;
this.y = y;
this.z = 0;
}
/**
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
*/
public MutableCoord(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
@Override
public double x()
{
return x;
}
@Override
public double y()
{
return y;
}
@Override
public double z()
{
return z;
}
@Override
public MutableCoord result(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
return this;
}
}

@ -6,26 +6,25 @@ import mightypork.utils.math.constraints.NumberConstraint;
/** /**
* Coord view composed of given {@link NumberConstraint}s, using their current * Coord view composed of given {@link NumberConstraint}s, using their current
* values.<br> * values.
* Operations yield a new {@link MutableCoord} with the result.
* *
* @author MightyPork * @author MightyPork
*/ */
public class ConstraintCoord extends VecView { class NumConstrVec extends VecView {
private final NumberConstraint constrX; private final NumberConstraint constrX;
private final NumberConstraint constrY; private final NumberConstraint constrY;
private final NumberConstraint constrZ; private final NumberConstraint constrZ;
public ConstraintCoord(NumberConstraint x, NumberConstraint y, NumberConstraint z) { public NumConstrVec(NumberConstraint x, NumberConstraint y, NumberConstraint z) {
this.constrX = x; this.constrX = x;
this.constrY = y; this.constrY = y;
this.constrZ = z; this.constrZ = z;
} }
public ConstraintCoord(NumberConstraint x, NumberConstraint y) { public NumConstrVec(NumberConstraint x, NumberConstraint y) {
this.constrX = x; this.constrX = x;
this.constrY = y; this.constrY = y;
this.constrZ = NumberConstraint.ZERO; this.constrZ = NumberConstraint.ZERO;

@ -1,25 +0,0 @@
package mightypork.utils.math.coord;
/**
* 2D coord for anonymous implementations.<br>
* Operations yield a new {@link MutableCoord} with the result.
*
* @author MightyPork
*/
public abstract class SynthCoord2D extends VecView {
@Override
public abstract double x();
@Override
public abstract double y();
@Override
public double z()
{
return 0;
}
}

@ -1,23 +0,0 @@
package mightypork.utils.math.coord;
/**
* 3D immutable coord for anonymous implementations.<br>
* Operations yield a new {@link MutableCoord} with the result.
*
* @author MightyPork
*/
public abstract class SynthCoord3D extends VecView {
@Override
public abstract double x();
@Override
public abstract double y();
@Override
public abstract double z();
}

@ -0,0 +1,153 @@
package mightypork.utils.math.coord;
public class Synths {
private static abstract class HeteroSynth extends VecProxy {
public HeteroSynth(Vec observed) {
super(observed);
}
@Override
protected abstract double processX(double x);
@Override
protected abstract double processY(double y);
@Override
protected abstract double processZ(double z);
}
private static abstract class UniformSynth extends VecProxy {
public UniformSynth(Vec observed) {
super(observed);
}
@Override
protected double processX(double x)
{
return super.processX(x);
}
@Override
protected double processY(double y)
{
return super.processY(y);
}
@Override
protected double processZ(double z)
{
return super.processZ(z);
}
protected abstract double process(double a);
}
public static class Round extends UniformSynth {
public Round(Vec observed) {
super(observed);
}
@Override
protected double process(double a)
{
return Math.round(a);
}
}
public static class Ceil extends UniformSynth {
public Ceil(Vec observed) {
super(observed);
}
@Override
protected double process(double a)
{
return Math.ceil(a);
}
}
public static class Floor extends UniformSynth {
public Floor(Vec observed) {
super(observed);
}
@Override
protected double process(double a)
{
return Math.floor(a);
}
}
public static class Neg extends UniformSynth {
public Neg(Vec observed) {
super(observed);
}
@Override
protected double process(double a)
{
return -a;
}
}
public static class Half extends UniformSynth {
public Half(Vec observed) {
super(observed);
}
@Override
protected double process(double a)
{
return a / 2;
}
}
public static class Norm extends HeteroSynth {
public Norm(Vec observed) {
super(observed);
}
@Override
protected double processX(double x)
{
return 0;
}
@Override
protected double processY(double y)
{
return 0;
}
@Override
protected double processZ(double z)
{
return 0;
}
}
}

@ -12,8 +12,8 @@ import mightypork.utils.math.constraints.VecConstraint;
*/ */
public interface Vec extends VecConstraint { public interface Vec extends VecConstraint {
public static final VecView ZERO = new FixedCoord(0, 0, 0); public static final VecView ZERO = new ConstVec(0, 0, 0);
public static final VecView ONE = new FixedCoord(1, 1, 1); public static final VecView ONE = new ConstVec(0, 0, 0);
/** /**
@ -53,57 +53,105 @@ public interface Vec extends VecConstraint {
/** /**
* @return X as float * @return X constraint
*/ */
float xf(); @Override
NumberConstraint xc();
/** /**
* @return Y as float * @return Y constraint
*/ */
float yf(); @Override
NumberConstraint yc();
/** /**
* @return Z as float * @return Z constraint
*/ */
float zf(); @Override
NumberConstraint zc();
/** /**
* @return X constraint * Get vector size
*
* @return size
*/ */
@Override double size();
NumberConstraint xc();
/** /**
* @return Y constraint * @return true if zero
*/ */
@Override public boolean isZero();
NumberConstraint yc();
/** /**
* @return Z constraint * Get distance to other point
*
* @param point other point
* @return distance
*/ */
@Override double distTo(Vec point);
NumberConstraint zc();
/**
* Get middle of line to other point
*
* @param point other point
* @return result
*/
VecView midTo(Vec point);
/**
* Create vector from this point to other point
*
* @param point second point
* @return result
*/
public VecView vecTo(Vec point);
/**
* Get cross product (vector multiplication)
*
* @param vec other vector
* @return result
*/
public VecView cross(Vec vec);
/** /**
* Get a new mutable variable holding the current state * Get dot product (scalar multiplication)
* *
* @return a mutable copy * @param vec other vector
* @return dot product
*/ */
VecMutable copy(); public double dot(Vec vec);
/** /**
* Get immutable view at this vec * Get a view at current state, not propagating further changes.
*
* @return a immutable copy
*/
VecView value();
/**
* Get immutable proxy view at this vec
* *
* @return immutable view * @return immutable view
*/ */
VecView view(); VecView view();
/**
* Get a mutable copy of current values.
*
* @return mutable copy
*/
VecMutable mutable();
} }

@ -2,11 +2,29 @@ package mightypork.utils.math.coord;
/** /**
* 3D coordinate methods * Implementation of coordinate methods
* *
* @author MightyPork * @author MightyPork
* @param <V> Return type of methods
*/ */
interface VecMath<V> extends Vec { abstract class VecMath<V extends Vec> extends AbstractVec {
/**
* <p>
* Some operation was performed and this result was obtained.
* </p>
* <p>
* It's now up to implementing class what to do - mutable ones can alter
* it's data values, immutable can return a new Vec.
* </p>
*
* @param x
* @param y
* @param z
* @return the result Vec
*/
public abstract V result(double x, double y, double z);
/** /**
* Set X coordinate (if immutable, in a copy). * Set X coordinate (if immutable, in a copy).
@ -14,7 +32,10 @@ interface VecMath<V> extends Vec {
* @param x x coordinate * @param x x coordinate
* @return result * @return result
*/ */
V setX(double x); public V setX(double x)
{
return result(x, y(), z());
}
/** /**
@ -23,7 +44,10 @@ interface VecMath<V> extends Vec {
* @param y y coordinate * @param y y coordinate
* @return result * @return result
*/ */
V setY(double y); public V setY(double y)
{
return result(x(), y, z());
}
/** /**
@ -32,33 +56,10 @@ interface VecMath<V> extends Vec {
* @param z z coordinate * @param z z coordinate
* @return result * @return result
*/ */
V setZ(double z); public V setZ(double z)
{
return result(x(), y(), z);
/** }
* Get distance to other point
*
* @param point other point
* @return distance
*/
double distTo(Vec point);
/**
* Get dot product (scalar multiplication)
*
* @param vec other vector
* @return dot product
*/
double dot(Vec vec);
/**
* Get vector size
*
* @return size
*/
double size();
/** /**
@ -66,31 +67,10 @@ interface VecMath<V> extends Vec {
* *
* @return result * @return result
*/ */
V abs(); public V abs()
{
return result(Math.abs(x()), Math.abs(y()), Math.abs(z()));
/** }
* @return true if zero
*/
boolean isZero();
/**
* Create vector from this point to other point
*
* @param point second point
* @return result
*/
V vecTo(Vec point);
/**
* Get middle of line to other point
*
* @param point other point
* @return result
*/
V midTo(Vec point);
/** /**
@ -99,7 +79,10 @@ interface VecMath<V> extends Vec {
* @param vec offset * @param vec offset
* @return result * @return result
*/ */
V add(Vec vec); public V add(Vec vec)
{
return add(vec.x(), vec.y(), vec.z());
}
/** /**
@ -110,7 +93,10 @@ interface VecMath<V> extends Vec {
* @param y y offset * @param y y offset
* @return result * @return result
*/ */
V add(double x, double y); public V add(double x, double y)
{
return add(x, y, 0);
}
/** /**
@ -121,7 +107,10 @@ interface VecMath<V> extends Vec {
* @param z z offset * @param z z offset
* @return result * @return result
*/ */
V add(double x, double y, double z); public V add(double x, double y, double z)
{
return result(x() + x, y() + y, z() + z);
}
/** /**
@ -129,7 +118,10 @@ interface VecMath<V> extends Vec {
* *
* @return result * @return result
*/ */
V half(); public V half()
{
return mul(0.5);
}
/** /**
@ -138,7 +130,10 @@ interface VecMath<V> extends Vec {
* @param d multiplier * @param d multiplier
* @return result * @return result
*/ */
V mul(double d); public V mul(double d)
{
return mul(d, d, d);
}
/** /**
@ -147,7 +142,10 @@ interface VecMath<V> extends Vec {
* @param vec vector of multipliers * @param vec vector of multipliers
* @return result * @return result
*/ */
V mul(Vec vec); public V mul(Vec vec)
{
return mul(vec.x(), vec.y(), vec.z());
}
/** /**
@ -158,7 +156,10 @@ interface VecMath<V> extends Vec {
* @param y y multiplier * @param y y multiplier
* @return result * @return result
*/ */
V mul(double x, double y); public V mul(double x, double y)
{
return mul(x, y, 1);
}
/** /**
@ -169,7 +170,10 @@ interface VecMath<V> extends Vec {
* @param z z multiplier * @param z z multiplier
* @return result * @return result
*/ */
V mul(double x, double y, double z); public V mul(double x, double y, double z)
{
return result(x() * x, y() * y, z() * z);
}
/** /**
@ -177,7 +181,10 @@ interface VecMath<V> extends Vec {
* *
* @return result * @return result
*/ */
V round(); public V round()
{
return result(Math.round(x()), Math.round(y()), Math.round(z()));
}
/** /**
@ -185,7 +192,10 @@ interface VecMath<V> extends Vec {
* *
* @return result * @return result
*/ */
V floor(); public V floor()
{
return result(Math.floor(x()), Math.floor(y()), Math.floor(z()));
}
/** /**
@ -193,7 +203,10 @@ interface VecMath<V> extends Vec {
* *
* @return result * @return result
*/ */
V ceil(); public V ceil()
{
return result(Math.ceil(x()), Math.ceil(y()), Math.ceil(z()));
}
/** /**
@ -202,7 +215,10 @@ interface VecMath<V> extends Vec {
* @param vec offset * @param vec offset
* @return result * @return result
*/ */
V sub(Vec vec); public V sub(Vec vec)
{
return sub(vec.x(), vec.y(), vec.z());
}
/** /**
@ -213,7 +229,10 @@ interface VecMath<V> extends Vec {
* @param y y offset * @param y y offset
* @return result * @return result
*/ */
V sub(double x, double y); public V sub(double x, double y)
{
return sub(x, y, 0);
}
/** /**
@ -224,7 +243,10 @@ interface VecMath<V> extends Vec {
* @param z z offset * @param z z offset
* @return result * @return result
*/ */
V sub(double x, double y, double z); public V sub(double x, double y, double z)
{
return result(x() - x, y() - y, z() - z);
}
/** /**
@ -232,7 +254,10 @@ interface VecMath<V> extends Vec {
* *
* @return result * @return result
*/ */
V neg(); public V neg()
{
return result(-x(), -y(), -z());
}
/** /**
@ -241,15 +266,43 @@ interface VecMath<V> extends Vec {
* @param size size we need * @param size size we need
* @return result * @return result
*/ */
V norm(double size); public V norm(double size)
{
if (isZero()) return result(x(), y(), z()); // can't norm zero vector
/**
* Get cross product (vector multiplication) final double k = size / size();
*
* @param vec other vector return mul(k);
* @return result }
*/
V cross(Vec vec);
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + Double.valueOf(x()).hashCode();
result = prime * result + Double.valueOf(y()).hashCode();
result = prime * result + Double.valueOf(z()).hashCode();
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Vec)) return false;
final Vec other = (Vec) obj;
return x() == other.x() && y() == other.y() && z() == other.z();
}
@Override
public String toString()
{
return String.format("(%.1f %.1f %.1f)", x(), y(), z());
}
} }

@ -1,398 +0,0 @@
package mightypork.utils.math.coord;
import mightypork.utils.math.constraints.NumberConstraint;
/**
* Implementation of coordinate methods
*
* @author MightyPork
* @param <V> Return type of methods
*/
abstract class VecMathImpl<V> implements VecMath<V> {
private NumberConstraint constraintZ, constraintY, constraintX;
private CoordProxy view = null;
@Override
public abstract double x();
@Override
public abstract double y();
@Override
public abstract double z();
@Override
public int xi()
{
return (int) Math.round(x());
}
@Override
public int yi()
{
return (int) Math.round(y());
}
@Override
public int zi()
{
return (int) Math.round(z());
}
@Override
public float xf()
{
return (float) x();
}
@Override
public float yf()
{
return (float) y();
}
@Override
public float zf()
{
return (float) z();
}
@Override
public VecView getVec()
{
return this.view();
}
/**
* <p>
* Some operation was performed and this result was obtained.
* </p>
* <p>
* It's now up to implementing class what to do - mutable ones can alter
* it's data values, immutable can return a new Vec.
* </p>
*
* @param x
* @param y
* @param z
* @return the result Vec
*/
public abstract V result(double x, double y, double z);
@Override
public VecMutable copy()
{
return new MutableCoord(this);
}
@Override
public VecView view()
{
if (view == null) view = new CoordProxy(this);
return view;
}
@Override
public NumberConstraint xc()
{
if (constraintX == null) constraintX = new NumberConstraint() {
@Override
public double getValue()
{
return x();
}
};
return constraintX;
}
@Override
public NumberConstraint yc()
{
if (constraintY == null) constraintY = new NumberConstraint() {
@Override
public double getValue()
{
return y();
}
};
return constraintY;
}
@Override
public NumberConstraint zc()
{
if (constraintZ == null) constraintZ = new NumberConstraint() {
@Override
public double getValue()
{
return z();
}
};
return constraintZ;
}
@Override
public V setX(double x)
{
return result(x, y(), z());
}
@Override
public V setY(double y)
{
return result(x(), y, z());
}
@Override
public V setZ(double z)
{
return result(x(), y(), z);
}
@Override
public double size()
{
final double x = x(), y = y(), z = z();
return Math.sqrt(x * x + y * y + z * z);
}
@Override
public boolean isZero()
{
return x() == 0 && y() == 0 && z() == 0;
}
@Override
public V abs()
{
return result(Math.abs(x()), Math.abs(y()), Math.abs(z()));
}
@Override
public V add(Vec vec)
{
return add(vec.x(), vec.y(), vec.z());
}
@Override
public V add(double x, double y)
{
return add(x, y, 0);
}
@Override
public V add(double x, double y, double z)
{
return result(x() + x, y() + y, z() + z);
}
@Override
public double distTo(Vec point)
{
final double dx = x() - point.x();
final double dy = y() - point.y();
final double dz = z() - point.z();
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
@Override
public V midTo(Vec point)
{
final double dx = (point.x() - x()) * 0.5;
final double dy = (point.y() - y()) * 0.5;
final double dz = (point.z() - z()) * 0.5;
return result(dx, dy, dz);
}
@Override
public V half()
{
return mul(0.5);
}
@Override
public V mul(double d)
{
return mul(d, d, d);
}
@Override
public V mul(Vec vec)
{
return mul(vec.x(), vec.y(), vec.z());
}
@Override
public V mul(double x, double y)
{
return mul(x, y, 1);
}
@Override
public V mul(double x, double y, double z)
{
return result(x() * x, y() * y, z() * z);
}
@Override
public V round()
{
return result(Math.round(x()), Math.round(y()), Math.round(z()));
}
@Override
public V floor()
{
return result(Math.floor(x()), Math.floor(y()), Math.floor(z()));
}
@Override
public V ceil()
{
return result(Math.ceil(x()), Math.ceil(y()), Math.ceil(z()));
}
@Override
public V sub(Vec vec)
{
return sub(vec.x(), vec.y(), vec.z());
}
@Override
public V sub(double x, double y)
{
return sub(x, y, 0);
}
@Override
public V sub(double x, double y, double z)
{
return result(x() - x, y() - y, z() - z);
}
@Override
public V vecTo(Vec point)
{
return result(point.x() - x(), point.y() - y(), point.z() - z());
}
@Override
public V cross(Vec vec)
{
//@formatter:off
return result(
y() * vec.z() - z() * vec.y(),
z() * vec.x() - x() * vec.z(),
x() * vec.y() - y() * vec.x());
//@formatter:on
}
@Override
public double dot(Vec vec)
{
return x() * vec.x() + y() * vec.y() + z() * vec.z();
}
@Override
public V neg()
{
return result(-x(), -y(), -z());
}
@Override
public V norm(double size)
{
if (isZero()) return result(x(), y(), z()); // can't norm zero vector
final double k = size / size();
return mul(k);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + Double.valueOf(x()).hashCode();
result = prime * result + Double.valueOf(y()).hashCode();
result = prime * result + Double.valueOf(z()).hashCode();
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Vec)) return false;
final Vec other = (Vec) obj;
return x() == other.x() && y() == other.y() && z() == other.z();
}
@Override
public String toString()
{
return String.format("(%.1f %.1f %.1f)", x(), y(), z());
}
}

@ -1,12 +1,139 @@
package mightypork.utils.math.coord; package mightypork.utils.math.coord;
import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing;
/** /**
* Mutable coord * Mutable coord
* *
* @author MightyPork * @author MightyPork
*/ */
public interface VecMutable extends VecMath<VecMutable> { public abstract class VecMutable extends VecMath<VecMutable> {
/**
* Get a variable initialized as zero (0,0,0)
*
* @return new mutable vector
*/
public static VecMutable zero()
{
return make(ZERO);
}
/**
* Get a variable initialized as one (1,1,1)
*
* @return one mutable vector
*/
public static VecMutable one()
{
return make(ONE);
}
/**
* Make new from coords
*
* @param x X coordinate
* @param y Y coordinate
* @return mutable vector
*/
public static VecMutable make(double x, double y)
{
return make(x, y, 0);
}
/**
* Make new as copy of another
*
* @param copied copied vec
* @return mutable vector
*/
public static VecMutable make(Vec copied)
{
return make(copied.x(), copied.y(), copied.z());
}
/**
* Make new from coords
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
* @return mutable vector
*/
public static VecMutable make(double x, double y, double z)
{
return new VecMutableImpl(x, y, z);
}
/**
* Create an animated vector; This way different easing / settings can be
* specified for each coordinate.
*
* @param animX x animator
* @param animY y animator
* @param animZ z animator
* @return animated mutable vector
*/
public static VecMutableAnim makeAnim(AnimDouble animX, AnimDouble animY, AnimDouble animZ)
{
return new VecMutableAnim(animX, animY, animZ);
}
/**
* Create an animated vector
*
* @param animStart initial positioon
* @param easing animation easing
* @return animated mutable vector
*/
public static VecMutableAnim makeAnim(Vec animStart, Easing easing)
{
return new VecMutableAnim(animStart, easing);
}
/**
* Create an animated vector, initialized at 0,0,0
*
* @param easing animation easing
* @return animated mutable vector
*/
public static VecMutableAnim makeAnim(Easing easing)
{
return new VecMutableAnim(Vec.ZERO, easing);
}
@Override
public abstract VecMutable result(double x, double y, double z);
@Override
public abstract double x();
@Override
public abstract double y();
@Override
public abstract double z();
public VecMutable reset()
{
return result(0, 0, 0);
}
/** /**
* Set coordinates to match other coord. * Set coordinates to match other coord.
@ -14,7 +141,10 @@ public interface VecMutable extends VecMath<VecMutable> {
* @param copied coord whose coordinates are used * @param copied coord whose coordinates are used
* @return result * @return result
*/ */
VecMutable setTo(Vec copied); public VecMutable setTo(Vec copied)
{
return result(copied.x(), copied.y(), copied.z());
}
/** /**
@ -25,7 +155,10 @@ public interface VecMutable extends VecMath<VecMutable> {
* @param y y coordinate * @param y y coordinate
* @return result * @return result
*/ */
VecMutable setTo(double x, double y); public VecMutable setTo(double x, double y)
{
return result(x, y, z());
}
/** /**
@ -36,5 +169,8 @@ public interface VecMutable extends VecMath<VecMutable> {
* @param z z coordinate * @param z z coordinate
* @return result * @return result
*/ */
VecMutable setTo(double x, double y, double z); public VecMutable setTo(double x, double y, double z)
{
return result(x, y, z);
}
} }

@ -0,0 +1,192 @@
package mightypork.utils.math.coord;
import mightypork.gamecore.control.timing.Pauseable;
import mightypork.gamecore.control.timing.Updateable;
import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing;
/**
* 3D coordinated with support for transitions, mutable.
*
* @author MightyPork
*/
public class VecMutableAnim extends VecMutable implements Pauseable, Updateable {
private final AnimDouble x, y, z;
private double defaultDuration = 0;
VecMutableAnim(AnimDouble x, AnimDouble y, AnimDouble z) {
this.x = x;
this.y = y;
this.z = z;
}
VecMutableAnim(Vec start, Easing easing) {
x = new AnimDouble(start.x(), easing);
y = new AnimDouble(start.y(), easing);
z = new AnimDouble(start.z(), easing);
}
@Override
public double x()
{
return x.now();
}
@Override
public double y()
{
return y.now();
}
@Override
public double z()
{
return z.now();
}
/**
* @return the default duration (seconds)
*/
public double getDefaultDuration()
{
return defaultDuration;
}
/**
* Set default animation duration (when changed without using animate())
*
* @param defaultDuration default duration (seconds)
*/
public void setDefaultDuration(double defaultDuration)
{
this.defaultDuration = defaultDuration;
}
@Override
public VecMutableAnim result(double x, double y, double z)
{
this.x.animate(x, defaultDuration);
this.y.animate(y, defaultDuration);
this.z.animate(z, defaultDuration);
return this;
}
public VecMutableAnim add(Vec offset, double speed)
{
animate(view().add(offset), speed);
return this;
}
public VecMutableAnim animate(double x, double y, double z, double duration)
{
this.x.animate(x, duration);
this.y.animate(y, duration);
this.z.animate(z, duration);
return this;
}
public VecMutableAnim animate(Vec target, double duration)
{
animate(target.x(), target.y(), target.z(), duration);
return this;
}
@Override
public void update(double delta)
{
x.update(delta);
y.update(delta);
z.update(delta);
}
@Override
public void pause()
{
x.pause();
y.pause();
z.pause();
}
@Override
public void resume()
{
x.resume();
y.resume();
z.resume();
}
@Override
public boolean isPaused()
{
return x.isPaused(); // BÚNO
}
/**
* @return true if the animation is finished
*/
public boolean isFinished()
{
return x.isFinished(); // BÚNO
}
/**
* @return current animation duration
*/
public double getDuration()
{
return x.getDuration(); // BÚNO
}
/**
* @return elapsed time since the start of the animation
*/
public double getElapsed()
{
return x.getElapsed(); // BÚNO
}
/**
* @return animation progress (elapsed / duration)
*/
public double getProgress()
{
return x.getProgress(); // BÚNO
}
/**
* Set easing for all three coordinates
*
* @param easing
*/
public void setEasing(Easing easing)
{
x.setEasing(easing);
y.setEasing(easing);
z.setEasing(easing);
}
}

@ -2,45 +2,57 @@ package mightypork.utils.math.coord;
/** /**
* Mutable vec default implementation * Mutable coordinate.<br>
* All Vec methods (except copy) alter data values and return this instance.
* *
* @author MightyPork * @author MightyPork
*/ */
abstract class VecMutableImpl extends VecMathImpl<VecMutable> implements VecMutable { class VecMutableImpl extends VecMutable {
@Override private double x, y, z;
public abstract double x();
@Override
public abstract double y();
@Override /**
public abstract double z(); * @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
*/
public VecMutableImpl(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
@Override @Override
public abstract VecMutable result(double x, double y, double z); public double x()
{
return x;
}
@Override @Override
public VecMutable setTo(Vec copied) public double y()
{ {
return result(copied.x(), copied.y(), copied.z()); return y;
} }
@Override @Override
public VecMutable setTo(double x, double y, double z) public double z()
{ {
return result(x, y, z); return z;
} }
@Override @Override
public VecMutable setTo(double x, double y) public VecMutableImpl result(double x, double y, double z)
{ {
return result(x, y, z()); this.x = x;
this.y = y;
this.z = z;
return this;
} }
} }

@ -0,0 +1,32 @@
package mightypork.utils.math.coord;
/**
* View of another coordinate, immutable.<br>
* GetVec()
*
* @author MightyPork
*/
class VecProxy extends AbstractVecProxy {
final Vec observed;
/**
* Protected, in order to enforce the use of view() method on Vec, which
* uses caching.
*
* @param observed
*/
public VecProxy(Vec observed) {
this.observed = observed;
}
@Override
protected Vec getSource()
{
return observed;
}
}

@ -1,24 +1,177 @@
package mightypork.utils.math.coord; package mightypork.utils.math.coord;
import mightypork.gamecore.control.interf.DefaultImpl;
import mightypork.utils.math.constraints.NumberConstraint;
/** /**
* Read-only coordinate, operations with it will yield a new * Read-only coordinate.
* {@link MutableCoord} with the result.
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class VecView extends VecMathImpl<VecView> { public abstract class VecView extends VecMath<VecView> {
/**
* Get a zero (0,0,0) constant
*
* @return new constant vec
*/
public static VecView zero()
{
return ZERO.view();
}
/**
* Get a one (1,1,1) constant
*
* @return one constant
*/
public static VecView one()
{
return ONE.view();
}
/**
* Make a constant vector
*
* @param x X value
* @param y Y value
* @return new constant vec
*/
public static VecView make(double x, double y)
{
return new ConstVec(x, y);
}
/**
* Make a constant vector
*
* @param x X value
* @param y Y value
* @param z Z value
* @return new constant vector
*/
public static VecView make(double x, double y, double z)
{
return new ConstVec(x, y, z);
}
/**
* Make a view at number constraints, reflecting their future changes.
*
* @param x X value
* @param y Y value
* @return view at the values
*/
public static VecView make(NumberConstraint x, NumberConstraint y)
{
return new NumConstrVec(x, y);
}
/**
* Make a view at number constraints, reflecting their future changes.
*
* @param x X value
* @param y Y value
* @param z Z value
* @return view at the values
*/
public static VecView make(NumberConstraint x, NumberConstraint y, NumberConstraint z)
{
return new NumConstrVec(x, y, z);
}
// synth views
private VecView view_round;
private VecView view_floor;
private VecView view_ceil;
private VecView view_neg;
private VecView view_half;
@Override @Override
public VecView result(double x, double y, double z) public VecView result(double x, double y, double z)
{ {
return new FixedCoord(x, y, z); return new ConstVec(x, y, z);
} }
@Override @Override
@Deprecated
public VecView view() public VecView view()
{ {
return this; // already not mutable return this; // already not mutable
} }
@Override
public abstract double x();
@Override
public abstract double y();
@Override
@DefaultImpl
public double z()
{
return 0; // implemented for ease with 2D anonymous subtypes
}
@Override
public VecView round()
{
// lazy init
if (view_round == null) view_round = new Synths.Round(this);
return view_round;
}
@Override
public VecView floor()
{
// lazy init
if (view_floor == null) view_floor = new Synths.Floor(this);
return view_floor;
}
@Override
public VecView ceil()
{
// lazy init
if (view_ceil == null) view_ceil = new Synths.Ceil(this);
return view_ceil;
}
@Override
public VecView half()
{
// lazy init
if (view_half == null) view_half = new Synths.Half(this);
return view_half;
}
@Override
public VecView neg()
{
// lazy init
if (view_neg == null) view_neg = new Synths.Neg(this);
return view_neg;
}
} }

@ -7,53 +7,62 @@ import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
public abstract class RectImpl<T extends Rect> implements RectMath<T> { /**
* Abstract {@link Rect}, implementing all but the data getters
*
* @author MightyPork
*/
public abstract class AbstractRect implements Rect {
private VecConstraint tl, tc, tr, cl, c, cr, bl, bc, br; private VecConstraint tl;
private VecConstraint tc;
private VecConstraint tr;
private VecConstraint cl;
private VecConstraint c;
private VecConstraint cr;
private VecConstraint bl;
private VecConstraint bc;
private VecConstraint br;
@Override @Override
public RectView getRect() public final RectValue getRect()
{ {
return this.view(); return this.view();
} }
@Override @Override
public abstract VecView getOrigin(); public final VecView getTopLeft()
@Override
public abstract VecView getSize();
@Override
public VecView getTopLeft()
{ {
// lazy init
if (tl == null) tl = cTopLeft(this); if (tl == null) tl = cTopLeft(this);
return tl.getVec(); return tl.getVec();
} }
@Override @Override
public VecView getTopCenter() public final VecView getTopCenter()
{ {
// lazy init
if (tc == null) tc = cTopCenter(this); if (tc == null) tc = cTopCenter(this);
return tc.getVec(); return tc.getVec();
} }
@Override @Override
public VecView getTopRight() public final VecView getTopRight()
{ {
// lazy init
if (tr == null) tr = cTopRight(this); if (tr == null) tr = cTopRight(this);
return tr.getVec(); return tr.getVec();
} }
@Override @Override
public VecView getCenterLeft() public final VecView getCenterLeft()
{ {
// lazy init
if (cl == null) cl = cCenterLeft(this); if (cl == null) cl = cCenterLeft(this);
return cl.getVec(); return cl.getVec();
} }
@ -62,38 +71,43 @@ public abstract class RectImpl<T extends Rect> implements RectMath<T> {
@Override @Override
public final VecView getCenter() public final VecView getCenter()
{ {
// lazy init
if (c == null) c = cCenter(this); if (c == null) c = cCenter(this);
return c.getVec(); return c.getVec();
} }
@Override @Override
public VecView getCenterRight() public final VecView getCenterRight()
{ {
// lazy init
if (cr == null) cr = cCenterRight(this); if (cr == null) cr = cCenterRight(this);
return cr.getVec(); return cr.getVec();
} }
@Override @Override
public VecView getBottomLeft() public final VecView getBottomLeft()
{ {
// lazy init
if (bl == null) bl = cBottomLeft(this); if (bl == null) bl = cBottomLeft(this);
return bl.getVec(); return bl.getVec();
} }
@Override @Override
public VecView getBottomCenter() public final VecView getBottomCenter()
{ {
// lazy init
if (bc == null) bc = cBottomCenter(this); if (bc == null) bc = cBottomCenter(this);
return bc.getVec(); return bc.getVec();
} }
@Override @Override
public VecView getBottomRight() public final VecView getBottomRight()
{ {
// lazy init
if (br == null) br = cBottomRight(this); if (br == null) br = cBottomRight(this);
return br.getVec(); return br.getVec();
} }
@ -142,51 +156,23 @@ public abstract class RectImpl<T extends Rect> implements RectMath<T> {
@Override @Override
public final T move(Vec move) public RectValue view()
{ {
return move(move.x(), move.y()); return new RectProxy(this);
}
@Override
public final T shrink(Vec shrink)
{
return shrink(shrink.x(), shrink.y());
}
@Override
public final T shrink(double x, double y)
{
return shrink(x, x, y, y);
}
@Override
public final T grow(Vec grow)
{
return grow(grow.x(), grow.y());
}
@Override
public final T grow(double x, double y)
{
return grow(x, x, y, y);
} }
@Override @Override
public RectView view() public final RectMutable mutable()
{ {
return new RectProxy(this); return RectMutable.make(this);
} }
@Override @Override
public RectMutable copy() public RectValue value()
{ {
return new MutableRect(this); return RectValue.make(getOrigin(), getSize());
} }
@ -210,4 +196,5 @@ public abstract class RectImpl<T extends Rect> implements RectMath<T> {
{ {
return String.format("Rect { %s - %s }", getOrigin().toString(), getOrigin().add(getSize())); return String.format("Rect { %s - %s }", getOrigin().toString(), getOrigin().add(getSize()));
} }
} }

@ -0,0 +1,47 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.coord.VecView;
class ConstRect extends RectValue {
private final VecView pos;
private final VecView size;
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
*/
public ConstRect(double x, double y, double width, double height) {
pos = VecView.make(x, y);
size = VecView.make(Math.abs(width), Math.abs(height));
}
@Override
public ConstRect value()
{
return this; // nothing can change.
}
@Override
public VecView getOrigin()
{
return pos;
}
@Override
public VecView getSize()
{
return size;
}
}

@ -1,105 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.coord.FixedCoord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
public class FixedRect extends RectView {
private final VecView pos;
private final VecView size;
/**
* Create at 0,0 with zero size
*/
public FixedRect() {
pos = Vec.ZERO;
size = Vec.ZERO;
}
/**
* Create at 0,0 with given size
*
* @param width
* @param height
*/
public FixedRect(double width, double height) {
this(0, 0, width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param width
* @param height
*/
public FixedRect(Vec origin, double width, double height) {
this(origin.x(), origin.y(), width, height);
}
/**
* Create at 0,0 with given size.
*
* @param size
*/
public FixedRect(Vec size) {
this(0, 0, size.x(), size.y());
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
*/
public FixedRect(Vec origin, Vec size) {
this(origin.x(), origin.y(), size.x(), size.y());
}
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
*/
public FixedRect(double x, double y, double width, double height) {
pos = new FixedCoord(x, y);
size = new FixedCoord(Math.abs(width), Math.abs(height));
}
/**
* Create as copy of another
*
* @param other copied
*/
public FixedRect(Rect other) {
this(other.getOrigin(), other.getSize());
}
@Override
public VecView getOrigin()
{
return pos;
}
@Override
public VecView getSize()
{
return size;
}
}

@ -1,233 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView;
public class MutableRect extends RectImpl<RectMutable> implements RectMutable {
private final VecMutable pos = new MutableCoord();
private final VecMutable size = new MutableCoord();
/**
* Create at 0,0 with zero size
*/
public MutableRect() {
// keep default zeros
}
/**
* Create at 0,0 with given size
*
* @param width
* @param height
*/
public MutableRect(double width, double height) {
this.pos.setTo(0, 0);
this.size.setTo(width, height).abs();
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param width
* @param height
*/
public MutableRect(Vec origin, double width, double height) {
this.pos.setTo(origin);
this.size.setTo(width, height).abs();
}
/**
* Create at 0,0 with given size.
*
* @param size
*/
public MutableRect(Vec size) {
this(0, 0, size.x(), size.y());
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
*/
public MutableRect(Vec origin, Vec size) {
this.pos.setTo(origin);
this.size.setTo(size).abs();
}
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
*/
public MutableRect(double x, double y, double width, double height) {
pos.setTo(x, y);
size.setTo(width, height).abs();
}
/**
* Create as copy of another
*
* @param other copied
*/
public MutableRect(Rect other) {
this(other.getOrigin(), other.getSize());
}
/**
* Set to other rect's coordinates
*
* @param rect other rect
*/
@Override
public void setTo(Rect rect)
{
setTo(rect.getOrigin(), rect.getSize());
}
@Override
public void setTo(Vec origin, Vec size)
{
this.pos.setTo(origin);
this.size.setTo(size).abs();
}
@Override
public void setTo(Vec origin, double width, double height)
{
this.pos.setTo(origin);
this.size.setTo(width, height).abs();
}
@Override
public void setOrigin(Vec origin)
{
this.pos.setTo(origin);
}
@Override
public void setSize(Vec size)
{
this.size.setTo(size).abs();
}
/**
* Add X and Y to origin
*
* @param x x to add
* @param y y to add
* @return result
*/
@Override
public RectMutable move(double x, double y)
{
pos.add(x, y);
return this;
}
/**
* Get a copy
*
* @return copy
*/
@Override
public RectMutable copy()
{
return new MutableRect(this);
}
/**
* Shrink the rect
*
* @param left shrink
* @param right shrink
* @param top shrink
* @param bottom shrink
* @return result
*/
@Override
public RectMutable shrink(double left, double right, double top, double bottom)
{
pos.add(left, top);
size.sub(left + right, top + bottom).abs();
return this;
}
/**
* Grow the rect
*
* @param left growth
* @param right growth
* @param top growth
* @param bottom growth
* @return result
*/
@Override
public RectMutable grow(double left, double right, double top, double bottom)
{
pos.sub(left, top);
size.add(left + right, top + bottom).abs();
return this;
}
/**
* Round coords
*
* @return result
*/
@Override
public RectMutable round()
{
pos.round();
size.round();
return this;
}
@Override
public VecView getOrigin()
{
return pos.view();
}
@Override
public VecView getSize()
{
return size.view();
}
@Override
public RectView view()
{
return new FixedRect(this);
}
}

@ -2,6 +2,7 @@ package mightypork.utils.math.rect;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
@ -12,8 +13,8 @@ import mightypork.utils.math.coord.VecView;
*/ */
public interface Rect extends RectConstraint { public interface Rect extends RectConstraint {
RectView ONE = new FixedRect(0, 0, 1, 1); RectValue ONE = new ConstRect(0, 0, 1, 1);
RectView ZERO = new FixedRect(0, 0, 0, 0); RectValue ZERO = new ConstRect(0, 0, 0, 0);
/** /**
@ -21,15 +22,23 @@ public interface Rect extends RectConstraint {
* *
* @return copy * @return copy
*/ */
RectMutable copy(); RectMutable mutable();
/** /**
* Get a readonly copy * Get a copy of current value
* *
* @return copy * @return copy
*/ */
RectView view(); RectValue value();
/**
* Get a proxying view
*
* @return copy
*/
RectValue view();
/** /**
@ -85,4 +94,13 @@ public interface Rect extends RectConstraint {
double yMax(); double yMax();
/**
* Check if point is inside this rectangle
*
* @param point point to test
* @return is inside
*/
boolean contains(Vec point);
} }

@ -4,12 +4,7 @@ package mightypork.utils.math.rect;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
/** abstract class RectMath<T extends Rect> extends AbstractRect {
* Operations available in rects
*
* @author MightyPork
*/
interface RectMath<T extends Rect> extends Rect {
/** /**
* Add vector to origin * Add vector to origin
@ -17,7 +12,10 @@ interface RectMath<T extends Rect> extends Rect {
* @param move offset vector * @param move offset vector
* @return result * @return result
*/ */
T move(Vec move); public T move(Vec move)
{
return move(move.x(), move.y());
}
/** /**
@ -27,7 +25,7 @@ interface RectMath<T extends Rect> extends Rect {
* @param y y to add * @param y y to add
* @return result * @return result
*/ */
T move(double x, double y); public abstract T move(double x, double y);
/** /**
@ -36,7 +34,11 @@ interface RectMath<T extends Rect> extends Rect {
* @param shrink shrink size (horisontal and vertical) * @param shrink shrink size (horisontal and vertical)
* @return result * @return result
*/ */
T shrink(Vec shrink);
public T shrink(Vec shrink)
{
return shrink(shrink.x(), shrink.y());
}
/** /**
@ -46,7 +48,10 @@ interface RectMath<T extends Rect> extends Rect {
* @param y vertical shrink * @param y vertical shrink
* @return result * @return result
*/ */
T shrink(double x, double y); public T shrink(double x, double y)
{
return shrink(x, x, y, y);
}
/** /**
@ -58,7 +63,7 @@ interface RectMath<T extends Rect> extends Rect {
* @param bottom shrink * @param bottom shrink
* @return result * @return result
*/ */
T shrink(double left, double right, double top, double bottom); public abstract T shrink(double left, double right, double top, double bottom);
/** /**
@ -67,7 +72,10 @@ interface RectMath<T extends Rect> extends Rect {
* @param grow grow size (added to each side) * @param grow grow size (added to each side)
* @return grown copy * @return grown copy
*/ */
T grow(Vec grow); public final T grow(Vec grow)
{
return grow(grow.x(), grow.y());
}
/** /**
@ -77,7 +85,10 @@ interface RectMath<T extends Rect> extends Rect {
* @param y vertical grow * @param y vertical grow
* @return result * @return result
*/ */
T grow(double x, double y); public final T grow(double x, double y)
{
return grow(x, x, y, y);
}
/** /**
@ -89,16 +100,7 @@ interface RectMath<T extends Rect> extends Rect {
* @param bottom growth * @param bottom growth
* @return result * @return result
*/ */
T grow(double left, double right, double top, double bottom); public abstract T grow(double left, double right, double top, double bottom);
/**
* Check if point is inside this rectangle
*
* @param point point to test
* @return is inside
*/
boolean contains(Vec point);
/** /**
@ -106,6 +108,5 @@ interface RectMath<T extends Rect> extends Rect {
* *
* @return result * @return result
*/ */
T round(); public abstract T round();
} }

@ -2,27 +2,173 @@ package mightypork.utils.math.rect;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
public interface RectMutable extends RectMath<RectMutable> { /**
* Mutable rectangle; operations change it's state.
*
* @author MightyPork
*/
public abstract class RectMutable extends RectMath<RectMutable> {
/**
* Create at 0,0 with zero size
*
* @return new mutable rect
*/
public static RectMutable zero()
{
return make(0, 0, 0, 0);
}
/**
* Create at 1,1 with zero size
*
* @return new mutable rect
*/
public static RectMutable one()
{
return make(0, 0, 1, 1);
}
/**
* Create at 0,0 with given size
*
* @param width
* @param height
* @return new mutable rect
*/
public static RectMutable make(double width, double height)
{
return make(0, 0, width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param width
* @param height
* @return new mutable rect
*/
public static RectMutable make(Vec origin, double width, double height)
{
return make(origin, VecView.make(width, height));
}
/**
* Create at 0,0 with given size.
*
* @param size
* @return new mutable rect
*/
public static RectMutable make(Vec size)
{
return make(VecView.zero(), size);
}
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
* @return new mutable rect
*/
public static RectMutable make(double x, double y, double width, double height)
{
return make(VecView.make(x, y), VecView.make(width, height));
}
/**
* Create as copy of another
*
* @param other copied
* @return new mutable rect
*/
public static RectMutable make(Rect other)
{
return make(other.getOrigin(), other.getSize());
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
* @return new mutable rect
*/
public static RectMutable make(Vec origin, Vec size)
{
return new RectMutableImpl(origin, size);
}
/** /**
* Set to other rect's coordinates * Set to other rect's coordinates
* *
* @param rect other rect * @param rect other rect
* @return this
*/ */
void setTo(Rect rect); public RectMutable setTo(Rect rect)
{
return setTo(rect.getOrigin(), rect.getSize());
}
void setTo(Vec origin, Vec size); /**
* Set to given size and position
*
* @param origin new origin
* @param width new width
* @param height new height
* @return this
*/
public RectMutable setTo(Vec origin, double width, double height)
{
return setTo(origin, VecView.make(width, height));
}
void setTo(Vec origin, double width, double height); /**
* Set to given size and position
*
* @param origin new origin
* @param size new size
* @return this
*/
public RectMutable setTo(Vec origin, Vec size)
{
setOrigin(origin);
setSize(size);
return this;
}
void setOrigin(Vec origin); /**
* Set new origin
*
* @param origin new origin
* @return this
*/
public abstract RectMutable setOrigin(Vec origin);
void setSize(Vec size); /**
* Set new size
*
* @param size new size
* @return this
*/
public abstract RectMutable setSize(Vec size);
} }

@ -0,0 +1,120 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView;
class RectMutableImpl extends RectMutable {
final VecMutable pos = VecMutable.zero();
final VecMutable size = VecMutable.zero();
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
*/
public RectMutableImpl(Vec origin, Vec size) {
this.pos.setTo(origin);
this.size.setTo(size).abs();
}
/**
* Add X and Y to origin
*
* @param x x to add
* @param y y to add
* @return result
*/
@Override
public RectMutableImpl move(double x, double y)
{
pos.add(x, y);
return this;
}
/**
* Shrink the rect
*
* @param left shrink
* @param right shrink
* @param top shrink
* @param bottom shrink
* @return result
*/
@Override
public RectMutableImpl shrink(double left, double right, double top, double bottom)
{
pos.add(left, top);
size.sub(left + right, top + bottom).abs();
return this;
}
/**
* Grow the rect
*
* @param left growth
* @param right growth
* @param top growth
* @param bottom growth
* @return result
*/
@Override
public RectMutableImpl grow(double left, double right, double top, double bottom)
{
pos.sub(left, top);
size.add(left + right, top + bottom).abs();
return this;
}
/**
* Round coords
*
* @return result
*/
@Override
public RectMutableImpl round()
{
pos.round();
size.round();
return this;
}
@Override
public VecView getOrigin()
{
return pos.view();
}
@Override
public VecView getSize()
{
return size.view();
}
@Override
public RectMutable setOrigin(Vec origin)
{
this.pos.setTo(origin);
return this;
}
@Override
public RectMutable setSize(Vec size)
{
this.size.setTo(size).abs();
return this;
}
}

@ -9,7 +9,7 @@ import mightypork.utils.math.coord.VecView;
* *
* @author MightyPork * @author MightyPork
*/ */
public class RectProxy extends RectView { public class RectProxy extends RectValue {
private final Rect observed; private final Rect observed;

@ -0,0 +1,138 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
/**
* Immutable rect
*
* @author MightyPork
*/
public abstract class RectValue extends RectMath<RectValue> {
/**
* Create at 0,0 with zero size
*
* @return new mutable rect
*/
public static RectValue zero()
{
return make(0, 0, 0, 0);
}
/**
* Create at 1,1 with zero size
*
* @return new mutable rect
*/
public static RectValue one()
{
return make(0, 0, 1, 1);
}
/**
* Create at 0,0 with given size
*
* @param width
* @param height
* @return new mutable rect
*/
public static RectValue make(double width, double height)
{
return make(0, 0, width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param width
* @param height
* @return new mutable rect
*/
public static RectValue make(Vec origin, double width, double height)
{
return make(origin, VecView.make(width, height));
}
/**
* Create at 0,0 with given size.
*
* @param size
* @return new mutable rect
*/
public static RectValue make(Vec size)
{
return make(VecView.zero(), size);
}
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
* @return new mutable rect
*/
public static RectValue make(double x, double y, double width, double height)
{
return new ConstRect(x, y, width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
* @return new mutable rect
*/
public static RectValue make(Vec origin, Vec size)
{
return make(origin.x(), origin.y(), size.x(), size.y());
}
@Override
public RectValue move(double x, double y)
{
return RectValue.make(getOrigin().add(x, y), getSize());
}
@Override
public RectValue shrink(double left, double right, double top, double bottom)
{
return RectValue.make(getOrigin().add(left, top), getSize().sub(left + right, top + bottom));
}
@Override
public RectValue grow(double left, double right, double top, double bottom)
{
return RectValue.make(getOrigin().sub(left, top), getSize().add(left + right, top + bottom));
}
@Override
public RectValue round()
{
return RectValue.make(getOrigin().round(), getSize().round());
}
@Override
public RectValue view()
{
return this;
}
}

@ -1,69 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.coord.VecView;
/**
* Immutable rect
*
* @author MightyPork
*/
public abstract class RectView extends RectImpl<RectView> {
protected RectView result(VecView origin, VecView size)
{
return new FixedRect(origin, size);
}
@Override
public RectView move(double x, double y)
{
return result(getOrigin().add(x, y), getSize());
}
@Override
public RectView shrink(double left, double right, double top, double bottom)
{
return result(getOrigin().add(left, top), getSize().sub(left + right, top + bottom));
}
@Override
public RectView grow(double left, double right, double top, double bottom)
{
return result(getOrigin().sub(left, top), getSize().add(left + right, top + bottom));
}
@Override
public RectView round()
{
return result(getOrigin().round(), getSize().round());
}
@Override
public MutableRect copy()
{
return new MutableRect(this);
}
@Override
public RectView view()
{
return this;
}
@Override
public abstract VecView getOrigin();
@Override
public abstract VecView getSize();
}

@ -3,7 +3,6 @@ package mightypork.utils.objects;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.math.Range; import mightypork.utils.math.Range;
import mightypork.utils.math.coord.FixedCoord;
import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView; import mightypork.utils.math.coord.VecView;
@ -155,8 +154,8 @@ public class Convert {
public static VecView toCoord(Object o, Vec def) public static VecView toCoord(Object o, Vec def)
{ {
try { try {
if (o == null) return new FixedCoord(def); if (o == null) return def.value();
if (o instanceof Vec) return new FixedCoord((Vec) o); if (o instanceof Vec) return ((Vec) o).value();
if (o instanceof String) { if (o instanceof String) {
String s = ((String) o).trim().toUpperCase(); String s = ((String) o).trim().toUpperCase();
@ -172,19 +171,19 @@ public class Convert {
final double y = Double.parseDouble(parts[1].trim()); final double y = Double.parseDouble(parts[1].trim());
if (parts.length == 2) { if (parts.length == 2) {
return new FixedCoord(x, y); return VecView.make(x, y);
} }
final double z = Double.parseDouble(parts[2].trim()); final double z = Double.parseDouble(parts[2].trim());
return new FixedCoord(x, y, z); return VecView.make(x, y, z);
} }
} }
} catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) { } catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) {
// ignore // ignore
} }
return new FixedCoord(def); return def.value();
} }
@ -292,7 +291,7 @@ public class Convert {
*/ */
public static VecView toCoord(Object o) public static VecView toCoord(Object o)
{ {
return toCoord(o, Vec.ZERO); return toCoord(o, VecView.zero());
} }

@ -45,6 +45,14 @@ public class Mutable<T> {
} }
@Override
public String toString()
{
if (o == null) return "<null>";
return o.toString();
}
@Override @Override
public int hashCode() public int hashCode()
{ {
@ -61,21 +69,10 @@ public class Mutable<T> {
if (this == obj) return true; if (this == obj) return true;
if (obj == null) return false; if (obj == null) return false;
if (!(obj instanceof Mutable)) return false; if (!(obj instanceof Mutable)) return false;
final Mutable<?> other = (Mutable<?>) obj; final Mutable<?> other = (Mutable<?>) obj;
if (o == null) { if (o == null) {
if (other.o != null) return false; if (other.o != null) return false;
} else if (!o.equals(other.o)) { } else if (!o.equals(other.o)) return false;
return false;
}
return true; return true;
} }
@Override
public String toString()
{
if (o == null) return "<null>";
return o.toString();
}
} }

Loading…
Cancel
Save