Tweaks to constraint and screen system

v5stable
ondra 10 years ago
parent f3c6f4f0f5
commit 6460ad9029
  1. 4
      src/mightypork/gamecore/gui/renderers/ImagePainter.java
  2. 107
      src/mightypork/gamecore/gui/renderers/TextPainter.java
  3. 98
      src/mightypork/gamecore/gui/renderers/TextRenderer.java
  4. 13
      src/mightypork/gamecore/gui/screens/LayeredScreen.java
  5. 4
      src/mightypork/gamecore/gui/screens/Screen.java
  6. 13
      src/mightypork/gamecore/gui/screens/ScreenLayer.java
  7. 21
      src/mightypork/gamecore/render/DisplaySystem.java
  8. 4
      src/mightypork/gamecore/render/Render.java
  9. 33
      src/mightypork/gamecore/render/fonts/DeferredFont.java
  10. 7
      src/mightypork/gamecore/render/fonts/DeferredFontNative.java
  11. 5
      src/mightypork/gamecore/render/fonts/FontBank.java
  12. 200
      src/mightypork/gamecore/render/fonts/FontRenderer.java
  13. 8
      src/mightypork/gamecore/render/fonts/GLFont.java
  14. 10
      src/mightypork/gamecore/render/fonts/NullFont.java
  15. 37
      src/mightypork/gamecore/render/fonts/SlickFont.java
  16. 12
      src/mightypork/gamecore/render/textures/DeferredTexture.java
  17. 15
      src/mightypork/gamecore/render/textures/FilterMode.java
  18. 32
      src/mightypork/gamecore/render/textures/FilteredTexture.java
  19. 6
      src/mightypork/gamecore/render/textures/TextureBank.java
  20. 15
      src/mightypork/gamecore/render/textures/WrapMode.java
  21. 4
      src/mightypork/rogue/Const.java
  22. 18
      src/mightypork/rogue/Res.java
  23. 41
      src/mightypork/rogue/screens/LayerFps.java
  24. 6
      src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java
  25. 25
      src/mightypork/rogue/screens/test_bouncyboxes/LayerBouncyBoxes.java
  26. 7
      src/mightypork/rogue/screens/test_bouncyboxes/ScreenTestBouncy.java
  27. 46
      src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java
  28. 5
      src/mightypork/rogue/screens/test_cat_sound/ScreenTestCat.java
  29. 24
      src/mightypork/rogue/screens/test_font/ScreenTestFont.java
  30. 10
      src/mightypork/utils/math/animation/AnimDouble.java
  31. 159
      src/mightypork/utils/math/constraints/ConstraintFactory.java
  32. 34
      src/mightypork/utils/string/StringProvider.java

@ -5,12 +5,12 @@ import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.TxQuad; import mightypork.gamecore.render.textures.TxQuad;
public class ImageRenderer extends PluggableRenderer { public class ImagePainter extends PluggableRenderer {
private TxQuad texture; private TxQuad texture;
public ImageRenderer(TxQuad texture) { public ImagePainter(TxQuad texture) {
this.texture = texture; this.texture = texture;
} }

@ -0,0 +1,107 @@
package mightypork.gamecore.gui.renderers;
import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.fonts.GLFont;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.utils.math.color.RGB;
import mightypork.utils.string.StringProvider;
import mightypork.utils.string.StringProvider.StringWrapper;
public class TextPainter extends PluggableRenderer {
private final FontRenderer font;
private RGB color;
private Align align;
private StringProvider text;
public TextPainter(GLFont font) {
this(font, Align.LEFT, RGB.WHITE);
}
public TextPainter(GLFont font, Align align, RGB color, String text) {
this(font, align, color, new StringWrapper(text));
}
public TextPainter(GLFont font, Align align, RGB color, StringProvider text) {
this.font = new FontRenderer(font);
this.color = color;
this.align = align;
this.text = text;
}
public TextPainter(GLFont font, Align align, RGB color) {
this(font, align, color, (StringProvider) null);
}
/**
* Use size specified during font init instead of size provided by
* {@link GLFont} instance (measured from tile heights.<br>
* This is better when the font is drawn in original size, but can cause
* weird artifacts if the font is scaled up.
*
* @param enable use it
*/
public void usePtSize(boolean enable) {
font.usePtSize(enable);
}
@Override
public void render()
{
if (getText() == null) return;
font.draw(getText(), getRect(), getAlign(), getColor());
}
public final void setColor(RGB color)
{
this.color = color;
}
public final void setAlign(Align align)
{
this.align = align;
}
public final void setText(String text)
{
this.text = new StringWrapper(text);
}
public final void setText(StringProvider text)
{
this.text = text;
}
protected RGB getColor()
{
return color;
}
protected Align getAlign()
{
return align;
}
protected String getText()
{
return text.getString();
}
}

@ -1,98 +0,0 @@
package mightypork.gamecore.gui.renderers;
import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.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, RGB color, Align align) {
this(font, "MISSINGNO", color, 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;
}
public String getText()
{
return text;
}
@Override
public void render()
{
render(getText());
}
public void render(String text)
{
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);
}
}

@ -2,14 +2,15 @@ package mightypork.gamecore.gui.screens;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.TreeSet;
import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.render.Render;
public abstract class LayeredScreen extends Screen { public abstract class LayeredScreen extends Screen {
private final Collection<ScreenLayer> layers = new LinkedList<ScreenLayer>(); private final Collection<ScreenLayer> layers = new TreeSet<ScreenLayer>();
public LayeredScreen(AppAccess app) { public LayeredScreen(AppAccess app) {
@ -18,10 +19,12 @@ public abstract class LayeredScreen extends Screen {
@Override @Override
protected final void renderScreen() protected void renderScreen()
{ {
for (final ScreenLayer layer : layers) { for (final ScreenLayer layer : layers) {
Render.pushState();
layer.render(); layer.render();
Render.popState();
} }
} }
@ -31,7 +34,7 @@ public abstract class LayeredScreen extends Screen {
* *
* @param layer * @param layer
*/ */
protected final void addLayer(ScreenLayer layer) protected void addLayer(ScreenLayer layer)
{ {
this.layers.add(layer); this.layers.add(layer);
addChildClient(layer); addChildClient(layer);
@ -43,7 +46,7 @@ public abstract class LayeredScreen extends Screen {
* *
* @param layer * @param layer
*/ */
protected final void removeLayer(ScreenLayer layer) protected void removeLayer(ScreenLayer layer)
{ {
this.layers.remove(layer); this.layers.remove(layer);
removeChildClient(layer); removeChildClient(layer);

@ -146,7 +146,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override @Override
public final void render() public void render()
{ {
if (!isActive()) return; if (!isActive()) return;
@ -154,7 +154,9 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
Render.setupOrtho(); Render.setupOrtho();
} }
Render.pushState();
renderScreen(); renderScreen();
Render.popState();
} }

@ -15,7 +15,7 @@ import mightypork.utils.math.coord.Rect;
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class ScreenLayer extends AppSubModule implements Renderable, RectConstraint, KeyBinder { public abstract class ScreenLayer extends AppSubModule implements Comparable<ScreenLayer>, Renderable, RectConstraint, KeyBinder {
private final Screen screen; private final Screen screen;
@ -56,4 +56,15 @@ public abstract class ScreenLayer extends AppSubModule implements Renderable, Re
return screen.getRect(); return screen.getRect();
} }
/**
* @return higher = on top.
*/
public abstract int getPriority();
@Override
public final int compareTo(ScreenLayer o)
{
return Integer.compare(getPriority(), o.getPriority());
}
} }

@ -10,6 +10,7 @@ import mightypork.gamecore.control.bus.clients.RootBusNode;
import mightypork.gamecore.control.bus.events.ScreenChangeEvent; import mightypork.gamecore.control.bus.events.ScreenChangeEvent;
import mightypork.gamecore.control.timing.FpsMeter; import mightypork.gamecore.control.timing.FpsMeter;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.NumberConstraint;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
@ -206,4 +207,24 @@ public class DisplaySystem extends RootBusNode implements RectConstraint {
{ {
return fpsMeter.getFPS(); return fpsMeter.getFPS();
} }
public static final NumberConstraint width = new NumberConstraint() {
@Override
public double getValue()
{
return getWidth();
}
};
public static final NumberConstraint height = new NumberConstraint() {
@Override
public double getValue()
{
return getHeight();
}
};
} }

@ -174,8 +174,8 @@ public class Render {
{ {
pushed++; pushed++;
if (pushed >= 3) { if (pushed >= 20) {
Log.w("Suspicious amount of state pushes: " + pushed); Log.w("Suspicious number of state pushes: " + pushed);
} }
// Log.f3("push : "+pushed); // Log.f3("push : "+pushed);

@ -8,6 +8,7 @@ import java.io.InputStream;
import mightypork.gamecore.loading.BaseDeferredResource; import mightypork.gamecore.loading.BaseDeferredResource;
import mightypork.gamecore.loading.MustLoadInMainThread; import mightypork.gamecore.loading.MustLoadInMainThread;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.files.FileUtils; import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.LoggedName; import mightypork.utils.logging.LoggedName;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
@ -27,19 +28,19 @@ public class DeferredFont extends BaseDeferredResource implements GLFont {
{ {
PLAIN(Font.PLAIN), BOLD(Font.BOLD), ITALIC(Font.ITALIC), BOLD_ITALIC(Font.BOLD + Font.ITALIC); PLAIN(Font.PLAIN), BOLD(Font.BOLD), ITALIC(Font.ITALIC), BOLD_ITALIC(Font.BOLD + Font.ITALIC);
int numeric; int numval;
private FontStyle(int style) { private FontStyle(int style) {
this.numeric = style; this.numval = style;
} }
} }
private SlickFont font = null; private SlickFont font = null;
private final double size; private final double size;
private final FontStyle style; private final FontStyle style;
private final boolean antiAlias;
private final String extraChars; private final String extraChars;
private FilterMode filter;
/** /**
@ -50,7 +51,7 @@ public class DeferredFont extends BaseDeferredResource implements GLFont {
* @param size size (px) * @param size size (px)
*/ */
public DeferredFont(String resourcePath, String extraChars, double size) { public DeferredFont(String resourcePath, String extraChars, double size) {
this(resourcePath, extraChars, size, FontStyle.PLAIN, true); this(resourcePath, extraChars, size, FontStyle.PLAIN, FilterMode.NEAREST);
} }
@ -63,7 +64,7 @@ public class DeferredFont extends BaseDeferredResource implements GLFont {
* @param style font style * @param style font style
*/ */
public DeferredFont(String resourcePath, String extraChars, double size, FontStyle style) { public DeferredFont(String resourcePath, String extraChars, double size, FontStyle style) {
this(resourcePath, extraChars, size, style, true); this(resourcePath, extraChars, size, style, FilterMode.NEAREST);
} }
@ -74,23 +75,23 @@ public class DeferredFont extends BaseDeferredResource implements GLFont {
* @param extraChars extra chars (0-255 loaded by default) * @param extraChars extra chars (0-255 loaded by default)
* @param size size (pt) * @param size size (pt)
* @param style font style * @param style font style
* @param antialias use antialiasing * @param filter gl filtering mode
*/ */
public DeferredFont(String resourcePath, String extraChars, double size, FontStyle style, boolean antialias) { public DeferredFont(String resourcePath, String extraChars, double size, FontStyle style, FilterMode filter) {
super(resourcePath); super(resourcePath);
this.size = size; this.size = size;
this.style = style; this.style = style;
this.antiAlias = antialias;
this.extraChars = extraChars; this.extraChars = extraChars;
this.filter = filter;
} }
@Override @Override
protected final void loadResource(String path) throws FontFormatException, IOException protected final void loadResource(String path) throws FontFormatException, IOException
{ {
final Font awtFont = getAwtFont(path, (float) size, style.numeric); final Font awtFont = getAwtFont(path, (float) size, style.numval);
font = new SlickFont(awtFont, antiAlias, extraChars); font = new SlickFont(awtFont, filter, extraChars);
} }
@ -163,11 +164,19 @@ public class DeferredFont extends BaseDeferredResource implements GLFont {
* @return font height * @return font height
*/ */
@Override @Override
public int getHeight() public int getGlyphHeight()
{ {
if (!ensureLoaded()) return 0; if (!ensureLoaded()) return 0;
return font.getHeight(); return font.getGlyphHeight();
}
@Override
public int getSize()
{
if (!ensureLoaded()) return 0;
return font.getSize();
} }

@ -5,6 +5,7 @@ import java.awt.Font;
import java.awt.FontFormatException; import java.awt.FontFormatException;
import java.io.IOException; import java.io.IOException;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.logging.LoggedName; import mightypork.utils.logging.LoggedName;
@ -48,10 +49,10 @@ public class DeferredFontNative extends DeferredFont {
* @param extraChars extra chars (0-255 loaded by default) * @param extraChars extra chars (0-255 loaded by default)
* @param size size (pt) * @param size size (pt)
* @param style font style * @param style font style
* @param antialias use antialiasing * @param filter GL filtering mode
*/ */
public DeferredFontNative(String fontName, String extraChars, double size, FontStyle style, boolean antialias) { public DeferredFontNative(String fontName, String extraChars, double size, FontStyle style, FilterMode filter) {
super(fontName, extraChars, size, style, antialias); super(fontName, extraChars, size, style, filter);
} }

@ -6,6 +6,8 @@ import java.util.HashMap;
import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppAdapter; import mightypork.gamecore.control.AppAdapter;
import mightypork.gamecore.control.bus.events.ResourceLoadRequest; import mightypork.gamecore.control.bus.events.ResourceLoadRequest;
import mightypork.gamecore.render.fonts.DeferredFont.FontStyle;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
@ -19,8 +21,7 @@ import org.newdawn.slick.opengl.Texture;
public class FontBank extends AppAdapter { public class FontBank extends AppAdapter {
private static final GLFont NULL_FONT = new NullFont(); private static final GLFont NULL_FONT = new NullFont();
public FontBank(AppAccess app) { public FontBank(AppAccess app) {
super(app); super(app);
} }

@ -7,19 +7,118 @@ import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
/**
* Font renderer
*
* @author MightyPork
*/
public class FontRenderer { public class FontRenderer {
private final GLFont font; private GLFont font;
private boolean nativeRes = false;
public static enum Align
{
LEFT, CENTER, RIGHT;
}
private RGB color;
/**
* @param font used font
*/
public FontRenderer(GLFont font) { public FontRenderer(GLFont font) {
this(font, RGB.WHITE);
if (font == null) throw new NullPointerException("Font cannot be null."); }
/**
* @param font used font
* @param color drawing color
*/
public FontRenderer(GLFont font, RGB color) {
this.font = font;
this.color = color;
}
/**
* Use size specified during font init instead of size provided by
* {@link GLFont} instance (measured from tile heights.<br>
* This is better when the font is drawn in original size, but can cause
* weird artifacts if the font is scaled up.
*
* @param use use it
*/
public void usePtSize(boolean use)
{
nativeRes = use;
}
/**
* Get region needed to draw text at size
*
* @param text text to draw
* @param height drawing height
* @return taken space (width, height)
*/
public Coord getNeededSpace(String text, double height)
{
return font.getNeededSpace(text).mul(getScale(height));
}
/**
* Get width needed to draw text at size
*
* @param text text to draw
* @param height drawing height
* @return needed width
*/
public double getWidth(String text, double height)
{
return getNeededSpace(text, height).x;
}
private double getScale(double height)
{
return height / (nativeRes ? font.getSize() : font.getGlyphHeight());
}
/**
* Change drawing font
*
* @param font font to use for drawing
*/
public void setFont(GLFont font)
{
this.font = font; this.font = font;
} }
/**
* Set drawing color
*
* @param color color
*/
public void setColor(RGB color)
{
this.color = color;
}
/**
* Draw on screen
*
* @param text text to draw
* @param pos origin (min coord)
* @param height drawing height
* @param color drawing color
*/
public void draw(String text, Coord pos, double height, RGB color) public void draw(String text, Coord pos, double height, RGB color)
{ {
Render.pushState(); Render.pushState();
@ -32,27 +131,98 @@ public class FontRenderer {
Render.popState(); Render.popState();
} }
public Coord getNeededSpace(String text, double height) /**
* Draw on screen
*
* @param text text to draw
* @param bounds drawing bounds (height for font height, horizontal bounds for align)
* @param align horizontal alignment (with respect to bounds)
*/
public void draw(String text, Rect bounds, Align align)
{ {
return font.getNeededSpace(text).mul(getScale(height)); this.draw(text, bounds, align, this.color);
} }
public double getWidth(String text, double height)
/**
* Draw on screen
*
* @param text text to draw
* @param bounds drawing bounds (height for font height, horizontal bounds for align)
* @param align horizontal alignment (with respect to bounds)
* @param color drawing color
*/
public void draw(String text, Rect bounds, Align align, RGB color)
{ {
return getNeededSpace(text, height).x; Coord start;
switch (align) {
case LEFT:
start = bounds.getX1Y1();
break;
case CENTER:
start = bounds.getCenterV1();
break;
case RIGHT:
default:
start = bounds.getX2Y1();
break;
}
draw(text, start, bounds.getHeight(), align, color);
} }
public Rect getBounds(String text, Coord pos, double height) /**
* Draw on screen
*
* @param text text to draw
* @param pos origin (min coord)
* @param height drawing height
* @param align horizontal alignment
*/
public void draw(String text, Coord pos, double height, Align align)
{ {
return Rect.fromSize(getNeededSpace(text, height)).add(pos); draw(text, pos, height, align, this.color);
} }
private double getScale(double height) /**
* Draw on screen
*
* @param text text to draw
* @param pos origin (min coord)
* @param height drawing height
* @param align horizontal alignment
* @param color drawing color
*/
public void draw(String text, Coord pos, double height, Align align, RGB color)
{ {
return height / font.getHeight();
final double w = getWidth(text, height);
final Coord start;
switch (align) {
case LEFT:
start = pos;
break;
case CENTER:
start = pos.sub(w / 2D, 0);
break;
case RIGHT:
default:
start = pos.sub(w, 0);
break;
}
draw(text, start, height, color);
} }
} }

@ -28,7 +28,7 @@ public interface GLFont {
/** /**
* @return font height * @return font height
*/ */
int getHeight(); int getGlyphHeight();
/** /**
@ -36,5 +36,11 @@ public interface GLFont {
* @return space needed * @return space needed
*/ */
int getWidth(String text); int getWidth(String text);
/**
* @return specified font size
*/
int getSize();
} }

@ -27,7 +27,7 @@ public class NullFont implements GLFont {
@Override @Override
public int getHeight() public int getGlyphHeight()
{ {
return 0; return 0;
} }
@ -38,5 +38,13 @@ public class NullFont implements GLFont {
{ {
return 0; return 0;
} }
@Override
public int getSize()
{
// TODO Auto-generated method stub
return 0;
}
} }

@ -4,8 +4,10 @@ package mightypork.gamecore.render.fonts;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.*;
import java.awt.Font; import java.awt.Font;
import java.lang.reflect.Field;
import mightypork.gamecore.render.Render; import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
@ -21,18 +23,28 @@ import org.newdawn.slick.TrueTypeFont;
public class SlickFont implements GLFont { public class SlickFont implements GLFont {
private final TrueTypeFont ttf; private final TrueTypeFont ttf;
private FilterMode filter;
private int fsize;
/** /**
* A font with ASCII and extra chars * A font with ASCII and extra chars
* *
* @param font font to load * @param font font to load
* @param antiAlias antialiasing * @param filtering filtering mode
* @param extraChars extra chars to load * @param extraChars extra chars to load
*/ */
public SlickFont(Font font, boolean antiAlias, String extraChars) { public SlickFont(Font font, FilterMode filtering, String extraChars) {
ttf = new TrueTypeFont(font, antiAlias, stripASCII(extraChars)); this.filter = filtering;
this.fsize = font.getSize();
ttf = new TrueTypeFont(font, true, stripASCII(extraChars));
}
public void setFiltering(FilterMode filter)
{
this.filter = filter;
} }
@ -50,12 +62,12 @@ public class SlickFont implements GLFont {
} }
private static void prepareForRender() private void prepareForRender()
{ {
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_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter.num);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter.num);
} }
@ -86,12 +98,12 @@ public class SlickFont implements GLFont {
@Override @Override
public Coord getNeededSpace(String text) public Coord getNeededSpace(String text)
{ {
return new Coord(getWidth(text), getHeight()); return new Coord(getWidth(text), getGlyphHeight());
} }
@Override @Override
public int getHeight() public int getGlyphHeight()
{ {
return ttf.getHeight(); return ttf.getHeight();
} }
@ -102,5 +114,12 @@ public class SlickFont implements GLFont {
{ {
return ttf.getWidth(text); return ttf.getWidth(text);
} }
@Override
public int getSize()
{
return fsize;
}
} }

@ -21,9 +21,9 @@ import org.newdawn.slick.opengl.Texture;
public class DeferredTexture extends BaseDeferredResource implements FilteredTexture { public class DeferredTexture extends BaseDeferredResource implements FilteredTexture {
private Texture backingTexture; private Texture backingTexture;
private Filter filter_min = Filter.LINEAR; private FilterMode filter_min = FilterMode.LINEAR;
private Filter filter_mag = Filter.LINEAR; private FilterMode filter_mag = FilterMode.LINEAR;
private Wrap wrap = Wrap.CLAMP; private WrapMode wrap = WrapMode.CLAMP;
public DeferredTexture(String resourcePath) { public DeferredTexture(String resourcePath) {
@ -186,7 +186,7 @@ public class DeferredTexture extends BaseDeferredResource implements FilteredTex
@Override @Override
public void setFilter(Filter filterMin, Filter filterMag) public void setFilter(FilterMode filterMin, FilterMode filterMag)
{ {
this.filter_min = filterMin; this.filter_min = filterMin;
this.filter_mag = filterMag; this.filter_mag = filterMag;
@ -194,14 +194,14 @@ public class DeferredTexture extends BaseDeferredResource implements FilteredTex
@Override @Override
public void setWrap(Wrap wrapping) public void setWrap(WrapMode wrapping)
{ {
this.wrap = wrapping; this.wrap = wrapping;
} }
@Override @Override
public void setFilter(Filter filter) public void setFilter(FilterMode filter)
{ {
setFilter(filter, filter); setFilter(filter, filter);
} }

@ -0,0 +1,15 @@
package mightypork.gamecore.render.textures;
import org.lwjgl.opengl.GL11;
public enum FilterMode
{
LINEAR(GL11.GL_LINEAR), NEAREST(GL11.GL_NEAREST);
public final int num;
private FilterMode(int gl) {
this.num = gl;
}
}

@ -1,44 +1,18 @@
package mightypork.gamecore.render.textures; package mightypork.gamecore.render.textures;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
public interface FilteredTexture extends Texture { public interface FilteredTexture extends Texture {
public static enum Filter
{
LINEAR(GL11.GL_LINEAR), NEAREST(GL11.GL_NEAREST);
public final int num;
private Filter(int gl) {
this.num = gl;
}
}
public static enum Wrap
{
CLAMP(GL11.GL_CLAMP), REPEAT(GL11.GL_REPEAT);
public final int num;
private Wrap(int gl) {
this.num = gl;
}
}
/** /**
* Set filter for scaling * Set filter for scaling
* *
* @param filterMin downscale filter * @param filterMin downscale filter
* @param filterMag upscale filter * @param filterMag upscale filter
*/ */
void setFilter(Filter filterMin, Filter filterMag); void setFilter(FilterMode filterMin, FilterMode filterMag);
/** /**
@ -46,11 +20,11 @@ public interface FilteredTexture extends Texture {
* *
* @param filter filter * @param filter filter
*/ */
void setFilter(Filter filter); void setFilter(FilterMode filter);
/** /**
* @param wrapping wrap mode * @param wrapping wrap mode
*/ */
void setWrap(Wrap wrapping); void setWrap(WrapMode wrapping);
} }

@ -6,8 +6,6 @@ import java.util.HashMap;
import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppAdapter; import mightypork.gamecore.control.AppAdapter;
import mightypork.gamecore.control.bus.events.ResourceLoadRequest; import mightypork.gamecore.control.bus.events.ResourceLoadRequest;
import mightypork.gamecore.render.textures.FilteredTexture.Filter;
import mightypork.gamecore.render.textures.FilteredTexture.Wrap;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
@ -39,7 +37,7 @@ public class TextureBank extends AppAdapter {
*/ */
public void loadTexture(String key, String resourcePath) public void loadTexture(String key, String resourcePath)
{ {
loadTexture(key, resourcePath, Filter.LINEAR, Filter.NEAREST, Wrap.CLAMP); loadTexture(key, resourcePath, FilterMode.LINEAR, FilterMode.NEAREST, WrapMode.CLAMP);
} }
@ -52,7 +50,7 @@ public class TextureBank extends AppAdapter {
* @param filter_mag mag filter (when rendered larger) * @param filter_mag mag filter (when rendered larger)
* @param wrap texture wrapping * @param wrap texture wrapping
*/ */
public void loadTexture(String key, String resourcePath, Filter filter_min, Filter filter_mag, Wrap wrap) public void loadTexture(String key, String resourcePath, FilterMode filter_min, FilterMode filter_mag, WrapMode wrap)
{ {
final DeferredTexture tx = new DeferredTexture(resourcePath); final DeferredTexture tx = new DeferredTexture(resourcePath);
tx.setFilter(filter_min, filter_mag); tx.setFilter(filter_min, filter_mag);

@ -0,0 +1,15 @@
package mightypork.gamecore.render.textures;
import org.lwjgl.opengl.GL11;
public enum WrapMode
{
CLAMP(GL11.GL_CLAMP), REPEAT(GL11.GL_REPEAT);
public final int num;
private WrapMode(int gl) {
this.num = gl;
}
}

@ -18,6 +18,6 @@ public class Const {
public static final int FPS_RENDER = 100; // max public static final int FPS_RENDER = 100; // max
// INITIAL WINDOW SIZE // INITIAL WINDOW SIZE
public static final int WINDOW_W = 1024; public static final int WINDOW_W = 800;
public static final int WINDOW_H = 768; public static final int WINDOW_H = 600;
} }

@ -10,10 +10,10 @@ import mightypork.gamecore.render.fonts.DeferredFont;
import mightypork.gamecore.render.fonts.DeferredFont.FontStyle; import mightypork.gamecore.render.fonts.DeferredFont.FontStyle;
import mightypork.gamecore.render.fonts.FontBank; import mightypork.gamecore.render.fonts.FontBank;
import mightypork.gamecore.render.fonts.GLFont; import mightypork.gamecore.render.fonts.GLFont;
import mightypork.gamecore.render.textures.FilteredTexture.Filter; import mightypork.gamecore.render.textures.FilterMode;
import mightypork.gamecore.render.textures.FilteredTexture.Wrap;
import mightypork.gamecore.render.textures.TextureBank; import mightypork.gamecore.render.textures.TextureBank;
import mightypork.gamecore.render.textures.TxQuad; import mightypork.gamecore.render.textures.TxQuad;
import mightypork.gamecore.render.textures.WrapMode;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
@ -56,13 +56,23 @@ public class Res {
private static void loadFonts() private static void loadFonts()
{ {
fonts.loadFont("default", new DeferredFont("/res/font/PolygonPixel5x7Standard.ttf", null, 32, FontStyle.PLAIN, true)); //@formatter:off
fonts.loadFont(
"default",
new DeferredFont("/res/font/PolygonPixel5x7Standard.ttf",
null,
16,
FontStyle.PLAIN,
FilterMode.NEAREST
)
);
//@formatter:on
} }
private static void loadTextures() private static void loadTextures()
{ {
textures.loadTexture("test.kitten", "/res/img/kitten.png", Filter.LINEAR, Filter.NEAREST, Wrap.CLAMP); textures.loadTexture("test.kitten", "/res/img/kitten.png", FilterMode.LINEAR, FilterMode.NEAREST, WrapMode.CLAMP);
} }

@ -0,0 +1,41 @@
package mightypork.rogue.screens;
import mightypork.gamecore.gui.renderers.TextPainter;
import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.gui.screens.ScreenLayer;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.rogue.Res;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
public class LayerFps extends ScreenLayer {
TextPainter tp;
private FontRenderer fr;
public LayerFps(Screen screen) {
super(screen);
fr = new FontRenderer(Res.getFont("default"), RGB.WHITE);
}
@Override
public void render()
{
Coord pos = new Coord(DisplaySystem.getWidth() - 8, 8);
fr.draw(getDisplay().getFps() + " fps", pos, 32, Align.RIGHT);
}
@Override
public int getPriority()
{
return Integer.MAX_VALUE;
}
}

@ -31,11 +31,11 @@ public class BouncyBox extends PluggableRenderer implements Updateable {
// move // move
final NumberConstraint move_length = c_sub(c_width(this), side); final NumberConstraint move_length = c_sub(c_width(this), side);
final NumberConstraint offset = c_mul(move_length, c_n(pos)); final NumberConstraint offset = c_mul(move_length, pos);
abox = c_move(abox, offset, c_n(0)); abox = c_move(abox, offset, 0);
// add padding // add padding
abox = c_shrink(abox, c_percent(side, c_n(10))); abox = c_shrink(abox, c_percent(side, 10));
box = abox; box = abox;
} }

@ -7,15 +7,16 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import mightypork.gamecore.gui.renderers.RowHolder; import mightypork.gamecore.gui.renderers.RowHolder;
import mightypork.gamecore.gui.renderers.TextRenderer; import mightypork.gamecore.gui.renderers.TextPainter;
import mightypork.gamecore.gui.renderers.TextRenderer.Align;
import mightypork.gamecore.gui.screens.Screen; import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.gui.screens.ScreenLayer; import mightypork.gamecore.gui.screens.ScreenLayer;
import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys; import mightypork.gamecore.input.Keys;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.string.StringProvider;
public class LayerBouncyBoxes extends ScreenLayer { public class LayerBouncyBoxes extends ScreenLayer {
@ -46,24 +47,26 @@ public class LayerBouncyBoxes extends ScreenLayer {
}); });
// 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 RectConstraint holder_rect = c_shrink(this, c_percent(c_height(this), 4));
addChildClient(layout = new RowHolder(screen, holder_rect, 8)); addChildClient(layout = new RowHolder(screen, holder_rect, 11));
for (int i = 0; i < 7; i++) { for (int i = 0; i <= 9; i++) {
final BouncyBox bbr = new BouncyBox(); final BouncyBox bbr = new BouncyBox();
layout.add(bbr); layout.add(bbr);
boxes.add(bbr); boxes.add(bbr);
} }
layout.add(new TextRenderer(Res.getFont("default"), RGB.WHITE, Align.LEFT) { StringProvider sp = new StringProvider() {
@Override @Override
public String getText() public String getString()
{ {
return "Running at " + getDisplay().getFps() + " fps!"; return "Running at " + getDisplay().getFps() + " fps!";
} }
}); };
layout.add(new TextPainter(Res.getFont("default"), Align.LEFT, RGB.WHITE, sp));
} }
@ -88,5 +91,11 @@ public class LayerBouncyBoxes extends ScreenLayer {
bbr.goRight(); bbr.goRight();
} }
} }
@Override
public int getPriority()
{
return 0;
}
} }

@ -6,6 +6,7 @@ import mightypork.gamecore.control.bus.events.ScreenRequestEvent;
import mightypork.gamecore.gui.screens.LayeredScreen; import mightypork.gamecore.gui.screens.LayeredScreen;
import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys; import mightypork.gamecore.input.Keys;
import mightypork.rogue.screens.LayerFps;
public class ScreenTestBouncy extends LayeredScreen { public class ScreenTestBouncy extends LayeredScreen {
@ -17,8 +18,10 @@ public class ScreenTestBouncy extends LayeredScreen {
super(app); super(app);
layer = new LayerBouncyBoxes(this); layer = new LayerBouncyBoxes(this);
addLayer(layer); addLayer(new LayerFps(this));
addLayer(layer);
bindKeyStroke(new KeyStroke(Keys.KEY_C), new Runnable() { bindKeyStroke(new KeyStroke(Keys.KEY_C), new Runnable() {

@ -7,21 +7,23 @@ import java.util.Random;
import mightypork.gamecore.control.bus.events.MouseButtonEvent; import mightypork.gamecore.control.bus.events.MouseButtonEvent;
import mightypork.gamecore.control.interf.Updateable; import mightypork.gamecore.control.interf.Updateable;
import mightypork.gamecore.gui.renderers.ImageRenderer; import mightypork.gamecore.gui.renderers.ImagePainter;
import mightypork.gamecore.gui.renderers.TextRenderer; import mightypork.gamecore.gui.renderers.TextPainter;
import mightypork.gamecore.gui.renderers.TextRenderer.Align;
import mightypork.gamecore.gui.screens.Screen; import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.gui.screens.ScreenLayer; import mightypork.gamecore.gui.screens.ScreenLayer;
import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.input.KeyStroke; import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys; import mightypork.gamecore.input.Keys;
import mightypork.gamecore.render.DisplaySystem; import mightypork.gamecore.render.DisplaySystem;
import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.rogue.Res; import mightypork.rogue.Res;
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.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import mightypork.utils.string.StringProvider;
public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButtonEvent.Listener { public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButtonEvent.Listener {
@ -32,8 +34,8 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
private final Random rand = new Random(); private final Random rand = new Random();
private final ImageRenderer cat; private final ImagePainter cat;
private final TextRenderer text; private final TextPainter text;
public LayerFlyingCat(Screen screen) { public LayerFlyingCat(Screen screen) {
@ -42,19 +44,23 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
xPos.setTo(DisplaySystem.getWidth() / 2); xPos.setTo(DisplaySystem.getWidth() / 2);
yPos.setTo(DisplaySystem.getHeight() / 2); yPos.setTo(DisplaySystem.getHeight() / 2);
cat = new ImageRenderer(Res.getTxQuad("test.kitten")); cat = new ImagePainter(Res.getTxQuad("test.kitten"));
cat.setContext(c_centered(c_box(this, c_n(size), c_n(size)), c_n(xPos), c_n(yPos))); cat.setContext(c_centered(c_box(this, size, size), xPos, yPos));
//@formatter:off final RectConstraint fpsbox = c_centered(c_box(this, 0, 64), InputSystem.mouseX, InputSystem.mouseY);
final RectConstraint flyingFontBox = c_centered(
c_box(this, c_n(0), c_n(64)),
InputSystem.mouseX,
InputSystem.mouseY
);
//@formatter:on
text = new TextRenderer(Res.getFont("default"), RGB.YELLOW, Align.CENTER); final StringProvider sp = new StringProvider() {
text.setContext(flyingFontBox);
@Override
public String getString()
{
return getDisplay().getFps() + " fps";
}
};
text = new TextPainter(Res.getFont("default"), Align.CENTER, RGB.YELLOW, sp);
text.setContext(fpsbox);
bindKeyStroke(new KeyStroke(Keys.KEY_RETURN), new Runnable() { bindKeyStroke(new KeyStroke(Keys.KEY_RETURN), new Runnable() {
@ -97,7 +103,13 @@ public class LayerFlyingCat extends ScreenLayer implements Updateable, MouseButt
public void render() public void render()
{ {
cat.render(); cat.render();
text.render(getDisplay().getFps()+" fps"); text.render();
}
@Override
public int getPriority()
{
return 0;
} }
} }

@ -9,17 +9,18 @@ import mightypork.gamecore.input.Keys;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.rogue.events.ActionRequest; import mightypork.rogue.events.ActionRequest;
import mightypork.rogue.events.ActionRequest.RequestType; import mightypork.rogue.events.ActionRequest.RequestType;
import mightypork.rogue.screens.LayerFps;
public class ScreenTestCat extends LayeredScreen { public class ScreenTestCat extends LayeredScreen {
LayerFlyingCat layer;
public ScreenTestCat(AppAccess app) { public ScreenTestCat(AppAccess app) {
super(app); super(app);
addLayer(layer = new LayerFlyingCat(this)); addLayer(new LayerFps(this));
addLayer(new LayerFlyingCat(this));
bindKeyStroke(new KeyStroke(Keys.KEY_ESCAPE), new Runnable() { bindKeyStroke(new KeyStroke(Keys.KEY_ESCAPE), new Runnable() {

@ -2,36 +2,36 @@ package mightypork.rogue.screens.test_font;
import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.AppAccess;
import static mightypork.utils.math.constraints.ConstraintFactory.*;
import mightypork.gamecore.gui.renderers.TextPainter;
import mightypork.gamecore.gui.screens.Screen; import mightypork.gamecore.gui.screens.Screen;
import mightypork.gamecore.render.fonts.FontRenderer; import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.constraints.RectConstraint;
public class ScreenTestFont extends Screen { public class ScreenTestFont extends Screen {
private final FontRenderer fr; private final TextPainter tp;
public ScreenTestFont(AppAccess app) { public ScreenTestFont(AppAccess app) {
super(app); super(app);
fr = new FontRenderer(Res.getFont("default")); tp = new TextPainter(Res.getFont("default"), Align.CENTER, RGB.GREEN);
tp.setText("Hello World!");
RectConstraint strbox = c_grow(c_center(this), 0, c_div(c_height(this), 10));
tp.setContext(strbox);
} }
@Override @Override
protected void renderScreen() protected void renderScreen()
{ {
final String str = "O hai"; tp.render();
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);
} }

@ -4,6 +4,7 @@ package mightypork.utils.math.animation;
import mightypork.gamecore.control.interf.Updateable; import mightypork.gamecore.control.interf.Updateable;
import mightypork.gamecore.control.timing.Pauseable; import mightypork.gamecore.control.timing.Pauseable;
import mightypork.utils.math.Calc; import mightypork.utils.math.Calc;
import mightypork.utils.math.constraints.NumberConstraint;
/** /**
@ -11,7 +12,7 @@ import mightypork.utils.math.Calc;
* *
* @author MightyPork * @author MightyPork
*/ */
public class AnimDouble implements Updateable, Pauseable { public class AnimDouble implements Updateable, Pauseable, NumberConstraint {
/** target double */ /** target double */
protected double to = 0; protected double to = 0;
@ -318,4 +319,11 @@ public class AnimDouble implements Updateable, Pauseable {
{ {
return paused; return paused;
} }
@Override
public double getValue()
{
return now();
}
} }

@ -1,7 +1,6 @@
package mightypork.utils.math.constraints; package mightypork.utils.math.constraints;
import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
@ -14,27 +13,26 @@ import mightypork.utils.math.coord.Rect;
*/ */
public class ConstraintFactory { public class ConstraintFactory {
public static NumberConstraint c_min(final NumberConstraint a, final NumberConstraint b) public static NumberConstraint c_min(final Object a, final Object b)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@Override @Override
public double getValue() public double getValue()
{ {
return Math.min(a.getValue(), b.getValue()); return Math.min(n(a).getValue(), n(b).getValue());
} }
}; };
} }
public static NumberConstraint c_max(final Object a, final Object b)
public static NumberConstraint c_max(final NumberConstraint a, final NumberConstraint b)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@Override @Override
public double getValue() public double getValue()
{ {
return Math.max(a.getValue(), b.getValue()); return Math.max(n(a).getValue(), n(b).getValue());
} }
}; };
} }
@ -53,6 +51,19 @@ public class ConstraintFactory {
} }
public static NumberConstraint c_half(final NumberConstraint a)
{
return new NumberConstraint() {
@Override
public double getValue()
{
return a.getValue()/2;
}
};
}
public static NumberConstraint c_round(final NumberConstraint a) public static NumberConstraint c_round(final NumberConstraint a)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@ -117,93 +128,64 @@ public class ConstraintFactory {
}; };
} }
public static NumberConstraint c_add(final Object a, final Object b)
public static NumberConstraint c_add(final NumberConstraint a, final NumberConstraint b)
{
return new NumberConstraint() {
@Override
public double getValue()
{
return a.getValue() + b.getValue();
}
};
}
public static NumberConstraint c_sub(final NumberConstraint a, final NumberConstraint b)
{
return new NumberConstraint() {
@Override
public double getValue()
{
return a.getValue() - b.getValue();
}
};
}
public static NumberConstraint c_mul(final NumberConstraint a, final NumberConstraint b)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@Override @Override
public double getValue() public double getValue()
{ {
return a.getValue() * b.getValue(); return n(a).getValue() + n(b).getValue();
} }
}; };
} }
public static NumberConstraint c_sub(final Object a, final Object b)
public static NumberConstraint c_div(final NumberConstraint a, final NumberConstraint b)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@Override @Override
public double getValue() public double getValue()
{ {
return a.getValue() / b.getValue(); return n(a).getValue() - n(b).getValue();
} }
}; };
} }
public static NumberConstraint c_mul(final Object a, final Object b)
public static NumberConstraint c_percent(final NumberConstraint whole, final NumberConstraint percent)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@Override @Override
public double getValue() public double getValue()
{ {
return whole.getValue() * (percent.getValue() / 100); return n(a).getValue() * n(b).getValue();
} }
}; };
} }
public static NumberConstraint c_div(final Object a, final Object b)
public static NumberConstraint c_n(final double a)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@Override @Override
public double getValue() public double getValue()
{ {
return a; return n(a).getValue() / n(b).getValue();
} }
}; };
} }
public static NumberConstraint c_percent(final Object whole, final Object percent)
public static NumberConstraint c_n(final AnimDouble a)
{ {
return new NumberConstraint() { return new NumberConstraint() {
@Override @Override
public double getValue() public double getValue()
{ {
return a.now(); return n(whole).getValue() * (n(percent).getValue() / 100);
} }
}; };
} }
@ -273,26 +255,27 @@ public class ConstraintFactory {
} }
public static RectConstraint c_shrink(RectConstraint r, NumberConstraint shrink) public static RectConstraint c_shrink(RectConstraint r, Object shrink)
{ {
return c_shrink(r, shrink, shrink, shrink, shrink); NumberConstraint n = n(shrink);
return c_shrink(r, n, n, n, n);
} }
public static RectConstraint c_shrink(RectConstraint context, NumberConstraint horiz, NumberConstraint vert) public static RectConstraint c_shrink(RectConstraint context, Object horiz, Object vert)
{ {
return c_shrink(context, horiz, vert, horiz, vert); return c_shrink(context, horiz, vert, horiz, vert);
} }
public static RectConstraint c_shrink(final RectConstraint r, final NumberConstraint x1, final NumberConstraint y1, final NumberConstraint x2, final NumberConstraint y2) public static RectConstraint c_shrink(final RectConstraint r, final Object x1, final Object y1, final Object x2, final Object y2)
{ {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return r.getRect().shrink(x1.getValue(), y1.getValue(), x2.getValue(), y2.getValue()); return r.getRect().shrink(n(x1).getValue(), n(y1).getValue(), n(x2).getValue(), n(y2).getValue());
} }
}; };
} }
@ -310,27 +293,27 @@ public class ConstraintFactory {
}; };
} }
public static RectConstraint c_grow(RectConstraint r, Object grow)
public static RectConstraint c_grow(RectConstraint r, NumberConstraint grow)
{ {
return c_grow(r, grow, grow, grow, grow); NumberConstraint n = n(grow);
return c_grow(r, n, n, n, n);
} }
public static RectConstraint c_grow(RectConstraint r, NumberConstraint horiz, NumberConstraint vert) public static RectConstraint c_grow(RectConstraint r, Object horiz, Object vert)
{ {
return c_grow(r, horiz, vert, horiz, vert); return c_grow(r, horiz, vert, horiz, vert);
} }
public static RectConstraint c_grow(final RectConstraint r, final NumberConstraint x1, final NumberConstraint y1, final NumberConstraint x2, final NumberConstraint y2) public static RectConstraint c_grow(final RectConstraint r, final Object x1, final Object y1, final Object x2, final Object y2)
{ {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return r.getRect().grow(x1.getValue(), y1.getValue(), x2.getValue(), y2.getValue()); return r.getRect().grow(n(x1).getValue(), n(y1).getValue(), n(x2).getValue(), n(y2).getValue());
} }
}; };
} }
@ -355,8 +338,7 @@ public class ConstraintFactory {
}; };
} }
public static RectConstraint c_box(final RectConstraint r, final Object width, final Object height)
public static RectConstraint c_box(final RectConstraint r, final NumberConstraint width, final NumberConstraint height)
{ {
return new RectConstraint() { return new RectConstraint() {
@ -369,8 +351,8 @@ public class ConstraintFactory {
return Rect.fromSize( return Rect.fromSize(
origin.x, origin.x,
origin.y, origin.y,
width.getValue(), n(width).getValue(),
height.getValue() n(height).getValue()
); );
//@formatter:on //@formatter:on
} }
@ -378,7 +360,7 @@ public class ConstraintFactory {
} }
public static RectConstraint c_box(final RectConstraint r, final NumberConstraint x, final NumberConstraint y, final NumberConstraint width, final NumberConstraint height) public static RectConstraint c_box(final RectConstraint r, final Object x, final Object y, final Object width, final Object height)
{ {
return new RectConstraint() { return new RectConstraint() {
@ -389,10 +371,10 @@ public class ConstraintFactory {
//@formatter:off //@formatter:off
return Rect.fromSize( return Rect.fromSize(
origin.x + x.getValue(), origin.x + n(x).getValue(),
origin.y + y.getValue(), origin.y + n(y).getValue(),
width.getValue(), n(width).getValue(),
height.getValue() n(height).getValue()
); );
//@formatter:on //@formatter:on
} }
@ -400,7 +382,14 @@ public class ConstraintFactory {
} }
public static RectConstraint c_centered(final RectConstraint r, final NumberConstraint x, final NumberConstraint y) /**
* Center rect around given coords
* @param r rect
* @param x
* @param y
* @return centered
*/
public static RectConstraint c_centered(final RectConstraint r, final Object x, final Object y)
{ {
return new RectConstraint() { return new RectConstraint() {
@ -409,13 +398,13 @@ public class ConstraintFactory {
{ {
final Coord size = r.getRect().getSize(); final Coord size = r.getRect().getSize();
return Rect.fromSize(x.getValue() - size.x / 2D, y.getValue() - size.y / 2D, size.x, size.y); return Rect.fromSize(n(x).getValue() - size.x / 2D, n(y).getValue() - size.y / 2D, size.x, size.y);
} }
}; };
} }
public static RectConstraint c_box_abs(final RectConstraint r, final NumberConstraint x1, final NumberConstraint y1, final NumberConstraint x2, final NumberConstraint y2) public static RectConstraint c_box_abs(final RectConstraint r, final Object x1, final Object y1, final Object x2, final Object y2)
{ {
return new RectConstraint() { return new RectConstraint() {
@ -425,23 +414,43 @@ public class ConstraintFactory {
final Coord origin = r.getRect().getOrigin(); final Coord origin = r.getRect().getOrigin();
//@formatter:off //@formatter:off
return new Rect(origin.add(x1.getValue(), y1.getValue()), origin.add(x2.getValue(), y2.getValue())); return new Rect(origin.add(n(x1).getValue(), n(y1).getValue()), origin.add(n(x2).getValue(), n(y2).getValue()));
//@formatter:on //@formatter:on
} }
}; };
} }
public static RectConstraint c_move(final RectConstraint r, final Object x, final Object y)
public static RectConstraint c_move(final RectConstraint r, final NumberConstraint x, final NumberConstraint y)
{ {
return new RectConstraint() { return new RectConstraint() {
@Override @Override
public Rect getRect() public Rect getRect()
{ {
return r.getRect().add(x.getValue(), y.getValue()); return r.getRect().add(n(x).getValue(), n(y).getValue());
}
};
}
/**
* Convert {@link Double} to {@link NumberConstraint} if needed
* @param o unknown numeric value
* @return converted
*/
public static NumberConstraint n(final Object o) {
if(o instanceof NumberConstraint) return (NumberConstraint) o;
if(o instanceof Number) return new NumberConstraint() {
@Override
public double getValue()
{
return ((Number) o).doubleValue();
} }
}; };
throw new IllegalArgumentException("Invalid numeric type.");
} }
} }

@ -0,0 +1,34 @@
package mightypork.utils.string;
/**
* Can be used for dynamic string generating
*
* @author MightyPork
*/
public interface StringProvider {
String getString();
/**
* String provider with constant string
*
* @author MightyPork
*/
public static class StringWrapper implements StringProvider {
private final String value;
public StringWrapper(String value) {
this.value = value;
}
@Override
public String getString()
{
return value;
}
}
}
Loading…
Cancel
Save