Tweaks to constraint and screen system

This commit is contained in:
ondra
2014-04-08 23:26:54 +02:00
parent f3c6f4f0f5
commit 6460ad9029
32 changed files with 728 additions and 339 deletions
@@ -5,12 +5,12 @@ import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.TxQuad;
public class ImageRenderer extends PluggableRenderer {
public class ImagePainter extends PluggableRenderer {
private TxQuad texture;
public ImageRenderer(TxQuad texture) {
public ImagePainter(TxQuad 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.LinkedList;
import java.util.TreeSet;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.render.Render;
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) {
@@ -18,10 +19,12 @@ public abstract class LayeredScreen extends Screen {
@Override
protected final void renderScreen()
protected void renderScreen()
{
for (final ScreenLayer layer : layers) {
Render.pushState();
layer.render();
Render.popState();
}
}
@@ -31,7 +34,7 @@ public abstract class LayeredScreen extends Screen {
*
* @param layer
*/
protected final void addLayer(ScreenLayer layer)
protected void addLayer(ScreenLayer layer)
{
this.layers.add(layer);
addChildClient(layer);
@@ -43,7 +46,7 @@ public abstract class LayeredScreen extends Screen {
*
* @param layer
*/
protected final void removeLayer(ScreenLayer layer)
protected void removeLayer(ScreenLayer layer)
{
this.layers.remove(layer);
removeChildClient(layer);
@@ -146,7 +146,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override
public final void render()
public void render()
{
if (!isActive()) return;
@@ -154,7 +154,9 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
Render.setupOrtho();
}
Render.pushState();
renderScreen();
Render.popState();
}
@@ -15,7 +15,7 @@ import mightypork.utils.math.coord.Rect;
*
* @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;
@@ -56,4 +56,15 @@ public abstract class ScreenLayer extends AppSubModule implements Renderable, Re
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.timing.FpsMeter;
import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.NumberConstraint;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
@@ -206,4 +207,24 @@ public class DisplaySystem extends RootBusNode implements RectConstraint {
{
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();
}
};
}
+2 -2
View File
@@ -174,8 +174,8 @@ public class Render {
{
pushed++;
if (pushed >= 3) {
Log.w("Suspicious amount of state pushes: " + pushed);
if (pushed >= 20) {
Log.w("Suspicious number of state pushes: " + pushed);
}
// Log.f3("push : "+pushed);
@@ -8,6 +8,7 @@ import java.io.InputStream;
import mightypork.gamecore.loading.BaseDeferredResource;
import mightypork.gamecore.loading.MustLoadInMainThread;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.LoggedName;
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);
int numeric;
int numval;
private FontStyle(int style) {
this.numeric = style;
this.numval = style;
}
}
private SlickFont font = null;
private final double size;
private final FontStyle style;
private final boolean antiAlias;
private final String extraChars;
private FilterMode filter;
/**
@@ -50,7 +51,7 @@ public class DeferredFont extends BaseDeferredResource implements GLFont {
* @param size size (px)
*/
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
*/
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 size size (pt)
* @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);
this.size = size;
this.style = style;
this.antiAlias = antialias;
this.extraChars = extraChars;
this.filter = filter;
}
@Override
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
*/
@Override
public int getHeight()
public int getGlyphHeight()
{
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.io.IOException;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.logging.LoggedName;
@@ -48,10 +49,10 @@ public class DeferredFontNative extends DeferredFont {
* @param extraChars extra chars (0-255 loaded by default)
* @param size size (pt)
* @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) {
super(fontName, extraChars, size, style, antialias);
public DeferredFontNative(String fontName, String extraChars, double size, FontStyle style, FilterMode filter) {
super(fontName, extraChars, size, style, filter);
}
@@ -6,6 +6,8 @@ import java.util.HashMap;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppAdapter;
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 org.newdawn.slick.opengl.Texture;
@@ -19,8 +21,7 @@ import org.newdawn.slick.opengl.Texture;
public class FontBank extends AppAdapter {
private static final GLFont NULL_FONT = new NullFont();
public FontBank(AppAccess app) {
super(app);
}
@@ -7,19 +7,118 @@ import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
/**
* Font renderer
*
* @author MightyPork
*/
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) {
if (font == null) throw new NullPointerException("Font cannot be null.");
this(font, RGB.WHITE);
}
/**
* @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;
}
/**
* 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)
{
Render.pushState();
@@ -32,27 +131,98 @@ public class FontRenderer {
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);
}
/**
* 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)
{
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 double getWidth(String text, 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 getNeededSpace(text, height).x;
draw(text, pos, height, align, this.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
* @param color drawing color
*/
public void draw(String text, Coord pos, double height, Align align, RGB color)
{
return Rect.fromSize(getNeededSpace(text, height)).add(pos);
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);
}
private double getScale(double height)
{
return height / font.getHeight();
}
}
@@ -28,7 +28,7 @@ public interface GLFont {
/**
* @return font height
*/
int getHeight();
int getGlyphHeight();
/**
@@ -36,5 +36,11 @@ public interface GLFont {
* @return space needed
*/
int getWidth(String text);
/**
* @return specified font size
*/
int getSize();
}
@@ -27,7 +27,7 @@ public class NullFont implements GLFont {
@Override
public int getHeight()
public int getGlyphHeight()
{
return 0;
}
@@ -38,5 +38,13 @@ public class NullFont implements GLFont {
{
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 java.awt.Font;
import java.lang.reflect.Field;
import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
@@ -21,18 +23,28 @@ import org.newdawn.slick.TrueTypeFont;
public class SlickFont implements GLFont {
private final TrueTypeFont ttf;
private FilterMode filter;
private int fsize;
/**
* A font with ASCII and extra chars
*
* @param font font to load
* @param antiAlias antialiasing
* @param filtering filtering mode
* @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_T, GL_CLAMP);
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_MIN_FILTER, filter.num);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter.num);
}
@@ -86,12 +98,12 @@ public class SlickFont implements GLFont {
@Override
public Coord getNeededSpace(String text)
{
return new Coord(getWidth(text), getHeight());
return new Coord(getWidth(text), getGlyphHeight());
}
@Override
public int getHeight()
public int getGlyphHeight()
{
return ttf.getHeight();
}
@@ -102,5 +114,12 @@ public class SlickFont implements GLFont {
{
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 {
private Texture backingTexture;
private Filter filter_min = Filter.LINEAR;
private Filter filter_mag = Filter.LINEAR;
private Wrap wrap = Wrap.CLAMP;
private FilterMode filter_min = FilterMode.LINEAR;
private FilterMode filter_mag = FilterMode.LINEAR;
private WrapMode wrap = WrapMode.CLAMP;
public DeferredTexture(String resourcePath) {
@@ -186,7 +186,7 @@ public class DeferredTexture extends BaseDeferredResource implements FilteredTex
@Override
public void setFilter(Filter filterMin, Filter filterMag)
public void setFilter(FilterMode filterMin, FilterMode filterMag)
{
this.filter_min = filterMin;
this.filter_mag = filterMag;
@@ -194,14 +194,14 @@ public class DeferredTexture extends BaseDeferredResource implements FilteredTex
@Override
public void setWrap(Wrap wrapping)
public void setWrap(WrapMode wrapping)
{
this.wrap = wrapping;
}
@Override
public void setFilter(Filter filter)
public void setFilter(FilterMode 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;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.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
*
* @param filterMin downscale 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
*/
void setFilter(Filter filter);
void setFilter(FilterMode filter);
/**
* @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.AppAdapter;
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 org.newdawn.slick.opengl.Texture;
@@ -39,7 +37,7 @@ public class TextureBank extends AppAdapter {
*/
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 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);
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;
}
}