|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Azercoco & Technici4n |
| 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 all |
| 14 | + * 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 THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package aztech.modern_industrialization.items.client; |
| 25 | + |
| 26 | +import aztech.modern_industrialization.MITooltips; |
| 27 | +import aztech.modern_industrialization.blocks.structure.member.StructureMemberMode; |
| 28 | +import aztech.modern_industrialization.blocks.structure.member.StructureMultiblockMemberBlockItem; |
| 29 | +import aztech.modern_industrialization.machines.multiblocks.HatchType; |
| 30 | +import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StateStructureMemberTest; |
| 31 | +import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StructureMemberTest; |
| 32 | +import aztech.modern_industrialization.util.RenderHelper; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Locale; |
| 36 | +import net.minecraft.ChatFormatting; |
| 37 | +import net.minecraft.client.gui.Font; |
| 38 | +import net.minecraft.client.gui.GuiGraphics; |
| 39 | +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; |
| 40 | +import net.minecraft.client.renderer.MultiBufferSource; |
| 41 | +import net.minecraft.network.chat.Component; |
| 42 | +import net.minecraft.world.item.ItemStack; |
| 43 | +import org.joml.Matrix4f; |
| 44 | + |
| 45 | +// TODO SWEDZ: there has to be a better way to align all the text and stuff. maybe some kind of builder? |
| 46 | +public final class ClientStructureMemberBlockTooltip implements ClientTooltipComponent { |
| 47 | + private static final int TEXT_ROW_HEIGHT = 9; |
| 48 | + private static final int TEXT_BEFORE_IMAGE_ROW_HEIGHT = TEXT_ROW_HEIGHT + 4; |
| 49 | + private static final int IMAGE_ROW_HEIGHT = 20; |
| 50 | + |
| 51 | + private final StructureMultiblockMemberBlockItem.TooltipData data; |
| 52 | + |
| 53 | + private final ItemStack preview; |
| 54 | + private final List<ItemStack> members; |
| 55 | + |
| 56 | + public ClientStructureMemberBlockTooltip(StructureMultiblockMemberBlockItem.TooltipData data) { |
| 57 | + this.data = data; |
| 58 | + |
| 59 | + preview = data.preview().getBlock().asItem().getDefaultInstance(); |
| 60 | + |
| 61 | + members = new ArrayList<>(); |
| 62 | + for (StructureMemberTest member : data.members()) { |
| 63 | + if (member instanceof StateStructureMemberTest stateTest) { |
| 64 | + members.add(stateTest.blockState().getBlock().asItem().getDefaultInstance()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public int getHeight() { |
| 71 | + int height = 0; |
| 72 | + |
| 73 | + height += TEXT_ROW_HEIGHT; |
| 74 | + |
| 75 | + if (renderName()) { |
| 76 | + height += TEXT_ROW_HEIGHT; |
| 77 | + } |
| 78 | + |
| 79 | + if (renderCasing()) { |
| 80 | + height += TEXT_ROW_HEIGHT; |
| 81 | + } |
| 82 | + |
| 83 | + if (renderPreview()) { |
| 84 | + height += TEXT_BEFORE_IMAGE_ROW_HEIGHT; |
| 85 | + height += IMAGE_ROW_HEIGHT; |
| 86 | + } |
| 87 | + |
| 88 | + if (renderMembers()) { |
| 89 | + height += TEXT_BEFORE_IMAGE_ROW_HEIGHT; |
| 90 | + height += IMAGE_ROW_HEIGHT; |
| 91 | + } |
| 92 | + |
| 93 | + if (renderHatch()) { |
| 94 | + height += TEXT_ROW_HEIGHT; |
| 95 | + for (HatchType hatchType : HatchType.values()) { |
| 96 | + if (data.hatchFlags().allows(hatchType)) { |
| 97 | + height += TEXT_ROW_HEIGHT; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return height; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int getWidth(Font font) { |
| 107 | + // TODO SWEDZ: get actual width |
| 108 | + return 18 * 6; |
| 109 | + } |
| 110 | + |
| 111 | + private void renderRowImage(List<ItemStack> stacks, Font font, int x, int y, GuiGraphics graphics) { |
| 112 | + int i = 0; |
| 113 | + for (var stack : stacks) { |
| 114 | + RenderHelper.renderAndDecorateItem(graphics, font, stack, x + i * 18, y); |
| 115 | + if (++i >= 5) { |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void renderRowImageText(int count, Font font, int x, int y, Matrix4f matrix, MultiBufferSource.BufferSource buffer) { |
| 122 | + if (count >= 6) { |
| 123 | + font.drawInBatch(Component.literal("+ ...").withStyle(MITooltips.DEFAULT_STYLE), x + 18 * 5, y + 5, -1, true, matrix, buffer, |
| 124 | + Font.DisplayMode.NORMAL, 0, 0xF000F0); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void renderRowText(Component text, Font font, int x, int y, Matrix4f matrix, MultiBufferSource.BufferSource buffer) { |
| 129 | + font.drawInBatch(text, x, y, -1, true, matrix, buffer, Font.DisplayMode.NORMAL, 0, 0xF000F0); |
| 130 | + } |
| 131 | + |
| 132 | + private boolean renderName() { |
| 133 | + return data.mode() == StructureMemberMode.VARIABLE; |
| 134 | + } |
| 135 | + |
| 136 | + private boolean renderPreview() { |
| 137 | + return data.mode() == StructureMemberMode.SIMPLE || data.mode() == StructureMemberMode.HATCH; |
| 138 | + } |
| 139 | + |
| 140 | + private boolean renderMembers() { |
| 141 | + return data.mode() == StructureMemberMode.SIMPLE || data.mode() == StructureMemberMode.HATCH; |
| 142 | + } |
| 143 | + |
| 144 | + private boolean renderCasing() { |
| 145 | + return data.mode() == StructureMemberMode.HATCH; |
| 146 | + } |
| 147 | + |
| 148 | + private boolean renderHatch() { |
| 149 | + return data.mode() == StructureMemberMode.HATCH && data.hatchFlags().flags != 0; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void renderImage(Font font, int x, int y, GuiGraphics graphics) { |
| 154 | + int lineY = y; |
| 155 | + |
| 156 | + lineY += TEXT_ROW_HEIGHT; |
| 157 | + |
| 158 | + if (renderName()) { |
| 159 | + lineY += TEXT_ROW_HEIGHT; |
| 160 | + } |
| 161 | + |
| 162 | + if (renderCasing()) { |
| 163 | + lineY += TEXT_ROW_HEIGHT; |
| 164 | + } |
| 165 | + |
| 166 | + if (renderPreview()) { |
| 167 | + lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT; |
| 168 | + renderRowImage(List.of(preview), font, x, lineY, graphics); |
| 169 | + lineY += IMAGE_ROW_HEIGHT; |
| 170 | + } |
| 171 | + |
| 172 | + if (renderMembers()) { |
| 173 | + lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT; |
| 174 | + renderRowImage(members, font, x, lineY, graphics); |
| 175 | + lineY += IMAGE_ROW_HEIGHT; |
| 176 | + } |
| 177 | + |
| 178 | + if (renderHatch()) { |
| 179 | + lineY += TEXT_ROW_HEIGHT; |
| 180 | + for (HatchType hatchType : HatchType.values()) { |
| 181 | + if (data.hatchFlags().allows(hatchType)) { |
| 182 | + lineY += TEXT_ROW_HEIGHT; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void renderText(Font font, int x, int y, Matrix4f matrix, MultiBufferSource.BufferSource buffer) { |
| 190 | + int lineY = y; |
| 191 | + |
| 192 | + // TODO SWEDZ: use translations |
| 193 | + renderRowText(Component.literal("Mode: ").append(data.mode().text()), font, x, lineY, matrix, buffer); |
| 194 | + lineY += TEXT_ROW_HEIGHT; |
| 195 | + |
| 196 | + if (renderName()) { |
| 197 | + renderRowText(Component.literal("Name: %s".formatted(data.name())), font, x, lineY, matrix, buffer); |
| 198 | + lineY += TEXT_ROW_HEIGHT; |
| 199 | + } |
| 200 | + |
| 201 | + if (renderCasing()) { |
| 202 | + renderRowText(Component.literal("Casing: %s".formatted(data.casing().key.getPath())), font, x, lineY, matrix, buffer); |
| 203 | + lineY += TEXT_ROW_HEIGHT; |
| 204 | + } |
| 205 | + |
| 206 | + if (renderPreview()) { |
| 207 | + renderRowText(Component.literal("Preview").withStyle(ChatFormatting.UNDERLINE), font, x, lineY, matrix, buffer); |
| 208 | + lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT; |
| 209 | + lineY += IMAGE_ROW_HEIGHT; |
| 210 | + } |
| 211 | + |
| 212 | + if (renderMembers()) { |
| 213 | + renderRowText(Component.literal("Members").withStyle(ChatFormatting.UNDERLINE), font, x, lineY, matrix, buffer); |
| 214 | + lineY += TEXT_BEFORE_IMAGE_ROW_HEIGHT; |
| 215 | + renderRowImageText(members.size(), font, x, lineY, matrix, buffer); |
| 216 | + lineY += IMAGE_ROW_HEIGHT; |
| 217 | + } |
| 218 | + |
| 219 | + if (renderHatch()) { |
| 220 | + renderRowText(Component.literal("Hatches").withStyle(ChatFormatting.UNDERLINE), font, x, lineY, matrix, buffer); |
| 221 | + lineY += TEXT_ROW_HEIGHT; |
| 222 | + for (HatchType hatchType : HatchType.values()) { |
| 223 | + if (data.hatchFlags().allows(hatchType)) { |
| 224 | + renderRowText(Component.literal("- %s".formatted(hatchType.name().toLowerCase(Locale.ROOT))), font, x, lineY, matrix, buffer); |
| 225 | + lineY += TEXT_ROW_HEIGHT; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments