xxxxxxxxxx
110
let cfree;
// For loading text
let lines;
// An array of buttons
let cols = 7;
let rows = 3;
var buttons = [];//new Array(cols * rows);
// Preload the grammar
function preload() {
//gold image backgorund preload
calendarimg = loadImage("background.jpg");
lines = loadStrings("cfg_test.txt");
}
function setup() {
textAlign(CENTER, CENTER);
//create canvas
let canv = createCanvas(1000, 600);
canv.parent("canvhold");
// A loop to evenly space out the buttons along the window
//for (var i = 0; i < buttons.length; i++) {
buttons.push(new Button(700, 200, 50, 40));
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
buttons.push(new Button(x * 115 + 128, y * 50 + 280, 50, 40));
}
}
cfree = new ContextFree();
// A button to generate a new sentence
//let button = select('#generate');
// button.mousePressed(generate);
//A button to clear everything
// let clearButton = select('#clearButton'); clearButton.position(500,20);
// clearButton.mousePressed(clearAll);
// Here, regex parses the grammar files and adds rules
// Look through all the rules
for (let i = 0; i < lines.length; i++) {
// A regex to get rid of comments
let line = lines[i].replace(/#.*$/, "").trim(); // Get rid if any comments
// Match the rule syntax
let match = /(\w+)\s*->\s*(.*)/.exec(line);
// If we found a fule
if (match) {
// The rule is the first group
let rule = match[1];
// The expansions can found in group 2
let expansions = match[2].split(/\s*\|\s*/);
// For every expansion
for (var j = 0; j < expansions.length; j++) {
// Make the expansion an array and add the rule
let tokens = expansions[j].split(/\s+/);
cfree.addRule(rule, tokens);
}
}
}
}
function draw() {
image(calendarimg, 0, 0, 1000, 600);
// Show all the buttons
for (var i = 0; i < buttons.length; i++) {
buttons[i].display();
}
}
// Remove everything
function clearAll() {
let elements = selectAll(".text");
for (var i = 0; i < elements.length; i++) {
elements[i].remove();
}
}
function generate() {
// Get an expansion starting with 'S'
let expansion = cfree.getExpansion("S");
let par1 = createP("Today's Kindness:");
par1.class("text");
par1.size(200, 50);
par1.position(200, 150);
// Make a paragraph
let par2 = createP(expansion);
par2.class("text");
par2.size(200, 50);
par2.position(200, 200);
}
function mousePressed() {
// When the mouse is pressed, we must check every single button
for (var i = 0; i < buttons.length; i++) {
buttons[i].click(mouseX, mouseY);
}
}