Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
Last active August 29, 2015 14:09
Show Gist options
  • Save nataliefreed/871cd281839a2408f008 to your computer and use it in GitHub Desktop.
Save nataliefreed/871cd281839a2408f008 to your computer and use it in GitHub Desktop.
Code generator for Processing classes
/*
Hi there!! You should probably not read this code for learning because it is not a good example
in a variety of ways! It was written in a hurry. Also Processing/Java, while being
great for many reasons, is a really terrible language to write code-that-generates-code in (IMHO).
But! You should /run/ this code for learning. :)
-Natalie Freed, Nov 2013
*/
import controlP5.*;
ControlP5 cp5;
String textValue = "";
String className = "";
Function function1;
int margin = 20;
int line = 2;
PFont font;
ArrayList<Attribute> attributes;
ArrayList<Function> functions;
void setup() {
size(900, 700);
font = createFont("Arial", 20);
cp5 = new ControlP5(this);
attributes = new ArrayList();
functions = new ArrayList();
attributes.add(new Attribute("", ""));
attributes.get(0).addTextField(1, margin, 130, "numPages", "int");
attributes.add(new Attribute("", ""));
attributes.get(1).addTextField(2, margin, 320, "hasPictures", "boolean");
function1 = new Function("","", new ArrayList());
cp5.addTextfield("className")
.setPosition(margin, 20)
.setSize(200, 40)
.setFont(font)
.setValue("Book")
.setLabel("Class Name (eg. Book, Bicycle, TextField)")
.getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setFont(createFont("Arial", 16)).toUpperCase(false).setColor(0)
;
cp5.addTextfield("functionName")
.setPosition(margin, 520)
.setSize(200, 40)
.setFont(font)
.setValue("getText")
.setLabel("Function Name (eg. turnPage, markPage, search)")
.getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setFont(createFont("Arial", 16)).toUpperCase(false).setColor(0)
;
cp5.addTextfield("functionReturnType")
.setPosition(margin, 590)
.setSize(200, 40)
.setFont(font)
.setValue("String")
.setLabel("Function Return Type (eg. void, int, float, boolean, String)")
.getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setFont(createFont("Arial", 16)).toUpperCase(false).setColor(0)
;
textFont(font);
}
void draw() {
background(100);
fill(200);
noStroke();
line = 2;
rect(width/2+5, 0, width/2, height);
fill(0);
className = cp5.get(Textfield.class, "className").getText();
if (className.length() > 0)
{
className = Character.toUpperCase(className.charAt(0))+className.substring(1);
}
text("class " + className, width/2+margin, margin*line++);
text("{", width/2+margin, margin*line++);
attributes.get(0).name = cp5.get(Textfield.class, "attribute1Name").getText();
attributes.get(0).dataType = cp5.get(Textfield.class, "attribute1DataType").getText();
attributes.get(1).name = cp5.get(Textfield.class, "attribute2Name").getText();
attributes.get(1).dataType = cp5.get(Textfield.class, "attribute2DataType").getText();
text(attributes.get(0).toString() + ";", width/2+margin*2, margin*line++);
text(attributes.get(1).toString() + ";", width/2+margin*2, margin*line++);
line++;
text(className + "(" + attributes.get(0).toString() + ", " + attributes.get(1).toString() + ")", width/2+margin*2, margin*line++);
text("{", width/2+margin*2, margin*line++);
text("this." + attributes.get(0).name + " = " + attributes.get(0).name + ";", width/2+margin*3, margin*line++);
text("this." + attributes.get(1).name + " = " + attributes.get(1).name + ";", width/2+margin*3, margin*line++);
text("}", width/2+margin*2, margin*line++);
line++;
function1.name = cp5.get(Textfield.class, "functionName").getText();
function1.returnType = cp5.get(Textfield.class, "functionReturnType").getText();
text(function1.toString(), width/2+margin*2, margin*line++);
text("{", width/2+margin*2, margin*line++);
text("//Code for " + function1.name + "-ing goes here", width/2+margin*3, margin*line++);
text("}", width/2+margin*2, margin*line++);
text("}", width/2+margin, margin*line++);
}
class Attribute
{
String name;
String dataType;
Attribute(String name, String dataType)
{
this.name = name;
this.dataType = dataType;
}
void addTextField(int number, int x, int y, String value, String dataType)
{
cp5.addTextfield("attribute" + number + "Name")
.setPosition(x, y)
.setSize(200, 40)
.setFont(font)
.setValue(value)
.setLabel("Attribute " + number + " Name (eg. numPages, title, author)")
.getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setFont(createFont("Arial", 16)).toUpperCase(false).setColor(0)
;
cp5.addTextfield("attribute" + number + "DataType")
.setPosition(margin, y+70)
.setSize(200, 40)
.setFont(font)
.setValue(dataType)
.setLabel("Attribute " + number + " Data Type (eg. int, float, boolean, String, PVector)")
.getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setFont(createFont("Arial", 16)).toUpperCase(false).setColor(0)
;
}
String toString()
{
String asString = dataType + " " + name;
return asString;
}
}
class Function
{
String name;
String returnType;
ArrayList<Attribute> parameters;
Function(String name, String returnType, ArrayList<Attribute> parameters)
{
this.name = name;
this.returnType = returnType;
this.parameters = parameters;
}
String toString()
{
String asString = returnType + " " + name + "(";
if (parameters.size() > 0)
{
for (int i=0; i<parameters.size ()-1; i++)
{
asString += parameters.get(i) + ", ";
}
asString += parameters.get(parameters.size()-1) + ")"; //no comma for last one
}
else asString += ")";
// asString += "\n{\n\n}";
return asString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment