all compiles and "works" now.

v5stable
Ondřej Hruška 10 years ago
parent 2fbebd4fbc
commit de34b66fde
  1. 2
      src/mightypork/gamecore/control/AppSubModule.java
  2. 4
      src/mightypork/gamecore/control/bus/clients/BusNode.java
  3. 42
      src/mightypork/gamecore/control/bus/clients/ClientHub.java
  4. 25
      src/mightypork/gamecore/control/interf/Enableable.java
  5. 15
      src/mightypork/gamecore/gui/Hideable.java
  6. 33
      src/mightypork/gamecore/gui/components/AbstractComponent.java
  7. 163
      src/mightypork/gamecore/gui/components/BusEnabledPainter.java
  8. 60
      src/mightypork/gamecore/gui/components/Component.java
  9. 6
      src/mightypork/gamecore/gui/components/SimplePainter.java
  10. 10
      src/mightypork/gamecore/gui/components/layout/AbstractLayout.java
  11. 18
      src/mightypork/gamecore/gui/components/layout/ColumnHolder.java
  12. 24
      src/mightypork/gamecore/gui/components/layout/RowHolder.java
  13. 3
      src/mightypork/gamecore/gui/components/painters/ImagePainter.java
  14. 3
      src/mightypork/gamecore/gui/components/painters/QuadPainter.java
  15. 3
      src/mightypork/gamecore/gui/components/painters/TextPainter.java
  16. 5
      src/mightypork/gamecore/gui/screens/ScreenLayer.java
  17. 4
      src/mightypork/gamecore/input/InputSystem.java
  18. 3
      src/mightypork/gamecore/loading/AsyncResourceLoader.java
  19. 59
      src/mightypork/gamecore/render/Render.java
  20. 3
      src/mightypork/gamecore/render/fonts/impl/CachedFont.java
  21. 3
      src/mightypork/gamecore/render/textures/TxQuad.java
  22. 39
      src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java
  23. 15
      src/mightypork/rogue/screens/test_bouncyboxes/LayerBouncyBoxes.java
  24. 2
      src/mightypork/test/TestConstr.java
  25. 44
      src/mightypork/test/TestTiled.java
  26. 88
      src/mightypork/utils/math/constraints/rect/Rect.java
  27. 14
      src/mightypork/utils/math/constraints/rect/RectConst.java
  28. 7
      src/mightypork/utils/math/constraints/rect/RectDigest.java
  29. 8
      src/mightypork/utils/math/constraints/rect/RectProxy.java
  30. 79
      src/mightypork/utils/math/constraints/rect/TiledRect.java
  31. 63
      src/mightypork/utils/math/constraints/vect/Vect.java

@ -15,7 +15,7 @@ import mightypork.gamecore.render.DisplaySystem;
*
* @author MightyPork
*/
public abstract class AppSubModule extends BusNode implements AppAccess {
public class AppSubModule extends BusNode implements AppAccess {
private final AppAccess app;

@ -15,7 +15,7 @@ import mightypork.gamecore.control.bus.EventBus;
*
* @author MightyPork
*/
public abstract class BusNode implements BusAccess, DelegatingClient, ToggleableClient {
public abstract class BusNode implements BusAccess, ClientHub {
private final BusAccess busAccess;
@ -58,6 +58,7 @@ public abstract class BusNode implements BusAccess, DelegatingClient, Toggleable
*
* @param client
*/
@Override
public final void addChildClient(Object client)
{
if (client instanceof RootBusNode) {
@ -75,6 +76,7 @@ public abstract class BusNode implements BusAccess, DelegatingClient, Toggleable
*
* @param client subscriber to remove
*/
@Override
public final void removeChildClient(Object client)
{
if (client != null) {

@ -0,0 +1,42 @@
package mightypork.gamecore.control.bus.clients;
import java.util.Collection;
import mightypork.gamecore.control.bus.EventBus;
/**
* Common methods for client hubs (ie delegating vlient implementations)
*
* @author MightyPork
*/
public interface ClientHub extends DelegatingClient, ToggleableClient {
@Override
public boolean doesDelegate();
@Override
public Collection<Object> getChildClients();
@Override
public boolean isListening();
/**
* Add a child subscriber to the {@link EventBus}.<br>
*
* @param client
*/
public void addChildClient(Object client);
/**
* Remove a child subscriber
*
* @param client subscriber to remove
*/
void removeChildClient(Object client);
}

@ -0,0 +1,25 @@
package mightypork.gamecore.control.interf;
/**
* Can be enabled or disabled.<br>
* Implementations should take appropriate action (ie. stop listening to events,
* updating etc.)
*
* @author MightyPork
*/
public interface Enableable {
/**
* Change enabled state
*
* @param yes enabled
*/
public void enable(boolean yes);
/**
* @return true if enabled
*/
public boolean isEnabled();
}

@ -0,0 +1,15 @@
package mightypork.gamecore.gui;
/**
* Element that can be hidden or visible
*
* @author MightyPork
*/
public interface Hideable {
void setVisible(boolean yes);
boolean isVisible();
}

@ -1,33 +0,0 @@
package mightypork.gamecore.gui.components;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppSubModule;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.rect.Rect;
public abstract class AbstractComponent extends AppSubModule implements PluggableRenderable {
private RectBound context;
public AbstractComponent(AppAccess app) {
super(app);
}
@Override
public void setRect(RectBound context)
{
this.context = context;
}
@Override
public Rect getRect()
{
return context.getRect();
}
}

@ -0,0 +1,163 @@
package mightypork.gamecore.gui.components;
import java.util.Collection;
import mightypork.gamecore.audio.SoundSystem;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppSubModule;
import mightypork.gamecore.control.bus.EventBus;
import mightypork.gamecore.control.bus.clients.ClientHub;
import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.utils.annotations.DefaultImpl;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.rect.Rect;
public abstract class BusEnabledPainter extends SimplePainter implements ClientHub, Component, AppAccess {
private RectBound context;
private boolean enabled;
private boolean visible = true;
private final AppSubModule subModule;
public BusEnabledPainter(AppAccess app) {
this.subModule = new AppSubModule(app);
}
@Override
public void setRect(RectBound context)
{
this.context = context;
}
@Override
public Rect getRect()
{
return context.getRect();
}
@Override
public EventBus getEventBus()
{
return subModule.getEventBus();
}
@Override
public Collection<Object> getChildClients()
{
return subModule.getChildClients();
}
@Override
public boolean doesDelegate()
{
return subModule.doesDelegate();
}
@Override
public boolean isListening()
{
return subModule.isListening();
}
@Override
public SoundSystem getSoundSystem()
{
return subModule.getSoundSystem();
}
@Override
public InputSystem getInput()
{
return subModule.getInput();
}
@Override
public DisplaySystem getDisplay()
{
return subModule.getDisplay();
}
@Override
public void shutdown()
{
subModule.shutdown();
}
@Override
public void addChildClient(Object client)
{
subModule.addChildClient(client);
}
@Override
public void removeChildClient(Object client)
{
subModule.removeChildClient(client);
}
@Override
public void enable(boolean yes)
{
subModule.setDelegating(yes);
subModule.setListening(yes);
enabled = yes;
}
@Override
public boolean isEnabled()
{
return enabled;
}
@Override
public void setVisible(boolean visible)
{
this.visible = visible;
}
@Override
public boolean isVisible()
{
return visible;
}
@Override
public final void render()
{
if (!visible) return;
paint();
}
protected abstract void paint();
@Override
@DefaultImpl
public void update(double delta)
{
}
}

@ -0,0 +1,60 @@
package mightypork.gamecore.gui.components;
import mightypork.gamecore.control.bus.clients.ToggleableClient;
import mightypork.gamecore.control.interf.Enableable;
import mightypork.gamecore.control.timing.Updateable;
import mightypork.gamecore.gui.Hideable;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.rect.Rect;
/**
* UI component interface
*
* @author MightyPork
*/
public interface Component extends Hideable, PluggableRenderable, Updateable, Enableable, ToggleableClient {
/**
* Enable the component. This includes listening to event bus, and any
* event-related actions.
*/
@Override
void enable(boolean yes);
@Override
boolean isEnabled();
/**
* Set visible. When not visible, the component should not render.
*/
@Override
void setVisible(boolean yes);
@Override
boolean isVisible();
@Override
Rect getRect();
@Override
void setRect(RectBound rect);
@Override
void render();
@Override
void update(double delta);
@Override
public boolean isListening();
}

@ -1,8 +1,6 @@
package mightypork.gamecore.gui.components.painters;
package mightypork.gamecore.gui.components;
import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.gamecore.gui.components.Renderable;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.rect.Rect;
import mightypork.utils.math.constraints.rect.RectBoundAdapter;
@ -13,7 +11,7 @@ import mightypork.utils.math.constraints.rect.RectBoundAdapter;
*
* @author MightyPork
*/
public abstract class AbstractPainter extends RectBoundAdapter implements PluggableRenderable {
public abstract class SimplePainter extends RectBoundAdapter implements PluggableRenderable {
@Override
public abstract void render();

@ -5,20 +5,20 @@ import java.util.LinkedList;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.bus.EventBus;
import mightypork.gamecore.gui.components.AbstractComponent;
import mightypork.gamecore.gui.components.BusEnabledPainter;
import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.gamecore.gui.components.Renderable;
import mightypork.gamecore.gui.components.painters.AbstractPainter;
import mightypork.gamecore.gui.components.SimplePainter;
import mightypork.utils.math.constraints.RectBound;
/**
* Bag for {@link AbstractPainter} elements with constraints.<br>
* Bag for {@link SimplePainter} elements with constraints.<br>
* Elements are exposed to {@link EventBus}.
*
* @author MightyPork
*/
public abstract class AbstractLayout extends AbstractComponent {
public abstract class AbstractLayout extends BusEnabledPainter {
final LinkedList<PluggableRenderable> elements = new LinkedList<>();
@ -65,7 +65,7 @@ public abstract class AbstractLayout extends AbstractComponent {
@Override
public void render()
public void paint()
{
for (final Renderable element : elements) {
element.render();

@ -4,6 +4,7 @@ package mightypork.gamecore.gui.components.layout;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.rect.TiledRect;
/**
@ -13,18 +14,18 @@ import mightypork.utils.math.constraints.RectBound;
*/
public class ColumnHolder extends AbstractLayout {
private final int cols;
private final TiledRect tiler;
private int col = 0;
/**
* @param app app access
* @param context context
* @param rows number of rows
* @param cols number of columns
*/
public ColumnHolder(AppAccess app, RectBound context, int rows) {
public ColumnHolder(AppAccess app, RectBound context, int cols) {
super(app, context);
this.cols = rows;
this.tiler = getRect().columns(cols).zeroBased();
}
@ -33,11 +34,10 @@ public class ColumnHolder extends AbstractLayout {
* Context must be assigned before rendering.
*
* @param app app access
* @param rows number of rows
* @param cols number of columns
*/
public ColumnHolder(AppAccess app, int rows) {
super(app);
this.cols = rows;
public ColumnHolder(AppAccess app, int cols) {
this(app, null, cols);
}
@ -51,7 +51,7 @@ public class ColumnHolder extends AbstractLayout {
{
if (elem == null) return;
elem.setRect(column(this, cols, col++));
elem.setRect(tiler.column(col++));
attach(elem);
}

@ -4,6 +4,7 @@ package mightypork.gamecore.gui.components.layout;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.rect.TiledRect;
/**
@ -13,31 +14,30 @@ import mightypork.utils.math.constraints.RectBound;
*/
public class RowHolder extends AbstractLayout {
private final int rows;
private final TiledRect tiler;
private int row = 0;
/**
* Make a row holder.<br>
* Context must be assigned before rendering.
*
* @param app app access
* @param context bounding context
* @param rows number of rows
*/
public RowHolder(AppAccess app, RectBound context, int rows) {
super(app, context);
this.rows = rows;
public RowHolder(AppAccess app, int rows) {
this(app, null, rows);
}
/**
* Make a row holder.<br>
* Context must be assigned before rendering.
*
* @param app app access
* @param context bounding context
* @param rows number of rows
*/
public RowHolder(AppAccess app, int rows) {
super(app);
this.rows = rows;
public RowHolder(AppAccess app, RectBound context, int rows) {
super(app, context);
this.tiler = getRect().rows(rows).zeroBased();
}
@ -51,7 +51,7 @@ public class RowHolder extends AbstractLayout {
{
if (elem == null) return;
elem.setRect(row(this, rows, row++));
elem.setRect(tiler.row(row++));
attach(elem);
}

@ -1,6 +1,7 @@
package mightypork.gamecore.gui.components.painters;
import mightypork.gamecore.gui.components.SimplePainter;
import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.TxQuad;
@ -10,7 +11,7 @@ import mightypork.gamecore.render.textures.TxQuad;
*
* @author MightyPork
*/
public class ImagePainter extends AbstractPainter {
public class ImagePainter extends SimplePainter {
private TxQuad texture;

@ -1,6 +1,7 @@
package mightypork.gamecore.gui.components.painters;
import mightypork.gamecore.gui.components.SimplePainter;
import mightypork.gamecore.render.Render;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.color.RGB;
@ -11,7 +12,7 @@ import mightypork.utils.math.color.RGB;
*
* @author MightyPork
*/
public class QuadPainter extends AbstractPainter {
public class QuadPainter extends SimplePainter {
private final RGB colorHMinVMin;
private final RGB colorHMaxVMin;

@ -1,6 +1,7 @@
package mightypork.gamecore.gui.components.painters;
import mightypork.gamecore.gui.components.SimplePainter;
import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.gamecore.render.fonts.GLFont;
@ -19,7 +20,7 @@ import mightypork.utils.string.StringProvider.StringWrapper;
*
* @author MightyPork
*/
public class TextPainter extends AbstractPainter {
public class TextPainter extends SimplePainter {
private final FontRenderer font;
private RGB color;

@ -2,6 +2,7 @@ package mightypork.gamecore.gui.screens;
import mightypork.gamecore.control.AppSubModule;
import mightypork.gamecore.gui.Hideable;
import mightypork.gamecore.gui.components.Renderable;
import mightypork.gamecore.input.KeyBinder;
import mightypork.gamecore.input.KeyBindingPool;
@ -17,7 +18,7 @@ import mightypork.utils.math.constraints.vect.Vect;
*
* @author MightyPork
*/
public abstract class ScreenLayer extends AppSubModule implements Comparable<ScreenLayer>, Renderable, RectBound, KeyBinder {
public abstract class ScreenLayer extends AppSubModule implements Comparable<ScreenLayer>, Renderable, RectBound, KeyBinder, Hideable {
private final Screen screen;
@ -67,12 +68,14 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
}
@Override
public boolean isVisible()
{
return visible;
}
@Override
public void setVisible(boolean visible)
{
this.visible = visible;

@ -103,8 +103,8 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
}
// counters as fields to save memory.
private final VectVar mouseMove = VectVar.makeVar();
private final VectVar mouseLastPos = VectVar.makeVar();
private final VectVar mouseMove = Vect.makeVar();
private final VectVar mouseLastPos = Vect.makeVar();
@Override

@ -114,9 +114,12 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L
}
// apparently, destroy method exists on thread :/
@SuppressWarnings("deprecation")
@Override
public void destroy()
{
Log.i("Stopping resource loader thread.");
stopped = true;
exs.shutdownNow();
}

@ -10,6 +10,7 @@ import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.rect.Rect;
import mightypork.utils.math.constraints.rect.RectDigest;
import mightypork.utils.math.constraints.vect.Vect;
import mightypork.utils.math.constraints.vect.VectConst;
@ -350,20 +351,18 @@ public class Render {
*/
public static void quad(Rect quad)
{
final double x1 = quad.left().value();
final double y1 = quad.top().value();
final double x2 = quad.right().value();
final double y2 = quad.bottom().value();
final RectDigest q = quad.digest();
System.out.println(q);
// draw with color
unbindTexture();
// quad
glBegin(GL_QUADS);
glVertex2d(x1, y2);
glVertex2d(x2, y2);
glVertex2d(x2, y1);
glVertex2d(x1, y1);
glVertex2d(q.left, q.bottom);
glVertex2d(q.right, q.bottom);
glVertex2d(q.right, q.top);
glVertex2d(q.left, q.top);
glEnd();
}
@ -390,25 +389,22 @@ public class Render {
*/
public static void quadUV_nobound(Rect quad, Rect uvs)
{
final double x1 = quad.left().value();
final double y1 = quad.top().value();
final double x2 = quad.right().value();
final double y2 = quad.bottom().value();
final RectDigest q = quad.digest();
final double tx1 = uvs.left().value();
final double ty1 = uvs.top().value();
final double tx2 = uvs.right().value();
final double ty2 = uvs.bottom().value();
final RectDigest u = uvs.digest();
// quad with texture
glTexCoord2d(tx1, ty2);
glVertex2d(x1, y2);
glTexCoord2d(tx2, ty2);
glVertex2d(x2, y2);
glTexCoord2d(tx2, ty1);
glVertex2d(x2, y1);
glTexCoord2d(tx1, ty1);
glVertex2d(x1, y1);
glTexCoord2d(u.left, u.bottom);
glVertex2d(q.left, q.bottom);
glTexCoord2d(u.right, u.bottom);
glVertex2d(q.right, q.bottom);
glTexCoord2d(u.right, u.top);
glVertex2d(q.right, q.top);
glTexCoord2d(u.left, u.top);
glVertex2d(q.left, q.top);
}
@ -436,24 +432,23 @@ public class Render {
*/
public static void quadColor(Rect quad, RGB colorHMinVMin, RGB colorHMaxVMin, RGB colorHMaxVMax, RGB colorHMinVMax)
{
final double x1 = quad.left().value();
final double y1 = quad.top().value();
final double x2 = quad.right().value();
final double y2 = quad.bottom().value();
final RectDigest r = quad.digest();
// draw with color
unbindTexture();
glBegin(GL_QUADS);
setColor(colorHMinVMax);
glVertex2d(x1, y2);
glVertex2d(r.left, r.bottom);
setColor(colorHMaxVMax);
glVertex2d(x2, y2);
glVertex2d(r.right, r.bottom);
setColor(colorHMaxVMin);
glVertex2d(x2, y1);
glVertex2d(r.right, r.top);
setColor(colorHMinVMin);
glVertex2d(x1, y1);
glVertex2d(r.left, r.top);
glEnd();
}

@ -23,6 +23,7 @@ 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.constraints.vect.Vect;
import mightypork.utils.math.constraints.vect.VectConst;
import org.lwjgl.BufferUtils;
@ -426,7 +427,7 @@ public class CachedFont implements GLFont {
@Override
public VectConst getNeededSpace(String text)
{
return VectConst.make(getWidth(text), getLineHeight());
return Vect.make(getWidth(text), getLineHeight());
}
}

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

@ -4,49 +4,30 @@ package mightypork.rogue.screens.test_bouncyboxes;
import java.util.Random;
import mightypork.gamecore.control.timing.Updateable;
import mightypork.gamecore.gui.components.painters.AbstractPainter;
import mightypork.gamecore.gui.components.SimplePainter;
import mightypork.gamecore.render.Render;
import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.num.NumBound;
import mightypork.utils.math.constraints.num.Num;
import mightypork.utils.math.constraints.rect.Rect;
public class BouncyBox extends AbstractPainter implements Updateable {
public class BouncyBox extends SimplePainter implements Updateable {
private final Random rand = new Random();
private final RectBound box;
private final Rect box;
private final AnimDouble pos = new AnimDouble(0, Easing.BOUNCE_OUT);
public BouncyBox() {
// create box
final NumBound side = height(this);
RectBound abox = box(this, side, side);
Rect abox;
// move
final NumBound move_length = sub(width(this), side);
final NumBound offset = mul(move_length, pos);
abox = move(abox, offset, 0);
// add padding
/*
* leftEdge(this)
* .growRight(height(this))
* .move(
* width(this)
* .sub(height(this))
* .mul(pos),
* 0)
* .shrink(
* height(this)
* .perc(10)
* )
*/
abox = shrink(abox, perc(side, 10));
abox = leftEdge().growRight(height());
abox = abox.move(width().sub(height()).mul(pos), Num.ZERO);
abox = abox.shrink(height().perc(10));
box = abox;
}
@ -55,7 +36,7 @@ public class BouncyBox extends AbstractPainter implements Updateable {
@Override
public void render()
{
Render.quad(box.getRect(), RGB.GREEN);
Render.quad(box, RGB.GREEN);
}

@ -13,8 +13,8 @@ import mightypork.gamecore.input.Keys;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.rogue.Res;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.constraints.vect.VectConst;
import mightypork.utils.math.constraints.rect.Rect;
import mightypork.utils.math.constraints.vect.Vect;
public class LayerBouncyBoxes extends ScreenLayer {
@ -45,11 +45,14 @@ public class LayerBouncyBoxes extends ScreenLayer {
});
// shrink screen rect by 8% on all sides
final RectBound holder_rect = shrink(this, perc(width(this), 4));
addChildClient(layout = new RowHolder(screen, holder_rect, 11));
final Rect b = bounds();
for (int i = 0; i <= 9; i++) {
final Rect holder_rect = b.shrink(b.height().perc(8));
addChildClient(layout = new RowHolder(screen, holder_rect, 2));
for (int i = 0; i <= 0; i++) {
final BouncyBox bbr = new BouncyBox();
layout.add(bbr);
boxes.add(bbr);
@ -57,7 +60,7 @@ public class LayerBouncyBoxes extends ScreenLayer {
final TextPainter tp = new TextPainter(Res.getFont("default"), Align.LEFT, RGB.WHITE);
tp.setText("Press \"C\" for \"Cat\" screen.");
tp.setShadow(RGB.RED, VectConst.make(2, 2));
tp.setShadow(RGB.RED, Vect.make(2, 2));
layout.add(tp);
}

@ -42,12 +42,14 @@ public class TestConstr {
{
final Vect a = Vect.make(3, 3);
@SuppressWarnings("deprecation")
final VectConst v = a.freeze().freeze().freeze();
System.out.println("\nTest " + ++cnt + ": " + (v == a.freeze()));
}
{
final Vect a = Vect.make(3, 3);
@SuppressWarnings("deprecation")
final VectConst v = a.freeze().freeze().freeze();
System.out.println("\nTest " + ++cnt + ": " + (v == a.freeze()));
}

@ -0,0 +1,44 @@
package mightypork.test;
import mightypork.utils.math.constraints.num.Num;
import mightypork.utils.math.constraints.num.NumVar;
import mightypork.utils.math.constraints.rect.Rect;
public class TestTiled {
public static void main(String[] args)
{
// {
// RectVar area = Rect.makeVar(0, 0, 100, 100);
//
// TiledRect tiled = area.tiles(5, 5).oneBased();
//
// System.out.println(tiled.span(1, 1, 1, 1));
// System.out.println(tiled.span(1, 1, 3, 1));
// }
// {
// RectVar area = Rect.makeVar(0, 0, 100, 100);
// TiledRect tiled = area.columns(4);
//
// System.out.println(tiled.column(2));
//
//
//
// }
//
{
Rect abox;
final Rect b = Rect.make(100, 100, 100, 10);
final NumVar pos = Num.makeVar(1);
abox = b.leftEdge().growRight(b.height());
abox = abox.move(b.width().sub(b.height()).mul(pos), Num.ZERO);
//abox = abox.shrink(b.height().perc(10));
System.out.println(abox);
}
}
}

@ -193,7 +193,7 @@ public abstract class Rect implements RectBound {
@Override
public String toString()
{
return String.format("Rect { %s - %s }", origin(), origin().freeze().add(size()));
return String.format("Rect { at %s , size %s }", origin(), size());
}
@ -301,7 +301,7 @@ public abstract class Rect implements RectBound {
/**
* Shrink to sides
*
* @param shrink shrink size (horisontal and vertical)
* @param shrink shrink size (horizontal and vertical)
* @return result
*/
@ -311,6 +311,30 @@ public abstract class Rect implements RectBound {
}
/**
* Shrink to all sides
*
* @param shrink shrink
* @return result
*/
public final Rect shrink(double shrink)
{
return shrink(shrink, shrink, shrink, shrink);
}
/**
* Shrink to all sides
*
* @param shrink shrink
* @return result
*/
public final Rect shrink(Num shrink)
{
return shrink(shrink, shrink, shrink, shrink);
}
/**
* Shrink to sides at sides
*
@ -353,13 +377,13 @@ public abstract class Rect implements RectBound {
public Rect shrinkTop(final double shrink)
{
return growTop(-shrink);
return growUp(-shrink);
}
public Rect shrinkBottom(final double shrink)
{
return growBottom(-shrink);
return growDown(-shrink);
}
@ -375,13 +399,13 @@ public abstract class Rect implements RectBound {
}
public Rect growTop(final double shrink)
public Rect growUp(final double shrink)
{
return grow(0, 0, shrink, 0);
}
public Rect growBottom(final double shrink)
public Rect growDown(final double shrink)
{
return grow(0, 0, 0, shrink);
}
@ -423,13 +447,13 @@ public abstract class Rect implements RectBound {
}
public Rect growTop(final Num shrink)
public Rect growUp(final Num shrink)
{
return grow(Num.ZERO, Num.ZERO, shrink, Num.ZERO);
}
public Rect growBottom(final Num shrink)
public Rect growDown(final Num shrink)
{
return grow(Num.ZERO, Num.ZERO, Num.ZERO, shrink);
}
@ -447,6 +471,30 @@ public abstract class Rect implements RectBound {
}
/**
* Grow to all sides
*
* @param grow grow
* @return result
*/
public final Rect grow(double grow)
{
return grow(grow, grow, grow, grow);
}
/**
* Grow to all sides
*
* @param grow grow
* @return result
*/
public final Rect grow(Num grow)
{
return grow(grow, grow, grow, grow);
}
/**
* Grow to sides
*
@ -598,7 +646,7 @@ public abstract class Rect implements RectBound {
public Num left()
{
return p_l != null ? p_l : (p_l = origin().yn());
return p_l != null ? p_l : (p_l = origin().xn());
}
@ -759,18 +807,40 @@ public abstract class Rect implements RectBound {
}
/**
* Get TiledRect with given number of evenly spaced tiles. Tile indexes are
* one-based by default.
*
* @param horizontal horizontal tile count
* @param vertical vertical tile count
* @return tiled rect
*/
public TiledRect tiles(int horizontal, int vertical)
{
return new TiledRect(this, horizontal, vertical);
}
/**
* Get TiledRect with N columns and 1 row. Column indexes are one-based by
* default.
*
* @param columns number of columns
* @return tiled rect
*/
public TiledRect columns(int columns)
{
return new TiledRect(this, columns, 1);
}
/**
* Get TiledRect with N rows and 1 column. Row indexes are one-based by
* default.
*
* @param rows number of columns
* @return tiled rect
*/
public TiledRect rows(int rows)
{
return new TiledRect(this, 1, rows);

@ -364,16 +364,16 @@ public class RectConst extends Rect {
@Override
public RectConst growTop(double shrink)
public RectConst growUp(double shrink)
{
return super.growTop(shrink).freeze();
return super.growUp(shrink).freeze();
}
@Override
public RectConst growBottom(double shrink)
public RectConst growDown(double shrink)
{
return super.growBottom(shrink).freeze();
return super.growDown(shrink).freeze();
}
@ -413,15 +413,15 @@ public class RectConst extends Rect {
}
public RectConst growTop(NumConst shrink)
public RectConst growUp(NumConst shrink)
{
return super.growTop(shrink).freeze();
return super.growUp(shrink).freeze();
}
public RectConst growBottom(NumConst shrink)
{
return super.growBottom(shrink).freeze();
return super.growDown(shrink).freeze();
}

@ -38,4 +38,11 @@ public class RectDigest {
this.top = rect.top().value();
this.bottom = rect.bottom().value();
}
@Override
public String toString()
{
return String.format("Rect at: (%.1f, %.1f), size: (%.1f, %.1f), bounds: L %.1f R %.1f T %.1f B %.1f", x, y, width, height, left, right, top, bottom);
}
}

@ -3,12 +3,14 @@ package mightypork.utils.math.constraints.rect;
public class RectProxy extends RectAdapter {
private Rect source;
private final Rect source;
public RectProxy(Rect source) {
this.source = source;
}
@Override
protected Rect getSource()
{

@ -5,7 +5,9 @@ import mightypork.utils.math.constraints.num.Num;
/**
* Utility for cutting rect into evenly sized cells.
* Utility for cutting rect into evenly sized cells.<br>
* It's by default one-based, but this can be switched by calling the oneBased()
* and zeroBased() methods.
*
* @author MightyPork
*/
@ -16,6 +18,8 @@ public class TiledRect extends RectProxy {
final private Num perRow;
final private Num perCol;
private int based = 1;
TiledRect(Rect source, int horizontal, int vertical) {
super(source);
@ -28,15 +32,42 @@ public class TiledRect extends RectProxy {
/**
* Get a tile. Tiles count from 0 to n-1
* Set to one-based mode, and return itself (for chaining).
*
* @return this
*/
public TiledRect oneBased()
{
based = 1;
return this;
}
/**
* Set to zero-based mode, and return itself (for chaining).
*
* @return this
*/
public TiledRect zeroBased()
{
based = 0;
return this;
}
/**
* Get a tile.
*
* @param x x position (zero based)
* @param y y position (zero based)
* @param x x position
* @param y y position
* @return tile
* @throws IndexOutOfBoundsException when invalid index is specified.
*/
public Rect tile(int x, int y)
{
x -= based;
y -= based;
if (x >= tilesX || x < 0) {
throw new IndexOutOfBoundsException("X coordinate out fo range.");
}
@ -48,7 +79,7 @@ public class TiledRect extends RectProxy {
final Num leftMove = left().add(perCol.mul(x));
final Num topMove = top().add(perRow.mul(y));
return Rect.make(perCol,perRow).move(leftMove, topMove);
return Rect.make(perCol, perRow).move(leftMove, topMove);
}
@ -56,57 +87,61 @@ public class TiledRect extends RectProxy {
* Get a span (tile spanning across multiple cells)
*
* @param x_from x start position
* @param x_to x end position (included)
* @param y_from y start position
* @param y_to y end position (included)
* @param size_x horizontal size (columns)
* @param size_y vertical size (rows)
* @return tile the tile
* @throws IndexOutOfBoundsException when invalid index is specified.
*/
public Rect span(int x_from, int x_to, int y_from, int y_to)
public Rect span(int x_from, int y_from, int size_x, int size_y)
{
x_from -= based;
y_from -= based;
final int x_to = x_from + size_x;
final int y_to = y_from + size_y;
if (size_x <= 0 || size_y <= 0) {
throw new IndexOutOfBoundsException("Size must be > 0.");
}
if (x_from >= tilesX || x_from < 0 || x_to >= tilesX || x_to < 0) {
throw new IndexOutOfBoundsException("X coordinate(s) out of range.");
}
if (y_from >= tilesY || y_from < 0 || y_to >= tilesY || y_to < 0) {
throw new IndexOutOfBoundsException("Y coordinate(s) out of range.");
}
if (x_from > x_to) {
throw new IndexOutOfBoundsException("x_from > x_to");
}
if (y_from > y_to) {
throw new IndexOutOfBoundsException("y_from > y_to");
}
final Num leftMove = left().add(perCol.mul(x_from));
final Num topMove = top().add(perRow.mul(y_from));
return Rect.make(perCol.mul(x_to - x_from + 1), perRow.mul(x_to - x_from + 1)).move(leftMove, topMove);
return Rect.make(perCol.mul(size_x), perRow.mul(size_y)).move(leftMove, topMove);
}
/**
* Get n-th column (tile n,1)
* Get n-th column
*
* @param n column index (zero based)
* @param n column index
* @return the column tile
* @throws IndexOutOfBoundsException when invalid index is specified.
*/
public Rect column(int n)
{
return tile(n, 1);
return tile(n, based);
}
/**
* Get n-th row (tile 1,n)
* Get n-th row
*
* @param n row index (zero based)
* @param n row index
* @return the row rect
* @throws IndexOutOfBoundsException when invalid index is specified.
*/
public Rect row(int n)
{
return tile(1, n);
return tile(based, n);
}
}

@ -3,7 +3,6 @@ package mightypork.utils.math.constraints.vect;
import mightypork.utils.annotations.DefaultImpl;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.Calc;
import mightypork.utils.math.constraints.VectBound;
import mightypork.utils.math.constraints.num.Num;
import mightypork.utils.math.constraints.num.NumConst;
@ -107,13 +106,6 @@ public abstract class Vect implements VectBound {
private Num p_zc;
@Override
public String toString()
{
return String.format("(%s,%s,%s)", Calc.toString(x()), Calc.toString(y()), Calc.toString(z()));
}
/**
* @return X coordinate
*/
@ -1099,30 +1091,6 @@ public abstract class Vect implements VectBound {
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + Double.valueOf(x()).hashCode();
result = prime * result + Double.valueOf(y()).hashCode();
result = prime * result + Double.valueOf(z()).hashCode();
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Vect)) return false;
final Vect other = (Vect) obj;
return x() == other.x() && y() == other.y() && z() == other.z();
}
/**
* Expand to a rect, with given growth to each side.
*
@ -1151,4 +1119,35 @@ public abstract class Vect implements VectBound {
{
return Rect.make(this, Vect.ZERO).grow(left, right, top, bottom);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + Double.valueOf(x()).hashCode();
result = prime * result + Double.valueOf(y()).hashCode();
result = prime * result + Double.valueOf(z()).hashCode();
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Vect)) return false;
final Vect other = (Vect) obj;
return x() == other.x() && y() == other.y() && z() == other.z();
}
@Override
public String toString()
{
return String.format("(%.1f, %.1f, %.1f)", x(), y(), z());
}
}

Loading…
Cancel
Save