Base of Constraints system.

v5stable
Ondřej Hruška 11 years ago
parent 1af5f01520
commit 5c261c4513
  1. 1
      src/mightypork/rogue/display/ScreenSplash.java
  2. 34
      src/mightypork/rogue/display/constraints/Constraint.java
  3. 9
      src/mightypork/rogue/display/constraints/ConstraintContext.java
  4. 84
      src/mightypork/rogue/testing/TestConstraints.java
  5. 19
      src/mightypork/rogue/util/RenderUtils.java
  6. 465
      src/mightypork/utils/math/coord/Coord.java
  7. 7
      src/mightypork/utils/math/coord/CoordAnimated.java
  8. 32
      src/mightypork/utils/math/coord/Rect.java

@ -2,7 +2,6 @@ package mightypork.rogue.display;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import mightypork.rogue.App;

@ -0,0 +1,34 @@
package mightypork.rogue.display.constraints;
import mightypork.utils.math.coord.Coord;
public abstract class Constraint implements ConstraintContext {
protected ConstraintContext context;
public Constraint(ConstraintContext context) {
this.context = context;
}
public void setContext(ConstraintContext context) {
this.context = context;
}
public ConstraintContext getContext()
{
return context;
}
protected Coord origin() {
return context.getRect().getOrigin();
}
protected Coord size() {
return context.getRect().getSize();
}
}

@ -0,0 +1,9 @@
package mightypork.rogue.display.constraints;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
public interface ConstraintContext {
public Rect getRect();
}

@ -0,0 +1,84 @@
package mightypork.rogue.testing;
import mightypork.rogue.display.constraints.Constraint;
import mightypork.rogue.display.constraints.ConstraintContext;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
public class TestConstraints {
public static void main(String[] args)
{
ConstraintContext context = new ConstraintContext() {
@Override
public Rect getRect()
{
return Rect.fromSize(new Coord(0, 0), new Coord(400, 300));
}
};
class Navbar extends Constraint {
private double height;
public Navbar(ConstraintContext context, double height) {
super(context);
this.height = height;
}
@Override
public Rect getRect()
{
return Rect.fromSize(origin().setY(size().y - height), size().setY(height));
}
}
class TileHorizontal extends Constraint {
private int count;
private int tile;
public TileHorizontal(ConstraintContext context, int tileCount, int aTile) {
super(context);
this.count = tileCount;
setTile(aTile);
}
public void setTile(int aTile)
{
if (aTile > count) throw new IndexOutOfBoundsException("Tile count exceeded: " + aTile + " max: " + count);
this.tile = aTile;
}
@Override
public Rect getRect()
{
Coord size = size().mul(1D / count, 1);
return Rect.fromSize(origin().add(size.x * tile, 0), size);
}
}
Navbar nb = new Navbar(context, 100);
TileHorizontal tile = new TileHorizontal(nb, 5, 0);
for (int i = 0; i < 5; i++) {
tile.setTile(i);
System.out.println(tile.getRect());
}
System.out.println("nb:" + nb.getRect());
System.out.println("ctx:" + context.getRect());
}
}

@ -24,6 +24,7 @@ public class RenderUtils {
private static final Coord AXIS_Y = new Coord(0, 1, 0);
private static final Coord AXIS_Z = new Coord(0, 0, 1);
/**
* Render quad 2D
*
@ -371,7 +372,7 @@ public class RenderUtils {
*/
public static void quadRect(Rect rect)
{
quadCoord(rect.getMin(), rect.getMax());
quadCoord(rect.getOrigin(), rect.getMax());
}
@ -384,7 +385,7 @@ public class RenderUtils {
public static void quadRect(Rect rect, RGB color)
{
setColor(color);
quadCoord(rect.getMin(), rect.getMax());
quadCoord(rect.getOrigin(), rect.getMax());
}
@ -398,7 +399,7 @@ public class RenderUtils {
*/
public static void quadBorder(Rect rect, double border, RGB borderColor, RGB insideColor)
{
quadCoordBorder(rect.getMin(), rect.getMax(), border, borderColor, insideColor);
quadCoordBorder(rect.getOrigin(), rect.getMax(), border, borderColor, insideColor);
}
@ -411,7 +412,7 @@ public class RenderUtils {
*/
public static void quadGradH(Rect rect, RGB colorLeft, RGB colorRight)
{
quadCoordGradH(rect.getMin(), rect.getMax(), colorLeft, colorRight);
quadCoordGradH(rect.getOrigin(), rect.getMax(), colorLeft, colorRight);
}
@ -424,7 +425,7 @@ public class RenderUtils {
*/
public static void quadGradHBilinear(Rect rect, RGB colorOuter, RGB colorMiddle)
{
quadCoordGradHBilinear(rect.getMin(), rect.getMax(), colorOuter, colorMiddle);
quadCoordGradHBilinear(rect.getOrigin(), rect.getMax(), colorOuter, colorMiddle);
}
@ -437,7 +438,7 @@ public class RenderUtils {
*/
public static void quadGradV(Rect rect, RGB colorTop, RGB colorBottom)
{
quadCoordGradV(rect.getMin(), rect.getMax(), colorTop, colorBottom);
quadCoordGradV(rect.getOrigin(), rect.getMax(), colorTop, colorBottom);
}
@ -450,7 +451,7 @@ public class RenderUtils {
*/
public static void quadGradVBilinear(Rect rect, RGB colorOuter, RGB colorMiddle)
{
quadCoordGradVBilinear(rect.getMin(), rect.getMax(), colorOuter, colorMiddle);
quadCoordGradVBilinear(rect.getOrigin(), rect.getMax(), colorOuter, colorMiddle);
}
@ -464,7 +465,7 @@ public class RenderUtils {
*/
public static void quadRectOutset(Rect rect, double border, RGB fill, boolean inset)
{
quadCoordOutset(rect.getMin(), rect.getMax(), border, fill, inset);
quadCoordOutset(rect.getOrigin(), rect.getMax(), border, fill, inset);
}
@ -526,7 +527,7 @@ public class RenderUtils {
setColor(tint);
TextureManager.bind(texture);
quadTexturedAbs(quad, txCoords.div(texture.getImageHeight()));
quadTexturedAbs(quad, txCoords.mul(1 / texture.getImageHeight()));
TextureManager.unbind();
glDisable(GL_TEXTURE_2D);

@ -7,13 +7,12 @@ import mightypork.utils.math.Calc;
/**
* Coordinate class, object with three or two double coordinates.<br>
* Coordinate in 3D space, or a vector of three {@link Double}s<br>
*
* @author MightyPork
*/
public class Coord {
/** RNG */
protected static Random rand = new Random();
@ -29,33 +28,6 @@ public class Coord {
return a.distTo(b);
}
/**
* Generate random coord (gaussian)
*
* @param max max distance from 0
* @return new coord
*/
public static Coord random(double max)
{
return new Coord(Calc.clampd(rand.nextGaussian() * max, -max * 2, max * 2), Calc.clampd(rand.nextGaussian() * max, -max * 2, max * 2),
Calc.clampd(rand.nextGaussian() * max, -max * 2, max * 2));
}
/**
* Generate random coord (min-max)
*
* @param min min offset
* @param max max offset
* @return new coord
*/
public static Coord random(double min, double max)
{
return new Coord((rand.nextBoolean() ? -1 : 1) * (min + rand.nextDouble() * (max - min)), (rand.nextBoolean() ? -1 : 1) * (min + rand.nextDouble() * (max - min)),
(rand.nextBoolean() ? -1 : 1) * (min + rand.nextDouble() * (max - min)));
}
/** X coordinate */
public double x = 0;
@ -78,9 +50,7 @@ public class Coord {
* @param copied copied coord
*/
public Coord(Coord copied) {
this.x = copied.x;
this.y = copied.y;
this.z = copied.z;
setTo(copied);
}
@ -108,76 +78,73 @@ public class Coord {
/**
* Get a copy offset by vector
* Add a vector, in a copy
*
* @param vec offset
* @return the offset copy
* @return changed copy
*/
public Coord add(Coord vec)
{
return getCopy().add_ip(vec);
return copy().add_ip(vec);
}
/**
* Get a copy offset by 2D coordinate
* Add a vector, in place
*
* @param x x offset
* @param y y offset
* @return the offset copy
* @param vec offset
* @return this
*/
public Coord add(Number x, Number y)
public Coord add_ip(Coord vec)
{
return getCopy().add_ip(x, y);
return add_ip(vec.x, vec.y, vec.z);
}
/**
* Get a copy offset by 3D coordinate
* Add to each component, in a copy.<br>
* Z is unchanged.
*
* @param x x offset
* @param y y offset
* @param z z offset
* @return the offset copy
* @return changed copy
*/
public Coord add(Number x, Number y, Number z)
public Coord add(Number x, Number y)
{
return getCopy().add_ip(x, y, z);
return copy().add_ip(x, y);
}
/**
* Offset by vector in place
* Add to each component, in place.<br>
* Z is unchanged.
*
* @param vec offset
* @param x x offset
* @param y y offset
* @return this
*/
public Coord add_ip(Coord vec)
public Coord add_ip(Number x, Number y)
{
this.x += vec.x;
this.y += vec.y;
this.z += vec.z;
return this;
return add_ip(x, y, 0);
}
/**
* Offset by 2D coordinate in place
* Add to each component, in a copy.
*
* @param x x offset
* @param y y offset
* @return this
* @param z z offset
* @return changed copy
*/
public Coord add_ip(Number x, Number y)
public Coord add(Number x, Number y, Number z)
{
this.x += x.doubleValue();
this.y += y.doubleValue();
return this;
return copy().add_ip(x, y, z);
}
/**
* Offset by 3D coordinate in place
* Add to each component, in place.
*
* @param x x offset
* @param y y offset
@ -194,9 +161,11 @@ public class Coord {
/**
* @return copy of this vector
* Make a copy
*
* @return a copy
*/
public Coord getCopy()
public Coord copy()
{
return new Coord(x, y, z);
}
@ -215,30 +184,14 @@ public class Coord {
/**
* Get copy divided by number
*
* @param d number to divide by
* @return divided copy
*/
public Coord div(double d)
{
return getCopy().div_ip(d);
}
/**
* Divide by number in place
* Check if this rectangle in inside a rectangular zone
*
* @param d number to divide by
* @return this
* @param rect checked rect.
* @return is inside
*/
public Coord div_ip(double d)
public boolean isInRect(Rect rect)
{
if (d == 0) return this;
x /= d;
y /= d;
z /= d;
return this;
return isInRect(rect.min, rect.max);
}
@ -256,111 +209,150 @@ public class Coord {
/**
* Check if this rectangle in inside a rectangular zone
* Get middle of line to other point
*
* @param rect checked rect.
* @return is inside
* @param other other point
* @return middle
*/
public boolean isInRect(Rect rect)
public Coord midTo(Coord other)
{
return isInRect(rect.min, rect.max);
return add(vecTo(other).half_ip());
}
/**
* Get middle of line to other point
* Get copy divided by two
*
* @param other other point
* @return middle
* @return copy halved
*/
public Coord midTo(Coord other)
public Coord half()
{
return add(vecTo(other).mul_ip(0.5));
return copy().half_ip();
}
/**
* Multiply by number
* Divide in place by two
*
* @param d number
* @return multiplied copy
* @return this
*/
public Coord mul(double d)
public Coord half_ip()
{
return getCopy().mul_ip(d);
mul_ip(0.5);
return this;
}
/**
* Multiply coords by number
* Multiply each component, in a copy.
*
* @param xd x multiplier
* @param yd y multiplier
* @param zd z multiplier
* @return multiplied copy
* @param d multiplier
* @return changed copy
*/
public Coord mul(double xd, double yd, double zd)
public Coord mul(double d)
{
return getCopy().mul_ip(xd, yd, zd);
return copy().mul_ip(d);
}
/**
* Multiply by number in place
* Multiply each component, in place.
*
* @param d multiplier
* @return this
*/
public Coord mul_ip(double d)
{
x *= d;
y *= d;
z *= d;
return this;
return mul_ip(d, d, d);
}
/**
* Multiply coords by number in place
* Multiply each component, in a copy.
*
* @param xd x multiplier
* @param yd y multiplier
* @param zd z multiplier
* @param vec vector of multipliers
* @return changed copy
*/
public Coord mul(Coord vec)
{
return copy().mul_ip(vec);
}
/**
* Multiply each component, in a copy.<br>
* Z is unchanged.
*
* @param x x multiplier
* @param y y multiplier
* @return changed copy
*/
public Coord mul(double x, int y)
{
return copy().mul_ip(x, y);
}
/**
* Multiply each component, in place.<br>
* Z is unchanged.
*
* @param x x multiplier
* @param y y multiplier
* @return this
*/
public Coord mul_ip(double xd, double yd, double zd)
public Coord mul_ip(double x, double y)
{
x *= xd;
y *= yd;
z *= zd;
return this;
return mul_ip(x, y, 1);
}
/**
* offset randomly
* Multiply each component, in a copy.
*
* @param max max +- offset
* @return offset coord
* @param x x multiplier
* @param y y multiplier
* @param z z multiplier
* @return changed copy
*/
public Coord random_offset(double max)
public Coord mul(double x, double y, double z)
{
Coord v = random(1).norm_ip(0.00001 + rand.nextDouble() * max);
return copy().mul_ip(x, y, z);
}
return getCopy().add_ip(v);
/**
* Multiply each component, in place.
*
* @param vec vector of multipliers
* @return this
*/
public Coord mul_ip(Coord vec)
{
return mul_ip(vec.x, vec.y, vec.z);
}
/**
* offset randomly
* Multiply each component, in place.
*
* @param min min offset
* @param max max offset
* @return offset coord
* @param x x multiplier
* @param y y multiplier
* @param z z multiplier
* @return this
*/
public Coord random_offset(double min, double max)
public Coord mul_ip(double x, double y, double z)
{
this.x *= x;
this.y *= y;
this.z *= z;
return this;
}
public Coord random_offset(double max)
{
return getCopy().add_ip(random(min, max));
return copy().random_offset_ip(max);
}
@ -372,7 +364,20 @@ public class Coord {
*/
public Coord random_offset_ip(double max)
{
return add(random(max));
return add(random(1).norm_ip(rand.nextDouble() * max));
}
/**
* offset randomly
*
* @param min min offset
* @param max max offset
* @return offset coord
*/
public Coord random_offset(double min, double max)
{
return copy().random_offset_ip(min, max);
}
@ -385,7 +390,7 @@ public class Coord {
*/
public Coord random_offset_ip(double min, double max)
{
add(random(min, max));
add_ip(random(min, max));
return this;
}
@ -397,7 +402,7 @@ public class Coord {
*/
public Coord round()
{
return getCopy().round_ip();
return copy().round_ip();
}
@ -420,7 +425,7 @@ public class Coord {
*
* @param other other coord
*/
public void setMax(Coord other)
public void setToMax(Coord other)
{
x = Math.max(x, other.x);
y = Math.max(y, other.y);
@ -433,7 +438,7 @@ public class Coord {
*
* @param other other coord
*/
public void setMin(Coord other)
public void setToMin(Coord other)
{
x = Math.min(x, other.x);
y = Math.min(y, other.y);
@ -449,8 +454,7 @@ public class Coord {
*/
public Coord setTo(Coord copied)
{
setTo(copied.x, copied.y, copied.z);
return this;
return setTo(copied.x, copied.y, copied.z);
}
@ -463,8 +467,7 @@ public class Coord {
*/
public Coord setTo(Number x, Number y)
{
setTo(x, y, 0);
return this;
return setTo(x, y, 0);
}
@ -493,7 +496,7 @@ public class Coord {
*/
public Coord setX(Number x)
{
return getCopy().setX_ip(x);
return copy().setX_ip(x);
}
@ -518,7 +521,7 @@ public class Coord {
*/
public Coord setY(Number y)
{
return getCopy().setY_ip(y);
return copy().setY_ip(y);
}
@ -543,7 +546,7 @@ public class Coord {
*/
public Coord setZ(Number z)
{
return getCopy().setZ_ip(z);
return copy().setZ_ip(z);
}
@ -568,7 +571,7 @@ public class Coord {
*/
public Coord sub(Coord vec)
{
return getCopy().sub_ip(vec);
return copy().sub_ip(vec);
}
@ -581,7 +584,7 @@ public class Coord {
*/
public Coord sub(Number x, Number y)
{
return getCopy().sub_ip(x, y);
return copy().sub_ip(x, y);
}
@ -595,7 +598,7 @@ public class Coord {
*/
public Coord sub(Number x, Number y, Number z)
{
return getCopy().sub_ip(x, y, z);
return copy().sub_ip(x, y, z);
}
@ -607,10 +610,7 @@ public class Coord {
*/
public Coord sub_ip(Coord vec)
{
this.x -= vec.x;
this.y -= vec.y;
this.z -= vec.z;
return this;
return sub_ip(vec.x, vec.y, vec.z);
}
@ -623,9 +623,7 @@ public class Coord {
*/
public Coord sub_ip(Number x, Number y)
{
this.x -= x.doubleValue();
this.y -= y.doubleValue();
return this;
return sub_ip(x, y, 0);
}
@ -667,24 +665,6 @@ public class Coord {
}
/**
* @return X as double
*/
public double xd()
{
return x;
}
/**
* @return X as float
*/
public float xf()
{
return (float) x;
}
/**
* @return X as int
*/
@ -703,24 +683,6 @@ public class Coord {
}
/**
* @return Y as double
*/
public double yd()
{
return y;
}
/**
* @return Y as float
*/
public float yf()
{
return (float) y;
}
/**
* @return Y as int
*/
@ -739,24 +701,6 @@ public class Coord {
}
/**
* @return Z as double
*/
public double zd()
{
return z;
}
/**
* @return Z as float
*/
public float zf()
{
return (float) z;
}
/**
* @return Z as int
*/
@ -796,11 +740,11 @@ public class Coord {
* Multiply by other vector, vector multiplication
*
* @param vec other vector
* @return copy multiplied
* @return changed copy
*/
public Coord cross(Coord vec)
{
return getCopy().cross_ip(vec);
return copy().cross_ip(vec);
}
@ -812,7 +756,13 @@ public class Coord {
*/
public Coord cross_ip(Coord vec)
{
setTo(y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x);
//@formatter:off
setTo(
y * vec.z - z * vec.y,
z * vec.x - x * vec.z,
x * vec.y - y * vec.x
);
//@formatter:on
return this;
}
@ -832,11 +782,11 @@ public class Coord {
/**
* Negate all coordinates (* -1)
*
* @return negated coordinate
* @return negated copy
*/
public Coord neg()
{
return getCopy().neg_ip();
return copy().neg_ip();
}
@ -860,60 +810,45 @@ public class Coord {
*/
public Coord norm(double size)
{
return getCopy().norm_ip(size);
return copy().norm_ip(size);
}
/**
* Scale vector to given size, in place
* Scale vector to given size, in place.<br>
* Zero vector remains zero.
*
* @param size size we need
* @return scaled vector
*/
public Coord norm_ip(double size)
{
if (size() == 0) {
z = -1;
}
if (size == 0) {
setTo(0, 0, 0);
return this;
}
if (isZero()) return this;
double k = size / size();
mul_ip(k);
return this;
return mul_ip(k);
}
/**
* Get vector size
*
* @return vector size in units
* @return size in units
*/
public double size()
{
return Math.sqrt(x * x + y * y + z * z);
}
if (isZero()) return 0;
/**
* Get copy divided by two
* @return copy halved
*/
public Coord half()
{
return getCopy().half_ip();
return Math.sqrt(x * x + y * y + z * z);
}
/**
* Divide in place by two
* @return this
* @return true if this coord is a zero coord
*/
public Coord half_ip()
public boolean isZero()
{
mul_ip(0.5);
return this;
return x == 0 && y == 0 && z == 0;
}
@ -955,22 +890,60 @@ public class Coord {
/**
* @return true if this coord is a zero coord
* Generate a zero coordinate
*
* @return coord of all zeros
*/
public boolean isZero()
{
return x == 0 && y == 0 && z == 0;
}
public static Coord zero()
{
return new Coord(0, 0, 0);
}
/**
* Generate a unit coordinate
*
* @return coord of all ones
*/
public static Coord one()
{
return new Coord(1, 1, 1);
}
/**
* Generate random coord (gaussian)
*
* @param max max distance from 0
* @return new coord
*/
public static Coord random(double max)
{
//@formatter:off
return new Coord(
Calc.clampd(rand.nextGaussian() * max, -max * 2, max * 2),
Calc.clampd(rand.nextGaussian() * max, -max * 2, max * 2),
Calc.clampd(rand.nextGaussian() * max, -max * 2, max * 2)
);
//@formatter:on
}
/**
* Generate random coord (min-max)
*
* @param min min offset
* @param max max offset
* @return new coord
*/
public static Coord random(double min, double max)
{
//@formatter:off
return new Coord(
(rand.nextBoolean() ? -1 : 1) * (min + rand.nextDouble() * (max - min)),
(rand.nextBoolean() ? -1 : 1) * (min + rand.nextDouble() * (max - min)),
(rand.nextBoolean() ? -1 : 1) * (min + rand.nextDouble() * (max - min))
);
//@formatter:on
}
}

@ -4,6 +4,11 @@ import mightypork.utils.math.Calc;
import mightypork.utils.time.Updateable;
/**
* TODO revise
*
* @author MightyPork
*/
public class CoordAnimated extends Coord implements Updateable {
private double animTime = 0;
@ -81,7 +86,7 @@ public class CoordAnimated extends Coord implements Updateable {
*/
public Coord animGetCurrent()
{
if (time == 0) return getCopy(); // avoid zero division
if (time == 0) return copy(); // avoid zero division
if (start == null) start = new Coord();
if (offs == null) offs = new Coord();

@ -205,32 +205,6 @@ public class Rect {
}
/**
* Divide in copy
*
* @param factor divisor
* @return offset copy
*/
public Rect div(double factor)
{
return copy().div_ip(factor);
}
/**
* Divide coord in place
*
* @param factor divisor
* @return this
*/
public Rect div_ip(double factor)
{
min.div_ip(factor);
max.div_ip(factor);
return this;
}
/**
* Get copy with the same center and height=0
*
@ -375,7 +349,7 @@ public class Rect {
/**
* @return highjest coordinates xy
* @return highest coordinates xy
*/
public Coord getMax()
{
@ -386,7 +360,7 @@ public class Rect {
/**
* @return lowest coordinates xy
*/
public Coord getMin()
public Coord getOrigin()
{
return min;
}
@ -764,7 +738,7 @@ public class Rect {
@Override
public String toString()
{
return "rect{ " + min + " - " + max + " }";
return String.format("[( %4d, %4d )-( %4d, %4d )]", (int) min.x, (int) min.y, (int) max.x, (int) max.y);
}

Loading…
Cancel
Save