Advertisement
TNT_Block

Cryptonic Buttons

Nov 5th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. package net.minecraft.client.gui;
  2.  
  3. import org.lwjgl.opengl.GL11;
  4.  
  5. import de.cryptonicdev.cryptonic.main.Cryptonic;
  6. import net.minecraft.client.Minecraft;
  7. import net.minecraft.client.audio.PositionedSoundRecord;
  8. import net.minecraft.client.audio.SoundHandler;
  9. import net.minecraft.client.renderer.GlStateManager;
  10. import net.minecraft.util.ResourceLocation;
  11.  
  12. public class GuiButton extends Gui
  13. {
  14. protected static final ResourceLocation buttonTextures = new ResourceLocation("textures/gui/widgets.png");
  15.  
  16. /** Button width in pixels */
  17. protected int width;
  18.  
  19. /** Button height in pixels */
  20. protected int height;
  21.  
  22. /** The x position of this control. */
  23. public int xPosition;
  24.  
  25. /** The y position of this control. */
  26. public int yPosition;
  27.  
  28. /** The string displayed on this control. */
  29. public String displayString;
  30. public int id;
  31.  
  32. /** True if this control is enabled, false to disable. */
  33. public boolean enabled;
  34.  
  35. /** Hides the button completely if false. */
  36. public boolean visible;
  37. protected boolean hovered;
  38.  
  39. public GuiButton(int buttonId, int x, int y, String buttonText)
  40. {
  41. this(buttonId, x, y, 200, 20, buttonText);
  42. }
  43.  
  44. public GuiButton(int buttonId, int x, int y, int widthIn, int heightIn, String buttonText)
  45. {
  46. this.width = 200;
  47. this.height = 20;
  48. this.enabled = true;
  49. this.visible = true;
  50. this.id = buttonId;
  51. this.xPosition = x;
  52. this.yPosition = y;
  53. this.width = widthIn;
  54. this.height = heightIn;
  55. this.displayString = buttonText;
  56. }
  57.  
  58. /**
  59. * Returns 0 if the button is disabled, 1 if the mouse is NOT hovering over this button and 2 if it IS hovering over
  60. * this button.
  61. */
  62. protected int getHoverState(boolean mouseOver)
  63. {
  64. int i = 1;
  65.  
  66. if (!this.enabled)
  67. {
  68. i = 0;
  69. }
  70. else if (mouseOver)
  71. {
  72. i = 2;
  73. }
  74.  
  75. return i;
  76. }
  77.  
  78. /**
  79. * Draws this button to the screen.
  80. */
  81. public void drawButton(Minecraft mc, int mouseX, int mouseY)
  82. {
  83. if (this.visible)
  84. {
  85. FontRenderer fontrenderer = mc.fontRendererObj;
  86. mc.getTextureManager().bindTexture(buttonTextures);
  87. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  88. this.hovered = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
  89. int i = this.getHoverState(this.hovered);
  90. GlStateManager.enableBlend();
  91. GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
  92. GlStateManager.blendFunc(770, 771);
  93. // this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + i * 20, this.width / 2, this.height);
  94. // this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);
  95.  
  96. int color = Cryptonic.INSTANCE.getClientColor(); // 0xffee3b3b
  97.  
  98. if (enabled && hovered) {
  99.  
  100. Gui.drawRect(xPosition, yPosition, xPosition + width, yPosition + height, 0x80303030);
  101. Gui.drawRect(xPosition, yPosition, xPosition + width, yPosition + 1, color);
  102. Gui.drawRect(xPosition, yPosition + height - 1, xPosition + width, yPosition + height, color);
  103. Gui.drawRect(xPosition, yPosition, xPosition + 1, yPosition + height, color);
  104. Gui.drawRect(xPosition + width - 1, yPosition, xPosition + width, yPosition + height, color);
  105. } else if (enabled) {
  106. Gui.drawRect(xPosition, yPosition, xPosition + width, yPosition + height, 0x80000000);
  107. Gui.drawRect(xPosition, yPosition, xPosition + width, yPosition + 0.5, color);
  108. Gui.drawRect(xPosition, yPosition + height - 0.5, xPosition + width, yPosition + height, color);
  109. Gui.drawRect(xPosition, yPosition, xPosition + 0.5, yPosition + height, color);
  110. Gui.drawRect(xPosition + width - 0.5, yPosition, xPosition + width, yPosition + height, color);
  111. } else {
  112. Gui.drawRect(xPosition, yPosition, xPosition + width, yPosition + height, 0x807f7f7f);
  113. Gui.drawRect(xPosition, yPosition, xPosition + width, yPosition + 1, color);
  114. Gui.drawRect(xPosition, yPosition + height - 1, xPosition + width, yPosition + height, color);
  115. Gui.drawRect(xPosition, yPosition, xPosition + 1, yPosition + height, color);
  116. Gui.drawRect(xPosition + width - 1, yPosition, xPosition + width, yPosition + height, color);
  117. }
  118.  
  119. this.mouseDragged(mc, mouseX, mouseY);
  120. this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height - 8) / 2, -1);
  121. }
  122. }
  123.  
  124. /**
  125. * Fired when the mouse button is dragged. Equivalent of MouseListener.mouseDragged(MouseEvent e).
  126. */
  127. protected void mouseDragged(Minecraft mc, int mouseX, int mouseY)
  128. {
  129. }
  130.  
  131. /**
  132. * Fired when the mouse button is released. Equivalent of MouseListener.mouseReleased(MouseEvent e).
  133. */
  134. public void mouseReleased(int mouseX, int mouseY)
  135. {
  136. }
  137.  
  138. /**
  139. * Returns true if the mouse has been pressed on this control. Equivalent of MouseListener.mousePressed(MouseEvent
  140. * e).
  141. */
  142. public boolean mousePressed(Minecraft mc, int mouseX, int mouseY)
  143. {
  144. return this.enabled && this.visible && mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
  145. }
  146.  
  147. /**
  148. * Whether the mouse cursor is currently over the button.
  149. */
  150. public boolean isMouseOver()
  151. {
  152. return this.hovered;
  153. }
  154.  
  155. public void drawButtonForegroundLayer(int mouseX, int mouseY)
  156. {
  157. }
  158.  
  159. public void playPressSound(SoundHandler soundHandlerIn)
  160. {
  161. soundHandlerIn.playSound(PositionedSoundRecord.create(new ResourceLocation("gui.button.press"), 1.0F));
  162. }
  163.  
  164. public int getButtonWidth()
  165. {
  166. return this.width;
  167. }
  168.  
  169. public void setWidth(int width)
  170. {
  171. this.width = width;
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement