Search in sources :

Example 1 with RiskLevel

use of mudmap2.backend.RiskLevel in project mudmap2 by Neop.

the class PlaceDialog method create.

/**
 * Creates the dialog
 */
@Override
void create() {
    setLayout(new GridLayout(0, 2, 4, 4));
    add(new JLabel("Name"));
    if (place != null)
        textfield_name = new JTextField(place.getName());
    else
        textfield_name = new JTextField();
    add(textfield_name);
    place_group_null = new PlaceGroup("none", null);
    add(new JLabel("Place group"));
    combobox_place_group = new JComboBox<>();
    combobox_place_group.addItem(place_group_null);
    for (PlaceGroup a : world.getPlaceGroups()) combobox_place_group.addItem(a);
    if (place != null && place.getPlaceGroup() != null)
        combobox_place_group.setSelectedItem(place.getPlaceGroup());
    add(combobox_place_group);
    add(new JLabel("Risk level"));
    combobox_risk = new JComboBox<>();
    for (RiskLevel rl : world.getRiskLevels()) combobox_risk.addItem(rl);
    if (place != null && place.getRiskLevel() != null)
        combobox_risk.setSelectedItem(place.getRiskLevel());
    add(combobox_risk);
    Integer min_lvl = 0, max_lvl = 0;
    if (place != null) {
        min_lvl = place.getRecLevelMin();
        max_lvl = place.getRecLevelMax();
    }
    add(new JLabel("Minimal level"));
    spinner_rec_lvl_min = new JSpinner();
    spinner_rec_lvl_min.setModel(new SpinnerNumberModel(max(min_lvl, 0), 0, 1000, 1));
    add(spinner_rec_lvl_min);
    if (min_lvl < 0)
        spinner_rec_lvl_min.setEnabled(false);
    add(new JLabel());
    add(checkbox_lvl_min = new JCheckBox("Show min level"));
    checkbox_lvl_min.setSelected(min_lvl >= 0);
    checkbox_lvl_min.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            spinner_rec_lvl_min.setEnabled(((JCheckBox) ae.getSource()).isSelected());
        }
    });
    add(new JLabel("Maximum level"));
    spinner_rec_lvl_max = new JSpinner();
    spinner_rec_lvl_max.setModel(new SpinnerNumberModel(max(max_lvl, 0), 0, 1000, 1));
    add(spinner_rec_lvl_max);
    if (max_lvl < 0)
        spinner_rec_lvl_max.setEnabled(false);
    add(new JLabel());
    add(checkbox_lvl_max = new JCheckBox("Show max level"));
    checkbox_lvl_max.setSelected(max_lvl >= 0);
    checkbox_lvl_max.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            spinner_rec_lvl_max.setEnabled(((JCheckBox) ae.getSource()).isSelected());
        }
    });
    JButton button_cancel = new JButton("Cancel");
    add(button_cancel);
    button_cancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            dispose();
        }
    });
    JButton button_ok = new JButton("Ok");
    add(button_ok);
    getRootPane().setDefaultButton(button_ok);
    button_ok.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            save();
            dispose();
        }
    });
    // listener for escape key
    getRootPane().registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            dispose();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
    pack();
    setLocation(getParent().getX() + (getParent().getWidth() - getWidth()) / 2, getParent().getY() + (getParent().getHeight() - getHeight()) / 2);
}
Also used : ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) SpinnerNumberModel(javax.swing.SpinnerNumberModel) JCheckBox(javax.swing.JCheckBox) GridLayout(java.awt.GridLayout) ActionListener(java.awt.event.ActionListener) JSpinner(javax.swing.JSpinner) PlaceGroup(mudmap2.backend.PlaceGroup) RiskLevel(mudmap2.backend.RiskLevel)

Example 2 with RiskLevel

use of mudmap2.backend.RiskLevel in project mudmap2 by Neop.

the class PlaceDialog method save.

/**
 * Saves the place data
 */
public void save() {
    // if(!textfield_name.getText().isEmpty()){ // name not empty
    try {
        if (layer == null)
            layer = world.getNewLayer();
        // create place if it doesn't exist else just set the name
        if (place == null)
            layer.put(place = new Place(textfield_name.getText(), px, py, layer));
        else
            place.setName(textfield_name.getText());
        PlaceGroup a = (PlaceGroup) combobox_place_group.getSelectedItem();
        // replace null group with null
        place.setPlaceGroup(a != place_group_null ? a : null);
        place.setRiskLevel((RiskLevel) combobox_risk.getSelectedItem());
        if (checkbox_lvl_min.isSelected()) {
            place.setRecLevelMin((Integer) spinner_rec_lvl_min.getValue());
        } else {
            place.setRecLevelMin(-1);
        }
        if (checkbox_lvl_max.isSelected()) {
            place.setRecLevelMax((Integer) spinner_rec_lvl_max.getValue());
        } else {
            place.setRecLevelMax(-1);
        }
    } catch (Exception ex) {
        Logger.getLogger(PlaceDialog.class.getName()).log(Level.SEVERE, null, ex);
    }
    // }
    getParent().repaint();
}
Also used : PlaceGroup(mudmap2.backend.PlaceGroup) Place(mudmap2.backend.Place)

Example 3 with RiskLevel

use of mudmap2.backend.RiskLevel in project mudmap2 by Neop.

the class EditWorldDialog method create.

@Override
void create() {
    setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    GridBagConstraints constraints_l = new GridBagConstraints();
    GridBagConstraints constraints_r = new GridBagConstraints();
    constraints.insets = constraints_l.insets = constraints_r.insets = new Insets(2, 2, 2, 2);
    constraints_l.fill = GridBagConstraints.HORIZONTAL;
    constraints_r.fill = GridBagConstraints.BOTH;
    constraints_r.gridx = 1;
    constraints_l.weightx = constraints_r.weightx = 1.0;
    constraints_l.gridy = ++constraints_r.gridy;
    add(new JLabel("World name"), constraints_l);
    add(textfield_name = new JTextField(world.getName()), constraints_r);
    textfield_name.setColumns(20);
    constraints_l.gridy = ++constraints_r.gridy;
    constraints.gridy = constraints_l.gridy = ++constraints_r.gridy;
    constraints.gridwidth = 2;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    add(new JSeparator(), constraints);
    constraints.gridy = constraints_l.gridy = ++constraints_r.gridy;
    add(new JLabel("Risk Levels"), constraints);
    risklevel_colors = new HashMap<>();
    for (RiskLevel rl : world.getRiskLevels()) {
        constraints_l.gridy = ++constraints_r.gridy;
        JTextField tf_rl_name = new JTextField(rl.getDescription());
        add(tf_rl_name, constraints_l);
        ColorChooserButton colorchooser = new ColorChooserButton(getParent(), rl.getColor());
        add(colorchooser, constraints_r);
        risklevel_colors.put(rl, new Pair<>(tf_rl_name, colorchooser));
    }
    constraints_l.gridy = ++constraints_r.gridy;
    add(risklevel_new_name = new JTextField(), constraints_l);
    add(risklevel_new_color = new ColorChooserButton(getParent()), constraints_r);
    constraints_l.gridy = ++constraints_r.gridy;
    add(new JLabel("Tile center color"), constraints_l);
    add(tile_center_color = new ColorChooserButton(getParent(), world.getTileCenterColor()), constraints_r);
    constraints.gridy = constraints_l.gridy = ++constraints_r.gridy;
    add(new JSeparator(), constraints);
    buttongroup_place_id = new ButtonGroup();
    radiobutton_place_id_none = new JRadioButton("Don't show place ID on map");
    radiobutton_place_id_unique = new JRadioButton("Show place ID if name isn't unique");
    radiobutton_place_id_all = new JRadioButton("Always show place ID");
    buttongroup_place_id.add(radiobutton_place_id_none);
    buttongroup_place_id.add(radiobutton_place_id_unique);
    buttongroup_place_id.add(radiobutton_place_id_all);
    constraints_l.gridy = ++constraints_r.gridy;
    add(radiobutton_place_id_none, constraints_l);
    /*constraints_l.gridy = ++constraints_r.gridy; // feature (temporarily?) removed
        add(radiobutton_place_id_unique, constraints_l);*/
    constraints_l.gridy = ++constraints_r.gridy;
    add(radiobutton_place_id_all, constraints_l);
    switch(world.getShowPlaceId()) {
        case NONE:
            buttongroup_place_id.setSelected(radiobutton_place_id_none.getModel(), true);
            break;
        default:
        case UNIQUE:
            buttongroup_place_id.setSelected(radiobutton_place_id_unique.getModel(), true);
            break;
        case ALL:
            buttongroup_place_id.setSelected(radiobutton_place_id_all.getModel(), true);
            break;
    }
    constraints_l.insets = constraints_r.insets = new Insets(0, 2, 0, 2);
    constraints_l.gridy = ++constraints_r.gridy;
    JButton button_cancel = new JButton("Cancel");
    add(button_cancel, constraints_l);
    button_cancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            dispose();
        }
    });
    JButton button_ok = new JButton("Ok");
    add(button_ok, constraints_r);
    getRootPane().setDefaultButton(button_ok);
    button_ok.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                save();
            } catch (Exception ex) {
                Logger.getLogger(EditWorldDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
            dispose();
        }
    });
    pack();
    setResizable(false);
    setLocation(getParent().getX() + (getParent().getWidth() - getWidth()) / 2, getParent().getY() + (getParent().getHeight() - getHeight()) / 2);
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) JRadioButton(javax.swing.JRadioButton) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) JSeparator(javax.swing.JSeparator) ActionListener(java.awt.event.ActionListener) ColorChooserButton(mudmap2.frontend.GUIElement.ColorChooserButton) ButtonGroup(javax.swing.ButtonGroup) RiskLevel(mudmap2.backend.RiskLevel)

Example 4 with RiskLevel

use of mudmap2.backend.RiskLevel in project mudmap2 by Neop.

the class EditWorldDialog method save.

/**
 * Saves the changes
 */
private void save() throws Exception {
    String name = textfield_name.getText();
    // if textfield is not empty
    if (!name.isEmpty()) {
        world.setName(name);
    }
    // modify risk levels
    for (Map.Entry<RiskLevel, Pair<JTextField, ColorChooserButton>> rl_color : risklevel_colors.entrySet()) {
        String description = rl_color.getValue().first.getText();
        if (description.isEmpty())
            world.removeRiskLevel(rl_color.getKey());
        else {
            rl_color.getKey().setDescription(description);
            rl_color.getKey().setColor(rl_color.getValue().second.getColor());
        }
    }
    world.setTileCenterColor(tile_center_color.getColor());
    // add new risk level, if name not empty
    String name_new = risklevel_new_name.getText();
    if (!name_new.isEmpty()) {
        world.addRiskLevel(new RiskLevel(name_new, risklevel_new_color.getColor()));
    }
    ButtonModel selection = buttongroup_place_id.getSelection();
    if (selection == radiobutton_place_id_none.getModel())
        world.setShowPlaceID(World.ShowPlaceID.NONE);
    else if (selection == radiobutton_place_id_all.getModel())
        world.setShowPlaceID(World.ShowPlaceID.ALL);
    else
        world.setShowPlaceID(World.ShowPlaceID.UNIQUE);
    getParent().repaint();
}
Also used : ButtonModel(javax.swing.ButtonModel) HashMap(java.util.HashMap) Map(java.util.Map) RiskLevel(mudmap2.backend.RiskLevel) Pair(mudmap2.utils.Pair)

Example 5 with RiskLevel

use of mudmap2.backend.RiskLevel in project mudmap2 by Neop.

the class WorldFileJSON method writeFile.

/**
 * Write world to file
 * @param world
 * @throws java.io.IOException
 */
@Override
public void writeFile(World world) throws IOException {
    JSONObject root = new JSONObject();
    // metaWriter data
    // mudmap version
    String mudmapVer = getClass().getPackage().getImplementationVersion();
    if (mudmapVer != null)
        root.put("mudmapVer", getClass().getPackage().getImplementationVersion());
    else
        root.put("mudmapVer", "dev");
    // file version
    root.put("fileVer", versionMajor + "." + versionMinor);
    // world name
    root.put("worldName", world.getName());
    root.put("showPlaceID", world.getShowPlaceId());
    // tile center color
    if (world.getTileCenterColor() != null) {
        root.put("tileCenterCol", colToHex(world.getTileCenterColor()));
    }
    // cardinal and non cardinal path color
    if (world.getPathColor() != null) {
        root.put("pathCol", colToHex(world.getPathColor()));
    }
    if (world.getPathColorNstd() != null) {
        root.put("pathColNonCardinal", colToHex(world.getPathColorNstd()));
    }
    // other path colors
    JSONArray pathColorsArray = new JSONArray();
    root.put("pathColDefs", pathColorsArray);
    for (Map.Entry<String, Color> pathCol : world.getPathColors().entrySet()) {
        if (pathCol.getValue() != null) {
            JSONObject pathColObj = new JSONObject();
            pathColObj.put("path", pathCol.getKey());
            pathColObj.put("col", colToHex(pathCol.getValue()));
            pathColorsArray.put(pathColObj);
        }
    }
    // risk level colors
    JSONArray riskLevelColors = new JSONArray();
    root.put("riskLevels", riskLevelColors);
    for (RiskLevel rlc : world.getRiskLevels()) {
        JSONObject rlo = new JSONObject();
        rlo.put("id", rlc.getId());
        rlo.put("desc", rlc.getDescription());
        rlo.put("col", colToHex(rlc.getColor()));
        riskLevelColors.put(rlo);
    }
    // areaArray
    // create IDs for areaArray
    HashMap<PlaceGroup, Integer> areaIDs = new HashMap<>();
    // incremental id
    Integer cnt = 0;
    for (PlaceGroup a : world.getPlaceGroups()) {
        Boolean inUse = false;
        // removePlace unused
        for (Layer layer : world.getLayers()) {
            for (Place place : layer.getPlaces()) {
                if (place.getPlaceGroup() == a) {
                    inUse = true;
                    break;
                }
            }
        }
        if (inUse)
            areaIDs.put(a, ++cnt);
    }
    // add areaArray
    JSONArray areas = new JSONArray();
    root.put("areas", areas);
    for (PlaceGroup area : world.getPlaceGroups()) {
        JSONObject areaObj = new JSONObject();
        areaObj.put("id", areaIDs.get(area));
        areaObj.put("name", area.getName());
        areaObj.put("col", colToHex(area.getColor()));
        areas.put(areaObj);
    }
    // helper to assign new layer ids
    Integer nextLayerID = 0;
    layerIDs = new HashMap<>();
    // layers (for quadtree optimization
    JSONArray layers = new JSONArray();
    root.put("layers", layers);
    for (Layer layer : world.getLayers()) {
        if (!layer.getPlaces().isEmpty()) {
            JSONObject layerObj = new JSONObject();
            // add layer to id map
            Integer layerID = nextLayerID++;
            layerIDs.put(layer.getId(), layerID);
            layerObj.put("id", layerID);
            layerObj.put("centerX", layer.getCenterX());
            layerObj.put("centerY", layer.getCenterY());
            if (layer.hasName())
                layerObj.put("name", layer.getName());
            layers.put(layerObj);
        }
    }
    // places
    JSONArray places = new JSONArray();
    root.put("places", places);
    for (Layer layer : world.getLayers()) {
        for (Place place : layer.getPlaces()) {
            JSONObject placeObj = new JSONObject();
            placeObj.put("id", place.getId());
            placeObj.put("n", place.getName());
            placeObj.put("l", translateLayerID(place.getLayer().getId()));
            placeObj.put("x", place.getX());
            placeObj.put("y", place.getY());
            if (place.getPlaceGroup() != null)
                placeObj.put("a", areaIDs.get(place.getPlaceGroup()));
            if (place.getRiskLevel() != null)
                placeObj.put("r", place.getRiskLevel().getId());
            if (place.getRecLevelMin() > -1)
                placeObj.put("lvlMin", place.getRecLevelMin());
            if (place.getRecLevelMax() > -1)
                placeObj.put("lvlMax", place.getRecLevelMax());
            // child places
            if (!place.getChildren().isEmpty()) {
                JSONArray children = new JSONArray();
                placeObj.put("c", children);
                for (Place child : place.getChildren()) {
                    children.put(child.getId());
                }
            }
            // parent places
            if (!place.getParents().isEmpty()) {
                JSONArray parents = new JSONArray();
                placeObj.put("p", parents);
                for (Place parent : place.getParents()) {
                    parents.put(parent.getId());
                }
            }
            // flags
            if (!place.getFlags().isEmpty()) {
                JSONArray flags = new JSONArray();
                placeObj.put("f", flags);
                for (Map.Entry<String, Boolean> flag : place.getFlags().entrySet()) {
                    if (flag.getValue())
                        flags.put(flag.getKey());
                }
            }
            // comments
            if (!place.getComments().isEmpty()) {
                JSONArray comments = new JSONArray();
                placeObj.put("co", comments);
                int idx = 0;
                for (String comment : place.getComments()) {
                    comments.put(idx++, comment);
                }
            }
            places.put(placeObj);
        }
    }
    // paths
    JSONArray pathsArray = new JSONArray();
    root.put("paths", pathsArray);
    // paths that have already been added
    HashSet<Path> paths = new HashSet<>();
    for (Layer layer : world.getLayers()) {
        for (Place place : layer.getPlaces()) {
            for (Path path : place.getPaths()) {
                if (!paths.contains(path)) {
                    JSONArray pathObj = new JSONArray();
                    JSONObject p1 = new JSONObject();
                    p1.put("p", path.getPlaces()[0].getId());
                    p1.put("e", path.getExit(path.getPlaces()[0]));
                    pathObj.put(p1);
                    JSONObject p2 = new JSONObject();
                    p2.put("p", path.getPlaces()[1].getId());
                    p2.put("e", path.getExit(path.getPlaces()[1]));
                    pathObj.put(p2);
                    pathsArray.put(pathObj);
                    paths.add(path);
                }
            }
        }
    }
    // home position
    WorldCoordinate home = world.getHome();
    JSONObject obj = new JSONObject();
    obj.put("l", translateLayerID(home.getLayer()));
    obj.put("x", home.getX());
    obj.put("y", home.getY());
    root.put("home", obj);
    // add metaWriter data from WorldTab
    if (metaWriter != null)
        root.put("meta", metaWriter.getMeta(layerIDs));
    try (FileWriter writer = new FileWriter(filename)) {
        // indentation for better readability (for debugging), increases file size
        // root.write(writer, 4, 0);
        root.write(writer);
        fileRoot = root;
    } catch (Exception ex) {
        System.out.println(ex.getLocalizedMessage());
    }
}
Also used : Path(mudmap2.backend.Path) WorldCoordinate(mudmap2.backend.WorldCoordinate) HashMap(java.util.HashMap) Color(java.awt.Color) FileWriter(java.io.FileWriter) Layer(mudmap2.backend.Layer) WorldFileInvalidTypeException(mudmap2.backend.WorldFileReader.Exception.WorldFileInvalidTypeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PlaceGroup(mudmap2.backend.PlaceGroup) HashMap(java.util.HashMap) Map(java.util.Map) RiskLevel(mudmap2.backend.RiskLevel) Place(mudmap2.backend.Place) HashSet(java.util.HashSet)

Aggregations

RiskLevel (mudmap2.backend.RiskLevel)5 PlaceGroup (mudmap2.backend.PlaceGroup)4 HashMap (java.util.HashMap)3 Place (mudmap2.backend.Place)3 Color (java.awt.Color)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 JButton (javax.swing.JButton)2 JLabel (javax.swing.JLabel)2 JTextField (javax.swing.JTextField)2 Layer (mudmap2.backend.Layer)2 Path (mudmap2.backend.Path)2 WorldCoordinate (mudmap2.backend.WorldCoordinate)2 WorldFileInvalidTypeException (mudmap2.backend.WorldFileReader.Exception.WorldFileInvalidTypeException)2 GridBagConstraints (java.awt.GridBagConstraints)1 GridBagLayout (java.awt.GridBagLayout)1 GridLayout (java.awt.GridLayout)1 Insets (java.awt.Insets)1