Générateur de phrase simple / Processing

Génération de phrase; sujet, verbe, complément. Les expressions sont stockées dans un fichier XML.

Source:
sentencesProcessing.pde / Processing 3.5.3

XML xml;
XML[] childXml;
int childNumber;

PFont typoSelected;
String[] typoTable = {"arial.ttf", "cour.ttf", "times.ttf", "verdana.ttf"};
int textSizeMax;
int textSizeMin;
int textMaxWidth;
float sizeOfText;
int textXPosition;

String sentence;
int typoSelect;

int startTimer;
int timerlaps;

void setup(){
  size(900,600);
  //fullScreen();
  background(255,255,255);
  
  //Variable des textes
  textMaxWidth = int(width*0.9);
  textSizeMax = 120;
  textSizeMin = 80;
  timerlaps = 10000; //temps entre chaque generation de phrase (millisecondes)
  
  //Chargement XML
  xml = loadXML("base.xml");
  childXml = xml.getChildren("sentencePart");
  childNumber = childXml.length;
  println("childNumber: " + childNumber);
  
  makeSentence();   
}

void draw(){
  int timeNow = millis();
  if(timeNow-startTimer > timerlaps){
    makeSentence();
  }
}

void makeSentence(){
  startTimer = millis();
  
  sizeOfText = random(textSizeMin, textSizeMax);
  typoSelect = int(random(0,typoTable.length-1));
  typoSelected = createFont(typoTable[typoSelect], sizeOfText);
  
  sentence = "";
  int childSelected;
  String[] sentencePart = {"subject","verb","complement"};
  
  for(int i=0; i<sentencePart.length; i++){
    childSelected = int(random(0, childNumber));
    sentence += childXml[childSelected].getChildren(sentencePart[i])[0].getContent();
    if(i != 2){
      sentence += " ";
    }
  }
  
  println(sentence);
  background(255,255,255);
  fill(0,0,0);
 
  textResize();
}

void textResize(){
  textFont(typoSelected);
  textSize(sizeOfText);
  textAlign(CENTER, CENTER);
   if(textWidth(sentence) >= textMaxWidth && sizeOfText > 1){
    sizeOfText -= 0.5;
    textResize();
  }else{
    println("sizeOfText: " + sizeOfText + " / " + "textWidth: " + textWidth(sentence) + "/" + textMaxWidth);
    rectMode(CENTER);
    text(sentence, width/2, height/2 - sizeOfText/2);
  }
}


void keyPressed(){
  if(keyCode == ENTER){
    makeSentence();
  }
}

Laisser un commentaire