Problem with toggling a selection tool
in
Programming Questions
•
1 years ago
I am trying to toggle a selection tool from rectangular marquee to elliptical marquee by clicking on their respective small icon BEFORE I begin my selection. Both these selection tools are classes inherited from an abstract class, itself inherited from an abstract class.
My strategy has been to try to use an "activateSelectionTool" function in the middle class called SelectionTool but I cannot seem to make the toggle this way.
What would be an intelligence way of approaching this problem?
Here is my code:
RectSelectionTool selectRectangle;
EllipseSelectionTool selectEllipse;
void setup() {
size (screen.width, screen.height);
background (225, 225, 255);
selectRectangle = new RectSelectionTool (0, 0, 0, 0, 0, 0);
selectEllipse=new EllipseSelectionTool (0, 0, 0, 0, 0, 50);
}
void draw() {
background (225, 225, 255);
selectRectangle.rollOver();
selectRectangle.activateSelectionTool ();
selectRectangle.select();
selectRectangle.display();
selectEllipse.rollOver();
selectEllipse.activateSelectionTool ();
selectEllipse.select();
selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
selectRectangle.click (mouseX, mouseY);
selectEllipse.click (mouseX, mouseY);
}
void mouseReleased () {
selectRectangle.release ();
selectEllipse.release();
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
}
// methods of the abstract InteractiveBoxes class
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
} // end the click function
void press (int mX, int mY) {
if (over==true) {
pressed=true;
boxPosXOff=mX-boxPosX;
boxPosYOff=mY-boxPosY;
}
} // end of the press function
void drag (int mX, int mY) {
if (pressed==true) {
boxPosX=mX-boxPosXOff;
boxPosY=mY-boxPosYOff;
}
} // end of the drag function
void release () {
pressed=false;
} // end of release function
}
abstract class SelectionTool extends InteractiveBoxes {
PImage icon;
int iconXPos, iconYPos;
int SelectX, SelectY, SelectW, SelectH;
boolean activate = false;
// constructor of the abstract SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, int tempIconXPos, int tempIconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
iconXPos=tempIconXPos;
iconYPos=tempIconYPos;
} // end of the constructor
// methods of the abstract SelectionTool class
void rollOver () {
if ((mouseX>=iconXPos) && (mouseX <=iconXPos+50) && (mouseY>=iconYPos) && (mouseY<=iconYPos+50))
{
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void activateSelectionTool () {
if (over==true && pressed==true) {
activate=true;
}
}
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
abstract void select ();
void display () {
image (icon, iconXPos, iconYPos);
} // end of the display function
} // end of the SelectionTool class
class RectSelectionTool extends SelectionTool {
// constructor of the RectSelectionTool class
RectSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, int iconXPos, int iconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH, iconXPos, iconYPos);
icon=loadImage("rectangularmarquee.jpg");
} // end of the constructor
// methods of the RectSelectionTool class
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
ellipseMode(CORNER);
if (activate==true && mousePressed)
{
rect (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
rect (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
} // end of the RectSelectionTool class
class EllipseSelectionTool extends SelectionTool {
// constructor of the EllipseSelectionTool class
EllipseSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, int iconXPos, int iconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH, iconXPos, iconYPos);
icon=loadImage ("ellipticalmarquee.jpg");
} // end of the constructor
// methods of the EllipseSelectionTool class
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
if (activate==true && mousePressed) {
ellipseMode(CORNER);
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
ellipseMode(CORNER);
ellipse (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
} // end of the EllipseSelectionTool class
My strategy has been to try to use an "activateSelectionTool" function in the middle class called SelectionTool but I cannot seem to make the toggle this way.
What would be an intelligence way of approaching this problem?
Here is my code:
RectSelectionTool selectRectangle;
EllipseSelectionTool selectEllipse;
void setup() {
size (screen.width, screen.height);
background (225, 225, 255);
selectRectangle = new RectSelectionTool (0, 0, 0, 0, 0, 0);
selectEllipse=new EllipseSelectionTool (0, 0, 0, 0, 0, 50);
}
void draw() {
background (225, 225, 255);
selectRectangle.rollOver();
selectRectangle.activateSelectionTool ();
selectRectangle.select();
selectRectangle.display();
selectEllipse.rollOver();
selectEllipse.activateSelectionTool ();
selectEllipse.select();
selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
selectRectangle.click (mouseX, mouseY);
selectEllipse.click (mouseX, mouseY);
}
void mouseReleased () {
selectRectangle.release ();
selectEllipse.release();
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
}
// methods of the abstract InteractiveBoxes class
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
} // end the click function
void press (int mX, int mY) {
if (over==true) {
pressed=true;
boxPosXOff=mX-boxPosX;
boxPosYOff=mY-boxPosY;
}
} // end of the press function
void drag (int mX, int mY) {
if (pressed==true) {
boxPosX=mX-boxPosXOff;
boxPosY=mY-boxPosYOff;
}
} // end of the drag function
void release () {
pressed=false;
} // end of release function
}
abstract class SelectionTool extends InteractiveBoxes {
PImage icon;
int iconXPos, iconYPos;
int SelectX, SelectY, SelectW, SelectH;
boolean activate = false;
// constructor of the abstract SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, int tempIconXPos, int tempIconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
iconXPos=tempIconXPos;
iconYPos=tempIconYPos;
} // end of the constructor
// methods of the abstract SelectionTool class
void rollOver () {
if ((mouseX>=iconXPos) && (mouseX <=iconXPos+50) && (mouseY>=iconYPos) && (mouseY<=iconYPos+50))
{
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void activateSelectionTool () {
if (over==true && pressed==true) {
activate=true;
}
}
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
abstract void select ();
void display () {
image (icon, iconXPos, iconYPos);
} // end of the display function
} // end of the SelectionTool class
class RectSelectionTool extends SelectionTool {
// constructor of the RectSelectionTool class
RectSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, int iconXPos, int iconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH, iconXPos, iconYPos);
icon=loadImage("rectangularmarquee.jpg");
} // end of the constructor
// methods of the RectSelectionTool class
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
ellipseMode(CORNER);
if (activate==true && mousePressed)
{
rect (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
rect (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
} // end of the RectSelectionTool class
class EllipseSelectionTool extends SelectionTool {
// constructor of the EllipseSelectionTool class
EllipseSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, int iconXPos, int iconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH, iconXPos, iconYPos);
icon=loadImage ("ellipticalmarquee.jpg");
} // end of the constructor
// methods of the EllipseSelectionTool class
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
if (activate==true && mousePressed) {
ellipseMode(CORNER);
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
ellipseMode(CORNER);
ellipse (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
} // end of the EllipseSelectionTool class
1