Search in sources :

Example 1 with DrawableWidget

use of gregtech.api.gui.widgets.DrawableWidget in project GregTech by GregTechCEu.

the class OreDictionaryItemFilter method initUI.

@Override
public void initUI(Consumer<Widget> widgetGroup) {
    widgetGroup.accept(new ImageWidget(12, 0, 20, 20, GuiTextures.INFO_ICON).setTooltip("cover.ore_dictionary_filter.info"));
    widgetGroup.accept(new ImageWidget(10, 25, 156, 14, GuiTextures.DISPLAY));
    widgetGroup.accept(new TextFieldWidget2(14, 29, 152, 12, () -> oreDictFilterExpression, this::setOreDictFilterExpression).setAllowedChars(Pattern.compile("[(!]* *[0-9a-zA-Z*]* *\\)*( *[&|^]? *[(!]* *[0-9a-zA-Z*]* *\\)*)*")).setMaxLength(64).setScale(0.75f).setValidator(input -> {
        // remove all operators that are double
        input = input.replaceAll("\\*{2,}", "*");
        input = input.replaceAll("&{2,}", "&");
        input = input.replaceAll("\\|{2,}", "|");
        input = input.replaceAll("!{2,}", "!");
        input = input.replaceAll("\\^{2,}", "^");
        input = input.replaceAll(" {2,}", " ");
        // move ( and ) so it doesn't create invalid expressions f.e. xxx (& yyy) => xxx & (yyy)
        // append or prepend ( and ) if the amount is not equal
        StringBuilder builder = new StringBuilder();
        int unclosed = 0;
        char last = ' ';
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == ' ') {
                if (last != '(')
                    builder.append(" ");
                continue;
            }
            if (c == '(')
                unclosed++;
            else if (c == ')') {
                unclosed--;
                if (last == '&' || last == '|' || last == '^') {
                    int l = builder.lastIndexOf(" " + last);
                    int l2 = builder.lastIndexOf("" + last);
                    builder.insert(l == l2 - 1 ? l : l2, ")");
                    continue;
                }
                if (i > 0 && builder.charAt(builder.length() - 1) == ' ') {
                    builder.deleteCharAt(builder.length() - 1);
                }
            } else if ((c == '&' || c == '|' || c == '^') && last == '(') {
                builder.deleteCharAt(builder.lastIndexOf("("));
                builder.append(c).append(" (");
                continue;
            }
            builder.append(c);
            last = c;
        }
        if (unclosed > 0) {
            for (int i = 0; i < unclosed; i++) {
                builder.append(")");
            }
        } else if (unclosed < 0) {
            unclosed = -unclosed;
            for (int i = 0; i < unclosed; i++) {
                builder.insert(0, "(");
            }
        }
        input = builder.toString();
        input = input.replaceAll(" {2,}", " ");
        return input;
    }));
    widgetGroup.accept(new DrawableWidget(36, 1, 100, 18).setBackgroundDrawer(((mouseX, mouseY, partialTicks, context, widget) -> {
        if (testStack.isEmpty()) {
            return;
        }
        int color = 0xD15858;
        if (testResult) {
            color = 0x66C261;
        }
        GlStateManager.pushMatrix();
        GlStateManager.translate(widget.getPosition().x, widget.getPosition().y, 0);
        GlStateManager.colorMask(true, true, true, true);
        Widget.drawText(I18n.format(testMsg), 22, 6.5f, 0.75f, color, false);
        color |= (140 & 0xFF) << 24;
        Widget.drawGradientRect(0, 0, 18, 18, color, color);
        GlStateManager.popMatrix();
    })));
    widgetGroup.accept(new OreDictFilterTestSlot(36, 1).setListener(stack -> {
        testStack = stack;
        updateTestMsg();
    }));
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) java.util(java.util) GlStateManager(net.minecraft.client.renderer.GlStateManager) DrawableWidget(gregtech.api.gui.widgets.DrawableWidget) GuiTextures(gregtech.api.gui.GuiTextures) ImageWidget(gregtech.api.gui.widgets.ImageWidget) I18n(net.minecraft.client.resources.I18n) Consumer(java.util.function.Consumer) ItemStack(net.minecraft.item.ItemStack) Widget(gregtech.api.gui.Widget) OreDictExprFilter(gregtech.api.util.OreDictExprFilter) TextFieldWidget2(gregtech.api.gui.widgets.TextFieldWidget2) Pattern(java.util.regex.Pattern) OreDictFilterTestSlot(gregtech.api.gui.widgets.OreDictFilterTestSlot) ItemStackKey(gregtech.api.util.ItemStackKey) OreDictFilterTestSlot(gregtech.api.gui.widgets.OreDictFilterTestSlot) DrawableWidget(gregtech.api.gui.widgets.DrawableWidget) ImageWidget(gregtech.api.gui.widgets.ImageWidget) TextFieldWidget2(gregtech.api.gui.widgets.TextFieldWidget2)

Aggregations

GuiTextures (gregtech.api.gui.GuiTextures)1 Widget (gregtech.api.gui.Widget)1 DrawableWidget (gregtech.api.gui.widgets.DrawableWidget)1 ImageWidget (gregtech.api.gui.widgets.ImageWidget)1 OreDictFilterTestSlot (gregtech.api.gui.widgets.OreDictFilterTestSlot)1 TextFieldWidget2 (gregtech.api.gui.widgets.TextFieldWidget2)1 ItemStackKey (gregtech.api.util.ItemStackKey)1 OreDictExprFilter (gregtech.api.util.OreDictExprFilter)1 java.util (java.util)1 Consumer (java.util.function.Consumer)1 Pattern (java.util.regex.Pattern)1 GlStateManager (net.minecraft.client.renderer.GlStateManager)1 I18n (net.minecraft.client.resources.I18n)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1