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;
}
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);
}
}
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();
}
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();
}
}
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;
}
Aggregations