Search in sources :

Example 1 with ItemPrototype

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

the class InfinityContainerRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    super.createRenderers(register, map, dataTable, entity, prototype);
    if (entity.json().has("infinity_settings") && entity.json().getJSONObject("infinity_settings").has("filters")) {
        List<String> items = new ArrayList<>();
        Utils.<JSONObject>forEach(entity.json().getJSONObject("infinity_settings").getJSONArray("filters"), j -> {
            if (j.getInt("count") > 0)
                items.add(j.getString("name"));
        });
        if (!items.isEmpty()) {
            String itemName = items.get(0);
            Optional<ItemPrototype> optItem = dataTable.getItem(itemName);
            if (optItem.isPresent()) {
                Sprite spriteIcon = new Sprite();
                spriteIcon.image = FactorioData.getIcon(optItem.get());
                spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
                spriteIcon.bounds = new Rectangle2D.Double(-0.3, -0.3, 0.6, 0.6);
                Renderer delegate = RenderUtils.spriteRenderer(spriteIcon, entity, prototype);
                register.accept(new Renderer(Layer.OVERLAY4, delegate.getBounds()) {

                    @Override
                    public void render(Graphics2D g) throws Exception {
                        g.setColor(new Color(0, 0, 0, 128));
                        g.fill(spriteIcon.bounds);
                        delegate.render(g);
                    }
                });
            }
        }
    }
}
Also used : Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) Graphics2D(java.awt.Graphics2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) JSONObject(org.json.JSONObject) Renderer(com.demod.fbsr.Renderer)

Example 2 with ItemPrototype

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

the class InserterRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    Point2D.Double pos = entity.getPosition();
    Direction dir = entity.getDirection();
    Sprite sprite = RenderUtils.getSpriteFromAnimation(prototype.lua().get("platform_picture").get("sheet"));
    sprite.source.x += sprite.source.width * (dir.back().cardinal());
    Sprite spriteArmHand = RenderUtils.getSpriteFromAnimation(prototype.lua().get("hand_open_picture"));
    double armStretch = -prototype.lua().get("pickup_position").get(2).todouble();
    boolean modded = entity.json().has("pickup_position") || entity.json().has("drop_position");
    Point2D.Double pickupPos;
    Point2D.Double insertPos;
    Point2D.Double inPos;
    Point2D.Double outPos;
    if (entity.json().has("pickup_position")) {
        pickupPos = Utils.parsePoint2D(entity.json().getJSONObject("pickup_position"));
        inPos = new Point2D.Double(pos.x + pickupPos.x, pos.y + pickupPos.y);
    } else if (modded) {
        inPos = dir.offset(pos, armStretch);
        pickupPos = new Point2D.Double(inPos.x - pos.x, inPos.y - pos.y);
    } else {
        pickupPos = Utils.parsePoint2D(prototype.lua().get("pickup_position"));
        inPos = dir.offset(pos, armStretch);
    }
    if (entity.json().has("drop_position")) {
        insertPos = Utils.parsePoint2D(entity.json().getJSONObject("drop_position"));
        outPos = new Point2D.Double(pos.x + insertPos.x, pos.y + insertPos.y);
    } else if (modded) {
        outPos = dir.offset(pos, -armStretch);
        insertPos = new Point2D.Double(outPos.x - pos.x, outPos.y - pos.y);
    } else {
        insertPos = Utils.parsePoint2D(prototype.lua().get("insert_position"));
        outPos = dir.offset(pos, -armStretch);
    }
    register.accept(RenderUtils.spriteRenderer(sprite, entity, prototype));
    register.accept(new Renderer(Layer.ENTITY2, sprite.bounds) {

        @Override
        public void render(Graphics2D g) {
            AffineTransform pat = g.getTransform();
            Rectangle2D.Double bounds = spriteArmHand.bounds;
            Rectangle source = spriteArmHand.source;
            BufferedImage image = spriteArmHand.image;
            g.translate(pos.x, pos.y);
            g.rotate(dir.back().ordinal() * Math.PI / 4.0);
            g.translate(bounds.x, 0);
            g.scale(bounds.width, armStretch);
            g.drawImage(image, 0, 1, 1, 0, source.x, source.y, source.x + source.width, source.y + source.height, null);
            g.setTransform(pat);
        }
    });
    register.accept(new Renderer(Layer.OVERLAY3, inPos) {

        @Override
        public void render(Graphics2D g) {
            AffineTransform pat = g.getTransform();
            Sprite grabberMarker = getGrabMarker(dataTable);
            Rectangle2D.Double bounds = grabberMarker.bounds;
            Rectangle source = grabberMarker.source;
            BufferedImage image = grabberMarker.image;
            double pickupRotate = Math.atan2(pickupPos.y, pickupPos.x);
            if (modded) {
                g.setTransform(pat);
                g.setColor(RenderUtils.withAlpha(Color.yellow, 64));
                g.setStroke(new BasicStroke(0.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g.draw(new Line2D.Double(pos, inPos));
            }
            g.setTransform(pat);
            g.translate(inPos.x, inPos.y);
            // HACK magic numbers
            Point2D.Double magicImageShift = new Point2D.Double(bounds.x + 0.1, bounds.y + -0.05);
            g.translate(magicImageShift.x, magicImageShift.y);
            if (modded) {
                g.translate(-Math.cos(pickupRotate) * 0.2, -Math.sin(pickupRotate) * 0.2);
                g.rotate(pickupRotate + Math.PI / 2.0, -magicImageShift.x, -magicImageShift.y);
            } else {
                g.rotate(dir.back().ordinal() * Math.PI / 4.0, -magicImageShift.x, -magicImageShift.y);
            }
            // magic numbers from Factorio code
            g.scale(0.8, 0.8);
            g.drawImage(image, 0, 0, 1, 1, source.x, source.y, source.x + source.width, source.y + source.height, null);
            g.setTransform(pat);
        }
    });
    register.accept(new Renderer(Layer.OVERLAY3, outPos) {

        @Override
        public void render(Graphics2D g) {
            AffineTransform pat = g.getTransform();
            Sprite placeMarker = getPlaceMarker(dataTable);
            Rectangle2D.Double bounds = placeMarker.bounds;
            Rectangle source = placeMarker.source;
            BufferedImage image = placeMarker.image;
            double insertRotate = Math.atan2(insertPos.y, insertPos.x);
            if (modded) {
                g.setTransform(pat);
                g.setColor(RenderUtils.withAlpha(Color.yellow, 64));
                g.setStroke(new BasicStroke(0.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g.draw(new Line2D.Double(pos, outPos));
            }
            g.setTransform(pat);
            g.translate(outPos.x, outPos.y);
            // HACK magic numbers
            Point2D.Double magicImageShift = new Point2D.Double(bounds.x + 0.1, bounds.y + 0.35);
            g.translate(magicImageShift.x, magicImageShift.y);
            if (modded) {
                g.translate(Math.cos(insertRotate) * 0.2, Math.sin(insertRotate) * 0.2);
                g.rotate(insertRotate + Math.PI / 2.0, -magicImageShift.x, -magicImageShift.y);
            } else {
                g.rotate(dir.back().ordinal() * Math.PI / 4.0, -magicImageShift.x, -magicImageShift.y);
            }
            // magic numbers from Factorio code
            g.scale(0.8, 0.8);
            g.drawImage(image, 0, 0, 1, 1, source.x, source.y, source.x + source.width, source.y + source.height, null);
            g.setTransform(pat);
        }
    });
    if (entity.json().has("filters")) {
        List<String> items = new ArrayList<>();
        Utils.forEach(entity.json().getJSONArray("filters"), (JSONObject j) -> {
            items.add(j.getString("name"));
        });
        if (!items.isEmpty()) {
            String itemName = items.get(0);
            Sprite spriteIcon = new Sprite();
            Optional<ItemPrototype> optItem = dataTable.getItem(itemName);
            if (optItem.isPresent()) {
                spriteIcon.image = FactorioData.getIcon(optItem.get());
                spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
                spriteIcon.bounds = new Rectangle2D.Double(-0.3, -0.3, 0.6, 0.6);
                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, 128));
                        g.fill(spriteIcon.bounds);
                        delegate.render(g);
                    }
                });
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) Direction(com.demod.fbsr.Direction) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) JSONObject(org.json.JSONObject) Point2D(java.awt.geom.Point2D) Renderer(com.demod.fbsr.Renderer) AffineTransform(java.awt.geom.AffineTransform)

Example 3 with ItemPrototype

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

the class LogisticContainerRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    super.createRenderers(register, map, dataTable, entity, prototype);
    if (entity.json().has("request_filters")) {
        List<String> items = new ArrayList<>();
        Utils.<JSONObject>forEach(entity.json().getJSONArray("request_filters"), j -> {
            items.add(j.getString("name"));
        });
        if (!items.isEmpty()) {
            String itemName = items.get(0);
            Optional<ItemPrototype> optItem = dataTable.getItem(itemName);
            if (optItem.isPresent()) {
                Sprite spriteIcon = new Sprite();
                spriteIcon.image = FactorioData.getIcon(optItem.get());
                spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
                spriteIcon.bounds = new Rectangle2D.Double(-0.3, -0.3, 0.6, 0.6);
                Renderer delegate = RenderUtils.spriteRenderer(spriteIcon, entity, prototype);
                register.accept(new Renderer(Layer.OVERLAY4, delegate.getBounds()) {

                    @Override
                    public void render(Graphics2D g) throws Exception {
                        g.setColor(new Color(0, 0, 0, 128));
                        g.fill(spriteIcon.bounds);
                        delegate.render(g);
                    }
                });
            }
        }
    }
}
Also used : Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) Graphics2D(java.awt.Graphics2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) JSONObject(org.json.JSONObject) Renderer(com.demod.fbsr.Renderer)

Example 4 with ItemPrototype

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

the class SplitterRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    Direction dir = entity.getDirection();
    Sprite belt1Sprite = TransportBeltRendering.getBeltSprite(prototype, dir, BeltBend.NONE);
    Sprite belt2Sprite = new Sprite(belt1Sprite);
    Point2D.Double beltShift = dir.left().offset(new Point2D.Double(), 0.5);
    belt1Sprite.bounds.x += beltShift.x;
    belt1Sprite.bounds.y += beltShift.y;
    belt2Sprite.bounds.x -= beltShift.x;
    belt2Sprite.bounds.y -= beltShift.y;
    register.accept(RenderUtils.spriteRenderer(Layer.ENTITY, belt1Sprite, entity, prototype));
    register.accept(RenderUtils.spriteRenderer(Layer.ENTITY, belt2Sprite, entity, prototype));
    LuaValue structurePatch = prototype.lua().get("structure_patch");
    if (!structurePatch.isnil() && (dir == Direction.WEST || dir == Direction.EAST)) {
        List<Sprite> patch = RenderUtils.getSpritesFromAnimation(structurePatch, dir);
        register.accept(RenderUtils.spriteRenderer(Layer.ENTITY2, patch, entity, prototype));
    }
    List<Sprite> sprites = RenderUtils.getSpritesFromAnimation(prototype.lua().get("structure"), dir);
    register.accept(RenderUtils.spriteRenderer(Layer.ENTITY2, sprites, entity, prototype));
    Double pos = entity.getPosition();
    Point2D.Double leftPos = dir.left().offset(pos, 0.5);
    Point2D.Double rightPos = dir.right().offset(pos, 0.5);
    if (entity.json().has("input_priority")) {
        boolean right = entity.json().getString("input_priority").equals("right");
        Point2D.Double inputPos = dir.offset(right ? rightPos : leftPos, 0);
        register.accept(new Renderer(Layer.OVERLAY3, inputPos) {

            @Override
            public void render(Graphics2D g) {
                AffineTransform pat = g.getTransform();
                Color color = Color.yellow;
                Color shadow = Color.darkGray;
                double shadowShift = 0.07;
                g.setTransform(pat);
                g.translate(inputPos.x, inputPos.y);
                g.rotate(dir.back().ordinal() * Math.PI / 4.0 + Math.PI);
                g.translate(shadowShift, shadowShift);
                g.setColor(shadow);
                g.fill(markerShape);
                g.setTransform(pat);
                g.translate(inputPos.x, inputPos.y);
                g.rotate(dir.back().ordinal() * Math.PI / 4.0 + Math.PI);
                g.setColor(color);
                g.fill(markerShape);
                g.setTransform(pat);
            }
        });
    }
    if (entity.json().has("output_priority")) {
        boolean right = entity.json().getString("output_priority").equals("right");
        Point2D.Double outputPos = dir.offset(right ? rightPos : leftPos, 0.6);
        if (entity.json().has("filter")) {
            Point2D.Double iconPos = right ? rightPos : leftPos;
            String itemName = entity.json().getString("filter");
            Sprite spriteIcon = new Sprite();
            Optional<ItemPrototype> optItem = dataTable.getItem(itemName);
            if (optItem.isPresent()) {
                spriteIcon.image = FactorioData.getIcon(optItem.get());
                spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
                spriteIcon.bounds = new Rectangle2D.Double(-0.3, -0.3, 0.6, 0.6);
                Renderer delegate = RenderUtils.spriteRenderer(spriteIcon, entity, prototype);
                spriteIcon.bounds = new Rectangle2D.Double(iconPos.x - 0.3, iconPos.y - 0.3, 0.6, 0.6);
                register.accept(new Renderer(Layer.OVERLAY2, delegate.getBounds()) {

                    @Override
                    public void render(Graphics2D g) throws Exception {
                        g.setColor(new Color(0, 0, 0, 128));
                        g.fill(spriteIcon.bounds);
                        delegate.render(g);
                    }
                });
            }
        } else {
            register.accept(new Renderer(Layer.OVERLAY3, outputPos) {

                @Override
                public void render(Graphics2D g) {
                    AffineTransform pat = g.getTransform();
                    Color color = Color.yellow;
                    Color shadow = Color.darkGray;
                    double shadowShift = 0.07;
                    g.setTransform(pat);
                    g.translate(outputPos.x, outputPos.y);
                    g.rotate(dir.back().ordinal() * Math.PI / 4.0 + Math.PI);
                    g.translate(shadowShift, shadowShift);
                    g.setColor(shadow);
                    g.fill(markerShape);
                    g.setTransform(pat);
                    g.translate(outputPos.x, outputPos.y);
                    g.rotate(dir.back().ordinal() * Math.PI / 4.0 + Math.PI);
                    g.setColor(color);
                    g.fill(markerShape);
                    g.setTransform(pat);
                }
            });
        }
    }
}
Also used : Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) Direction(com.demod.fbsr.Direction) Double(java.awt.geom.Point2D.Double) Double(java.awt.geom.Point2D.Double) Graphics2D(java.awt.Graphics2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) Point2D(java.awt.geom.Point2D) Renderer(com.demod.fbsr.Renderer) AffineTransform(java.awt.geom.AffineTransform) LuaValue(org.luaj.vm2.LuaValue)

Example 5 with ItemPrototype

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

the class FBSR method getItemLogisticColor.

private static Color getItemLogisticColor(DataTable table, String itemName) {
    return itemColorCache.computeIfAbsent(itemName, k -> {
        Optional<ItemPrototype> optProto = table.getItem(k);
        if (!optProto.isPresent()) {
            System.err.println("ITEM MISSING FOR LOGISTICS: " + k);
            return Color.MAGENTA;
        }
        DataPrototype prototype = optProto.get();
        BufferedImage image = FactorioData.getIcon(prototype);
        Color color = RenderUtils.getAverageColor(image);
        // return new Color(color.getRGB() | 0xA0A0A0);
        // return color.brighter().brighter();
        float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        return Color.getHSBColor(hsb[0], Math.max(0.25f, hsb[1]), Math.max(0.5f, hsb[2]));
    // return Color.getHSBColor(hsb[0], Math.max(1f, hsb[1]),
    // Math.max(0.75f, hsb[2]));
    });
}
Also used : ItemPrototype(com.demod.factorio.prototype.ItemPrototype) Color(java.awt.Color) DataPrototype(com.demod.factorio.prototype.DataPrototype) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ItemPrototype (com.demod.factorio.prototype.ItemPrototype)7 Color (java.awt.Color)7 Graphics2D (java.awt.Graphics2D)6 Rectangle (java.awt.Rectangle)6 Rectangle2D (java.awt.geom.Rectangle2D)6 Renderer (com.demod.fbsr.Renderer)5 Sprite (com.demod.fbsr.Sprite)5 ArrayList (java.util.ArrayList)5 JSONObject (org.json.JSONObject)5 Point2D (java.awt.geom.Point2D)4 Direction (com.demod.fbsr.Direction)3 AffineTransform (java.awt.geom.AffineTransform)3 BufferedImage (java.awt.image.BufferedImage)3 BasicStroke (java.awt.BasicStroke)2 LuaValue (org.luaj.vm2.LuaValue)2 DataTable (com.demod.factorio.DataTable)1 FactorioData (com.demod.factorio.FactorioData)1 Utils (com.demod.factorio.Utils)1 DataPrototype (com.demod.factorio.prototype.DataPrototype)1 EntityPrototype (com.demod.factorio.prototype.EntityPrototype)1