aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/config/gui/MooConfigGui.java
blob: aff5315de6cde4e2279dafc90427dafd7646c973 (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
265
266
267
268
269
270
271
272
273
package de.cowtipper.cowlection.config.gui;

import de.cowtipper.cowlection.Cowlection;
import de.cowtipper.cowlection.config.MooConfig;
import de.cowtipper.cowlection.config.MooConfigCategory;
import de.cowtipper.cowlection.listener.PlayerListener;
import de.cowtipper.cowlection.util.GuiHelper;
import net.minecraft.client.audio.SoundEventAccessorComposite;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StringUtils;
import org.lwjgl.input.Keyboard;

import java.io.IOException;
import java.util.Arrays;

/**
 * Main config gui containing:
 * <ul>
 * <li>menu ({@link MooConfigMenuList}) with list of config categories ({@link MooConfigCategory})</li>
 * <li>the current opened config category with its sub-categories ({@link MooConfigCategoryScrolling})</li>
 * </ul>
 * Based on {@link net.minecraft.client.gui.GuiControls}
 */
public class MooConfigGui extends GuiScreen {
    public static long showDungeonPerformanceOverlayUntil;
    private static final String searchPlaceholder = "" + EnumChatFormatting.GRAY + EnumChatFormatting.ITALIC + "Search config";
    public int menuWidth;
    private MooConfigMenuList menu;
    private int selectedMenuIndex = -1;
    private MooConfigCategory currentConfigCategory;
    private final boolean isOutsideOfSkyBlock;
    /**
     * equivalent of GuiModList.keyBindingList
     */
    private MooConfigCategoryScrolling currentConfigCategoryGui;
    private GuiButton btnClose;
    private GuiTextField fieldSearchQuery;
    private String searchQuery;

    public MooConfigGui(String searchQuery) {
        isOutsideOfSkyBlock = PlayerListener.registerSkyBlockListeners();
        this.searchQuery = searchQuery;
    }

    /**
     * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
     * window resizes, the buttonList is cleared beforehand.
     */
    @Override
    public void initGui() {
        Keyboard.enableRepeatEvents(true);
        MooConfigPreview.parent = this;

        // re-register SkyBlock listeners if necessary (mainly so that previews function correctly outside of SkyBlock)
        PlayerListener.registerSkyBlockListeners();

        for (MooConfigCategory configCategory : MooConfig.getConfigCategories()) {
            menuWidth = Math.max(menuWidth, fontRendererObj.getStringWidth(configCategory.getMenuDisplayName()) + 10 + 2);
        }
        menuWidth = Math.min(menuWidth, 150);
        this.menu = new MooConfigMenuList(this, menuWidth);

        this.buttonList.add(this.btnClose = new GuiButton(6, this.width - 25, 3, 22, 20, EnumChatFormatting.RED + "X"));
        this.fieldSearchQuery = new GuiTextField(42, this.fontRendererObj, 5, 11, 100, 15);
        this.fieldSearchQuery.setMaxStringLength(42);
        this.fieldSearchQuery.setText(searchPlaceholder);
        if (selectedMenuIndex == -1) {
            // no category selected yet: this isn't a resize
            if (!StringUtils.isNullOrEmpty(searchQuery)) {
                this.fieldSearchQuery.setText(searchQuery);
                // switch to search
                searchConfigEntries();
            } else {
                // switch to 1st category
                selectConfigCategory(0);
            }
        } else if (selectedMenuIndex == -42) {
            this.fieldSearchQuery.setText(searchQuery);
        }
    }

    @Override
    public void handleMouseInput() throws IOException {
        super.handleMouseInput();
        if (currentConfigCategoryGui != null) {
            this.currentConfigCategoryGui.handleMouseInput();
        }
    }

    /**
     * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
     */
    @Override
    protected void actionPerformed(GuiButton button) throws IOException {
        if (button.enabled) {
            if (button.id == 6) { // close gui
                this.mc.displayGuiScreen(null);
            }
        }
    }

    /**
     * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
     */
    @Override
    protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
        if (mouseButton != 0 || (currentConfigCategoryGui != null && !this.currentConfigCategoryGui.mouseClicked(mouseX, mouseY, mouseButton))) {
            super.mouseClicked(mouseX, mouseY, mouseButton);
            fieldSearchQuery.mouseClicked(mouseX, mouseY, mouseButton);
            if (fieldSearchQuery.isFocused()) {
                if (mouseButton == /* right click */ 1
                        || (mouseButton == /* left click */ 0 && searchPlaceholder.equals(fieldSearchQuery.getText()))) {
                    // clear search query
                    fieldSearchQuery.setText("");
                    searchConfigEntries();
                }
            }
        }
    }

    /**
     * Called when a mouse button is released.  Args : mouseX, mouseY, releaseButton
     */
    protected void mouseReleased(int mouseX, int mouseY, int state) {
        if (state != 0 || currentConfigCategoryGui != null && !this.currentConfigCategoryGui.mouseReleased(mouseX, mouseY, state)) {
            super.mouseReleased(mouseX, mouseY, state);
        }
    }

    /**
     * Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
     * KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
     */
    @Override
    protected void keyTyped(char typedChar, int keyCode) throws IOException {
        if (keyCode == Keyboard.KEY_ESCAPE && (currentConfigCategoryGui == null || !currentConfigCategoryGui.isModifyingKeyBind())) {
            super.keyTyped(typedChar, keyCode);
        } else if (this.currentConfigCategoryGui != null) {
            if (this.fieldSearchQuery.isFocused() && this.fieldSearchQuery.textboxKeyTyped(typedChar, keyCode)) {
                String queryText = fieldSearchQuery.getText();
                if (queryText.length() >= 3) {
                    String soundName = "mob." + queryText + ".say";
                    SoundEventAccessorComposite sound = mc.getSoundHandler().getSound(new ResourceLocation(soundName));
                    if (sound != null) {
                        mc.thePlayer.playSound(soundName, 0.5f, 1);
                    }
                }
                searchConfigEntries();
            } else {
                this.currentConfigCategoryGui.keyTyped(typedChar, keyCode);
            }
        }
    }

    private void searchConfigEntries() {
        searchQuery = this.fieldSearchQuery.getText();
        // display search page if not already displayed
        if (!selectConfigCategory(-42)) {
            // was already searching before, update search results
            this.currentConfigCategoryGui.showFilteredConfigEntries(searchQuery);
        }
    }

    /**
     * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
     */
    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        if (!showDungeonPerformanceOverlay()) {
            this.menu.drawScreen(mouseX, mouseY, partialTicks);
        }

        String guiTitle = "" + EnumChatFormatting.BOLD + EnumChatFormatting.UNDERLINE + Cowlection.MODNAME + " config:" + EnumChatFormatting.RESET + " " + (currentConfigCategory != null ? currentConfigCategory.getDisplayName() : "Search");
        int guiTitleX = ((menu.getRight() + this.width) / 2) - this.fontRendererObj.getStringWidth(guiTitle) / 2;
        this.drawCenteredString(this.fontRendererObj, guiTitle, guiTitleX, 16, 0xFFFFFF);
        super.drawScreen(mouseX, mouseY, partialTicks);

        if (currentConfigCategoryGui != null) {
            currentConfigCategoryGui.drawScreen(mouseX, mouseY, partialTicks);
        }
        if (btnClose.isMouseOver()) {
            GuiHelper.drawHoveringText(Arrays.asList(EnumChatFormatting.RED + "Save & close settings", "" + EnumChatFormatting.GRAY + EnumChatFormatting.ITALIC + "Hint:" + EnumChatFormatting.RESET + " alternatively press ESC"), mouseX, mouseY, width, height, 300);
        }
        fieldSearchQuery.drawTextBox();
    }

    @Override
    public void updateScreen() {
        fieldSearchQuery.updateCursorCounter();
    }

    @Override
    public void drawDefaultBackground() {
        if (!MooConfigGui.showDungeonPerformanceOverlay()) {
            super.drawDefaultBackground();
        }
    }

    // config category menu methods:

    /**
     * Select a config category via the menu
     */
    public boolean selectConfigCategory(int index) {
        if (index == this.selectedMenuIndex) {
            return false;
        }
        this.selectedMenuIndex = index;
        if (index >= 0 && index <= MooConfig.getConfigCategories().size()) {
            this.currentConfigCategory = MooConfig.getConfigCategories().get(selectedMenuIndex);
            // reset search to placeholder
            this.fieldSearchQuery.setText(searchPlaceholder);
            this.searchQuery = "";
        } else {
            // show search results
            this.currentConfigCategory = null;
        }

        switchDisplayedConfigCategory();
        Cowlection.getInstance().getConfig().syncFromGui();
        return true;
    }

    /**
     * Helper method for menu: is config category selected?
     */
    public boolean isConfigCategorySelected(int index) {
        return index == selectedMenuIndex;
    }

    public void switchDisplayedConfigCategory() {
        if (currentConfigCategory == null) {
            // display search
            currentConfigCategoryGui = new MooConfigCategoryScrolling(this, mc, searchQuery, menu.getRight() + 3);
        } else {
            // display one category
            currentConfigCategoryGui = new MooConfigCategoryScrolling(this, mc, currentConfigCategory, menu.getRight() + 3);
        }
    }

    @Override
    public void renderToolTip(ItemStack stack, int x, int y) {
        super.renderToolTip(stack, x, y);
    }

    @Override
    public void handleComponentHover(IChatComponent component, int x, int y) {
        super.handleComponentHover(component, x, y);
    }

    @Override
    public void onGuiClosed() {
        Keyboard.enableRepeatEvents(false);
        Cowlection.getInstance().getConfig().syncFromGui();

        if (isOutsideOfSkyBlock) {
            PlayerListener.unregisterSkyBlockListeners();
        }
        if (MooConfigPreview.parent != null) {
            MooConfigPreview.parent = null;
        }
    }

    public static boolean showDungeonPerformanceOverlay() {
        return showDungeonPerformanceOverlayUntil > System.currentTimeMillis();
    }
}