Search in sources :

Example 1 with NumberProperty

use of games.strategy.engine.data.properties.NumberProperty in project triplea by triplea-game.

the class GameParser method parseProperties.

private void parseProperties(final Node root) throws GameParseException {
    final Collection<String> runningList = new ArrayList<>();
    final GameProperties properties = data.getProperties();
    for (final Element current : getChildren("property", root)) {
        final String editable = current.getAttribute("editable");
        final String property = current.getAttribute("name");
        String value = current.getAttribute("value");
        runningList.add(property);
        if (value == null || value.length() == 0) {
            final List<Element> valueChildren = getChildren("value", current);
            if (!valueChildren.isEmpty()) {
                final Element valueNode = valueChildren.get(0);
                if (valueNode != null) {
                    value = valueNode.getTextContent();
                }
            }
        }
        if (editable != null && editable.equalsIgnoreCase("true")) {
            parseEditableProperty(current, property, value);
        } else {
            final List<Node> children2 = getNonTextNodesIgnoring(current, "value");
            if (children2.size() == 0) {
                // definition
                try {
                    // test if it is an integer
                    final int integer = Integer.parseInt(value);
                    properties.set(property, integer);
                } catch (final NumberFormatException e) {
                    // then it must be a string
                    properties.set(property, value);
                }
            } else {
                final String type = children2.get(0).getNodeName();
                if (type.equals("boolean")) {
                    properties.set(property, Boolean.valueOf(value));
                } else if (type.equals("file")) {
                    properties.set(property, new File(value));
                } else if (type.equals("number")) {
                    int intValue = 0;
                    if (value != null) {
                        try {
                            intValue = Integer.parseInt(value);
                        } catch (final NumberFormatException e) {
                        // value already 0
                        }
                    }
                    properties.set(property, intValue);
                } else {
                    properties.set(property, value);
                }
            }
        }
    }
    data.getPlayerList().forEach(playerId -> data.getProperties().addPlayerProperty(new NumberProperty(Constants.getIncomePercentageFor(playerId), null, 999, 0, 100)));
    data.getPlayerList().forEach(playerId -> data.getProperties().addPlayerProperty(new NumberProperty(Constants.getPuIncomeBonus(playerId), null, 999, 0, 0)));
}
Also used : GameProperties(games.strategy.engine.data.properties.GameProperties) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) File(java.io.File) NumberProperty(games.strategy.engine.data.properties.NumberProperty)

Example 2 with NumberProperty

use of games.strategy.engine.data.properties.NumberProperty in project triplea by triplea-game.

the class GameParser method parseEditableProperty.

private void parseEditableProperty(final Element property, final String name, final String defaultValue) throws GameParseException {
    // what type
    final List<Node> children = getNonTextNodes(property);
    if (children.size() != 1) {
        throw newGameParseException("Editable properties must have exactly 1 child specifying the type. Number of children found:" + children.size() + " for node:" + property.getNodeName());
    }
    final Element child = (Element) children.get(0);
    final String childName = child.getNodeName();
    final IEditableProperty editableProperty;
    if (childName.equals("boolean")) {
        editableProperty = new BooleanProperty(name, null, Boolean.valueOf(defaultValue));
    } else if (childName.equals("file")) {
        editableProperty = new FileProperty(name, null, defaultValue);
    } else if (childName.equals("list") || childName.equals("combo")) {
        final StringTokenizer tokenizer = new StringTokenizer(child.getAttribute("values"), ",");
        final Collection<String> values = new ArrayList<>();
        while (tokenizer.hasMoreElements()) {
            values.add(tokenizer.nextToken());
        }
        editableProperty = new ComboProperty<>(name, null, defaultValue, values);
    } else if (childName.equals("number")) {
        final int max = Integer.valueOf(child.getAttribute("max"));
        final int min = Integer.valueOf(child.getAttribute("min"));
        final int def = Integer.valueOf(defaultValue);
        editableProperty = new NumberProperty(name, null, max, min, def);
    } else if (childName.equals("color")) {
        // Parse the value as a hexidecimal number
        final int def = Integer.valueOf(defaultValue, 16);
        editableProperty = new ColorProperty(name, null, def);
    } else if (childName.equals("string")) {
        editableProperty = new StringProperty(name, null, defaultValue);
    } else {
        throw newGameParseException("Unrecognized property type:" + childName);
    }
    data.getProperties().addEditableProperty(editableProperty);
}
Also used : BooleanProperty(games.strategy.engine.data.properties.BooleanProperty) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) StringProperty(games.strategy.engine.data.properties.StringProperty) IEditableProperty(games.strategy.engine.data.properties.IEditableProperty) StringTokenizer(java.util.StringTokenizer) FileProperty(games.strategy.engine.data.properties.FileProperty) NumberProperty(games.strategy.engine.data.properties.NumberProperty) ColorProperty(games.strategy.engine.data.properties.ColorProperty)

Example 3 with NumberProperty

use of games.strategy.engine.data.properties.NumberProperty in project triplea by triplea-game.

the class ViewMenu method addMapFontAndColorEditorMenu.

private void addMapFontAndColorEditorMenu() {
    final Action mapFontOptions = SwingAction.of("Edit Map Font and Color", e -> {
        final List<IEditableProperty> properties = new ArrayList<>();
        final NumberProperty fontsize = new NumberProperty("Font Size", null, 60, 0, MapImage.getPropertyMapFont().getSize());
        final ColorProperty territoryNameColor = new ColorProperty("Territory Name and PU Color", null, MapImage.getPropertyTerritoryNameAndPuAndCommentColor());
        final ColorProperty unitCountColor = new ColorProperty("Unit Count Color", null, MapImage.getPropertyUnitCountColor());
        final ColorProperty factoryDamageColor = new ColorProperty("Factory Damage Color", null, MapImage.getPropertyUnitFactoryDamageColor());
        final ColorProperty hitDamageColor = new ColorProperty("Hit Damage Color", null, MapImage.getPropertyUnitHitDamageColor());
        properties.add(fontsize);
        properties.add(territoryNameColor);
        properties.add(unitCountColor);
        properties.add(factoryDamageColor);
        properties.add(hitDamageColor);
        final PropertiesUi pui = new PropertiesUi(properties, true);
        final JPanel ui = new JPanel();
        ui.setLayout(new BorderLayout());
        ui.add(pui, BorderLayout.CENTER);
        ui.add(new JLabel("<html>Change the font and color of 'text' (not pictures) on the map. " + "<br /><em>(Some people encounter problems with the color picker, and this " + "<br />is a bug outside of triplea, located in the 'look and feel' that " + "<br />you are using. If you have an error come up, try switching to the " + "<br />basic 'look and feel', then setting the color, then switching back.)</em></html>"), BorderLayout.NORTH);
        final Object[] options = { "Set Properties", "Reset To Default", "Cancel" };
        final int result = JOptionPane.showOptionDialog(frame, ui, "Edit Map Font and Color", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, 2);
        if (result == 1) {
            MapImage.resetPropertyMapFont();
            MapImage.resetPropertyTerritoryNameAndPuAndCommentColor();
            MapImage.resetPropertyUnitCountColor();
            MapImage.resetPropertyUnitFactoryDamageColor();
            MapImage.resetPropertyUnitHitDamageColor();
            frame.getMapPanel().resetMap();
        } else if (result == 0) {
            MapImage.setPropertyMapFont(new Font("Ariel", Font.BOLD, fontsize.getValue()));
            MapImage.setPropertyTerritoryNameAndPuAndCommentColor((Color) territoryNameColor.getValue());
            MapImage.setPropertyUnitCountColor((Color) unitCountColor.getValue());
            MapImage.setPropertyUnitFactoryDamageColor((Color) factoryDamageColor.getValue());
            MapImage.setPropertyUnitHitDamageColor((Color) hitDamageColor.getValue());
            frame.getMapPanel().resetMap();
        }
    });
    add(mapFontOptions).setMnemonic(KeyEvent.VK_C);
}
Also used : JPanel(javax.swing.JPanel) AbstractAction(javax.swing.AbstractAction) SwingAction(games.strategy.ui.SwingAction) Action(javax.swing.Action) Color(java.awt.Color) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) PropertiesUi(games.strategy.engine.data.properties.PropertiesUi) Font(java.awt.Font) IEditableProperty(games.strategy.engine.data.properties.IEditableProperty) BorderLayout(java.awt.BorderLayout) NumberProperty(games.strategy.engine.data.properties.NumberProperty) ColorProperty(games.strategy.engine.data.properties.ColorProperty)

Aggregations

NumberProperty (games.strategy.engine.data.properties.NumberProperty)3 ArrayList (java.util.ArrayList)3 ColorProperty (games.strategy.engine.data.properties.ColorProperty)2 IEditableProperty (games.strategy.engine.data.properties.IEditableProperty)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 BooleanProperty (games.strategy.engine.data.properties.BooleanProperty)1 FileProperty (games.strategy.engine.data.properties.FileProperty)1 GameProperties (games.strategy.engine.data.properties.GameProperties)1 PropertiesUi (games.strategy.engine.data.properties.PropertiesUi)1 StringProperty (games.strategy.engine.data.properties.StringProperty)1 SwingAction (games.strategy.ui.SwingAction)1 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1 Font (java.awt.Font)1 File (java.io.File)1 StringTokenizer (java.util.StringTokenizer)1 AbstractAction (javax.swing.AbstractAction)1 Action (javax.swing.Action)1 JLabel (javax.swing.JLabel)1