Advertisement
TNT_Block

GuiSlot

Nov 7th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.51 KB | None | 0 0
  1. package net.minecraft.client.gui;
  2.  
  3. import net.minecraft.client.Minecraft;
  4. import net.minecraft.client.renderer.GlStateManager;
  5. import net.minecraft.client.renderer.Tessellator;
  6. import net.minecraft.client.renderer.WorldRenderer;
  7. import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
  8. import net.minecraft.util.MathHelper;
  9. import net.minecraft.util.ResourceLocation;
  10.  
  11. import org.lwjgl.input.Mouse;
  12.  
  13. import de.cryptonicdev.cryptonic.main.Cryptonic;
  14. import de.cryptonicdev.cryptonic.utils.RenderUtils;
  15.  
  16. public abstract class GuiSlot {
  17. protected final Minecraft mc;
  18. protected int width;
  19. protected int height;
  20.  
  21. /** The top of the slot container. Affects the overlays and scrolling. */
  22. protected int top;
  23.  
  24. /** The bottom of the slot container. Affects the overlays and scrolling. */
  25. protected int bottom;
  26. protected int right;
  27. protected int left;
  28.  
  29. /** The height of a slot. */
  30. protected final int slotHeight;
  31.  
  32. /** The buttonID of the button used to scroll up */
  33. private int scrollUpButtonID;
  34.  
  35. /** The buttonID of the button used to scroll down */
  36. private int scrollDownButtonID;
  37. protected int mouseX;
  38. protected int mouseY;
  39. protected boolean field_148163_i = true;
  40.  
  41. /** Where the mouse was in the window when you first clicked to scroll */
  42. protected int initialClickY = -2;
  43.  
  44. /**
  45. * What to multiply the amount you moved your mouse by (used for slowing down
  46. * scrolling when over the items and not on the scroll bar)
  47. */
  48. protected float scrollMultiplier;
  49.  
  50. /** How far down this slot has been scrolled */
  51. protected float amountScrolled;
  52.  
  53. /** The element in the list that was selected */
  54. protected int selectedElement = -1;
  55.  
  56. /** The time when this button was last clicked. */
  57. protected long lastClicked;
  58. protected boolean field_178041_q = true;
  59.  
  60. /**
  61. * Set to true if a selected element in this gui will show an outline box
  62. */
  63. protected boolean showSelectionBox = true;
  64. protected boolean hasListHeader;
  65. protected int headerPadding;
  66. private boolean enabled = true;
  67.  
  68. public GuiSlot(Minecraft mcIn, int width, int height, int topIn, int bottomIn, int slotHeightIn) {
  69. this.mc = mcIn;
  70. this.width = width;
  71. this.height = height;
  72. this.top = topIn;
  73. this.bottom = bottomIn;
  74. this.slotHeight = slotHeightIn;
  75. this.left = 0;
  76. this.right = width;
  77. }
  78.  
  79. public void setDimensions(int widthIn, int heightIn, int topIn, int bottomIn) {
  80. this.width = widthIn;
  81. this.height = heightIn;
  82. this.top = topIn;
  83. this.bottom = bottomIn;
  84. this.left = 0;
  85. this.right = widthIn;
  86. }
  87.  
  88. public void setShowSelectionBox(boolean showSelectionBoxIn) {
  89. this.showSelectionBox = showSelectionBoxIn;
  90. }
  91.  
  92. /**
  93. * Sets hasListHeader and headerHeight. Params: hasListHeader, headerHeight. If
  94. * hasListHeader is false headerHeight is set to 0.
  95. */
  96. protected void setHasListHeader(boolean hasListHeaderIn, int headerPaddingIn) {
  97. this.hasListHeader = hasListHeaderIn;
  98. this.headerPadding = headerPaddingIn;
  99.  
  100. if (!hasListHeaderIn) {
  101. this.headerPadding = 0;
  102. }
  103. }
  104.  
  105. protected abstract int getSize();
  106.  
  107. /**
  108. * The element in the slot that was clicked, boolean for whether it was double
  109. * clicked or not
  110. */
  111. protected abstract void elementClicked(int slotIndex, boolean isDoubleClick, int mouseX, int mouseY);
  112.  
  113. /**
  114. * Returns true if the element passed in is currently selected
  115. */
  116. protected abstract boolean isSelected(int slotIndex);
  117.  
  118. /**
  119. * Return the height of the content being scrolled
  120. */
  121. protected int getContentHeight() {
  122. return this.getSize() * this.slotHeight + this.headerPadding;
  123. }
  124.  
  125. protected abstract void drawBackground();
  126.  
  127. protected void func_178040_a(int p_178040_1_, int p_178040_2_, int p_178040_3_) {
  128. }
  129.  
  130. protected abstract void drawSlot(int entryID, int p_180791_2_, int p_180791_3_, int p_180791_4_, int mouseXIn,
  131. int mouseYIn);
  132.  
  133. /**
  134. * Handles drawing a list's header row.
  135. */
  136. protected void drawListHeader(int p_148129_1_, int p_148129_2_, Tessellator p_148129_3_) {
  137. }
  138.  
  139. protected void func_148132_a(int p_148132_1_, int p_148132_2_) {
  140. }
  141.  
  142. protected void func_148142_b(int p_148142_1_, int p_148142_2_) {
  143. }
  144.  
  145. public int getSlotIndexFromScreenCoords(int p_148124_1_, int p_148124_2_) {
  146. int i = this.left + this.width / 2 - this.getListWidth() / 2;
  147. int j = this.left + this.width / 2 + this.getListWidth() / 2;
  148. int k = p_148124_2_ - this.top - this.headerPadding + (int) this.amountScrolled - 4;
  149. int l = k / this.slotHeight;
  150. return p_148124_1_ < this.getScrollBarX() && p_148124_1_ >= i && p_148124_1_ <= j && l >= 0 && k >= 0
  151. && l < this.getSize() ? l : -1;
  152. }
  153.  
  154. /**
  155. * Registers the IDs that can be used for the scrollbar's up/down buttons.
  156. */
  157. public void registerScrollButtons(int scrollUpButtonIDIn, int scrollDownButtonIDIn) {
  158. this.scrollUpButtonID = scrollUpButtonIDIn;
  159. this.scrollDownButtonID = scrollDownButtonIDIn;
  160. }
  161.  
  162. /**
  163. * Stop the thing from scrolling out of bounds
  164. */
  165. protected void bindAmountScrolled() {
  166. this.amountScrolled = MathHelper.clamp_float(this.amountScrolled, 0.0F, (float) this.func_148135_f());
  167. }
  168.  
  169. public int func_148135_f() {
  170. return Math.max(0, this.getContentHeight() - (this.bottom - this.top - 4));
  171. }
  172.  
  173. /**
  174. * Returns the amountScrolled field as an integer.
  175. */
  176. public int getAmountScrolled() {
  177. return (int) this.amountScrolled;
  178. }
  179.  
  180. public boolean isMouseYWithinSlotBounds(int p_148141_1_) {
  181. return p_148141_1_ >= this.top && p_148141_1_ <= this.bottom && this.mouseX >= this.left
  182. && this.mouseX <= this.right;
  183. }
  184.  
  185. /**
  186. * Scrolls the slot by the given amount. A positive value scrolls down, and a
  187. * negative value scrolls up.
  188. */
  189. public void scrollBy(int amount) {
  190. this.amountScrolled += (float) amount;
  191. this.bindAmountScrolled();
  192. this.initialClickY = -2;
  193. }
  194.  
  195. public void actionPerformed(GuiButton button) {
  196. if (button.enabled) {
  197. if (button.id == this.scrollUpButtonID) {
  198. this.amountScrolled -= (float) (this.slotHeight * 2 / 3);
  199. this.initialClickY = -2;
  200. this.bindAmountScrolled();
  201. } else if (button.id == this.scrollDownButtonID) {
  202. this.amountScrolled += (float) (this.slotHeight * 2 / 3);
  203. this.initialClickY = -2;
  204. this.bindAmountScrolled();
  205. }
  206. }
  207. }
  208.  
  209. public void drawScreen(int mouseXIn, int mouseYIn, float p_148128_3_) {
  210. if (this.field_178041_q) {
  211. this.mouseX = mouseXIn;
  212. this.mouseY = mouseYIn;
  213. ScaledResolution res = new ScaledResolution(mc);
  214. mc.getTextureManager().bindTexture(new ResourceLocation("cryptonic/background.jpg"));
  215. Gui.drawScaledCustomSizeModalRect(0, 0, 0F, 0F, res.getScaledWidth(), res.getScaledHeight(),
  216. res.getScaledWidth(), res.getScaledHeight(), res.getScaledWidth(), res.getScaledHeight());
  217. int i = this.getScrollBarX();
  218. int j = i + 6;
  219. this.bindAmountScrolled();
  220. GlStateManager.disableLighting();
  221. GlStateManager.disableFog();
  222. Tessellator tessellator = Tessellator.getInstance();
  223. WorldRenderer worldrenderer = tessellator.getWorldRenderer();
  224. this.mc.getTextureManager().bindTexture(Gui.optionsBackground);
  225. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  226. int k = this.left + this.width / 2 - this.getListWidth() / 2 + 2;
  227. int l = this.top + 4 - (int) this.amountScrolled;
  228.  
  229. if (this.hasListHeader) {
  230. this.drawListHeader(k, l, tessellator);
  231. }
  232.  
  233. this.drawSelectionBox(k, l, mouseXIn, mouseYIn);
  234. GlStateManager.disableDepth();
  235. int i1 = 4;
  236. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  237. mc.getTextureManager().bindTexture(new ResourceLocation("cryptonic/background.jpg"));
  238. Gui.drawModalRectWithCustomSizedTexture(left, bottom, 0F, 0 - (res.getScaledHeight() - bottom), width,
  239. height, width, height);
  240. Gui.drawRect(left, bottom, width, height, 0x40000000);
  241. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  242. mc.getTextureManager().bindTexture(new ResourceLocation("cryptonic/background.jpg"));
  243. Gui.drawModalRectWithCustomSizedTexture(left, 0, 0F, 0F, width, top, res.getScaledWidth(),
  244. res.getScaledHeight());
  245. Gui.drawRect(left, 0, right, top, 0x40000000);
  246. GlStateManager.enableBlend();
  247. GlStateManager.tryBlendFuncSeparate(770, 771, 0, 1);
  248. GlStateManager.disableAlpha();
  249. GlStateManager.shadeModel(7425);
  250. GlStateManager.disableTexture2D();
  251. int j1 = this.func_148135_f();
  252. if (j1 > 0) {
  253. int k1 = (this.bottom - this.top) * (this.bottom - this.top) / this.getContentHeight();
  254. k1 = MathHelper.clamp_int(k1, 32, this.bottom - this.top - 8);
  255. int l1 = (int) this.amountScrolled * (this.bottom - this.top - k1) / j1 + this.top;
  256.  
  257. if (l1 < this.top) {
  258. l1 = this.top;
  259. }
  260. worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
  261. worldrenderer.pos((double) i, (double) this.bottom, 0.0D).tex(0.0D, 1.0D).color(0, 0, 0, 255)
  262. .endVertex();
  263. worldrenderer.pos((double) j, (double) this.bottom, 0.0D).tex(1.0D, 1.0D).color(0, 0, 0, 255)
  264. .endVertex();
  265. worldrenderer.pos((double) j, (double) this.top, 0.0D).tex(1.0D, 0.0D).color(0, 0, 0, 255).endVertex();
  266. worldrenderer.pos((double) i, (double) this.top, 0.0D).tex(0.0D, 0.0D).color(0, 0, 0, 255).endVertex();
  267. tessellator.draw();
  268. Gui.drawRect(i, l1, j, l1 + k1, 0xffababab);
  269. }
  270.  
  271. this.func_148142_b(mouseXIn, mouseYIn);
  272. GlStateManager.enableTexture2D();
  273. GlStateManager.shadeModel(7424);
  274. GlStateManager.enableAlpha();
  275. GlStateManager.disableBlend();
  276. }
  277. }
  278.  
  279. public void handleMouseInput() {
  280. if (this.isMouseYWithinSlotBounds(this.mouseY)) {
  281. if (Mouse.getEventButton() == 0 && Mouse.getEventButtonState() && this.mouseY >= this.top
  282. && this.mouseY <= this.bottom) {
  283. int i = (this.width - this.getListWidth()) / 2;
  284. int j = (this.width + this.getListWidth()) / 2;
  285. int k = this.mouseY - this.top - this.headerPadding + (int) this.amountScrolled - 4;
  286. int l = k / this.slotHeight;
  287.  
  288. if (l < this.getSize() && this.mouseX >= i && this.mouseX <= j && l >= 0 && k >= 0) {
  289. this.elementClicked(l, false, this.mouseX, this.mouseY);
  290. this.selectedElement = l;
  291. } else if (this.mouseX >= i && this.mouseX <= j && k < 0) {
  292. this.func_148132_a(this.mouseX - i, this.mouseY - this.top + (int) this.amountScrolled - 4);
  293. }
  294. }
  295.  
  296. if (Mouse.isButtonDown(0) && this.getEnabled()) {
  297. if (this.initialClickY == -1) {
  298. boolean flag1 = true;
  299.  
  300. if (this.mouseY >= this.top && this.mouseY <= this.bottom) {
  301. int j2 = (this.width - this.getListWidth()) / 2;
  302. int k2 = (this.width + this.getListWidth()) / 2;
  303. int l2 = this.mouseY - this.top - this.headerPadding + (int) this.amountScrolled - 4;
  304. int i1 = l2 / this.slotHeight;
  305.  
  306. if (i1 < this.getSize() && this.mouseX >= j2 && this.mouseX <= k2 && i1 >= 0 && l2 >= 0) {
  307. boolean flag = i1 == this.selectedElement
  308. && Minecraft.getSystemTime() - this.lastClicked < 250L;
  309. this.elementClicked(i1, flag, this.mouseX, this.mouseY);
  310. this.selectedElement = i1;
  311. this.lastClicked = Minecraft.getSystemTime();
  312. } else if (this.mouseX >= j2 && this.mouseX <= k2 && l2 < 0) {
  313. this.func_148132_a(this.mouseX - j2,
  314. this.mouseY - this.top + (int) this.amountScrolled - 4);
  315. flag1 = false;
  316. }
  317.  
  318. int i3 = this.getScrollBarX();
  319. int j1 = i3 + 6;
  320.  
  321. if (this.mouseX >= i3 && this.mouseX <= j1) {
  322. this.scrollMultiplier = -1.0F;
  323. int k1 = this.func_148135_f();
  324.  
  325. if (k1 < 1) {
  326. k1 = 1;
  327. }
  328.  
  329. int l1 = (int) ((float) ((this.bottom - this.top) * (this.bottom - this.top))
  330. / (float) this.getContentHeight());
  331. l1 = MathHelper.clamp_int(l1, 32, this.bottom - this.top - 8);
  332. this.scrollMultiplier /= (float) (this.bottom - this.top - l1) / (float) k1;
  333. } else {
  334. this.scrollMultiplier = 1.0F;
  335. }
  336.  
  337. if (flag1) {
  338. this.initialClickY = this.mouseY;
  339. } else {
  340. this.initialClickY = -2;
  341. }
  342. } else {
  343. this.initialClickY = -2;
  344. }
  345. } else if (this.initialClickY >= 0) {
  346. this.amountScrolled -= (float) (this.mouseY - this.initialClickY) * this.scrollMultiplier;
  347. this.initialClickY = this.mouseY;
  348. }
  349. } else {
  350. this.initialClickY = -1;
  351. }
  352.  
  353. int i2 = Mouse.getEventDWheel();
  354.  
  355. if (i2 != 0) {
  356. if (i2 > 0) {
  357. i2 = -1;
  358. } else if (i2 < 0) {
  359. i2 = 1;
  360. }
  361.  
  362. this.amountScrolled += (float) (i2 * this.slotHeight / 2);
  363. }
  364. }
  365. }
  366.  
  367. public void setEnabled(boolean enabledIn) {
  368. this.enabled = enabledIn;
  369. }
  370.  
  371. public boolean getEnabled() {
  372. return this.enabled;
  373. }
  374.  
  375. /**
  376. * Gets the width of the list
  377. */
  378. public int getListWidth() {
  379. return 220;
  380. }
  381.  
  382. /**
  383. * Draws the selection box around the selected slot element.
  384. */
  385. protected void drawSelectionBox(int p_148120_1_, int p_148120_2_, int mouseXIn, int mouseYIn) {
  386. int i = this.getSize();
  387. Tessellator tessellator = Tessellator.getInstance();
  388. WorldRenderer worldrenderer = tessellator.getWorldRenderer();
  389.  
  390. for (int j = 0; j < i; ++j) {
  391. int k = p_148120_2_ + j * this.slotHeight + this.headerPadding;
  392. int l = this.slotHeight - 4;
  393.  
  394. if (k > this.bottom || k + l < this.top) {
  395. this.func_178040_a(j, p_148120_1_, k);
  396. }
  397.  
  398. if (this.showSelectionBox && this.isSelected(j)) {
  399. int i1 = this.left + (this.width / 2 - this.getListWidth() / 2);
  400. int j1 = this.left + this.width / 2 + this.getListWidth() / 2;
  401. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  402. int left = i1;
  403. int top = k - 2;
  404. int right = j1;
  405. int bottom = k + l + 2;
  406. Gui.drawRect(left, top, right, bottom, 0x60ababab);
  407. int color = Cryptonic.INSTANCE.getClientColor();
  408. Gui.drawRect(left, top, right, top + 1, color);
  409. Gui.drawRect(left, bottom - 1, right, bottom, color);
  410. Gui.drawRect(left, top, left + 1, bottom, color);
  411. Gui.drawRect(right - 1, top, right, bottom, color);
  412. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  413. }
  414.  
  415. this.drawSlot(j, p_148120_1_, k, l, mouseXIn, mouseYIn);
  416. }
  417. }
  418.  
  419. protected int getScrollBarX() {
  420. return this.width / 2 + 124;
  421. }
  422.  
  423. /**
  424. * Overlays the background to hide scrolled items
  425. */
  426. protected void overlayBackground(int startY, int endY, int startAlpha, int endAlpha) {
  427. Tessellator tessellator = Tessellator.getInstance();
  428. WorldRenderer worldrenderer = tessellator.getWorldRenderer();
  429. this.mc.getTextureManager().bindTexture(Gui.optionsBackground);
  430. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  431. float f = 32.0F;
  432. worldrenderer.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
  433. worldrenderer.pos((double) this.left, (double) endY, 0.0D).tex(0.0D, (double) ((float) endY / 32.0F))
  434. .color(64, 64, 64, endAlpha).endVertex();
  435. worldrenderer.pos((double) (this.left + this.width), (double) endY, 0.0D)
  436. .tex((double) ((float) this.width / 32.0F), (double) ((float) endY / 32.0F)).color(64, 64, 64, endAlpha)
  437. .endVertex();
  438. worldrenderer.pos((double) (this.left + this.width), (double) startY, 0.0D)
  439. .tex((double) ((float) this.width / 32.0F), (double) ((float) startY / 32.0F))
  440. .color(64, 64, 64, startAlpha).endVertex();
  441. worldrenderer.pos((double) this.left, (double) startY, 0.0D).tex(0.0D, (double) ((float) startY / 32.0F))
  442. .color(64, 64, 64, startAlpha).endVertex();
  443. tessellator.draw();
  444. }
  445.  
  446. /**
  447. * Sets the left and right bounds of the slot. Param is the left bound, right is
  448. * calculated as left + width.
  449. */
  450. public void setSlotXBoundsFromLeft(int leftIn) {
  451. this.left = leftIn;
  452. this.right = leftIn + this.width;
  453. }
  454.  
  455. public int getSlotHeight() {
  456. return this.slotHeight;
  457. }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement