-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathDimensionsComponent.java
More file actions
71 lines (64 loc) · 2.7 KB
/
DimensionsComponent.java
File metadata and controls
71 lines (64 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.uwsoft.editor.renderer.components;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.graphics.g2d.PolygonRegion;
import com.badlogic.gdx.graphics.g2d.PolygonSprite;
import com.badlogic.gdx.math.EarClippingTriangulator;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Pool;
import com.uwsoft.editor.renderer.data.ShapeVO;
import com.uwsoft.editor.renderer.utils.PolygonUtils;
public class DimensionsComponent implements Component,Pool.Poolable {
public float width = 0;
public float height = 0;
public Rectangle boundBox;
public Polygon polygon;
public boolean hit(float x, float y){
if(polygon != null) {
return polygon.contains(x, y);
} else if(boundBox != null) {
return (x >= boundBox.x && x < boundBox.x+boundBox.width && y >= boundBox.y && y < boundBox.y+boundBox.height);
} else {
return (x >= 0 && x < width && y >= 0 && y < height);
}
}
public void setPolygon(PolygonComponent polygonComponent) {
Vector2[] verticesArray = PolygonUtils.mergeTouchingPolygonsToOne(polygonComponent.vertices);
float[] vertices = new float[verticesArray.length*2];
for(int i = 0; i < verticesArray.length; i++) {
vertices[i*2] = (verticesArray[i].x);
vertices[i*2+1] = (verticesArray[i].y);
}
polygon = new Polygon(vertices);
}
public void setFromShape(ShapeVO shape) {
Vector2 minPoint = new Vector2();
Vector2 maxPoint = new Vector2();
if(shape.polygons != null) {
for(int i = 0; i < shape.polygons.length; i++) {
for(int j = 0; j < shape.polygons[i].length; j++) {
if(i == 0 && j == 0) {
minPoint.x = shape.polygons[i][j].x;
minPoint.y = shape.polygons[i][j].y;
maxPoint.x = shape.polygons[i][j].x;
maxPoint.y = shape.polygons[i][j].y;
}
if(minPoint.x > shape.polygons[i][j].x) minPoint.x = shape.polygons[i][j].x;
if(minPoint.y > shape.polygons[i][j].y) minPoint.y = shape.polygons[i][j].y;
if(maxPoint.x < shape.polygons[i][j].x) maxPoint.x = shape.polygons[i][j].x;
if(maxPoint.y < shape.polygons[i][j].y) maxPoint.y = shape.polygons[i][j].y;
}
}
width = maxPoint.x - minPoint.x;
height = maxPoint.y - minPoint.y;
}
}
@Override
public void reset() {
width=0;
height=0;
boundBox=null;
polygon=null;
}
}