Search in sources :

Example 21 with FunctionContext

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

the class GuiGate method preLoad.

protected void preLoad(BuildCraftJsonGui json) {
    GateLogic gate = container.gate;
    TypedKeyMap<String, Object> properties = json.properties;
    FunctionContext context = json.context;
    properties.put("statement.container", gate);
    context.putConstantBoolean("gate.two_columns", gate.isSplitInTwo());
    context.putConstantLong("gate.slots", gate.variant.numSlots);
    context.putConstantLong("gate.triggers.args", gate.variant.numTriggerArgs);
    context.putConstantLong("gate.actions.args", gate.variant.numActionArgs);
    context.put_b("gate.two_columns", () -> gate.isOn);
    context.putConstant("gate.material", String.class, gate.variant.material.tag);
    context.putConstant("gate.modifier", String.class, gate.variant.modifier.tag);
    context.putConstant("gate.logic", String.class, gate.variant.logic.tag);
    context.putConstant("gate.variant", String.class, gate.variant.getLocalizedName());
    properties.put("gate.triggers.possible", container.possibleTriggersContext);
    properties.put("gate.actions.possible", container.possibleActionsContext);
    context.put_l_b("gate.is_connected", (i) -> {
        if (i < 0 || i >= gate.connections.length) {
            return false;
        }
        return gate.connections[(int) i];
    }).setNeverInline();
    context.put_l_b("gate.trigger.is_on", (i) -> {
        if (i < 0 || i >= gate.triggerOn.length) {
            return false;
        }
        return gate.triggerOn[(int) i];
    }).setNeverInline();
    context.put_l_b("gate.set.is_on", (i) -> {
        if (i < 0 || i >= gate.triggerOn.length) {
            return false;
        }
        return gate.actionOn[(int) i];
    }).setNeverInline();
    context.put_l_b("gate.action.is_on", (i) -> {
        if (i < 0 || i >= gate.actionOn.length) {
            return false;
        }
        return gate.actionOn[(int) i] && gate.statements[(int) i].action.get() != null;
    }).setNeverInline();
    for (int s = 0; s < gate.variant.numSlots; s++) {
        final int i = s;
        String tName = "gate.trigger/" + i;
        String aName = "gate.action/" + i;
        properties.put(tName, gate.statements[i].trigger);
        properties.put(aName, gate.statements[i].action);
        properties.put(tName, container.possibleTriggersContext);
        properties.put(aName, container.possibleActionsContext);
    }
    for (int c = 0; c < gate.connections.length; c++) {
        final int connection = c;
        String name = "gate.connection/" + c;
        properties.put(name, gate.connections[c]);
        properties.put(name, IButtonBehaviour.TOGGLE);
        properties.put(name, (IButtonClickEventListener) (b, k) -> {
            container.setConnected(connection, b.isButtonActive());
        });
    }
}
Also used : GuiBC8(buildcraft.lib.gui.GuiBC8) ContainerGate(buildcraft.transport.container.ContainerGate) MessageUtil(buildcraft.lib.misc.MessageUtil) ResourceLocation(net.minecraft.util.ResourceLocation) IButtonClickEventListener(buildcraft.lib.gui.button.IButtonClickEventListener) BuildCraftJsonGui(buildcraft.lib.gui.json.BuildCraftJsonGui) GateLogic(buildcraft.transport.gate.GateLogic) FunctionContext(buildcraft.lib.expression.FunctionContext) IButtonBehaviour(buildcraft.lib.gui.button.IButtonBehaviour) TypedKeyMap(buildcraft.lib.misc.collect.TypedKeyMap) FunctionContext(buildcraft.lib.expression.FunctionContext) GateLogic(buildcraft.transport.gate.GateLogic)

Example 22 with FunctionContext

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

the class ElementTypeDrawnStack method deserialize0.

@Override
protected IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    boolean foreground = resolveEquationBool(json, "foreground", ctx, false);
    Item item = JsonUtils.getItem(json.json, "id");
    int meta = resolveEquationInt(json, "meta", ctx);
    ItemStack stack = new ItemStack(item, 1, meta);
    ISimpleDrawable icon = new GuiStack(stack);
    IGuiArea area = IGuiArea.create(pos, 16, 16);
    return new GuiElementDrawable(gui, area, icon, foreground, visible);
}
Also used : Item(net.minecraft.item.Item) IGuiArea(buildcraft.lib.gui.pos.IGuiArea) ISimpleDrawable(buildcraft.lib.gui.ISimpleDrawable) GuiElementDrawable(buildcraft.lib.gui.elem.GuiElementDrawable) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) ItemStack(net.minecraft.item.ItemStack) GuiStack(buildcraft.lib.gui.GuiStack) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition)

Example 23 with FunctionContext

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

the class ElementTypeLedger method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    inheritProperty(json, "color", "colour");
    String side = json.properties.get("side");
    String title = json.properties.get("title");
    int colour = resolveEquationInt(json, "colour", ctx);
    boolean positive = "right".equalsIgnoreCase(side);
    Ledger_Neptune ledger = new Ledger_Neptune(gui, colour, positive);
    ledger.setTitle(title);
    addChildren(gui, ledger.positionLedgerIconStart, info, json, "closed", ledger.getClosedElements()::add);
    ledger.calculateMaxSize();
    ResourceLocation def = gui.jsonGuiDefinition;
    def = new ResourceLocation(def.getResourceDomain(), def.getResourcePath().replace(".json", ""));
    ledger.setOpenProperty(GuiConfigManager.getOrAddBoolean(def, json.name + ".is_open", false));
    return ledger;
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) FunctionContext(buildcraft.lib.expression.FunctionContext) Ledger_Neptune(buildcraft.lib.gui.ledger.Ledger_Neptune)

Example 24 with FunctionContext

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

the class ElementTypeSprite method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    inheritProperty(json, "source.pos[0]", "source.area[0]");
    inheritProperty(json, "source.pos[1]", "source.area[1]");
    inheritProperty(json, "source.size[0]", "source.area[2]");
    inheritProperty(json, "source.size[1]", "source.area[3]");
    inheritProperty(json, "area", "source.area");
    inheritProperty(json, "area[0]", "source.area[0]");
    inheritProperty(json, "area[1]", "source.area[1]");
    inheritProperty(json, "area[2]", "source.area[2]");
    inheritProperty(json, "area[3]", "source.area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    IGuiArea srcArea = resolveArea(json, "source.area", PositionAbsolute.ORIGIN, ctx);
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    boolean foreground = resolveEquationBool(json, "foreground", ctx, false);
    // TODO: Allow the source sprite to be changing as well!
    SrcTexture tex = resolveTexture(info, json, "source");
    String origin = tex.origin;
    int texSize = tex.texSize;
    if (// 
    !json.properties.containsKey("source.area[2]") && // 
    !json.properties.containsKey("source.area[3]") && !json.properties.containsKey("source.area")) {
        srcArea = new GuiRectangle(texSize, texSize);
    }
    ISprite sprite = gui.properties.get(origin, ISprite.class);
    if (sprite == null) {
        ResourceLocation loc = SpriteUtil.transformLocation(new ResourceLocation(origin));
        sprite = new SpriteRaw(loc, 0, 0, 1, 1);
    }
    if (srcArea instanceof GuiRectangle) {
        double u = srcArea.getX();
        double v = srcArea.getY();
        double uSize = srcArea.getWidth();
        double vSize = srcArea.getHeight();
        sprite = GuiUtil.subRelative(sprite, u, v, uSize, vSize, texSize);
    } else {
        final IGuiArea a = srcArea;
        INodeDouble u = () -> a.getX() / texSize;
        INodeDouble v = () -> a.getY() / texSize;
        INodeDouble uSize = () -> a.getEndX() / texSize;
        INodeDouble vSize = () -> a.getEndY() / texSize;
        sprite = new SubSpriteChanging(sprite, u, v, uSize, vSize);
    }
    ISimpleDrawable icon = new GuiSpriteScaled(sprite, area.offsetToOrigin());
    GuiElementDrawable element = new GuiElementDrawable(gui, area, icon, foreground, visible);
    return element;
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) ISimpleDrawable(buildcraft.lib.gui.ISimpleDrawable) GuiRectangle(buildcraft.lib.gui.pos.GuiRectangle) ISprite(buildcraft.api.core.render.ISprite) GuiElementDrawable(buildcraft.lib.gui.elem.GuiElementDrawable) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) SubSpriteChanging(buildcraft.lib.client.sprite.SubSpriteChanging) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) ResourceLocation(net.minecraft.util.ResourceLocation) GuiSpriteScaled(buildcraft.lib.gui.GuiSpriteScaled) SpriteRaw(buildcraft.lib.client.sprite.SpriteRaw)

Example 25 with FunctionContext

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

the class ElementTypeText method deserialize0.

// pos: the position of the text
// text: The text to be drawn. Will be localised first, and used as a fallback
// expression: A replacement for text -- uses an expression rather than as a literal.
// colour: Default colour to be drawn
// centered: If true then the text will be centered around pos
@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
    INodeObject<String> text;
    String prop;
    if ((prop = json.properties.get("text")) != null) {
        String localized = LocaleUtil.localize(prop);
        text = new NodeConstantObject<>(String.class, localized);
    } else if ((prop = json.properties.get("expression")) != null) {
        try {
            text = GenericExpressionCompiler.compileExpressionString(prop, ctx);
        } catch (InvalidExpressionException e) {
            throw new JsonSyntaxException("Invalid expression for '" + json.name + "'", e);
        }
    } else {
        throw new JsonSyntaxException("Require either 'text' or 'expression'!");
    }
    int colour;
    if (json.properties.containsKey("colour")) {
        colour = resolveEquationInt(json, "colour", ctx);
    } else {
        colour = resolveEquationInt(json, "color", ctx);
    }
    GuiElementText element = new GuiElementText(gui, pos, text, colour);
    element.setCentered("true".equals(json.properties.get("centered")));
    element.setDropShadow("true".equals(json.properties.get("shadow")));
    element.setForeground("true".equals(json.properties.get("foreground")));
    return element;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) GuiElementText(buildcraft.lib.gui.elem.GuiElementText) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition)

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