Picture.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * By default a picture's width and height are 1
  55. * and its position is 0, 0.
  56. *
  57. * @param name the name of the picture in the scene graph
  58. * @param flipY If true, the Y coordinates of the texture will be flipped.
  59. */
  60. public Picture(String name, boolean flipY){
  61. super(name, new Quad(1, 1, flipY));
  62. setQueueBucket(Bucket.Gui);
  63. setCullHint(CullHint.Never);
  64. }
  65. /**
  66. * Creates a named picture.
  67. * By default a picture's width and height are 1
  68. * and its position is 0, 0.
  69. * The image texture coordinates will not be flipped.
  70. *
  71. * @param name the name of the picture in the scene graph
  72. */
  73. public Picture(String name){
  74. this(name, false);
  75. }
  76. /*
  77. * Serialization only. Do not use.
  78. */
  79. public Picture(){
  80. }
  81. /**
  82. * Set the width in pixels of the picture, if the width
  83. * does not match the texture's width, then the texture will
  84. * be scaled to fit the picture.
  85. *
  86. * @param width the width to set.
  87. */
  88. public void setWidth(float width){
  89. this.width = width;
  90. setLocalScale(new Vector3f(width, height, 1f));
  91. }
  92. /**
  93. * Set the height in pixels of the picture, if the height
  94. * does not match the texture's height, then the texture will
  95. * be scaled to fit the picture.
  96. *
  97. * @param height the height to set.
  98. */
  99. public void setHeight(float height){
  100. this.height = height;
  101. setLocalScale(new Vector3f(width, height, 1f));
  102. }
  103. /**
  104. * Set the position of the picture in pixels.
  105. * The origin (0, 0) is at the bottom-left of the screen.
  106. *
  107. * @param x The x coordinate
  108. * @param y The y coordinate
  109. */
  110. public void setPosition(float x, float y){
  111. float z = getLocalTranslation().getZ();
  112. setLocalTranslation(x, y, z);
  113. }
  114. /**
  115. * Set the image to put on the picture.
  116. *
  117. * @param assetManager The {@link AssetManager} to use to load the image.
  118. * @param imgName The image name.
  119. * @param useAlpha If true, the picture will appear transparent and allow
  120. * objects behind it to appear through. If false, the transparent
  121. * portions will be black.
  122. */
  123. public void setImage(AssetManager assetManager, String imgName, boolean useAlpha){
  124. TextureKey key = new TextureKey(imgName, true);
  125. Texture2D tex = (Texture2D) assetManager.loadTexture(key);
  126. setTexture(assetManager, tex, useAlpha);
  127. }
  128. /**
  129. * Set the texture to put on the picture.
  130. *
  131. * @param assetManager The {@link AssetManager} to use to load the material.
  132. * @param tex The texture
  133. * @param useAlpha If true, the picture will appear transparent and allow
  134. * objects behind it to appear through. If false, the transparent
  135. * portions will be black.
  136. */
  137. public void setTexture(AssetManager assetManager, Texture2D tex, boolean useAlpha){
  138. if (getMaterial() == null){
  139. Material mat = new Material(assetManager, "Common/MatDefs/Gui/Gui.j3md");
  140. mat.setColor("Color", ColorRGBA.White);
  141. setMaterial(mat);
  142. }
  143. material.getAdditionalRenderState().setBlendMode(useAlpha ? BlendMode.Alpha : BlendMode.Off);
  144. material.setTexture("Texture", tex);
  145. }
  146. }