Advertisement
ekgame

Fancy buttons

Jul 2nd, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. package lt.ekgame.reflect.gui;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.GradientPaint;
  7. import java.awt.Graphics2D;
  8. import java.awt.Rectangle;
  9. import java.awt.RenderingHints;
  10. import java.awt.geom.Rectangle2D;
  11. import java.awt.image.BufferedImage;
  12.  
  13. import net.minecraft.client.gui.Gui;
  14.  
  15. import org.lwjgl.opengl.GL11;
  16.  
  17. import lt.ekgame.reflect.Reflect;
  18.  
  19. public class RTab {
  20.    
  21.     private int textureID;
  22.     private int textureID_hover;
  23.    
  24.     private int animation = 0;
  25.     int movement = 40;
  26.     int maxDelay = 0;
  27.     int delay = 0;
  28.    
  29.     public String text;
  30.     public Rectangle bounds;
  31.    
  32.     int hoverAlpha = 0;
  33.    
  34.     public RTab(String text, int delay)
  35.     {
  36.         bounds = new Rectangle();
  37.         this.maxDelay = delay;
  38.         setText(text, bounds);
  39.     }
  40.    
  41.     public void setText(String text, Rectangle bounds)
  42.     {
  43.         textureID = createTexture(text, bounds, false);
  44.         textureID_hover = createTexture(text, bounds, true);
  45.     }
  46.    
  47.     private int createTexture(String text, Rectangle bounds, boolean hover)
  48.     {
  49.         BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
  50.        
  51.         String string = text;
  52.         int padding = 8;
  53.        
  54.         int offset = 3;
  55.        
  56.         Graphics2D g = (Graphics2D) img.getGraphics();
  57.        
  58.         Font font = new Font("arial", Font.PLAIN, 21);
  59.         FontMetrics fontMetrics = g.getFontMetrics(font);
  60.        
  61.         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  62.        
  63.         if (hover)
  64.         {
  65.             g.setColor(new Color(25, 186, 255, 30));
  66.         }
  67.         else
  68.         {
  69.             g.setColor(new Color(0, 0, 0, 30));
  70.         }
  71.        
  72.         for (int i = 0; i < offset; i++)
  73.         {
  74.             g.fillRoundRect(i, i, fontMetrics.stringWidth(string) + padding * 2 + offset * 2 - i * 2, fontMetrics.getHeight()/2 + padding * 2 + offset * 2 - i * 2, 15 +  (offset - i) * 2, 15 + (offset - i) * 2);
  75.         }  
  76.        
  77.         if (hover)
  78.         {
  79.             g.setColor(new Color(25, 186, 255, 100));
  80.             g.fillRoundRect(0 + offset, 0 + offset, fontMetrics.stringWidth(string) + padding * 2, fontMetrics.getHeight()/2 + padding * 2, 15, 15);
  81.         }
  82.         else
  83.         {
  84.             g.setPaint(new GradientPaint(0, 0, new Color(250, 250, 250), 0, fontMetrics.getHeight() - 5, new Color(200, 200, 200), true));
  85.             g.fillRoundRect(0 + offset, 0 + offset, fontMetrics.stringWidth(string) + padding * 2, fontMetrics.getHeight()/2 + padding * 2, 15, 15);
  86.         }
  87.        
  88.         g.setFont(font);
  89.        
  90.         g.setColor(new Color(0, 0, 0, 255));
  91.         g.drawString(string, padding + offset + 1, fontMetrics.getHeight()/2 + padding + offset + 1);
  92.        
  93.         g.setPaint(new GradientPaint(0, 0, new Color(250, 250, 250), 0, fontMetrics.getHeight(), new Color(50, 50, 50), true));
  94.         g.drawString(string, padding + offset, fontMetrics.getHeight()/2 + padding + offset);
  95.        
  96.         bounds.setSize(fontMetrics.stringWidth(string) + padding * 2 + offset * 2, fontMetrics.getHeight()/2 + padding * 2 + offset * 2);
  97.        
  98.         return Reflect.getMC().renderEngine.allocateAndSetupTexture(img);
  99.     }
  100.    
  101.     public void setPos(int x, int y)
  102.     {
  103.         bounds.setLocation(x, y);
  104.         System.out.println(x + " " + y);
  105.     }
  106.    
  107.     public int getX()
  108.     {
  109.         return bounds.x;
  110.     }
  111.    
  112.     public int getY()
  113.     {
  114.         return bounds.y;
  115.     }
  116.    
  117.     public int getWidth()
  118.     {
  119.         return bounds.width;
  120.     }
  121.    
  122.     public int getHeight()
  123.     {
  124.         return bounds.height;
  125.     }
  126.    
  127.     private boolean pointInBounds(int x, int y)
  128.     {
  129.         return bounds.contains(x, y);
  130.     }
  131.    
  132.     public void draw(int mouseX, int mouseY, Gui gui)
  133.     {
  134.         int speed = 20;
  135.        
  136.         if (Reflect.isGuiDispalyed())
  137.         {
  138.             if (delay < maxDelay)
  139.             {
  140.                 delay = Math.min(delay + 1, maxDelay);
  141.             }
  142.             else
  143.             {
  144.                 animation = Math.min(animation + speed, 100);
  145.             }
  146.         }
  147.         else
  148.         {
  149.             if (delay > 0)
  150.             {
  151.                 delay = Math.max(delay - 1, 0);
  152.             }
  153.             else
  154.             {
  155.                 animation = Math.max(animation - speed, 0);
  156.             }
  157.         }
  158.        
  159.         //System.out.println(delay + "/" + maxDelay);
  160.        
  161.         int y = movement - (int)(((float)movement / 100) * animation);
  162.         float alpha = (((float)1 / 100) * animation);
  163.        
  164.         if (pointInBounds(mouseX, mouseY))
  165.         {
  166.             hoverAlpha = 255;
  167.         }
  168.         else
  169.         {
  170.             if (hoverAlpha > 0)
  171.             {
  172.                 int spd = 20;
  173.                 hoverAlpha -= Math.min(spd, hoverAlpha);
  174.             }
  175.         }
  176.        
  177.         GL11.glColor4f(1F, 1F, 1F, alpha);
  178.         GL11.glBindTexture(3553, textureID);
  179.         gui.drawTexturedModalRect(getX(), getY() - y, 0, 0, getWidth(), getHeight());
  180.         if (hoverAlpha > 0)
  181.         {
  182.             GL11.glColor4f(1F, 1F, 1F, (float)hoverAlpha/255);
  183.             GL11.glBindTexture(3553, textureID_hover);
  184.             gui.drawTexturedModalRect(getX(), getY() - y, 0, 0, getWidth(), getHeight());
  185.         }
  186.     }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement