jeu-de-combat/src/engine/gui/UIElement.java

65 lines
2.2 KiB
Java

package engine.gui;
import engine.Engine;
import engine.math.Vector3f;
import engine.object.Camera;
import engine.object.ObjectGl;
public class UIElement extends UIDummy{
private final ObjectGl obj;
private final Engine engine;
private final Camera camera;
private float scalingFactor;
private float xPos;
private float yPos;
private float zPos;
/**
* Crée un elements d'interface càd un sprite qui suis les mouvements de la camera, pas besoin d'ajouter l'ObjectGl
* dans la liste de rendu cette classe s'en occupe lors de l'initialisation
* @param obj l'objet à mettre dans le conteneur
* @param posX la position relative à la camera souhaité sur l'axe X, 0.0 à l'extreme gauche, 1.0 à l'extreme droite
* @param posY la position relative à la camera souhaité sur l'axe Y, 0.0 à l'extreme bas, 1.0 à l'extreme haut
* @param engine le moteur de rendu depuis lequel UIElement va determiner la camera à tracker
*/
public UIElement(ObjectGl obj, float posX, float posY, Engine engine){
this.obj = obj;
this.engine = engine;
this.camera = engine.getCamera();
this.scalingFactor = obj.getScalingFactor();
this.xPos = posX;
this.yPos = posY;
this.zPos = obj.getZPos();
this.getObjInPosition();
}
public void init(){
this.engine.add_objectGl(obj);
}
private void getObjInPosition(){
obj.resetTransform();
obj.scale(new Vector3f(this.scalingFactor, this.scalingFactor, 1.0f));
// Position in the camera space
float dimension = this.camera.getDimension();
float ar = this.camera.getAspectRatio().y / this.camera.getAspectRatio().x;
float x = dimension * 2 * this.xPos - dimension;
float y = dimension * ar * 2 * this.yPos - dimension * ar;
obj.translate(new Vector3f(x, y, this.zPos));
System.out.println(x);
// Camera position
obj.translate(new Vector3f(- engine.transformationView.x, engine.transformationView.y));
}
public void updateScalingFactor(float scaleFactor){
this.scalingFactor *= scaleFactor;
}
public void update(){
this.getObjInPosition();
}
}