Finished Coord system, but some bugs in rects.

This commit is contained in:
Ondřej Hruška
2014-04-12 01:57:40 +02:00
parent e3e77dc308
commit 950943e01c
73 changed files with 4661 additions and 3426 deletions
@@ -6,7 +6,7 @@ import java.io.IOException;
import mightypork.gamecore.loading.DeferredResource;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.LoggedName;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Vec;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.SoundStore;
@@ -204,7 +204,7 @@ public class DeferredAudio extends DeferredResource {
* @param pos coord
* @return source id
*/
public int playAsEffect(double pitch, double gain, boolean loop, Coord pos)
public int playAsEffect(double pitch, double gain, boolean loop, Vec pos)
{
if (!ensureLoaded()) return -1;
@@ -12,7 +12,10 @@ 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.Coord;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
@@ -26,10 +29,10 @@ import org.newdawn.slick.openal.SoundStore;
*/
public class SoundSystem extends RootBusNode implements Updateable {
private static final Coord INITIAL_LISTENER_POS = Coord.ZERO;
private static final Vec INITIAL_LISTENER_POS = Vec.ZERO;
private static final int MAX_SOURCES = 256;
private static Coord listener = new Coord();
private static VecMutable listener = new MutableCoord(0,0,0);
private static boolean inited;
@@ -39,7 +42,7 @@ public class SoundSystem extends RootBusNode implements Updateable {
*
* @param pos
*/
public static void setListener(Coord pos)
public static void setListener(Vec pos)
{
listener.setTo(pos);
FloatBuffer buf3 = Buffers.alloc(3);
@@ -60,9 +63,9 @@ public class SoundSystem extends RootBusNode implements Updateable {
/**
* @return listener coordinate
*/
public static Coord getListener()
public static VecView getListener()
{
return listener;
return listener.view();
}
// -- instance --
@@ -3,7 +3,7 @@ package mightypork.gamecore.audio.players;
import mightypork.gamecore.audio.DeferredAudio;
import mightypork.gamecore.audio.Volume;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Vec;
/**
@@ -59,7 +59,7 @@ public class EffectPlayer extends BaseAudioPlayer {
* @param pos play position
* @return source id
*/
public int play(double pitch, double gain, Coord pos)
public int play(double pitch, double gain, Vec pos)
{
if (!hasAudio()) return -1;
@@ -1,8 +1,9 @@
package mightypork.gamecore.control.bus.events;
import mightypork.gamecore.gui.constraints.RectConstraint;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
/**
@@ -18,7 +19,7 @@ public class MouseButtonEvent implements Event<MouseButtonEvent.Listener> {
private final int button;
private final int wheeld;
private final Coord pos;
private final Vec pos;
private final boolean down;
@@ -30,7 +31,7 @@ public class MouseButtonEvent implements Event<MouseButtonEvent.Listener> {
* @param down button pressed
* @param wheeld wheel change
*/
public MouseButtonEvent(Coord pos, int button, boolean down, int wheeld) {
public MouseButtonEvent(Vec pos, int button, boolean down, int wheeld) {
this.button = button;
this.down = down;
this.pos = pos;
@@ -77,9 +78,9 @@ public class MouseButtonEvent implements Event<MouseButtonEvent.Listener> {
/**
* @return mouse position when the event occurred
*/
public Coord getPos()
public VecView getPos()
{
return pos;
return pos.view();
}
@@ -109,7 +110,7 @@ public class MouseButtonEvent implements Event<MouseButtonEvent.Listener> {
*/
public boolean isOver(RectConstraint rect)
{
return pos.isInRect(rect.getRect());
return rect.getRect().contains(pos);
}
@@ -2,7 +2,8 @@ package mightypork.gamecore.control.bus.events;
import mightypork.gamecore.control.bus.events.types.UnloggedEvent;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
/**
@@ -13,15 +14,15 @@ import mightypork.utils.math.coord.Coord;
@UnloggedEvent
public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
private final Coord move;
private final Coord pos;
private final Vec move;
private final Vec pos;
/**
* @param pos end pos
* @param move move vector
*/
public MouseMotionEvent(Coord pos, Coord move) {
public MouseMotionEvent(Vec pos, Vec move) {
this.move = move;
this.pos = pos;
}
@@ -30,18 +31,18 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
/**
* @return movement since last {@link MouseMotionEvent}
*/
public Coord getMove()
public VecView getMove()
{
return move;
return move.view();
}
/**
* @return current mouse position
*/
public Coord getPos()
public VecView getPos()
{
return pos;
return pos.view();
}
@@ -1,7 +1,8 @@
package mightypork.gamecore.control.bus.events;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
/**
@@ -12,7 +13,7 @@ import mightypork.utils.math.coord.Coord;
public class ScreenChangeEvent implements Event<ScreenChangeEvent.Listener> {
private final boolean fullscreen;
private final Coord screenSize;
private final Vec screenSize;
private final boolean fsChanged;
@@ -21,7 +22,7 @@ public class ScreenChangeEvent implements Event<ScreenChangeEvent.Listener> {
* @param fullscreen is now fullscreen
* @param size new screen size
*/
public ScreenChangeEvent(boolean fsChanged, boolean fullscreen, Coord size) {
public ScreenChangeEvent(boolean fsChanged, boolean fullscreen, Vec size) {
this.fullscreen = fullscreen;
this.screenSize = size;
this.fsChanged = fsChanged;
@@ -49,9 +50,9 @@ public class ScreenChangeEvent implements Event<ScreenChangeEvent.Listener> {
/**
* @return new screen size
*/
public Coord getScreenSize()
public VecView getScreenSize()
{
return screenSize;
return screenSize.view();
}
@@ -4,7 +4,7 @@ package mightypork.gamecore.control.timing;
import java.util.LinkedHashSet;
import java.util.Set;
import mightypork.gamecore.gui.constraints.RectCache;
import mightypork.utils.math.constraints.RectCache;
/**
@@ -1,9 +1,9 @@
package mightypork.gamecore.gui.components;
import mightypork.gamecore.gui.constraints.PluggableContext;
import mightypork.gamecore.gui.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.constraints.PluggableContext;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.Rect;
/**
@@ -1,9 +1,9 @@
package mightypork.gamecore.gui.components;
import mightypork.gamecore.gui.constraints.ContextAdapter;
import mightypork.gamecore.gui.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.constraints.ContextAdapter;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.Rect;
/**
@@ -1,10 +1,10 @@
package mightypork.gamecore.gui.components.layout;
import static mightypork.gamecore.gui.constraints.Constraints.*;
import static mightypork.utils.math.constraints.Constraints.*;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.gamecore.gui.constraints.RectConstraint;
import mightypork.utils.math.constraints.RectConstraint;
/**
@@ -9,8 +9,8 @@ import mightypork.gamecore.control.bus.clients.BusNode;
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;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.Rect;
/**
@@ -1,10 +1,10 @@
package mightypork.gamecore.gui.components.layout;
import static mightypork.gamecore.gui.constraints.Constraints.*;
import static mightypork.utils.math.constraints.Constraints.*;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.gamecore.gui.constraints.RectConstraint;
import mightypork.utils.math.constraints.RectConstraint;
/**
@@ -6,8 +6,11 @@ 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.Coord;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.coord.CoordValue;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.rect.Rect;
import mightypork.utils.string.StringProvider;
import mightypork.utils.string.StringProvider.StringWrapper;
@@ -28,7 +31,7 @@ public class TextPainter extends PluggableRenderer {
private boolean shadow;
private RGB shadowColor = RGB.BLACK;
private Coord shadowOffset = Coord.one();
private VecMutable shadowOffset = new MutableCoord(1, 1);
/**
@@ -87,13 +90,13 @@ public class TextPainter extends PluggableRenderer {
final Rect rect = getRect();
if (shadow) {
font.draw(str, rect.add(shadowOffset), align, shadowColor);
font.draw(str, rect.move(shadowOffset), align, shadowColor);
}
font.draw(str, rect, align, color);
}
public void setShadow(RGB color, Coord offset)
public void setShadow(RGB color, Vec offset)
{
setShadow(true);
setShadowColor(color);
@@ -113,9 +116,9 @@ public class TextPainter extends PluggableRenderer {
}
public void setShadowOffset(Coord shadowOffset)
public void setShadowOffset(Vec shadowOffset)
{
this.shadowOffset = shadowOffset;
this.shadowOffset.setTo(shadowOffset);
}
@@ -1,854 +0,0 @@
package mightypork.gamecore.gui.constraints;
import mightypork.gamecore.control.timing.Poller;
import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
/**
* Constraint factory.<br>
* 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_top(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_bottom(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 side)
{
return _box(side, side);
}
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
}
};
}
public static RectConstraint _centered(final RectConstraint r, final RectConstraint centerTo)
{
return new RectConstraint() {
@Override
public Rect getRect()
{
final Coord size = r.getRect().getSize();
final Coord center = centerTo.getRect().getCenter();
return Rect.fromSize(center.x() - size.x() / 2D, center.y() - size.y() / 2D, size.x(), size.y());
}
};
}
/**
* 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();
}
}
@@ -1,30 +0,0 @@
package mightypork.gamecore.gui.constraints;
import mightypork.utils.math.coord.Rect;
/**
* Basic pluggable context implementation
*
* @author MightyPork
*/
public abstract class ContextAdapter implements PluggableContext {
private RectConstraint backing = null;
@Override
public void setContext(RectConstraint rect)
{
this.backing = rect;
}
@Override
public Rect getRect()
{
return backing.getRect();
}
}
@@ -1,16 +0,0 @@
package mightypork.gamecore.gui.constraints;
/**
* Numeric constraint
*
* @author MightyPork
*/
public interface NumberConstraint {
/**
* @return current value
*/
double getValue();
}
@@ -1,23 +0,0 @@
package mightypork.gamecore.gui.constraints;
import mightypork.utils.math.coord.Rect;
/**
* Interface for constraints that can be assigned context
*
* @author MightyPork
*/
public interface PluggableContext extends RectConstraint {
/**
* @param rect context to set
*/
abstract void setContext(RectConstraint rect);
@Override
abstract Rect getRect();
}
@@ -1,56 +0,0 @@
package mightypork.gamecore.gui.constraints;
import mightypork.gamecore.control.timing.Pollable;
import mightypork.gamecore.control.timing.Poller;
import mightypork.utils.math.coord.Rect;
/**
* {@link RectConstraint} cache, used to reduce CPU load with very complex
* constraints.<br>
* 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());
}
}
@@ -1,18 +0,0 @@
package mightypork.gamecore.gui.constraints;
import mightypork.utils.math.coord.Rect;
/**
* Rect constraint (ie. region)
*
* @author MightyPork
*/
public interface RectConstraint {
/**
* @return rect region
*/
Rect getRect();
}
@@ -5,7 +5,7 @@ import java.util.Collection;
import java.util.TreeSet;
import mightypork.gamecore.control.AppAccess;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Vec;
/**
@@ -79,7 +79,7 @@ public abstract class LayeredScreen extends Screen {
@Override
protected void onSizeChanged(Coord size)
protected void onSizeChanged(Vec size)
{
for (final ScreenLayer layer : layers) {
layer.onSizeChanged(size);
@@ -6,13 +6,13 @@ import mightypork.gamecore.control.AppSubModule;
import mightypork.gamecore.control.bus.events.ScreenChangeEvent;
import mightypork.gamecore.control.interf.DefaultImpl;
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.coord.Coord;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.rect.Rect;
/**
@@ -153,7 +153,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
* @param size screen size
*/
@DefaultImpl
protected void onSizeChanged(Coord size)
protected void onSizeChanged(Vec size)
{
//
}
@@ -1,15 +1,16 @@
package mightypork.gamecore.gui.screens;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppSubModule;
import mightypork.gamecore.control.interf.DefaultImpl;
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.coord.Coord;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.rect.Rect;
/**
@@ -98,7 +99,7 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
* @param size screen size
*/
@DefaultImpl
protected void onSizeChanged(Coord size)
protected void onSizeChanged(Vec size)
{
//
}
@@ -109,4 +110,6 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
*/
public abstract int getPriority();
}
+18 -14
View File
@@ -10,7 +10,9 @@ import mightypork.gamecore.control.timing.Updateable;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.rogue.events.ActionRequest;
import mightypork.rogue.events.ActionRequest.RequestType;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
@@ -92,8 +94,8 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
Display.processMessages();
final Coord moveSum = Coord.zero();
final Coord lastPos = Coord.zero();
final VecMutable moveSum = new MutableCoord();
final VecMutable lastPos = new MutableCoord();
boolean wasMouse = false;
while (Mouse.next()) {
@@ -115,24 +117,26 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
}
private void onMouseEvent(Coord moveSum, Coord lastPos)
private void onMouseEvent(VecMutable moveSum, VecMutable lastPos)
{
final int button = Mouse.getEventButton();
final boolean down = Mouse.getEventButtonState();
final Coord pos = new Coord(Mouse.getEventX(), Mouse.getEventY());
final Coord move = new Coord(Mouse.getEventDX(), Mouse.getEventDY());
final VecMutable pos = new MutableCoord(Mouse.getEventX(), Mouse.getEventY());
final VecMutable move = new MutableCoord(Mouse.getEventDX(), Mouse.getEventDY());
final int wheeld = Mouse.getEventDWheel();
if (DisplaySystem.yAxisDown) {
flipScrY(pos);
move.mul_ip(1, -1, 1);
move.mul(1, -1, 1);
}
if (button != -1 || wheeld != 0) {
getEventBus().send(new MouseButtonEvent(pos.freeze(), button, down, wheeld));
getEventBus().send(new MouseButtonEvent(pos.copy(), button, down, wheeld));
}
moveSum.add_ip(move.freeze());
moveSum.add(move);
lastPos.setTo(pos);
}
@@ -147,9 +151,9 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
}
private static void flipScrY(Coord c)
private static void flipScrY(VecMutable c)
{
if (DisplaySystem.yAxisDown) c.setY_ip(DisplaySystem.getSize().y() - c.y());
if (DisplaySystem.yAxisDown) c.setY(DisplaySystem.getHeight() - c.y());
}
@@ -158,11 +162,11 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
*
* @return mouse position
*/
public static Coord getMousePos()
public static VecView getMousePos()
{
final Coord pos = new Coord(Mouse.getX(), Mouse.getY());
final VecMutable pos = new MutableCoord(Mouse.getX(), Mouse.getY());
flipScrY(pos);
return pos.freeze();
return pos.view();
}
@@ -9,12 +9,15 @@ import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppModule;
import mightypork.gamecore.control.bus.events.ScreenChangeEvent;
import mightypork.gamecore.control.timing.FpsMeter;
import mightypork.gamecore.gui.constraints.NumberConstraint;
import mightypork.gamecore.gui.constraints.RectConstraint;
import mightypork.utils.logging.Log;
import mightypork.utils.math.coord.ConstraintCoordView;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.constraints.Constraints;
import mightypork.utils.math.constraints.NumberConstraint;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.ConstraintCoord;
import mightypork.utils.math.coord.SynthCoord2D;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.Rect;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
@@ -175,7 +178,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
*
* @return size
*/
public static Coord getSize()
public static VecView getSize()
{
return size;
}
@@ -228,7 +231,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
@Override
public Rect getRect()
{
return new Rect(Coord.ZERO, getSize());
return new Rect(Vec.ZERO, getSize());
}
@@ -239,26 +242,49 @@ public class DisplaySystem extends AppModule implements RectConstraint {
{
return fpsMeter.getFPS();
}
public Vec getCenter()
{
return center;
}
/** Screen width constraint */
public static final NumberConstraint width = new NumberConstraint() {
private static final VecView size = new SynthCoord2D() {
@Override
public double getValue()
public double y()
{
return getHeight();
}
@Override
public double x()
{
return getWidth();
}
};
/** Screen width constraint */
private static final NumberConstraint width = size.xc();
/** Screen height constaint */
public static final NumberConstraint height = new NumberConstraint() {
private static final NumberConstraint height = size.yc();
private static final VecView center = new SynthCoord2D() {
@Override
public double getValue()
public double y()
{
return getHeight();
return size.half().x();
}
@Override
public double x()
{
return size.half().y();
}
};
public static final ConstraintCoordView size = new ConstraintCoordView(width, height, null);
}
+15 -12
View File
@@ -9,8 +9,11 @@ import mightypork.gamecore.render.textures.TxQuad;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.coord.CoordValue;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.Rect;
import mightypork.utils.math.rect.Rect;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
@@ -26,9 +29,9 @@ import org.newdawn.slick.util.ResourceLoader;
*/
public class Render {
public static final Coord AXIS_X = new Coord(1, 0, 0).freeze();
public static final Coord AXIS_Y = new Coord(0, 1, 0).freeze();
public static final Coord AXIS_Z = new Coord(0, 0, 1).freeze();
public static final Vec AXIS_X = new CoordValue(1, 0, 0);
public static final Vec AXIS_Y = new CoordValue(0, 1, 0);
public static final Vec AXIS_Z = new CoordValue(0, 0, 1);
/**
@@ -84,7 +87,7 @@ public class Render {
*
* @param coord coord
*/
public static void translate(Coord coord)
public static void translate(Vec coord)
{
glTranslated(coord.x(), coord.y(), coord.z());
}
@@ -120,7 +123,7 @@ public class Render {
*
* @param factor vector of scaling factors
*/
public static void scale(Coord factor)
public static void scale(Vec factor)
{
glScaled(factor.x(), factor.y(), factor.z());
}
@@ -209,9 +212,9 @@ public class Render {
* @param angle rotate angle
* @param axis rotation axis
*/
public static void rotate(double angle, Coord axis)
public static void rotate(double angle, Vec axis)
{
final Coord vec = axis.norm(1);
final Vec vec = axis.view().norm(1);
glRotated(angle, vec.x(), vec.y(), vec.z());
}
@@ -434,7 +437,7 @@ public class Render {
* @param colorHMinVMax
*/
public static void quadColor(Rect quad, RGB colorHMinVMin, RGB colorHMaxVMin, RGB colorHMaxVMax, RGB colorHMinVMax)
{
{
final double x1 = quad.xMin();
final double y1 = quad.yMin();
final double x2 = quad.xMax();
@@ -508,7 +511,7 @@ public class Render {
*/
public static void quadTextured(Rect quad, Texture texture)
{
quadTextured(quad, Rect.ONE, texture, RGB.WHITE);
quadTextured(quad, new Rect(0,0,1,1), texture, RGB.WHITE);
}
@@ -545,7 +548,7 @@ public class Render {
// fix projection for changed size
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
final Coord s = DisplaySystem.getSize();
final Vec s = DisplaySystem.getSize();
glViewport(0, 0, s.xi(), s.yi());
glOrtho(0, s.x(), (DisplaySystem.yAxisDown ? 1 : -1) * s.y(), 0, -1000, 1000);
@@ -2,9 +2,11 @@ package mightypork.gamecore.render.fonts;
import mightypork.gamecore.render.Render;
import static mightypork.utils.math.constraints.Constraints.*;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.Rect;
/**
@@ -49,7 +51,7 @@ public class FontRenderer {
* @param height drawing height
* @return taken space (width, height)
*/
public Coord getNeededSpace(String text, double height)
public Vec getNeededSpace(String text, double height)
{
return font.getNeededSpace(text).mul(getScale(height));
}
@@ -104,11 +106,11 @@ public class FontRenderer {
* @param height drawing height
* @param color drawing color
*/
public void draw(String text, Coord pos, double height, RGB color)
public void draw(String text, Vec pos, double height, RGB color)
{
Render.pushMatrix();
Render.translate(pos.round());
Render.translate(pos.view().round());
Render.scaleXY(getScale(height));
font.draw(text, color);
@@ -142,20 +144,20 @@ public class FontRenderer {
*/
public void draw(String text, Rect bounds, Align align, RGB color)
{
Coord start;
VecView start;
switch (align) {
case LEFT:
start = bounds.getHMinVMin();
start = _top_left(bounds);
break;
case CENTER:
start = bounds.getCenterVMin();
start = _center_top(bounds);
break;
case RIGHT:
default:
start = bounds.getHMaxVMin();
start = _top_right(bounds);
break;
}
@@ -171,7 +173,7 @@ public class FontRenderer {
* @param height drawing height
* @param align horizontal alignment
*/
public void draw(String text, Coord pos, double height, Align align)
public void draw(String text, VecView pos, double height, Align align)
{
draw(text, pos, height, align, this.color);
}
@@ -186,12 +188,12 @@ public class FontRenderer {
* @param align horizontal alignment
* @param color drawing color
*/
public void draw(String text, Coord pos, double height, Align align, RGB color)
public void draw(String text, VecView pos, double height, Align align, RGB color)
{
final double w = getWidth(text, height);
final Coord start;
final VecView start;
switch (align) {
case LEFT:
@@ -2,7 +2,7 @@ package mightypork.gamecore.render.fonts;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.VecView;
/**
@@ -27,7 +27,7 @@ public interface GLFont {
* @param text string to check
* @return coord (width, height)
*/
Coord getNeededSpace(String text);
VecView getNeededSpace(String text);
/**
@@ -23,7 +23,8 @@ import mightypork.gamecore.render.fonts.GLFont;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.CoordValue;
import mightypork.utils.math.coord.VecView;
import org.lwjgl.BufferUtils;
import org.lwjgl.util.glu.GLU;
@@ -422,9 +423,9 @@ public class CachedFont implements GLFont {
@Override
public Coord getNeededSpace(String text)
public VecView getNeededSpace(String text)
{
return new Coord(getWidth(text), getHeight());
return new CoordValue(getWidth(text), getHeight());
}
}
@@ -13,7 +13,8 @@ import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.LoggedName;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
/**
@@ -169,9 +170,9 @@ public class DeferredFont extends DeferredResource implements GLFont {
* @return coord (width, height)
*/
@Override
public Coord getNeededSpace(String text)
public VecView getNeededSpace(String text)
{
if (!ensureLoaded()) return Coord.zero();
if (!ensureLoaded()) return Vec.ZERO;
return font.getNeededSpace(text);
}
@@ -4,7 +4,8 @@ package mightypork.gamecore.render.fonts.impl;
import mightypork.gamecore.render.fonts.GLFont;
import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
/**
@@ -22,9 +23,9 @@ public class NullFont implements GLFont {
@Override
public Coord getNeededSpace(String str)
public VecView getNeededSpace(String str)
{
return Coord.zero();
return Vec.ZERO;
}
@@ -5,7 +5,7 @@ import mightypork.gamecore.loading.DeferredResource;
import mightypork.gamecore.loading.MustLoadInMainThread;
import mightypork.gamecore.render.Render;
import mightypork.utils.logging.LoggedName;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.rect.Rect;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
@@ -6,7 +6,7 @@ import java.util.HashMap;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppAdapter;
import mightypork.gamecore.control.bus.events.ResourceLoadRequest;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.rect.Rect;
import org.newdawn.slick.opengl.Texture;
@@ -1,7 +1,7 @@
package mightypork.gamecore.render.textures;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.rect.Rect;
import org.newdawn.slick.opengl.Texture;
@@ -74,7 +74,7 @@ public class TxQuad {
*/
public TxQuad(Texture tx, Rect uvs) {
this.tx = tx;
this.uvs = uvs.view();
this.uvs = uvs;
}
@@ -85,7 +85,7 @@ public class TxQuad {
*/
public TxQuad(TxQuad txQuad) {
this.tx = txQuad.tx;
this.uvs = txQuad.uvs.view();
this.uvs = txQuad.uvs;
}