Search in sources :

Example 1 with PilotOptions

use of megamek.common.options.PilotOptions in project megameklab by MegaMek.

the class PrintEntity method writeTextFields.

protected void writeTextFields() {
    setTextField("title", getRecordSheetTitle().toUpperCase());
    setTextField("type", getEntity().getShortNameRaw());
    setTextField("mpWalk", formatWalk());
    setTextField("mpRun", formatRun());
    setTextField("mpJump", formatJump());
    setTextField("tonnage", Integer.toString((int) getEntity().getWeight()));
    setTextField("techBase", formatTechBase());
    setTextField("rulesLevel", formatRulesLevel());
    setTextField("era", formatEra(getEntity().getYear()));
    setTextField("cost", formatCost());
    setTextField("bv", Integer.toString(getEntity().calculateBattleValue()));
    UnitRole role = UnitRoleHandler.getRoleFor(getEntity());
    if (role == UnitRole.UNDETERMINED) {
        hideElement("lblRole", true);
        hideElement("role", true);
    } else {
        setTextField("role", role.toString());
    }
    // This will require building the graphics tree so we measure the elements.
    if (getEntity().getCrew().getCrewType() != CrewType.SINGLE) {
        build();
    }
    for (int i = 0; i < getEntity().getCrew().getSlotCount(); i++) {
        // If we have multiple named crew for the unit, change the "Name:" label to
        // the label of the slot. This will usually require adjusting the position of the
        // name or the length of the blank.
        double nameOffset = 0;
        if (getEntity().getCrew().getSlotCount() > 1) {
            Element element = getSVGDocument().getElementById("crewName" + i);
            if (null != element) {
                float oldWidth = ((SVGTextContentElement) element).getComputedTextLength();
                element.setTextContent(getEntity().getCrew().getCrewType().getRoleName(i) + ":");
                nameOffset = SVGLocatableSupport.getBBox(element).getWidth() - oldWidth;
            }
        }
        if (!getEntity().getCrew().getName().equalsIgnoreCase("unnamed")) {
            Element element = getSVGDocument().getElementById("blanksCrew" + i);
            if (null != element) {
                hideElement(element);
            }
            if (nameOffset != 0) {
                element = getSVGDocument().getElementById("pilotName" + i);
                if (null != element) {
                    double offset = nameOffset;
                    String prev = element.getAttribute(SVGConstants.SVG_X_ATTRIBUTE);
                    if (null != prev) {
                        offset += Double.parseDouble(prev);
                    } else {
                        offset += ((SVGTextContentElement) element).getStartPositionOfChar(0).getX();
                    }
                    element.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE, Double.toString(offset));
                }
            }
            setTextField("pilotName" + i, getEntity().getCrew().getName(i), true);
            setTextField("gunnerySkill" + i, Integer.toString(getEntity().getCrew().getGunnery(i)), true);
            setTextField("pilotingSkill" + i, Integer.toString(getEntity().getCrew().getPiloting(i)), true);
            StringJoiner spaList = new StringJoiner(", ");
            PilotOptions spas = getEntity().getCrew().getOptions();
            for (Enumeration<IOptionGroup> optionGroups = spas.getGroups(); optionGroups.hasMoreElements(); ) {
                IOptionGroup optiongroup = optionGroups.nextElement();
                if (spas.count(optiongroup.getKey()) > 0) {
                    for (Enumeration<IOption> options = optiongroup.getOptions(); options.hasMoreElements(); ) {
                        IOption option = options.nextElement();
                        if (option != null && option.booleanValue()) {
                            spaList.add(option.getDisplayableNameWithValue().replaceAll(" \\(.*?\\)", ""));
                        }
                    }
                }
            }
            if (spaList.length() > 0) {
                Element rect = getSVGDocument().getElementById("spas" + getEntity().getCrew().getSlotCount());
                if ((null != rect) && (rect instanceof SVGRectElement)) {
                    Rectangle2D bbox = getRectBBox((SVGRectElement) rect);
                    Element canvas = (Element) ((Node) rect).getParentNode();
                    String spaText = "Abilities: " + spaList.toString();
                    float fontSize = FONT_SIZE_MEDIUM;
                    if (getTextLength(spaText, fontSize) > bbox.getWidth()) {
                        fontSize = (float) bbox.getHeight() / 2.4f;
                    }
                    double lineHeight = fontSize * 1.2;
                    addMultilineTextElement(canvas, bbox.getX(), bbox.getY() + lineHeight, bbox.getWidth(), lineHeight, spaText, fontSize, "start", "normal", "black", ' ');
                }
            }
        } else {
            setTextField("pilotName" + i, null);
            setTextField("gunnerySkill" + i, null);
            setTextField("pilotingSkill" + i, null);
            if (nameOffset != 0) {
                Element element = getSVGDocument().getElementById("blankCrewName" + i);
                if (null != element) {
                    float w = ((SVGGraphicsElement) element).getBBox().getWidth();
                    element.setAttributeNS(null, SVGConstants.SVG_D_ATTRIBUTE, String.format("M %f,0 %f,0", nameOffset, w - nameOffset));
                }
            }
        }
    }
}
Also used : IOptionGroup(megamek.common.options.IOptionGroup) PilotOptions(megamek.common.options.PilotOptions) SVGGraphicsElement(org.apache.batik.anim.dom.SVGGraphicsElement) SVGRectElement(org.w3c.dom.svg.SVGRectElement) Element(org.w3c.dom.Element) SVGTextContentElement(org.w3c.dom.svg.SVGTextContentElement) SVGTextContentElement(org.w3c.dom.svg.SVGTextContentElement) IOption(megamek.common.options.IOption) Rectangle2D(java.awt.geom.Rectangle2D) SVGRectElement(org.w3c.dom.svg.SVGRectElement) UnitRole(megamek.common.UnitRole) StringJoiner(java.util.StringJoiner)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)1 StringJoiner (java.util.StringJoiner)1 UnitRole (megamek.common.UnitRole)1 IOption (megamek.common.options.IOption)1 IOptionGroup (megamek.common.options.IOptionGroup)1 PilotOptions (megamek.common.options.PilotOptions)1 SVGGraphicsElement (org.apache.batik.anim.dom.SVGGraphicsElement)1 Element (org.w3c.dom.Element)1 SVGRectElement (org.w3c.dom.svg.SVGRectElement)1 SVGTextContentElement (org.w3c.dom.svg.SVGTextContentElement)1