aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/searchoverlay/OverlayScreen.java
blob: 0850c80aebf76051ba0ab7d443fc948584736f58 (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
274
275
package de.hysky.skyblocker.skyblock.searchoverlay;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.item.ItemStack;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;

import static de.hysky.skyblocker.skyblock.itemlist.ItemRepository.getItemStack;

public class OverlayScreen extends Screen {

    protected static final Identifier SEARCH_ICON_TEXTURE = new Identifier("icon/search");
    private static final Identifier BACKGROUND_TEXTURE = new Identifier("social_interactions/background");
    private static final int rowHeight = 20;
    private TextFieldWidget searchField;
    private ButtonWidget finishedButton;
    private ButtonWidget maxPetButton;
    private ButtonWidget dungeonStarButton;
    private ButtonWidget[] suggestionButtons;
    private ButtonWidget[] historyButtons;

    public OverlayScreen(Text title) {
        super(title);
    }

    /**
     * Creates the layout for the overlay screen.
     */
    @Override
    protected void init() {
        super.init();
        int rowWidth = (int) (this.width * 0.4);
        int startX = (int) (this.width * 0.5) - rowWidth / 2;
        int startY = (int) ((int) (this.height * 0.5) - (rowHeight * (1 + SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.maxSuggestions + 0.75 + SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.historyLength)) / 2);

        // Search field
        this.searchField = new TextFieldWidget(textRenderer, startX, startY, rowWidth - rowHeight, rowHeight, Text.translatable("gui.recipebook.search_hint"));
        searchField.setText(SearchOverManager.search);
        searchField.setChangedListener(SearchOverManager::updateSearch);
        searchField.setMaxLength(30);

        // finish buttons
        finishedButton = ButtonWidget.builder(Text.literal("").setStyle(Style.EMPTY.withColor(Formatting.GREEN)), a -> close())
                .position(startX + rowWidth - rowHeight, startY)
                .size(rowHeight, rowHeight).build();


        // suggested item buttons
        int rowOffset = rowHeight;
        int totalSuggestions = SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.maxSuggestions;
        this.suggestionButtons = new ButtonWidget[totalSuggestions];
        for (int i = 0; i < totalSuggestions; i++) {
            suggestionButtons[i] = ButtonWidget.builder(Text.literal(SearchOverManager.getSuggestion(i)).setStyle(Style.EMPTY), a -> {
                        SearchOverManager.updateSearch(a.getMessage().getString());
                        close();
                    })
                    .position(startX, startY + rowOffset)
                    .size(rowWidth, rowHeight).build();
            suggestionButtons[i].visible = false;
            rowOffset += rowHeight;
        }
        // history item buttons
        rowOffset += (int) (rowHeight * 0.75);
        int historyLength = SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.historyLength;
        this.historyButtons = new ButtonWidget[historyLength];
        for (int i = 0; i < historyLength; i++) {
            String text = SearchOverManager.getHistory(i);
            if (text != null) {
                historyButtons[i] = ButtonWidget.builder(Text.literal(text).setStyle(Style.EMPTY), (a) -> {
                            SearchOverManager.updateSearch(a.getMessage().getString());
                            close();
                        })
                        .position(startX, startY + rowOffset)
                        .size(rowWidth, rowHeight).build();
                rowOffset += rowHeight;
            } else {
                break;
            }
        }
        //auction only elements
        if (SearchOverManager.isAuction) {
            //max pet level button
            maxPetButton = ButtonWidget.builder(Text.literal("temp"), a -> {
                        SearchOverManager.maxPetLevel = !SearchOverManager.maxPetLevel;
                        updateMaxPetText();
                    })
                    .tooltip(Tooltip.of(Text.translatable("skyblocker.config.general.searchOverlay.maxPet.@Tooltip")))
                    .position(startX, startY - rowHeight - 8)
                    .size(rowWidth / 2, rowHeight).build();
            updateMaxPetText();

            //dungeon star input
            dungeonStarButton = ButtonWidget.builder(Text.literal("✪"), a -> updateStars())
                    .tooltip(Tooltip.of(Text.translatable("skyblocker.config.general.searchOverlay.starsTooltip")))
                    .position(startX + (int) (rowWidth * 0.5), startY - rowHeight - 8)
                    .size(rowWidth / 2, rowHeight).build();

            updateStars();
        }

        //add drawables in order to make tab navigation sensible
        addDrawableChild(searchField);
        for (ButtonWidget suggestion : suggestionButtons) {
            addDrawableChild(suggestion);
        }
        for (ButtonWidget historyOption : historyButtons) {
            if (historyOption != null) {
                addDrawableChild(historyOption);
            }
        }
        addDrawableChild(finishedButton);

        if (SearchOverManager.isAuction) {
            addDrawableChild(maxPetButton);
            addDrawableChild(dungeonStarButton);

        }

        //focus the search box
        this.setInitialFocus(searchField);
    }

    /**
     * finds if the mouse is clicked on the dungeon star button and if so works out amount of stars to set
     *
     * @param mouseX the X coordinate of the mouse
     * @param mouseY the Y coordinate of the mouse
     * @param button the mouse button number
     * @return super
     */
    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        if (SearchOverManager.isAuction && dungeonStarButton.isHovered() && client != null) {
            double actualTextWidth = client.textRenderer.getWidth(dungeonStarButton.getMessage());
            double textOffset = (dungeonStarButton.getWidth() - actualTextWidth) / 2;
            double offset = mouseX - (dungeonStarButton.getX() + textOffset);
            int starCount = (int) ((offset / actualTextWidth) * 10);
            starCount = Math.clamp(0, starCount + 1, 10);
            //if same as old value set stars to 0 else set to selected amount
            if (starCount == SearchOverManager.dungeonStars) {
                SearchOverManager.dungeonStars = 0;
            } else {
                SearchOverManager.dungeonStars = starCount;
            }
        }

        return super.mouseClicked(mouseX, mouseY, button);
    }

    private void updateMaxPetText() {
        if (SearchOverManager.maxPetLevel) {
            maxPetButton.setMessage(Text.translatable("skyblocker.config.general.searchOverlay.maxPet").append(Text.literal(" ✔")).formatted(Formatting.GREEN));
        } else {
            maxPetButton.setMessage(Text.translatable("skyblocker.config.general.searchOverlay.maxPet").append(Text.literal(" ❌")).formatted(Formatting.RED));
        }
    }

    private void updateStars() {
        MutableText stars = Text.empty();
        for (int i = 0; i < SearchOverManager.dungeonStars; i++) {
            stars.append(Text.literal("✪").formatted(Formatting.GREEN));
        }
        for (int i = SearchOverManager.dungeonStars; i < 10; i++) {
            stars.append(Text.literal("✪"));
        }
        dungeonStarButton.setMessage(stars);
    }

    @Override
    public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) {
        super.renderBackground(context, mouseX, mouseY, delta);
        //find max height
        int maxHeight = rowHeight * (1 + suggestionButtons.length + historyButtons.length);
        if (historyButtons.length > 0) { //add space for history label if it could exist
            maxHeight += (int) (rowHeight * 0.75);
        }
        context.drawGuiTexture(BACKGROUND_TEXTURE, searchField.getX() - 8, searchField.getY() - 8, (int) (this.width * 0.4) + 16, maxHeight + 16);
    }

    /**
     * Renders the search icon, label for the history and item Stacks for item names
     */
    @Override
    public void render(DrawContext context, int mouseX, int mouseY, float delta) {
        super.render(context, mouseX, mouseY, delta);
        int renderOffset = (rowHeight - 16) / 2;
        context.drawGuiTexture(SEARCH_ICON_TEXTURE, finishedButton.getX() + renderOffset, finishedButton.getY() + renderOffset, 16, 16);
        //labels
        if (historyButtons.length > 0 && historyButtons[0] != null) {
            context.drawText(textRenderer, Text.translatable("skyblocker.config.general.searchOverlay.historyLabel"), historyButtons[0].getX() + renderOffset, historyButtons[0].getY() - rowHeight / 2, 0xFFFFFFFF, true);
        }

        //draw item stacks and tooltip to buttons
        for (int i = 0; i < suggestionButtons.length; i++) {
            drawItemAndTooltip(context, mouseX, mouseY, SearchOverManager.getSuggestionId(i), suggestionButtons[i], renderOffset);
        }
        for (int i = 0; i < historyButtons.length; i++) {
            drawItemAndTooltip(context, mouseX, mouseY, SearchOverManager.getHistoryId(i), historyButtons[i], renderOffset);
        }
    }

    /**
     * Draws the item and tooltip for the given button
     */
    private void drawItemAndTooltip(DrawContext context, int mouseX, int mouseY, String id, ButtonWidget button, int renderOffset) {
        if (id == null || id.isEmpty()) return;
        ItemStack item = getItemStack(id);
        if (item == null) return;
        context.drawItem(item, button.getX() + renderOffset, button.getY() + renderOffset);

        // Draw tooltip
        if (button.isMouseOver(mouseX, mouseY)) {
            context.drawItemTooltip(textRenderer, item, mouseX, mouseY);
        }
    }

    /**
     * updates if the suggestions buttons should be visible based on if they have a value
     */
    @Override
    public final void tick() {
        super.tick();
        //update suggestion buttons text
        for (int i = 0; i < SkyblockerConfigManager.get().uiAndVisuals.searchOverlay.maxSuggestions; i++) {
            String text = SearchOverManager.getSuggestion(i);
            if (!text.isEmpty()) {
                suggestionButtons[i].visible = true;

                boolean isNewText = !text.equals(suggestionButtons[i].getMessage().getString());
                if (!isNewText) continue;

                suggestionButtons[i].setMessage(Text.literal(text).setStyle(Style.EMPTY));
            } else {
                suggestionButtons[i].visible = false;
            }
        }
    }

    /**
     * When a key is pressed. If enter key pressed and search box selected close
     */
    @Override
    public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
        if (keyCode == GLFW.GLFW_KEY_ENTER && searchField.isActive()) {
            close();
            return true;
        }
        return super.keyPressed(keyCode, scanCode, modifiers);
    }

    @Override
    public boolean shouldPause() {
        return false;
    }

    /**
     * Closes the overlay screen and gets the manager to send a packet update about the sign
     */
    @Override
    public void close() {
        assert this.client != null;
        assert this.client.player != null;
        SearchOverManager.pushSearch();
        super.close();
    }
}