36
« on: November 17, 2012, 11:27:52 pm »
Quick update, the code for the above test app is now:
package app;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import kitt.com.jpct.GameControls;
import kitt.com.jpct.XMLLoader;
import kitt.com.jpct.anim.Animator;
import kitt.com.jpct.anim.AnimatorCallback;
import kitt.com.jpct.event.CanvasEventHandler;
import kitt.com.jpct.event.Controller2ButtonBinding;
import kitt.com.jpct.event.ControllerAxisBinding;
import kitt.com.jpct.event.ControllerBinding;
import kitt.com.jpct.event.ControllerButtonBinding;
import kitt.com.jpct.event.EventHandler;
import kitt.com.jpct.event.KeyBinding;
import kitt.com.jpct.event.bind.CanvasBinding;
import kitt.com.jpct.event.bind.EventBinding;
import kitt.com.jpct.event.bind.EventBindingCallback;
import kitt.com.jpct.ext.GameWorld;
import kitt.com.jpct.ext.Object3DEx;
import kitt.com.jpct.ext.RadialCamera;
import kitt.com.jpct.game.ControllerData;
import kitt.com.jpct.game.GamePads;
import kitt.com.jpct.hud.HUDCellElement;
import kitt.com.jpct.hud.HUDElement;
import kitt.com.jpct.hud.HUDManager;
import kitt.com.jpct.hud.HUDTextLayer;
import kitt.com.jpct.hud.HUDTextureElement;
import kitt.com.jpct.hud.anim.HUDAnimator;
import kitt.com.jpct.hud.kits.HUDSelectorKit;
import kitt.com.jpct.scene.Scene;
import kitt.com.jpct.swing.GLPanel;
import kitt.com.jpct.swing.RenderCallback;
import kitt.com.jpct.tex.TextureEx;
import kitt.com.jpct.tex.TextureLabel;
import kitt.com.jpct.tex.TextureSequence;
import kitt.com.jpct.tex.TextureStored;
import kitt.com.jpct.tex.TextureSystem;
import kitt.com.math.Coordinate;
import kitt.com.math.Dimension;
import kitt.com.swing.Util;
import kitt.com.swing.canvas.CanvasButton;
import kitt.com.swing.canvas.CanvasGUI;
import kitt.com.swing.canvas.CanvasLabel;
import kitt.com.swing.canvas.CanvasPointer;
import kitt.com.swing.canvas.data.CanvasImageFont;
import kitt.com.swing.canvas.kits.MenuKit;
import kitt.com.swing.canvas.kits.ToggleMenuKit;
import kitt.com.swing.canvas.kits.WidgetKits;
import com.threed.jpct.Config;
import com.threed.jpct.Object3D;
import com.threed.jpct.Texture;
public class HUDTest extends JFrame implements EventBindingCallback, RenderCallback, AnimatorCallback {
private static final long serialVersionUID = 1L;
private static final String modelsdir = "res/models/";
private static final String texdir = "res/tex/";
private static final String xmldir = "res/xml/";
private GLPanel mainpanel = null;
private HUDManager hud = null;
private TextureStored guitexture = null;
private TextureStored barstexture = null;
private RadialCamera camera = null;
private ControllerData controller = null;
private GameWorld gameworld = null;
// private File ping = null;
private Object3DEx box = null;
private CanvasEventHandler canvashandler = null;
private HUDTextLayer textlayer = null;
private CanvasImageFont font = null;
private CanvasGUI basecanvas = null;
private GameControls gamecontroller = null;
public HUDTest() throws HeadlessException {
this.setup();
}
private void setup() {
this.setTitle("HUD Test");
this.setBounds(0, 0, 256, 256);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setResizable(false);
Container cpane = this.getContentPane();
cpane.setLayout(null);
this.setVisible(true);
Dimension dim = Util.getRealSize(this, 1024, 768);
Coordinate loc = Util.centerScreen(dim.width, dim.height);
this.setBounds(loc.x, loc.y, dim.width, dim.height);
// Config.isIndoor = true;
Config.tuneForOutdoor();
Config.doSorting = false;
Config.glFullscreen = false;
Config.glAvoidTextureCopies = true;
Config.glColorDepth = 24;
// Config.maxAnimationSubSequences = 10;
Config.farPlane = 100;
Config.glShadowZBias = 0.5f;
Config.saveMemory=true;
Config.maxTextureLayers = 2;
Config.maxPolysVisible = 20000;
Config.collideOffset = 1;
Config.glUseIgnorantBlits = true;
Config.autoBuild = true;
Config.lightMul = 5;
// Config.glTrilinear = true;
this.mainpanel = new GLPanel(1024, 768);
this.mainpanel.setRenderGL(false);
this.gameworld = this.mainpanel.getWorld();
// AudioManager.playLoop("res/audio/music/danosongs.com-bitbybits.ogg", -5.0f);
// this.ping = new File("res/audio/effects/ping.wav");
this.basecanvas = new CanvasGUI();
this.mainpanel.setBaseCanvas(this.basecanvas);
this.basecanvas.setBackground(new Color(0,128,128,128));
this.basecanvas.setForeground(new Color(64,255,255));
this.basecanvas.setName("Main Menu");
this.basecanvas.setFont(CanvasGUI.FONTLARGE);
this.basecanvas.setTextColor(Color.WHITE);
CanvasGUI.DEFAULT_DISABLED = new Color(255,0,0,128);
CanvasGUI.DEFAULT_HOVER = new Color(0,255,0,128);
CanvasGUI.DEFAULT_PRESSED = new Color(0,0,255,128);
CanvasGUI.DEFAULT_FOREGROUND = new Color(0,255,0);
CanvasGUI.DEFAULT_BACKGROUND = CanvasGUI.HALFBLACK;;
this.canvashandler = new CanvasEventHandler(this.basecanvas);
this.canvashandler.addTo(this.basecanvas);
this.mainpanel.setCurrentEventHandler(this.canvashandler);
this.basecanvas.setFlags(CanvasGUI.ROUNDED | CanvasGUI.BORDER);
this.guitexture = TextureSystem.loadStoredImage("res/tex/gui/base.png", "gui");
this.guitexture.makeTexture();
this.guitexture.addTexture();
this.barstexture = TextureSystem.loadStoredImage("res/tex/gui/bars-ver.png", "bars");
this.barstexture.makeTexture();
this.barstexture.addTexture();
TextureStored image = TextureSystem.loadStoredImage("res/tex/font-base.png", "font");
CanvasGUI.IMAGEFONT = new CanvasImageFont(image.getImage(), 32, 32);
CanvasGUI.IMAGEFONT.setBaseline(8);
CanvasGUI.IMAGEFONT.setAsciiOffset(32);
CanvasGUI.IMAGEFONT.setScale(0.5f);
TextureStored[] icons = XMLLoader.loadStoredPackFromXML("res/xml/texpack-icons.xml", HUDTest.texdir, null);
XMLLoader.loadStoredPackFromXML("res/xml/texpack-basic.xml", HUDTest.texdir, null);
image = TextureSystem.loadStoredImage("res/tex/icons/icon-logo-full.png", "icon-logo");
CanvasLabel label = new CanvasLabel(null, "Hacker's\nHaven", 100, 50, 100, 100);
label.setFlags(CanvasGUI.ROUNDED | CanvasGUI.BORDER | CanvasGUI.SHOWTEXT |
CanvasGUI.TEXT_LEFT | CanvasGUI.TEXT_TOP | CanvasGUI.IMAGE_STRETCH);
label.setImage(image.getImage());
this.basecanvas.addWidget(label);
CanvasButton button = new CanvasButton(null, "Pause", 0, 0, 100, 100);
button.setID("Pause");
this.basecanvas.addWidget(button);
String[][] options = {
{"GameStart", "Start"},
{"GameLoad", "Load"},
{"GameConfigure", "Settings"},
{"GameDelete", "Delete"},
{"GameExit", "Exit"}
};
MenuKit menu = WidgetKits.makeMenu(this.basecanvas, "Main Menu",
null, 150, 40,
options, 3, -1);
menu.parent.setLocation(200, 250);
menu.parent.setEnabled(false);
CanvasGUI.DEFAULT_FOREGROUND = new Color(0,255,255);
String[][] toptions = {
{"GameFun", "Fun"},
{"GameWicked", "Wicked"},
{"GameCool", "Cool"},
{"GameRun", "Run"}
};
ToggleMenuKit togglemenu = WidgetKits.makeToggleMenu(this.basecanvas, "Sub Menu",
null, 150, 40,
toptions, 2, CanvasGUI.HEXAGONAL | CanvasGUI.BORDER | CanvasGUI.SHOWTEXT |
CanvasGUI.TEXT_RIGHT | CanvasGUI.TEXT_BOTTOM | CanvasGUI.FILL | CanvasGUI.IMAGE_STRETCH);
togglemenu.parent.setLocation(400, 50);
togglemenu.parent.setSingleToggle(true);
for (int i = 0; i < togglemenu.options.length; i++) {
togglemenu.options[i].setImage(icons[i].getImage());
}
CanvasPointer pointer = new CanvasPointer(this.guitexture.getImage());
pointer.setImageOffset(new Coordinate(0, 200));
pointer.setImagePart(new Dimension(64,64));
pointer.setSize(64, 64);
pointer.setLocation(-8, -20);
this.basecanvas.setPointer(pointer);
this.basecanvas.setBackImage(new BufferedImage(this.mainpanel.getWidth(), this.mainpanel.getHeight(),
BufferedImage.TYPE_INT_RGB));
this.guitexture.makeTexture();
this.barstexture.makeTexture();
this.mainpanel.setLocation(0, 0);
cpane.add(this.mainpanel);
this.mainpanel.alignBaseCanvas();
this.gamecontroller = new GameControls(this.gameworld, this.mainpanel.getBuffer());
this.hud = this.gamecontroller.getHUD();
// EventHandler.installFixer();
this.mainpanel.addEventHandler(this.gamecontroller.getHandler());
this.mainpanel.setCurrentEventHandlerGL(this.gamecontroller.getHandler());
this.mainpanel.setHUD(this.hud);
this.controller = GamePads.getController(0);
if (this.controller != null) {
this.controller.setEnabled(true);
System.out.println("Controller Enabled");
this.gamecontroller.setControllerDevice(this.controller);
this.canvashandler.addController(this.controller);
}
this.camera = new RadialCamera();
this.gameworld.setCameraTo(this.camera);
this.camera.setDistance(0.05f);
HUDTextureElement element = new HUDTextureElement(null, "text", 0, 512, 1024, 256);
element.setTexture(this.guitexture.getTexture(), 0, 0, 1024, 200);
element.setZOrder(0);
this.hud.add(element);
this.font = new CanvasImageFont(CanvasGUI.IMAGEFONT, 0.8f);
this.textlayer = new HUDTextLayer(element, "text-layer", 0, 0, 1024, 256);
this.textlayer.setZOrder(1);
this.textlayer.setFont(font);
this.textlayer.setBorder(32);
this.textlayer.setZOrder(1);
this.hud.add(this.textlayer);
TextureStored tex = TextureSystem.findStoredImage(null, "hud-panel-option");
if(tex != null) tex.addTexture();
HUDSelectorKit selectorkit = HUDSelectorKit.makeMenuSelector(
HUDSelectorKit.CELLELEMENTS, "Menu", null, "Panel",
-200.0f, 50.0f, 256.0f, 32.0f, 10, this.hud, null, tex, 0, 0, 512, 64);
if(selectorkit != null) {
HUDAnimator animator = new HUDAnimator();
animator.setTranslation(200.0f, -8.0f);
animator.setSpeed(0.025f);
animator.setScale(1.0f, 1.5f);
HUDAnimator select = new HUDAnimator();
select.setSpeed(0.1f);
select.setTranslation(0.0f, 10.0f);
select.setBounce(true);
this.gamecontroller.setHUDList(selectorkit.elements, animator, select);
for(HUDElement elem : selectorkit.elements) {
TextureLabel ntex = new TextureLabel(tex, elem.getName());
ntex.initializeCenters(2);
ntex.getCenters()[0] = new Coordinate(256, 32);
ntex.getCenters()[1] = new Coordinate(768, 32);
ntex.setFont(this.font);
ntex.setText(elem.getName());
ntex.updateText();
ntex.makeTexture();
ntex.addTexture();
TextureSystem.addStoredImage(ntex);
((HUDTextureElement)elem).setTexture(ntex.getTexture());
}
}
tex = TextureSystem.findStoredImage(null, "hud-icon-hex");
if(tex != null) tex.addTexture();
selectorkit = HUDSelectorKit.makeSelector(HUDSelectorKit.EVENODD | HUDSelectorKit.HEXAGONAL |
HUDSelectorKit.HORIZONTAL | HUDSelectorKit.OFFSETUP | HUDSelectorKit.CELLELEMENTS,
"Menu", null, "Hex",
800.0f, 50.0f, 80.0f, 80.0f, 3, 3, this.hud, null, tex, 0, 0, 128, 128);
if(selectorkit != null) {
String[] iconnames = {
"icon-buffer",
"icon-clone",
"icon-crash-bomb",
"icon-decode",
"icon-deflector",
"icon-firewall",
"icon-mask",
"icon-slicer",
"icon-spy-bot",
"icon-torrent"
};
for(int i = 0; i < iconnames.length; i++) {
TextureStored icontex = TextureSystem.findStoredImage(null, iconnames[i]);
if(icontex != null && tex != null) {
BufferedImage img = Util.cloneImage(tex.getImage());
Graphics2D g = (Graphics2D)img.getGraphics();
Util.quickPaint(g, icontex.getImage(), 0, 0, false, false);
Util.quickPaint(g, icontex.getImage(), 128, 0, false, false);
g.dispose();
TextureEx eltex = new TextureEx(null, "hex-" + iconnames[i], new Texture(img, true));
TextureSystem.addTexture(eltex);
eltex.addTexture();
System.out.println(eltex.getName());
((HUDCellElement)selectorkit.elements[i]).setTexture(eltex.getTexture());
if(i == (i/3)*3)
((HUDCellElement)selectorkit.elements[i]).setCell(1);
}
}
}
this.hud.zSort();
// TextureSequence tseq = XMLLoader.loadSequenceFromXML("res/xml/texseq-dissolve.xml", HUDTest.texdir, null);
// tseq.addTextures();
// tseq = XMLLoader.loadSequenceFromXML("res/xml/texseq-appear.xml", HUDTest.texdir, null);
// tseq.addTextures();
TextureSequence tanim = XMLLoader.loadSequenceFromXML("res/xml/texseq-barslide.xml", HUDTest.texdir, null);
tanim.addTextures();
// tseq = XMLLoader.loadSequenceFromXML("res/xml/texseq-grid-shine.xml", HUDTest.texdir, null);
// tseq.makeTexture();
// if(tseq != null) {
// AnimatedBackground background = new AnimatedBackground("background", this.mainpanel.getCanvas());
// background.setTexture(tseq);
// background.setFullImage();
// this.mainpanel.setBackgroundElement(background);
// }
Scene scene = XMLLoader.loadSceneFromXML("res/xml/scene-internet.xml", HUDTest.xmldir, HUDTest.modelsdir, HUDTest.texdir);
scene.setWorld(this.gameworld);
scene.setScene();
this.box = XMLLoader.loadObjectXML("res/xml/avat-shiva.xml", HUDTest.modelsdir, HUDTest.texdir);
this.box.averageCollisionXZ();
this.box.setCollisionMode(Object3D.COLLISION_CHECK_SELF);
this.box.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
this.box.translate(scene.getSpawn("Logon"));
this.gameworld.addObjectEx(this.box);
this.gamecontroller.setPlayerObject(this.box);
this.gamecontroller.getPlayerAnimator().setCurrentMeshAnimation("stand", false);
element = new HUDTextureElement(null, "Pointer", 0, 0, 32, 32);
element.setTexture(this.guitexture.getTexture(), 64, 200, 64, 64);
element.setVisible(true);
this.hud.add(element);
this.gamecontroller.setPointer(element);
EventBinding gevent = new EventBinding("Pause");
gevent.setController(this.controller);
gevent.setControllerEvent(new ControllerButtonBinding(false, 9));
gevent.setKeyEvent(new KeyBinding(KeyEvent.VK_P, 0, EventBinding.KEY_PRESSED));
gevent.setCallback(this);
this.gamecontroller.getHandler().addEvent(gevent);
this.canvashandler.addEvent(gevent);
gevent = new EventBinding("Canvas");
gevent.setController(this.controller);
gevent.setControllerEvent(new ControllerButtonBinding(false, 3));
gevent.setKeyEvent(new KeyBinding(KeyEvent.VK_ESCAPE, 0,
EventBinding.KEY_PRESSED));
gevent.setCallback(this);
this.gamecontroller.getHandler().addEvent(gevent);
CanvasBinding canevent = new CanvasBinding(null, this.basecanvas);
canevent.setController(this.controller);
canevent.setControllerEvent(new ControllerAxisBinding(false, ControllerBinding.DIRECTIONAL, ControllerBinding.BOTH));
canevent.setEffectPointer(true);
this.canvashandler.addEvent(canevent);
canevent = new CanvasBinding(null, this.basecanvas);
canevent.setController(this.controller);
canevent.setControllerEvent(new Controller2ButtonBinding(false, 4, 5));
canevent.setNavigateWidgets(true);
this.canvashandler.addEvent(canevent);
canevent = new CanvasBinding(null, this.basecanvas);
canevent.setController(this.controller);
canevent.setControllerEvent(new ControllerButtonBinding(false, 1));
canevent.setSimulateClick(true);
this.canvashandler.addEvent(canevent);
this.gamecontroller.enableControls();
}
public static void main(String[] args) {
HUDTest app = new HUDTest();
try {
app.loop();
} catch (Exception e) {
e.printStackTrace();
}
}
private void loop() throws Exception {
System.out.println("Looping");
if(!GamePads.isInitialized()) GamePads.initialize();
this.mainpanel.setRenderGL(true);
this.textlayer.addLine("Environment simulation loaded.");
this.textlayer.addLine("Avatar Shiva loaded.");
this.textlayer.addLine("Interface initiated.");
this.textlayer.addLine("Login in progress ...");
this.mainpanel.clearTimer();
while (this.isShowing()) {
while(this.gamecontroller.getHandler().hasCommands() || this.canvashandler.hasCommands()) {
String tcomm = "";
if(this.gamecontroller.getHandler().hasCommands()) tcomm = this.gamecontroller.getHandler().popCommand();
else if(this.canvashandler.hasCommands()) tcomm = this.canvashandler.popCommand();
System.out.println(tcomm);
this.textlayer.addLine(tcomm);
if(tcomm.compareTo("Panel-2") == 0) {
this.gamecontroller.getPlayerAnimator().setCurrentMeshAnimation("access", true);
}
if(tcomm.compareTo("Panel-3") == 0) {
this.gamecontroller.getPlayerAnimator().setCurrentMeshAnimation("delete", true);
// AudioManager.pauseLoop(true);
}
if(tcomm.compareTo("Panel-4") == 0) {
// AudioManager.pauseLoop(false);
}
if(tcomm.compareTo("Panel-5") == 0) {
this.gamecontroller.getPlayerAnimator().setCurrentMeshAnimation("appear", true);
// AudioManager.playEffect(this.ping);
}
if(tcomm.compareTo("Panel-6") == 0) {
this.gamecontroller.getPlayerAnimator().setCurrentMeshAnimation("failb", true);
}
if(tcomm.compareTo("Pause") == 0) {
if(this.mainpanel.isRenderGL()) {
this.mainpanel.setPaused(!this.mainpanel.isPaused());
this.gamecontroller.setPause(this.mainpanel.isPaused());
} else {
this.mainpanel.setRenderGL(true);
}
}
if(tcomm.compareTo("Canvas") == 0) {
if(this.mainpanel.isRenderGL()) {
this.mainpanel.snapshot(this.basecanvas.getBackImage());
this.mainpanel.setPaused(true);
this.gamecontroller.setPause(true);
this.mainpanel.setRenderGL(false);
}
}
}
this.mainpanel.renderScene();
Thread.sleep(30);
}
System.out.println("Exiting!");
this.mainpanel.dispose();
EventHandler.removeFixer();
System.exit(0);
}
@Override
public boolean eventCall(int event, EventBinding binding) {
System.out.println("Callback!");
return true;
}
@Override
public void animateCallback(Animator animator, int event) {
// if(animator == this.mainanimator) {
// if(event == AnimatorCallback.TEXTURE_DONE) {
// this.mainanimator.setAnimateMesh(true);
// }
// if(event == AnimatorCallback.MESH_DONE) {
//
// }
// }
}
@Override
public void renderCallback() {
// Called after each render.
}
}