diff --git a/src/mightypork/gamecore/audio/SoundSystem.java b/src/mightypork/gamecore/audio/SoundSystem.java index c9044ba..f6bbcf5 100644 --- a/src/mightypork/gamecore/audio/SoundSystem.java +++ b/src/mightypork/gamecore/audio/SoundSystem.java @@ -31,13 +31,7 @@ public class SoundSystem extends RootBusNode implements Updateable { private static Coord listener = new Coord(); - static { - // initialize sound system - SoundStore.get().setMaxSources(MAX_SOURCES); - SoundStore.get().init(); - - setListener(INITIAL_LISTENER_POS); - } + private static boolean inited; /** @@ -86,6 +80,15 @@ public class SoundSystem extends RootBusNode implements Updateable { */ public SoundSystem(AppAccess app) { super(app); + + if (!inited) { + SoundStore.get().setMaxSources(MAX_SOURCES); + SoundStore.get().init(); + + setListener(INITIAL_LISTENER_POS); + + inited = true; + } } diff --git a/src/mightypork/gamecore/control/GameLoop.java b/src/mightypork/gamecore/control/GameLoop.java index 7c467ea..92d6acd 100644 --- a/src/mightypork/gamecore/control/GameLoop.java +++ b/src/mightypork/gamecore/control/GameLoop.java @@ -8,7 +8,7 @@ import mightypork.gamecore.control.bus.events.MainLoopTaskRequest; import mightypork.gamecore.control.bus.events.UpdateEvent; import mightypork.gamecore.control.interf.NoImpl; import mightypork.gamecore.control.timing.TimerDelta; -import mightypork.gamecore.gui.renderers.Renderable; +import mightypork.gamecore.gui.components.Renderable; import mightypork.gamecore.gui.screens.ScreenRegistry; diff --git a/src/mightypork/gamecore/control/bus/events/MouseButtonEvent.java b/src/mightypork/gamecore/control/bus/events/MouseButtonEvent.java index c46009d..00c7579 100644 --- a/src/mightypork/gamecore/control/bus/events/MouseButtonEvent.java +++ b/src/mightypork/gamecore/control/bus/events/MouseButtonEvent.java @@ -1,7 +1,7 @@ package mightypork.gamecore.control.bus.events; -import mightypork.utils.math.constraints.RectConstraint; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.utils.math.coord.Coord; diff --git a/src/mightypork/gamecore/control/interf/Pollable.java b/src/mightypork/gamecore/control/interf/Pollable.java new file mode 100644 index 0000000..400492c --- /dev/null +++ b/src/mightypork/gamecore/control/interf/Pollable.java @@ -0,0 +1,15 @@ +package mightypork.gamecore.control.interf; + + +/** + * Can be asked to update it's state + * + * @author MightyPork + */ +public interface Pollable { + + /** + * Update internal state + */ + void poll(); +} diff --git a/src/mightypork/gamecore/gui/renderers/PluggableRenderable.java b/src/mightypork/gamecore/gui/components/PluggableRenderable.java similarity index 66% rename from src/mightypork/gamecore/gui/renderers/PluggableRenderable.java rename to src/mightypork/gamecore/gui/components/PluggableRenderable.java index 6306fe0..12b42d5 100644 --- a/src/mightypork/gamecore/gui/renderers/PluggableRenderable.java +++ b/src/mightypork/gamecore/gui/components/PluggableRenderable.java @@ -1,8 +1,8 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components; -import mightypork.utils.math.constraints.PluggableContext; -import mightypork.utils.math.constraints.RectConstraint; +import mightypork.gamecore.gui.constraints.PluggableContext; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/gamecore/gui/renderers/PluggableRenderer.java b/src/mightypork/gamecore/gui/components/PluggableRenderer.java similarity index 72% rename from src/mightypork/gamecore/gui/renderers/PluggableRenderer.java rename to src/mightypork/gamecore/gui/components/PluggableRenderer.java index 124c942..93ccf36 100644 --- a/src/mightypork/gamecore/gui/renderers/PluggableRenderer.java +++ b/src/mightypork/gamecore/gui/components/PluggableRenderer.java @@ -1,8 +1,8 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components; -import mightypork.utils.math.constraints.ContextAdapter; -import mightypork.utils.math.constraints.RectConstraint; +import mightypork.gamecore.gui.constraints.ContextAdapter; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/gamecore/gui/renderers/Renderable.java b/src/mightypork/gamecore/gui/components/Renderable.java similarity index 75% rename from src/mightypork/gamecore/gui/renderers/Renderable.java rename to src/mightypork/gamecore/gui/components/Renderable.java index 245f1a0..ffe6cbb 100644 --- a/src/mightypork/gamecore/gui/renderers/Renderable.java +++ b/src/mightypork/gamecore/gui/components/Renderable.java @@ -1,4 +1,4 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components; /** diff --git a/src/mightypork/gamecore/gui/renderers/ColumnHolder.java b/src/mightypork/gamecore/gui/components/layout/ColumnHolder.java similarity index 75% rename from src/mightypork/gamecore/gui/renderers/ColumnHolder.java rename to src/mightypork/gamecore/gui/components/layout/ColumnHolder.java index 30275dd..7933761 100644 --- a/src/mightypork/gamecore/gui/renderers/ColumnHolder.java +++ b/src/mightypork/gamecore/gui/components/layout/ColumnHolder.java @@ -1,9 +1,10 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components.layout; -import static mightypork.utils.math.constraints.ConstraintFactory.*; +import static mightypork.gamecore.gui.constraints.Constraints.*; import mightypork.gamecore.control.AppAccess; -import mightypork.utils.math.constraints.RectConstraint; +import mightypork.gamecore.gui.components.PluggableRenderable; +import mightypork.gamecore.gui.constraints.RectConstraint; /** @@ -51,7 +52,7 @@ public class ColumnHolder extends ElementHolder { { if (elem == null) return; - elem.setContext(c_column(this, cols, col++)); + elem.setContext(_column(this, cols, col++)); attach(elem); } diff --git a/src/mightypork/gamecore/gui/renderers/ElementHolder.java b/src/mightypork/gamecore/gui/components/layout/ElementHolder.java similarity index 84% rename from src/mightypork/gamecore/gui/renderers/ElementHolder.java rename to src/mightypork/gamecore/gui/components/layout/ElementHolder.java index 7c67f34..9c73540 100644 --- a/src/mightypork/gamecore/gui/renderers/ElementHolder.java +++ b/src/mightypork/gamecore/gui/components/layout/ElementHolder.java @@ -1,4 +1,4 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components.layout; import java.util.LinkedList; @@ -6,7 +6,10 @@ import java.util.LinkedList; import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.bus.EventBus; import mightypork.gamecore.control.bus.clients.BusNode; -import mightypork.utils.math.constraints.RectConstraint; +import mightypork.gamecore.gui.components.PluggableRenderable; +import mightypork.gamecore.gui.components.PluggableRenderer; +import mightypork.gamecore.gui.components.Renderable; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/gamecore/gui/renderers/RowHolder.java b/src/mightypork/gamecore/gui/components/layout/RowHolder.java similarity index 75% rename from src/mightypork/gamecore/gui/renderers/RowHolder.java rename to src/mightypork/gamecore/gui/components/layout/RowHolder.java index 58e2b4b..3041153 100644 --- a/src/mightypork/gamecore/gui/renderers/RowHolder.java +++ b/src/mightypork/gamecore/gui/components/layout/RowHolder.java @@ -1,9 +1,10 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components.layout; -import static mightypork.utils.math.constraints.ConstraintFactory.*; +import static mightypork.gamecore.gui.constraints.Constraints.*; import mightypork.gamecore.control.AppAccess; -import mightypork.utils.math.constraints.RectConstraint; +import mightypork.gamecore.gui.components.PluggableRenderable; +import mightypork.gamecore.gui.constraints.RectConstraint; /** @@ -51,7 +52,7 @@ public class RowHolder extends ElementHolder { { if (elem == null) return; - elem.setContext(c_row(this, rows, row++)); + elem.setContext(_row(this, rows, row++)); attach(elem); } diff --git a/src/mightypork/gamecore/gui/renderers/ImagePainter.java b/src/mightypork/gamecore/gui/components/painters/ImagePainter.java similarity index 83% rename from src/mightypork/gamecore/gui/renderers/ImagePainter.java rename to src/mightypork/gamecore/gui/components/painters/ImagePainter.java index 9972dee..5069388 100644 --- a/src/mightypork/gamecore/gui/renderers/ImagePainter.java +++ b/src/mightypork/gamecore/gui/components/painters/ImagePainter.java @@ -1,6 +1,7 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components.painters; +import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.render.Render; import mightypork.gamecore.render.textures.TxQuad; diff --git a/src/mightypork/gamecore/gui/renderers/TextPainter.java b/src/mightypork/gamecore/gui/components/painters/TextPainter.java similarity index 96% rename from src/mightypork/gamecore/gui/renderers/TextPainter.java rename to src/mightypork/gamecore/gui/components/painters/TextPainter.java index d802423..5bf39d3 100644 --- a/src/mightypork/gamecore/gui/renderers/TextPainter.java +++ b/src/mightypork/gamecore/gui/components/painters/TextPainter.java @@ -1,6 +1,7 @@ -package mightypork.gamecore.gui.renderers; +package mightypork.gamecore.gui.components.painters; +import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.render.fonts.FontRenderer; import mightypork.gamecore.render.fonts.FontRenderer.Align; import mightypork.gamecore.render.fonts.GLFont; diff --git a/src/mightypork/gamecore/gui/constraints/Constraints.java b/src/mightypork/gamecore/gui/constraints/Constraints.java new file mode 100644 index 0000000..d852964 --- /dev/null +++ b/src/mightypork/gamecore/gui/constraints/Constraints.java @@ -0,0 +1,831 @@ +package mightypork.gamecore.gui.constraints; + + +import mightypork.gamecore.input.InputSystem; +import mightypork.gamecore.render.DisplaySystem; +import mightypork.utils.math.coord.Coord; +import mightypork.utils.math.coord.Rect; + + +/** + * Constraint factory.
+ * Import statically for best experience. + * + * @author MightyPork + */ +public class Constraints { + + /* ================= Variables ================= */ + + public static final NumberConstraint _mouseX = new NumberConstraint() { + + @Override + public double getValue() + { + return InputSystem.getMousePos().x; + } + }; + + public static final NumberConstraint _mouseY = new NumberConstraint() { + + @Override + public double getValue() + { + return InputSystem.getMousePos().y; + } + }; + + public static final NumberConstraint _screenW = new NumberConstraint() { + + @Override + public double getValue() + { + return DisplaySystem.getWidth(); + } + }; + + public static final NumberConstraint _screenH = new NumberConstraint() { + + @Override + public double getValue() + { + return DisplaySystem.getHeight(); + } + }; + + + /* ================= Arithmetics ================= */ + + public static NumberConstraint _min(final Object a, final Object b) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return Math.min(_nv(a), _nv(b)); + } + }; + } + + + public static NumberConstraint _max(final Object a, final Object b) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return Math.max(_nv(a), _nv(b)); + } + }; + } + + + public static NumberConstraint _abs(final NumberConstraint a) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return Math.abs(a.getValue()); + } + }; + } + + + public static NumberConstraint _half(final NumberConstraint a) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return a.getValue() / 2; + } + }; + } + + + public static NumberConstraint _round(final NumberConstraint a) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return Math.round(a.getValue()); + } + }; + } + + + public static RectConstraint _round(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().round(); + } + }; + } + + + public static NumberConstraint _ceil(final NumberConstraint a) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return Math.ceil(a.getValue()); + } + }; + } + + + public static NumberConstraint _floor(final NumberConstraint a) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return Math.floor(a.getValue()); + } + }; + } + + + public static NumberConstraint _neg(final NumberConstraint a) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return -a.getValue(); + } + }; + } + + + public static NumberConstraint _add(final Object a, final Object b) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return _nv(a) + _nv(b); + } + }; + } + + + public static NumberConstraint _sub(final Object a, final Object b) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return _nv(a) - _nv(b); + } + }; + } + + + public static NumberConstraint _mul(final Object a, final Object b) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return _nv(a) * _nv(b); + } + }; + } + + + public static NumberConstraint _div(final Object a, final Object b) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return _nv(a) / _nv(b); + } + }; + } + + + public static NumberConstraint _percent(final Object whole, final Object percent) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return _nv(whole) * (_nv(percent) / 100); + } + }; + } + + + public static NumberConstraint _width(final RectConstraint r) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return r.getRect().getSize().x; + } + }; + } + + + public static NumberConstraint _height(final RectConstraint r) + { + return new NumberConstraint() { + + @Override + public double getValue() + { + return r.getRect().getSize().y; + } + }; + } + + + /* ================= Layout utilities ================= */ + + public static RectConstraint _row(final RectConstraint r, final int rows, final int index) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final double height = r.getRect().getSize().y; + final double perRow = height / rows; + + final Coord origin = r.getRect().getOrigin().add(0, perRow * index); + final Coord size = r.getRect().getSize().setY(perRow); + + return Rect.fromSize(origin, size); + } + }; + } + + + public static RectConstraint _column(final RectConstraint r, final int columns, final int index) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final double width = r.getRect().getSize().x; + final double perCol = width / columns; + + final Coord origin = r.getRect().getOrigin().add(perCol * index, 0); + final Coord size = r.getRect().getSize().setX(perCol); + + return Rect.fromSize(origin, size); + } + }; + } + + + public static RectConstraint _tile(final RectConstraint r, final int rows, final int cols, final int left, final int top) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final double height = r.getRect().getSize().y; + final double width = r.getRect().getSize().y; + final double perRow = height / rows; + final double perCol = width / cols; + + final Coord origin = r.getRect().getOrigin().add(perCol * left, perRow * (rows - top - 1)); + + return Rect.fromSize(origin, perCol, perRow); + } + }; + } + + + /* ================= Rect manipulation ================= */ + + public static RectConstraint _shrink(RectConstraint r, Object shrink) + { + final NumberConstraint n = _n(shrink); + return _shrink(r, n, n, n, n); + } + + + public static RectConstraint _shrink(RectConstraint context, Object horiz, Object vert) + { + return _shrink(context, horiz, vert, horiz, vert); + } + + + public static RectConstraint _shrink(final RectConstraint r, final Object xmin, final Object ymin, final Object xmax, final Object ymax) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(_nv(xmin), _nv(ymin), _nv(xmax), _nv(ymax)); + } + }; + } + + + public static RectConstraint _shrink_up(final RectConstraint r, final Object shrink) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(0, _nv(shrink), 0, 0); + } + }; + } + + + public static RectConstraint _shrink_down(final RectConstraint r, final Object shrink) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(0, 0, 0, _nv(shrink)); + } + }; + } + + + public static RectConstraint _shrink_left(final RectConstraint r, final Object shrink) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(_nv(shrink), 0, 0, 0); + } + }; + } + + + public static RectConstraint _shrink_right(final RectConstraint r, final Object shrink) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(0, 0, _nv(shrink), 0); + } + }; + } + + + public static RectConstraint _grow(RectConstraint r, Object grow) + { + final NumberConstraint n = _n(grow); + return _grow(r, n, n, n, n); + } + + + public static RectConstraint _grow(RectConstraint r, Object horiz, Object vert) + { + return _grow(r, horiz, vert, horiz, vert); + } + + + public static RectConstraint _grow(final RectConstraint r, final Object xmin, final Object ymin, final Object xmax, final Object ymax) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().grow(_nv(xmin), _nv(ymin), _nv(xmax), _nv(ymax)); + } + }; + } + + + public static RectConstraint _grow_up(final RectConstraint r, final Object grow) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().grow(0, _nv(grow), 0, 0); + } + }; + } + + + public static RectConstraint _grow_down(final RectConstraint r, final Object grow) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().grow(0, 0, 0, _nv(grow)); + } + }; + } + + + public static RectConstraint _grow_left(final RectConstraint r, final Object grow) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().grow(_nv(grow), 0, 0, 0); + } + }; + } + + + public static RectConstraint _grow_right(final RectConstraint r, final Object grow) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().grow(0, 0, _nv(grow), 0); + } + }; + } + + + /* ================= Box creation ================= */ + + public static RectConstraint _box(final Object width, final Object height) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + //@formatter:off + return Rect.fromSize( + 0, + 0, + _nv(width), + _nv(height) + ); + //@formatter:on + } + }; + } + + + public static RectConstraint _box(final RectConstraint r, final Object width, final Object height) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final Coord origin = r.getRect().getOrigin(); + + //@formatter:off + return Rect.fromSize( + origin.x, + origin.y, + _nv(width), + _nv(height) + ); + //@formatter:on + } + }; + } + + + public static RectConstraint _box(final RectConstraint r, final Object x, final Object y, final Object width, final Object height) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final Coord origin = r.getRect().getOrigin(); + + //@formatter:off + return Rect.fromSize( + origin.x + _nv(x), + origin.y + _nv(y), + _nv(width), + _nv(height) + ); + //@formatter:on + } + }; + } + + + public static RectConstraint _box_abs(final RectConstraint r, final Object xmin, final Object ymin, final Object xmax, final Object ymax) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final Coord origin = r.getRect().getOrigin(); + + //@formatter:off + return new Rect(origin.add(_nv(xmin), _nv(ymin)), origin.add(_nv(xmax), _nv(ymax))); + //@formatter:on + } + }; + } + + + /** + * Center rect around given coords + * + * @param r rect + * @param x + * @param y + * @return centered + */ + public static RectConstraint _centered(final RectConstraint r, final Object x, final Object y) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final Coord size = r.getRect().getSize(); + + return Rect.fromSize(_nv(x) - size.x / 2D, _nv(y) - size.y / 2D, size.x, size.y); + } + }; + } + + + public static RectConstraint _move(final RectConstraint r, final Object x, final Object y) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().add(_nv(x), _nv(y)); + } + }; + } + + + /* ================= Rect bounds ================= */ + + /** + * Get zero-sized rect at the center + * + * @param r context + * @return center + */ + public static RectConstraint _center(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getCenter(), 0, 0); + } + }; + } + + + public static RectConstraint _left_edge(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(0, 0, r.getRect().getWidth(), 0); + } + }; + } + + + public static RectConstraint _top_edge(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(0, 0, 0, r.getRect().getHeight()); + } + }; + } + + + public static RectConstraint _right_edge(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(r.getRect().getWidth(), 0, 0, 0); + } + }; + } + + + public static RectConstraint _bottom_edge(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return r.getRect().shrink(0, r.getRect().getHeight(), 0, 0); + } + }; + } + + + public static RectConstraint _left_top(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getHMinVMin(), 0, 0); + } + }; + } + + + public static RectConstraint _left_bottom(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getHMinVMax(), 0, 0); + } + }; + } + + + public static RectConstraint _right_top(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getHMaxVMin(), 0, 0); + } + }; + } + + + public static RectConstraint _right_bottom(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getHMaxVMax(), 0, 0); + } + }; + } + + + public static RectConstraint _center_top(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getCenterVMin(), 0, 0); + } + }; + } + + + public static RectConstraint _center_bottom(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getCenterVMax(), 0, 0); + } + }; + } + + + public static RectConstraint _center_left(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getCenterHMin(), 0, 0); + } + }; + } + + + public static RectConstraint _center_right(final RectConstraint r) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + return Rect.fromSize(r.getRect().getCenterHMax(), 0, 0); + } + }; + } + + + /* ================= Helpers ================= */ + + public static RectCache _cache(final RectConstraint rc) + { + return new RectCache(rc); + } + + + public static RectCache _cache(final Poller poller, final RectConstraint rc) + { + return new RectCache(poller, rc); + } + + + /** + * Convert {@link Number} to {@link NumberConstraint} if needed + * + * @param o unknown numeric value + * @return converted + */ + public static NumberConstraint _n(final Object o) + { + + if (o instanceof NumberConstraint) return (NumberConstraint) o; + + if (o instanceof Number) return new NumberConstraint() { + + @Override + public double getValue() + { + return ((Number) o).doubleValue(); + } + }; + + throw new IllegalArgumentException("Invalid numeric type."); + } + + + /** + * Convert {@link Number} or {@link NumberConstraint} to double (current + * value) + * + * @param o unknown numeric value + * @return double value + */ + public static double _nv(final Object o) + { + return _n(o).getValue(); + } + +} diff --git a/src/mightypork/utils/math/constraints/ContextAdapter.java b/src/mightypork/gamecore/gui/constraints/ContextAdapter.java similarity index 89% rename from src/mightypork/utils/math/constraints/ContextAdapter.java rename to src/mightypork/gamecore/gui/constraints/ContextAdapter.java index 2e96c29..87f005c 100644 --- a/src/mightypork/utils/math/constraints/ContextAdapter.java +++ b/src/mightypork/gamecore/gui/constraints/ContextAdapter.java @@ -1,4 +1,4 @@ -package mightypork.utils.math.constraints; +package mightypork.gamecore.gui.constraints; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/utils/math/constraints/NumberConstraint.java b/src/mightypork/gamecore/gui/constraints/NumberConstraint.java similarity index 77% rename from src/mightypork/utils/math/constraints/NumberConstraint.java rename to src/mightypork/gamecore/gui/constraints/NumberConstraint.java index d00cfbc..1d0d778 100644 --- a/src/mightypork/utils/math/constraints/NumberConstraint.java +++ b/src/mightypork/gamecore/gui/constraints/NumberConstraint.java @@ -1,4 +1,4 @@ -package mightypork.utils.math.constraints; +package mightypork.gamecore.gui.constraints; /** diff --git a/src/mightypork/utils/math/constraints/PluggableContext.java b/src/mightypork/gamecore/gui/constraints/PluggableContext.java similarity index 88% rename from src/mightypork/utils/math/constraints/PluggableContext.java rename to src/mightypork/gamecore/gui/constraints/PluggableContext.java index 8671918..72af20b 100644 --- a/src/mightypork/utils/math/constraints/PluggableContext.java +++ b/src/mightypork/gamecore/gui/constraints/PluggableContext.java @@ -1,4 +1,4 @@ -package mightypork.utils.math.constraints; +package mightypork.gamecore.gui.constraints; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/gamecore/gui/constraints/Poller.java b/src/mightypork/gamecore/gui/constraints/Poller.java new file mode 100644 index 0000000..1089d97 --- /dev/null +++ b/src/mightypork/gamecore/gui/constraints/Poller.java @@ -0,0 +1,49 @@ +package mightypork.gamecore.gui.constraints; + + +import java.util.LinkedHashSet; +import java.util.Set; + +import mightypork.gamecore.control.interf.Pollable; + + +/** + * Used to poll a number of {@link Pollable}s, such as {@link RectCache} + * + * @author MightyPork + */ +public class Poller implements Pollable { + + private final Set pollables = new LinkedHashSet<>(); + + + /** + * Add a pollable + * + * @param p pollable + */ + public void add(Pollable p) + { + pollables.add(p); + } + + + /** + * Remove a pollalbe + * + * @param p pollable + */ + public void remove(Pollable p) + { + pollables.remove(p); + } + + + @Override + public void poll() + { + for (final Pollable p : pollables) { + p.poll(); + } + } +} diff --git a/src/mightypork/gamecore/gui/constraints/RectCache.java b/src/mightypork/gamecore/gui/constraints/RectCache.java new file mode 100644 index 0000000..dc89582 --- /dev/null +++ b/src/mightypork/gamecore/gui/constraints/RectCache.java @@ -0,0 +1,55 @@ +package mightypork.gamecore.gui.constraints; + + +import mightypork.gamecore.control.interf.Pollable; +import mightypork.utils.math.coord.Rect; + + +/** + * {@link RectConstraint} cache, used to reduce CPU load with very complex + * constraints.
+ * Calculates only when polled. + * + * @author MightyPork + */ +public class RectCache implements RectConstraint, Pollable { + + private final RectConstraint observed; + private final Rect cached = new Rect(); + + + /** + * @param observed cached constraint + */ + public RectCache(RectConstraint observed) { + this.observed = observed; + poll(); + } + + + /** + * Create and join a poller + * + * @param poller poller to join + * @param rc observed constraint + */ + public RectCache(Poller poller, RectConstraint rc) { + this(rc); + poller.add(this); + } + + + @Override + public Rect getRect() + { + return cached; + } + + + @Override + public void poll() + { + cached.setTo(observed.getRect()); + } + +} diff --git a/src/mightypork/utils/math/constraints/RectConstraint.java b/src/mightypork/gamecore/gui/constraints/RectConstraint.java similarity index 81% rename from src/mightypork/utils/math/constraints/RectConstraint.java rename to src/mightypork/gamecore/gui/constraints/RectConstraint.java index 34b6961..5b311b1 100644 --- a/src/mightypork/utils/math/constraints/RectConstraint.java +++ b/src/mightypork/gamecore/gui/constraints/RectConstraint.java @@ -1,4 +1,4 @@ -package mightypork.utils.math.constraints; +package mightypork.gamecore.gui.constraints; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/gamecore/gui/screens/LayeredScreen.java b/src/mightypork/gamecore/gui/screens/LayeredScreen.java index f10fd3f..36f414d 100644 --- a/src/mightypork/gamecore/gui/screens/LayeredScreen.java +++ b/src/mightypork/gamecore/gui/screens/LayeredScreen.java @@ -6,6 +6,7 @@ import java.util.TreeSet; import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.render.Render; +import mightypork.utils.math.coord.Coord; /** @@ -60,4 +61,32 @@ public abstract class LayeredScreen extends Screen { removeChildClient(layer); } + + @Override + protected void onScreenEnter() + { + for (final ScreenLayer layer : layers) { + layer.onScreenEnter(); + } + } + + + @Override + protected void onScreenLeave() + { + + for (final ScreenLayer layer : layers) { + layer.onScreenLeave(); + } + } + + + @Override + protected void onSizeChanged(Coord size) + { + for (final ScreenLayer layer : layers) { + layer.onSizeChanged(size); + } + } + } diff --git a/src/mightypork/gamecore/gui/screens/Screen.java b/src/mightypork/gamecore/gui/screens/Screen.java index a38e53f..9fd53ed 100644 --- a/src/mightypork/gamecore/gui/screens/Screen.java +++ b/src/mightypork/gamecore/gui/screens/Screen.java @@ -5,12 +5,12 @@ import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.AppSubModule; import mightypork.gamecore.control.bus.events.ScreenChangeEvent; import mightypork.gamecore.control.interf.NoImpl; -import mightypork.gamecore.gui.renderers.Renderable; +import mightypork.gamecore.gui.components.Renderable; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.gamecore.input.KeyBinder; import mightypork.gamecore.input.KeyBindingPool; import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.render.Render; -import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/gamecore/gui/screens/ScreenLayer.java b/src/mightypork/gamecore/gui/screens/ScreenLayer.java index 65a559f..5c355f8 100644 --- a/src/mightypork/gamecore/gui/screens/ScreenLayer.java +++ b/src/mightypork/gamecore/gui/screens/ScreenLayer.java @@ -2,11 +2,13 @@ package mightypork.gamecore.gui.screens; import mightypork.gamecore.control.AppSubModule; -import mightypork.gamecore.gui.renderers.Renderable; +import mightypork.gamecore.control.interf.NoImpl; +import mightypork.gamecore.gui.components.Renderable; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.gamecore.input.KeyBinder; import mightypork.gamecore.input.KeyBindingPool; import mightypork.gamecore.input.KeyStroke; -import mightypork.utils.math.constraints.RectConstraint; +import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Rect; @@ -70,6 +72,38 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable - * Import statically for best experience. - * - * @author MightyPork - */ -public class ConstraintFactory { - - public static NumberConstraint c_min(final Object a, final Object b) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return Math.min(n(a).getValue(), n(b).getValue()); - } - }; - } - - - public static NumberConstraint c_max(final Object a, final Object b) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return Math.max(n(a).getValue(), n(b).getValue()); - } - }; - } - - - public static NumberConstraint c_abs(final NumberConstraint a) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return Math.abs(a.getValue()); - } - }; - } - - - public static NumberConstraint c_half(final NumberConstraint a) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return a.getValue() / 2; - } - }; - } - - - public static NumberConstraint c_round(final NumberConstraint a) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return Math.round(a.getValue()); - } - }; - } - - - public static RectConstraint c_round(final RectConstraint r) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - return r.getRect().round(); - } - }; - } - - - public static NumberConstraint c_ceil(final NumberConstraint a) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return Math.ceil(a.getValue()); - } - }; - } - - - public static NumberConstraint c_floor(final NumberConstraint a) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return Math.floor(a.getValue()); - } - }; - } - - - public static NumberConstraint c_neg(final NumberConstraint a) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return -a.getValue(); - } - }; - } - - - public static NumberConstraint c_add(final Object a, final Object b) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return n(a).getValue() + n(b).getValue(); - } - }; - } - - - public static NumberConstraint c_sub(final Object a, final Object b) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return n(a).getValue() - n(b).getValue(); - } - }; - } - - - public static NumberConstraint c_mul(final Object a, final Object b) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return n(a).getValue() * n(b).getValue(); - } - }; - } - - - public static NumberConstraint c_div(final Object a, final Object b) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return n(a).getValue() / n(b).getValue(); - } - }; - } - - - public static NumberConstraint c_percent(final Object whole, final Object percent) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return n(whole).getValue() * (n(percent).getValue() / 100); - } - }; - } - - - public static NumberConstraint c_width(final RectConstraint r) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return r.getRect().getSize().x; - } - }; - } - - - public static NumberConstraint c_height(final RectConstraint r) - { - return new NumberConstraint() { - - @Override - public double getValue() - { - return r.getRect().getSize().y; - } - }; - } - - - public static RectConstraint c_row(final RectConstraint r, final int rows, final int index) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - final double height = r.getRect().getSize().y; - final double perRow = height / rows; - - final Coord origin = r.getRect().getOrigin().add(0, perRow * index); - final Coord size = r.getRect().getSize().setY(perRow); - - return Rect.fromSize(origin, size); - } - }; - } - - - public static RectConstraint c_column(final RectConstraint r, final int columns, final int index) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - final double width = r.getRect().getSize().x; - final double perCol = width / columns; - - final Coord origin = r.getRect().getOrigin().add(perCol * index, 0); - final Coord size = r.getRect().getSize().setX(perCol); - - return Rect.fromSize(origin, size); - } - }; - } - - - public static RectConstraint c_shrink(RectConstraint r, Object shrink) - { - final NumberConstraint n = n(shrink); - return c_shrink(r, n, n, n, n); - } - - - public static RectConstraint c_shrink(RectConstraint context, Object horiz, Object vert) - { - return c_shrink(context, horiz, vert, horiz, vert); - } - - - public static RectConstraint c_shrink(final RectConstraint r, final Object x1, final Object y1, final Object x2, final Object y2) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - return r.getRect().shrink(n(x1).getValue(), n(y1).getValue(), n(x2).getValue(), n(y2).getValue()); - } - }; - } - - - public static RectConstraint c_center(final RectConstraint r) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - return Rect.fromSize(r.getRect().getCenter(), 0, 0); - } - }; - } - - - public static RectConstraint c_grow(RectConstraint r, Object grow) - { - final NumberConstraint n = n(grow); - return c_grow(r, n, n, n, n); - } - - - public static RectConstraint c_grow(RectConstraint r, Object horiz, Object vert) - { - return c_grow(r, horiz, vert, horiz, vert); - } - - - public static RectConstraint c_grow(final RectConstraint r, final Object x1, final Object y1, final Object x2, final Object y2) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - return r.getRect().grow(n(x1).getValue(), n(y1).getValue(), n(x2).getValue(), n(y2).getValue()); - } - }; - } - - - public static RectConstraint c_tile(final RectConstraint r, final int rows, final int cols, final int left, final int top) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - final double height = r.getRect().getSize().y; - final double width = r.getRect().getSize().y; - final double perRow = height / rows; - final double perCol = width / cols; - - final Coord origin = r.getRect().getOrigin().add(perCol * left, perRow * (rows - top - 1)); - - return Rect.fromSize(origin, perCol, perRow); - } - }; - } - - - public static RectConstraint c_box(final RectConstraint r, final Object width, final Object height) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - final Coord origin = r.getRect().getOrigin(); - - //@formatter:off - return Rect.fromSize( - origin.x, - origin.y, - n(width).getValue(), - n(height).getValue() - ); - //@formatter:on - } - }; - } - - - public static RectConstraint c_box(final RectConstraint r, final Object x, final Object y, final Object width, final Object height) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - final Coord origin = r.getRect().getOrigin(); - - //@formatter:off - return Rect.fromSize( - origin.x + n(x).getValue(), - origin.y + n(y).getValue(), - n(width).getValue(), - n(height).getValue() - ); - //@formatter:on - } - }; - } - - - /** - * Center rect around given coords - * - * @param r rect - * @param x - * @param y - * @return centered - */ - public static RectConstraint c_centered(final RectConstraint r, final Object x, final Object y) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - final Coord size = r.getRect().getSize(); - - return Rect.fromSize(n(x).getValue() - size.x / 2D, n(y).getValue() - size.y / 2D, size.x, size.y); - } - }; - } - - - public static RectConstraint c_box_abs(final RectConstraint r, final Object x1, final Object y1, final Object x2, final Object y2) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - final Coord origin = r.getRect().getOrigin(); - - //@formatter:off - return new Rect(origin.add(n(x1).getValue(), n(y1).getValue()), origin.add(n(x2).getValue(), n(y2).getValue())); - //@formatter:on - } - }; - } - - - public static RectConstraint c_move(final RectConstraint r, final Object x, final Object y) - { - return new RectConstraint() { - - @Override - public Rect getRect() - { - return r.getRect().add(n(x).getValue(), n(y).getValue()); - } - }; - } - - - /** - * Convert {@link Double} to {@link NumberConstraint} if needed - * - * @param o unknown numeric value - * @return converted - */ - public static NumberConstraint n(final Object o) - { - - if (o instanceof NumberConstraint) return (NumberConstraint) o; - - if (o instanceof Number) return new NumberConstraint() { - - @Override - public double getValue() - { - return ((Number) o).doubleValue(); - } - }; - - throw new IllegalArgumentException("Invalid numeric type."); - } - -} diff --git a/src/mightypork/utils/math/coord/Coord.java b/src/mightypork/utils/math/coord/Coord.java index 4a6980c..6e8dff9 100644 --- a/src/mightypork/utils/math/coord/Coord.java +++ b/src/mightypork/utils/math/coord/Coord.java @@ -3,8 +3,8 @@ package mightypork.utils.math.coord; import java.util.Random; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.utils.math.Calc; -import mightypork.utils.math.constraints.RectConstraint; /** diff --git a/src/mightypork/utils/math/coord/Rect.java b/src/mightypork/utils/math/coord/Rect.java index 1ce3b5f..7093702 100644 --- a/src/mightypork/utils/math/coord/Rect.java +++ b/src/mightypork/utils/math/coord/Rect.java @@ -1,8 +1,8 @@ package mightypork.utils.math.coord; +import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.utils.math.Calc; -import mightypork.utils.math.constraints.RectConstraint; /** @@ -67,15 +67,15 @@ public class Rect implements RectConstraint { /** * Make rect from min coord and size * - * @param xMin min x - * @param yMin min y + * @param xmin min x + * @param ymin min y * @param width size x * @param height size y * @return the new rect */ - public static Rect fromSize(double xMin, double yMin, double width, double height) + public static Rect fromSize(double xmin, double ymin, double width, double height) { - return new Rect(xMin, yMin, xMin + width, yMin + height); + return new Rect(xmin, ymin, xmin + width, ymin + height); } /** Lowest coordinates xy */ @@ -117,13 +117,13 @@ public class Rect implements RectConstraint { /** * New Rect * - * @param x1 lower x - * @param y1 lower y - * @param x2 upper x - * @param y2 upper y + * @param xmin lower x + * @param ymin lower y + * @param xmax upper x + * @param ymax upper y */ - public Rect(double x1, double y1, double x2, double y2) { - setTo(x1, y1, x2, y2); + public Rect(double xmin, double ymin, double xmax, double ymax) { + setTo(xmin, ymin, xmax, ymax); } @@ -227,7 +227,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getCenterV1() + public Coord getCenterVMin() { return new Coord((max.x + min.x) / 2D, min.y); } @@ -238,7 +238,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getCenterH1() + public Coord getCenterHMin() { return new Coord(min.x, (max.y + min.y) / 2D); } @@ -249,7 +249,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getCenterH2() + public Coord getCenterHMax() { return new Coord(max.x, (max.y + min.y) / 2D); } @@ -260,7 +260,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getCenterV2() + public Coord getCenterVMax() { return new Coord((max.x + min.x) / 2D, max.y); } @@ -271,7 +271,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getX1Y1() + public Coord getHMinVMin() { return new Coord(min.x, min.y); } @@ -282,7 +282,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getX1Y2() + public Coord getHMinVMax() { return new Coord(min.x, max.y); } @@ -295,7 +295,7 @@ public class Rect implements RectConstraint { */ public Coord getMax() { - return getX2Y2(); + return getHMaxVMax(); } @@ -306,7 +306,7 @@ public class Rect implements RectConstraint { */ public Coord getMin() { - return getX1Y1(); + return getHMinVMin(); } @@ -326,7 +326,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getX2Y1() + public Coord getHMaxVMin() { return new Coord(max.x, min.y); } @@ -337,7 +337,7 @@ public class Rect implements RectConstraint { * * @return center */ - public Coord getX2Y2() + public Coord getHMaxVMax() { return new Coord(max.x, max.y); } @@ -399,31 +399,31 @@ public class Rect implements RectConstraint { /** * Shrink the rect * - * @param x1 shrink - * @param y1 shrink - * @param x2 shrink - * @param y2 shrink + * @param xmin shrink + * @param ymin shrink + * @param xmax shrink + * @param ymax shrink * @return changed copy */ - public Rect shrink(double x1, double y1, double x2, double y2) + public Rect shrink(double xmin, double ymin, double xmax, double ymax) { - return copy().shrink_ip(x1, y1, x2, y2); + return copy().shrink_ip(xmin, ymin, xmax, ymax); } /** * Shrink the rect in place * - * @param x1 shrink - * @param y1 shrink - * @param x2 shrink - * @param y2 shrink + * @param xmin shrink + * @param ymin shrink + * @param xmax shrink + * @param ymax shrink * @return this */ - public Rect shrink_ip(double x1, double y1, double x2, double y2) + public Rect shrink_ip(double xmin, double ymin, double xmax, double ymax) { - min.add_ip(x1, y2); - max.add_ip(-x2, -y1); + min.add_ip(xmin, ymin); + max.add_ip(-xmax, -ymax); return this; } @@ -484,31 +484,31 @@ public class Rect implements RectConstraint { /** * Grow the rect * - * @param x1 growth - * @param y1 growth - * @param x2 growth - * @param y2 growth + * @param xmin growth + * @param ymin growth + * @param xmax growth + * @param ymax growth * @return changed copy */ - public Rect grow(double x1, double y1, double x2, double y2) + public Rect grow(double xmin, double ymin, double xmax, double ymax) { - return copy().grow_ip(x1, y1, x2, y2); + return copy().grow_ip(xmin, ymin, xmax, ymax); } /** * Grow the rect in place * - * @param x1 growth - * @param y1 growth - * @param x2 growth - * @param y2 growth + * @param xmin growth + * @param ymin growth + * @param xmax growth + * @param ymax growth * @return this */ - public Rect grow_ip(double x1, double y1, double x2, double y2) + public Rect grow_ip(double xmin, double ymin, double xmax, double ymax) { - min.add_ip(-x1, -y2); - max.add_ip(x2, y1); + min.add_ip(-xmin, -ymin); + max.add_ip(xmax, ymax); return this; } @@ -617,17 +617,17 @@ public class Rect implements RectConstraint { /** * Set to coordinates * - * @param x1 lower x - * @param y1 lower y - * @param x2 upper x - * @param y2 upper y + * @param xmin lower x + * @param ymin lower y + * @param xmax upper x + * @param ymax upper y */ - public void setTo(double x1, double y1, double x2, double y2) + public void setTo(double xmin, double ymin, double xmax, double ymax) { - min.x = Calc.min(x1, x2); - min.y = Calc.min(y1, y2); - max.x = Calc.max(x1, x2); - max.y = Calc.max(y1, y2); + min.x = Calc.min(xmin, xmax); + min.y = Calc.min(ymin, ymax); + max.x = Calc.max(xmin, xmax); + max.y = Calc.max(ymin, ymax); } @@ -723,7 +723,7 @@ public class Rect implements RectConstraint { /** * @return lower x */ - public double x1() + public double xMin() { return min.x; } @@ -732,7 +732,7 @@ public class Rect implements RectConstraint { /** * @return upper x */ - public double x2() + public double xMax() { return max.x; } @@ -741,7 +741,7 @@ public class Rect implements RectConstraint { /** * @return lower y */ - public double y1() + public double yMin() { return min.y; } @@ -750,7 +750,7 @@ public class Rect implements RectConstraint { /** * @return upper y */ - public double y2() + public double yMax() { return max.y; }