Show ConsoleSpatial.java syntax highlighted
/*
* Copyright (C) 2001-2005 Pleasant nightmare studio
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.pleasantnightmare.riotinecity.console;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;
import org.pleasantnightmare.riotinecity.util.TextureMappingCalculator;
import java.nio.FloatBuffer;
/**
* @author deus
* @version 1.0
* @since Mar 23, 2007 9:20:19 PM
*/
public class ConsoleSpatial extends Node {
private static final String TEXTURE = "images/console-background.png";
private static final int LINES_COUNT = 20;
private static final int WIDTH = 512;
private static final int HEIGHT = 384;
private Text[] lines = new Text[LINES_COUNT];
public ConsoleSpatial(int windowHeight) {
super("console");
setRenderQueueMode(Renderer.QUEUE_ORTHO);
setLocalTranslation(new Vector3f(WIDTH / 2, windowHeight - (HEIGHT / 2), 0));
background();
textLines();
zbuffer();
updateRenderState();
}
private void zbuffer() {
ZBufferState state = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
state.setEnabled(true);
state.setFunction(ZBufferState.CF_ALWAYS);
setRenderState(state);
}
private void textLines() {
final int fontHeight = 18;
final int edgeDisplacer = 9;
final int start = -(HEIGHT / 2) + edgeDisplacer;
// Font
TextureState font = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
font.setTexture(TextureManager.loadTexture("images/font1.png", Texture.MM_LINEAR, Texture.FM_LINEAR, 1.0f, true));
AlphaState as = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
as.setBlendEnabled(true);
for (int i = 0; i < lines.length; i++) {
lines[i] = new Text("action-text", "");
lines[i].setRenderState(font);
lines[i].setRenderState(as);
lines[i].setLocalTranslation(-(WIDTH / 2) + edgeDisplacer, start + (i * fontHeight), 0);
lines[i].setTextColor(ColorRGBA.yellow);
lines[i].setZOrder(-1);
lines[i].updateRenderState();
attachChild(lines[i]);
}
}
private void background() {
Quad background = new Quad("console-background", WIDTH, HEIGHT);
background.setLightCombineMode(LightState.OFF);
background.setRenderState(backgroundTexture());
background.setRenderState(backgroundAlpha());
background.setTextureBuffer(0, backgroundMapping());
attachChild(background);
}
private FloatBuffer backgroundMapping() {
FloatBuffer b = BufferUtils.createVector2Buffer(4);
TextureMappingCalculator c = new TextureMappingCalculator(512, 512);
b.put(c.getUForPixel(0)).put(c.getVForPixel(0));
b.put(c.getUForPixel(0)).put(c.getVForPixel(HEIGHT));
b.put(c.getUForPixel(WIDTH)).put(c.getVForPixel(HEIGHT));
b.put(c.getUForPixel(WIDTH)).put(c.getVForPixel(0));
return b;
}
private AlphaState backgroundAlpha() {
AlphaState as = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
as.setBlendEnabled(true);
as.setSrcFunction(AlphaState.SB_SRC_ALPHA);
as.setDstFunction(AlphaState.SB_ONE_MINUS_SRC_ALPHA);
as.setTestEnabled(true);
as.setTestFunction(AlphaState.TF_GREATER);
as.setEnabled(true);
return as;
}
private TextureState backgroundTexture() {
TextureState textureState = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
Texture texture = TextureManager.loadTexture(TEXTURE, Texture.MM_LINEAR, Texture.FM_LINEAR, 1.0f, true);
textureState.setTexture(texture);
return textureState;
}
public void setLine(int line, String text) {
if (line < 0 || line > lines.length - 1)
throw new IllegalArgumentException("Line index out of bounds: " + line);
lines[line].print(text);
}
public int linesCount() {
return LINES_COUNT;
}
}
See more files for this project here