diff --git a/engine/src/core/com/jme3/font/BitmapText.java b/engine/src/core/com/jme3/font/BitmapText.java index b84e2baaa..231fcdb43 100644 --- a/engine/src/core/com/jme3/font/BitmapText.java +++ b/engine/src/core/com/jme3/font/BitmapText.java @@ -361,17 +361,17 @@ public class BitmapText extends Node { private void assemble() { // first generate quadlist letters.update(); - for (int i = 0; i < textPages.length; i++) { textPages[i].assemble(letters); } needRefresh = false; } - public void render(RenderManager rm) { + public void render(RenderManager rm, ColorRGBA color) { for (BitmapTextPage page : textPages) { Material mat = page.getMaterial(); - mat.setTexture("Texture", page.getTexture()); + mat.setTexture("ColorMap", page.getTexture()); + mat.setColor("Color", color); mat.render(page, rm); } } diff --git a/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java b/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java index 1f36a9775..851754886 100644 --- a/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java +++ b/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java @@ -31,6 +31,7 @@ */ package com.jme3.niftygui; +import com.jme3.font.BitmapFont; import com.jme3.font.BitmapText; import com.jme3.material.Material; import com.jme3.material.RenderState; @@ -63,13 +64,11 @@ public class RenderDeviceJme implements RenderDevice { private NiftyJmeDisplay display; private RenderManager rm; private Renderer r; - private HashMap textCacheLastFrame = new HashMap(); - private HashMap textCacheCurrentFrame = new HashMap(); + private HashMap textCacheLastFrame = new HashMap(); + private HashMap textCacheCurrentFrame = new HashMap(); private final Quad quad = new Quad(1, -1, true); private final Geometry quadGeom = new Geometry("nifty-quad", quad); - private final Material niftyMat; - private final Material niftyQuadMat; - private final Material niftyQuadGradMat; + private final Material unshadedMat; private boolean clipWasSet = false; private BlendMode blendMode = null; private VertexBuffer quadDefaultTC = quad.getBuffer(Type.TexCoord); @@ -78,6 +77,36 @@ public class RenderDeviceJme implements RenderDevice { private Matrix4f tempMat = new Matrix4f(); private ColorRGBA tempColor = new ColorRGBA(); + private static class CachedTextKey { + + BitmapFont font; + String text; +// ColorRGBA color; + + public CachedTextKey(BitmapFont font, String text/*, ColorRGBA color*/) { + this.font = font; + this.text = text; +// this.color = color; + } + + @Override + public boolean equals(Object other) { + CachedTextKey otherKey = (CachedTextKey) other; + return font.equals(otherKey.font) && + text.equals(otherKey.text)/* && + color.equals(otherKey.color)*/; + } + + @Override + public int hashCode() { + int hash = 5; + hash = 53 * hash + font.hashCode(); + hash = 53 * hash + text.hashCode(); +// hash = 53 * hash + color.hashCode(); + return hash; + } + } + public RenderDeviceJme(NiftyJmeDisplay display) { this.display = display; @@ -89,17 +118,10 @@ public class RenderDeviceJme implements RenderDevice { quadModTC.setUsage(Usage.Stream); - //Color + texture color material for text and images - niftyMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyTex.j3md"); - niftyMat.getAdditionalRenderState().setDepthTest(false); - //Color material for uniform colored quads - niftyQuadMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuad.j3md"); - niftyQuadMat.getAdditionalRenderState().setDepthTest(false); - - //vertex color only for gradient quads (although i didn't find a way in nifty to make a gradient using vertex color) - niftyQuadGradMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuadGrad.j3md"); - niftyQuadGradMat.getAdditionalRenderState().setDepthTest(false); - + // GUI material + unshadedMat = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); + unshadedMat.getAdditionalRenderState().setDepthTest(false); + unshadedMat.getAdditionalRenderState().setDepthWrite(false); } public void setResourceLoader(NiftyResourceLoader niftyResourceLoader) { @@ -113,7 +135,6 @@ public class RenderDeviceJme implements RenderDevice { // TODO: Cursor support public MouseCursor createMouseCursor(String str, int x, int y) { return new MouseCursor() { - public void dispose() { } }; @@ -126,6 +147,7 @@ public class RenderDeviceJme implements RenderDevice { } public RenderImage createImage(String filename, boolean linear) { + //System.out.println("createImage(" + filename + ", " + linear + ")"); return new RenderImageJme(filename, linear, display); } @@ -137,12 +159,10 @@ public class RenderDeviceJme implements RenderDevice { } public void endFrame() { - HashMap temp = textCacheLastFrame; + HashMap temp = textCacheLastFrame; textCacheLastFrame = textCacheCurrentFrame; textCacheCurrentFrame = temp; textCacheCurrentFrame.clear(); - -// System.exit(1); } public int getWidth() { @@ -186,36 +206,7 @@ public class RenderDeviceJme implements RenderDevice { private ColorRGBA convertColor(Color inColor, ColorRGBA outColor) { return outColor.set(inColor.getRed(), inColor.getGreen(), inColor.getBlue(), inColor.getAlpha()); } - -// private void setColor(Color color) { -// ByteBuffer buf = (ByteBuffer) quadColor.getData(); -// buf.rewind(); -// -// int color2 = convertColor(color); -// buf.putInt(color2); -// buf.putInt(color2); -// buf.putInt(color2); -// buf.putInt(color2); -// -// buf.flip(); -// quadColor.updateData(buf); -// } - /** - * - * @param font - * @param str - * @param x - * @param y - * @param color - * @param size - * @deprecated use renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY) instead - */ - @Deprecated - public void renderFont(RenderFont font, String str, int x, int y, Color color, float size) { - renderFont(font, str, x, y, color, size, size); - } - @Override public void renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY) { if (str.length() == 0) { @@ -228,7 +219,8 @@ public class RenderDeviceJme implements RenderDevice { RenderFontJme jmeFont = (RenderFontJme) font; - String key = font + str + color.getColorString(); + ColorRGBA colorRgba = convertColor(color, tempColor); + CachedTextKey key = new CachedTextKey(jmeFont.getFont(), str/*, colorRgba*/); BitmapText text = textCacheLastFrame.get(key); if (text == null) { text = jmeFont.createText(); @@ -237,19 +229,24 @@ public class RenderDeviceJme implements RenderDevice { } textCacheCurrentFrame.put(key, text); - niftyMat.setColor("Color", convertColor(color, tempColor)); - niftyMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); -// niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); - text.setMaterial(niftyMat); +// unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend()); +// unshadedMat.setBoolean("VertexColor", true); +// unshadedMat.setColor("Color", convertColor(color, tempColor)); +// text.setMaterial(unshadedMat); + + float width = text.getLineWidth(); +// float height = text.getLineHeight(); + float x0 = x + 0.5f * width * (1f - sizeX); + float y0 = y; // + 0.5f * height * (1f - sizeY); tempMat.loadIdentity(); - tempMat.setTranslation(x, getHeight() - y, 0); + tempMat.setTranslation(x0, getHeight() - y0, 0); tempMat.setScale(sizeX, sizeY, 0); - - rm.setWorldMatrix(tempMat); - text.render(rm); -// System.out.println("renderFont"); + rm.setWorldMatrix(tempMat); + text.render(rm, colorRgba); + +// System.out.format("renderFont(%s, %s, %d, %d, %s, %f, %f)\n", jmeFont.getFont(), str, x, y, color.toString(), sizeX, sizeY); } public void renderImage(RenderImage image, int x, int y, int w, int h, @@ -259,10 +256,10 @@ public class RenderDeviceJme implements RenderDevice { RenderImageJme jmeImage = (RenderImageJme) image; Texture2D texture = jmeImage.getTexture(); - niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyMat.setColor("Color", convertColor(color, tempColor)); - niftyMat.setTexture("Texture", texture); - //setColor(color); + unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend()); + unshadedMat.setColor("Color", convertColor(color, tempColor)); + unshadedMat.setTexture("ColorMap", texture); + unshadedMat.setBoolean("VertexColor", false); float imageWidth = jmeImage.getWidth(); float imageHeight = jmeImage.getHeight(); @@ -295,9 +292,11 @@ public class RenderDeviceJme implements RenderDevice { tempMat.setScale(w * scale, h * scale, 0); rm.setWorldMatrix(tempMat); - niftyMat.render(quadGeom, rm); -// -// System.out.println("renderImage (Sub)"); + unshadedMat.render(quadGeom, rm); + + //System.out.format("renderImage2(%s, %d, %d, %d, %d, %d, %d, %d, %d, %s, %f, %d, %d)\n", texture.getKey().toString(), + // x, y, w, h, srcX, srcY, srcW, srcH, + // color.toString(), scale, centerX, centerY); } public void renderImage(RenderImage image, int x, int y, int width, int height, @@ -305,10 +304,10 @@ public class RenderDeviceJme implements RenderDevice { RenderImageJme jmeImage = (RenderImageJme) image; - niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyMat.setColor("Color", convertColor(color, tempColor)); - niftyMat.setTexture("Texture", jmeImage.getTexture()); - //setColor(color); + unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend()); + unshadedMat.setColor("Color", convertColor(color, tempColor)); + unshadedMat.setTexture("ColorMap", jmeImage.getTexture()); + unshadedMat.setBoolean("VertexColor", false); quad.clearBuffer(Type.TexCoord); quad.setBuffer(quadDefaultTC); @@ -321,24 +320,25 @@ public class RenderDeviceJme implements RenderDevice { tempMat.setScale(width * imageScale, height * imageScale, 0); rm.setWorldMatrix(tempMat); - niftyMat.render(quadGeom, rm); -// -// System.out.println("renderImage"); + unshadedMat.render(quadGeom, rm); + + //System.out.format("renderImage1(%s, %d, %d, %d, %d, %s, %f)\n", jmeImage.getTexture().getKey().toString(), x, y, width, height, color.toString(), imageScale); } public void renderQuad(int x, int y, int width, int height, Color color) { - if (color.getAlpha() > 0) { - niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyQuadMat.setColor("Color", convertColor(color, tempColor)); - - tempMat.loadIdentity(); - tempMat.setTranslation(x, getHeight() - y, 0); - tempMat.setScale(width, height, 0); - - rm.setWorldMatrix(tempMat); - niftyQuadMat.render(quadGeom, rm); - } -// System.out.println("renderQuad (Solid)"); + unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend()); + unshadedMat.setColor("Color", convertColor(color, tempColor)); + unshadedMat.setTexture("ColorMap", null); + unshadedMat.setBoolean("VertexColor", false); + + tempMat.loadIdentity(); + tempMat.setTranslation(x, getHeight() - y, 0); + tempMat.setScale(width, height, 0); + + rm.setWorldMatrix(tempMat); + unshadedMat.render(quadGeom, rm); + + //System.out.format("renderQuad1(%d, %d, %d, %d, %s)\n", x, y, width, height, color.toString()); } public void renderQuad(int x, int y, int width, int height, @@ -356,26 +356,30 @@ public class RenderDeviceJme implements RenderDevice { buf.flip(); quadColor.updateData(buf); - niftyQuadGradMat.getAdditionalRenderState().setBlendMode(convertBlend()); + unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend()); + unshadedMat.setColor("Color", ColorRGBA.White); + unshadedMat.setTexture("ColorMap", null); + unshadedMat.setBoolean("VertexColor", true); tempMat.loadIdentity(); tempMat.setTranslation(x, getHeight() - y, 0); tempMat.setScale(width, height, 0); rm.setWorldMatrix(tempMat); - niftyQuadGradMat.render(quadGeom, rm); -// -// System.out.println("renderQuad (Grad)"); + unshadedMat.render(quadGeom, rm); + + //System.out.format("renderQuad2(%d, %d, %d, %d, %s, %s, %s, %s)\n", x, y, width, height, topLeft.toString(), + // topRight.toString(), + // bottomRight.toString(), + // bottomLeft.toString()); } public void enableClip(int x0, int y0, int x1, int y1) { -// System.out.println("enableClip"); clipWasSet = true; r.setClipRect(x0, getHeight() - y1, x1 - x0, y1 - y0); } public void disableClip() { -// System.out.println("disableClip"); if (clipWasSet) { r.clearClipRect(); clipWasSet = false; diff --git a/engine/src/niftygui/com/jme3/niftygui/RenderFontJme.java b/engine/src/niftygui/com/jme3/niftygui/RenderFontJme.java index dccef3435..21bb7be04 100644 --- a/engine/src/niftygui/com/jme3/niftygui/RenderFontJme.java +++ b/engine/src/niftygui/com/jme3/niftygui/RenderFontJme.java @@ -50,9 +50,6 @@ public class RenderFontJme implements RenderFont { public RenderFontJme(String name, NiftyJmeDisplay display) { this.display = display; font = display.getAssetManager().loadFont(name); - if (font == null) { - throw new RuntimeException( "Font not loaded:" + name ); - } text = new BitmapText(font); actualSize = font.getPreferredSize(); text.setSize(actualSize); @@ -62,6 +59,10 @@ public class RenderFontJme implements RenderFont { return new BitmapText(font); } + public BitmapFont getFont() { + return font; + } + public BitmapText getText(){ return text; } @@ -80,8 +81,9 @@ public class RenderFontJme implements RenderFont { * @return width of the given text for the current font */ public int getWidth(final String str) { - if (str.length() == 0) + if (str.length() == 0) { return 0; + } // Note: BitmapFont is now fixed to return the proper line width // at least for now. The older commented out (by someone else, not me) diff --git a/engine/src/niftygui/com/jme3/niftygui/RenderImageJme.java b/engine/src/niftygui/com/jme3/niftygui/RenderImageJme.java index e871e8a71..145009475 100644 --- a/engine/src/niftygui/com/jme3/niftygui/RenderImageJme.java +++ b/engine/src/niftygui/com/jme3/niftygui/RenderImageJme.java @@ -63,8 +63,9 @@ public class RenderImageJme implements RenderImage { } public RenderImageJme(Texture2D texture){ - if (texture.getImage() == null) + if (texture.getImage() == null) { throw new IllegalArgumentException("texture.getImage() cannot be null"); + } this.texture = texture; this.image = texture.getImage();