|
| 1 | +/* |
| 2 | + * This file is part of Cubic World Generation, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) 2015-2020 contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component; |
| 25 | + |
| 26 | +import java.util.Map.Entry; |
| 27 | +import java.util.function.Consumer; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.DummyWorld; |
| 31 | +import io.github.opencubicchunks.cubicchunks.cubicgen.preset.wrapper.BlockStateDesc; |
| 32 | +import net.minecraft.client.gui.GuiButton; |
| 33 | +import net.minecraft.client.renderer.BufferBuilder; |
| 34 | +import net.minecraft.client.renderer.RenderHelper; |
| 35 | +import org.lwjgl.opengl.GL11; |
| 36 | + |
| 37 | +import net.minecraft.block.state.IBlockState; |
| 38 | +import net.minecraft.client.Minecraft; |
| 39 | +import net.minecraft.client.renderer.GlStateManager; |
| 40 | +import net.minecraft.client.renderer.Tessellator; |
| 41 | +import net.minecraft.client.renderer.texture.ITextureObject; |
| 42 | +import net.minecraft.client.renderer.texture.TextureMap; |
| 43 | +import net.minecraft.client.renderer.tileentity.TileEntityItemStackRenderer; |
| 44 | +import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; |
| 45 | +import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; |
| 46 | +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; |
| 47 | +import net.minecraft.client.renderer.vertex.VertexFormat; |
| 48 | +import net.minecraft.item.ItemStack; |
| 49 | +import net.minecraft.tileentity.TileEntity; |
| 50 | +import net.minecraft.util.math.BlockPos; |
| 51 | + |
| 52 | +public class CwgGuiBlockStateButton extends GuiButton { |
| 53 | + |
| 54 | + public static final int SIZE = 24; |
| 55 | + private String tooltip; |
| 56 | + private BlockStateDesc blockState; |
| 57 | + private Consumer<CwgGuiBlockStateButton> onClick; |
| 58 | + private Consumer<CwgGuiBlockStateButton> onRightClick; |
| 59 | + |
| 60 | + public CwgGuiBlockStateButton(BlockStateDesc blockState) { |
| 61 | + super(0, 0, 0, SIZE, SIZE, ""); |
| 62 | + this.blockState = blockState; |
| 63 | + this.tooltip = generateTooltip(this.blockState); |
| 64 | + } |
| 65 | + |
| 66 | + public void onClick(Consumer<CwgGuiBlockStateButton> action) { |
| 67 | + this.onClick = action; |
| 68 | + } |
| 69 | + |
| 70 | + private static String generateTooltip(BlockStateDesc blockState) { |
| 71 | + StringBuffer sb = new StringBuffer(128); |
| 72 | + sb.append(blockState.getBlockId()); |
| 73 | + for (Entry<String, String> entry : blockState.getProperties().entrySet()) { |
| 74 | + sb.append(" \n "); |
| 75 | + sb.append(entry.getKey()); |
| 76 | + sb.append(" = "); |
| 77 | + sb.append(entry.getValue()); |
| 78 | + } |
| 79 | + return sb.toString(); |
| 80 | + } |
| 81 | + |
| 82 | + public BlockStateDesc getBlockState() { |
| 83 | + return blockState; |
| 84 | + } |
| 85 | + |
| 86 | + public void setBlockState(BlockStateDesc iBlockState1) { |
| 87 | + blockState = iBlockState1; |
| 88 | + tooltip = generateTooltip(blockState); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean mousePressed(Minecraft mc, int x, int y) { |
| 93 | + onClick.accept(this); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTick) { |
| 99 | + if (!visible || blockState == null || blockState.getBlockState() == null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + this.hovered = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height; |
| 103 | + |
| 104 | + IBlockState blockstate = blockState.getBlockState(); |
| 105 | + RenderHelper.disableStandardItemLighting(); |
| 106 | + GlStateManager.enableDepth(); |
| 107 | + GlStateManager.enableRescaleNormal(); |
| 108 | + |
| 109 | + BufferBuilder buffer = Tessellator.getInstance().getBuffer(); |
| 110 | + ITextureObject blockTexture = Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.LOCATION_BLOCKS_TEXTURE); |
| 111 | + GL11.glBindTexture(GL11.GL_TEXTURE_2D, blockTexture.getGlTextureId()); |
| 112 | + VertexFormat format = DefaultVertexFormats.BLOCK; |
| 113 | + GlStateManager.pushMatrix(); |
| 114 | + GlStateManager.translate(this.x, this.y + 16f, 100.0F); |
| 115 | + GlStateManager.scale(12.0F, 12.0F, -12.0F); |
| 116 | + GlStateManager.rotate(210.0F, 1.0F, 0.0F, 0.0F); |
| 117 | + GlStateManager.rotate(45.0F, 0.0F, 1.0F, 0.0F); |
| 118 | + buffer.begin(GL11.GL_QUADS, format); |
| 119 | + Minecraft.getMinecraft().getBlockRendererDispatcher().renderBlock(blockstate, BlockPos.ORIGIN, |
| 120 | + DummyWorld.getInstanceWithBlockState(blockstate), buffer); |
| 121 | + Tessellator.getInstance().draw(); |
| 122 | + if (blockstate.getBlock().hasTileEntity(blockstate)) { |
| 123 | + TileEntity te = blockstate.getBlock().createTileEntity(null, blockstate); |
| 124 | + if (te != null) { |
| 125 | + TileEntitySpecialRenderer<TileEntity> tileentityspecialrenderer = |
| 126 | + TileEntityRendererDispatcher.instance.getRenderer(te); |
| 127 | + if (tileentityspecialrenderer != null) { |
| 128 | + TileEntityItemStackRenderer.instance.renderByItem(new ItemStack(blockstate.getBlock())); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + GlStateManager.popMatrix(); |
| 133 | + GlStateManager.disableRescaleNormal(); |
| 134 | + |
| 135 | + GlStateManager.enableDepth(); |
| 136 | + GlStateManager.enableLighting(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override public void drawButtonForegroundLayer(int mouseX, int mouseY) { |
| 140 | + if (hovered) { |
| 141 | + Minecraft.getMinecraft().currentScreen.drawHoveringText(tooltip, mouseX, mouseY); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public String getBlockName() { |
| 146 | + return this.blockState.getBlockId(); |
| 147 | + } |
| 148 | + |
| 149 | + public String getBlockProperties() { |
| 150 | + return blockState.getProperties().entrySet().stream() |
| 151 | + .map(e -> e.getKey() + "=" + e.getValue()) |
| 152 | + .collect(Collectors.joining(",", "[", "]")); |
| 153 | + } |
| 154 | +} |
0 commit comments