selectOutput feature

Dear all,
I developed a very simple sketch in which I use the selectOutput function.
It works but, as soon as the select output file window is open, the sketch graphic disappear and the desktop is shown.
Did you have any similar problem ? How did you fix it ?
Thank you very much for your help and cooperation
regards

you can download the sketch from here

Please post your code in the form of a MCVE.

More info here:

Dear all,
I developed a very simple sketch in which I use the selectOutput function.
It works but, as soon as the select output file window is open, the sketch graphic disappear and the desktop is shown.
Did you have any similar problem ? How did you fix it ?
Thank you very much for your help and cooperation
regards

This is the source code of TestFile.pde

PImage myImage;
cButton myButton;


void setup()
{
  fullScreen(P2D, 2);
  myImage=loadImage("img.jpg");
  myButton=new cButton("","button",(displayWidth-myImage.width)/2,(displayHeight-myImage.height)/2,100);
}

void draw()
{
   image(myImage,(displayWidth-myImage.width)/2,(displayHeight-myImage.height)/2);
   myButton.drawBottone();
   int x = myButton.testMouse();
   if(x>=0) {
     selectOutput("Select a file to write to:", "fileSelected");
   }
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
  }
}

This is the source code of cButton class buttonClass.pde file:


    final static String RELEASED
            = "Released";
    final static String OVER
            = "Over";
    final static String PRESSED
            = "Pressed";

    class cButton {

        PImage released;
        PImage over;
        PImage pressed;
        int status;
        int left;
        int right;
        int top;
        int bottom;
        int x;
        int y;
        int mouseStatus;
        float res;
        int sx;
        int sy;

        cButton(String path, String name, int lx, int ly, int resize) {
            released = loadImage(path + name + RELEASED + ".png");
            over = loadImage(path + name + OVER + ".png");
            pressed = loadImage(path + name + PRESSED + ".png");
            status = 0;
            x = lx;
            y = ly;
            res = (float) resize;
            sx = (int) ((float) released.width * (res / 100));
            sy = (int) ((float) released.height * (res / 100));
            right = x + sx;
            bottom = y + sy;
            top = y;
            left = x;

            mouseStatus = 0;
        }

        int mouse() {
            int x = testMouse();
            drawBottone();
            return x;
        }

        public void drawBottone() {
            switch (status) {
                case 0: // released
                    image(released, x, y, sx, sy);
                    break;
                case 1: // over
                    image(over, x, y, sx, sy);
                    break;
                case 2: // pressed
                    image(pressed, x, y, sx, sy);
                    break;
            }
        }

        public void drawBottone(int x, int y) {
            switch (status) {
                case 0: // released
                    image(released, x, y, sx, sy);
                    break;
                case 1: // over
                    image(over, x, y, sx, sy);
                    break;
                case 2: // pressed
                    image(pressed, x, y, sx, sy);
                    break;
            }
        }

        public int testMouse() {
            int flg;
            int ret;
            flg = 0;
            ret = -1;
            // println("X: "+mouseX+" Y: "+mouseY);
            if (mouseX >= left && mouseX <= right) {
                if (mouseY >= top && mouseY <= bottom) {
                    if (mousePressed == true && mouseButton == LEFT) {
                        flg = 2;
                         ret = 1;

                    } else {
                        flg = 1;
                    }
                }
            }
            
            status = flg;
            if(ret==1) {
              if( mouseStatus<0) {
                mouseStatus=ret;
              } else
                ret=-1;
            }else
              mouseStatus=ret;
            // println(ret);
            return ret;
        }
    }

You can simplify your example even further. Try to get rid of images and classes. Like this:

void setup() {
  size(400, 400);
}

void draw() {

  if (mouseX >= width/2 && mousePressed) {
    selectOutput("Select a file to write to:", "fileSelected");
  }
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
  }
}

This code is roughly equivalent to yours, without any extra code or dependencies.

The problem with this code is that you’re calling selectInput() from inside the draw() function. This is causing 60 dialogs per second to display, which is breaking your sketch.

To fix this, you need to prevent multiple dialogs from popping up: you could add a boolean variable that tracks this, or you could move your logic into the mousePressed() function.

Dear Kevin,
thank you for your suggestion. I modified the code:

int status;

void setup() {
  size(400, 400);
  status=0;
}

void draw() {

  if (mouseX >= width/2 && mousePressed) {
    if(status==0) {
      status=1;
      selectOutput("Select a file to write to:", "fileSelected");
    }
  }
}

void fileSelected(File selection) {
  status=0;
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
  }
}

The status variable is set before open the file selection window.
It works but the system varaible mousePressed remains always true…
How to reset it ?
Had you a similar problem ? How did you fix it ?
Thank you very much for your help and cooperation.
regards

like @Kevin suggested, could move it to mousePressed

//_________________________________________________________________ 
void setup() {
  size(100, 100);
}

//_________________________________________________________________ 
void draw_menu() {
  fill(255);
  rect(50, 80, 49, 19);
  fill(0);
  textAlign(CENTER, BOTTOM);
  text("File", 75, 97);
}

//_________________________________________________________________ 
void draw() {
  background(200, 200, 0);
  draw_menu();
}

//_________________________________________________________________ 
void mousePressed() {
  if ( overRect(50, 80, 50, 20) ) selectOutput("Select a file to write to:", "fileSelected");
}

//_________________________________________________________________ 
void fileSelected(File selection) {
  if (selection == null)     println("Window was closed or the user hit cancel.");
  else                       println("User selected " + selection.getAbsolutePath());
}

//_________________________________________________________________ mouse position over rectangle yes/no
boolean overRect(int x, int y, int width, int height) {
  if (mouseX >= x && mouseX <= x+width &&  mouseY >= y && mouseY <= y+height)  return true;
  else                                                                         return false;
}


Ok. I understand. Thank you.