Search in sources :

Example 1 with IContainingElement

use of buildcraft.lib.gui.IContainingElement in project BuildCraft by BuildCraft.

the class ElementType method deserialize.

public final IGuiElement deserialize(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    IGuiElement element = deserialize0(gui, parent, info, json);
    if (element instanceof GuiElementSimple) {
        ((GuiElementSimple) element).name = json.fullName;
    }
    gui.context.putConstant(json.fullName + ".pos", IGuiPosition.class, element);
    gui.context.putConstant(json.fullName + ".area", IGuiArea.class, element);
    gui.varData.addNodes(json.createTickableNodes());
    List<IGuiElement> children = new ArrayList<>();
    IContainingElement container;
    if (element instanceof IContainingElement) {
        container = (IContainingElement) element;
    } else {
        container = new GuiElementContainerResizing(gui, element);
        container.getChildElements().add(element);
    }
    addChildren(gui, container.getChildElementPosition(), info, json, "children", children::add);
    // Special case tooltips + help
    if (json.json.has("help") && !(this instanceof ElementTypeHelp)) {
        addType(gui, parent, info, json, "help", children::add, ElementTypeHelp.INSTANCE);
    }
    if (json.json.has("tooltip") && !(this instanceof ElementTypeToolTip)) {
        addType(gui, parent, info, json, "tooltip", children::add, ElementTypeToolTip.INSTANCE);
    }
    if (!children.isEmpty()) {
        element = container;
        container.getChildElements().addAll(children);
        container.calculateSizes();
    }
    return element;
}
Also used : IContainingElement(buildcraft.lib.gui.IContainingElement) GuiElementSimple(buildcraft.lib.gui.GuiElementSimple) ArrayList(java.util.ArrayList) IGuiElement(buildcraft.lib.gui.IGuiElement) GuiElementContainerResizing(buildcraft.lib.gui.elem.GuiElementContainerResizing)

Example 2 with IContainingElement

use of buildcraft.lib.gui.IContainingElement in project BuildCraft by BuildCraft.

the class BuildCraftJsonGui method load.

public final void load() {
    ResourceLoaderContext loadHistory = new ResourceLoaderContext();
    try (InputStreamReader reader = loadHistory.startLoading(jsonGuiDefinition)) {
        JsonObject obj = new Gson().fromJson(reader, JsonObject.class);
        JsonGuiInfo info = new JsonGuiInfo(obj, context, loadHistory);
        sizeX = (int) GenericExpressionCompiler.compileExpressionLong(info.sizeX, context).evaluate();
        sizeY = (int) GenericExpressionCompiler.compileExpressionLong(info.sizeY, context).evaluate();
        varData.setNodes(info.createTickableNodes());
        for (JsonGuiElement elem : info.elements) {
            String typeName = elem.properties.get("type");
            ElementType type = JsonGuiTypeRegistry.TYPES.get(typeName);
            if (type == null) {
                BCLog.logger.warn("Unknown type " + typeName);
            } else {
                IGuiElement e = type.deserialize(this, rootElement, info, elem);
                String parent = elem.properties.get("parent");
                IContainingElement p = properties.get("custom." + parent, IContainingElement.class);
                properties.put("custom." + elem.name, e);
                if (p == null) {
                    shownElements.add(e);
                } else {
                    p.getChildElements().add(e);
                    p.calculateSizes();
                }
            }
        }
    } catch (InvalidExpressionException iee) {
        throw new JsonSyntaxException("Failed to resolve the size of " + jsonGuiDefinition, iee);
    } catch (IOException e) {
        throw new Error(e);
    }
    loadHistory.finishLoading();
}
Also used : ResourceLoaderContext(buildcraft.lib.client.model.ResourceLoaderContext) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) IOException(java.io.IOException) IContainingElement(buildcraft.lib.gui.IContainingElement) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) IGuiElement(buildcraft.lib.gui.IGuiElement)

Aggregations

IContainingElement (buildcraft.lib.gui.IContainingElement)2 IGuiElement (buildcraft.lib.gui.IGuiElement)2 ResourceLoaderContext (buildcraft.lib.client.model.ResourceLoaderContext)1 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)1 GuiElementSimple (buildcraft.lib.gui.GuiElementSimple)1 GuiElementContainerResizing (buildcraft.lib.gui.elem.GuiElementContainerResizing)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1