Search in sources :

Example 1 with FactorioData

use of com.demod.factorio.FactorioData in project Factorio-FBSR by demodude4u.

the class FBSR method createItemListPanel.

private static PanelRenderer createItemListPanel(DataTable table, String title, Map<String, Double> items) {
    final double header = 0.8;
    final double spacing = 0.7;
    final double iconSize = 0.6;
    return new PanelRenderer(3.0, header + items.size() * spacing + 0.2) {

        @Override
        public void render(Graphics2D g, double width, double height) {
            g.setColor(GRID_COLOR);
            g.setStroke(GRID_STROKE);
            g.draw(new Rectangle2D.Double(0, 0, width, height));
            Font font = new Font("Monospaced", Font.BOLD, 1).deriveFont(0.6f);
            g.setFont(font);
            g.drawString(title, 0.3f, 0.65f);
            double startX = 0.6;
            double startY = header + spacing / 2.0;
            Rectangle2D.Double spriteBox = new Rectangle2D.Double(startX - iconSize / 2.0, startY - iconSize / 2.0, iconSize, iconSize);
            Point2D.Double textPos = new Point2D.Double(startX + 0.5, startY + 0.18);
            items.entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).forEach(e -> {
                String itemName = e.getKey();
                double amount = e.getValue();
                Optional<BufferedImage> image = Optional.empty();
                if (itemName.equals(TotalRawCalculator.RAW_TIME)) {
                    image = Optional.of(timeIcon);
                } else {
                    Optional<? extends DataPrototype> prototype = table.getItem(itemName);
                    if (!prototype.isPresent()) {
                        prototype = table.getFluid(itemName);
                    }
                    image = prototype.map(FactorioData::getIcon);
                }
                image.ifPresent(i -> {
                    RenderUtils.drawImageInBounds(i, new Rectangle(0, 0, i.getWidth(), i.getHeight()), spriteBox, g);
                });
                String amountStr;
                if (amount < 99999) {
                    g.setColor(GRID_COLOR);
                    amountStr = RenderUtils.fmtDouble(Math.ceil(amount));
                } else if (amount < 9999999) {
                    g.setColor(GRID_COLOR.brighter());
                    amountStr = RenderUtils.fmtDouble(Math.ceil(amount / 1000)) + "k";
                } else {
                    g.setColor(GRID_COLOR.brighter().brighter());
                    amountStr = RenderUtils.fmtDouble(Math.ceil(amount / 1000000)) + "M";
                }
                g.setFont(font);
                g.drawString(amountStr, (float) textPos.x, (float) textPos.y);
                spriteBox.y += spacing;
                textPos.y += spacing;
            });
        }
    };
}
Also used : Color(java.awt.Color) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Point2D(java.awt.geom.Point2D) Level(com.demod.dcba.CommandReporting.Level) Rectangle2D(java.awt.geom.Rectangle2D) RenderingHints(java.awt.RenderingHints) DataTable(com.demod.factorio.DataTable) JSONException(org.json.JSONException) RailEdge(com.demod.fbsr.WorldMap.RailEdge) JSONObject(org.json.JSONObject) Ellipse2D(java.awt.geom.Ellipse2D) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) LinkedHashMultiset(com.google.common.collect.LinkedHashMultiset) Shape(java.awt.Shape) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) DataPrototype(com.demod.factorio.prototype.DataPrototype) Collection(java.util.Collection) Set(java.util.Set) AffineTransform(java.awt.geom.AffineTransform) CubicCurve2D(java.awt.geom.CubicCurve2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) List(java.util.List) ModInfo(com.demod.factorio.ModInfo) FactorioData(com.demod.factorio.FactorioData) Entry(java.util.Map.Entry) Optional(java.util.Optional) BasicStroke(java.awt.BasicStroke) Queue(java.util.Queue) EntityPrototype(com.demod.factorio.prototype.EntityPrototype) Rectangle(java.awt.Rectangle) Multiset(com.google.common.collect.Multiset) HashMap(java.util.HashMap) RecipePrototype(com.demod.factorio.prototype.RecipePrototype) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RailNode(com.demod.fbsr.WorldMap.RailNode) ImmutableList(com.google.common.collect.ImmutableList) Graphics2D(java.awt.Graphics2D) SimpleEntry(java.util.AbstractMap.SimpleEntry) Layer(com.demod.fbsr.Renderer.Layer) Stroke(java.awt.Stroke) TilePrototype(com.demod.factorio.prototype.TilePrototype) Line2D(java.awt.geom.Line2D) CommandReporting(com.demod.dcba.CommandReporting) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) Consumer(java.util.function.Consumer) Utils(com.demod.factorio.Utils) ArrayDeque(java.util.ArrayDeque) TotalRawCalculator(com.demod.factorio.TotalRawCalculator) Table(com.google.common.collect.Table) Rectangle2D(java.awt.geom.Rectangle2D) Rectangle(java.awt.Rectangle) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Point2D(java.awt.geom.Point2D)

Example 2 with FactorioData

use of com.demod.factorio.FactorioData in project Factorio-FBSR by demodude4u.

the class AssemblingMachineRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    LuaValue animationLua = prototype.lua().get("idle_animation");
    if (animationLua.isnil()) {
        animationLua = prototype.lua().get("animation");
    }
    List<Sprite> sprites = RenderUtils.getSpritesFromAnimation(animationLua, entity.getDirection());
    register.accept(RenderUtils.spriteRenderer(sprites, entity, prototype));
    Sprite spriteIcon = new Sprite();
    String recipe = entity.json().optString("recipe", null);
    if (recipe != null) {
        Optional<RecipePrototype> optRecipe = dataTable.getRecipe(recipe);
        if (optRecipe.isPresent()) {
            RecipePrototype protoRecipe = optRecipe.get();
            if (!protoRecipe.lua().get("icon").isnil() || !protoRecipe.lua().get("icons").isnil()) {
                spriteIcon.image = FactorioData.getIcon(protoRecipe);
            } else {
                String name;
                if (protoRecipe.lua().get("results") != LuaValue.NIL) {
                    name = protoRecipe.lua().get("results").get(1).get("name").toString();
                } else {
                    name = protoRecipe.lua().get("result").toString();
                }
                Optional<? extends DataPrototype> protoProduct = dataTable.getItem(name);
                if (!protoProduct.isPresent()) {
                    protoProduct = dataTable.getFluid(name);
                }
                spriteIcon.image = protoProduct.map(FactorioData::getIcon).orElse(RenderUtils.EMPTY_IMAGE);
            }
            spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
            spriteIcon.bounds = new Rectangle2D.Double(-0.7, -1.0, 1.4, 1.4);
            Renderer delegate = RenderUtils.spriteRenderer(spriteIcon, entity, prototype);
            register.accept(new Renderer(Layer.OVERLAY2, delegate.getBounds()) {

                @Override
                public void render(Graphics2D g) throws Exception {
                    g.setColor(new Color(0, 0, 0, 180));
                    g.fill(spriteIcon.bounds);
                    delegate.render(g);
                }
            });
        }
    }
}
Also used : Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) FactorioData(com.demod.factorio.FactorioData) RecipePrototype(com.demod.factorio.prototype.RecipePrototype) Graphics2D(java.awt.Graphics2D) Renderer(com.demod.fbsr.Renderer) LuaValue(org.luaj.vm2.LuaValue)

Aggregations

FactorioData (com.demod.factorio.FactorioData)2 RecipePrototype (com.demod.factorio.prototype.RecipePrototype)2 Color (java.awt.Color)2 Graphics2D (java.awt.Graphics2D)2 Rectangle (java.awt.Rectangle)2 Rectangle2D (java.awt.geom.Rectangle2D)2 CommandReporting (com.demod.dcba.CommandReporting)1 Level (com.demod.dcba.CommandReporting.Level)1 DataTable (com.demod.factorio.DataTable)1 ModInfo (com.demod.factorio.ModInfo)1 TotalRawCalculator (com.demod.factorio.TotalRawCalculator)1 Utils (com.demod.factorio.Utils)1 DataPrototype (com.demod.factorio.prototype.DataPrototype)1 EntityPrototype (com.demod.factorio.prototype.EntityPrototype)1 ItemPrototype (com.demod.factorio.prototype.ItemPrototype)1 TilePrototype (com.demod.factorio.prototype.TilePrototype)1 Renderer (com.demod.fbsr.Renderer)1 Layer (com.demod.fbsr.Renderer.Layer)1 Sprite (com.demod.fbsr.Sprite)1 RailEdge (com.demod.fbsr.WorldMap.RailEdge)1