Skip to content

Instantly share code, notes, and snippets.

@nazgee
Created May 7, 2013 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazgee/5532411 to your computer and use it in GitHub Desktop.
Save nazgee/5532411 to your computer and use it in GitHub Desktop.
Positioner class for andengine
package eu.nazgee.features.utils;
import org.andengine.entity.IEntity;
public class Positioner {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
static float[] TMP_ENTITY_ANCHORED = new float[2];
static float[] TMP_ENTITY_MOVABLE = new float[2];
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
static public void placeTopOfAndCenter(IEntity pAnchored, IEntity pMovable) {
placeTopOfAndCenter(pAnchored, pMovable, 0, 0);
}
static public void placeTopOfAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.VER_TOP_C.relation, tx, ty);
}
static public void placeBelowOfAndCenter(IEntity pAnchored, IEntity pMovable) {
placeBelowOfAndCenter(pAnchored, pMovable, 0, 0);
}
static public void placeBelowOfAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.VER_BTM_C.relation, tx, ty);
}
static public void placeLeftOfAndCenter(IEntity pAnchored, IEntity pMovable) {
placeLeftOfAndCenter(pAnchored, pMovable, 0, 0);
}
static public void placeLeftOfAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.HOR_L_CNT.relation, tx, ty);
}
static public void placeRightOfAndCenter(IEntity pAnchored, IEntity pMovable) {
placeRightOfAndCenter(pAnchored, pMovable, 0, 0);
}
static public void placeRightOfAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.HOR_R_CNT.relation, tx, ty);
}
static public void centerOnHorizontalAxes(IEntity pAnchored, IEntity pMovable) {
centerOnHorizontalAxes(pAnchored, pMovable, 0, 0);
}
static public void centerOnHorizontalAxes(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
placeVertical(pAnchored, pMovable, eAnchorRelation.IN_C_CNT.relation, tx, ty);
}
static public void centerOnVerticalAxes(IEntity pAnchored, IEntity pMovable) {
centerOnVerticalAxes(pAnchored, pMovable, 0, 0);
}
static public void centerOnVerticalAxes(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
placeHorizontal(pAnchored, pMovable, eAnchorRelation.IN_C_CNT.relation, tx, ty);
}
static public void center(IEntity pAnchored, IEntity pMovable) {
center(pAnchored, pMovable, 0, 0);
}
static public void center(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_C_CNT.relation, tx, ty);
}
static public void alignLeftEdges(IEntity pAnchored, IEntity pMovable) {
alignLeftEdges(pAnchored, pMovable, 0, 0);
}
static public void alignLeftEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
placeHorizontal(pAnchored, pMovable, eAnchorRelation.IN_L_CNT.relation, tx, ty);
}
static public void alignLeftEdgesAndCenter(IEntity pAnchored, IEntity pMovable) {
alignLeftEdgesAndCenter(pAnchored, pMovable, 0, 0);
}
static public void alignLeftEdgesAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_L_CNT.relation, tx, ty);
}
static public void alignRightEdges(IEntity pAnchored, IEntity pMovable) {
alignRightEdges(pAnchored, pMovable, 0, 0);
}
static public void alignRightEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
placeHorizontal(pAnchored, pMovable, eAnchorRelation.IN_R_CNT.relation, tx, ty);
}
static public void alignRightEdgesAndCenter(IEntity pAnchored, IEntity pMovable) {
alignRightEdgesAndCenter(pAnchored, pMovable, 0, 0);
}
static public void alignRightEdgesAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_R_CNT.relation, tx, ty);
}
static public void alignTopEdges(IEntity pAnchored, IEntity pMovable) {
alignTopEdges(pAnchored, pMovable, 0, 0);
}
static public void alignTopEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
placeVertical(pAnchored, pMovable, eAnchorRelation.IN_C_TOP.relation, tx, ty);
}
static public void alignTopEdgesAndCenter(IEntity pAnchored, IEntity pMovable) {
alignTopEdgesAndCenter(pAnchored, pMovable, 0, 0);
}
static public void alignTopEdgesAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_C_TOP.relation, tx, ty);
}
static public void alignTopRightEdges(IEntity pAnchored, IEntity pMovable) {
alignTopRightEdges(pAnchored, pMovable, 0, 0);
}
static public void alignTopRightEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_R_TOP.relation, tx, ty);
}
static public void alignTopLeftEdges(IEntity pAnchored, IEntity pMovable) {
alignTopLeftEdges(pAnchored, pMovable, 0, 0);
}
static public void alignTopLeftEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_L_TOP.relation, tx, ty);
}
static public void alignBottomEdges(IEntity pAnchored, IEntity pMovable) {
alignTopEdges(pAnchored, pMovable, 0, 0);
}
static public void alignBottomEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
placeVertical(pAnchored, pMovable, eAnchorRelation.IN_C_BTM.relation, tx, ty);
}
static public void alignBottomEdgesAndCenter(IEntity pAnchored, IEntity pMovable) {
alignBottomEdgesAndCenter(pAnchored, pMovable, 0, 0);
}
static public void alignBottomEdgesAndCenter(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_C_BTM.relation, tx, ty);
}
static public void alignBottomRightEdges(IEntity pAnchored, IEntity pMovable) {
alignBottomRightEdges(pAnchored, pMovable, 0, 0);
}
static public void alignBottomRightEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_R_BTM.relation, tx, ty);
}
static public void alignBottomLeftEdges(IEntity pAnchored, IEntity pMovable) {
alignBottomLeftEdges(pAnchored, pMovable, 0, 0);
}
static public void alignBottomLeftEdges(IEntity pAnchored, IEntity pMovable, final float tx, final float ty) {
place(pAnchored, pMovable, eAnchorRelation.IN_L_BTM.relation, tx, ty);
}
static public void place(IEntity pAnchored, IEntity pMovable, AnchorRelation pRelation) {
place(pAnchored, pMovable, pRelation, 0, 0);
}
static public void place(IEntity pAnchored, IEntity pMovable, AnchorRelation pRelation, final float tX, final float tY) {
place(pAnchored, pMovable, pRelation, tX, tY, true, true);
}
static public void placeVertical(IEntity pAnchored, IEntity pMovable, AnchorRelation pRelation, final float tX, final float tY) {
place(pAnchored, pMovable, pRelation, tX, tY, true, false);
}
static public void placeHorizontal(IEntity pAnchored, IEntity pMovable, AnchorRelation pRelation, final float tX, final float tY) {
place(pAnchored, pMovable, pRelation, tX, tY, false, true);
}
static public void place(IEntity pAnchored, IEntity pMovable, AnchorRelation pRelation, final float tX, final float tY, boolean pVertical, boolean pHorizontal) {
pAnchored.convertLocalCoordinatesToSceneCoordinates(
pRelation.anchorA.X * pAnchored.getWidth(),
pRelation.anchorA.Y * pAnchored.getHeight(), TMP_ENTITY_ANCHORED);
pMovable.convertLocalCoordinatesToSceneCoordinates(
pRelation.anchorB.X * pMovable.getWidth(),
pRelation.anchorB.Y * pMovable.getHeight(), TMP_ENTITY_MOVABLE);
final float dX = !pHorizontal ? 0 : TMP_ENTITY_MOVABLE[0] - TMP_ENTITY_ANCHORED[0];
final float dY = !pVertical ? 0 : TMP_ENTITY_MOVABLE[1] - TMP_ENTITY_ANCHORED[1];
pMovable.setPosition(pMovable.getX() - dX + tX, pMovable.getY() - dY + tY);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static class AnchorPoint {
public final float X;
public final float Y;
private AnchorPoint(final float pX, final float pY) {
this.X = pX;
this.Y = pY;
}
}
public static class AnchorRelation {
public final AnchorPoint anchorA;
public final AnchorPoint anchorB;
private AnchorRelation(final AnchorPoint pAnchorA, final AnchorPoint pAnchorB) {
this.anchorA = pAnchorA;
this.anchorB = pAnchorB;
}
}
public static enum eAnchorPoint {
L_TOP(0.0f,1.0f),C_TOP(0.5f,1.0f),R_TOP(1.0f,1.0f),
L_CNT(0.0f,0.5f),C_CNT(0.5f,0.5f),R_CNT(1.0f,0.5f),
L_BTM(0.0f,0.0f),C_BTM(0.5f,0.0f),R_BTM(1.0f,0.0f);
public final AnchorPoint anchor;
private eAnchorPoint(final float pX, final float pY) {
anchor = new AnchorPoint(pX, pY);
}
}
public static enum eAnchorRelation {
HOR_L_TOP(eAnchorPoint.L_TOP.anchor, eAnchorPoint.R_TOP.anchor),
HOR_L_CNT(eAnchorPoint.L_CNT.anchor, eAnchorPoint.R_CNT.anchor),
HOR_L_BTM(eAnchorPoint.L_BTM.anchor, eAnchorPoint.R_BTM.anchor),
HOR_R_TOP(eAnchorPoint.R_TOP.anchor, eAnchorPoint.L_TOP.anchor),
HOR_R_CNT(eAnchorPoint.R_CNT.anchor, eAnchorPoint.L_CNT.anchor),
HOR_R_BTM(eAnchorPoint.R_BTM.anchor, eAnchorPoint.L_BTM.anchor),
VER_TOP_L(eAnchorPoint.L_TOP.anchor, eAnchorPoint.L_BTM.anchor),
VER_TOP_C(eAnchorPoint.C_TOP.anchor, eAnchorPoint.C_BTM.anchor),
VER_TOP_R(eAnchorPoint.R_TOP.anchor, eAnchorPoint.R_BTM.anchor),
VER_BTM_L(eAnchorPoint.L_BTM.anchor, eAnchorPoint.L_TOP.anchor),
VER_BTM_C(eAnchorPoint.C_BTM.anchor, eAnchorPoint.C_TOP.anchor),
VER_BTM_R(eAnchorPoint.R_BTM.anchor, eAnchorPoint.R_TOP.anchor),
IN_L_TOP(eAnchorPoint.L_TOP.anchor),
IN_L_CNT(eAnchorPoint.L_CNT.anchor),
IN_L_BTM(eAnchorPoint.L_BTM.anchor),
IN_R_TOP(eAnchorPoint.R_TOP.anchor),
IN_R_CNT(eAnchorPoint.R_CNT.anchor),
IN_R_BTM(eAnchorPoint.R_BTM.anchor),
IN_C_TOP(eAnchorPoint.C_TOP.anchor),
IN_C_CNT(eAnchorPoint.C_CNT.anchor),
IN_C_BTM(eAnchorPoint.C_BTM.anchor);
public final AnchorRelation relation;
private eAnchorRelation(final AnchorPoint pAnchorA, final AnchorPoint pAnchorB) {
relation = new AnchorRelation(pAnchorA, pAnchorB);
}
private eAnchorRelation(final AnchorPoint pAnchor) {
relation = new AnchorRelation(pAnchor, pAnchor);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment