Conflicting commands with mousePressed
in
Programming Questions
•
1 years ago
I created a box that can be dragged anywhere on the canvas when the mouse is on it and is pressed and dragged. It is an object called "artworks" which extends this movability from the functions of the abstract Interactivve Boxes class. When I press F1, I can flip through an array of images in that box, one at a time.
I also created a selection tool with rectangular and/or elliptical marquee. From draw, I activate it by pressing the CONTROL KEY and mousePressed.
My problem is that when I try to use the selection tool over the images, the drag function activates at the same time as the selection tool.
Is there a way I can tell the program to NOT drag (i.e. move) the box when the CONTROL KEY is pressed (and therefore, when I am using the selection tool)? (In other words, when I use the selection tool, the drag function cannot be used).
I would also like to copy-paste from the "artworks" with my selection tool. Is the best strategy to do this to use the loadPixels() and updatePixels()? I am aware that a circular selection will probably not work.
int toggle;
PictureGallery artWorks;
RectSelectionTool selectRectangle;
EllipseSelectionTool selectEllipse;
void setup() {
size (screen.width, screen.height);
background (225, 225, 255);
toggle=0;
artWorks=new PictureGallery (50, 140, 330, 360, color (100));
selectRectangle = new RectSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
selectEllipse=new EllipseSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
}
void draw() {
background (225, 225, 255);
artWorks.rollOver();
artWorks.display();
artWorks.getIconX();
artWorks.getIconY();
if (key == CODED) {
if (keyCode == KeyEvent.VK_CONTROL && toggle==1) {
selectEllipse.select();
}
else if (keyCode == KeyEvent.VK_CONTROL && toggle==2) {
selectRectangle.select();
}
}
selectRectangle.display();
selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
artWorks.press(mouseX, mouseY);
selectEllipse.setToggle();
selectRectangle.setToggle();
if (toggle==1) {
selectEllipse.click (mouseX, mouseY);
}
else if (toggle==2) {
selectRectangle.click (mouseX, mouseY);
}
selectRectangle.rollOver();
selectEllipse.rollOver();
}
void mouseDragged() {
artWorks.drag (mouseX, mouseY);
}
void mouseReleased () {
artWorks.release();
if (toggle==1) {
selectEllipse.release();
}
else if (toggle==2) {
selectRectangle.release ();
}
}
void keyPressed () {
artWorks.flipThrough();
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
color boxClr;
PFont fontA; // declare a variable that refers to the default font for all the subclasses
int fontHeight;
int marginSpacing;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight, color tempboxClr) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
boxClr=tempboxClr;
fontA = loadFont("ArialMT-48.vlw"); // Load the default font that the subclasses will be able to use
fontHeight=48; // set a default value for the font height (determined by the font size limit)
marginSpacing=5;
}
// 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 void display();
}
class PictureGallery extends InteractiveBoxes {
PImage[] images=new PImage[10];
int imageIndex;
// constructor of the Picture Gallery class
PictureGallery (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
imageIndex=0;
for (int i = 0; i < images.length; i ++ ) {
images[i] = loadImage( "picture" + i + ".jpg" );
}
} // end of the constructor
// methods of the PictureGallery class
int getIconX () {
return boxPosX;
}
int getIconY () {
return (boxPosY+boxPosH);
}
PImage getImage () {
return images[imageIndex];
}
void flipThrough() {
if (key == CODED) {
if (keyCode == KeyEvent.VK_F1)
{
imageIndex++;
if (imageIndex>images.length-1)
{
imageIndex=0;
}
}
}
}
void display () {
try {
for (int i = 0; i < images.length; i ++ ) {
strokeWeight(4);
strokeJoin(ROUND);
fill (boxClr);
rect (boxPosX-2, boxPosY, boxPosW+4, boxPosH+100);
fill(240); // Set the fill for text instructions
fontHeight=16;
textFont(fontA, fontHeight); // Set the font type and size at Arial 16 for text instructions
text("Drag this box where you please. Press F1 key \nto flip through images in this picture gallery.", boxPosX+marginSpacing, ((boxPosY+boxPosH)+fontHeight+marginSpacing));
text("Click on selection tool and CTRL \nkey to select. Press P to paste.", boxPosX+marginSpacing+100, ((boxPosY+boxPosH)+3*fontHeight+4*marginSpacing));
rect (boxPosX-2, boxPosY-2, boxPosW+4, boxPosH+4);
image (images[imageIndex], boxPosX, boxPosY, boxPosW, boxPosH);
}
}
catch (ArrayIndexOutOfBoundsException e) {
println ("Hey, that’s not a valid index!");
}
catch (NullPointerException e) {
println( "I can't find this array! ");
}
} // end of the display function
} // end of the Picture Gallery class
abstract class SelectionTool extends InteractiveBoxes {
PImage icon;
int iconXPos, iconYPos, iconW, iconH;
int SelectX, SelectY, SelectW, SelectH;
// constructor of the abstract SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int tempIconW, int tempIconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
iconW=tempIconW;
iconH=tempIconH;
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
} // end of the constructor
// methods of the abstract SelectionTool class
boolean rollOver (boolean over) {
if (mousePressed && (mouseX>=iconXPos) && (mouseX <=iconXPos+iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
over=true;
}
else {
over=false;
}
return over;
} // end of the rollOver function
void click (int mX, int mY) {
if (over==true) {
}
else {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
}
} // end the click function
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
abstract void select ();
abstract void display ();
} // end of the SelectionTool class
class RectSelectionTool extends SelectionTool {
// constructor of the RectSelectionTool class
RectSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage("rectangularmarquee.jpg");
} // end of the constructor
// methods of the RectSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
if (mousePressed) {
rect (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
rect (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
void setToggle () {
if (mousePressed && (mouseX>=iconXPos+iconW) && (mouseX <=iconXPos+2*iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
toggle=2;
println("toggle2");
}
}
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
fill(0, 0, 0, 0);
stroke(boxClr);
rect (SelectX, SelectY, SelectW, SelectH);
stroke(0);
} // end of release function
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos+iconW, iconYPos);
} // end of the display function
} // end of the RectSelectionTool class
class EllipseSelectionTool extends SelectionTool {
// constructor of the EllipseSelectionTool class
EllipseSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage ("ellipticalmarquee.jpg");
} // end of the constructor
// methods of the EllipseSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
ellipseMode(CORNER);
if (mousePressed) {
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
ellipse (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
void setToggle () {
if (mousePressed && (mouseX>=iconXPos) && (mouseX <=iconXPos+iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
toggle=1;
println("toggle1");
}
}
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
fill(0, 0, 0, 0);
stroke(boxClr);
ellipseMode(CORNER);
ellipse (SelectX, SelectY, SelectW, SelectH);
stroke(0);
} // end of release function
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos, iconYPos);
} // end of the display function
} // end of the EllipseSelectionTool class
I also created a selection tool with rectangular and/or elliptical marquee. From draw, I activate it by pressing the CONTROL KEY and mousePressed.
My problem is that when I try to use the selection tool over the images, the drag function activates at the same time as the selection tool.
Is there a way I can tell the program to NOT drag (i.e. move) the box when the CONTROL KEY is pressed (and therefore, when I am using the selection tool)? (In other words, when I use the selection tool, the drag function cannot be used).
I would also like to copy-paste from the "artworks" with my selection tool. Is the best strategy to do this to use the loadPixels() and updatePixels()? I am aware that a circular selection will probably not work.
int toggle;
PictureGallery artWorks;
RectSelectionTool selectRectangle;
EllipseSelectionTool selectEllipse;
void setup() {
size (screen.width, screen.height);
background (225, 225, 255);
toggle=0;
artWorks=new PictureGallery (50, 140, 330, 360, color (100));
selectRectangle = new RectSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
selectEllipse=new EllipseSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
}
void draw() {
background (225, 225, 255);
artWorks.rollOver();
artWorks.display();
artWorks.getIconX();
artWorks.getIconY();
if (key == CODED) {
if (keyCode == KeyEvent.VK_CONTROL && toggle==1) {
selectEllipse.select();
}
else if (keyCode == KeyEvent.VK_CONTROL && toggle==2) {
selectRectangle.select();
}
}
selectRectangle.display();
selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
artWorks.press(mouseX, mouseY);
selectEllipse.setToggle();
selectRectangle.setToggle();
if (toggle==1) {
selectEllipse.click (mouseX, mouseY);
}
else if (toggle==2) {
selectRectangle.click (mouseX, mouseY);
}
selectRectangle.rollOver();
selectEllipse.rollOver();
}
void mouseDragged() {
artWorks.drag (mouseX, mouseY);
}
void mouseReleased () {
artWorks.release();
if (toggle==1) {
selectEllipse.release();
}
else if (toggle==2) {
selectRectangle.release ();
}
}
void keyPressed () {
artWorks.flipThrough();
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
color boxClr;
PFont fontA; // declare a variable that refers to the default font for all the subclasses
int fontHeight;
int marginSpacing;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight, color tempboxClr) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
boxClr=tempboxClr;
fontA = loadFont("ArialMT-48.vlw"); // Load the default font that the subclasses will be able to use
fontHeight=48; // set a default value for the font height (determined by the font size limit)
marginSpacing=5;
}
// 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 void display();
}
class PictureGallery extends InteractiveBoxes {
PImage[] images=new PImage[10];
int imageIndex;
// constructor of the Picture Gallery class
PictureGallery (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
imageIndex=0;
for (int i = 0; i < images.length; i ++ ) {
images[i] = loadImage( "picture" + i + ".jpg" );
}
} // end of the constructor
// methods of the PictureGallery class
int getIconX () {
return boxPosX;
}
int getIconY () {
return (boxPosY+boxPosH);
}
PImage getImage () {
return images[imageIndex];
}
void flipThrough() {
if (key == CODED) {
if (keyCode == KeyEvent.VK_F1)
{
imageIndex++;
if (imageIndex>images.length-1)
{
imageIndex=0;
}
}
}
}
void display () {
try {
for (int i = 0; i < images.length; i ++ ) {
strokeWeight(4);
strokeJoin(ROUND);
fill (boxClr);
rect (boxPosX-2, boxPosY, boxPosW+4, boxPosH+100);
fill(240); // Set the fill for text instructions
fontHeight=16;
textFont(fontA, fontHeight); // Set the font type and size at Arial 16 for text instructions
text("Drag this box where you please. Press F1 key \nto flip through images in this picture gallery.", boxPosX+marginSpacing, ((boxPosY+boxPosH)+fontHeight+marginSpacing));
text("Click on selection tool and CTRL \nkey to select. Press P to paste.", boxPosX+marginSpacing+100, ((boxPosY+boxPosH)+3*fontHeight+4*marginSpacing));
rect (boxPosX-2, boxPosY-2, boxPosW+4, boxPosH+4);
image (images[imageIndex], boxPosX, boxPosY, boxPosW, boxPosH);
}
}
catch (ArrayIndexOutOfBoundsException e) {
println ("Hey, that’s not a valid index!");
}
catch (NullPointerException e) {
println( "I can't find this array! ");
}
} // end of the display function
} // end of the Picture Gallery class
abstract class SelectionTool extends InteractiveBoxes {
PImage icon;
int iconXPos, iconYPos, iconW, iconH;
int SelectX, SelectY, SelectW, SelectH;
// constructor of the abstract SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int tempIconW, int tempIconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
iconW=tempIconW;
iconH=tempIconH;
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
} // end of the constructor
// methods of the abstract SelectionTool class
boolean rollOver (boolean over) {
if (mousePressed && (mouseX>=iconXPos) && (mouseX <=iconXPos+iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
over=true;
}
else {
over=false;
}
return over;
} // end of the rollOver function
void click (int mX, int mY) {
if (over==true) {
}
else {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
}
} // end the click function
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
abstract void select ();
abstract void display ();
} // end of the SelectionTool class
class RectSelectionTool extends SelectionTool {
// constructor of the RectSelectionTool class
RectSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage("rectangularmarquee.jpg");
} // end of the constructor
// methods of the RectSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
if (mousePressed) {
rect (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
rect (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
void setToggle () {
if (mousePressed && (mouseX>=iconXPos+iconW) && (mouseX <=iconXPos+2*iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
toggle=2;
println("toggle2");
}
}
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
fill(0, 0, 0, 0);
stroke(boxClr);
rect (SelectX, SelectY, SelectW, SelectH);
stroke(0);
} // end of release function
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos+iconW, iconYPos);
} // end of the display function
} // end of the RectSelectionTool class
class EllipseSelectionTool extends SelectionTool {
// constructor of the EllipseSelectionTool class
EllipseSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage ("ellipticalmarquee.jpg");
} // end of the constructor
// methods of the EllipseSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
ellipseMode(CORNER);
if (mousePressed) {
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
ellipse (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
void setToggle () {
if (mousePressed && (mouseX>=iconXPos) && (mouseX <=iconXPos+iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
toggle=1;
println("toggle1");
}
}
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
fill(0, 0, 0, 0);
stroke(boxClr);
ellipseMode(CORNER);
ellipse (SelectX, SelectY, SelectW, SelectH);
stroke(0);
} // end of release function
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos, iconYPos);
} // end of the display function
} // end of the EllipseSelectionTool class
1