diff --git a/src/mightypork/gamecore/audio/SoundSystem.java b/src/mightypork/gamecore/audio/SoundSystem.java index 7d7c7a4..cbe42a7 100644 --- a/src/mightypork/gamecore/audio/SoundSystem.java +++ b/src/mightypork/gamecore/audio/SoundSystem.java @@ -12,7 +12,6 @@ import mightypork.gamecore.control.bus.clients.RootBusNode; import mightypork.gamecore.control.bus.events.ResourceLoadRequest; import mightypork.gamecore.control.timing.Updateable; import mightypork.utils.math.Calc.Buffers; -import mightypork.utils.math.coord.MutableCoord; import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.VecMutable; 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 int MAX_SOURCES = 256; - private static VecMutable listener = new MutableCoord(0, 0, 0); - - private static boolean inited; + private static VecMutable listener = VecMutable.zero(); + private static boolean soundSystemInited = false; /** @@ -45,10 +43,10 @@ public class SoundSystem extends RootBusNode implements Updateable { public static void setListener(Vec pos) { listener.setTo(pos); - FloatBuffer buf3 = Buffers.alloc(3); - FloatBuffer buf6 = Buffers.alloc(6); + final FloatBuffer buf3 = Buffers.alloc(3); + final FloatBuffer buf6 = Buffers.alloc(6); 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); buf3.clear(); Buffers.fill(buf3, 0, 0, 0); @@ -56,7 +54,6 @@ public class SoundSystem extends RootBusNode implements Updateable { buf6.clear(); Buffers.fill(buf6, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f); AL10.alListener(AL10.AL_ORIENTATION, buf6); - buf3 = buf6 = null; } @@ -84,13 +81,12 @@ public class SoundSystem extends RootBusNode implements Updateable { public SoundSystem(AppAccess app) { super(app); - if (!inited) { + if (!soundSystemInited) { SoundStore.get().setMaxSources(MAX_SOURCES); SoundStore.get().init(); - setListener(INITIAL_LISTENER_POS); - inited = true; + soundSystemInited = true; } } diff --git a/src/mightypork/gamecore/control/bus/EventBus.java b/src/mightypork/gamecore/control/bus/EventBus.java index b388fe6..a309ff6 100644 --- a/src/mightypork/gamecore/control/bus/EventBus.java +++ b/src/mightypork/gamecore/control/bus/EventBus.java @@ -293,7 +293,7 @@ final public class EventBus implements Destroyable { @Override 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)); } diff --git a/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java b/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java index e69a8ff..0f5bbe7 100644 --- a/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java +++ b/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java @@ -14,8 +14,8 @@ import mightypork.utils.math.coord.VecView; @UnloggedEvent public class MouseMotionEvent implements Event { - private final Vec move; - private final Vec pos; + private final VecView move; + private final VecView pos; /** @@ -23,8 +23,8 @@ public class MouseMotionEvent implements Event { * @param move move vector */ public MouseMotionEvent(Vec pos, Vec move) { - this.move = move; - this.pos = pos; + this.move = move.value(); + this.pos = pos.value(); } @@ -33,7 +33,7 @@ public class MouseMotionEvent implements Event { */ public VecView getMove() { - return move.view(); + return move; } @@ -42,7 +42,7 @@ public class MouseMotionEvent implements Event { */ public VecView getPos() { - return pos.view(); + return pos; } diff --git a/src/mightypork/gamecore/control/timing/TimerFps.java b/src/mightypork/gamecore/control/timing/TimerFps.java index 4759175..087db15 100644 --- a/src/mightypork/gamecore/control/timing/TimerFps.java +++ b/src/mightypork/gamecore/control/timing/TimerFps.java @@ -23,7 +23,7 @@ public class TimerFps { * @param fps target FPS */ public TimerFps(long fps) { - FRAME = Math.round(SECOND / fps); + FRAME = Math.round(SECOND / (double) fps); lastFrame = System.nanoTime(); nextFrame = System.nanoTime() + FRAME; diff --git a/src/mightypork/gamecore/gui/components/PluggableRenderable.java b/src/mightypork/gamecore/gui/components/PluggableRenderable.java index 3255a64..3495313 100644 --- a/src/mightypork/gamecore/gui/components/PluggableRenderable.java +++ b/src/mightypork/gamecore/gui/components/PluggableRenderable.java @@ -1,9 +1,9 @@ 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.rect.RectView; +import mightypork.utils.math.rect.RectValue; /** @@ -11,14 +11,14 @@ import mightypork.utils.math.rect.RectView; * * @author MightyPork */ -public interface PluggableRenderable extends Renderable, PluggableContext { +public interface PluggableRenderable extends Renderable, PluggableRect { @Override void render(); @Override - RectView getRect(); + RectValue getRect(); @Override diff --git a/src/mightypork/gamecore/gui/components/PluggableRenderer.java b/src/mightypork/gamecore/gui/components/PluggableRenderer.java index bbaf463..46cd859 100644 --- a/src/mightypork/gamecore/gui/components/PluggableRenderer.java +++ b/src/mightypork/gamecore/gui/components/PluggableRenderer.java @@ -3,7 +3,7 @@ package mightypork.gamecore.gui.components; import mightypork.utils.math.constraints.ContextAdapter; 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 - public RectView getRect() + public RectValue getRect() { return super.getRect(); } diff --git a/src/mightypork/gamecore/gui/components/layout/ElementHolder.java b/src/mightypork/gamecore/gui/components/layout/ElementHolder.java index 997759f..0bab0ec 100644 --- a/src/mightypork/gamecore/gui/components/layout/ElementHolder.java +++ b/src/mightypork/gamecore/gui/components/layout/ElementHolder.java @@ -10,7 +10,7 @@ import mightypork.gamecore.gui.components.PluggableRenderable; import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.gui.components.Renderable; 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 - public RectView getRect() + public RectValue getRect() { return context.getRect(); } diff --git a/src/mightypork/gamecore/gui/components/painters/TextPainter.java b/src/mightypork/gamecore/gui/components/painters/TextPainter.java index 2384b47..6887cc1 100644 --- a/src/mightypork/gamecore/gui/components/painters/TextPainter.java +++ b/src/mightypork/gamecore/gui/components/painters/TextPainter.java @@ -6,10 +6,9 @@ import mightypork.gamecore.render.fonts.FontRenderer; import mightypork.gamecore.render.fonts.FontRenderer.Align; import mightypork.gamecore.render.fonts.GLFont; import mightypork.utils.math.color.RGB; -import mightypork.utils.math.coord.MutableCoord; import mightypork.utils.math.coord.Vec; 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.StringWrapper; @@ -30,7 +29,7 @@ public class TextPainter extends PluggableRenderer { private boolean shadow; 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; final String str = text.getString(); - final RectView rect = getRect(); + final RectValue rect = getRect(); if (shadow) { font.draw(str, rect.move(shadowOffset), align, shadowColor); diff --git a/src/mightypork/gamecore/gui/screens/Screen.java b/src/mightypork/gamecore/gui/screens/Screen.java index d5f26ca..37aef14 100644 --- a/src/mightypork/gamecore/gui/screens/Screen.java +++ b/src/mightypork/gamecore/gui/screens/Screen.java @@ -12,7 +12,7 @@ import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.render.Render; import mightypork.utils.math.constraints.RectConstraint; 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 - public RectView getRect() + public RectValue getRect() { return getDisplay().getRect(); } diff --git a/src/mightypork/gamecore/gui/screens/ScreenLayer.java b/src/mightypork/gamecore/gui/screens/ScreenLayer.java index 05b84a2..c61e6ef 100644 --- a/src/mightypork/gamecore/gui/screens/ScreenLayer.java +++ b/src/mightypork/gamecore/gui/screens/ScreenLayer.java @@ -10,7 +10,7 @@ import mightypork.gamecore.input.KeyStroke; import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.coord.Vec; 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 children = listDirectory(f); if (children.size() == 0) { - f.delete(); + if (!f.delete()) throw new IOException("Could not delete a directory: " + f); continue; } } diff --git a/src/mightypork/utils/files/InstanceLock.java b/src/mightypork/utils/files/InstanceLock.java index 6eabfcd..6b7a43b 100644 --- a/src/mightypork/utils/files/InstanceLock.java +++ b/src/mightypork/utils/files/InstanceLock.java @@ -31,7 +31,7 @@ public class InstanceLock { try { fileLock.release(); randomAccessFile.close(); - lockFile.delete(); + if (!lockFile.delete()) throw new IOException(); } catch (final Exception e) { System.err.println("Unable to remove lock file."); e.printStackTrace(); diff --git a/src/mightypork/utils/files/ZipBuilder.java b/src/mightypork/utils/files/ZipBuilder.java index abfa0e2..875baa6 100644 --- a/src/mightypork/utils/files/ZipBuilder.java +++ b/src/mightypork/utils/files/ZipBuilder.java @@ -1,7 +1,11 @@ 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.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -22,11 +26,11 @@ public class ZipBuilder { /** * @param target target zip file - * @throws FileNotFoundException if the file is directory or cannot be - * created + * @throws IOException if the file is directory or cannot be created */ - public ZipBuilder(File target) throws FileNotFoundException { - target.getParentFile().mkdirs(); + public ZipBuilder(File target) throws IOException { + + if (!target.getParentFile().mkdirs()) throw new IOException("Could not create output directory."); final FileOutputStream dest = new FileOutputStream(target); out = new ZipOutputStream(new BufferedOutputStream(dest)); diff --git a/src/mightypork/utils/files/ZipUtils.java b/src/mightypork/utils/files/ZipUtils.java index a0e2f94..faa330d 100644 --- a/src/mightypork/utils/files/ZipUtils.java +++ b/src/mightypork/utils/files/ZipUtils.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import mightypork.utils.logging.Log; import mightypork.utils.string.validation.StringFilter; @@ -45,13 +46,13 @@ public class ZipUtils { * @param outputDir target directory * @param filter string filter (will be used to test entry names (paths)) * @return list of entries extracted (paths) - * @throws IOException + * @throws IOException if the file)s) cannot be created */ public static List extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException { final ArrayList files = new ArrayList<>(); - outputDir.mkdirs(); + if (!outputDir.mkdirs()) throw new IOException("Could not create output directory."); final Enumeration zipFileEntries = zip.entries(); @@ -67,7 +68,7 @@ public class ZipUtils { if (entry.isDirectory() || (filter != null && !filter.accept(entryPath))) continue; // make sure directories exist - destinationParent.mkdirs(); + if (!destinationParent.mkdirs()) throw new IOException("Could not create directory."); if (!entry.isDirectory()) { extractZipEntry(zip, entry, destFile); @@ -130,7 +131,7 @@ public class ZipUtils { */ 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); BufferedInputStream is = new BufferedInputStream(in); @@ -171,7 +172,8 @@ public class ZipUtils { { try(ZipFile zf = new ZipFile(selectedFile)) { return zf.getEntry(string) != null; - } catch (final Exception e) { + } catch (final IOException | RuntimeException e) { + Log.w("Error reading zip.", e); return false; } diff --git a/src/mightypork/utils/files/ion/BinaryUtils.java b/src/mightypork/utils/files/ion/BinaryUtils.java index 042e371..9efde90 100644 --- a/src/mightypork/utils/files/ion/BinaryUtils.java +++ b/src/mightypork/utils/files/ion/BinaryUtils.java @@ -176,7 +176,7 @@ public class BinaryUtils { 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); return buf.getChar(); } @@ -184,7 +184,7 @@ public class BinaryUtils { 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); return buf.getShort(); } @@ -192,7 +192,8 @@ public class BinaryUtils { 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); return buf.getLong(); } @@ -200,7 +201,7 @@ public class BinaryUtils { 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); return buf.getInt(); } @@ -208,7 +209,7 @@ public class BinaryUtils { 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); return buf.getFloat(); } @@ -216,7 +217,7 @@ public class BinaryUtils { 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); return buf.getDouble(); } diff --git a/src/mightypork/utils/files/ion/Ion.java b/src/mightypork/utils/files/ion/Ion.java index a65b0f6..3d0dbb6 100644 --- a/src/mightypork/utils/files/ion/Ion.java +++ b/src/mightypork/utils/files/ion/Ion.java @@ -117,7 +117,7 @@ public class Ion { final String f = path.toString(); 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); diff --git a/src/mightypork/utils/logging/ArchivingLog.java b/src/mightypork/utils/logging/ArchivingLog.java index c5d773a..cb2479a 100644 --- a/src/mightypork/utils/logging/ArchivingLog.java +++ b/src/mightypork/utils/logging/ArchivingLog.java @@ -79,7 +79,7 @@ public class ArchivingLog extends SimpleLog { 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 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."); + } } } diff --git a/src/mightypork/utils/logging/Log.java b/src/mightypork/utils/logging/Log.java index dfb416c..a609d42 100644 --- a/src/mightypork/utils/logging/Log.java +++ b/src/mightypork/utils/logging/Log.java @@ -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 thrown thrown exception @@ -212,7 +235,7 @@ public class Log { /** - * Log exception thrown + * Log exception thrown as error * * @param thrown thrown exception */ diff --git a/src/mightypork/utils/logging/SimpleLog.java b/src/mightypork/utils/logging/SimpleLog.java index eb74007..6f51d10 100644 --- a/src/mightypork/utils/logging/SimpleLog.java +++ b/src/mightypork/utils/logging/SimpleLog.java @@ -76,7 +76,7 @@ public class SimpleLog implements LogWriter { protected void printHeader() { 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); - } - } diff --git a/src/mightypork/utils/math/Polar.java b/src/mightypork/utils/math/Polar.java index efe8e14..fe33bd0 100644 --- a/src/mightypork/utils/math/Polar.java +++ b/src/mightypork/utils/math/Polar.java @@ -3,8 +3,6 @@ package mightypork.utils.math; import mightypork.utils.math.Calc.Deg; 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.VecView; @@ -22,7 +20,7 @@ public class Polar { /** distance in units */ private double radius = 0; - private ConstraintCoord coord = null; + private VecView coord = null; /** @@ -141,21 +139,21 @@ public class Polar { { // lazy init if (coord == null) { - coord = new ConstraintCoord(new NumberConstraint() { + coord = new VecView() { @Override - public double getValue() + public double x() { return radius * Math.cos(angle); } - }, new NumberConstraint() { + @Override - public double getValue() + public double y() { return radius * Math.sin(angle); } - }); + }; } return coord; diff --git a/src/mightypork/utils/math/constraints/Constraints.java b/src/mightypork/utils/math/constraints/Constraints.java index dbdaa75..e3013b7 100644 --- a/src/mightypork/utils/math/constraints/Constraints.java +++ b/src/mightypork/utils/math/constraints/Constraints.java @@ -2,12 +2,10 @@ package mightypork.utils.math.constraints; import mightypork.gamecore.control.timing.Poller; -import mightypork.utils.math.coord.FixedCoord; -import mightypork.utils.math.coord.SynthCoord3D; +import mightypork.utils.math.coord.AbstractVecProxy; import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.VecView; -import mightypork.utils.math.rect.FixedRect; -import mightypork.utils.math.rect.RectView; +import mightypork.utils.math.rect.RectValue; /** @@ -132,7 +130,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().round(); } @@ -255,7 +253,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { final double height = r.getRect().getHeight(); final double perRow = height / rows; @@ -263,7 +261,7 @@ public class Constraints { final Vec origin = r.getRect().getOrigin().add(0, perRow * index); 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() { @Override - public RectView getRect() + public RectValue getRect() { final double width = r.getRect().getWidth(); final double perCol = width / columns; @@ -282,7 +280,7 @@ public class Constraints { final Vec origin = r.getRect().getOrigin().add(perCol * index, 0); 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() { @Override - public RectView getRect() + public RectValue getRect() { final double height = 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)); - return new FixedRect(origin, perCol, perRow); + return RectValue.make(origin, perCol, perRow); } }; } @@ -328,7 +326,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(toDouble(left), toDouble(top), toDouble(right), toDouble(bottom)); } @@ -341,7 +339,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(0, toDouble(shrink), 0, 0); } @@ -354,7 +352,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(0, 0, 0, toDouble(shrink)); } @@ -367,7 +365,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(toDouble(shrink), 0, 0, 0); } @@ -380,7 +378,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(0, 0, toDouble(shrink), 0); } @@ -406,7 +404,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().grow(toDouble(left), toDouble(right), toDouble(top), toDouble(bottom)); } @@ -419,7 +417,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().grow(0, toDouble(grow), 0, 0); } @@ -432,7 +430,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().grow(0, 0, 0, toDouble(grow)); } @@ -445,7 +443,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().grow(toDouble(grow), 0, 0, 0); } @@ -458,7 +456,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().grow(0, 0, toDouble(grow), 0); } @@ -479,9 +477,9 @@ public class Constraints { return new RectConstraint() { @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() { @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() { @Override - public RectView getRect() + public RectValue getRect() { 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() { @Override - public RectView getRect() + public RectValue getRect() { 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() { @Override - public RectView getRect() + public RectValue getRect() { final VecView size = r.getRect().getSize(); 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() { @Override - public RectView getRect() + public RectValue getRect() { 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() { @Override - public RectView getRect() + public RectValue getRect() { 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() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().move(move.getVec()); } @@ -595,7 +593,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().move(toDouble(x), toDouble(y)); } @@ -645,7 +643,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { final double t = toDouble(top); final double r = toDouble(right); @@ -655,7 +653,7 @@ public class Constraints { final double x = c.getVec().x(); 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() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(0, 0, r.getRect().getWidth(), 0); } @@ -681,7 +679,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(0, 0, 0, r.getRect().getHeight()); } @@ -694,7 +692,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { return r.getRect().shrink(r.getRect().getWidth(), 0, 0, 0); } @@ -707,7 +705,7 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { 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) { - return new VecConstraintSynth() { + return new AbstractVecProxy() { @Override - public VecView getVec() + public VecView getSource() { 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) { - return new SynthCoord3D() { + return new AbstractVecProxy() { @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) { - return new VecConstraintSynth() { + return new AbstractVecProxy() { @Override - public VecView getVec() + public VecView getSource() { 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) { - return new VecConstraintSynth() { + return new AbstractVecProxy() { @Override - public VecView getVec() + public VecView getSource() { 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) { - return new VecConstraintSynth() { + return new AbstractVecProxy() { @Override - public VecView getVec() + public VecView getSource() { return c.getVec().mul(toDouble(mul)); } @@ -870,10 +853,10 @@ public class Constraints { public static VecConstraint cOrigin(final RectConstraint r) { - return new VecConstraintSynth() { + return new AbstractVecProxy() { @Override - public VecView getVec() + public VecView getSource() { return r.getRect().getOrigin(); } @@ -883,10 +866,10 @@ public class Constraints { public static VecConstraint cSize(final RectConstraint r) { - return new VecConstraintSynth() { + return new AbstractVecProxy() { @Override - public VecView getVec() + public VecView getSource() { return r.getRect().getSize(); } @@ -971,11 +954,11 @@ public class Constraints { return new RectConstraint() { @Override - public RectView getRect() + public RectValue getRect() { final Vec v = c.getVec(); - return new FixedRect(v.x(), v.y(), 0, 0); + return RectValue.make(v.x(), v.y(), 0, 0); } }; } diff --git a/src/mightypork/utils/math/constraints/ContextAdapter.java b/src/mightypork/utils/math/constraints/ContextAdapter.java index bb5250c..5c99acb 100644 --- a/src/mightypork/utils/math/constraints/ContextAdapter.java +++ b/src/mightypork/utils/math/constraints/ContextAdapter.java @@ -1,7 +1,7 @@ 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 */ -public abstract class ContextAdapter implements PluggableContext { +public abstract class ContextAdapter implements PluggableRect { private RectConstraint backing = null; @@ -22,7 +22,7 @@ public abstract class ContextAdapter implements PluggableContext { @Override - public RectView getRect() + public RectValue getRect() { return backing.getRect(); } diff --git a/src/mightypork/utils/math/constraints/FixedNumberConstraint.java b/src/mightypork/utils/math/constraints/FixedNumberConstraint.java new file mode 100644 index 0000000..339ac1c --- /dev/null +++ b/src/mightypork/utils/math/constraints/FixedNumberConstraint.java @@ -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; + } + +} diff --git a/src/mightypork/utils/math/constraints/NumberConstraint.java b/src/mightypork/utils/math/constraints/NumberConstraint.java index e127fe8..c39dc2b 100644 --- a/src/mightypork/utils/math/constraints/NumberConstraint.java +++ b/src/mightypork/utils/math/constraints/NumberConstraint.java @@ -8,23 +8,8 @@ package mightypork.utils.math.constraints; */ public interface NumberConstraint { - public static final NumberConstraint ZERO = new NumberConstraint() { - - @Override - public double getValue() - { - return 0; - } - }; - - public static final NumberConstraint ONE = new NumberConstraint() { - - @Override - public double getValue() - { - return 0; - } - }; + public static final NumberConstraint ZERO = new FixedNumberConstraint(0); + public static final NumberConstraint ONE = new FixedNumberConstraint(1); /** diff --git a/src/mightypork/utils/math/constraints/PluggableContext.java b/src/mightypork/utils/math/constraints/PluggableRect.java similarity index 65% rename from src/mightypork/utils/math/constraints/PluggableContext.java rename to src/mightypork/utils/math/constraints/PluggableRect.java index cb1ed35..b2cc6f5 100644 --- a/src/mightypork/utils/math/constraints/PluggableContext.java +++ b/src/mightypork/utils/math/constraints/PluggableRect.java @@ -1,7 +1,7 @@ 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 */ -public interface PluggableContext extends RectConstraint { +public interface PluggableRect extends RectConstraint { /** * @param rect context to set @@ -18,6 +18,6 @@ public interface PluggableContext extends RectConstraint { @Override - abstract RectView getRect(); + abstract RectValue getRect(); } diff --git a/src/mightypork/utils/math/constraints/RectCache.java b/src/mightypork/utils/math/constraints/RectCache.java index 2434d34..9a26b0c 100644 --- a/src/mightypork/utils/math/constraints/RectCache.java +++ b/src/mightypork/utils/math/constraints/RectCache.java @@ -3,13 +3,13 @@ package mightypork.utils.math.constraints; import mightypork.gamecore.control.timing.Pollable; import mightypork.gamecore.control.timing.Poller; -import mightypork.utils.math.rect.MutableRect; -import mightypork.utils.math.rect.RectView; +import mightypork.utils.math.rect.RectMutable; +import mightypork.utils.math.rect.RectValue; /** - * {@link RectConstraint} cache, used to reduce CPU load with very complex - * constraints.
+ * {@link RectConstraint} cache, used for caching computed Rect from a complex + * {@link RectConstraint}.
* Calculates only when polled. * * @author MightyPork @@ -17,7 +17,7 @@ import mightypork.utils.math.rect.RectView; public class RectCache implements RectConstraint, Pollable { 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 - public RectView getRect() + public RectValue getRect() { return cached.view(); } diff --git a/src/mightypork/utils/math/constraints/RectConstraint.java b/src/mightypork/utils/math/constraints/RectConstraint.java index d7be01f..7251239 100644 --- a/src/mightypork/utils/math/constraints/RectConstraint.java +++ b/src/mightypork/utils/math/constraints/RectConstraint.java @@ -1,7 +1,7 @@ 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 */ - RectView getRect(); + RectValue getRect(); } diff --git a/src/mightypork/utils/math/constraints/VecConstraintSynth.java b/src/mightypork/utils/math/constraints/VecConstraintSynth.java deleted file mode 100644 index a26fc09..0000000 --- a/src/mightypork/utils/math/constraints/VecConstraintSynth.java +++ /dev/null @@ -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()); - } - -} diff --git a/src/mightypork/utils/math/coord/AbstractVec.java b/src/mightypork/utils/math/coord/AbstractVec.java new file mode 100644 index 0000000..f1f2915 --- /dev/null +++ b/src/mightypork/utils/math/coord/AbstractVec.java @@ -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; + } + +} diff --git a/src/mightypork/utils/math/coord/AbstractVecProxy.java b/src/mightypork/utils/math/coord/AbstractVecProxy.java new file mode 100644 index 0000000..eecd5e6 --- /dev/null +++ b/src/mightypork/utils/math/coord/AbstractVecProxy.java @@ -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; + } + +} diff --git a/src/mightypork/utils/math/coord/AnimCoord.java b/src/mightypork/utils/math/coord/AnimCoord.java deleted file mode 100644 index 9e9fee2..0000000 --- a/src/mightypork/utils/math/coord/AnimCoord.java +++ /dev/null @@ -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 - } -} diff --git a/src/mightypork/utils/math/coord/FixedCoord.java b/src/mightypork/utils/math/coord/ConstVec.java similarity index 57% rename from src/mightypork/utils/math/coord/FixedCoord.java rename to src/mightypork/utils/math/coord/ConstVec.java index 3111f76..8ff7ad4 100644 --- a/src/mightypork/utils/math/coord/FixedCoord.java +++ b/src/mightypork/utils/math/coord/ConstVec.java @@ -2,27 +2,26 @@ package mightypork.utils.math.coord; /** - * Coordinate with immutable numeric values.
- * Operations yield a new {@link MutableCoord} with the result. + * Coordinate with immutable numeric values. * * @author MightyPork */ -public class FixedCoord extends VecView { +class ConstVec extends VecView { private final double x, y, z; - public FixedCoord(Vec other) { + public ConstVec(Vec other) { this(other.x(), other.y(), other.z()); } - public FixedCoord(double x, double y) { + public ConstVec(double x, double y) { this(x, y, 0); } - public FixedCoord(double x, double y, double z) { + public ConstVec(double x, double y, double z) { this.x = x; this.y = y; this.z = z; @@ -49,4 +48,11 @@ public class FixedCoord extends VecView { return z; } + + @Override + public VecView value() + { + return this; // it's constant already + } + } diff --git a/src/mightypork/utils/math/coord/CoordProxy.java b/src/mightypork/utils/math/coord/CoordProxy.java deleted file mode 100644 index 45939fb..0000000 --- a/src/mightypork/utils/math/coord/CoordProxy.java +++ /dev/null @@ -1,58 +0,0 @@ -package mightypork.utils.math.coord; - - -/** - *

- * [ Use Vec.view() method to make a proxy! ] - *

- *

- * View of another coordinate, immutable.
- * Operations yield a new {@link MutableCoord} with the result. - *

- * - * @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(); - } - -} diff --git a/src/mightypork/utils/math/coord/MutableCoord.java b/src/mightypork/utils/math/coord/MutableCoord.java deleted file mode 100644 index e376c18..0000000 --- a/src/mightypork/utils/math/coord/MutableCoord.java +++ /dev/null @@ -1,86 +0,0 @@ -package mightypork.utils.math.coord; - - -/** - * Mutable coordinate.
- * 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; - } -} diff --git a/src/mightypork/utils/math/coord/ConstraintCoord.java b/src/mightypork/utils/math/coord/NumConstrVec.java similarity index 71% rename from src/mightypork/utils/math/coord/ConstraintCoord.java rename to src/mightypork/utils/math/coord/NumConstrVec.java index 7624be0..6af8773 100644 --- a/src/mightypork/utils/math/coord/ConstraintCoord.java +++ b/src/mightypork/utils/math/coord/NumConstrVec.java @@ -6,26 +6,25 @@ import mightypork.utils.math.constraints.NumberConstraint; /** * Coord view composed of given {@link NumberConstraint}s, using their current - * values.
- * Operations yield a new {@link MutableCoord} with the result. + * values. * * @author MightyPork */ -public class ConstraintCoord extends VecView { +class NumConstrVec extends VecView { private final NumberConstraint constrX; private final NumberConstraint constrY; private final NumberConstraint constrZ; - public ConstraintCoord(NumberConstraint x, NumberConstraint y, NumberConstraint z) { + public NumConstrVec(NumberConstraint x, NumberConstraint y, NumberConstraint z) { this.constrX = x; this.constrY = y; this.constrZ = z; } - public ConstraintCoord(NumberConstraint x, NumberConstraint y) { + public NumConstrVec(NumberConstraint x, NumberConstraint y) { this.constrX = x; this.constrY = y; this.constrZ = NumberConstraint.ZERO; diff --git a/src/mightypork/utils/math/coord/SynthCoord2D.java b/src/mightypork/utils/math/coord/SynthCoord2D.java deleted file mode 100644 index d6d9ef0..0000000 --- a/src/mightypork/utils/math/coord/SynthCoord2D.java +++ /dev/null @@ -1,25 +0,0 @@ -package mightypork.utils.math.coord; - - -/** - * 2D coord for anonymous implementations.
- * 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; - } -} diff --git a/src/mightypork/utils/math/coord/SynthCoord3D.java b/src/mightypork/utils/math/coord/SynthCoord3D.java deleted file mode 100644 index f29b72f..0000000 --- a/src/mightypork/utils/math/coord/SynthCoord3D.java +++ /dev/null @@ -1,23 +0,0 @@ -package mightypork.utils.math.coord; - - -/** - * 3D immutable coord for anonymous implementations.
- * 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(); - -} diff --git a/src/mightypork/utils/math/coord/Synths.java b/src/mightypork/utils/math/coord/Synths.java new file mode 100644 index 0000000..a1bd3ad --- /dev/null +++ b/src/mightypork/utils/math/coord/Synths.java @@ -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; + } + } +} diff --git a/src/mightypork/utils/math/coord/Vec.java b/src/mightypork/utils/math/coord/Vec.java index c0aedb0..748e2cd 100644 --- a/src/mightypork/utils/math/coord/Vec.java +++ b/src/mightypork/utils/math/coord/Vec.java @@ -12,8 +12,8 @@ import mightypork.utils.math.constraints.VecConstraint; */ public interface Vec extends VecConstraint { - public static final VecView ZERO = new FixedCoord(0, 0, 0); - public static final VecView ONE = new FixedCoord(1, 1, 1); + public static final VecView ZERO = new ConstVec(0, 0, 0); + 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 - NumberConstraint xc(); + double size(); /** - * @return Y constraint + * @return true if zero */ - @Override - NumberConstraint yc(); + public boolean isZero(); /** - * @return Z constraint + * Get distance to other point + * + * @param point other point + * @return distance */ - @Override - NumberConstraint zc(); + double distTo(Vec point); + + + /** + * 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 */ VecView view(); + + /** + * Get a mutable copy of current values. + * + * @return mutable copy + */ + VecMutable mutable(); } diff --git a/src/mightypork/utils/math/coord/VecMath.java b/src/mightypork/utils/math/coord/VecMath.java index 478fddc..da17bb7 100644 --- a/src/mightypork/utils/math/coord/VecMath.java +++ b/src/mightypork/utils/math/coord/VecMath.java @@ -2,11 +2,29 @@ package mightypork.utils.math.coord; /** - * 3D coordinate methods + * Implementation of coordinate methods * * @author MightyPork + * @param Return type of methods */ -interface VecMath extends Vec { +abstract class VecMath extends AbstractVec { + + /** + *

+ * Some operation was performed and this result was obtained. + *

+ *

+ * It's now up to implementing class what to do - mutable ones can alter + * it's data values, immutable can return a new Vec. + *

+ * + * @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). @@ -14,7 +32,10 @@ interface VecMath extends Vec { * @param x x coordinate * @return result */ - V setX(double x); + public V setX(double x) + { + return result(x, y(), z()); + } /** @@ -23,7 +44,10 @@ interface VecMath extends Vec { * @param y y coordinate * @return result */ - V setY(double y); + public V setY(double y) + { + return result(x(), y, z()); + } /** @@ -32,33 +56,10 @@ interface VecMath extends Vec { * @param z z coordinate * @return result */ - V setZ(double 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(); + public V setZ(double z) + { + return result(x(), y(), z); + } /** @@ -66,31 +67,10 @@ interface VecMath extends Vec { * * @return result */ - V abs(); - - - /** - * @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); + public V abs() + { + return result(Math.abs(x()), Math.abs(y()), Math.abs(z())); + } /** @@ -99,7 +79,10 @@ interface VecMath extends Vec { * @param vec offset * @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 extends Vec { * @param y y offset * @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 extends Vec { * @param z z offset * @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 extends Vec { * * @return result */ - V half(); + public V half() + { + return mul(0.5); + } /** @@ -138,7 +130,10 @@ interface VecMath extends Vec { * @param d multiplier * @return result */ - V mul(double d); + public V mul(double d) + { + return mul(d, d, d); + } /** @@ -147,7 +142,10 @@ interface VecMath extends Vec { * @param vec vector of multipliers * @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 extends Vec { * @param y y multiplier * @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 extends Vec { * @param z z multiplier * @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 extends Vec { * * @return result */ - V round(); + public V round() + { + return result(Math.round(x()), Math.round(y()), Math.round(z())); + } /** @@ -185,7 +192,10 @@ interface VecMath extends Vec { * * @return result */ - V floor(); + public V floor() + { + return result(Math.floor(x()), Math.floor(y()), Math.floor(z())); + } /** @@ -193,7 +203,10 @@ interface VecMath extends Vec { * * @return result */ - V ceil(); + public V ceil() + { + return result(Math.ceil(x()), Math.ceil(y()), Math.ceil(z())); + } /** @@ -202,7 +215,10 @@ interface VecMath extends Vec { * @param vec offset * @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 extends Vec { * @param y y offset * @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 extends Vec { * @param z z offset * @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 extends Vec { * * @return result */ - V neg(); + public V neg() + { + return result(-x(), -y(), -z()); + } /** @@ -241,15 +266,43 @@ interface VecMath extends Vec { * @param size size we need * @return result */ - V norm(double size); - - - /** - * Get cross product (vector multiplication) - * - * @param vec other vector - * @return result - */ - V cross(Vec vec); - + 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()); + } } diff --git a/src/mightypork/utils/math/coord/VecMathImpl.java b/src/mightypork/utils/math/coord/VecMathImpl.java deleted file mode 100644 index a9a68e2..0000000 --- a/src/mightypork/utils/math/coord/VecMathImpl.java +++ /dev/null @@ -1,398 +0,0 @@ -package mightypork.utils.math.coord; - - -import mightypork.utils.math.constraints.NumberConstraint; - - -/** - * Implementation of coordinate methods - * - * @author MightyPork - * @param Return type of methods - */ -abstract class VecMathImpl implements VecMath { - - 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(); - } - - - /** - *

- * Some operation was performed and this result was obtained. - *

- *

- * It's now up to implementing class what to do - mutable ones can alter - * it's data values, immutable can return a new Vec. - *

- * - * @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()); - } -} diff --git a/src/mightypork/utils/math/coord/VecMutable.java b/src/mightypork/utils/math/coord/VecMutable.java index 47b0fe5..b21e8c7 100644 --- a/src/mightypork/utils/math/coord/VecMutable.java +++ b/src/mightypork/utils/math/coord/VecMutable.java @@ -1,12 +1,139 @@ package mightypork.utils.math.coord; +import mightypork.utils.math.animation.AnimDouble; +import mightypork.utils.math.animation.Easing; + + /** * Mutable coord * * @author MightyPork */ -public interface VecMutable extends VecMath { +public abstract class VecMutable extends VecMath { + + /** + * 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. @@ -14,7 +141,10 @@ public interface VecMutable extends VecMath { * @param copied coord whose coordinates are used * @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 { * @param y y coordinate * @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 { * @param z z coordinate * @return result */ - VecMutable setTo(double x, double y, double z); + public VecMutable setTo(double x, double y, double z) + { + return result(x, y, z); + } } diff --git a/src/mightypork/utils/math/coord/VecMutableAnim.java b/src/mightypork/utils/math/coord/VecMutableAnim.java new file mode 100644 index 0000000..7c02d5c --- /dev/null +++ b/src/mightypork/utils/math/coord/VecMutableAnim.java @@ -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); + } + +} diff --git a/src/mightypork/utils/math/coord/VecMutableImpl.java b/src/mightypork/utils/math/coord/VecMutableImpl.java index 48fb6c9..d24b176 100644 --- a/src/mightypork/utils/math/coord/VecMutableImpl.java +++ b/src/mightypork/utils/math/coord/VecMutableImpl.java @@ -2,45 +2,57 @@ package mightypork.utils.math.coord; /** - * Mutable vec default implementation + * Mutable coordinate.
+ * All Vec methods (except copy) alter data values and return this instance. * * @author MightyPork */ -abstract class VecMutableImpl extends VecMathImpl implements VecMutable { +class VecMutableImpl extends VecMutable { - @Override - public abstract double x(); - - - @Override - public abstract double y(); + private double x, y, z; - @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 - public abstract VecMutable result(double x, double y, double z); + public double x() + { + return x; + } @Override - public VecMutable setTo(Vec copied) + public double y() { - return result(copied.x(), copied.y(), copied.z()); + return y; } @Override - public VecMutable setTo(double x, double y, double z) + public double z() { - return result(x, y, z); + return z; } @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; } } diff --git a/src/mightypork/utils/math/coord/VecProxy.java b/src/mightypork/utils/math/coord/VecProxy.java new file mode 100644 index 0000000..f0c2642 --- /dev/null +++ b/src/mightypork/utils/math/coord/VecProxy.java @@ -0,0 +1,32 @@ +package mightypork.utils.math.coord; + + +/** + * View of another coordinate, immutable.
+ * 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; + } + +} diff --git a/src/mightypork/utils/math/coord/VecView.java b/src/mightypork/utils/math/coord/VecView.java index 7aa7419..46c6202 100644 --- a/src/mightypork/utils/math/coord/VecView.java +++ b/src/mightypork/utils/math/coord/VecView.java @@ -1,24 +1,177 @@ 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 - * {@link MutableCoord} with the result. + * Read-only coordinate. * * @author MightyPork */ -public abstract class VecView extends VecMathImpl { +public abstract class VecView extends VecMath { + + /** + * 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 public VecView result(double x, double y, double z) { - return new FixedCoord(x, y, z); + return new ConstVec(x, y, z); } @Override + @Deprecated public VecView view() { 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; + } } diff --git a/src/mightypork/utils/math/rect/RectImpl.java b/src/mightypork/utils/math/rect/AbstractRect.java similarity index 67% rename from src/mightypork/utils/math/rect/RectImpl.java rename to src/mightypork/utils/math/rect/AbstractRect.java index bac64fd..b507bf0 100644 --- a/src/mightypork/utils/math/rect/RectImpl.java +++ b/src/mightypork/utils/math/rect/AbstractRect.java @@ -7,53 +7,62 @@ import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.VecView; -public abstract class RectImpl implements RectMath { +/** + * 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 - public RectView getRect() + public final RectValue getRect() { return this.view(); } @Override - public abstract VecView getOrigin(); - - - @Override - public abstract VecView getSize(); - - - @Override - public VecView getTopLeft() + public final VecView getTopLeft() { + // lazy init if (tl == null) tl = cTopLeft(this); return tl.getVec(); } @Override - public VecView getTopCenter() + public final VecView getTopCenter() { + // lazy init if (tc == null) tc = cTopCenter(this); return tc.getVec(); } @Override - public VecView getTopRight() + public final VecView getTopRight() { + // lazy init if (tr == null) tr = cTopRight(this); return tr.getVec(); } @Override - public VecView getCenterLeft() + public final VecView getCenterLeft() { + // lazy init if (cl == null) cl = cCenterLeft(this); return cl.getVec(); } @@ -62,38 +71,43 @@ public abstract class RectImpl implements RectMath { @Override public final VecView getCenter() { + // lazy init if (c == null) c = cCenter(this); return c.getVec(); } @Override - public VecView getCenterRight() + public final VecView getCenterRight() { + // lazy init if (cr == null) cr = cCenterRight(this); return cr.getVec(); } @Override - public VecView getBottomLeft() + public final VecView getBottomLeft() { + // lazy init if (bl == null) bl = cBottomLeft(this); return bl.getVec(); } @Override - public VecView getBottomCenter() + public final VecView getBottomCenter() { + // lazy init if (bc == null) bc = cBottomCenter(this); return bc.getVec(); } @Override - public VecView getBottomRight() + public final VecView getBottomRight() { + // lazy init if (br == null) br = cBottomRight(this); return br.getVec(); } @@ -142,51 +156,23 @@ public abstract class RectImpl implements RectMath { @Override - public final T move(Vec move) + public RectValue view() { - return move(move.x(), move.y()); - } - - - @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); + return new RectProxy(this); } @Override - public RectView view() + public final RectMutable mutable() { - return new RectProxy(this); + return RectMutable.make(this); } @Override - public RectMutable copy() + public RectValue value() { - return new MutableRect(this); + return RectValue.make(getOrigin(), getSize()); } @@ -210,4 +196,5 @@ public abstract class RectImpl implements RectMath { { return String.format("Rect { %s - %s }", getOrigin().toString(), getOrigin().add(getSize())); } + } diff --git a/src/mightypork/utils/math/rect/ConstRect.java b/src/mightypork/utils/math/rect/ConstRect.java new file mode 100644 index 0000000..056c9f0 --- /dev/null +++ b/src/mightypork/utils/math/rect/ConstRect.java @@ -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; + } + +} diff --git a/src/mightypork/utils/math/rect/FixedRect.java b/src/mightypork/utils/math/rect/FixedRect.java deleted file mode 100644 index 9313a9b..0000000 --- a/src/mightypork/utils/math/rect/FixedRect.java +++ /dev/null @@ -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; - } - -} diff --git a/src/mightypork/utils/math/rect/MutableRect.java b/src/mightypork/utils/math/rect/MutableRect.java deleted file mode 100644 index b8928b1..0000000 --- a/src/mightypork/utils/math/rect/MutableRect.java +++ /dev/null @@ -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 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); - } -} diff --git a/src/mightypork/utils/math/rect/Rect.java b/src/mightypork/utils/math/rect/Rect.java index d6c6dfe..5a7d395 100644 --- a/src/mightypork/utils/math/rect/Rect.java +++ b/src/mightypork/utils/math/rect/Rect.java @@ -2,6 +2,7 @@ package mightypork.utils.math.rect; import mightypork.utils.math.constraints.RectConstraint; +import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.VecView; @@ -12,8 +13,8 @@ import mightypork.utils.math.coord.VecView; */ public interface Rect extends RectConstraint { - RectView ONE = new FixedRect(0, 0, 1, 1); - RectView ZERO = new FixedRect(0, 0, 0, 0); + RectValue ONE = new ConstRect(0, 0, 1, 1); + RectValue ZERO = new ConstRect(0, 0, 0, 0); /** @@ -21,15 +22,23 @@ public interface Rect extends RectConstraint { * * @return copy */ - RectMutable copy(); + RectMutable mutable(); /** - * Get a readonly copy + * Get a copy of current value * * @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(); + + /** + * Check if point is inside this rectangle + * + * @param point point to test + * @return is inside + */ + boolean contains(Vec point); + } diff --git a/src/mightypork/utils/math/rect/RectMath.java b/src/mightypork/utils/math/rect/RectMath.java index 1004879..8e41119 100644 --- a/src/mightypork/utils/math/rect/RectMath.java +++ b/src/mightypork/utils/math/rect/RectMath.java @@ -4,12 +4,7 @@ package mightypork.utils.math.rect; import mightypork.utils.math.coord.Vec; -/** - * Operations available in rects - * - * @author MightyPork - */ -interface RectMath extends Rect { +abstract class RectMath extends AbstractRect { /** * Add vector to origin @@ -17,7 +12,10 @@ interface RectMath extends Rect { * @param move offset vector * @return result */ - T move(Vec move); + public T move(Vec move) + { + return move(move.x(), move.y()); + } /** @@ -27,7 +25,7 @@ interface RectMath extends Rect { * @param y y to add * @return result */ - T move(double x, double y); + public abstract T move(double x, double y); /** @@ -36,7 +34,11 @@ interface RectMath extends Rect { * @param shrink shrink size (horisontal and vertical) * @return result */ - T shrink(Vec shrink); + + public T shrink(Vec shrink) + { + return shrink(shrink.x(), shrink.y()); + } /** @@ -46,7 +48,10 @@ interface RectMath extends Rect { * @param y vertical shrink * @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 extends Rect { * @param bottom shrink * @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 extends Rect { * @param grow grow size (added to each side) * @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 extends Rect { * @param y vertical grow * @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 extends Rect { * @param bottom growth * @return result */ - 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); + public abstract T grow(double left, double right, double top, double bottom); /** @@ -106,6 +108,5 @@ interface RectMath extends Rect { * * @return result */ - T round(); - + public abstract T round(); } diff --git a/src/mightypork/utils/math/rect/RectMutable.java b/src/mightypork/utils/math/rect/RectMutable.java index 7a3ddd5..4e4fbc8 100644 --- a/src/mightypork/utils/math/rect/RectMutable.java +++ b/src/mightypork/utils/math/rect/RectMutable.java @@ -2,27 +2,173 @@ package mightypork.utils.math.rect; import mightypork.utils.math.coord.Vec; +import mightypork.utils.math.coord.VecView; -public interface RectMutable extends RectMath { +/** + * Mutable rectangle; operations change it's state. + * + * @author MightyPork + */ +public abstract class RectMutable extends RectMath { + + /** + * 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 * * @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); } diff --git a/src/mightypork/utils/math/rect/RectMutableImpl.java b/src/mightypork/utils/math/rect/RectMutableImpl.java new file mode 100644 index 0000000..1ecce8d --- /dev/null +++ b/src/mightypork/utils/math/rect/RectMutableImpl.java @@ -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; + } +} diff --git a/src/mightypork/utils/math/rect/RectProxy.java b/src/mightypork/utils/math/rect/RectProxy.java index 2a78789..35c8406 100644 --- a/src/mightypork/utils/math/rect/RectProxy.java +++ b/src/mightypork/utils/math/rect/RectProxy.java @@ -9,7 +9,7 @@ import mightypork.utils.math.coord.VecView; * * @author MightyPork */ -public class RectProxy extends RectView { +public class RectProxy extends RectValue { private final Rect observed; diff --git a/src/mightypork/utils/math/rect/RectValue.java b/src/mightypork/utils/math/rect/RectValue.java new file mode 100644 index 0000000..286ea1b --- /dev/null +++ b/src/mightypork/utils/math/rect/RectValue.java @@ -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 { + + /** + * 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; + } + +} diff --git a/src/mightypork/utils/math/rect/RectView.java b/src/mightypork/utils/math/rect/RectView.java deleted file mode 100644 index a9df140..0000000 --- a/src/mightypork/utils/math/rect/RectView.java +++ /dev/null @@ -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 { - - 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(); - -} diff --git a/src/mightypork/utils/objects/Convert.java b/src/mightypork/utils/objects/Convert.java index 09338c9..eecb7d9 100644 --- a/src/mightypork/utils/objects/Convert.java +++ b/src/mightypork/utils/objects/Convert.java @@ -3,7 +3,6 @@ package mightypork.utils.objects; import mightypork.utils.logging.Log; import mightypork.utils.math.Range; -import mightypork.utils.math.coord.FixedCoord; import mightypork.utils.math.coord.Vec; import mightypork.utils.math.coord.VecView; @@ -155,8 +154,8 @@ public class Convert { public static VecView toCoord(Object o, Vec def) { try { - if (o == null) return new FixedCoord(def); - if (o instanceof Vec) return new FixedCoord((Vec) o); + if (o == null) return def.value(); + if (o instanceof Vec) return ((Vec) o).value(); if (o instanceof String) { String s = ((String) o).trim().toUpperCase(); @@ -172,19 +171,19 @@ public class Convert { final double y = Double.parseDouble(parts[1].trim()); if (parts.length == 2) { - return new FixedCoord(x, y); + return VecView.make(x, y); } 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) { // ignore } - return new FixedCoord(def); + return def.value(); } @@ -292,7 +291,7 @@ public class Convert { */ public static VecView toCoord(Object o) { - return toCoord(o, Vec.ZERO); + return toCoord(o, VecView.zero()); } diff --git a/src/mightypork/utils/objects/Mutable.java b/src/mightypork/utils/objects/Mutable.java index 961bb7e..f4bde57 100644 --- a/src/mightypork/utils/objects/Mutable.java +++ b/src/mightypork/utils/objects/Mutable.java @@ -45,6 +45,14 @@ public class Mutable { } + @Override + public String toString() + { + if (o == null) return ""; + return o.toString(); + } + + @Override public int hashCode() { @@ -61,21 +69,10 @@ public class Mutable { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Mutable)) return false; - final Mutable other = (Mutable) obj; if (o == null) { if (other.o != null) return false; - } else if (!o.equals(other.o)) { - return false; - } + } else if (!o.equals(other.o)) return false; return true; } - - - @Override - public String toString() - { - if (o == null) return ""; - return o.toString(); - } }