1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.jme3.app; 34 35 import com.jme3.asset.AssetManager; 36 import com.jme3.font.BitmapFont; 37 import com.jme3.font.BitmapText; 38 import com.jme3.renderer.RenderManager; 39 import com.jme3.renderer.Statistics; 40 import com.jme3.renderer.ViewPort; 41 import com.jme3.renderer.queue.RenderQueue.Bucket; 42 import com.jme3.scene.Node; 43 import com.jme3.scene.Spatial; 44 import com.jme3.scene.control.Control; 45 46 /** 47 * The <code>StatsView</code> provides a heads-up display (HUD) of various 48 * statistics of rendering. The data is retrieved every frame from a 49 * {@link com.jme3.renderer.Statistics} and then displayed on screen.<br/> 50 * <br/> 51 * Usage:<br/> 52 * To use the stats view, you need to retrieve the 53 * {@link com.jme3.renderer.Statistics} from the 54 * {@link com.jme3.renderer.Renderer} used by the application. Then, attach 55 * the <code>StatsView</code> to the scene graph.<br/> 56 * <code><br/> 57 * Statistics stats = renderer.getStatistics();<br/> 58 * StatsView statsView = new StatsView("MyStats", assetManager, stats);<br/> 59 * rootNode.attachChild(statsView);<br/> 60 * </code> 61 */ 62 public class StatsView extends Node implements Control { 63 64 private BitmapText[] labels; 65 private Statistics statistics; 66 67 private String[] statLabels; 68 private int[] statData; 69 70 private boolean enabled = true; 71 72 private final StringBuilder stringBuilder = new StringBuilder(); 73 74 public StatsView(String name, AssetManager manager, Statistics stats){ 75 super(name); 76 77 setQueueBucket(Bucket.Gui); 78 setCullHint(CullHint.Never); 79 80 statistics = stats; 81 82 statLabels = statistics.getLabels(); 83 statData = new int[statLabels.length]; 84 labels = new BitmapText[statLabels.length]; 85 86 BitmapFont font = manager.loadFont("Interface/Fonts/Console.fnt"); 87 for (int i = 0; i < labels.length; i++){ 88 labels[i] = new BitmapText(font); 89 labels[i].setLocalTranslation(0, labels[i].getLineHeight() * (i+1), 0); 90 attachChild(labels[i]); 91 } 92 93 addControl(this); 94 } 95 96 public void update(float tpf) { 97 98 if (!isEnabled()) 99 return; 100 101 statistics.getData(statData); 102 for (int i = 0; i < labels.length; i++) { 103 stringBuilder.setLength(0); 104 stringBuilder.append(statLabels[i]).append(" = ").append(statData[i]); 105 labels[i].setText(stringBuilder); 106 } 107 108 // Moved to ResetStatsState to make sure it is 109 // done even if there is no StatsView or the StatsView 110 // is disable. 111 //statistics.clearFrame(); 112 } 113 114 public Control cloneForSpatial(Spatial spatial) { 115 return (Control) spatial; 116 } 117 118 public void setSpatial(Spatial spatial) { 119 } 120 121 public void setEnabled(boolean enabled) { 122 this.enabled = enabled; 123 } 124 125 public boolean isEnabled() { 126 return enabled; 127 } 128 129 public void render(RenderManager rm, ViewPort vp) { 130 } 131 132 } 133