Search in sources :

Example 16 with World

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

the class WorldTabTest method testGetWorld.

/**
 * Test of getWorld method, of class WorldTab.
 */
@Test
public void testGetWorld() {
    System.out.println("getWorld");
    WorldTab instance = new WorldTab(null, new World(), false);
    assertNotNull(instance.getWorld());
    instance = new WorldTab(null, new World(), true);
    assertNotNull(instance.getWorld());
    instance = new WorldTab(null, new World(), "", false);
    assertNotNull(instance.getWorld());
    instance = new WorldTab(null, new World(), "", true);
    assertNotNull(instance.getWorld());
    instance = new WorldTab(instance);
    assertNotNull(instance.getWorld());
}
Also used : World(mudmap2.backend.World) Test(org.junit.Test)

Example 17 with World

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

the class WorldTabTest method testCreateLayer.

/**
 * Test of save method, of class WorldTab.
 */
/*@Test
    public void testSave() {
        System.out.println("save");
        WorldTab instance = null;
        instance.save();
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }*/
/**
 * Test of showMessage method, of class WorldTab.
 */
/*@Test
    public void testShowMessage() {
        System.out.println("showMessage");
        String message = "";
        WorldTab instance = null;
        instance.showMessage(message);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }*/
/**
 * Test of layerSelected method, of class WorldTab.
 */
/*@Test
    public void testLayerSelected() {
        System.out.println("layerSelected");
        Layer layer = null;
        MouseEvent event = null;
        WorldTab instance = null;
        instance.layerSelected(layer, event);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }*/
/**
 * Test of createLayer method, of class WorldTab.
 */
@Test
public void testCreateLayer() {
    System.out.println("createLayer");
    World world = new World();
    WorldTab instance = new WorldTab(null, world, false);
    int layerCnt = world.getLayers().size();
    instance.createLayer();
    assertEquals(layerCnt + 1, world.getLayers().size());
}
Also used : World(mudmap2.backend.World) Test(org.junit.Test)

Example 18 with World

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

the class MapPainterDefault method paint.

@Override
public void paint(Graphics g, int tileSize, double graphicsWidth, double graphicsHeight, Layer layer, WorldCoordinate curPos) {
    this.graphicsWidth = graphicsWidth;
    this.graphicsHeight = graphicsHeight;
    this.tileSize = tileSize;
    this.curPos = curPos;
    tileFont = g.getFont();
    final float selectionStrokeWidth = getTileSelectionStrokeWidth();
    final int tileBorderWidthScaled = getTileBorderWidth();
    // max number of text lines tht fit in a tile
    FontMetrics fm = g.getFontMetrics();
    final int maxLines = (int) Math.round((double) (tileSize - 3 * (tileBorderWidthScaled + (int) Math.ceil(getRiskLevelStrokeWidth()))) / fm.getHeight());
    final int maxLineLength = tileSize - 2 * (tileBorderWidthScaled + (int) selectionStrokeWidth + (int) Math.ceil(getRiskLevelStrokeWidth()));
    final Boolean drawText = fm.stringWidth("WW") < (tileSize - 2 * (getRiskLevelStrokeWidth() + getTileBorderWidth()));
    // screen center in world coordinates
    // note: wdtwd2
    final double screenCenterX = (graphicsWidth / tileSize) / 2.0;
    final double screenCenterY = (graphicsHeight / tileSize) / 2.0;
    final int placeXOffset = (int) (Math.round((float) curPos.getX()) - Math.round(screenCenterX));
    final int placeYOffset = (int) (Math.round((float) curPos.getY()) - Math.floor(screenCenterY));
    // more precalculation
    final double placeXpxConst = remint(screenCenterX) - remint(curPos.getX());
    final double placeYPXConst = remint(screenCenterY) + remint(curPos.getY());
    // prepare graphic for paths
    // Paths will be drawn on this graphic and later on copied to g
    // to mask out the tile positions on graphic_path
    ArrayList<Pair<Integer, Integer>> tilePositions = new ArrayList<>();
    BufferedImage imagePath = new BufferedImage((int) graphicsWidth, (int) graphicsHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics graphicPath = imagePath.getGraphics();
    ((Graphics2D) graphicPath).setStroke(new BasicStroke(getPathStrokeWidth()));
    ((Graphics2D) graphicPath).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // getPlace the locations of copied places
    HashSet<Pair<Integer, Integer>> copiedPlaceLocations = mudmap2.CopyPaste.getCopyPlaceLocations();
    // clear screen
    if (backgroundColor == null) {
        g.clearRect(0, 0, (int) graphicsWidth + 1, (int) graphicsHeight + 1);
    } else {
        g.setColor(backgroundColor);
        g.fillRect(0, 0, (int) graphicsWidth + 1, (int) graphicsHeight + 1);
    }
    // ------------------ draw the grid --------------------------------
    if (isGridEnabled()) {
        g.setColor(Color.lightGray);
        for (int tileX = (g.getClipBounds().x / tileSize) - 1; tileX < graphicsWidth / tileSize + 1; ++tileX) {
            final int x = (int) Math.round((tileX + placeXpxConst) * tileSize);
            g.drawLine(x, 0, x, (int) graphicsHeight);
        }
        for (int tileY = (g.getClipBounds().y / tileSize) - 1; tileY < graphicsHeight / tileSize + 1; ++tileY) {
            final int y = (int) Math.round((tileY + placeYPXConst) * tileSize);
            g.drawLine(0, y, (int) graphicsWidth, y);
        }
    }
    // ------------------ draw the tiles / places ----------------------
    for (int tileX = (g.getClipBounds().x / tileSize) - 1; tileX < graphicsWidth / tileSize + 1; ++tileX) {
        for (int tileY = (g.getClipBounds().y / tileSize) - 1; tileY < graphicsHeight / tileSize + 1; ++tileY) {
            // place position on the map
            final int placeX = tileX + placeXOffset;
            final int placeY = (int) (graphicsHeight / tileSize) - tileY + placeYOffset;
            if (layer != null && layer.exist(placeX, placeY)) {
                Place curPlace = layer.get(placeX, placeY);
                // place position in pixel on the screen
                final int placeXpx = (int) Math.round((tileX + placeXpxConst) * tileSize);
                final int placeYpx = (int) Math.round((tileY + placeYPXConst) * tileSize);
                tilePositions.add(new Pair<>(placeXpx, placeYpx));
                // number of drawn text lines
                int lineNum = 0;
                // draw place group color
                if (curPlace.getPlaceGroup() != null) {
                    g.setColor(curPlace.getPlaceGroup().getColor());
                    g.fillRect(placeXpx, placeYpx, tileSize, tileSize);
                }
                // draw tile center color
                if (drawText) {
                    g.setColor(layer.getWorld().getTileCenterColor());
                    g.fillRect(placeXpx + tileBorderWidthScaled, placeYpx + tileBorderWidthScaled, tileSize - 2 * tileBorderWidthScaled, tileSize - 2 * tileBorderWidthScaled);
                }
                // draw risk level border
                if (curPlace.getRiskLevel() != null) {
                    g.setColor(curPlace.getRiskLevel().getColor());
                    ((Graphics2D) g).setStroke(new BasicStroke(getRiskLevelStrokeWidth()));
                    g.drawRect(placeXpx + tileBorderWidthScaled, placeYpx + tileBorderWidthScaled, tileSize - 2 * tileBorderWidthScaled - (int) (0.5 * getRiskLevelStrokeWidth()), tileSize - 2 * tileBorderWidthScaled - (int) (0.5 * getRiskLevelStrokeWidth()));
                }
                LinkedList<String> text = new LinkedList<>();
                String flags = "", exits = "";
                // draw text, if tiles are large enough
                if (drawText) {
                    g.setColor(Color.BLACK);
                    // place name
                    // gets place name if unique, else place name with ID
                    String placeName;
                    switch(layer.getWorld().getShowPlaceId()) {
                        default:
                        case UNIQUE:
                        case NONE:
                            // name only
                            placeName = curPlace.getName();
                            break;
                        case ALL:
                            // name and id
                            placeName = curPlace.toString();
                            break;
                    }
                    text.add(placeName);
                    int reclvlmin = curPlace.getRecLevelMin(), reclvlmax = curPlace.getRecLevelMax();
                    if (reclvlmin > -1 || reclvlmax > -1) {
                        String levelString = "lvl " + (reclvlmin > -1 ? reclvlmin : "?") + " - " + (reclvlmax > -1 ? reclvlmax : "?");
                        text.add(levelString);
                    }
                    // parents
                    if (lineNum < maxLines && !curPlace.getParents().isEmpty()) {
                        int parentsNum = curPlace.getParents().size();
                        String paStr = "Pa" + (parentsNum > 1 ? " (" + curPlace.getParents().size() + "): " : ": ");
                        boolean firstParent = true;
                        for (Place parent : curPlace.getParents()) {
                            paStr += (firstParent ? "" : ", ") + parent.getName();
                            firstParent = false;
                        }
                        text.add(paStr);
                    }
                    // children
                    if (lineNum < maxLines && !curPlace.getChildren().isEmpty()) {
                        int childrenNum = curPlace.getChildren().size();
                        String chStr = "Ch" + (childrenNum > 1 ? " (" + curPlace.getChildren().size() + "): " : ": ");
                        boolean firstChild = true;
                        for (Place child : curPlace.getChildren()) {
                            chStr += (firstChild ? "" : ", ") + child.getName();
                            firstChild = false;
                        }
                        text.add(chStr);
                    }
                    // flags
                    if (lineNum < maxLines) {
                        // place has comments
                        if (!curPlace.getComments().isEmpty())
                            flags += "Co";
                        if (!curPlace.getChildren().isEmpty())
                            flags += "Ch";
                        if (!curPlace.getParents().isEmpty())
                            flags += "Pa";
                        // other flags
                        for (Map.Entry<String, Boolean> flag : curPlace.getFlags().entrySet()) {
                            if (flag.getValue())
                                flags += flag.getKey().toUpperCase();
                            if (fm.stringWidth(flags) >= tileSize - 2 * tileBorderWidthScaled)
                                break;
                        }
                    }
                }
                // mark place group selection
                if (isSelected(curPlace) || (mudmap2.CopyPaste.isCut() && mudmap2.CopyPaste.isMarked(curPlace))) {
                    g.setColor(new Color(255, 255, 255, 128));
                    g.fillRect(placeXpx, placeYpx, tileSize, tileSize);
                }
                // draw path lines here
                boolean exitUp = false, exitDown = false, exitnstd = false;
                if (getShowPaths()) {
                    for (Path path : curPlace.getPaths()) {
                        Place otherPlace = path.getOtherPlace(curPlace);
                        Color colorPlace1 = layer.getWorld().getPathColor(path.getExitDirections()[0]);
                        Color colorPlace2 = layer.getWorld().getPathColor(path.getExitDirections()[1]);
                        if (path.getPlaces()[0] != curPlace) {
                            Color tmp = colorPlace1;
                            colorPlace1 = colorPlace2;
                            colorPlace2 = tmp;
                        }
                        // usually the main place (path.getPlaces()[0]) draws the path. If it isn't on screen, the other place draws it
                        if (Objects.equals(otherPlace.getLayer().getId(), layer.getId()) && (path.getPlaces()[0] == curPlace || !isOnScreen(otherPlace))) {
                            Pair<Integer, Integer> exitOffset = getExitOffset(path.getExit(curPlace));
                            Pair<Integer, Integer> exitOffsetOther = getExitOffset(path.getExit(otherPlace));
                            boolean drawCurves = getPathsCurved();
                            // exit positions on the map
                            final double exit1x = placeXpx + exitOffset.first;
                            final double exit1y = placeYpx + exitOffset.second;
                            final double exit2x = placeXpx + (otherPlace.getX() - curPlace.getX()) * tileSize + exitOffsetOther.first;
                            final double exit2y = placeYpx - (otherPlace.getY() - curPlace.getY()) * tileSize + exitOffsetOther.second;
                            if (colorPlace1.equals(colorPlace2)) {
                                // same color
                                ((Graphics2D) graphicPath).setPaint(colorPlace1);
                            } else {
                                // draw gradient
                                GradientPaint gp = new GradientPaint((float) exit1x, (float) exit1y, colorPlace1, (float) exit2x, (float) exit2y, colorPlace2);
                                ((Graphics2D) graphicPath).setPaint(gp);
                            }
                            if (drawCurves) {
                                Pair<Double, Double> normal1 = getExitNormal(path.getExit(curPlace));
                                Pair<Double, Double> normal2 = getExitNormal(path.getExit(otherPlace));
                                double dx = exit2x - exit1x;
                                double dy = exit2y - exit1y;
                                if (drawCurves = Math.sqrt(dx * dx + dy * dy) >= 1.5 * tileSize) {
                                    CubicCurve2D c = new CubicCurve2D.Double();
                                    // point 1
                                    c.setCurve(exit1x, exit1y, // point 2
                                    exit1x + normal1.first * tileSize, exit1y - normal1.second * tileSize, // point 3
                                    exit2x + normal2.first * tileSize, exit2y - normal2.second * tileSize, // point 4
                                    exit2x, exit2y);
                                    ((Graphics2D) graphicPath).draw(c);
                                }
                            }
                            if (!drawCurves) {
                                graphicPath.drawLine((int) exit1x, (int) exit1y, (int) exit2x, (int) exit2y);
                            }
                        }
                        // draw exit dots, if tiles are larger than 20
                        if (tileSize >= 20) {
                            g.setColor(colorPlace1);
                            String exit = path.getExit(curPlace);
                            switch(exit) {
                                case "u":
                                    exitUp = true;
                                    break;
                                case "d":
                                    exitDown = true;
                                    break;
                                default:
                                    Pair<Integer, Integer> exitOffset = getExitOffset(exit);
                                    if (exitOffset.first != tileSize / 2 || exitOffset.second != tileSize / 2) {
                                        int exitCircleRadius2 = getExitCircleRadius();
                                        g.fillOval(placeXpx + exitOffset.first - exitCircleRadius2, placeYpx + exitOffset.second - exitCircleRadius2, 2 * exitCircleRadius2, 2 * exitCircleRadius2);
                                    } else {
                                        // non-standard exit
                                        exitnstd = true;
                                    }
                                    break;
                            }
                        }
                    }
                }
                // draw exits
                if (tileSize >= 20 && (exitUp || exitDown) && drawText && lineNum <= maxLines) {
                    // have some arrows: ⬆⬇ ↑↓
                    exits = "" + (exitnstd ? "+" : "") + (exitUp ? "↑" : "") + (exitDown ? "↓" : "");
                }
                g.setColor(Color.BLACK);
                final int border = (int) (tileBorderWidthScaled + getRiskLevelStrokeWidth());
                drawText(g, placeXpx + border, placeYpx + border, tileSize - 2 * border, tileSize - 2 * border, text, flags, exits);
            }
            // TODO: extract from parent loop
            if (copiedPlaceLocations != null) {
                boolean locationFound = false;
                for (Pair<Integer, Integer> location : copiedPlaceLocations) {
                    if (location.first == placeX - placeSelectedX && location.second == placeY - placeSelectedY) {
                        locationFound = true;
                        break;
                    }
                }
                if (locationFound) {
                    // alternative: getScreenPosX();
                    int placeXpx = (int) ((tileX + remint(screenCenterX) - remint(curPos.getX())) * tileSize);
                    int placeYpx = (int) ((tileY + remint(screenCenterY) + remint(curPos.getY())) * tileSize);
                    drawCursor(g, Color.BLUE, placeXpx, placeYpx, selectionStrokeWidth);
                }
            }
            // draw cursor / place selection
            if (placeSelectionEnabled && placeX == placeSelectedX && placeY == placeSelectedY) {
                // alternative: getScreenPosX();
                int placeXpx = (int) ((tileX + remint(screenCenterX) - remint(curPos.getX())) * tileSize);
                int placeYpx = (int) ((tileY + remint(screenCenterY) + remint(curPos.getY())) * tileSize);
                drawCursor(g, TILE_SELECTION_COLOR, placeXpx, placeYpx, selectionStrokeWidth);
            }
        }
    }
    // mask out tile positions on graphicPath
    ((Graphics2D) graphicPath).setBackground(new Color(0, 0, 0, 0));
    int clearTileSize = tileSize - 2 * tileBorderWidthScaled;
    for (Pair<Integer, Integer> p : tilePositions) // graphicPath.clearRect(p.first, p.second, p.first + tileSize, p.second + tileSize);
    graphicPath.clearRect(p.first + tileBorderWidthScaled, p.second + tileBorderWidthScaled, clearTileSize, clearTileSize);
    // draw graphicPath to g
    if (getShowPaths())
        g.drawImage(imagePath, 0, 0, null);
    graphicPath.dispose();
}
Also used : BasicStroke(java.awt.BasicStroke) ArrayList(java.util.ArrayList) GradientPaint(java.awt.GradientPaint) BufferedImage(java.awt.image.BufferedImage) FontMetrics(java.awt.FontMetrics) CubicCurve2D(java.awt.geom.CubicCurve2D) Pair(mudmap2.utils.Pair) Path(mudmap2.backend.Path) Color(java.awt.Color) GradientPaint(java.awt.GradientPaint) LinkedList(java.util.LinkedList) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) HashMap(java.util.HashMap) Map(java.util.Map) Place(mudmap2.backend.Place)

Example 19 with World

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

the class WorldFileList method writeWorldList.

/**
 * Saves the available worlds list
 * do this after writing the world files or new places won't appear in list
 */
public static void writeWorldList() {
    final String file = Environment.getWorldsDir() + "worlds";
    try {
        // open file
        if (!Environment.isDirectory(Environment.getWorldsDir()))
            Environment.createDirectory(Environment.getWorldsDir());
        try (PrintWriter outstream = new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
            outstream.println("# MUD Map (v2) worlds file");
            outstream.println("ver " + META_FILE_VER_MAJOR + "." + META_FILE_VER_MINOR);
            for (Map.Entry<String, String> w : availableWorlds.entrySet()) {
                // check whether the file name in file equals the name in the list
                WorldFileDefault worldFile = new WorldFileDefault(w.getKey());
                String fw = null;
                try {
                    if (worldFile.canRead()) {
                        fw = worldFile.readWorldName();
                    }
                } catch (Exception ex) {
                    Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (fw != null) {
                    outstream.println("n " + fw);
                    String w_file = w.getKey();
                    outstream.println("f " + w_file);
                    if (w_file.startsWith(Environment.getWorldsDir())) {
                        w_file = w_file.substring(Environment.getWorldsDir().length());
                        outstream.println("g " + w_file);
                    }
                }
            }
        }
    } catch (IOException ex) {
        System.out.printf("Couldn't write worlds file " + file);
        Logger.getLogger(WorldManager.class.getName()).log(Level.INFO, null, ex);
        JOptionPane.showMessageDialog(null, "Could not write worlds list file " + file + ".\nYou might have to open your worlds manually from " + Environment.getWorldsDir(), "WorldManager", JOptionPane.WARNING_MESSAGE);
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 20 with World

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

the class WorldFileList method readWorldList.

/**
 * Get available worlds from worlds file
 */
public static void readWorldList() {
    try {
        // read from available worlds file
        BufferedReader reader = new BufferedReader(new FileReader(Environment.getAvailableWorldsFile()));
        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.startsWith("f ")) {
                // world file entry
                String file = line.substring(2).trim();
                WorldFileDefault worldFile = new WorldFileDefault(file);
                if (worldFile.canRead()) {
                    // is world file
                    String name;
                    try {
                        // get world name
                        name = worldFile.readWorldName();
                    } catch (Exception ex) {
                        Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
                        // use file name if world name not found
                        name = file.substring(file.lastIndexOf('/') + 1);
                    }
                    // add found world to list
                    availableWorlds.put(file, name);
                }
            }
        }
    } catch (FileNotFoundException ex) {
    } catch (IOException ex) {
        Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

World (mudmap2.backend.World)31 Test (org.junit.Test)27 IOException (java.io.IOException)11 FileNotFoundException (java.io.FileNotFoundException)9 Place (mudmap2.backend.Place)9 File (java.io.File)7 WorldFileDefault (mudmap2.backend.WorldFileReader.current.WorldFileDefault)7 Layer (mudmap2.backend.Layer)6 Path (mudmap2.backend.Path)6 WorldCoordinate (mudmap2.backend.WorldCoordinate)6 Color (java.awt.Color)5 HashMap (java.util.HashMap)5 WorldFile (mudmap2.backend.WorldFileReader.WorldFile)5 PlaceGroup (mudmap2.backend.PlaceGroup)4 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 Map (java.util.Map)3 JLabel (javax.swing.JLabel)3 RiskLevel (mudmap2.backend.RiskLevel)3 BorderLayout (java.awt.BorderLayout)2