127 lines
3.5 KiB
Java

package engine.object;
import engine.Engine;
import engine.gui.UIElement;
import engine.math.Vector3f;
import java.util.ArrayList;
import java.util.List;
/**
* Un texte construit selon la même logique qu'une sprite pour faire un element d'interface utilisez UIElementText
*/
public class Text {
private List<Letter> charList;
private final float size;
private final Engine engine;
private final float zPos;
private Vector3f transformation;
public Text(String text, float z, float size, Engine engine){
this.charList = new ArrayList<>();
this.zPos = z;
this.size = size;
this.engine = engine;
this.transformation = new Vector3f();
this.textToArrayObjectGl(text);
}
public void show(){
this.addCharListInEngine();
}
private void textToArrayObjectGl(String s){
for (int i = 0; i < s.length(); i++){
this.charList.add(this.charToObjectGl(s.charAt(i), i));
}
}
private void addCharListInEngine(){
int i = 0;
for (Letter obj : this.charList){
addCharInEngine(i, obj);
i++;
}
}
private void addCharInEngine(int i, Letter obj){
obj.translate(new Vector3f(i * 10.0f * this.size, 0.0f, 0.0f));
obj.translate(transformation);
this.engine.add_objectGl(obj);
}
public void linkToUIElement(UIElement ui){
}
public void setNewText(String text){
int i = 0;
for (Letter obj : this.charList) {
ObjectGlSetCharWrap(text.charAt(i), obj);
i++;
if (i >= text.length()) break;
}
while (i < text.length()){
Letter obj = this.charToObjectGl(text.charAt(i), i);
this.charList.add(obj);
addCharInEngine(i, obj);
i++;
}
if (i < this.charList.size()) removeFromIndexToEnd(i);
}
public void translate(Vector3f vec){
transformation = transformation.addXYZ(vec);
for (ObjectGl obj : this.charList){
obj.translate(vec);
}
}
public void remove(){
for (ObjectGl obj : this.charList){
this.engine.remove_objectGl(obj);
}
}
public List<ObjectGl> getObj(){
return new ArrayList<>(this.charList);
}
private void removeFromIndexToEnd(int i){
int j = 0;
for (Letter objectGl : this.charList){
if (j >= i) {
this.engine.remove_objectGl(objectGl);
}
j++;
}
this.charList = this.charList.subList(0, i);
}
private Letter charToObjectGl(char a, int index){
Letter objectGl = new Letter(this.zPos, 1.0f, 1.0f, this.size, "textures/dejavu10x10_gs_tc.png", null, index);
objectGl.setShader("shaders/StylishShaders/BasicVert.glsl","shaders/StylishShaders/TextFrag.glsl");
ObjectGlSetCharWrap(a, objectGl);
return objectGl;
}
private void ObjectGlSetCharWrap(char a, ObjectGl obj){
if (a < 132 && a > 96){
obj.setTextureWrap(0.0f + (a - 97) * 10.0f,40.0f,10.0f,10.0f, ObjectGl.DEFAULT);
}
else if (a < 91 && a > 64){
obj.setTextureWrap(0.0f + (a - 65) * 10.0f,30.0f,10.0f,10.0f, ObjectGl.DEFAULT);
}
else if (a < 64 && a > 31){
obj.setTextureWrap(0.0f + (a - 32) * 10.0f,0.0f,10.0f,10.0f, ObjectGl.DEFAULT);
}
}
public List<Letter> getCharList(){
return this.charList;
}
}