aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/gui/modularui/uifactory/SelectItemUIFactory.java
blob: 535263dd00ffa1d893a483b10cd839bb984f1a13 (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
package gregtech.common.gui.modularui.uifactory;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Supplier;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;

import com.gtnewhorizons.modularui.api.GlStateManager;
import com.gtnewhorizons.modularui.api.ModularUITextures;
import com.gtnewhorizons.modularui.api.drawable.GuiHelper;
import com.gtnewhorizons.modularui.api.drawable.IDrawable;
import com.gtnewhorizons.modularui.api.drawable.ItemDrawable;
import com.gtnewhorizons.modularui.api.drawable.Text;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.api.widget.Widget;
import com.gtnewhorizons.modularui.common.widget.ButtonWidget;
import com.gtnewhorizons.modularui.common.widget.DrawableWidget;
import com.gtnewhorizons.modularui.common.widget.TextWidget;

import gregtech.api.enums.Dyes;
import gregtech.api.gui.GUIColorOverride;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.util.GTUtil;
import gregtech.api.util.GTUtility;

/**
 * Creates UI for selecting item from given list. This is client-only UI to allow using client-preferred settings.
 */
public class SelectItemUIFactory {

    private final String header;
    private final ItemStack headerItem;
    public static final int UNSELECTED = -1;
    private static final int cols = 9;
    private final Consumer<ItemStack> selectedCallback;
    // passed in stack
    private final List<ItemStack> stacks;
    private final boolean noDeselect;
    private int selected;
    private boolean anotherWindow = false;
    private AtomicBoolean dialogOpened;
    private int guiTint = GTUtil.getRGBInt(Dyes.MACHINE_METAL.getRGBA());
    private Supplier<ItemStack> currentGetter;

    private final GUIColorOverride colorOverride = GUIColorOverride.get("SelectItemUIFactory");

    private int getTextColorOrDefault(String textType, int defaultColor) {
        return colorOverride.getTextColorOrDefault(textType, defaultColor);
    }

    private final Supplier<Integer> COLOR_TITLE = () -> getTextColorOrDefault("title", 0x222222);
    private final Supplier<Integer> COLOR_TEXT_GRAY = () -> getTextColorOrDefault("text_gray", 0x555555);

    public SelectItemUIFactory(String header, ItemStack headerItem, Consumer<ItemStack> selectedCallback,
        List<ItemStack> stacks) {
        this(header, headerItem, selectedCallback, stacks, UNSELECTED);
    }

    public SelectItemUIFactory(String header, ItemStack headerItem, Consumer<ItemStack> selectedCallback,
        List<ItemStack> stacks, int selected) {
        this(header, headerItem, selectedCallback, stacks, selected, false);
    }

    /**
     * Constructor for a dialog to select an item from given list. Given callback may be called zero or more times
     * depending on user action.
     *
     * @param header           Header text
     * @param headerItem       ItemStack to use as Dialog icon
     * @param selectedCallback callback upon selected
     * @param stacks           list to choose from
     * @param selected         preselected item. Use {@link #UNSELECTED} for unselected. Invalid selected will be
     *                         clamped to 0 or highest index
     * @param noDeselect       true if player cannot deselect, false otherwise. If this is set to true, selectedCallback
     *                         is guaranteed to be called with a nonnull stack
     */
    public SelectItemUIFactory(String header, ItemStack headerItem, Consumer<ItemStack> selectedCallback,
        List<ItemStack> stacks, int selected, boolean noDeselect) {
        this.header = header;
        this.headerItem = headerItem;
        this.selectedCallback = selectedCallback;
        this.stacks = stacks;
        this.noDeselect = noDeselect;
        this.selected = noDeselect ? Math.max(0, selected) : selected;
    }

    /**
     * @param anotherWindow If UI is shown on top of another window
     * @param dialogOpened  Flag to store whether this UI is opened and hence it should block duplicated creation of
     *                      this UI
     */
    public SelectItemUIFactory setAnotherWindow(boolean anotherWindow, AtomicBoolean dialogOpened) {
        this.anotherWindow = anotherWindow;
        this.dialogOpened = dialogOpened;
        return this;
    }

    public SelectItemUIFactory setGuiTint(int guiTint) {
        this.guiTint = guiTint;
        return this;
    }

    /**
     * @param currentGetter Getter for "current" item displayed that may change from external reasons
     */
    public SelectItemUIFactory setCurrentGetter(Supplier<ItemStack> currentGetter) {
        this.currentGetter = currentGetter;
        return this;
    }

    public ModularWindow createWindow(UIBuildContext buildContext) {
        ModularWindow.Builder builder = ModularWindow
            .builder(getGUIWidth(), 53 + 18 * ((stacks.size() - 1) / cols + 1));
        builder.setBackground(ModularUITextures.VANILLA_BACKGROUND);
        builder.setGuiTint(guiTint);

        if (headerItem != null) {
            builder.widget(
                new ItemDrawable(headerItem).asWidget()
                    .setPos(5, 5)
                    .setSize(16, 16));
        }
        builder.widget(
            new TextWidget(header).setDefaultColor(COLOR_TITLE.get())
                .setPos(25, 9));

        int currentSlotX = 9
            + getFontRenderer().getStringWidth(StatCollector.translateToLocal("GT5U.gui.select.current"));
        int currentSlotY = 24;
        builder.widget(new DrawableWidget() {

            @Override
            public void onScreenUpdate() {
                super.onScreenUpdate();
                if (currentGetter != null) {
                    ItemStack current = currentGetter.get();
                    selected = GTUtility.findMatchingStackInList(stacks, current);
                }
            }
        }.setDrawable(GTUITextures.SLOT_DARK_GRAY)
            .setPos(currentSlotX, currentSlotY)
            .setSize(18, 18))
            .widget(
                new ItemDrawable(() -> getCandidate(getSelected())).asWidgetWithTooltip()
                    .setPos(currentSlotX + 1, currentSlotY + 1));
        builder.widget(
            new TextWidget(StatCollector.translateToLocal("GT5U.gui.select.current"))
                .setDefaultColor(COLOR_TEXT_GRAY.get())
                .setPos(8, 25 + (18 - getFontRenderer().FONT_HEIGHT) / 2));

        for (int i = 0; i < stacks.size(); i++) {
            final int index = i;
            builder.widget(new ButtonWidget() {

                @Override
                public void draw(float partialTicks) {
                    GlStateManager.pushMatrix();
                    // so that item z levels are properly ordered
                    GlStateManager.translate(0, 0, 150 * getWindowLayer());
                    new ItemDrawable(stacks.get(index)).draw(1, 1, 16, 16, partialTicks);
                    GlStateManager.popMatrix();
                }
            }.setOnClick((clickData, widget) -> {
                if (clickData.mouseButton == 0) {
                    setSelected(index, widget);
                } else {
                    setSelected(UNSELECTED, widget);
                }
                selectedCallback.accept(getCandidate(getSelected()));
            })
                .setSynced(false, false)
                .dynamicTooltip(() -> getItemTooltips(index))
                .setUpdateTooltipEveryTick(true)
                .setBackground(
                    () -> new IDrawable[] {
                        index == selected ? GTUITextures.SLOT_DARK_GRAY : ModularUITextures.ITEM_SLOT, })
                .setPos(7 + 18 * (index % cols), 43 + 18 * (index / cols))
                .setSize(18, 18));
        }

        if (anotherWindow) {
            dialogOpened.set(true);
            builder.widget(new ButtonWidget() {

                @Override
                public void onDestroy() {
                    dialogOpened.set(false);
                }
            }.setOnClick(
                (clickData, widget) -> widget.getWindow()
                    .tryClose())
                .setSynced(false, false)
                .setBackground(ModularUITextures.VANILLA_BACKGROUND, new Text("x"))
                .setPos(getGUIWidth() - 15, 3)
                .setSize(12, 12));
        }

        return builder.build();
    }

    public int getSelected() {
        return selected;
    }

    public void setSelected(int selected, Widget widget) {
        if (selected == this.selected) return;
        int newSelected = GTUtility.clamp(selected, UNSELECTED, stacks.size() - 1);
        if (noDeselect && newSelected == UNSELECTED) return;

        this.selected = newSelected;
    }

    protected List<String> getItemTooltips(final int index) {
        return GuiHelper.getItemTooltip(stacks.get(index));
    }

    private ItemStack getCandidate(int listIndex) {
        return listIndex < 0 || listIndex >= stacks.size() ? null : stacks.get(listIndex);
    }

    private FontRenderer getFontRenderer() {
        return Minecraft.getMinecraft().fontRenderer;
    }

    private int getGUIWidth() {
        return 176;
    }
}