Code Part 2 : The editor classes.
Code:
public class RectangleEditor {
PApplet app;
GPanel pnlEditor;
GLabel lblWidth, lblHeight;
GHorzSlider sdrWidth, sdrHeight;
// Create the circle editor (only one allowed)
public RectangleEditor(PApplet theApplet){
app = theApplet;
if(rectangleEditor == null){
// Create the panel
pnlEditor = new GPanel(app, "Rectangle", 20,72, 180,26);
pnlEditor.setVisible(false);
pnlEditor.setCollapsed(false);
pnlEditor.setOpaque(true);
pnlEditor.addEventHandler(this, "hidePanel");
pnlEditor.setBorder(1);
pnlEditor.setAlpha(160);
// Create the controls for editing circles
lblWidth = new GLabel(app,"Width",2,2,50,10);
sdrWidth = new GHorzSlider(app,50,3,120,10);
sdrWidth.setLimits(MIN_WIDTH, MIN_WIDTH, MAX_WIDTH);
sdrWidth.setBorder(1);
sdrWidth.addEventHandler(this, "handleSliders");
lblHeight = new GLabel(app,"Height",2,12,50,10);
sdrHeight = new GHorzSlider(app,50,13,120,10);
sdrHeight.setLimits(MIN_WIDTH, MIN_WIDTH, MAX_WIDTH);
sdrHeight.setBorder(1);
sdrHeight.addEventHandler(this, "handleSliders");
// Add the controls to the panel
pnlEditor.add(lblWidth);
pnlEditor.add(sdrWidth);
pnlEditor.add(lblHeight);
pnlEditor.add(sdrHeight);
}
}
// Initialise the editor based on the selected shapes properties
public void initForRectangle(Rectangle r){
mouseEnabled = false;
sdrWidth.setValue(r.width, true);
sdrHeight.setValue(r.height, true);
pnlEditor.setXY(r.cx, r.cy);
pnlEditor.setCollapsed(false);
pnlEditor.setVisible(true);
}
// The user clicks on the title bar when finished editing
public void hidePanel(GPanel panel){
pnlEditor.setVisible(false);
pnlEditor.setFocus(false);
sdrWidth.setFocus(false);
sdrWidth.setFocus(false);
shapeToEdit = null;
mouseEnabled = true;
}
// Handle the slider control value changes
public void handleSliders(GSlider slider){
if(slider == sdrWidth)
((Rectangle)shapeToEdit).width = sdrWidth.getValue();
if(slider == sdrHeight)
((Rectangle)shapeToEdit).height = sdrHeight.getValue();
}
}
public class CircleEditor {
PApplet app;
GPanel pnlEditor;
GLabel lblRadius;
GHorzSlider sdrRadius;
// Create the circle editor (only one allowed)
public CircleEditor(PApplet theApplet){
app = theApplet;
if(circleEditor == null){
// Create the panel
pnlEditor = new GPanel(app, "Circle", 20,60, 180,16);
pnlEditor.setVisible(false);
pnlEditor.setCollapsed(false);
pnlEditor.setOpaque(true);
pnlEditor.addEventHandler(this, "hidePanel");
pnlEditor.setBorder(1);
pnlEditor.setAlpha(160);
// Create the controls for editing circles
lblRadius = new GLabel(app,"Radius",2,2,50,10);
sdrRadius = new GHorzSlider(app,50,3,120,10);
sdrRadius.setLimits(MIN_RADIUS, MIN_RADIUS, MAX_RADIUS);
sdrRadius.setBorder(1);
sdrRadius.addEventHandler(this, "handleSliders");
// Add the controls to the panel
pnlEditor.add(lblRadius);
pnlEditor.add(sdrRadius);
}
}
// Initialise the editor based on the selected shapes properties
public void initForCircle(Circle c){
mouseEnabled = false;
sdrRadius.setValue(c.radius, true);
pnlEditor.setXY(c.cx, c.cy);
pnlEditor.setCollapsed(false);
pnlEditor.setVisible(true);
}
// The user clicks on the title bar when finished editing
public void hidePanel(GPanel panel){
pnlEditor.setVisible(false);
pnlEditor.setFocus(false);
sdrRadius.setFocus(false);
shapeToEdit = null;
mouseEnabled = true;
}
// Handle the slider control value changes
public void handleSliders(GSlider slider){
if(shapeToEdit != null && slider == sdrRadius){
((Circle)shapeToEdit).radius = sdrRadius.getValue();
}
}
}