//
// $Id$
//
// BUI - a user interface library for the JME 3D engine
// Copyright (C) 2005, Michael Bayne, All Rights Reserved
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.jmex.bui.icon;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

import com.jmex.bui.util.RenderUtil;
import com.jme.image.Texture;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.renderer.Renderer;

/**
 * Displays an icon using image data that is already loaded as a texture.
 * This is mainly useful for images created by rendering to a texture.
 */
public class TextureIcon extends BIcon
{
    /**
     * Creates a texture icon with the supplied texture. <em>Note:</em>
     * the texture will be placed into <code>AM_REPLACE</code> mode (which
     * is not JME's default) to avoid strange interaction with the current
     * color. If this is not desirable, change it after constructing the
     * icon.
     */
    public TextureIcon (Texture texture, int width, int height)
    {
        _texture = texture;
        _texture.setApply(Texture.AM_REPLACE);
        _tstate = DisplaySystem.getDisplaySystem().getRenderer().
            createTextureState();
        _tstate.setTexture(_texture);
        _tstate.setEnabled(true);
        _width = width;
        _height = height;
    }

    // documentation inherited
    public int getWidth ()
    {
        return _width;
    }

    // documentation inherited
    public int getHeight ()
    {
        return _height;
    }

    // documentation inherited
    public void render (Renderer renderer, int x, int y)
    {
        super.render(renderer, x, y);

        RenderUtil.blendState.apply();
        _tstate.apply();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0); GL11.glVertex3f(x, y, 0);
        GL11.glTexCoord2f(0, 1); GL11.glVertex3f(x, y + _height, 0);
        GL11.glTexCoord2f(1, 1); GL11.glVertex3f(x + _width, y + _height, 0);
        GL11.glTexCoord2f(1, 0); GL11.glVertex3f(x + _width, y, 0);
        GL11.glEnd();
    }

    protected Texture _texture;
    protected TextureState _tstate;
    protected int _width, _height;
}

@@ -1728,295 +1728,731 @@
     
-_
 t
+his(width, height);%0A        setT
 exture
- = 
+(
 texture
+)
 ;%0A    
+%7D%0A%0A
     
-_texture.setApply(Texture.AM_REPLACE);%0A        _tstate = DisplaySystem.getDisplaySystem().getRenderer().%0A            createTextureState();%0A        _tstate.setTexture(_texture);%0A        _tstate.setEnabled(true);%0A        _width = width;%0A        _height = height
+/**%0A     * Creates a texture icon with no texture yet assigned. The icon will be%0A     * blank until a texture is provided with a subsequent call to %7B@link%0A     * #setTexture%7D.%0A     */%0A    public TextureIcon (int width, int height)%0A    %7B%0A        _tstate = DisplaySystem.getDisplaySystem().getRenderer().%0A            createTextureState();%0A        _width = width;%0A        _height = height;%0A    %7D%0A%0A    /**%0A     * Configures the texture to be used for this icon.%0A     */%0A    public void setTexture (Texture texture)%0A    %7B%0A        _texture = texture;%0A        _texture.setApply(Texture.AM_REPLACE);%0A        _tstate.setTexture(_texture);%0A        _tstate.setEnabled(true)
 ;%0A  
@@ -2779,16 +2779,52 @@
 x, y);%0A%0A
+        if (_texture != null) %7B%0A    
         
@@ -2854,32 +2854,36 @@
 pply();%0A        
+    
 _tstate.apply();
@@ -2875,32 +2875,36 @@
 tstate.apply();%0A
+    
         GL11.glB
@@ -2924,32 +2924,36 @@
 QUADS);%0A        
+  
+  
 GL11.glTexCoord2
@@ -2979,32 +2979,36 @@
 tex3f(x, y, 0);%0A
+    
         GL11.glT
@@ -3060,32 +3060,36 @@
 ht, 0);%0A        
+   
+ 
 GL11.glTexCoord2
@@ -3134,32 +3134,36 @@
  + _height, 0);%0A
+    
         GL11.glT
@@ -3218,24 +3218,28 @@
 0);%0A        
+    
 GL11.glEnd()
@@ -3236,24 +3236,34 @@
 11.glEnd();%0A
+        %7D%0A
     %7D%0A%0A    p

Allow our texture to be provided later.