Search in sources :

Example 1 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeButton method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    // TODO: Find a sane way of making the position variable!
    inheritProperty(json, "area[0]", "pos[0]");
    inheritProperty(json, "area[1]", "pos[1]");
    inheritProperty(json, "area[2]", "size[0]");
    inheritProperty(json, "area[3]", "size[1]");
    inheritProperty(json, "size[0]", "modes.enabled[2]");
    inheritProperty(json, "size[1]", "modes.enabled[3]");
    inheritProperty(json, "sprite", "modes.enabled.sprite");
    inheritProperty(json, "texture", "modes.enabled.texture");
    inheritProperty(json, "size[0]", "modes.disabled[2]");
    inheritProperty(json, "size[1]", "modes.disabled[3]");
    inheritProperty(json, "sprite", "modes.disabled.sprite");
    inheritProperty(json, "texture", "modes.disabled.texture");
    inheritProperty(json, "size[0]", "modes.active[2]");
    inheritProperty(json, "size[1]", "modes.active[3]");
    inheritProperty(json, "sprite", "modes.active.sprite");
    inheritProperty(json, "texture", "modes.active.texture");
    inheritProperty(json, "size[0]", "modes.hovered[2]");
    inheritProperty(json, "size[1]", "modes.hovered[3]");
    inheritProperty(json, "sprite", "modes.hovered.sprite");
    inheritProperty(json, "texture", "modes.hovered.texture");
    inheritProperty(json, "size[0]", "modes.active_hovered[2]");
    inheritProperty(json, "size[1]", "modes.active_hovered[3]");
    inheritProperty(json, "sprite", "modes.active_hovered.sprite");
    inheritProperty(json, "texture", "modes.active_hovered.texture");
    int posX = resolveEquationInt(json, "pos[0]", ctx);
    int posY = resolveEquationInt(json, "pos[1]", ctx);
    int sizeX = resolveEquationInt(json, "size[0]", ctx);
    int sizeY = resolveEquationInt(json, "size[1]", ctx);
    String buttonId;
    if (json.properties.containsKey("button")) {
        buttonId = json.properties.get("button");
    } else {
        buttonId = resolveEquation(json, "button_expression", ctx);
    }
    ISimpleDrawable drEnabled = resolveDrawable(ctx, info, json, gui, sizeX, sizeY, "modes.enabled");
    GuiRectangle rect = new GuiRectangle(posX, posY, sizeX, sizeY);
    GuiButtonDrawable.Builder buttonBuilder = new GuiButtonDrawable.Builder(rect, drEnabled);
    buttonBuilder.active = resolveDrawable(ctx, info, json, gui, sizeX, sizeY, "modes.active");
    buttonBuilder.hovered = resolveDrawable(ctx, info, json, gui, sizeX, sizeY, "modes.hovered");
    buttonBuilder.activeHovered = resolveDrawable(ctx, info, json, gui, sizeX, sizeY, "modes.active_hovered");
    buttonBuilder.disabled = resolveDrawable(ctx, info, json, gui, sizeX, sizeY, "modes.disabled");
    buttonBuilder.disabledActive = resolveDrawable(ctx, info, json, gui, sizeX, sizeY, "modes.active_disabled");
    GuiButtonDrawable button = new GuiButtonDrawable(gui, json.name, parent, buttonBuilder);
    IButtonBehaviour behaviour = gui.properties.get(buttonId, IButtonBehaviour.class);
    if (behaviour != null) {
        button.setBehaviour(behaviour);
    }
    Boolean current = gui.properties.get(buttonId, Boolean.class);
    if (current != null) {
        button.setActive(current.booleanValue());
    }
    IButtonClickEventListener listener = gui.properties.get(buttonId, IButtonClickEventListener.class);
    if (listener == null) {
        BCLog.logger.warn("[lib.gui.json] Unknown button id '" + buttonId + "'");
    } else {
        button.registerListener(listener);
    }
    return button;
}
Also used : IButtonClickEventListener(buildcraft.lib.gui.button.IButtonClickEventListener) ISimpleDrawable(buildcraft.lib.gui.ISimpleDrawable) IButtonBehaviour(buildcraft.lib.gui.button.IButtonBehaviour) GuiRectangle(buildcraft.lib.gui.pos.GuiRectangle) GuiButtonDrawable(buildcraft.lib.gui.button.GuiButtonDrawable) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 2 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeStatementSlot method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    if (!json.properties.containsKey("size[0]")) {
        json.properties.put("size[0]", "18");
    }
    if (!json.properties.containsKey("size[1]")) {
        json.properties.put("size[1]", "18");
    }
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    String source;
    if (json.properties.containsKey("source")) {
        source = json.properties.get("source");
    } else {
        source = resolveEquation(json, "source_expression", ctx);
        if (source == null) {
            throw new JsonSyntaxException("Expected either 'source' or 'source_expression' for " + NAME);
        }
    }
    boolean draw = !"false".equals(json.properties.get("draw"));
    FullStatement<?> fullStatement = gui.properties.get(source, FullStatement.class);
    if (fullStatement == null) {
        throw new JsonSyntaxException("Can't find a statement called '" + source + "'");
    }
    StatementContext<?> context = gui.properties.get(source, StatementContext.class);
    return new GuiElementStatement<>(gui, area, fullStatement, context, draw);
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) JsonSyntaxException(com.google.gson.JsonSyntaxException) FunctionContext(buildcraft.lib.expression.FunctionContext) GuiElementStatement(buildcraft.lib.gui.statement.GuiElementStatement)

Example 3 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeSlot method deserialize0.

// pos: the position of the slot
// slot: The slot to be moved
// index: If the slot was an InventorySlotHolder then this is the index of the slot in the list
// visible: If false then the slot won't be visible
@Override
protected IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    String slotName = json.properties.get("slot");
    IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
    Slot slot = gui.properties.get(slotName, Slot.class);
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    if (slot != null) {
        return new GuiElementSlotMover(gui, pos, visible, slot);
    }
    InventorySlotHolder holder = gui.properties.get(slotName, InventorySlotHolder.class);
    if (holder == null) {
        throw new JsonSyntaxException("Unknown slot '" + slotName + "'");
    }
    int index = resolveEquationInt(json, "index", ctx);
    if (index < 0 || index >= holder.slots.length) {
        throw new JsonSyntaxException("Invalid slot index! (" + index + ", min = 0, max = " + (holder.slots.length - 1) + ")");
    }
    return new GuiElementSlotMover(gui, pos, visible, holder.slots[index]);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Slot(net.minecraft.inventory.Slot) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition) GuiElementSlotMover(buildcraft.lib.gui.elem.GuiElementSlotMover)

Example 4 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeStatementParam method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    if (!json.properties.containsKey("size[0]")) {
        json.properties.put("size[0]", "18");
    }
    if (!json.properties.containsKey("size[1]")) {
        json.properties.put("size[1]", "18");
    }
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    String source;
    if (json.properties.containsKey("source")) {
        source = json.properties.get("source");
    } else {
        source = resolveEquation(json, "source_expression", ctx);
        if (source == null) {
            throw new JsonSyntaxException("Expected either 'source' or 'source_expression' for " + NAME);
        }
    }
    boolean draw = !"false".equals(json.properties.get("draw"));
    int index = resolveEquationInt(json, "index", ctx);
    FullStatement<?> fullStatement = gui.properties.get(source, FullStatement.class);
    IStatementContainer statementContainer = gui.properties.get("statement.container", IStatementContainer.class);
    return new GuiElementStatementParam(gui, area, statementContainer, fullStatement, index, draw);
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) JsonSyntaxException(com.google.gson.JsonSyntaxException) IStatementContainer(buildcraft.api.statements.IStatementContainer) GuiElementStatementParam(buildcraft.lib.gui.statement.GuiElementStatementParam) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 5 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeToolTip method deserialize0.

// Args:
// - pos[0], pos[1]: the area for help (where it will be drawn, relative to the root of the gui). Defaults
// to 0,0
// - size[0], size[1]: the size of the help area
// - area[0-3]: mapping for pos[0], pos[1], size[0], size[1]
// - text: The text to display in the tooltip
// - expression: The expression to display in the tooltip
@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    List<String> text = new ArrayList<>();
    String key = "text";
    boolean isExpression = false;
    if (json.properties.containsKey("expression") || json.properties.containsKey("expression[0]")) {
        key = "expression";
        isExpression = true;
    }
    if (json.properties.containsKey(key + "[0]")) {
        int i = 0;
        while (true) {
            String prop = json.properties.get(key + "[" + i + "]");
            if (prop == null) {
                break;
            }
            text.add(prop);
            i++;
        }
    } else {
        text.add(json.properties.getOrDefault(key, "ERROR: Text not given!"));
    }
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    ITooltipElement source;
    if (isExpression) {
        List<INodeObject<String>> nodes = new ArrayList<>(text.size());
        try {
            for (String s : text) {
                nodes.add(GenericExpressionCompiler.compileExpressionString(s, ctx));
            }
        } catch (InvalidExpressionException e) {
            throw new JsonSyntaxException(e);
        }
        source = (list) -> {
            if (visible.evaluate()) {
                String[] arr = new String[nodes.size()];
                for (int i = 0; i < arr.length; i++) {
                    arr[i] = nodes.get(i).evaluate();
                }
                list.add(ToolTip.createLocalized(arr));
            }
        };
    } else {
        ToolTip tooltip = ToolTip.createLocalized(text.toArray(new String[0]));
        source = (list) -> {
            if (visible.evaluate()) {
                list.add(tooltip);
            }
        };
    }
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    return new GuiElementToolTip(gui, area, source);
}
Also used : GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) ToolTip(buildcraft.lib.gui.elem.ToolTip) IGuiArea(buildcraft.lib.gui.pos.IGuiArea) ITooltipElement(buildcraft.lib.gui.ITooltipElement) ArrayList(java.util.ArrayList) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Aggregations

FunctionContext (buildcraft.lib.expression.FunctionContext)26 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)8 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)7 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)6 NodeStack (buildcraft.lib.expression.NodeStack)5 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)5 ResourceLocation (net.minecraft.util.ResourceLocation)5 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)4 INodeFunc (buildcraft.lib.expression.api.INodeFunc)4 INodeStack (buildcraft.lib.expression.api.INodeStack)4 IButtonBehaviour (buildcraft.lib.gui.button.IButtonBehaviour)4 IButtonClickEventListener (buildcraft.lib.gui.button.IButtonClickEventListener)4 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)4 Test (org.junit.Test)4 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)3 GuiBC8 (buildcraft.lib.gui.GuiBC8)3 ISimpleDrawable (buildcraft.lib.gui.ISimpleDrawable)3 BuildCraftJsonGui (buildcraft.lib.gui.json.BuildCraftJsonGui)3 TypedKeyMap (buildcraft.lib.misc.collect.TypedKeyMap)3