text card

This commit is contained in:
mmarc471@gmail.com
2020-09-09 15:24:50 +02:00
parent 10a0461b3b
commit 75df96126f
5 changed files with 92 additions and 5 deletions
@@ -10,6 +10,9 @@ import androidx.annotation.NonNull;
import com.iutlaval.myapplication.GameActivity;
import com.iutlaval.myapplication.InvalidDataException;
import java.util.ArrayList;
import java.util.List;
public class Drawable {
private float x,y;
@@ -64,15 +67,52 @@ public class Drawable {
System.out.println(x_scaled_size + " y: " + y_scaled_size);
System.out.println(y_size);
if(x_scaled_size <=0 || y_scaled_size <=0)
{
throw new InvalidDataException(name,x_scaled_size,y_scaled_size);
}
this.bitmap = Bitmap.createScaledBitmap(bitmap, (int)x_scaled_size, (int)y_scaled_size, GameActivity.bilinearFiltering);
Log.i("loul",""+this.bitmap.getWidth());
}
/**TODO : limiter la longeur des lignes et implementer le retour a la ligne
* ce constructeur permet de rendre du texte
* @param text le texte a rendre
* @param x_pos position en x du texte
* @param y_pos position en y du texte
* @param name nom du Drawable
* @param x_size taille du drawable entre 0 et 100
* @param y_size taille du drawable entre 0 et 100
*/
public Drawable(String text,float x_pos,float y_pos,String name, float x_size,float y_size,float textSize)
{
this(x_pos,y_pos,name);
//this ratio is used to dial the quality of the text;
List<String> linesOfText = cutText(20,text);
bitmap = Bitmap.createBitmap((int)(text.length()*textSize / linesOfText.size()), (int) 40*linesOfText.size(), Bitmap.Config.ARGB_8888);
checkPaint();
p.setTextSize(textSize*2);
Canvas c = new Canvas(bitmap);
int index = 1;
for(String line : linesOfText)
{
c.drawText(line ,0,textSize*2*index,p);
index++;
}
float x_scaled_size = x_size* GameActivity.screenWidth/100;
float y_scaled_size = y_size* GameActivity.screenHeight/100;
bitmap = Bitmap.createScaledBitmap(bitmap, (int)x_scaled_size, (int)y_scaled_size, GameActivity.bilinearFiltering);
}
/**
* ce constructeur n'est pas rapide essayer de l'utiliser le moin possible
* TODO : optimize it and remove the deprecated flag
@@ -149,8 +189,38 @@ public class Drawable {
this.y=y;
}
/**
* cette fonction permet d'initialiser le pinceau
*/
private void checkPaint() {
if(p==null)
p = new Paint();
}
/**
* cette fonction est appeler quand le drawable est retirer de la liste toDraw
*/
public void onDeletion(Renderer r){}
private List<String> cutText(int charPerLines,String text)
{
List<String> output = new ArrayList<>();
//TODO cut text in lines
String nextLine = "";
for (String word : text.split(" ")) {
if(word.length() < charPerLines)
{
if(word.length() + nextLine.length() +1 < charPerLines)
{
nextLine += " " + word;
}else{
output.add(nextLine);
nextLine="";
}
}else{
Log.e("TextRender","ERROR WORD TOO LONG :" + word);
}
}
return output;
}
}