aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/searchOverlay/OverlayScreen.java
blob: 9155a8946d92f21240baf3944800f0fe4ea16ee5 (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
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.client.item.TooltipContext;
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 java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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 int rowHeight = 20;
    private TextFieldWidget searchField;
    private ButtonWidget finishedButton;
    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().general.searchOverlay.maxSuggestions + 0.75 + SkyblockerConfigManager.get().general.searchOverlay.historyLength)) / 2);

        // Search field
        this.searchField = new TextFieldWidget(textRenderer,startX, startY, rowWidth - rowHeight, rowHeight, Text.literal("Search..."));
        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().general.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().general.searchOverlay.historyLength; //todo look different
        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;

                //set the tool tip
                String id = SearchOverManager.getHistoryId(i);
                if (id == null || id.isEmpty() || client == null) continue;
                ItemStack item = getItemStack(id);
                if (item == null) continue;
                MutableText tooltip = Text.literal("");
                item.getTooltip(client.player, TooltipContext.BASIC).forEach(line -> {
                    tooltip.append(line);
                    tooltip.append(Text.literal("\n"));
                });
                historyButtons[i].setTooltip(Tooltip.of(Text.of(tooltip)));
            }
            else {
                break;
            }
        }

        //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);

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

    /**
     * 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);
        if(historyButtons.length > 0  && historyButtons[0] != null){
            context.drawText(textRenderer, Text.translatable("text.autoconfig.skyblocker.option.general.searchOverlay.historyLabel")
                    , historyButtons[0].getX() + renderOffset, historyButtons[0].getY() - (rowHeight / 2), 0xFFFFFFFF, true);
        }

        //draw item stacks to buttons
        for (int i = 0; i < suggestionButtons.length; i++) {
            String id = SearchOverManager.getSuggestionId(i);
            if (id.isEmpty()) continue;
            ItemStack item = getItemStack(id);
            if (item == null) continue;
            context.drawItem(item,suggestionButtons[i].getX() + renderOffset,suggestionButtons[i].getY() + renderOffset);
        }
        for (int i = 0; i < historyButtons.length; i++) {
            String id = SearchOverManager.getHistoryId(i);
            if (id == null || id.isEmpty()) continue;
            ItemStack item = getItemStack(id);
            if (item == null) continue;
            context.drawItem(item,historyButtons[i].getX() + renderOffset,historyButtons[i].getY() + renderOffset);
        }
    }

    /**
     * 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();
    }

    /**
     * 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().general.searchOverlay.maxSuggestions; i++) {
            String text = SearchOverManager.getSuggestion(i);
            if (!text.isEmpty()){
                String text2 =  suggestionButtons[i].getMessage().getString();
                boolean isNewText = !text.equals(suggestionButtons[i].getMessage().getString());
                if (!isNewText) continue;
                suggestionButtons[i].visible = true;
                suggestionButtons[i].setMessage(Text.literal(text).setStyle(Style.EMPTY));

                //update the tool tip
                String id = SearchOverManager.getSuggestionId(i);
                if (id.isEmpty() || client == null) {
                    suggestionButtons[i].setTooltip(null);
                    continue;
                }
                ItemStack item = getItemStack(id);
                if (item == null) {
                    suggestionButtons[i].setTooltip(null);
                    continue;
                }
                MutableText tooltip = Text.literal("");
                item.getTooltip(client.player, TooltipContext.BASIC).forEach(line -> {
                    tooltip.append(line);
                    tooltip.append(Text.literal("\n"));
                });
                suggestionButtons[i].setTooltip(Tooltip.of(Text.of(tooltip)));
            }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) {
        super.keyPressed(keyCode,scanCode,modifiers);
        if (keyCode == GLFW.GLFW_KEY_ENTER && searchField.isActive()){
            close();
            return true;
        }
        return false;
    }
}