textbox refactored

master
GammelJAN 5 years ago
parent 50b459acc0
commit 29fa78cb4b

@ -1,21 +1,22 @@
Wie gehts Wie gehts. Das hier ist ein extra langer Text, mit dem ich testenasdfasdf will, ob die Textbox die Texte sinnvoll splittet, und auch die komplette potentielle Breite füllt.
gut#5 gut#6
schlecht#9 schlecht#10
abbrechen#-1
Das freut mich Das freut mich
wie gehts dir#20 wie gehts dir#21
tschüss#-1 tschüss#-1
Das tut mir leid. Wieso denn? Das tut mir leid. Wieso denn?
Spaß mir gehts gut#5 Spaß mir gehts gut#6
tschüss#-1 tschüss#-1
Halts Maul#14 Halts Maul#15
Ey es gibt gleich Stress hier Ey es gibt gleich Stress hier
cool, bin ich dabei#17 cool, bin ich dabei#18
KAMPF KAMPF
LOS#-1 LOS#-1
Ich will Krieg Ich will Krieg
KRIEG!#17 KRIEG!#18

@ -27,8 +27,7 @@ public class Textbox extends Actor{
Rectangle r; Rectangle r;
int printLine; int printLine;
int printChar; int printChar;
ArrayList<String> splitted; //ArrayList<String> splitted;
String toPring;
ShapeRenderer renderer; ShapeRenderer renderer;
@ -40,6 +39,8 @@ public class Textbox extends Actor{
float textHeight; float textHeight;
String toPrint;
public Textbox(String toPrint, String[] ans, float xPos, float yPos) { public Textbox(String toPrint, String[] ans, float xPos, float yPos) {
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontData/font.ttf")); FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontData/font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
@ -49,20 +50,23 @@ public class Textbox extends Actor{
font.setColor(Color.BLACK); font.setColor(Color.BLACK);
renderer = new ShapeRenderer(); renderer = new ShapeRenderer();
textHeight = getTextHeight("A"); textHeight = getTextHeight(font,"A");
printLine = 0; printLine = 0;
printChar = 0; printChar = 0;
this.ans = ans; this.ans = ans;
setName("textbox"); setName("textbox");
font = new BitmapFont(); font = new BitmapFont();
splitted = getSplitted(toPrint, (int)(Main.CAMERA_WIDTH/2));
setWidth(Main.CAMERA_WIDTH - 40);
//splitted = getSplitted(toPrint, (int)(Main.CAMERA_WIDTH - 40));
// CALCULATE NEEDED HEIGHT // CALCULATE NEEDED HEIGHT
float height = splitted.size() * 1.2f * textHeight + (ans.length+1) * 1.2f * textHeight; //float height = splitted.size() * 1.2f * textHeight + (ans.length+1) * 1.2f * textHeight;
r = new Rectangle(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20, Main.CAMERA_WIDTH/2, height); r = new Rectangle(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20, Main.CAMERA_WIDTH - 40, 0);
setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight());
this.toPrint = toPrint;
state = 0; state = 0;
} }
@ -75,27 +79,27 @@ public class Textbox extends Actor{
generator.dispose(); generator.dispose();
font.setColor(Color.BLACK); font.setColor(Color.BLACK);
textHeight = getTextHeight("A"); textHeight = getTextHeight(font,"A");
this.splitted = t.splitted;
this.ans = t.ans; this.ans = t.ans;
//System.out.println(splitted.size()); this.r = t.r;
float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight; this.r.setPosition(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20);
r = new Rectangle(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20, Main.CAMERA_WIDTH - 40, height);
setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight());
renderer = new ShapeRenderer(); renderer = new ShapeRenderer();
printLine = 0; printLine = 0;
printChar = 0; printChar = 0;
setName("textbox"); setName("textbox");
this.toPrint = t.toPrint;
} }
public void update(Dialogue d) { public void update(Dialogue d) {
this.splitted = getSplitted(d.question, (int) Main.CAMERA_WIDTH / 2); //this.splitted = getSplitted(d.question, (int) Main.CAMERA_WIDTH / 2);
this.toPrint = d.question;
this.ans = d.ans; this.ans = d.ans;
float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight; //float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight;
r = new Rectangle(getX(), getY(), getWidth(), height); r = new Rectangle(getX(), getY(), getWidth(), 0);
setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight());
this.state = 0; this.state = 0;
printLine = 0; printLine = 0;
@ -106,8 +110,6 @@ public class Textbox extends Actor{
@Override @Override
public void act(float delta) { public void act(float delta) {
if(state == 1){ if(state == 1){
if(Gdx.input.isKeyJustPressed(Input.Keys.DOWN)){ if(Gdx.input.isKeyJustPressed(Input.Keys.DOWN)){
if(selectedAsw < ans.length - 1) { if(selectedAsw < ans.length - 1) {
@ -130,21 +132,14 @@ public class Textbox extends Actor{
state = 3; state = 3;
} }
else{ else{
if(printChar >= splitted.get(printLine).length()){ if(printChar >= toPrint.length()){
if(splitted.size()-1 <= printLine){
state = 1; state = 1;
} }
else{
printLine++;
printChar = 0;
}
}
else{ else{
printChar++; printChar++;
} }
if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){ if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){
state = 1; state = 1;
printLine = splitted.size()-1;
} }
} }
@ -162,7 +157,18 @@ public class Textbox extends Actor{
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
int alignment = -1;
font.setColor(Color.CLEAR);
float height = font.draw(batch, toPrint.substring(0, printChar), getX()+2, getY(), getWidth(), alignment, true).height+5;
float textHeight = height;
for(String s : ans){
height += getTextHeight(font, "A") + 10;
}
setHeight(height);
font.setColor(Color.BLACK); font.setColor(Color.BLACK);
batch.end(); batch.end();
renderer.setProjectionMatrix(batch.getProjectionMatrix()); renderer.setProjectionMatrix(batch.getProjectionMatrix());
renderer.begin(ShapeRenderer.ShapeType.Filled); renderer.begin(ShapeRenderer.ShapeType.Filled);
@ -174,35 +180,23 @@ public class Textbox extends Actor{
renderer.rect(getX(), getY(), getWidth(), getHeight()); renderer.rect(getX(), getY(), getWidth(), getHeight());
renderer.end(); renderer.end();
batch.begin(); batch.begin();
if(state == 0){
for(int i = 0; i < splitted.size(); i++){
if(i == printLine){
font.draw(batch, splitted.get(i).substring(0, printChar), getX()+5, getY() + getHeight()-i*1.2f*textHeight - 5);
}
else if(i < printLine){
font.draw(batch, splitted.get(i), getX()+5, getY() + getHeight()-i*1.2f*textHeight - 5);
}
}
}
else{
for(int i = 0; i <= printLine; i++){
font.draw(batch, splitted.get(i), getX()+5, getY() + getHeight()-i*1.2f*textHeight - 5);
}
font.draw(batch, toPrint.substring(0, printChar), getX()+2, getY()+getHeight()-3, getWidth(), alignment, true);
if(state == 1){
for(int i = 0; i < ans.length; i++){ for(int i = 0; i < ans.length; i++){
if(selectedAsw == i){ if(selectedAsw == i){
font.setColor(Color.RED); font.setColor(Color.RED);
} }
else { font.draw(batch, ans[i], getX()+20, (getY()+getHeight()-5) - (textHeight + i*(getTextHeight(font, "A")+5)) - 3, getWidth(), alignment, true);
font.setColor(Color.BLACK); font.setColor(Color.BLACK);
} }
font.draw(batch, ans[i], getX()+20, getY() + getHeight() - ((splitted.size() + i + 1) * 1.2f * textHeight - 5));
}
} }
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
} }
/*
public ArrayList<String> getSplitted(String toSplit, int maxLength){ public ArrayList<String> getSplitted(String toSplit, int maxLength){
ArrayList<String> words = new ArrayList<>(); ArrayList<String> words = new ArrayList<>();
int tail = 0; int tail = 0;
@ -218,31 +212,33 @@ public class Textbox extends Actor{
ArrayList<String> toReturn = new ArrayList<>(); ArrayList<String> toReturn = new ArrayList<>();
String string = new String(); String string = new String();
for(String s : words){ for(String s : words){
if(getTextWidth(string)+getTextWidth(s) >= maxLength){ if(getTextWidth(font,string)+getTextWidth(font,s) >= getWidth()){
toReturn.add(string); toReturn.add(string);
string = new String(); string = new String();
string += s; string += s;
} }
else if(getTextWidth(string)+getTextWidth(s) < maxLength){ else if(getTextWidth(font,string)+getTextWidth(font,s) < getWidth()){
string += s; string += s;
} }
} }
toReturn.add(string); toReturn.add(string);
System.out.println(getWidth());
System.out.println(maxLength);
for(String s : toReturn){ for(String s : toReturn){
//System.out.println("-"+s+"-"); System.out.println("-"+s+"-"+getTextWidth(font,s));
} }
return toReturn; return toReturn;
} }
*/
public float getTextWidth(BitmapFont font, String text){
public float getTextWidth(String text){
GlyphLayout glyphLayout = new GlyphLayout(); GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font,text); glyphLayout.setText(font,text);
return glyphLayout.width; return glyphLayout.width;
} }
public float getTextHeight(String text){ public float getTextHeight(BitmapFont font,String text){
GlyphLayout glyphLayout = new GlyphLayout(); GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font,text); glyphLayout.setText(font,text);
return glyphLayout.height; return glyphLayout.height;

@ -70,7 +70,7 @@ public class QuestWindow {
renderer.setProjectionMatrix(batch.getProjectionMatrix()); renderer.setProjectionMatrix(batch.getProjectionMatrix());
if(visible && Main.gamestate != 2){ if(visible && Main.gamestate != 2 && Main.gamestate != 1){
if(visiblePerc < 1){ if(visiblePerc < 1){
visiblePerc += animationSpeed; visiblePerc += animationSpeed;
Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glEnable(GL20.GL_BLEND);

Loading…
Cancel
Save