Picture.java 5.5 KB

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