Search in sources :

Example 1 with RecordSheetEquipmentLine

use of megameklab.com.util.RecordSheetEquipmentLine in project megameklab by MegaMek.

the class PrintMech method writeEquipment.

@Override
protected void writeEquipment(SVGRectElement svgRect) {
    Map<Integer, Map<RecordSheetEquipmentLine, Integer>> eqMap = new TreeMap<>();
    Map<String, Integer> ammo = new TreeMap<>();
    for (Mounted m : mech.getEquipment()) {
        if ((m.getType() instanceof AmmoType) && (((AmmoType) m.getType()).getAmmoType() != AmmoType.T_COOLANT_POD)) {
            if (m.getLocation() != Entity.LOC_NONE) {
                String shortName = m.getType().getShortName().replace("Ammo", "");
                shortName = shortName.replace("(Clan)", "");
                String munition = ((AmmoType) m.getType()).getSubMunitionName().replace("(Clan) ", "");
                shortName = shortName.replace(munition, "");
                ammo.merge(shortName.trim(), m.getBaseShotsLeft(), Integer::sum);
            }
            continue;
        }
        if ((m.getType() instanceof AmmoType) || (m.getLocation() == Entity.LOC_NONE) || !UnitUtil.isPrintableEquipment(m.getType(), true)) {
            continue;
        }
        if (mech.hasETypeFlag(Entity.ETYPE_QUADVEE) && (m.getType() instanceof MiscType) && m.getType().hasFlag(MiscType.F_TRACKS)) {
            continue;
        }
        eqMap.putIfAbsent(m.getLocation(), new HashMap<>());
        RecordSheetEquipmentLine line = new RecordSheetEquipmentLine(m);
        eqMap.get(m.getLocation()).merge(line, 1, Integer::sum);
    }
    Rectangle2D bbox = getRectBBox(svgRect);
    Element canvas = (Element) ((Node) svgRect).getParentNode();
    int viewWidth = (int) bbox.getWidth();
    int viewHeight = (int) bbox.getHeight();
    int viewX = (int) bbox.getX();
    int viewY = (int) bbox.getY();
    int qtyX = (int) Math.round(viewX + viewWidth * 0.037);
    int nameX = (int) Math.round(viewX + viewWidth * 0.08);
    int locX = (int) Math.round(viewX + viewWidth * 0.41);
    int heatX = (int) Math.round(viewX + viewWidth * 0.48);
    int dmgX = (int) Math.round(viewX + viewWidth * 0.53);
    int minX = (int) Math.round(viewX + viewWidth * 0.75);
    int shortX = (int) Math.round(viewX + viewWidth * 0.82);
    int medX = (int) Math.round(viewX + viewWidth * 0.89);
    int longX = (int) Math.round(viewX + viewWidth * 0.96);
    int indent = (int) Math.round(viewWidth * 0.02);
    int currY = viewY + 10;
    float fontSize = FONT_SIZE_MEDIUM;
    float lineHeight = getFontHeight(fontSize) * 0.8f;
    addTextElement(canvas, qtyX, currY, "Qty", fontSize, "middle", "bold");
    addTextElement(canvas, nameX + indent, currY, "Type", fontSize, "start", "bold");
    addTextElement(canvas, locX, currY, "Loc", fontSize, "middle", "bold");
    addTextElement(canvas, heatX, currY, "Ht", fontSize, "middle", "bold");
    addTextElement(canvas, dmgX, currY, "Dmg", fontSize, "start", "bold");
    addTextElement(canvas, minX, currY, "Min", fontSize, "middle", "bold");
    addTextElement(canvas, shortX, currY, "Sht", fontSize, "middle", "bold");
    addTextElement(canvas, medX, currY, "Med", fontSize, "middle", "bold");
    addTextElement(canvas, longX, currY, "Lng", fontSize, "middle", "bold");
    currY += lineHeight * 1.2;
    int lines = 0;
    for (Integer loc : eqMap.keySet()) {
        for (RecordSheetEquipmentLine line : eqMap.get(loc).keySet()) {
            int rows = line.nRows();
            if ((rows == 1) && (getTextLength(line.getNameField(0, mech.isMixedTech()), fontSize) > locX - nameX)) {
                rows++;
            }
            lines += rows;
        }
    }
    if (lines > 12) {
        lineHeight = getFontHeight(fontSize) * 0.8f;
    }
    if (lines > 16) {
        fontSize = FONT_SIZE_SMALL;
    }
    if (lines > 20) {
        fontSize = FONT_SIZE_VSMALL;
    }
    for (Integer loc : eqMap.keySet()) {
        for (RecordSheetEquipmentLine line : eqMap.get(loc).keySet()) {
            for (int row = 0; row < line.nRows(); row++) {
                if (row == 0) {
                    addTextElement(canvas, qtyX, currY, Integer.toString(eqMap.get(loc).get(line)), fontSize, "middle", "normal");
                    lines = addMultilineTextElement(canvas, nameX, currY, locX - nameX - indent, lineHeight, line.getNameField(row, mech.isMixedTech()), fontSize, "start", "normal");
                } else {
                    lines = addMultilineTextElement(canvas, nameX + indent, currY, locX - nameX - indent, lineHeight, line.getNameField(row, mech.isMixedTech()), fontSize, "start", "normal");
                }
                addTextElement(canvas, locX, currY, line.getLocationField(row), fontSize, "middle", "normal");
                addTextElement(canvas, heatX, currY, line.getHeatField(row), fontSize, "middle", "normal");
                lines = Math.max(lines, addMultilineTextElement(canvas, dmgX, currY, minX - dmgX, lineHeight, line.getDamageField(row), fontSize, "start", "normal"));
                addTextElement(canvas, minX, currY, line.getMinField(row), fontSize, "middle", "normal");
                addTextElement(canvas, shortX, currY, line.getShortField(row), fontSize, "middle", "normal");
                addTextElement(canvas, medX, currY, line.getMediumField(row), fontSize, "middle", "normal");
                addTextElement(canvas, longX, currY, line.getLongField(row), fontSize, "middle", "normal");
                currY += lineHeight * lines;
            }
        }
    }
    StringJoiner quirksList = new StringJoiner(", ");
    Quirks quirks = mech.getQuirks();
    for (Enumeration<IOptionGroup> optionGroups = quirks.getGroups(); optionGroups.hasMoreElements(); ) {
        IOptionGroup optiongroup = optionGroups.nextElement();
        if (quirks.count(optiongroup.getKey()) > 0) {
            for (Enumeration<IOption> options = optiongroup.getOptions(); options.hasMoreElements(); ) {
                IOption option = options.nextElement();
                if (option != null && option.booleanValue()) {
                    quirksList.add(option.getDisplayableNameWithValue());
                }
            }
        }
    }
    if ((ammo.size() > 0) || (quirksList.length() > 0)) {
        Element svgGroup = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_G_TAG);
        canvas.appendChild(svgGroup);
        lines = 0;
        if (ammo.size() > 0) {
            lines = addMultilineTextElement(svgGroup, viewX + viewWidth * 0.025, 0, viewWidth * 0.95, lineHeight, "Ammo: " + ammo.entrySet().stream().map(e -> String.format("(%s) %d", e.getKey(), e.getValue())).collect(Collectors.joining(", ")), fontSize, "start", "normal");
        }
        if (quirksList.length() > 0) {
            lines += addMultilineTextElement(svgGroup, viewX + viewWidth * 0.025, lines * lineHeight, viewWidth * 0.95, lineHeight, "Quirks: " + quirksList.toString(), fontSize, "start", "normal");
        }
        svgGroup.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, String.format("translate(0,%f)", viewY + viewHeight - lines * lineHeight));
    }
}
Also used : IOptionGroup(megamek.common.options.IOptionGroup) MiscType(megamek.common.MiscType) SVGRectElement(org.w3c.dom.svg.SVGRectElement) Element(org.w3c.dom.Element) Rectangle2D(java.awt.geom.Rectangle2D) IOption(megamek.common.options.IOption) TreeMap(java.util.TreeMap) Quirks(megamek.common.options.Quirks) AmmoType(megamek.common.AmmoType) RecordSheetEquipmentLine(megameklab.com.util.RecordSheetEquipmentLine) Mounted(megamek.common.Mounted) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) StringJoiner(java.util.StringJoiner)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 StringJoiner (java.util.StringJoiner)1 TreeMap (java.util.TreeMap)1 AmmoType (megamek.common.AmmoType)1 MiscType (megamek.common.MiscType)1 Mounted (megamek.common.Mounted)1 IOption (megamek.common.options.IOption)1 IOptionGroup (megamek.common.options.IOptionGroup)1 Quirks (megamek.common.options.Quirks)1 RecordSheetEquipmentLine (megameklab.com.util.RecordSheetEquipmentLine)1 Element (org.w3c.dom.Element)1 SVGRectElement (org.w3c.dom.svg.SVGRectElement)1