Search in sources :

Example 11 with Polygon

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

the class ScnWidgetInputListener method keyDown.

@Override
public boolean keyDown(InputEvent event, int keycode) {
    super.keyDown(event, keycode);
    Polygon p = null;
    if (scnWidget.getStage() == null || scnWidget.getStage().getKeyboardFocus() != scnWidget)
        return false;
    switch(keycode) {
        case Keys.ENTER:
            break;
        case Keys.BACKSPACE:
            break;
        case Keys.Z:
            if (UIUtils.ctrl()) {
                Ctx.project.getUndoStack().undo();
            }
            break;
        case Keys.FORWARD_DEL:
            BaseActor a = Ctx.project.getSelectedActor();
            if (a == null)
                return false;
            Ctx.project.getUndoStack().add(new UndoDeleteActor(Ctx.project.getSelectedScene(), a));
            Ctx.project.getSelectedScene().removeActor(a);
            Ctx.project.setModified(this, Project.NOTIFY_ELEMENT_DELETED, null, a);
            break;
        case Keys.S:
            if (UIUtils.ctrl()) {
                try {
                    Ctx.project.saveProject();
                } catch (IOException e1) {
                    String msg = "Something went wrong while saving the actor.\n\n" + e1.getClass().getSimpleName() + " - " + e1.getMessage();
                    Message.showMsgDialog(scnWidget.getStage(), "Error", msg);
                    EditorLogger.printStackTrace(e1);
                }
            }
            break;
        case Keys.UP:
        case Keys.DOWN:
        case Keys.LEFT:
        case Keys.RIGHT:
            selActor = scnWidget.getSelectedActor();
            p = selActor.getBBox();
            undoOrg.set(p.getX(), p.getY());
            Ctx.project.setModified(this, Project.POSITION_PROPERTY, null, selActor);
            break;
        case Keys.PLUS:
        case 70:
            scnWidget.zoom(-1);
            break;
        case Keys.MINUS:
            scnWidget.zoom(1);
            break;
    }
    return false;
}
Also used : BaseActor(com.bladecoder.engine.model.BaseActor) IOException(java.io.IOException) Polygon(com.badlogic.gdx.math.Polygon) UndoDeleteActor(com.bladecoder.engineeditor.undo.UndoDeleteActor)

Example 12 with Polygon

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

the class ScnWidgetInputListener method clicked.

@Override
public void clicked(InputEvent event, float x, float y) {
    Scene scn = scnWidget.getScene();
    if (scn == null)
        return;
    Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
    scnWidget.screenToWorldCoords(p);
    // DOUBLE CLICK TO CREATE OR DELETE POINTS
    if (getTapCount() == 2) {
        // Check WALKZONE
        if (scn.getPolygonalNavGraph() != null && scnWidget.getShowWalkZone()) {
            Polygon poly = scn.getPolygonalNavGraph().getWalkZone();
            if (UIUtils.ctrl()) {
                // Delete the point if selected
                boolean deleted = PolygonUtils.deletePoint(poly, p.x, p.y, CanvasDrawer.CORNER_DIST);
                if (deleted) {
                    Ctx.project.setModified();
                    return;
                }
            } else {
                boolean created = PolygonUtils.addClampPointIfTolerance(poly, p.x, p.y, CanvasDrawer.CORNER_DIST);
                if (created) {
                    Ctx.project.setModified();
                    return;
                }
            }
        }
        if (scnWidget.getSelectedActor() != null) {
            Polygon poly = scnWidget.getSelectedActor().getBBox();
            if ((!(scnWidget.getSelectedActor() instanceof SpriteActor) || !((SpriteActor) scnWidget.getSelectedActor()).isBboxFromRenderer()) && !(scnWidget.getSelectedActor() instanceof AnchorActor)) {
                if (UIUtils.ctrl()) {
                    // Delete the point if selected
                    boolean deleted = PolygonUtils.deletePoint(poly, p.x, p.y, CanvasDrawer.CORNER_DIST);
                    if (deleted) {
                        Ctx.project.setModified();
                        return;
                    }
                } else {
                    boolean created = PolygonUtils.addClampPointIfTolerance(poly, p.x, p.y, CanvasDrawer.CORNER_DIST);
                    if (created) {
                        Ctx.project.setModified();
                        return;
                    }
                }
            }
        }
    }
}
Also used : AnchorActor(com.bladecoder.engine.model.AnchorActor) Vector2(com.badlogic.gdx.math.Vector2) SpriteActor(com.bladecoder.engine.model.SpriteActor) Scene(com.bladecoder.engine.model.Scene) Polygon(com.badlogic.gdx.math.Polygon)

Example 13 with Polygon

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

the class ScnWidgetInputListener method touchDragged.

@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
    // EditorLogger.debug("Touch Dragged - X: " + Gdx.input.getX() + " Y: "
    // + Gdx.input.getY());
    super.touchDragged(event, x, y, pointer);
    if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
        Scene scn = scnWidget.getScene();
        if (scn == null)
            return;
        Vector2 d = new Vector2(Gdx.input.getX(), Gdx.input.getY());
        scnWidget.screenToWorldCoords(d);
        d.sub(org);
        org.add(d);
        if (draggingMode == DraggingModes.DRAGGING_ACTOR) {
            selActor.setPosition(selActor.getX() + d.x, selActor.getY() + d.y);
            Ctx.project.setModified(this, Project.POSITION_PROPERTY, null, selActor);
        } else if (draggingMode == DraggingModes.DRAGGING_REFPOINT) {
            Vector2 refPoint = ((InteractiveActor) selActor).getRefPoint();
            refPoint.add(d.x, d.y);
            Ctx.project.setModified();
        } else if (draggingMode == DraggingModes.DRAGGING_BBOX_POINT) {
            Polygon poly = selActor.getBBox();
            float[] verts = poly.getVertices();
            verts[vertIndex] += d.x;
            verts[vertIndex + 1] += d.y;
            poly.dirty();
            Ctx.project.setModified();
        } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE_POINT) {
            Polygon poly = scn.getPolygonalNavGraph().getWalkZone();
            float[] verts = poly.getVertices();
            verts[vertIndex] += d.x;
            verts[vertIndex + 1] += d.y;
            poly.dirty();
            Ctx.project.setModified();
        } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE) {
            Polygon poly = scn.getPolygonalNavGraph().getWalkZone();
            poly.translate(d.x, d.y);
            Ctx.project.setModified();
        } else if (draggingMode == DraggingModes.DRAGGING_MARKER_0) {
            Vector2 depthVector = scnWidget.getScene().getDepthVector();
            depthVector.x += d.y;
            Ctx.project.setModified();
            updateFakeDepth();
        } else if (draggingMode == DraggingModes.DRAGGING_MARKER_100) {
            Vector2 depthVector = scnWidget.getScene().getDepthVector();
            depthVector.y += d.y;
            Ctx.project.setModified();
            updateFakeDepth();
        }
    } else if (Gdx.input.isButtonPressed(Buttons.RIGHT) || Gdx.input.isButtonPressed(Buttons.MIDDLE)) {
        Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
        scnWidget.screenToWorldCoords(p);
        p.sub(org);
        scnWidget.translate(p);
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) Scene(com.bladecoder.engine.model.Scene) Polygon(com.badlogic.gdx.math.Polygon)

Example 14 with Polygon

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

the class PolygonalNavGraph method createInitialGraph.

public void createInitialGraph(Collection<BaseActor> actors) {
    graphNodes.clear();
    // 1.- Add WalkZone convex nodes
    float[] verts = walkZone.getTransformedVertices();
    for (int i = 0; i < verts.length; i += 2) {
        if (!PolygonUtils.isVertexConcave(walkZone, i)) {
            graphNodes.add(new NavNodePolygonal(verts[i], verts[i + 1]));
        }
    }
    // 2.- Add obstacles concave nodes
    obstacles.clear();
    for (BaseActor a : actors) {
        if (a instanceof ObstacleActor && a.isVisible())
            obstacles.add(a.getBBox());
    }
    for (Polygon o : obstacles) {
        verts = o.getTransformedVertices();
        for (int i = 0; i < verts.length; i += 2) {
            if (PolygonUtils.isVertexConcave(o, i) && PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], false)) {
                graphNodes.add(new NavNodePolygonal(verts[i], verts[i + 1]));
            }
        }
    }
    // 3.- CALC LINE OF SIGHTs
    for (int i = 0; i < graphNodes.size() - 1; i++) {
        NavNodePolygonal n1 = graphNodes.get(i);
        for (int j = i + 1; j < graphNodes.size(); j++) {
            NavNodePolygonal n2 = graphNodes.get(j);
            if (inLineOfSight(n1.x, n1.y, n2.x, n2.y)) {
                n1.neighbors.add(n2);
                n2.neighbors.add(n1);
            }
        }
    }
}
Also used : ObstacleActor(com.bladecoder.engine.model.ObstacleActor) BaseActor(com.bladecoder.engine.model.BaseActor) Polygon(com.badlogic.gdx.math.Polygon)

Example 15 with Polygon

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

the class Scene method drawBBoxLines.

public void drawBBoxLines(ShapeRenderer renderer) {
    // renderer.begin(ShapeType.Rectangle);
    renderer.begin(ShapeType.Line);
    for (BaseActor a : actors.values()) {
        Polygon p = a.getBBox();
        if (p == null) {
            EngineLogger.error("ERROR DRAWING BBOX FOR: " + a.getId());
        }
        if (a instanceof ObstacleActor) {
            renderer.setColor(OBSTACLE_COLOR);
            renderer.polygon(p.getTransformedVertices());
        } else if (a instanceof AnchorActor) {
            renderer.setColor(Scene.ANCHOR_COLOR);
            renderer.line(p.getX() - Scene.ANCHOR_RADIUS, p.getY(), p.getX() + Scene.ANCHOR_RADIUS, p.getY());
            renderer.line(p.getX(), p.getY() - Scene.ANCHOR_RADIUS, p.getX(), p.getY() + Scene.ANCHOR_RADIUS);
        } else {
            renderer.setColor(ACTOR_BBOX_COLOR);
            renderer.polygon(p.getTransformedVertices());
        }
    // Rectangle r = a.getBBox().getBoundingRectangle();
    // renderer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    }
    if (polygonalNavGraph != null) {
        renderer.setColor(WALKZONE_COLOR);
        renderer.polygon(polygonalNavGraph.getWalkZone().getTransformedVertices());
        // DRAW LINEs OF SIGHT
        renderer.setColor(Color.WHITE);
        ArrayList<NavNodePolygonal> nodes = polygonalNavGraph.getGraphNodes();
        for (NavNodePolygonal n : nodes) {
            for (NavNodePolygonal n2 : n.neighbors) {
                renderer.line(n.x, n.y, n2.x, n2.y);
            }
        }
    }
    renderer.end();
}
Also used : NavNodePolygonal(com.bladecoder.engine.polygonalpathfinder.NavNodePolygonal) 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