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;
}
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);
}
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]);
}
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);
}
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);
}
Aggregations