textbox and questwindow now on its own matrix -> own layer on top of game world - font stopped working, not using .ttf file

master
GammelJAN 5 years ago
parent d483fab4a4
commit 0bd73badda

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 725 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 725 B

@ -25,6 +25,7 @@ import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.trs.main.view.UI.Textbox;
/**
*

@ -11,8 +11,10 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.trs.main.Main;
@ -28,21 +30,26 @@ public class FightDialogue {
int selectedAsw = 0;
String[] ans = {"Melee", "Range", "Skip"};
public FightDialogue(float camX, float camY){
public static Matrix4 m;
Batch batch = new SpriteBatch();
public FightDialogue(){
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontData/font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 21;
font = generator.generateFont(parameter);
generator.dispose();
font.setColor(Color.BLACK);
renderer = new ShapeRenderer();
r = new Rectangle(camX-Main.CAMERA_WIDTH/2f + Main.CAMERA_WIDTH*0.8f, camY-Main.CAMERA_HEIGHT/2 + Main.CAMERA_HEIGHT*0.2f, Main.CAMERA_WIDTH*0.18f, Main.CAMERA_HEIGHT*0.6f);
r = new Rectangle(Main.CAMERA_WIDTH - 200, 20, 180, Main.CAMERA_HEIGHT-40);
}
public void draw(Batch batch){
public void draw(Batch camBatch){
batch.setProjectionMatrix(m);
if(Gdx.input.isKeyJustPressed(Input.Keys.DOWN)){
if(selectedAsw < ans.length - 1) {

@ -13,7 +13,7 @@ import com.trs.main.StaticMath;
public class FightScreen {
final int gridWidth = 20;
final int gridWidth = 18;
final int gridHeight = 12;
Batch batch;
@ -29,16 +29,16 @@ public class FightScreen {
int state = 0;
public FightScreen(Batch batch, FightObject[] objects, Rectangle[] collisionRects, float camX, float camY) {
this.batch = batch;
this.objects = objects;
this.collisionRects = collisionRects;
this.renderer = new ShapeRenderer();
this.fightDialogue = new FightDialogue(camX, camY);
gridPos = new Vector2();
gridPos.x = (float)(Math.ceil((double)(camX-Main.CAMERA_WIDTH/2)/32.0) * 32.0) + 64;
gridPos.y = (float)(Math.ceil((double)(camY-Main.CAMERA_HEIGHT/2)/32.0) * 32.0) + 32;
this.batch = batch;
this.objects = objects;
this.collisionRects = collisionRects;
this.renderer = new ShapeRenderer();
this.fightDialogue = new FightDialogue();
gridPos = new Vector2();
gridPos.x = (float)(Math.ceil((double)(camX-Main.CAMERA_WIDTH/2)/32.0) * 32.0) + 64;
gridPos.y = (float)(Math.ceil((double)(camY-Main.CAMERA_HEIGHT/2)/32.0) * 32.0) + 32;
// SORTING OBJECTS BY INITIATIVE STAT

@ -15,7 +15,7 @@ import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Matrix4;
import com.trs.main.InformationQuest;
import com.trs.main.Main;
import com.trs.main.Quest;
@ -36,11 +36,17 @@ public class QuestWindow {
double visiblePerc;
public QuestWindow(){
Batch batch = new SpriteBatch();
public QuestWindow(Matrix4 m){
renderer = new ShapeRenderer();
visible = true;
selectedQuest = 0;
Matrix4 uiMatrix = m.cpy();
uiMatrix.setToOrtho2D(0, 0, Main.CAMERA_WIDTH, Main.CAMERA_HEIGHT);
batch.setProjectionMatrix(uiMatrix);
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontData/font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 21;
@ -49,7 +55,7 @@ public class QuestWindow {
font.setColor(Color.WHITE);
}
public void draw(Quest[] quests, Batch batch, float playerX, float playerY){
public void draw(Quest[] quests){
if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)){
if(selectedQuest < quests.length-1){
@ -63,10 +69,10 @@ public class QuestWindow {
}
float boxX = playerX + Main.CAMERA_WIDTH/2 - 200;
float boxY = playerY + Main.CAMERA_HEIGHT/2 - 0.4f*Main.CAMERA_HEIGHT - 30;
float boxWidth = 0.2f*Main.CAMERA_WIDTH;
float boxHeight = 0.4f*Main.CAMERA_HEIGHT;
float boxX = Main.CAMERA_WIDTH - 200;
float boxY = Main.CAMERA_HEIGHT - 250;
float boxWidth = 180;
float boxHeight = 230;
renderer.setProjectionMatrix(batch.getProjectionMatrix());

@ -3,18 +3,23 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.trs.main;
package com.trs.main.view.UI;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.trs.main.Dialogue;
import com.trs.main.Main;
import java.util.ArrayList;
/**
@ -23,6 +28,9 @@ import java.util.ArrayList;
*/
public class Textbox extends Actor{
final Color TextColor = Color.WHITE;
final Color SelectedColor = Color.FIREBRICK;
BitmapFont font;
Rectangle r;
float printChar;
@ -30,8 +38,6 @@ public class Textbox extends Actor{
float textSpeed = 0.8f;
//ArrayList<String> splitted;
ShapeRenderer renderer;
int state; // 0: drawing 1: waiting for input 2: finished
int selectedAsw = 0;
@ -42,15 +48,22 @@ public class Textbox extends Actor{
String toPrint;
public Textbox(String toPrint, String[] ans, float xPos, float yPos) {
Texture txt = new Texture("txt.png");
Texture txtTop = new Texture("txt_top.png");
Texture txtBot = new Texture("txt_bot.png");
public static Matrix4 m;
Batch batch = new SpriteBatch();
public Textbox(String toPrint, String[] ans) {
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontData/font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 21;
font = generator.generateFont(parameter);
generator.dispose();
font.setColor(Color.BLACK);
renderer = new ShapeRenderer();
textHeight = getTextHeight(font,"A");
printChar = 0;
this.ans = ans;
@ -58,40 +71,13 @@ public class Textbox extends Actor{
font = new BitmapFont();
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;
r = new Rectangle(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20, Main.CAMERA_WIDTH - 40, 0);
r = new Rectangle(20, 20, 814, 0);
setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight());
this.toPrint = toPrint;
state = 0;
}
public Textbox(Textbox t, float xPos, float yPos){
font = new BitmapFont();
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontData/font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 21;
font = generator.generateFont(parameter);
generator.dispose();
font.setColor(Color.BLACK);
textHeight = getTextHeight(font,"A");
this.ans = t.ans;
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();
printChar = 0;
setName("textbox");
this.toPrint = t.toPrint;
}
public void update(Dialogue d) {
//this.splitted = getSplitted(d.question, (int) Main.CAMERA_WIDTH / 2);
this.toPrint = d.question;
@ -155,82 +141,52 @@ public class Textbox extends Actor{
@Override
public void draw(Batch batch, float parentAlpha) {
public void draw(Batch camBatch, float parentAlpha) {
camBatch.end();
batch.setProjectionMatrix(m);
batch.begin();
int alignment = -1;
font.setColor(Color.CLEAR);
float height = font.draw(batch, toPrint.substring(0, (int)printChar), getX()+2, getY(), getWidth(), alignment, true).height+5;
float textHeight = height;
float height = font.draw(batch, toPrint.substring(0, (int)printChar), getX()+2, getY(), getWidth(), alignment, true).height;
float textHeight = getTextHeight(font, "A");
if(state == 1){
for(String s : ans){
height += getTextHeight(font, "A") + 10;
height += textHeight + 5;
}
}
setHeight(height);
font.setColor(Color.BLACK);
batch.end();
renderer.setProjectionMatrix(batch.getProjectionMatrix());
renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.setColor(Color.BLUE);
renderer.rect(getX(), getY(), getWidth(), getHeight());
renderer.end();
renderer.begin(ShapeRenderer.ShapeType.Line);
renderer.setColor(Color.RED);
renderer.rect(getX(), getY(), getWidth(), getHeight());
renderer.end();
batch.begin();
int amount = 1+(int)(getHeight()/txt.getHeight());
batch.draw(txtTop, getX(), getY() + amount*txt.getHeight());
for(int i = 0; i < amount; i++){
batch.draw(txt, getX(), getY() + txt.getHeight()*i);
}
batch.draw(txtBot, getX(), getY() - txtBot.getHeight());
font.draw(batch, toPrint.substring(0, (int)printChar), getX()+6, getY()+getHeight()-3, getWidth()-8, alignment, true);
font.setColor(TextColor);
font.draw(batch, toPrint.substring(0, (int)printChar), getX()+6, getY()+getHeight(), getWidth()-8, alignment, true);
if(state == 1){
for(int i = 0; i < ans.length; i++){
if(selectedAsw == i){
font.setColor(Color.RED);
font.setColor(SelectedColor);
}
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.draw(batch, ans[i], getX()+20, (getY()+getHeight()-5) - (textHeight + i*(getTextHeight(font, "A")+5)), getWidth(), alignment, true);
font.setColor(TextColor);
}
}
batch.end();
camBatch.begin();
super.draw(batch, parentAlpha);
}
/*
public ArrayList<String> getSplitted(String toSplit, int maxLength){
ArrayList<String> words = new ArrayList<>();
int tail = 0;
for(int head = 0; head < toSplit.length(); head++){
if(toSplit.charAt(head) == ' '){
words.add(toSplit.substring(tail, head+1));
head++;
tail=head;
}
}
words.add(toSplit.substring(tail, toSplit.length()));
ArrayList<String> toReturn = new ArrayList<>();
String string = new String();
for(String s : words){
if(getTextWidth(font,string)+getTextWidth(font,s) >= getWidth()){
toReturn.add(string);
string = new String();
string += s;
}
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+"-"+getTextWidth(font,s));
}
return toReturn;
}
*/
public float getTextWidth(BitmapFont font, String text){
GlyphLayout glyphLayout = new GlyphLayout();

@ -6,11 +6,14 @@
package com.trs.main.view.screens;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.trs.main.Main;
import com.trs.main.MapContainer;
import com.trs.main.Quest;
import com.trs.main.fightscreen.FightDialogue;
import com.trs.main.view.UI.QuestWindow;
import com.trs.main.view.UI.Textbox;
import com.trs.main.worldobjects.Player;
/**
@ -25,7 +28,11 @@ public class GameScreen extends AbstractScreen{
public GameScreen(Game game, float CAMERA_WIDTH, float CAMERA_HEIGHT) {
super(game, CAMERA_WIDTH, CAMERA_HEIGHT);
map = new MapContainer(CAMERA_WIDTH, CAMERA_HEIGHT, new Player(200, 200), "tiledmapData/maps/map1.tmx", 2, 1);
qw = new QuestWindow();
qw = new QuestWindow(map.getCamera().combined);
Matrix4 uiMatrix = map.getCamera().combined.cpy();
uiMatrix.setToOrtho2D(0, 0, Main.CAMERA_WIDTH, Main.CAMERA_HEIGHT);
Textbox.m = uiMatrix;
FightDialogue.m = uiMatrix;
}
public void loadNewMap(int map, int doorId){
@ -48,7 +55,7 @@ public class GameScreen extends AbstractScreen{
}
//qw.draw(rects, map.getStage().getBatch(), map.getPlayer().getX()+32, map.getPlayer().getY()+32);
qw.draw(rects, map.getStage().getBatch(), map.getStage().getCamera().position.x, map.getStage().getCamera().position.y );
qw.draw(rects);
if(map.getCollidingDoor() != null) {
loadNewMap(map.getCollidingDoor().destinationMap, map.getCollidingDoor().destinationDoor);

@ -9,7 +9,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
import com.trs.main.Dialogue;
import com.trs.main.DialogueParser;
import com.trs.main.Main;
import com.trs.main.Textbox;
import com.trs.main.view.UI.Textbox;
public class InteractionObject extends Actor{
Textbox t;
@ -37,7 +37,7 @@ public class InteractionObject extends Actor{
dialoguePath = "mapData/map"+mapId+"/interactionObjects/"+id+"/dialogue.txt";
parser = new DialogueParser(dialoguePath);
Dialogue nextDialogue = parser.firstDialogue();
this.t = new Textbox(nextDialogue.question, nextDialogue.ans, getX()+getWidth()/2, getY()+getHeight()/2);
this.t = new Textbox(nextDialogue.question, nextDialogue.ans);
setBounds(xPos, yPos, animatedSprite.getSprite().getWidth(), animatedSprite.getSprite().getHeight());
}
@ -53,14 +53,14 @@ public class InteractionObject extends Actor{
dialoguePath = "mapData/map"+mapId+"/interactionObjects/"+id+"/dialogue.txt";
parser = new DialogueParser(dialoguePath);
Dialogue nextDialogue = parser.firstDialogue();
this.t = new Textbox(nextDialogue.question, nextDialogue.ans, getX()+getWidth()/2, getY()+getHeight()/2);
this.t = new Textbox(nextDialogue.question, nextDialogue.ans);
setBounds(xPos, yPos, collisionRect.getWidth(), collisionRect.getHeight());
}
public void startDialogue(float xPos, float yPos) {
currentlyTalking = true;
getStage().addActor(new Textbox(t, xPos, yPos));
getStage().addActor(t);
}
@Override

@ -19,7 +19,7 @@ import com.trs.main.InformationQuest;
import com.trs.main.Main;
import com.trs.main.Quest;
import com.trs.main.StaticMath;
import com.trs.main.Textbox;
import com.trs.main.view.UI.Textbox;
/**
*
@ -54,6 +54,7 @@ public class MovingNpc extends Actor{
private AnimatedSprite questBubble;
public MovingNpc(Rectangle area, float xPos, float yPos, int id, int mapId, String texture){
super();
setName("npc");
this.id = id;
this.mapId = mapId;
@ -76,16 +77,17 @@ public class MovingNpc extends Actor{
dialoguePath = "mapData/map"+mapId+"/npcs/"+id+"/dialogue.txt";
parser = new DialogueParser(dialoguePath);
Dialogue nextDialogue = parser.firstDialogue();
this.t = new Textbox(nextDialogue.question, nextDialogue.ans, getX()+getWidth()/2, getY()+getHeight()/2);
this.t = new Textbox(nextDialogue.question, nextDialogue.ans);
collisionRect = new Rectangle(xPos + 16, yPos, 32, 16);
setBounds(xPos, yPos, animatedSprite.getSprite().getWidth(), animatedSprite.getSprite().getHeight());
}
public void startDialogue(float xPos, float yPos) {
currentlyTalking = true;
getStage().addActor(new Textbox(t, xPos, yPos));
getStage().addActor(t);
}
public void startSpecialDialogue(String path, float xPos, float yPos){
@ -93,8 +95,8 @@ public class MovingNpc extends Actor{
currentlyTalking = true;
specialDialogue = true;
Dialogue nextDialogue = tempSpecialDialogueParser.firstDialogue();
this.t = new Textbox(nextDialogue.question, nextDialogue.ans, getX()+getWidth()/2, getY()+getHeight()/2);
getStage().addActor(new Textbox(t, xPos, yPos));
this.t = new Textbox(nextDialogue.question, nextDialogue.ans);
getStage().addActor(t);
}
@Override
@ -134,7 +136,7 @@ public class MovingNpc extends Actor{
specialDialogue = false;
parser = new DialogueParser(dialoguePath);
Dialogue nextDialogue = parser.firstDialogue();
this.t = new Textbox(nextDialogue.question, nextDialogue.ans, getX()+getWidth()/2, getY()+getHeight()/2);
this.t = new Textbox(nextDialogue.question, nextDialogue.ans);
tempSpecialDialogueParser = null;
}
else {

Loading…
Cancel
Save