127 lines
3.5 KiB
Java
Raw Normal View History

package engine.object;
import engine.Engine;
2021-06-04 17:29:17 +02:00
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;
2021-06-03 22:07:52 +02:00
private final float size;
private final Engine engine;
private final float zPos;
2021-06-04 00:53:14 +02:00
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;
2021-06-04 00:53:14 +02:00
this.transformation = new Vector3f();
this.textToArrayObjectGl(text);
}
public void show(){
2021-06-03 22:07:52 +02:00
this.addCharListInEngine();
}
private void textToArrayObjectGl(String s){
for (int i = 0; i < s.length(); i++){
this.charList.add(this.charToObjectGl(s.charAt(i), i));
}
2021-06-03 22:07:52 +02:00
}
private void addCharListInEngine(){
int i = 0;
for (Letter obj : this.charList){
2021-06-03 22:48:54 +02:00
addCharInEngine(i, obj);
i++;
}
}
private void addCharInEngine(int i, Letter obj){
2021-06-03 22:48:54 +02:00
obj.translate(new Vector3f(i * 10.0f * this.size, 0.0f, 0.0f));
2021-06-04 00:53:14 +02:00
obj.translate(transformation);
2021-06-03 22:48:54 +02:00
this.engine.add_objectGl(obj);
}
2021-06-04 00:53:14 +02:00
public void linkToUIElement(UIElement ui){
}
2021-06-03 22:07:52 +02:00
public void setNewText(String text){
2021-06-03 22:48:54 +02:00
int i = 0;
for (Letter obj : this.charList) {
2021-06-03 22:48:54 +02:00
ObjectGlSetCharWrap(text.charAt(i), obj);
i++;
if (i >= text.length()) break;
}
while (i < text.length()){
Letter obj = this.charToObjectGl(text.charAt(i), i);
2021-06-03 22:48:54 +02:00
this.charList.add(obj);
addCharInEngine(i, obj);
i++;
}
if (i < this.charList.size()) removeFromIndexToEnd(i);
2021-06-03 22:07:52 +02:00
}
public void translate(Vector3f vec){
2021-06-04 00:53:14 +02:00
transformation = transformation.addXYZ(vec);
for (ObjectGl obj : this.charList){
obj.translate(vec);
}
}
2021-06-03 20:19:58 +02:00
public void remove(){
for (ObjectGl obj : this.charList){
this.engine.remove_objectGl(obj);
}
}
public List<ObjectGl> getObj(){
return new ArrayList<>(this.charList);
}
2021-06-03 22:48:54 +02:00
private void removeFromIndexToEnd(int i){
int j = 0;
for (Letter objectGl : this.charList){
2021-06-04 01:05:05 +02:00
if (j >= i) {
this.engine.remove_objectGl(objectGl);
}
2021-06-03 22:48:54 +02:00
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");
2021-06-03 22:48:54 +02:00
ObjectGlSetCharWrap(a, objectGl);
return objectGl;
}
private void ObjectGlSetCharWrap(char a, ObjectGl obj){
if (a < 132 && a > 96){
2021-06-03 22:48:54 +02:00
obj.setTextureWrap(0.0f + (a - 97) * 10.0f,40.0f,10.0f,10.0f, ObjectGl.DEFAULT);
}
else if (a < 91 && a > 64){
2021-06-03 22:48:54 +02:00
obj.setTextureWrap(0.0f + (a - 65) * 10.0f,30.0f,10.0f,10.0f, ObjectGl.DEFAULT);
}
2021-06-03 22:07:52 +02:00
else if (a < 64 && a > 31){
2021-06-03 22:48:54 +02:00
obj.setTextureWrap(0.0f + (a - 32) * 10.0f,0.0f,10.0f,10.0f, ObjectGl.DEFAULT);
2021-06-03 22:07:52 +02:00
}
}
public List<Letter> getCharList(){
2021-06-04 22:35:31 +02:00
return this.charList;
}
}