From 29fa78cb4bde1794db49542af09c08d1922c355e Mon Sep 17 00:00:00 2001 From: GammelJAN Date: Sun, 24 Jan 2021 13:51:25 +0100 Subject: [PATCH] textbox refactored --- core/assets/mapData/map1/npcs/0/dialogue.txt | 17 +-- core/src/com/trs/main/Textbox.java | 108 +++++++++--------- .../src/com/trs/main/view/UI/QuestWindow.java | 2 +- 3 files changed, 62 insertions(+), 65 deletions(-) diff --git a/core/assets/mapData/map1/npcs/0/dialogue.txt b/core/assets/mapData/map1/npcs/0/dialogue.txt index f29f676..6714eec 100644 --- a/core/assets/mapData/map1/npcs/0/dialogue.txt +++ b/core/assets/mapData/map1/npcs/0/dialogue.txt @@ -1,21 +1,22 @@ -Wie gehts -gut#5 -schlecht#9 +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#6 +schlecht#10 +abbrechen#-1 Das freut mich -wie gehts dir#20 +wie gehts dir#21 tschüss#-1 Das tut mir leid. Wieso denn? -Spaß mir gehts gut#5 +Spaß mir gehts gut#6 tschüss#-1 -Halts Maul#14 +Halts Maul#15 Ey es gibt gleich Stress hier -cool, bin ich dabei#17 +cool, bin ich dabei#18 KAMPF LOS#-1 Ich will Krieg -KRIEG!#17 \ No newline at end of file +KRIEG!#18 \ No newline at end of file diff --git a/core/src/com/trs/main/Textbox.java b/core/src/com/trs/main/Textbox.java index 83ff10e..5225dc7 100644 --- a/core/src/com/trs/main/Textbox.java +++ b/core/src/com/trs/main/Textbox.java @@ -27,8 +27,7 @@ public class Textbox extends Actor{ Rectangle r; int printLine; int printChar; - ArrayList splitted; - String toPring; + //ArrayList splitted; ShapeRenderer renderer; @@ -40,6 +39,8 @@ public class Textbox extends Actor{ float textHeight; + String toPrint; + public Textbox(String toPrint, String[] ans, float xPos, float yPos) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontData/font.ttf")); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); @@ -49,20 +50,23 @@ public class Textbox extends Actor{ font.setColor(Color.BLACK); renderer = new ShapeRenderer(); - textHeight = getTextHeight("A"); + textHeight = getTextHeight(font,"A"); printLine = 0; printChar = 0; this.ans = ans; setName("textbox"); 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 - 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()); + this.toPrint = toPrint; state = 0; } @@ -75,27 +79,27 @@ public class Textbox extends Actor{ generator.dispose(); font.setColor(Color.BLACK); - textHeight = getTextHeight("A"); - this.splitted = t.splitted; + textHeight = getTextHeight(font,"A"); this.ans = t.ans; - //System.out.println(splitted.size()); - float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight; - r = new Rectangle(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20, Main.CAMERA_WIDTH - 40, height); + this.r = t.r; + this.r.setPosition(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20); setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); renderer = new ShapeRenderer(); printLine = 0; printChar = 0; setName("textbox"); + this.toPrint = t.toPrint; } 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; - float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight; - r = new Rectangle(getX(), getY(), getWidth(), height); + //float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight; + r = new Rectangle(getX(), getY(), getWidth(), 0); setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); this.state = 0; printLine = 0; @@ -106,8 +110,6 @@ public class Textbox extends Actor{ @Override public void act(float delta) { - - if(state == 1){ if(Gdx.input.isKeyJustPressed(Input.Keys.DOWN)){ if(selectedAsw < ans.length - 1) { @@ -130,21 +132,14 @@ public class Textbox extends Actor{ state = 3; } else{ - if(printChar >= splitted.get(printLine).length()){ - if(splitted.size()-1 <= printLine){ - state = 1; - } - else{ - printLine++; - printChar = 0; - } + if(printChar >= toPrint.length()){ + state = 1; } else{ printChar++; } if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){ state = 1; - printLine = splitted.size()-1; } } @@ -162,7 +157,18 @@ public class Textbox extends Actor{ @Override public void draw(Batch batch, float parentAlpha) { - font.setColor(Color.BLACK); + + 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); + batch.end(); renderer.setProjectionMatrix(batch.getProjectionMatrix()); renderer.begin(ShapeRenderer.ShapeType.Filled); @@ -174,35 +180,23 @@ public class Textbox extends Actor{ renderer.rect(getX(), getY(), getWidth(), getHeight()); renderer.end(); 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); + + + 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++){ + if(selectedAsw == i){ + font.setColor(Color.RED); } + font.draw(batch, ans[i], getX()+20, (getY()+getHeight()-5) - (textHeight + i*(getTextHeight(font, "A")+5)) - 3, getWidth(), alignment, true); + font.setColor(Color.BLACK); } } - else{ - for(int i = 0; i <= printLine; i++){ - font.draw(batch, splitted.get(i), getX()+5, getY() + getHeight()-i*1.2f*textHeight - 5); - } - - for(int i = 0; i < ans.length; i++) { - if(selectedAsw == i) { - font.setColor(Color.RED); - } - else { - 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); } - + /* public ArrayList getSplitted(String toSplit, int maxLength){ ArrayList words = new ArrayList<>(); int tail = 0; @@ -218,31 +212,33 @@ public class Textbox extends Actor{ ArrayList toReturn = new ArrayList<>(); String string = new String(); for(String s : words){ - if(getTextWidth(string)+getTextWidth(s) >= maxLength){ + if(getTextWidth(font,string)+getTextWidth(font,s) >= getWidth()){ toReturn.add(string); string = new String(); string += s; } - else if(getTextWidth(string)+getTextWidth(s) < maxLength){ + else if(getTextWidth(font,string)+getTextWidth(font,s) < getWidth()){ string += s; } } toReturn.add(string); + System.out.println(getWidth()); + System.out.println(maxLength); for(String s : toReturn){ - //System.out.println("-"+s+"-"); + System.out.println("-"+s+"-"+getTextWidth(font,s)); } return toReturn; } - +*/ - public float getTextWidth(String text){ + public float getTextWidth(BitmapFont font, String text){ GlyphLayout glyphLayout = new GlyphLayout(); glyphLayout.setText(font,text); return glyphLayout.width; } - public float getTextHeight(String text){ + public float getTextHeight(BitmapFont font,String text){ GlyphLayout glyphLayout = new GlyphLayout(); glyphLayout.setText(font,text); return glyphLayout.height; diff --git a/core/src/com/trs/main/view/UI/QuestWindow.java b/core/src/com/trs/main/view/UI/QuestWindow.java index 219c9a7..f227f45 100644 --- a/core/src/com/trs/main/view/UI/QuestWindow.java +++ b/core/src/com/trs/main/view/UI/QuestWindow.java @@ -70,7 +70,7 @@ public class QuestWindow { renderer.setProjectionMatrix(batch.getProjectionMatrix()); - if(visible && Main.gamestate != 2){ + if(visible && Main.gamestate != 2 && Main.gamestate != 1){ if(visiblePerc < 1){ visiblePerc += animationSpeed; Gdx.gl.glEnable(GL20.GL_BLEND);