Search in sources :

Example 6 with Polygon

use of com.badlogic.gdx.math.Polygon in project bladecoder-adventure-engine by bladecoder.

the class ScnWidgetInputListener method touchUp.

@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
    super.touchUp(event, x, y, pointer, button);
    if (draggingMode == DraggingModes.DRAGGING_ACTOR) {
        Ctx.project.getUndoStack().add(new UndoPosition(selActor, new Vector2(undoOrg)));
    } else if (draggingMode == DraggingModes.DRAGGING_REFPOINT) {
        Ctx.project.getUndoStack().add(new UndoRefPosition((InteractiveActor) selActor, new Vector2(undoOrg)));
    } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE_POINT) {
        Polygon poly = scnWidget.getScene().getPolygonalNavGraph().getWalkZone();
        Ctx.project.getUndoStack().add(new UndoWalkzonePointPos(poly, vertIndex, new Vector2(undoOrg)));
    } else if (draggingMode == DraggingModes.DRAGGING_BBOX_POINT) {
        Ctx.project.getUndoStack().add(new UndoBboxPointPos(selActor, vertIndex, new Vector2(undoOrg)));
    } else if (draggingMode == DraggingModes.DRAGGING_MARKER_0 || draggingMode == DraggingModes.DRAGGING_MARKER_100) {
        Ctx.project.getUndoStack().add(new UndoDepthVector(scnWidget.getScene(), new Vector2(undoOrg)));
    } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE) {
        Ctx.project.getUndoStack().add(new UndoWalkZonePosition(scnWidget.getScene().getPolygonalNavGraph().getWalkZone(), new Vector2(undoOrg)));
    }
    draggingMode = DraggingModes.NONE;
    return;
}
Also used : UndoRefPosition(com.bladecoder.engineeditor.undo.UndoRefPosition) UndoBboxPointPos(com.bladecoder.engineeditor.undo.UndoBboxPointPos) Vector2(com.badlogic.gdx.math.Vector2) UndoPosition(com.bladecoder.engineeditor.undo.UndoPosition) UndoDepthVector(com.bladecoder.engineeditor.undo.UndoDepthVector) UndoWalkZonePosition(com.bladecoder.engineeditor.undo.UndoWalkZonePosition) Polygon(com.badlogic.gdx.math.Polygon) UndoWalkzonePointPos(com.bladecoder.engineeditor.undo.UndoWalkzonePointPos)

Example 7 with Polygon

use of com.badlogic.gdx.math.Polygon in project libgdx by libgdx.

the class BaseTmxMapLoader method loadObject.

protected void loadObject(TiledMap map, MapLayer layer, Element element) {
    if (element.getName().equals("object")) {
        MapObject object = null;
        float scaleX = convertObjectToTileSpace ? 1.0f / mapTileWidth : 1.0f;
        float scaleY = convertObjectToTileSpace ? 1.0f / mapTileHeight : 1.0f;
        float x = element.getFloatAttribute("x", 0) * scaleX;
        float y = (flipY ? (mapHeightInPixels - element.getFloatAttribute("y", 0)) : element.getFloatAttribute("y", 0)) * scaleY;
        float width = element.getFloatAttribute("width", 0) * scaleX;
        float height = element.getFloatAttribute("height", 0) * scaleY;
        if (element.getChildCount() > 0) {
            Element child = null;
            if ((child = element.getChildByName("polygon")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polygon polygon = new Polygon(vertices);
                polygon.setPosition(x, y);
                object = new PolygonMapObject(polygon);
            } else if ((child = element.getChildByName("polyline")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Float.parseFloat(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Float.parseFloat(point[1]) * scaleY * (flipY ? -1 : 1);
                }
                Polyline polyline = new Polyline(vertices);
                polyline.setPosition(x, y);
                object = new PolylineMapObject(polyline);
            } else if ((child = element.getChildByName("ellipse")) != null) {
                object = new EllipseMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        if (object == null) {
            String gid = null;
            if ((gid = element.getAttribute("gid", null)) != null) {
                int id = (int) Long.parseLong(gid);
                boolean flipHorizontally = ((id & FLAG_FLIP_HORIZONTALLY) != 0);
                boolean flipVertically = ((id & FLAG_FLIP_VERTICALLY) != 0);
                TiledMapTile tile = map.getTileSets().getTile(id & ~MASK_CLEAR);
                TiledMapTileMapObject tiledMapTileMapObject = new TiledMapTileMapObject(tile, flipHorizontally, flipVertically);
                TextureRegion textureRegion = tiledMapTileMapObject.getTextureRegion();
                tiledMapTileMapObject.getProperties().put("gid", id);
                tiledMapTileMapObject.setX(x);
                tiledMapTileMapObject.setY(flipY ? y : y - height);
                float objectWidth = element.getFloatAttribute("width", textureRegion.getRegionWidth());
                float objectHeight = element.getFloatAttribute("height", textureRegion.getRegionHeight());
                tiledMapTileMapObject.setScaleX(scaleX * (objectWidth / textureRegion.getRegionWidth()));
                tiledMapTileMapObject.setScaleY(scaleY * (objectHeight / textureRegion.getRegionHeight()));
                tiledMapTileMapObject.setRotation(element.getFloatAttribute("rotation", 0));
                object = tiledMapTileMapObject;
            } else {
                object = new RectangleMapObject(x, flipY ? y - height : y, width, height);
            }
        }
        object.setName(element.getAttribute("name", null));
        String rotation = element.getAttribute("rotation", null);
        if (rotation != null) {
            object.getProperties().put("rotation", Float.parseFloat(rotation));
        }
        String type = element.getAttribute("type", null);
        if (type != null) {
            object.getProperties().put("type", type);
        }
        int id = element.getIntAttribute("id", 0);
        if (id != 0) {
            object.getProperties().put("id", id);
        }
        object.getProperties().put("x", x);
        if (object instanceof TiledMapTileMapObject) {
            object.getProperties().put("y", y);
        } else {
            object.getProperties().put("y", (flipY ? y - height : y));
        }
        object.getProperties().put("width", width);
        object.getProperties().put("height", height);
        object.setVisible(element.getIntAttribute("visible", 1) == 1);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(object.getProperties(), properties);
        }
        layer.getObjects().add(object);
    }
}
Also used : Element(com.badlogic.gdx.utils.XmlReader.Element) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject) Polyline(com.badlogic.gdx.math.Polyline) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) Polygon(com.badlogic.gdx.math.Polygon) MapObject(com.badlogic.gdx.maps.MapObject) EllipseMapObject(com.badlogic.gdx.maps.objects.EllipseMapObject) PolygonMapObject(com.badlogic.gdx.maps.objects.PolygonMapObject) PolylineMapObject(com.badlogic.gdx.maps.objects.PolylineMapObject) TiledMapTileMapObject(com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject) RectangleMapObject(com.badlogic.gdx.maps.objects.RectangleMapObject)

Example 8 with Polygon

use of com.badlogic.gdx.math.Polygon in project bladecoder-adventure-engine by bladecoder.

the class CanvasDrawer method drawBBoxActors.

public void drawBBoxActors(Scene scn) {
    drawer.setProjectionMatrix(camera.combined);
    drawer.setTransformMatrix(new Matrix4());
    drawer.begin(ShapeType.Line);
    for (BaseActor a : scn.getActors().values()) {
        Polygon p = a.getBBox();
        if (p == null) {
            EditorLogger.error("ERROR DRAWING BBOX FOR: " + a.getId());
        }
        if (a instanceof ObstacleActor) {
            drawer.setColor(Scene.OBSTACLE_COLOR);
            drawer.polygon(p.getTransformedVertices());
        } else if (a instanceof InteractiveActor) {
            InteractiveActor iActor = (InteractiveActor) a;
            if (!scn.getLayer(iActor.getLayer()).isVisible())
                continue;
            drawer.setColor(Scene.ACTOR_BBOX_COLOR);
            if (p.getTransformedVertices().length > 2)
                drawer.polygon(p.getTransformedVertices());
        } else if (a instanceof AnchorActor) {
            drawer.setColor(Scene.ANCHOR_COLOR);
            drawer.line(p.getX() - Scene.ANCHOR_RADIUS, p.getY(), p.getX() + Scene.ANCHOR_RADIUS, p.getY());
            drawer.line(p.getX(), p.getY() - Scene.ANCHOR_RADIUS, p.getX(), p.getY() + Scene.ANCHOR_RADIUS);
        }
    // drawer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    }
    drawer.end();
}
Also used : AnchorActor(com.bladecoder.engine.model.AnchorActor) ObstacleActor(com.bladecoder.engine.model.ObstacleActor) InteractiveActor(com.bladecoder.engine.model.InteractiveActor) BaseActor(com.bladecoder.engine.model.BaseActor) Polygon(com.badlogic.gdx.math.Polygon) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 9 with Polygon

use of com.badlogic.gdx.math.Polygon in project bladecoder-adventure-engine by bladecoder.

the class CanvasDrawer method drawSelectedActor.

public void drawSelectedActor(BaseActor selectedActor) {
    // Gdx.gl20.glLineWidth(3);
    Gdx.gl20.glEnable(GL20.GL_BLEND);
    // Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    drawer.setProjectionMatrix(camera.combined);
    if (selectedActor instanceof AnchorActor) {
        drawer.begin(ShapeRenderer.ShapeType.Line);
        drawer.setColor(MOUSESELECTION_STROKE_COLOR);
        drawer.rect(selectedActor.getX() - Scene.ANCHOR_RADIUS, selectedActor.getY() - Scene.ANCHOR_RADIUS, Scene.ANCHOR_RADIUS * 2, Scene.ANCHOR_RADIUS * 2);
        drawer.end();
    } else {
        Polygon p = selectedActor.getBBox();
        Rectangle rect = p.getBoundingRectangle();
        drawer.begin(ShapeRenderer.ShapeType.Filled);
        drawer.setColor(MOUSESELECTION_FILL_COLOR);
        drawer.rect(rect.x, rect.y, rect.width, rect.height);
        drawer.end();
        drawer.begin(ShapeRenderer.ShapeType.Line);
        drawer.setColor(MOUSESELECTION_STROKE_COLOR);
        drawer.rect(rect.x, rect.y, rect.width, rect.height);
        // DRAW SELECTION BOUNDS
        if ((!(selectedActor instanceof SpriteActor) || !((SpriteActor) selectedActor).isBboxFromRenderer()) && !(selectedActor instanceof AnchorActor)) {
            float[] verts = selectedActor.getBBox().getTransformedVertices();
            for (int i = 0; i < verts.length; i += 2) drawer.rect(verts[i] - CORNER_DIST / 2, verts[i + 1] - CORNER_DIST / 2, CORNER_DIST, CORNER_DIST);
        }
        // DRAW REFPOINT
        if (selectedActor instanceof InteractiveActor) {
            Vector2 refPoint = ((InteractiveActor) selectedActor).getRefPoint();
            float orgX = selectedActor.getX() + refPoint.x;
            float orgY = selectedActor.getY() + refPoint.y;
            drawer.line(orgX - Scene.ANCHOR_RADIUS, orgY, orgX + Scene.ANCHOR_RADIUS, orgY);
            drawer.line(orgX, orgY - Scene.ANCHOR_RADIUS, orgX, orgY + Scene.ANCHOR_RADIUS);
        }
        drawer.end();
    }
}
Also used : AnchorActor(com.bladecoder.engine.model.AnchorActor) Vector2(com.badlogic.gdx.math.Vector2) InteractiveActor(com.bladecoder.engine.model.InteractiveActor) Rectangle(com.badlogic.gdx.math.Rectangle) SpriteActor(com.bladecoder.engine.model.SpriteActor) Polygon(com.badlogic.gdx.math.Polygon)

Example 10 with Polygon

use of com.badlogic.gdx.math.Polygon in project bladecoder-adventure-engine by bladecoder.

the class ScnWidgetInputListener method touchDown.

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
    super.touchDown(event, x, y, pointer, button);
    // EditorLogger.debug("Touch Down - X: " + x + " Y: " + y);
    Scene scn = scnWidget.getScene();
    if (scn == null)
        return false;
    Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
    scnWidget.screenToWorldCoords(p);
    org.set(p);
    if (button == Buttons.LEFT) {
        selActor = scnWidget.getSelectedActor();
        if (scn.getPolygonalNavGraph() != null && scnWidget.getShowWalkZone()) {
            // Check
            // WALKZONE
            // CHECK WALKZONE VERTEXS
            Polygon wzPoly = scn.getPolygonalNavGraph().getWalkZone();
            float[] verts = wzPoly.getTransformedVertices();
            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_WALKZONE_POINT;
                    vertIndex = i;
                    float[] v = wzPoly.getVertices();
                    undoOrg.set(v[i], v[i + 1]);
                    return true;
                }
            }
            // CHECK FOR WALKZONE DRAGGING
            if (wzPoly.contains(p.x, p.y)) {
                draggingMode = DraggingModes.DRAGGING_WALKZONE;
                undoOrg.set(wzPoly.getX(), wzPoly.getY());
                return true;
            }
        }
        // SELACTOR ORIGIN DRAGGING
        if (selActor != null && selActor instanceof InteractiveActor) {
            Vector2 refPoint = ((InteractiveActor) selActor).getRefPoint();
            float orgX = selActor.getX() + refPoint.x;
            float orgY = selActor.getY() + refPoint.y;
            float dst = Vector2.dst(p.x, p.y, orgX, orgY);
            if (dst < Scene.ANCHOR_RADIUS) {
                draggingMode = DraggingModes.DRAGGING_REFPOINT;
                undoOrg.set(refPoint.x, refPoint.y);
                return true;
            }
        }
        // SELACTOR VERTEXs DRAGGING
        if (selActor != null && (!(selActor instanceof SpriteActor) || !((SpriteActor) selActor).isBboxFromRenderer()) && !(scnWidget.getSelectedActor() instanceof AnchorActor)) {
            Polygon bbox = selActor.getBBox();
            float[] verts = bbox.getTransformedVertices();
            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_BBOX_POINT;
                    vertIndex = i;
                    float[] v = bbox.getVertices();
                    undoOrg.set(v[i], v[i + 1]);
                    return true;
                }
            }
        }
        // CHECK FOR ACTORS
        BaseActor a = scn.getActorAt(p.x, p.y);
        if (a != null && a != selActor) {
            selActor = a;
            BaseActor da = Ctx.project.getActor(selActor.getId());
            Ctx.project.setSelectedActor(da);
            return true;
        }
        if (a != null) {
            draggingMode = DraggingModes.DRAGGING_ACTOR;
            undoOrg.set(selActor.getX(), selActor.getY());
            return true;
        }
        // CHECK FOR DRAGGING DEPTH MARKERS
        Vector2 depthVector = scnWidget.getScene().getDepthVector();
        if (depthVector != null) {
            p.set(0, depthVector.x);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_0;
                Vector2 dv = scnWidget.getScene().getDepthVector();
                undoOrg.set(dv.x, dv.y);
                return true;
            }
            p.set(0, depthVector.y);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_100;
                Vector2 dv = scnWidget.getScene().getDepthVector();
                undoOrg.set(dv.x, dv.y);
                return true;
            }
        }
    }
    return true;
}
Also used : AnchorActor(com.bladecoder.engine.model.AnchorActor) Vector2(com.badlogic.gdx.math.Vector2) InteractiveActor(com.bladecoder.engine.model.InteractiveActor) SpriteActor(com.bladecoder.engine.model.SpriteActor) BaseActor(com.bladecoder.engine.model.BaseActor) Scene(com.bladecoder.engine.model.Scene) Polygon(com.badlogic.gdx.math.Polygon)

Aggregations

Polygon (com.badlogic.gdx.math.Polygon)17 Vector2 (com.badlogic.gdx.math.Vector2)7 BaseActor (com.bladecoder.engine.model.BaseActor)7 InteractiveActor (com.bladecoder.engine.model.InteractiveActor)6 AnchorActor (com.bladecoder.engine.model.AnchorActor)5 Rectangle (com.badlogic.gdx.math.Rectangle)4 SpriteActor (com.bladecoder.engine.model.SpriteActor)4 ObstacleActor (com.bladecoder.engine.model.ObstacleActor)3 Scene (com.bladecoder.engine.model.Scene)3 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 MapObject (com.badlogic.gdx.maps.MapObject)2 EllipseMapObject (com.badlogic.gdx.maps.objects.EllipseMapObject)2 PolygonMapObject (com.badlogic.gdx.maps.objects.PolygonMapObject)2 RectangleMapObject (com.badlogic.gdx.maps.objects.RectangleMapObject)2 TiledMapTileMapObject (com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject)2 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)2 World (com.bladecoder.engine.model.World)2 MapLayer (com.badlogic.gdx.maps.MapLayer)1 PolylineMapObject (com.badlogic.gdx.maps.objects.PolylineMapObject)1