aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/config/gui/MooConfigPreview.java
blob: 675c392edc570e1ce72da478ea8e42a4295d5d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package de.cowtipper.cowlection.config.gui;

import de.cowtipper.cowlection.config.MooConfig;
import de.cowtipper.cowlection.data.DataHelper;
import de.cowtipper.cowlection.util.MooChatComponent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiUtilRenderComponents;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.*;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.config.GuiUtils;
import org.apache.commons.lang3.text.WordUtils;

import java.util.HashMap;
import java.util.Map;

public class MooConfigPreview {
    private final Type type;
    private ItemStack[] items;
    private IChatComponent chatComponent;
    private Runnable previewRunnable;
    private long nextPreviewAt;
    public static ItemStack hoveredItem;
    public static IChatComponent hoveredChatComponent;
    public static MooConfigGui parent;

    public MooConfigPreview(IChatComponent chatComponent) {
        this.type = Type.CHAT;
        this.chatComponent = chatComponent;
    }

    public MooConfigPreview(ItemStack... items) {
        this.type = Type.ITEM;
        this.items = items;
    }

    public MooConfigPreview(PreviewChatRunnable previewRunnable) {
        this.type = Type.RUNNABLE;
        this.previewRunnable = previewRunnable;
    }

    public MooConfigPreview(String sound, float volume, float pitch) {
        this.type = Type.RUNNABLE_ON_HOVER;
        previewRunnable = () -> {
            if (Minecraft.getMinecraft().thePlayer != null) {
                Minecraft.getMinecraft().thePlayer.playSound(sound, volume, pitch);
            }
        };
    }

    public MooConfigPreview(DataHelper.DungeonClass dungeonClass) {
        this(new MooConfigPreview.PreviewChatRunnable() {
            @Override
            public IChatComponent getText() {
                String markWith;
                switch (MooConfig.filterDungPartiesWithDupes(dungeonClass)) {
                    case ALWAYS:
                        markWith = "" + EnumChatFormatting.WHITE + dungeonClass.getShortName();
                        break;
                    case SPECIAL:
                        markWith = EnumChatFormatting.GOLD + "²⁺" + EnumChatFormatting.YELLOW + dungeonClass.getShortName();
                        break;
                    default: // disabled/"do not mark":
                        markWith = "" + EnumChatFormatting.DARK_GRAY + EnumChatFormatting.ITALIC + "not marked";
                }
                return new MooChatComponent("Marked with: " + markWith).gray();
            }
        });
    }

    public void drawPreview(int x, int y, int mouseX, int mouseY, boolean enablePreview) {
        switch (type) {
            case ITEM:
                drawItemsPreview(x, y, mouseX, mouseY, enablePreview);
                break;
            case CHAT:
                drawChatPreview(x, y, mouseX, mouseY, enablePreview);
                break;
            case RUNNABLE:
                drawButtonWithRunnablePreview(x, y, mouseX, mouseY, false);
                break;
            case RUNNABLE_ON_HOVER:
                drawButtonWithRunnablePreview(x, y, mouseX, mouseY, true);
                break;
            default:
                // do nothing
                break;
        }
    }

    public static void drawPreviewHover(int mouseX, int mouseY) {
        if (hoveredItem != null) {
            // draw preview item tool tip
            parent.renderToolTip(hoveredItem, mouseX, mouseY);
            hoveredItem = null;
        } else if (hoveredChatComponent != null) {
            // draw hover event of hovered chat component
            parent.handleComponentHover(hoveredChatComponent, mouseX, mouseY);
            hoveredChatComponent = null;
        }
    }

    private void drawItemsPreview(int x, int yFakeHotbar, int mouseX, int mouseY, boolean enablePreview) {
        int xFakeHotbar = x + 15;
        GlStateManager.pushMatrix();
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        Minecraft.getMinecraft().getTextureManager().bindTexture(new ResourceLocation("textures/gui/container/inventory.png"));

        GuiUtils.drawTexturedModalRect(xFakeHotbar, yFakeHotbar, 87, 25, 18 * items.length, 18, 0);

        GlStateManager.enableRescaleNormal();
        GlStateManager.enableBlend();
        GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
        RenderHelper.enableGUIStandardItemLighting();

        int xItem = xFakeHotbar + 1;
        int yItem = yFakeHotbar + 1;
        for (ItemStack item : items) {
            Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(item, xItem, yItem);

            // check mouse hover to draw item tooltip
            if (enablePreview && mouseX >= xItem - 1 && mouseX < xItem + 18 - 1 && mouseY >= yItem - 1 && mouseY < yItem + 18 - 1) {
                hoveredItem = item;
                GlStateManager.disableLighting();
                GlStateManager.disableDepth();
                GlStateManager.colorMask(true, true, true, false);
                // draw white slot hover
                Gui.drawRect(xItem, yItem, xItem + 16, yItem + 16, 0x80FFFFFF);
                GlStateManager.colorMask(true, true, true, true);
                GlStateManager.enableDepth();
                GlStateManager.enableLighting();
            }
            xItem += 18;
        }
        RenderHelper.disableStandardItemLighting();
        GlStateManager.disableRescaleNormal();
        GlStateManager.disableBlend();
        GlStateManager.popMatrix();
    }

    public static ItemStack createDemoItem(String itemId, String itemName, String[] lore, Map<String, NBTBase> extraAttributes) {
        return createDemoItem(itemId, itemName, lore, extraAttributes, -1);
    }

    public static ItemStack createDemoItem(String itemId, String itemName, String[] lore, Map<String, NBTBase> extraAttributes, int color) {
        ItemStack demoItem = new ItemStack(Item.getByNameOrId(itemId));

        // lore
        NBTTagCompound display = new NBTTagCompound();
        demoItem.setTagInfo("display", display);
        NBTTagList loreTag = new NBTTagList();
        display.setTag("Lore", loreTag);
        for (String loreEntry : lore) {
            loreTag.appendTag(new NBTTagString(loreEntry));
        }
        // color
        if (color >= 0) {
            display.setInteger("color", color);
        }
        // SkyBlock extra attributes
        NBTTagCompound extraAttributesTag = new NBTTagCompound();
        demoItem.setTagInfo("ExtraAttributes", extraAttributesTag);
        for (Map.Entry<String, NBTBase> extraAttribute : extraAttributes.entrySet()) {
            extraAttributesTag.setTag(extraAttribute.getKey(), extraAttribute.getValue());
        }
        demoItem.setStackDisplayName(itemName);

        return demoItem;
    }

    public static ItemStack createDungeonItem(String modifier, String timestamp, String... lore) {
        Map<String, NBTBase> extraAttributes = new HashMap<>();
        extraAttributes.put("modifier", new NBTTagString(modifier));
        extraAttributes.put("baseStatBoostPercentage", new NBTTagInt(48));
        extraAttributes.put("item_tier", new NBTTagInt(1));
        extraAttributes.put("id", new NBTTagString("SKELETON_SOLDIER_LEGGINGS"));
        extraAttributes.put("timestamp", new NBTTagString(timestamp));


        return createDemoItem("leather_leggings", EnumChatFormatting.DARK_PURPLE + WordUtils.capitalize(modifier) + " Skeleton Soldier Leggings",
                lore, extraAttributes, 16759819);
    }

    private void drawChatPreview(int x, int y, int mouseX, int mouseY, boolean enablePreview) {
        FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
        int currentX = x + 15;
        int chatY = y + 20 / 2 - fontRenderer.FONT_HEIGHT / 2;

        fontRenderer.drawStringWithShadow(this.chatComponent.getFormattedText(), currentX, chatY, 0xffffffff);
        if (!enablePreview) {
            Gui.drawRect(currentX - 2, chatY - 2, currentX + fontRenderer.getStringWidth(chatComponent.getUnformattedText()) + 1, chatY + fontRenderer.FONT_HEIGHT + 1, 0xdd444444);
        }

        // hover checker for online best friends (partially taken from GuiNewChat#getChatComponent)
        if (enablePreview && mouseY >= chatY && mouseY <= chatY + fontRenderer.FONT_HEIGHT) {
            for (IChatComponent chatComponent : chatComponent) {
                if (chatComponent instanceof ChatComponentText) {
                    currentX += fontRenderer.getStringWidth(GuiUtilRenderComponents.func_178909_a(chatComponent.getUnformattedTextForChat(), false));
                    if (currentX > mouseX) {
                        hoveredChatComponent = chatComponent;
                        break;
                    }
                }
            }
        }
    }

    public static IChatComponent createDemoOnline(String name, String gameMode, String onlineTime) {
        return new MooChatComponent(name).darkGreen().setHover(new MooChatComponent(gameMode).yellow().appendFreshSibling(new MooChatComponent("Online for " + onlineTime).white()));
    }

    private void drawButtonWithRunnablePreview(int x, int y, int mouseX, int mouseY, boolean checkHover) {
        FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
        int currentX = x + 15;
        int chatY = y + 20 / 2 - fontRenderer.FONT_HEIGHT / 2;

        if (previewRunnable instanceof PreviewChatRunnable) {
            ((PreviewChatRunnable) previewRunnable).setPos(x, y);
            previewRunnable.run();
        } else {
            String previewText = "(hover me to preview)";
            fontRenderer.drawStringWithShadow(previewText, currentX, chatY, 0xffffffff);
            if (checkHover && nextPreviewAt < Minecraft.getSystemTime()
                    && mouseY >= chatY && mouseY <= chatY + fontRenderer.FONT_HEIGHT
                    && mouseX > currentX && mouseX < currentX + fontRenderer.getStringWidth(previewText)) {
                nextPreviewAt = Minecraft.getSystemTime() + 3000;
                previewRunnable.run();
            }
        }
    }

    public abstract static class PreviewChatRunnable implements Runnable {
        private int x;
        private int y;

        public void setPos(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public abstract IChatComponent getText();

        @Override
        public void run() {
            FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
            int currentX = x + 15;
            int chatY = y + 20 / 2 - fontRenderer.FONT_HEIGHT / 2;

            fontRenderer.drawStringWithShadow(getText().getFormattedText(), currentX, chatY, 0xffffffff);
        }
    }

    private enum Type {
        CHAT, ITEM, RUNNABLE, RUNNABLE_ON_HOVER
    }
}