Можно ли изменить фигуру кнопки с прямоугольника на многоугольник , пишу(пытаюсь по крайней мере) игру с тактической битвой и мне очень удобно наследовать класс Button из за обработчика событий , но хождение по квадратикам выглядит уж очень не красиво и хочется сделать поле боя на подобия героев меча и магии
1 ответ
немного изменение в вопросе куда вот это
void createHexagon(){
pos = new Vector2(nullx,nully);
bottom = new Vector2(pos.x + getWidth() / 2, pos.y); //точка нижнего угла
bottomright = new Vector2(pos.x + getWidth(), pos.y + getHeight() * .25f);//точка нижне-правого угла
topright = new Vector2(pos.x + getWidth(), pos.y + getHeight() * .75f);//точка верхне-правого угла
top = new Vector2(pos.x + getWidth() / 2, pos.y + getHeight());//точка верхнего угла
topleft = new Vector2(pos.x, pos.y + getHeight() * .75f);//точка верхне-левого угла
bottomleft = new Vector2(pos.x, pos.y + getHeight() * .25f);//точка нижне-левого угла
centr = new Vector2(pos.x + getWidth(),pos.y + getHeight());// точка сентра :)
System.out.println("CREATE HEXGAON");
}
boolean enterHexagon(float x,float y) {
boolean enter = false;
boolean a,b,c,d,e,f;
a = IsInTriangle(centr,bottom ,bottomright , x, x);
b = IsInTriangle(centr, bottomright, topright, x, y);
c = IsInTriangle(centr, topright, top, x, y);
d = IsInTriangle(centr, top, topleft, x, y);
e = IsInTriangle(centr, topleft, bottomleft, x, y);
f= IsInTriangle(centr, bottomleft, bottom, x, y);
if(a && b && c && d && e && f) {
enter = true;
}else {
enter = false;
}
System.out.println("ENTER HEXGAON");
return enter;
}
запихать вот в это
addListener(new InputListener() {
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
super.enter(event, x, y, pointer, fromActor);
}
}
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
super.exit(event, x, y, pointer, toActor);
}
});
или мне нужно переопределить это?
public Actor hit (float x, float y, boolean touchable) {
if (touchable && this.touchable != Touchable.enabled) return null;
return x >= 0 && x < width && y >= 0 && y < height ? this : null;
}
или что то из этого ?
public Vector2 screenToLocalCoordinates (Vector2 screenCoords) {
Stage stage = this.stage;
if (stage == null) return screenCoords;
return stageToLocalCoordinates(stage.screenToStageCoordinates(screenCoords));
}
/** Transforms the specified point in the stage's coordinates to the actor's local coordinate system. */
public Vector2 stageToLocalCoordinates (Vector2 stageCoords) {
if (parent != null) parent.stageToLocalCoordinates(stageCoords);
parentToLocalCoordinates(stageCoords);
return stageCoords;
}
/** Transforms the specified point in the actor's coordinates to be in the stage's coordinates.
* @see Stage#toScreenCoordinates(Vector2, com.badlogic.gdx.math.Matrix4) */
public Vector2 localToStageCoordinates (Vector2 localCoords) {
return localToAscendantCoordinates(null, localCoords);
}
/** Transforms the specified point in the actor's coordinates to be in the parent's coordinates. */
public Vector2 localToParentCoordinates (Vector2 localCoords) {
final float rotation = -this.rotation;
final float scaleX = this.scaleX;
final float scaleY = this.scaleY;
final float x = this.x;
final float y = this.y;
if (rotation == 0) {
if (scaleX == 1 && scaleY == 1) {
localCoords.x += x;
localCoords.y += y;
} else {
final float originX = this.originX;
final float originY = this.originY;
localCoords.x = (localCoords.x - originX) * scaleX + originX + x;
localCoords.y = (localCoords.y - originY) * scaleY + originY + y;
}
} else {
final float cos = (float)Math.cos(rotation * MathUtils.degreesToRadians);
final float sin = (float)Math.sin(rotation * MathUtils.degreesToRadians);
final float originX = this.originX;
final float originY = this.originY;
final float tox = (localCoords.x - originX) * scaleX;
final float toy = (localCoords.y - originY) * scaleY;
localCoords.x = (tox * cos + toy * sin) + originX + x;
localCoords.y = (tox * -sin + toy * cos) + originY + y;
}
return localCoords;
}
/** Converts coordinates for this actor to those of a parent actor. The ascendant does not need to be a direct parent. */
public Vector2 localToAscendantCoordinates (Actor ascendant, Vector2 localCoords) {
Actor actor = this;
while (actor != null) {
actor.localToParentCoordinates(localCoords);
actor = actor.parent;
if (actor == ascendant) break;
}
return localCoords;
}
/** Converts the coordinates given in the parent's coordinate system to this actor's coordinate system. */
public Vector2 parentToLocalCoordinates (Vector2 parentCoords) {
final float rotation = this.rotation;
final float scaleX = this.scaleX;
final float scaleY = this.scaleY;
final float childX = x;
final float childY = y;
if (rotation == 0) {
if (scaleX == 1 && scaleY == 1) {
parentCoords.x -= childX;
parentCoords.y -= childY;
} else {
final float originX = this.originX;
final float originY = this.originY;
parentCoords.x = (parentCoords.x - childX - originX) / scaleX + originX;
parentCoords.y = (parentCoords.y - childY - originY) / scaleY + originY;
}
} else {
final float cos = (float)Math.cos(rotation * MathUtils.degreesToRadians);
final float sin = (float)Math.sin(rotation * MathUtils.degreesToRadians);
final float originX = this.originX;
final float originY = this.originY;
final float tox = parentCoords.x - childX - originX;
final float toy = parentCoords.y - childY - originY;
parentCoords.x = (tox * cos + toy * sin) / scaleX + originX;
parentCoords.y = (tox * -sin + toy * cos) / scaleY + originY;
}
return parentCoords;
}
прошу прощение за странное и не совсем конструктивное изложение вопроса