Remade constraint system, improved rendering & font.

v5stable
Ondřej Hruška 11 years ago
parent 8073c5d712
commit 8e8e15355e
  1. 11
      src/mightypork/rogue/App.java
  2. 2
      src/mightypork/rogue/Res.java
  3. 3
      src/mightypork/rogue/bus/ChildClient.java
  4. 7
      src/mightypork/rogue/bus/events/ActionRequest.java
  5. 33
      src/mightypork/rogue/fonts/DeferredFont.java
  6. 2
      src/mightypork/rogue/fonts/DeferredFontNative.java
  7. 58
      src/mightypork/rogue/fonts/FontRenderer.java
  8. 19
      src/mightypork/rogue/fonts/GLFont.java
  9. 14
      src/mightypork/rogue/fonts/NullFont.java
  10. 36
      src/mightypork/rogue/fonts/SlickFont.java
  11. 92
      src/mightypork/rogue/gui/constraints/ElementHolder.java
  12. 15
      src/mightypork/rogue/gui/constraints/PluggableRenderable.java
  13. 21
      src/mightypork/rogue/gui/renderers/ColumnHolder.java
  14. 82
      src/mightypork/rogue/gui/renderers/ElementHolder.java
  15. 30
      src/mightypork/rogue/gui/renderers/ImageRenderer.java
  16. 23
      src/mightypork/rogue/gui/renderers/PluggableRenderable.java
  17. 33
      src/mightypork/rogue/gui/renderers/PluggableRenderer.java
  18. 21
      src/mightypork/rogue/gui/renderers/RowHolder.java
  19. 82
      src/mightypork/rogue/gui/renderers/TextRenderer.java
  20. 6
      src/mightypork/rogue/gui/screens/Screen.java
  21. 4
      src/mightypork/rogue/gui/screens/ScreenLayer.java
  22. 40
      src/mightypork/rogue/gui/screens/test_bouncyboxes/BouncyBox.java
  23. 38
      src/mightypork/rogue/gui/screens/test_bouncyboxes/LayerBouncyBoxes.java
  24. 23
      src/mightypork/rogue/gui/screens/test_bouncyboxes/ScreenTestBouncy.java
  25. 62
      src/mightypork/rogue/gui/screens/test_cat_sound/LayerFlyingCat.java
  26. 7
      src/mightypork/rogue/gui/screens/test_cat_sound/ScreenTestCat.java
  27. 34
      src/mightypork/rogue/gui/screens/test_font/ScreenTestFont.java
  28. 50
      src/mightypork/rogue/input/InputSystem.java
  29. 140
      src/mightypork/rogue/input/Keys.java
  30. 4
      src/mightypork/rogue/loading/AsyncResourceLoader.java
  31. 4
      src/mightypork/rogue/render/DisplaySystem.java
  32. 86
      src/mightypork/rogue/render/Render.java
  33. 2
      src/mightypork/rogue/sounds/DeferredAudio.java
  34. 2
      src/mightypork/rogue/sounds/NullAudio.java
  35. 2
      src/mightypork/rogue/textures/DeferredTexture.java
  36. 2
      src/mightypork/rogue/textures/TextureBank.java
  37. 2
      src/mightypork/rogue/textures/TxQuad.java
  38. 27
      src/mightypork/rogue/util/SlickLogRedirector.java
  39. 14
      src/mightypork/utils/control/bus/EventBus.java
  40. 5
      src/mightypork/utils/control/bus/EventChannel.java
  41. 16
      src/mightypork/utils/logging/Log.java
  42. 18
      src/mightypork/utils/logging/LogInstance.java
  43. 58
      src/mightypork/utils/math/constraints/Constraint.java
  44. 20
      src/mightypork/utils/math/constraints/ConstraintContext.java
  45. 190
      src/mightypork/utils/math/constraints/ConstraintFactory.java
  46. 25
      src/mightypork/utils/math/constraints/ContextAdapter.java
  47. 18
      src/mightypork/utils/math/constraints/NumConstraint.java
  48. 8
      src/mightypork/utils/math/constraints/NumEvaluable.java
  49. 15
      src/mightypork/utils/math/constraints/PluggableContext.java
  50. 22
      src/mightypork/utils/math/constraints/RectConstraint.java
  51. 10
      src/mightypork/utils/math/constraints/RectEvaluable.java
  52. 17
      src/mightypork/utils/math/constraints/SettableContext.java
  53. 2
      src/mightypork/utils/math/coord/Coord.java
  54. 224
      src/mightypork/utils/math/coord/Rect.java

@ -17,6 +17,7 @@ import mightypork.rogue.gui.screens.test_cat_sound.ScreenTestCat;
import mightypork.rogue.gui.screens.test_font.ScreenTestFont; import mightypork.rogue.gui.screens.test_font.ScreenTestFont;
import mightypork.rogue.input.InputSystem; import mightypork.rogue.input.InputSystem;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.input.Keys;
import mightypork.rogue.render.DisplaySystem; import mightypork.rogue.render.DisplaySystem;
import mightypork.rogue.sounds.SoundSystem; import mightypork.rogue.sounds.SoundSystem;
import mightypork.rogue.util.SlickLogRedirector; import mightypork.rogue.util.SlickLogRedirector;
@ -26,8 +27,6 @@ import mightypork.utils.control.interf.Updateable;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.logging.LogInstance; import mightypork.utils.logging.LogInstance;
import org.lwjgl.input.Keyboard;
/** /**
* Main class * Main class
@ -197,7 +196,7 @@ public class App implements AppAccess {
screens.add(new ScreenTestCat(this)); screens.add(new ScreenTestCat(this));
screens.add(new ScreenTestFont(this)); screens.add(new ScreenTestFont(this));
screens.showScreen("test.font"); screens.showScreen("test.bouncy");
} }
@ -228,7 +227,7 @@ public class App implements AppAccess {
Log.f3("Setting up hot keys..."); Log.f3("Setting up hot keys...");
// Go fullscreen // Go fullscreen
input().bindKeyStroke(new KeyStroke(Keyboard.KEY_F11), new Runnable() { input().bindKeyStroke(new KeyStroke(Keys.KEY_F11), new Runnable() {
@Override @Override
public void run() public void run()
@ -238,7 +237,7 @@ public class App implements AppAccess {
}); });
// Take screenshot // Take screenshot
input().bindKeyStroke(new KeyStroke(Keyboard.KEY_F2), new Runnable() { input().bindKeyStroke(new KeyStroke(Keys.KEY_F2), new Runnable() {
@Override @Override
public void run() public void run()
@ -248,7 +247,7 @@ public class App implements AppAccess {
}); });
// Exit // Exit
input().bindKeyStroke(new KeyStroke(Keyboard.KEY_LCONTROL, Keyboard.KEY_Q), new Runnable() { input().bindKeyStroke(new KeyStroke(Keys.KEY_LCONTROL, Keys.KEY_Q), new Runnable() {
@Override @Override
public void run() public void run()

@ -55,7 +55,7 @@ public class Res {
private static void loadFonts() private static void loadFonts()
{ {
fonts.loadFont("PolygonPixel_16", new DeferredFont("/res/font/PolygonPixel5x7Standard.ttf", null, 32, FontStyle.PLAIN, true)); fonts.loadFont("default", new DeferredFont("/res/font/PolygonPixel5x7Standard.ttf", null, 32, FontStyle.PLAIN, true));
} }

@ -10,7 +10,6 @@ import mightypork.rogue.AppAdapter;
import mightypork.utils.control.bus.EventBus; import mightypork.utils.control.bus.EventBus;
import mightypork.utils.control.bus.clients.DelegatingClient; import mightypork.utils.control.bus.clients.DelegatingClient;
import mightypork.utils.control.bus.clients.ToggleableClient; import mightypork.utils.control.bus.clients.ToggleableClient;
import mightypork.utils.logging.Log;
/** /**
@ -60,8 +59,6 @@ public class ChildClient extends AppAdapter implements DelegatingClient, Togglea
{ {
if (bus().isClientValid(client)) { if (bus().isClientValid(client)) {
clients.add(client); clients.add(client);
} else {
Log.w("Client rejected by bus: " + client.getClass().getSimpleName());
} }
} }

@ -37,6 +37,13 @@ public class ActionRequest implements Event<ActionRequest.Listener> {
void requestAction(RequestType request); void requestAction(RequestType request);
} }
@Override
public String toString()
{
return "ActionRequest(" + type + ")";
}
public static enum RequestType public static enum RequestType
{ {
FULLSCREEN, SCREENSHOT, SHUTDOWN; FULLSCREEN, SCREENSHOT, SHUTDOWN;

@ -20,7 +20,7 @@ import mightypork.utils.math.coord.Coord;
* @author MightyPork * @author MightyPork
*/ */
@MustLoadInMainThread @MustLoadInMainThread
@LoggedName(name="Font") @LoggedName(name = "Font")
public class DeferredFont extends BaseDeferredResource implements GLFont { public class DeferredFont extends BaseDeferredResource implements GLFont {
public static enum FontStyle public static enum FontStyle
@ -144,37 +144,6 @@ public class DeferredFont extends BaseDeferredResource implements GLFont {
} }
/**
* Draw string at 0,0
*
* @param str string to draw
* @param color draw color
* @param startIndex first drawn character index
* @param endIndex last drawn character index
*/
@Override
public void draw(String str, RGB color, int startIndex, int endIndex)
{
if (!ensureLoaded()) return;
font.draw(str, color, startIndex, endIndex);
}
/**
* Draw string 0,0
*
* @param str string to draw
*/
@Override
public void draw(String str)
{
if (!ensureLoaded()) return;
font.draw(str);
}
/** /**
* Get size needed to render give string * Get size needed to render give string
* *

@ -13,7 +13,7 @@ import mightypork.utils.logging.LoggedName;
* *
* @author MightyPork * @author MightyPork
*/ */
@LoggedName(name="FontNative") @LoggedName(name = "FontNative")
public class DeferredFontNative extends DeferredFont { public class DeferredFontNative extends DeferredFont {
/** /**

@ -0,0 +1,58 @@
package mightypork.rogue.fonts;
import mightypork.rogue.render.Render;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
public class FontRenderer {
private final GLFont font;
public FontRenderer(GLFont font) {
if (font == null) throw new NullPointerException("Font cannot be null.");
this.font = font;
}
public void draw(String text, Coord pos, double height, RGB color)
{
Render.pushState();
Render.translate(pos.round());
Render.scaleXY(getScale(height));
font.draw(text, color);
Render.popState();
}
public Coord getNeededSpace(String text, double height)
{
return font.getNeededSpace(text).mul(getScale(height));
}
public double getWidth(String text, double height)
{
return getNeededSpace(text, height).x;
}
public Rect getBounds(String text, Coord pos, double height)
{
return Rect.fromSize(getNeededSpace(text, height)).add(pos);
}
private double getScale(double height)
{
return height / font.getHeight();
}
}

@ -16,25 +16,6 @@ public interface GLFont {
void draw(String text, RGB color); void draw(String text, RGB color);
/**
* Draw string at position
*
* @param text string to draw
* @param color draw color
* @param startIndex first drawn character index
* @param endIndex last drawn character index
*/
void draw(String text, RGB color, int startIndex, int endIndex);
/**
* Draw string at position
*
* @param str string to draw
*/
void draw(String str);
/** /**
* Get suize needed to render give string * Get suize needed to render give string
* *

@ -19,20 +19,6 @@ public class NullFont implements GLFont {
} }
@Override
public void draw(String str, RGB color, int startIndex, int endIndex)
{
// nope
}
@Override
public void draw(String str)
{
// nope
}
@Override @Override
public Coord getNeededSpace(String str) public Coord getNeededSpace(String str)
{ {

@ -5,6 +5,7 @@ import static org.lwjgl.opengl.GL11.*;
import java.awt.Font; import java.awt.Font;
import mightypork.rogue.render.Render;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
@ -53,7 +54,7 @@ public class SlickFont implements GLFont {
{ {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} }
@ -67,37 +68,12 @@ public class SlickFont implements GLFont {
@Override @Override
public void draw(String str, RGB color) public void draw(String str, RGB color)
{ {
Render.pushState();
prepareForRender(); prepareForRender();
ttf.drawString(0, 0, str, rgbToSlickColor(color)); ttf.drawString(0, 0, str, rgbToSlickColor(color));
}
Render.popState();
/**
* Draw substring in color
*
* @param str string to draw
* @param color text color
* @param startIndex first char to draw
* @param endIndex last char to draw (INCLUDING!)
*/
@Override
public void draw(String str, RGB color, int startIndex, int endIndex)
{
prepareForRender();
ttf.drawString(0, 0, str, rgbToSlickColor(color), startIndex, endIndex);
}
/**
* Draw in white
*
* @param str chars to draw
*/
@Override
public void draw(String str)
{
prepareForRender();
ttf.drawString(0, 0, str);
} }

@ -1,92 +0,0 @@
package mightypork.rogue.gui.constraints;
import java.util.LinkedList;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.ChildClient;
import mightypork.rogue.render.Renderable;
import mightypork.utils.control.bus.EventBus;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect;
/**
* Bag for {@link PluggableRenderable} elements with constraints.<br>
* Elements are exposed to {@link EventBus}.
*
* @author MightyPork
*/
public class ElementHolder extends ChildClient implements ConstraintContext, PluggableRenderable {
private final LinkedList<PluggableRenderable> elements = new LinkedList<PluggableRenderable>();
private ConstraintContext context;
public ElementHolder(AppAccess app) {
super(app);
}
public ElementHolder(AppAccess app, ConstraintContext context) {
super(app);
this.context = context;
}
@Override
public void setContext(ConstraintContext context)
{
this.context = context;
}
@Override
public void render()
{
for (final Renderable element : elements) {
element.render();
}
}
@Override
public Rect getRect()
{
return context.getRect();
}
/**
* Add element to the holder.
*
* @param elem element; it's context will be set to the constraint.
* @param constraint Constraint to be used for the element. It's context
* will be set to this {@link ElementHolder}
*/
public void add(PluggableRenderable elem, RectConstraint constraint)
{
if (elem == null) return;
constraint.setContext(this);
elem.setContext(constraint);
elements.add(elem);
addChildClient(elem);
}
/**
* Remove element from the holder
*
* @param elem
*/
public void remove(PluggableRenderable elem)
{
if (elem == null) return;
elements.remove(elem);
removeChildClient(elem);
}
}

@ -1,15 +0,0 @@
package mightypork.rogue.gui.constraints;
import mightypork.rogue.render.Renderable;
import mightypork.utils.math.constraints.SettableContext;
/**
* {@link Renderable} with {@link SettableContext}
*
* @author MightyPork
*/
public interface PluggableRenderable extends Renderable, SettableContext {
// methods from both interfaces
}

@ -1,9 +1,9 @@
package mightypork.rogue.gui.constraints; package mightypork.rogue.gui.renderers;
import static mightypork.utils.math.constraints.ConstraintFactory.*; import static mightypork.utils.math.constraints.ConstraintFactory.*;
import mightypork.rogue.AppAccess; import mightypork.rogue.AppAccess;
import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.constraints.RectEvaluable;
public class ColumnHolder extends ElementHolder { public class ColumnHolder extends ElementHolder {
@ -12,7 +12,7 @@ public class ColumnHolder extends ElementHolder {
private int col = 0; private int col = 0;
public ColumnHolder(AppAccess app, ConstraintContext context, int rows) { public ColumnHolder(AppAccess app, RectEvaluable context, int rows) {
super(app, context); super(app, context);
this.cols = rows; this.cols = rows;
} }
@ -29,17 +29,14 @@ public class ColumnHolder extends ElementHolder {
* *
* @param elem * @param elem
*/ */
public void addRow(PluggableRenderable elem)
{
if (elem == null) return;
add(elem, c_column(null, cols, col++));
}
@Override @Override
public void remove(PluggableRenderable elem) public void add(final PluggableRenderable elem)
{ {
throw new UnsupportedOperationException("Can't remove from ColumnHolder."); if (elem == null) return;
elem.setContext(c_column(this, cols, col++));
attach(elem);
} }
} }

@ -0,0 +1,82 @@
package mightypork.rogue.gui.renderers;
import java.util.LinkedList;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.ChildClient;
import mightypork.rogue.render.Renderable;
import mightypork.utils.control.bus.EventBus;
import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.coord.Rect;
/**
* Bag for {@link PluggableRenderer} elements with constraints.<br>
* Elements are exposed to {@link EventBus}.
*
* @author MightyPork
*/
public abstract class ElementHolder extends ChildClient implements PluggableRenderable {
private final LinkedList<PluggableRenderable> elements = new LinkedList<PluggableRenderable>();
private RectEvaluable context;
public ElementHolder(AppAccess app) {
super(app);
}
public ElementHolder(AppAccess app, RectEvaluable context) {
super(app);
setContext(context);
}
@Override
public final void setContext(RectEvaluable context)
{
this.context = context;
}
@Override
public final void render()
{
for (final Renderable element : elements) {
element.render();
}
}
@Override
public final Rect getRect()
{
return context.getRect();
}
/**
* Add element to the holder, setting it's context.<br>
* Element must then be attached using the <code>attach</code> method.
*
* @param elem element
*/
public abstract void add(PluggableRenderable elem);
/**
* Connect to bus and add to element list
*
* @param elem element; it's context will be set to the constraint.
*/
public final void attach(PluggableRenderable elem)
{
if (elem == null) return;
elements.add(elem);
addChildClient(elem);
}
}

@ -0,0 +1,30 @@
package mightypork.rogue.gui.renderers;
import mightypork.rogue.render.Render;
import mightypork.rogue.textures.TxQuad;
public class ImageRenderer extends PluggableRenderer {
private TxQuad texture;
public ImageRenderer(TxQuad texture) {
this.texture = texture;
}
public void setTexture(TxQuad texture)
{
this.texture = texture;
}
@Override
public void render()
{
Render.quadTextured(getRect(), texture);
}
}

@ -0,0 +1,23 @@
package mightypork.rogue.gui.renderers;
import mightypork.rogue.render.Renderable;
import mightypork.utils.math.constraints.PluggableContext;
import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.coord.Rect;
public interface PluggableRenderable extends Renderable, PluggableContext {
@Override
void render();
@Override
Rect getRect();
@Override
void setContext(RectEvaluable rect);
}

@ -0,0 +1,33 @@
package mightypork.rogue.gui.renderers;
import mightypork.rogue.render.Renderable;
import mightypork.utils.math.constraints.ContextAdapter;
import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.coord.Rect;
/**
* {@link Renderable} with pluggable context
*
* @author MightyPork
*/
public abstract class PluggableRenderer extends ContextAdapter implements PluggableRenderable {
@Override
public abstract void render();
@Override
public Rect getRect()
{
return super.getRect();
}
@Override
public void setContext(RectEvaluable rect)
{
super.setContext(rect);
}
}

@ -1,9 +1,9 @@
package mightypork.rogue.gui.constraints; package mightypork.rogue.gui.renderers;
import static mightypork.utils.math.constraints.ConstraintFactory.*; import static mightypork.utils.math.constraints.ConstraintFactory.*;
import mightypork.rogue.AppAccess; import mightypork.rogue.AppAccess;
import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.constraints.RectEvaluable;
public class RowHolder extends ElementHolder { public class RowHolder extends ElementHolder {
@ -12,7 +12,7 @@ public class RowHolder extends ElementHolder {
private int row = 0; private int row = 0;
public RowHolder(AppAccess app, ConstraintContext context, int rows) { public RowHolder(AppAccess app, RectEvaluable context, int rows) {
super(app, context); super(app, context);
this.rows = rows; this.rows = rows;
} }
@ -29,17 +29,14 @@ public class RowHolder extends ElementHolder {
* *
* @param elem * @param elem
*/ */
public void addRow(PluggableRenderable elem)
{
if (elem == null) return;
add(elem, c_row(null, rows, row++));
}
@Override @Override
public void remove(PluggableRenderable elem) public void add(final PluggableRenderable elem)
{ {
throw new UnsupportedOperationException("Can't remove from RowHolder."); if (elem == null) return;
elem.setContext(c_row(this, rows, row++));
attach(elem);
} }
} }

@ -0,0 +1,82 @@
package mightypork.rogue.gui.renderers;
import mightypork.rogue.fonts.FontRenderer;
import mightypork.rogue.fonts.GLFont;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
public class TextRenderer extends PluggableRenderer {
public static enum Align
{
LEFT, CENTER, RIGHT;
}
private FontRenderer font;
private String text;
private RGB color;
private Align align;
public TextRenderer(GLFont font, String text, RGB color, Align align) {
this.font = new FontRenderer(font);
this.text = text;
this.color = color;
this.align = align;
}
public void setFont(FontRenderer font)
{
this.font = font;
}
public void setText(String text)
{
this.text = text;
}
public void setColor(RGB color)
{
this.color = color;
}
public void setAlign(Align align)
{
this.align = align;
}
@Override
public void render()
{
final double h = getRect().getHeight();
final double w = font.getWidth(text, h);
final Coord start;
switch (align) {
case LEFT:
start = getRect().getMin();
break;
case CENTER:
start = getRect().getCenterV1().sub_ip(w / 2D, 0);
break;
case RIGHT:
default:
start = getRect().getX2Y1().sub_ip(w, 0);
break;
}
font.draw(text, start, h, color);
}
}

@ -10,7 +10,7 @@ import mightypork.rogue.input.KeyBindingPool;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.render.Renderable; import mightypork.rogue.render.Renderable;
import mightypork.utils.control.interf.Destroyable; import mightypork.utils.control.interf.Destroyable;
import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
@ -20,7 +20,7 @@ import mightypork.utils.math.coord.Rect;
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class Screen extends ChildClient implements Renderable, Destroyable, KeyBinder, ConstraintContext, ScreenChangeEvent.Listener { public abstract class Screen extends ChildClient implements Renderable, Destroyable, KeyBinder, RectEvaluable, ScreenChangeEvent.Listener {
private final KeyBindingPool keybindings = new KeyBindingPool(); private final KeyBindingPool keybindings = new KeyBindingPool();
@ -70,7 +70,7 @@ public abstract class Screen extends ChildClient implements Renderable, Destroya
active = true; active = true;
needSetupViewport = true; needSetupViewport = true;
onSizeChanged(getRect().size()); onSizeChanged(getRect().getSize());
onScreenEnter(); onScreenEnter();
// enable events // enable events

@ -6,7 +6,7 @@ import mightypork.rogue.input.KeyBinder;
import mightypork.rogue.input.KeyBindingPool; import mightypork.rogue.input.KeyBindingPool;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.render.Renderable; import mightypork.rogue.render.Renderable;
import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
@ -15,7 +15,7 @@ import mightypork.utils.math.coord.Rect;
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class ScreenLayer extends ChildClient implements Renderable, ConstraintContext, KeyBinder { public abstract class ScreenLayer extends ChildClient implements Renderable, RectEvaluable, KeyBinder {
private final Screen screen; private final Screen screen;

@ -5,37 +5,33 @@ import static mightypork.utils.math.constraints.ConstraintFactory.*;
import java.util.Random; import java.util.Random;
import mightypork.rogue.gui.constraints.PluggableRenderable; import mightypork.rogue.gui.renderers.PluggableRenderer;
import mightypork.rogue.render.Render; import mightypork.rogue.render.Render;
import mightypork.utils.control.interf.Updateable; import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.animation.AnimDouble; import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing; import mightypork.utils.math.animation.Easing;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.constraints.NumEvaluable;
import mightypork.utils.math.constraints.NumConstraint; import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect;
public class BouncyBox implements PluggableRenderable, Updateable, ConstraintContext { public class BouncyBox extends PluggableRenderer implements Updateable {
private final Random rand = new Random(); private final Random rand = new Random();
private ConstraintContext context; private final RectEvaluable box;
private final RectConstraint box;
private final AnimDouble pos = new AnimDouble(0, Easing.BOUNCE_OUT); private final AnimDouble pos = new AnimDouble(0, Easing.BOUNCE_OUT);
public BouncyBox() { public BouncyBox() {
// create box // create box
final NumConstraint side = c_height(this); final NumEvaluable side = c_height(this);
RectConstraint abox = c_box_sized(this, side, side); RectEvaluable abox = c_box(this, side, side);
// move // move
final NumConstraint move_length = c_sub(c_width(this), side); final NumEvaluable move_length = c_sub(c_width(this), side);
final NumConstraint offset = c_mul(move_length, c_n(pos)); final NumEvaluable offset = c_mul(move_length, c_n(pos));
abox = c_move(abox, offset, c_n(0)); abox = c_move(abox, offset, c_n(0));
// add padding // add padding
@ -45,13 +41,6 @@ public class BouncyBox implements PluggableRenderable, Updateable, ConstraintCon
} }
@Override
public Rect getRect()
{
return context.getRect();
}
@Override @Override
public void render() public void render()
{ {
@ -59,22 +48,15 @@ public class BouncyBox implements PluggableRenderable, Updateable, ConstraintCon
} }
@Override
public void setContext(ConstraintContext context)
{
this.context = context;
}
public void goLeft() public void goLeft()
{ {
pos.animate(1, 0, 2 + rand.nextDouble() * 1); pos.animate(1, 0, 1 + rand.nextDouble() * 1);
} }
public void goRight() public void goRight()
{ {
pos.animate(0, 1, 2 + rand.nextDouble() * 1); pos.animate(0, 1, 1 + rand.nextDouble() * 1);
} }

@ -6,10 +6,16 @@ import static mightypork.utils.math.constraints.ConstraintFactory.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import mightypork.rogue.gui.constraints.RowHolder; import mightypork.rogue.Res;
import mightypork.rogue.gui.renderers.RowHolder;
import mightypork.rogue.gui.renderers.TextRenderer;
import mightypork.rogue.gui.renderers.TextRenderer.Align;
import mightypork.rogue.gui.screens.Screen; import mightypork.rogue.gui.screens.Screen;
import mightypork.rogue.gui.screens.ScreenLayer; import mightypork.rogue.gui.screens.ScreenLayer;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.input.Keys;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.RectEvaluable;
public class LayerBouncyBoxes extends ScreenLayer { public class LayerBouncyBoxes extends ScreenLayer {
@ -21,17 +27,37 @@ public class LayerBouncyBoxes extends ScreenLayer {
public LayerBouncyBoxes(Screen screen) { public LayerBouncyBoxes(Screen screen) {
super(screen); super(screen);
bindKeyStroke(new KeyStroke(true, Keys.KEY_RIGHT), new Runnable() {
@Override
public void run()
{
goRight();
}
});
bindKeyStroke(new KeyStroke(true, Keys.KEY_LEFT), new Runnable() {
@Override
public void run()
{
goLeft();
}
});
// shrink screen rect by 8% on all sides // shrink screen rect by 8% on all sides
final RectConstraint holder_rect = c_shrink(this, c_percent(c_height(this), c_n(8))); final RectEvaluable holder_rect = c_shrink(this, c_percent(c_height(this), c_n(8)));
addChildClient(layout = new RowHolder(screen, holder_rect, 16)); addChildClient(layout = new RowHolder(screen, holder_rect, 8));
for (int i = 0; i < 16; i++) { for (int i = 0; i < 7; i++) {
final BouncyBox bbr = new BouncyBox(); final BouncyBox bbr = new BouncyBox();
layout.addRow(bbr); layout.add(bbr);
boxes.add(bbr); boxes.add(bbr);
} }
layout.add(new TextRenderer(Res.getFont("default"), "This is a text, yo!", RGB.WHITE, Align.LEFT));
} }

@ -5,8 +5,7 @@ import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.events.ScreenRequestEvent; import mightypork.rogue.bus.events.ScreenRequestEvent;
import mightypork.rogue.gui.screens.LayeredScreen; import mightypork.rogue.gui.screens.LayeredScreen;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.input.Keys;
import org.lwjgl.input.Keyboard;
public class ScreenTestBouncy extends LayeredScreen { public class ScreenTestBouncy extends LayeredScreen {
@ -21,25 +20,7 @@ public class ScreenTestBouncy extends LayeredScreen {
addLayer(layer); addLayer(layer);
bindKeyStroke(new KeyStroke(true, Keyboard.KEY_RIGHT), new Runnable() { bindKeyStroke(new KeyStroke(Keys.KEY_C), new Runnable() {
@Override
public void run()
{
layer.goRight();
}
});
bindKeyStroke(new KeyStroke(true, Keyboard.KEY_LEFT), new Runnable() {
@Override
public void run()
{
layer.goLeft();
}
});
bindKeyStroke(new KeyStroke(Keyboard.KEY_C), new Runnable() {
@Override @Override
public void run() public void run()

@ -7,45 +7,60 @@ import java.util.Random;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.rogue.bus.events.MouseButtonEvent; import mightypork.rogue.bus.events.MouseButtonEvent;
import mightypork.rogue.gui.renderers.ImageRenderer;
import mightypork.rogue.gui.renderers.TextRenderer;
import mightypork.rogue.gui.renderers.TextRenderer.Align;
import mightypork.rogue.gui.screens.Screen; import mightypork.rogue.gui.screens.Screen;
import mightypork.rogue.gui.screens.ScreenLayer; import mightypork.rogue.gui.screens.ScreenLayer;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.render.Render; import mightypork.rogue.input.Keys;
import mightypork.utils.control.interf.Updateable; import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.animation.AnimDouble; import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing; import mightypork.utils.math.animation.Easing;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;
public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButtonEvent.Listener { public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButtonEvent.Listener {
private final RectConstraint kittenbox; private final AnimDouble size = new AnimDouble(400, Easing.SINE_BOTH);
private final AnimDouble xPos = new AnimDouble(200, Easing.ELASTIC_OUT);
private final AnimDouble s = new AnimDouble(400, Easing.SINE_BOTH); private final AnimDouble yPos = new AnimDouble(200, Easing.ELASTIC_OUT);
private final AnimDouble x = new AnimDouble(200, Easing.ELASTIC_OUT);
private final AnimDouble y = new AnimDouble(200, Easing.ELASTIC_OUT);
private final Random rand = new Random(); private final Random rand = new Random();
private final Texture cat_tx = Res.getTexture("test.kitten"); private final ImageRenderer cat;
private final TextRenderer text;
public LayerFlyingCat(Screen screen) { public LayerFlyingCat(Screen screen) {
super(screen); super(screen);
kittenbox = c_move(c_box_sized(this, c_n(s), c_n(s)), c_n(x), c_n(y)); xPos.setTo(disp().getWidth() / 2);
yPos.setTo(disp().getHeight() / 2);
cat = new ImageRenderer(Res.getTxQuad("test.kitten"));
cat.setContext(c_centered(c_box(this, c_n(size), c_n(size)), c_n(xPos), c_n(yPos)));
bindKeyStroke(new KeyStroke(Keyboard.KEY_RETURN), new Runnable() { //@formatter:off
final RectEvaluable flyingFontBox = c_centered(
c_box(this, c_n(0), c_n(64)),
input().c_mouse_x(),
input().c_mouse_y()
);
//@formatter:on
text = new TextRenderer(Res.getFont("default"), "YO", RGB.YELLOW, Align.CENTER);
text.setContext(flyingFontBox);
bindKeyStroke(new KeyStroke(Keys.KEY_RETURN), new Runnable() {
@Override @Override
public void run() public void run()
{ {
x.fadeTo(disp().getWidth() / 2 - s.getTo() / 2, 2); xPos.fadeTo(disp().getWidth() / 2, 2);
y.fadeTo(disp().getHeight() / 2 - s.getTo() / 2, 2); yPos.fadeTo(disp().getHeight() / 2, 2);
} }
}); });
} }
@ -54,9 +69,9 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
@Override @Override
public void update(double delta) public void update(double delta)
{ {
s.update(delta); size.update(delta);
x.update(delta); xPos.update(delta);
y.update(delta); yPos.update(delta);
} }
@ -67,20 +82,19 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
final Coord pos = event.getPos(); final Coord pos = event.getPos();
final double newSize = 200 + rand.nextInt(600);
final double t = 2; final double t = 2;
s.fadeTo(newSize, t / 2D); size.fadeTo(100 + rand.nextInt(700), t / 2D);
x.fadeTo(pos.x - newSize / 2D, t); xPos.fadeTo(pos.x, t);
y.fadeTo(pos.y - newSize / 2D, t); yPos.fadeTo(pos.y, t);
} }
@Override @Override
public void render() public void render()
{ {
Render.quadTextured(kittenbox.getRect(), cat_tx); cat.render();
text.render();
} }
} }

@ -8,8 +8,7 @@ import mightypork.rogue.bus.events.ActionRequest.RequestType;
import mightypork.rogue.bus.events.ScreenRequestEvent; import mightypork.rogue.bus.events.ScreenRequestEvent;
import mightypork.rogue.gui.screens.LayeredScreen; import mightypork.rogue.gui.screens.LayeredScreen;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.input.Keys;
import org.lwjgl.input.Keyboard;
public class ScreenTestCat extends LayeredScreen { public class ScreenTestCat extends LayeredScreen {
@ -22,7 +21,7 @@ public class ScreenTestCat extends LayeredScreen {
addLayer(layer = new LayerFlyingCat(this)); addLayer(layer = new LayerFlyingCat(this));
bindKeyStroke(new KeyStroke(Keyboard.KEY_ESCAPE), new Runnable() { bindKeyStroke(new KeyStroke(Keys.KEY_ESCAPE), new Runnable() {
@Override @Override
public void run() public void run()
@ -32,7 +31,7 @@ public class ScreenTestCat extends LayeredScreen {
} }
}); });
bindKeyStroke(new KeyStroke(Keyboard.KEY_B), new Runnable() { bindKeyStroke(new KeyStroke(Keys.KEY_B), new Runnable() {
@Override @Override
public void run() public void run()

@ -3,16 +3,21 @@ package mightypork.rogue.gui.screens.test_font;
import mightypork.rogue.AppAccess; import mightypork.rogue.AppAccess;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.rogue.fonts.GLFont; import mightypork.rogue.fonts.FontRenderer;
import mightypork.rogue.gui.screens.Screen; import mightypork.rogue.gui.screens.Screen;
import mightypork.rogue.render.Render; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
public class ScreenTestFont extends Screen { public class ScreenTestFont extends Screen {
private final FontRenderer fr;
public ScreenTestFont(AppAccess app) { public ScreenTestFont(AppAccess app) {
super(app); super(app);
fr = new FontRenderer(Res.getFont("default"));
} }
@ -40,15 +45,24 @@ public class ScreenTestFont extends Screen {
@Override @Override
protected void renderScreen() protected void renderScreen()
{ {
final GLFont font = Res.getFont("PolygonPixel_16"); final String str = "O hai";
final double height = getRect().getHeight() / 5D;
final Coord space = fr.getNeededSpace(str, height);
final Coord origin = getRect().getCenter().sub(space.half());
fr.draw(str, origin, height, RGB.GREEN);
final String s = "It works!"; // final GLFont font = Res.getFont("");
final double scale = getRect().height() / 50D; //
Render.pushState(); // final String s = "It works!";
Render.translate(getRect().getCenter().sub(font.getNeededSpace(s).mul(scale).half())); // final double scale = getRect().height() / 50D;
Render.scale(new Coord(scale)); // Render.pushState();
font.draw("It works!"); // Render.translate(getRect().getCenter().sub(font.getNeededSpace(s).mul(scale).half()));
Render.popState(); // Render.scale(new Coord(scale));
// font.draw("It works!");
// Render.popState();
} }

@ -9,6 +9,7 @@ import mightypork.rogue.bus.events.KeyboardEvent;
import mightypork.rogue.bus.events.MouseButtonEvent; import mightypork.rogue.bus.events.MouseButtonEvent;
import mightypork.rogue.bus.events.MouseMotionEvent; import mightypork.rogue.bus.events.MouseMotionEvent;
import mightypork.utils.control.interf.Updateable; import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.constraints.NumEvaluable;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
@ -154,4 +155,53 @@ public class InputSystem extends Subsystem implements Updateable, KeyBinder {
{ {
this.yAxisDown = yAxisDown; this.yAxisDown = yAxisDown;
} }
/**
* Get absolute mouse position
*
* @return mouse position
*/
public Coord getMousePos()
{
final Coord pos = new Coord(Mouse.getX(), Mouse.getY());
flipScrY(pos);
return pos;
}
public void grabMouse(boolean grab)
{
Mouse.setGrabbed(grab);
}
private final NumEvaluable cmousex = new NumEvaluable() {
@Override
public double getValue()
{
return getMousePos().x;
}
};
private final NumEvaluable cmousey = new NumEvaluable() {
@Override
public double getValue()
{
return getMousePos().y;
}
};
public NumEvaluable c_mouse_x()
{
return cmousex;
}
public NumEvaluable c_mouse_y()
{
return cmousey;
}
} }

@ -0,0 +1,140 @@
package mightypork.rogue.input;
import org.lwjgl.input.Keyboard;
/**
* Key constants, from LWJGL {@link Keyboard}
*
* @author MightyPork
*/
public interface Keys {
//@formatter:off
public static final int CHAR_NONE = '\0';
public static final int KEY_NONE = 0x00;
public static final int KEY_ESCAPE = 0x01;
public static final int KEY_1 = 0x02;
public static final int KEY_2 = 0x03;
public static final int KEY_3 = 0x04;
public static final int KEY_4 = 0x05;
public static final int KEY_5 = 0x06;
public static final int KEY_6 = 0x07;
public static final int KEY_7 = 0x08;
public static final int KEY_8 = 0x09;
public static final int KEY_9 = 0x0A;
public static final int KEY_0 = 0x0B;
public static final int KEY_Q = 0x10;
public static final int KEY_W = 0x11;
public static final int KEY_E = 0x12;
public static final int KEY_R = 0x13;
public static final int KEY_T = 0x14;
public static final int KEY_Y = 0x15;
public static final int KEY_U = 0x16;
public static final int KEY_I = 0x17;
public static final int KEY_O = 0x18;
public static final int KEY_P = 0x19;
public static final int KEY_A = 0x1E;
public static final int KEY_S = 0x1F;
public static final int KEY_D = 0x20;
public static final int KEY_F = 0x21;
public static final int KEY_G = 0x22;
public static final int KEY_H = 0x23;
public static final int KEY_J = 0x24;
public static final int KEY_K = 0x25;
public static final int KEY_L = 0x26;
public static final int KEY_Z = 0x2C;
public static final int KEY_X = 0x2D;
public static final int KEY_C = 0x2E;
public static final int KEY_V = 0x2F;
public static final int KEY_B = 0x30;
public static final int KEY_N = 0x31;
public static final int KEY_M = 0x32;
public static final int KEY_MINUS = 0x0C;
public static final int KEY_EQUALS = 0x0D;
public static final int KEY_SLASH = 0x35;
public static final int KEY_BACKSLASH = 0x2B;
public static final int KEY_LBRACKET = 0x1A;
public static final int KEY_RBRACKET = 0x1B;
public static final int KEY_SEMICOLON = 0x27;
public static final int KEY_APOSTROPHE = 0x28;
public static final int KEY_GRAVE = 0x29;
public static final int KEY_COMMA = 0x33;
public static final int KEY_PERIOD = 0x34;
public static final int KEY_SPACE = 0x39;
public static final int KEY_BACKSPACE = 0x0E;
public static final int KEY_TAB = 0x0F;
public static final int KEY_F1 = 0x3B;
public static final int KEY_F2 = 0x3C;
public static final int KEY_F3 = 0x3D;
public static final int KEY_F4 = 0x3E;
public static final int KEY_F5 = 0x3F;
public static final int KEY_F6 = 0x40;
public static final int KEY_F7 = 0x41;
public static final int KEY_F8 = 0x42;
public static final int KEY_F9 = 0x43;
public static final int KEY_F10 = 0x44;
public static final int KEY_F11 = 0x57;
public static final int KEY_F12 = 0x58;
public static final int KEY_F13 = 0x64;
public static final int KEY_F14 = 0x65;
public static final int KEY_F15 = 0x66;
public static final int KEY_CAPSLOCK = 0x3A;
public static final int KEY_SCROLLLOCK = 0x46;
public static final int KEY_NUMLOCK = 0x45;
public static final int KEY_SUBTRACT = 0x4A; /* - on numeric keypad */
public static final int KEY_ADD = 0x4E; /* + on numeric keypad */
public static final int KEY_NUMPAD0 = 0x52;
public static final int KEY_NUMPAD1 = 0x4F;
public static final int KEY_NUMPAD2 = 0x50;
public static final int KEY_NUMPAD3 = 0x51;
public static final int KEY_NUMPAD4 = 0x4B;
public static final int KEY_NUMPAD5 = 0x4C;
public static final int KEY_NUMPAD6 = 0x4D;
public static final int KEY_NUMPAD7 = 0x47;
public static final int KEY_NUMPAD8 = 0x48;
public static final int KEY_NUMPAD9 = 0x49;
public static final int KEY_DECIMAL = 0x53; /* . on numeric keypad */
public static final int KEY_NUMPADENTER = 0x9C; /* Enter on numeric keypad */
public static final int KEY_DIVIDE = 0xB5; /* / on numeric keypad */
public static final int KEY_MULTIPLY = 0x37; /* * on numeric keypad */
public static final int KEY_LCONTROL = 0x1D;
public static final int KEY_RCONTROL = 0x9D;
public static final int KEY_LALT = 0x38;
public static final int KEY_RALT = 0xB8;
public static final int KEY_LSHIFT = 0x2A;
public static final int KEY_RSHIFT = 0x36;
public static final int KEY_LMETA = 0xDB;
public static final int KEY_RMETA = 0xDC;
public static final int KEY_UP = 0xC8; /* UpArrow on arrow keypad */
public static final int KEY_DOWN = 0xD0; /* DownArrow on arrow keypad */
public static final int KEY_LEFT = 0xCB; /* LeftArrow on arrow keypad */
public static final int KEY_RIGHT = 0xCD; /* RightArrow on arrow keypad */
public static final int KEY_HOME = 0xC7; /* Home on arrow keypad */
public static final int KEY_END = 0xCF; /* End on arrow keypad */
public static final int KEY_PAGEUP = 0xC9; /* PgUp on arrow keypad */
public static final int KEY_PAGEDOWN = 0xD1; /* PgDn on arrow keypad */
public static final int KEY_RETURN = 0x1C;
public static final int KEY_PAUSE = 0xC5; /* Pause */
public static final int KEY_INSERT = 0xD2; /* Insert on arrow keypad */
public static final int KEY_DELETE = 0xD3; /* Delete on arrow keypad */
//@formatter:on
}

@ -64,7 +64,7 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L
// textures & fonts needs to be loaded in main thread // textures & fonts needs to be loaded in main thread
if (def.getClass().isAnnotationPresent(MustLoadInMainThread.class)) { if (def.getClass().isAnnotationPresent(MustLoadInMainThread.class)) {
Log.f3("<LOADER> Delegating to main thread:\n "+Log.str(def)); Log.f3("<LOADER> Delegating to main thread:\n " + Log.str(def));
app.bus().send(new MainLoopTaskRequest(new Runnable() { app.bus().send(new MainLoopTaskRequest(new Runnable() {
@ -78,7 +78,7 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L
continue; continue;
} }
Log.f3("<LOADER> Loading async:\n "+Log.str(def)); Log.f3("<LOADER> Loading async:\n " + Log.str(def));
exs.submit(new Runnable() { exs.submit(new Runnable() {

@ -9,7 +9,7 @@ import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.Subsystem; import mightypork.rogue.bus.Subsystem;
import mightypork.rogue.bus.events.ScreenChangeEvent; import mightypork.rogue.bus.events.ScreenChangeEvent;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.ConstraintContext; import mightypork.utils.math.constraints.RectEvaluable;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
@ -19,7 +19,7 @@ import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.DisplayMode;
public class DisplaySystem extends Subsystem implements ConstraintContext { public class DisplaySystem extends Subsystem implements RectEvaluable {
private DisplayMode windowDisplayMode; private DisplayMode windowDisplayMode;
private int targetFps; private int targetFps;

@ -12,7 +12,7 @@ import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
import org.newdawn.slick.opengl.SlickCallable; import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureImpl; import org.newdawn.slick.opengl.TextureImpl;
import org.newdawn.slick.opengl.TextureLoader; import org.newdawn.slick.opengl.TextureLoader;
@ -48,6 +48,11 @@ public class Render {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} }
@ -96,6 +101,17 @@ public class Render {
} }
/**
* Scale by X factor
*
* @param factor scaling factor
*/
public static void scaleXY(double factor)
{
glScaled(factor, factor, 1);
}
/** /**
* Scale by X factor * Scale by X factor
* *
@ -174,13 +190,25 @@ public class Render {
glRotated(angle, vec.x, vec.y, vec.z); glRotated(angle, vec.x, vec.y, vec.z);
} }
private static int pushed = 0;
/** /**
* Store GL state * Store GL state
*/ */
public static void pushState() public static void pushState()
{ {
SlickCallable.enterSafeBlock(); pushed++;
if (pushed >= 3) {
Log.w("Suspicious amount of state pushes: " + pushed);
}
// Log.f3("push : "+pushed);
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glPushClientAttrib(GL11.GL_ALL_CLIENT_ATTRIB_BITS);
GL11.glPushMatrix();
} }
@ -189,7 +217,17 @@ public class Render {
*/ */
public static void popState() public static void popState()
{ {
SlickCallable.leaveSafeBlock(); if (pushed == 0) {
Log.w("Pop without push.");
}
pushed--;
// Log.f3("pop : "+pushed);
GL11.glPopMatrix();
GL11.glPopClientAttrib();
GL11.glPopAttrib();
} }
@ -278,10 +316,10 @@ public class Render {
*/ */
public static void quad(Rect quad) public static void quad(Rect quad)
{ {
final double left = quad.xMin(); final double left = quad.x1();
final double bottom = quad.yMin(); final double bottom = quad.y1();
final double right = quad.xMax(); final double right = quad.x2();
final double top = quad.yMax(); final double top = quad.y2();
// draw with color // draw with color
unbindTexture(); unbindTexture();
@ -318,15 +356,15 @@ public class Render {
*/ */
public static void quadUV_nobound(Rect quad, Rect uvs) public static void quadUV_nobound(Rect quad, Rect uvs)
{ {
final double left = quad.xMin(); final double left = quad.x1();
final double bottom = quad.yMin(); final double bottom = quad.y1();
final double right = quad.xMax(); final double right = quad.x2();
final double top = quad.yMax(); final double top = quad.y2();
final double tleft = uvs.xMin(); final double tleft = uvs.x1();
final double tbottom = uvs.yMin(); final double tbottom = uvs.y1();
final double tright = uvs.xMax(); final double tright = uvs.x2();
final double ttop = uvs.yMax(); final double ttop = uvs.y2();
// quad with texture // quad with texture
glTexCoord2d(tleft, ttop); glTexCoord2d(tleft, ttop);
@ -342,10 +380,10 @@ public class Render {
public static void quadGradH(Rect quad, RGB colorLeft, RGB colorRight) public static void quadGradH(Rect quad, RGB colorLeft, RGB colorRight)
{ {
final double left = quad.xMin(); final double left = quad.x1();
final double bottom = quad.yMin(); final double bottom = quad.y1();
final double right = quad.yMax(); final double right = quad.y2();
final double top = quad.yMax(); final double top = quad.y2();
// draw with color // draw with color
unbindTexture(); unbindTexture();
@ -366,10 +404,10 @@ public class Render {
public static void quadGradV(Rect quad, RGB colorTop, RGB colorBottom) public static void quadGradV(Rect quad, RGB colorTop, RGB colorBottom)
{ {
final double left = quad.xMin(); final double left = quad.x1();
final double bottom = quad.yMin(); final double bottom = quad.y1();
final double right = quad.yMax(); final double right = quad.y2();
final double top = quad.yMax(); final double top = quad.y2();
// draw with color // draw with color
unbindTexture(); unbindTexture();
@ -396,9 +434,11 @@ public class Render {
*/ */
public static void quadTextured(Rect quad, Rect uvs, Texture texture, RGB tint) public static void quadTextured(Rect quad, Rect uvs, Texture texture, RGB tint)
{ {
pushState();
bindTexture(texture); bindTexture(texture);
setColor(tint); setColor(tint);
quadUV(quad, uvs); quadUV(quad, uvs);
popState();
} }

@ -17,7 +17,7 @@ import org.newdawn.slick.openal.SoundStore;
* *
* @author MightyPork * @author MightyPork
*/ */
@LoggedName(name="Audio") @LoggedName(name = "Audio")
public class DeferredAudio extends BaseDeferredResource { public class DeferredAudio extends BaseDeferredResource {
private enum PlayMode private enum PlayMode

@ -11,7 +11,7 @@ import mightypork.utils.logging.LoggedName;
* *
* @author MightyPork * @author MightyPork
*/ */
@LoggedName(name="NullAudio") @LoggedName(name = "NullAudio")
public class NullAudio extends DeferredAudio implements NullResource { public class NullAudio extends DeferredAudio implements NullResource {
public NullAudio() { public NullAudio() {

@ -17,7 +17,7 @@ import org.newdawn.slick.opengl.Texture;
* @author MightyPork * @author MightyPork
*/ */
@MustLoadInMainThread @MustLoadInMainThread
@LoggedName(name="Texture") @LoggedName(name = "Texture")
public class DeferredTexture extends BaseDeferredResource implements FilteredTexture { public class DeferredTexture extends BaseDeferredResource implements FilteredTexture {
private Texture backingTexture; private Texture backingTexture;

@ -62,6 +62,8 @@ public class TextureBank extends AppAdapter {
textures.put(key, tx); textures.put(key, tx);
lastTx = tx; lastTx = tx;
makeQuad(key, Rect.one());
} }

@ -70,7 +70,7 @@ public class TxQuad {
/** /**
* @param tx Texture * @param tx Texture
* @param uvs Rect of texturwe UVs (pixels - from left top) * @param uvs Rect of texture UVs (0-1)
*/ */
public TxQuad(Texture tx, Rect uvs) { public TxQuad(Texture tx, Rect uvs) {
this.tx = tx; this.tx = tx;

@ -1,58 +1,67 @@
package mightypork.rogue.util; package mightypork.rogue.util;
import mightypork.utils.logging.LogInstance; import mightypork.utils.logging.LogInstance;
import org.newdawn.slick.util.LogSystem; import org.newdawn.slick.util.LogSystem;
public class SlickLogRedirector implements LogSystem { public class SlickLogRedirector implements LogSystem {
LogInstance l; LogInstance l;
public SlickLogRedirector(LogInstance log) { public SlickLogRedirector(LogInstance log) {
this.l = log; this.l = log;
} }
@Override @Override
public void error(String msg, Throwable e) public void error(String msg, Throwable e)
{ {
l.e(msg, e); l.e(msg, e);
} }
@Override @Override
public void error(Throwable e) public void error(Throwable e)
{ {
l.e(e); l.e(e);
} }
@Override @Override
public void error(String msg) public void error(String msg)
{ {
l.e(msg); l.e(msg);
} }
@Override @Override
public void warn(String msg) public void warn(String msg)
{ {
l.w(msg); l.w(msg);
} }
@Override @Override
public void warn(String msg, Throwable e) public void warn(String msg, Throwable e)
{ {
l.e(msg, e); l.e(msg, e);
} }
@Override @Override
public void info(String msg) public void info(String msg)
{ {
l.i(msg); l.i(msg);
} }
@Override @Override
public void debug(String msg) public void debug(String msg)
{ {
l.f3(msg); l.f3(msg);
} }
} }

@ -47,9 +47,11 @@ final public class EventBus implements Destroyable {
busThread.start(); busThread.start();
} }
private boolean shallLog(Event<?> event) {
if(!logSending) return false; private boolean shallLog(Event<?> event)
if(event.getClass().isAnnotationPresent(UnloggedEvent.class)) return false; {
if (!logSending) return false;
if (event.getClass().isAnnotationPresent(UnloggedEvent.class)) return false;
return true; return true;
} }
@ -118,7 +120,7 @@ final public class EventBus implements Destroyable {
{ {
assertLive(); assertLive();
DelayedEvent adelay = event.getClass().getAnnotation(DelayedEvent.class); final DelayedEvent adelay = event.getClass().getAnnotation(DelayedEvent.class);
if (adelay != null) { if (adelay != null) {
sendDelayed(event, adelay.delay()); sendDelayed(event, adelay.delay());
return; return;
@ -158,7 +160,7 @@ final public class EventBus implements Destroyable {
final DelayQueueEntry dm = new DelayQueueEntry(delay, event); final DelayQueueEntry dm = new DelayQueueEntry(delay, event);
if (shallLog(event)) Log.f3("<bus> Q " + Log.str(event) + ", t = +" + delay + "s"); if (shallLog(event)) Log.f3("<bus> Qu " + Log.str(event) + ", t = +" + delay + "s");
sendQueue.add(dm); sendQueue.add(dm);
} }
@ -175,7 +177,7 @@ final public class EventBus implements Destroyable {
{ {
assertLive(); assertLive();
if (shallLog(event)) Log.f3("<bus> D " + Log.str(event)); if (shallLog(event)) Log.f3("<bus> Di " + Log.str(event));
dispatch(event); dispatch(event);
} }

@ -52,7 +52,7 @@ final public class EventChannel<EVENT extends Event<CLIENT>, CLIENT> {
public boolean broadcast(Event<?> event, Collection<Object> clients) public boolean broadcast(Event<?> event, Collection<Object> clients)
{ {
if (!canBroadcast(event)) return false; if (!canBroadcast(event)) return false;
return doBroadcast(eventClass.cast(event), clients, new HashSet<Object>()); return doBroadcast(eventClass.cast(event), clients, new HashSet<Object>());
} }
@ -131,8 +131,7 @@ final public class EventChannel<EVENT extends Event<CLIENT>, CLIENT> {
/** /**
* Check if the given event can be broadcasted by this * Check if the given event can be broadcasted by this {@link EventChannel}
* {@link EventChannel}
* *
* @param event event object * @param event event object
* @return can be broadcasted * @return can be broadcasted

@ -199,14 +199,26 @@ public class Log {
public static String str(Class<?> cls) public static String str(Class<?> cls)
{ {
LoggedName ln = cls.getAnnotation(LoggedName.class); final LoggedName ln = cls.getAnnotation(LoggedName.class);
if (ln != null) { if (ln != null) {
return ln.name(); return ln.name();
} }
String name = cls.getName();
String sep = "";
if (name.contains("$")) {
name = name.substring(name.lastIndexOf("$") + 1);
sep = "$";
} else {
name = name.substring(name.lastIndexOf(".") + 1);
sep = ".";
}
final Class<?> enclosing = cls.getEnclosingClass(); final Class<?> enclosing = cls.getEnclosingClass();
return (enclosing == null ? "" : str(enclosing) + ".") + cls.getSimpleName(); return (enclosing == null ? "" : str(enclosing) + sep) + name;
} }

@ -54,7 +54,7 @@ public class LogInstance {
private LogToSysoutMonitor sysoutMonitor; private LogToSysoutMonitor sysoutMonitor;
private long started_ms; private final long started_ms;
/** /**
@ -103,8 +103,8 @@ public class LogInstance {
logger.setUseParentHandlers(false); logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL); logger.setLevel(Level.ALL);
String stamp = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date()); final String stamp = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date());
i("= Logger \""+name+"\" initialized =\n"+stamp); i("= Logger \"" + name + "\" initialized =\n" + stamp);
} }
@ -226,7 +226,7 @@ public class LogInstance {
if (enabled) { if (enabled) {
logger.log(level, msg); logger.log(level, msg);
String fmt = formatMessage(level, msg, null); final String fmt = formatMessage(level, msg, null);
for (final LogMonitor mon : monitors.values()) { for (final LogMonitor mon : monitors.values()) {
mon.onMessageLogged(level, fmt); mon.onMessageLogged(level, fmt);
@ -240,7 +240,7 @@ public class LogInstance {
if (enabled) { if (enabled) {
logger.log(level, msg, t); logger.log(level, msg, t);
String fmt = formatMessage(level, msg, t); final String fmt = formatMessage(level, msg, t);
for (final LogMonitor mon : monitors.values()) { for (final LogMonitor mon : monitors.values()) {
mon.onMessageLogged(level, fmt); mon.onMessageLogged(level, fmt);
@ -375,10 +375,10 @@ public class LogInstance {
message = nl + message.substring(1); message = nl + message.substring(1);
} }
long time_ms = (System.currentTimeMillis()-started_ms); final long time_ms = (System.currentTimeMillis() - started_ms);
double time_s = time_ms / 1000D; final double time_s = time_ms / 1000D;
String time = String.format("%6.2f ", time_s); final String time = String.format("%6.2f ", time_s);
String time_blank = StringUtils.repeat(" ", time.length()); final String time_blank = StringUtils.repeat(" ", time.length());
String prefix = "[ ? ]"; String prefix = "[ ? ]";

@ -1,58 +0,0 @@
package mightypork.utils.math.constraints;
import mightypork.utils.math.coord.Coord;
/**
* A constraint based on a given {@link ConstraintContext}
*
* @author MightyPork
*/
public abstract class Constraint implements SettableContext {
private ConstraintContext context = null;
public Constraint(ConstraintContext context) {
this.context = context;
}
@Override
public void setContext(ConstraintContext context)
{
this.context = context;
}
/**
* @return the context
*/
public ConstraintContext getContext()
{
return context;
}
/**
* @return context rect origin
*/
protected Coord getOrigin()
{
if (context == null) return Coord.zero();
return context.getRect().getOrigin();
}
/**
* @return context rect size
*/
protected Coord getSize()
{
if (context == null) return Coord.zero();
return context.getRect().size();
}
}

@ -1,20 +0,0 @@
package mightypork.utils.math.constraints;
import mightypork.utils.math.coord.Rect;
/**
* Context for constraints, with a bounding {@link Rect}.
*
* @author MightyPork
*/
public interface ConstraintContext {
/**
* Get context boundary
*
* @return bounding rectangle
*/
public Rect getRect();
}

@ -14,9 +14,9 @@ import mightypork.utils.math.coord.Rect;
*/ */
public class ConstraintFactory { public class ConstraintFactory {
public static NumConstraint c_min(final NumConstraint a, final NumConstraint b) public static NumEvaluable c_min(final NumEvaluable a, final NumEvaluable b)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -27,9 +27,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_max(final NumConstraint a, final NumConstraint b) public static NumEvaluable c_max(final NumEvaluable a, final NumEvaluable b)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -40,9 +40,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_abs(final NumConstraint a) public static NumEvaluable c_abs(final NumEvaluable a)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -53,9 +53,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_round(final NumConstraint a) public static NumEvaluable c_round(final NumEvaluable a)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -66,22 +66,22 @@ public class ConstraintFactory {
} }
public static RectConstraint c_round(ConstraintContext context) public static RectEvaluable c_round(final RectEvaluable r)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return getContext().getRect().round(); return r.getRect().round();
} }
}; };
} }
public static NumConstraint c_ceil(final NumConstraint a) public static NumEvaluable c_ceil(final NumEvaluable a)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -92,9 +92,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_floor(final NumConstraint a) public static NumEvaluable c_floor(final NumEvaluable a)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -105,9 +105,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_neg(final NumConstraint a) public static NumEvaluable c_neg(final NumEvaluable a)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -118,9 +118,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_add(final NumConstraint a, final NumConstraint b) public static NumEvaluable c_add(final NumEvaluable a, final NumEvaluable b)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -131,9 +131,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_sub(final NumConstraint a, final NumConstraint b) public static NumEvaluable c_sub(final NumEvaluable a, final NumEvaluable b)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -144,9 +144,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_mul(final NumConstraint a, final NumConstraint b) public static NumEvaluable c_mul(final NumEvaluable a, final NumEvaluable b)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -157,9 +157,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_div(final NumConstraint a, final NumConstraint b) public static NumEvaluable c_div(final NumEvaluable a, final NumEvaluable b)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -170,9 +170,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_percent(final NumConstraint whole, final NumConstraint percent) public static NumEvaluable c_percent(final NumEvaluable whole, final NumEvaluable percent)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -183,9 +183,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_n(final double a) public static NumEvaluable c_n(final double a)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -196,9 +196,9 @@ public class ConstraintFactory {
} }
public static NumConstraint c_n(final AnimDouble a) public static NumEvaluable c_n(final AnimDouble a)
{ {
return new NumConstraint(null) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
@ -209,153 +209,161 @@ public class ConstraintFactory {
} }
public static NumConstraint c_width(final ConstraintContext context) public static NumEvaluable c_width(final RectEvaluable r)
{ {
return new NumConstraint(context) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
{ {
return getSize().x; return r.getRect().getSize().x;
} }
}; };
} }
public static NumConstraint c_height(final ConstraintContext context) public static NumEvaluable c_height(final RectEvaluable r)
{ {
return new NumConstraint(context) { return new NumEvaluable() {
@Override @Override
public double getValue() public double getValue()
{ {
return getSize().y; return r.getRect().getSize().y;
} }
}; };
} }
public static RectConstraint c_row(ConstraintContext context, final int rows, final int index) public static RectEvaluable c_row(final RectEvaluable r, final int rows, final int index)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
final double height = getContext().getRect().size().y; final double height = r.getRect().getSize().y;
final double perRow = height / rows; final double perRow = height / rows;
return Rect.fromSize(getOrigin().add(0, perRow * index), getSize().setY(perRow)); final Coord origin = r.getRect().getOrigin().add(0, perRow * index);
final Coord size = r.getRect().getSize().setY(perRow);
return Rect.fromSize(origin, size);
} }
}; };
} }
public static RectConstraint c_column(ConstraintContext context, final int columns, final int index) public static RectEvaluable c_column(final RectEvaluable r, final int columns, final int index)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
final double width = getContext().getRect().size().x; final double width = r.getRect().getSize().x;
final double perCol = width / columns; final double perCol = width / columns;
return Rect.fromSize(getOrigin().add(perCol * index, 0), getSize().setX(perCol)); final Coord origin = r.getRect().getOrigin().add(perCol * index, 0);
final Coord size = r.getRect().getSize().setX(perCol);
return Rect.fromSize(origin, size);
} }
}; };
} }
public static RectConstraint c_shrink(ConstraintContext context, NumConstraint shrink) public static RectEvaluable c_shrink(RectEvaluable r, NumEvaluable shrink)
{ {
return c_shrink(context, shrink, shrink, shrink, shrink); return c_shrink(r, shrink, shrink, shrink, shrink);
} }
public static RectConstraint c_shrink(ConstraintContext context, NumConstraint horiz, NumConstraint vert) public static RectEvaluable c_shrink(RectEvaluable context, NumEvaluable horiz, NumEvaluable vert)
{ {
return c_shrink(context, horiz, vert, horiz, vert); return c_shrink(context, horiz, vert, horiz, vert);
} }
public static RectConstraint c_shrink(ConstraintContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom) public static RectEvaluable c_shrink(final RectEvaluable r, final NumEvaluable x1, final NumEvaluable y1, final NumEvaluable x2, final NumEvaluable y2)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return getContext().getRect().shrink(left.getValue(), top.getValue(), right.getValue(), bottom.getValue()); return r.getRect().shrink(x1.getValue(), y1.getValue(), x2.getValue(), y2.getValue());
} }
}; };
} }
public static RectConstraint c_center(ConstraintContext context) public static RectEvaluable c_center(final RectEvaluable r)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return Rect.fromSize(getContext().getRect().getCenter(), 0, 0); return Rect.fromSize(r.getRect().getCenter(), 0, 0);
} }
}; };
} }
public static RectConstraint c_grow(ConstraintContext context, NumConstraint grow) public static RectEvaluable c_grow(RectEvaluable r, NumEvaluable grow)
{ {
return c_grow(context, grow, grow, grow, grow); return c_grow(r, grow, grow, grow, grow);
} }
public static RectConstraint c_grow(ConstraintContext context, NumConstraint horiz, NumConstraint vert) public static RectEvaluable c_grow(RectEvaluable r, NumEvaluable horiz, NumEvaluable vert)
{ {
return c_grow(context, horiz, vert, horiz, vert); return c_grow(r, horiz, vert, horiz, vert);
} }
public static RectConstraint c_grow(ConstraintContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom) public static RectEvaluable c_grow(final RectEvaluable r, final NumEvaluable x1, final NumEvaluable y1, final NumEvaluable x2, final NumEvaluable y2)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return getContext().getRect().grow(left.getValue(), top.getValue(), right.getValue(), bottom.getValue()); return r.getRect().grow(x1.getValue(), y1.getValue(), x2.getValue(), y2.getValue());
} }
}; };
} }
public static RectConstraint c_tile(ConstraintContext context, final int rows, final int cols, final int left, final int top) public static RectEvaluable c_tile(final RectEvaluable r, final int rows, final int cols, final int left, final int top)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
final double height = getSize().y; final double height = r.getRect().getSize().y;
final double width = getSize().y; final double width = r.getRect().getSize().y;
final double perRow = height / rows; final double perRow = height / rows;
final double perCol = width / cols; final double perCol = width / cols;
return Rect.fromSize(getOrigin().add(perCol * left, perRow * (rows - top - 1)), perCol, perRow); final Coord origin = r.getRect().getOrigin().add(perCol * left, perRow * (rows - top - 1));
return Rect.fromSize(origin, perCol, perRow);
} }
}; };
} }
public static RectConstraint c_box_sized(ConstraintContext context, final NumConstraint width, final NumConstraint height) public static RectEvaluable c_box(final RectEvaluable r, final NumEvaluable width, final NumEvaluable height)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
final Coord origin = getOrigin(); final Coord origin = r.getRect().getOrigin();
//@formatter:off //@formatter:off
return Rect.fromSize( return Rect.fromSize(
@ -370,19 +378,19 @@ public class ConstraintFactory {
} }
public static RectConstraint c_box_sized(ConstraintContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint width, final NumConstraint height) public static RectEvaluable c_box(final RectEvaluable r, final NumEvaluable x, final NumEvaluable y, final NumEvaluable width, final NumEvaluable height)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
final Coord origin = getOrigin(); final Coord origin = r.getRect().getOrigin();
//@formatter:off //@formatter:off
return Rect.fromSize( return Rect.fromSize(
origin.x + left.getValue(), origin.x + x.getValue(),
origin.y + bottom.getValue(), origin.y + y.getValue(),
width.getValue(), width.getValue(),
height.getValue() height.getValue()
); );
@ -392,36 +400,46 @@ public class ConstraintFactory {
} }
public static RectConstraint c_box_abs(ConstraintContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint right, final NumConstraint top) public static RectEvaluable c_centered(final RectEvaluable r, final NumEvaluable x, final NumEvaluable y)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
final Coord origin = getOrigin(); final Coord size = r.getRect().getSize();
return Rect.fromSize(x.getValue() - size.x / 2D, y.getValue() - size.y / 2D, size.x, size.y);
}
};
}
public static RectEvaluable c_box_abs(final RectEvaluable r, final NumEvaluable x1, final NumEvaluable y1, final NumEvaluable x2, final NumEvaluable y2)
{
return new RectEvaluable() {
@Override
public Rect getRect()
{
final Coord origin = r.getRect().getOrigin();
//@formatter:off //@formatter:off
return new Rect( return new Rect(origin.add(x1.getValue(), y1.getValue()), origin.add(x2.getValue(), y2.getValue()));
origin.x + left.getValue(),
origin.y + bottom.getValue(),
origin.x + right.getValue(),
origin.y + top.getValue()
);
//@formatter:on //@formatter:on
} }
}; };
} }
public static RectConstraint c_move(ConstraintContext context, final NumConstraint x, final NumConstraint y) public static RectEvaluable c_move(final RectEvaluable r, final NumEvaluable x, final NumEvaluable y)
{ {
return new RectConstraint(context) { return new RectEvaluable() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return getContext().getRect().add(x.getValue(), y.getValue()); return r.getRect().add(x.getValue(), y.getValue());
} }
}; };
} }

@ -0,0 +1,25 @@
package mightypork.utils.math.constraints;
import mightypork.utils.math.coord.Rect;
public class ContextAdapter implements PluggableContext {
private RectEvaluable backing = null;
@Override
public void setContext(RectEvaluable rect)
{
this.backing = rect;
}
@Override
public Rect getRect()
{
return backing.getRect();
}
}

@ -1,18 +0,0 @@
package mightypork.utils.math.constraints;
/**
* Constraint that provides size
*
* @author MightyPork
*/
public abstract class NumConstraint extends Constraint {
public NumConstraint(ConstraintContext context) {
super(context);
}
public abstract double getValue();
}

@ -0,0 +1,8 @@
package mightypork.utils.math.constraints;
public interface NumEvaluable {
double getValue();
}

@ -0,0 +1,15 @@
package mightypork.utils.math.constraints;
import mightypork.utils.math.coord.Rect;
public interface PluggableContext extends RectEvaluable {
abstract void setContext(RectEvaluable rect);
@Override
abstract Rect getRect();
}

@ -1,22 +0,0 @@
package mightypork.utils.math.constraints;
import mightypork.utils.math.coord.Rect;
/**
* Constraint that provides a rect ({@link ConstraintContext})
*
* @author MightyPork
*/
public abstract class RectConstraint extends Constraint implements ConstraintContext {
public RectConstraint(ConstraintContext context) {
super(context);
}
@Override
public abstract Rect getRect();
}

@ -0,0 +1,10 @@
package mightypork.utils.math.constraints;
import mightypork.utils.math.coord.Rect;
public interface RectEvaluable {
Rect getRect();
}

@ -1,17 +0,0 @@
package mightypork.utils.math.constraints;
/**
* Can be assigned a context / changed context
*
* @author MightyPork
*/
public interface SettableContext {
/**
* Assign a context
*
* @param context context
*/
void setContext(ConstraintContext context);
}

@ -202,7 +202,7 @@ public class Coord {
*/ */
public boolean isInRect(Rect rect) public boolean isInRect(Rect rect)
{ {
return isInRect(rect.min, rect.max); return isInRect(rect.getMin(), rect.getMax());
} }

@ -41,14 +41,25 @@ public class Rect {
/** /**
* Rectangle from size * Rectangle from size
* *
* @param x min X * @param sizeX size x
* @param y min Y * @param sizeY size y
* @param size rect size * @return rect
* @return the rect
*/ */
public static Rect fromSize(double x, double y, Coord size) public static Rect fromSize(double sizeX, double sizeY)
{ {
return fromSize(x, y, size.x, size.y); return fromSize(0, 0, sizeX, sizeY);
}
/**
* Get rect from size
*
* @param size size
* @return rect
*/
public static Rect fromSize(Coord size)
{
return fromSize(0, 0, size.x, size.y);
} }
@ -67,10 +78,10 @@ public class Rect {
} }
/** Lowest coordinates xy */ /** Lowest coordinates xy */
protected Coord min = new Coord(); private final Coord min = new Coord();
/** Highest coordinates xy */ /** Highest coordinates xy */
protected Coord max = new Coord(); private final Coord max = new Coord();
/** /**
@ -199,28 +210,6 @@ public class Rect {
} }
/**
* Get copy with the same center and height=0
*
* @return line
*/
public Rect getAxisH()
{
return new Rect(getCenterLeft(), getCenterRight());
}
/**
* Get copy with the same center and width=0
*
* @return line
*/
public Rect getAxisV()
{
return new Rect(getCenterBottom(), getCenterTop());
}
/** /**
* Get rect center * Get rect center
* *
@ -237,7 +226,7 @@ public class Rect {
* *
* @return center * @return center
*/ */
public Coord getCenterBottom() public Coord getCenterV1()
{ {
return new Coord((max.x + min.x) / 2D, min.y); return new Coord((max.x + min.x) / 2D, min.y);
} }
@ -248,7 +237,7 @@ public class Rect {
* *
* @return center * @return center
*/ */
public Coord getCenterLeft() public Coord getCenterH1()
{ {
return new Coord(min.x, (max.y + min.y) / 2D); return new Coord(min.x, (max.y + min.y) / 2D);
} }
@ -259,7 +248,7 @@ public class Rect {
* *
* @return center * @return center
*/ */
public Coord getCenterRight() public Coord getCenterH2()
{ {
return new Coord(max.x, (max.y + min.y) / 2D); return new Coord(max.x, (max.y + min.y) / 2D);
} }
@ -270,62 +259,18 @@ public class Rect {
* *
* @return center * @return center
*/ */
public Coord getCenterTop() public Coord getCenterV2()
{ {
return new Coord((max.x + min.x) / 2D, max.y); return new Coord((max.x + min.x) / 2D, max.y);
} }
/**
* Get bottom edge rect
*
* @return line
*/
public Rect getEdgeBottom()
{
return new Rect(getLeftBottom(), getRightBottom());
}
/**
* Get left edge rect
*
* @return line
*/
public Rect getEdgeLeft()
{
return new Rect(getLeftBottom(), getLeftTop());
}
/**
* Get right edge rect
*
* @return line
*/
public Rect getEdgeRight()
{
return new Rect(getRightBottom(), getRightTop());
}
/**
* Get top edge rect
*
* @return line
*/
public Rect getEdgeTop()
{
return new Rect(getLeftTop(), getRightTop());
}
/** /**
* Get left bottom * Get left bottom
* *
* @return center * @return center
*/ */
public Coord getLeftBottom() public Coord getX1Y1()
{ {
return new Coord(min.x, min.y); return new Coord(min.x, min.y);
} }
@ -336,60 +281,64 @@ public class Rect {
* *
* @return center * @return center
*/ */
public Coord getLeftTop() public Coord getX1Y2()
{ {
return new Coord(min.x, max.y); return new Coord(min.x, max.y);
} }
/** /**
* Alias for getX2Y2
*
* @return highest coordinates xy * @return highest coordinates xy
*/ */
public Coord getMax() public Coord getMax()
{ {
return getRightTop(); return getX2Y2();
} }
/** /**
* Alias for getX1Y1
*
* @return lowest coordinates xy * @return lowest coordinates xy
*/ */
public Coord getOrigin() public Coord getMin()
{ {
return getLeftBottom(); return getX1Y1();
} }
/** /**
* Get right bottom * Alias for getX1Y1
* *
* @return center * @return lowest coordinates xy
*/ */
public Coord getRightBottom() public Coord getOrigin()
{ {
return new Coord(max.x, min.y); return getMin();
} }
/** /**
* Get right top * Get right bottom
* *
* @return center * @return center
*/ */
public Coord getRightTop() public Coord getX2Y1()
{ {
return new Coord(max.x, max.y); return new Coord(max.x, min.y);
} }
/** /**
* Get size (width, height) as (x,y) * Get right top
* *
* @return coord of width,height * @return center
*/ */
public Coord size() public Coord getX2Y2()
{ {
return new Coord(max.x - min.x, max.y - min.y); return new Coord(max.x, max.y);
} }
@ -434,8 +383,8 @@ public class Rect {
/** /**
* Shrink to sides in place * Shrink to sides in place
* *
* @param x x to add * @param x horizontal shrink
* @param y y to add * @param y vertical shrink
* @return this * @return this
*/ */
public Rect shrink_ip(double x, double y) public Rect shrink_ip(double x, double y)
@ -449,31 +398,31 @@ public class Rect {
/** /**
* Shrink the rect * Shrink the rect
* *
* @param left left shrink * @param x1 shrink
* @param top top shrink * @param y1 shrink
* @param right right shrink * @param x2 shrink
* @param bottom bottom shrink * @param y2 shrink
* @return changed copy * @return changed copy
*/ */
public Rect shrink(double left, double top, double right, double bottom) public Rect shrink(double x1, double y1, double x2, double y2)
{ {
return copy().shrink_ip(left, top, right, bottom); return copy().shrink_ip(x1, y1, x2, y2);
} }
/** /**
* Shrink the rect in place * Shrink the rect in place
* *
* @param left left shrink * @param x1 shrink
* @param top top shrink * @param y1 shrink
* @param right right shrink * @param x2 shrink
* @param bottom bottom shrink * @param y2 shrink
* @return this * @return this
*/ */
public Rect shrink_ip(double left, double top, double right, double bottom) public Rect shrink_ip(double x1, double y1, double x2, double y2)
{ {
min.add_ip(left, bottom); min.add_ip(x1, y2);
max.add_ip(-right, -top); max.add_ip(-x2, -y1);
return this; return this;
} }
@ -493,8 +442,8 @@ public class Rect {
/** /**
* Grow to sides in copy * Grow to sides in copy
* *
* @param x x to add * @param x horizontal grow
* @param y y to add * @param y vertical grow
* @return changed copy * @return changed copy
*/ */
public Rect grow(double x, double y) public Rect grow(double x, double y)
@ -519,8 +468,8 @@ public class Rect {
/** /**
* Grow to sides in place * Grow to sides in place
* *
* @param x x to add * @param x horizontal grow
* @param y y to add * @param y vertical grow
* @return this * @return this
*/ */
public Rect grow_ip(double x, double y) public Rect grow_ip(double x, double y)
@ -534,31 +483,31 @@ public class Rect {
/** /**
* Grow the rect * Grow the rect
* *
* @param left left growth * @param x1 growth
* @param top top growth * @param y1 growth
* @param right right growth * @param x2 growth
* @param bottom bottom growth * @param y2 growth
* @return changed copy * @return changed copy
*/ */
public Rect grow(double left, double top, double right, double bottom) public Rect grow(double x1, double y1, double x2, double y2)
{ {
return copy().grow_ip(left, top, right, bottom); return copy().grow_ip(x1, y1, x2, y2);
} }
/** /**
* Grow the rect in place * Grow the rect in place
* *
* @param left left growth * @param x1 growth
* @param top top growth * @param y1 growth
* @param right right growth * @param x2 growth
* @param bottom bottom growth * @param y2 growth
* @return this * @return this
*/ */
public Rect grow_ip(double left, double top, double right, double bottom) public Rect grow_ip(double x1, double y1, double x2, double y2)
{ {
min.add_ip(-left, -bottom); min.add_ip(-x1, -y2);
max.add_ip(right, top); max.add_ip(x2, y1);
return this; return this;
} }
@ -773,7 +722,7 @@ public class Rect {
/** /**
* @return lower x * @return lower x
*/ */
public double xMin() public double x1()
{ {
return min.x; return min.x;
} }
@ -782,7 +731,7 @@ public class Rect {
/** /**
* @return upper x * @return upper x
*/ */
public double xMax() public double x2()
{ {
return max.x; return max.x;
} }
@ -791,7 +740,7 @@ public class Rect {
/** /**
* @return lower y * @return lower y
*/ */
public double yMin() public double y1()
{ {
return min.y; return min.y;
} }
@ -800,20 +749,31 @@ public class Rect {
/** /**
* @return upper y * @return upper y
*/ */
public double yMax() public double y2()
{ {
return max.y; return max.y;
} }
public double height() public double getHeight()
{ {
return max.y - min.y; return max.y - min.y;
} }
public double width() public double getWidth()
{ {
return max.x - min.x; return max.x - min.x;
} }
/**
* Get size (width, height) as (x,y)
*
* @return coord of width,height
*/
public Coord getSize()
{
return new Coord(max.x - min.x, max.y - min.y);
}
} }

Loading…
Cancel
Save