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.ui; 34 35 import com.jme3.asset.AssetManager; 36 import com.jme3.asset.TextureKey; 37 import com.jme3.material.Material; 38 import com.jme3.material.RenderState.BlendMode; 39 import com.jme3.math.ColorRGBA; 40 import com.jme3.math.Vector3f; 41 import com.jme3.renderer.queue.RenderQueue.Bucket; 42 import com.jme3.scene.Geometry; 43 import com.jme3.scene.shape.Quad; 44 import com.jme3.texture.Texture2D; 45 46 /** 47 * A <code>Picture</code> represents a 2D image drawn on the screen. 48 * It can be used to represent sprites or other background elements. 49 * 50 * @author Kirill Vainer 51 */ 52 public class Picture extends Geometry { 53 54 private float width = 1f; 55 private float height = 1f; 56 57 /** 58 * Create a named picture. 59 * 60 * By default a picture's width and height are 1 61 * and its position is 0, 0. 62 * 63 * @param name the name of the picture in the scene graph 64 * @param flipY If true, the Y coordinates of the texture will be flipped. 65 */ Picture(String name, boolean flipY)66 public Picture(String name, boolean flipY){ 67 super(name, new Quad(1, 1, flipY)); 68 setQueueBucket(Bucket.Gui); 69 setCullHint(CullHint.Never); 70 } 71 72 /** 73 * Creates a named picture. 74 * By default a picture's width and height are 1 75 * and its position is 0, 0. 76 * The image texture coordinates will not be flipped. 77 * 78 * @param name the name of the picture in the scene graph 79 */ Picture(String name)80 public Picture(String name){ 81 this(name, false); 82 } 83 84 /* 85 * Serialization only. Do not use. 86 */ Picture()87 public Picture(){ 88 } 89 90 /** 91 * Set the width in pixels of the picture, if the width 92 * does not match the texture's width, then the texture will 93 * be scaled to fit the picture. 94 * 95 * @param width the width to set. 96 */ setWidth(float width)97 public void setWidth(float width){ 98 this.width = width; 99 setLocalScale(new Vector3f(width, height, 1f)); 100 } 101 102 /** 103 * Set the height in pixels of the picture, if the height 104 * does not match the texture's height, then the texture will 105 * be scaled to fit the picture. 106 * 107 * @param height the height to set. 108 */ setHeight(float height)109 public void setHeight(float height){ 110 this.height = height; 111 setLocalScale(new Vector3f(width, height, 1f)); 112 } 113 114 /** 115 * Set the position of the picture in pixels. 116 * The origin (0, 0) is at the bottom-left of the screen. 117 * 118 * @param x The x coordinate 119 * @param y The y coordinate 120 */ setPosition(float x, float y)121 public void setPosition(float x, float y){ 122 float z = getLocalTranslation().getZ(); 123 setLocalTranslation(x, y, z); 124 } 125 126 /** 127 * Set the image to put on the picture. 128 * 129 * @param assetManager The {@link AssetManager} to use to load the image. 130 * @param imgName The image name. 131 * @param useAlpha If true, the picture will appear transparent and allow 132 * objects behind it to appear through. If false, the transparent 133 * portions will be the image's color at that pixel. 134 */ setImage(AssetManager assetManager, String imgName, boolean useAlpha)135 public void setImage(AssetManager assetManager, String imgName, boolean useAlpha){ 136 TextureKey key = new TextureKey(imgName, true); 137 Texture2D tex = (Texture2D) assetManager.loadTexture(key); 138 setTexture(assetManager, tex, useAlpha); 139 } 140 141 /** 142 * Set the texture to put on the picture. 143 * 144 * @param assetManager The {@link AssetManager} to use to load the material. 145 * @param tex The texture 146 * @param useAlpha If true, the picture will appear transparent and allow 147 * objects behind it to appear through. If false, the transparent 148 * portions will be the image's color at that pixel. 149 */ setTexture(AssetManager assetManager, Texture2D tex, boolean useAlpha)150 public void setTexture(AssetManager assetManager, Texture2D tex, boolean useAlpha){ 151 if (getMaterial() == null){ 152 Material mat = new Material(assetManager, "Common/MatDefs/Gui/Gui.j3md"); 153 mat.setColor("Color", ColorRGBA.White); 154 setMaterial(mat); 155 } 156 material.getAdditionalRenderState().setBlendMode(useAlpha ? BlendMode.Alpha : BlendMode.Off); 157 material.setTexture("Texture", tex); 158 } 159 160 } 161