G4P with PeasyCam not working on Mac

edited December 2016 in Library Questions

PeasyCam works only intermittently when I create a GWindow. The G4P with PeasyCam example does not work for me either.
Just calling GWindow.getWindow causes the problem.

Tagged:

Answers

  • I have just tried the G4P_with_PeasyCam example and it works for me on my iMac. Note that in this example you must collapse the panel by clicking on the title bar and then you can drag the cube round. When the panel is open you have to use the sliders to rotate the cube.

    You mention creating a GWindow, are you trying to use PeasyCam in this second window?

    Please post a simple example that demonstrates the problem together with any error messages otherwise I am going to have trouble helping you further.

  • edited December 2016

    The Peasycam sample does not open panel when I click title bar and when I default it open nothing works as expected. below is a mashup of the Hello Peasy sample and the GWindow sample. What I get is that moving camera with pressed mouse works only some of the time which is what I see in my app.

    Also, after exiting this app I will get random lines on the screen and within a few minutes my mac will crash.

    import peasy.*;
    import g4p_controls.*;
    
    GWindow[] window;
    GButton btnStart;
    GLabel lblInstr;
    PeasyCam cam;
    
    void setup() {
      size(900,900,P3D);
      cam = new PeasyCam(this, 100);
      cam.setMinimumDistance(50);
      cam.setMaximumDistance(500);
    
        btnStart = new GButton(this, 4, 34, 120, 60, "Create 3 Windows");
      lblInstr = new GLabel(this, 132, 34, 120, 60, "Use the mouse to draw a rectangle in any of the 3 windows");
      lblInstr.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      lblInstr.setVisible(false);
    
    }
    void draw() {
      rotateX(-.5);
      rotateY(-.5);
      background(0);
      fill(255,0,0);
      box(30);
      pushMatrix();
      translate(0,0,20);
      fill(0,0,255);
      box(5);
      popMatrix();
    }
    
    
    public void createWindows() {
      //int[] col = {color(200,0,0), color(0,200,0), color(0,0,200) };
      int col;
      window = new GWindow[3];
      for (int i = 0; i < 3; i++) {
        col = (128 << (i * 8)) | 0xff000000;
        window[i] = GWindow.getWindow(this, "Window "+i, 70+i*220, 160+i*50, 200, 200, JAVA2D);
        window[i].addData(new MyWinData());
        ((MyWinData)window[i].data).col = col;
        window[i].addDrawHandler(this, "windowDraw");
        window[i].addMouseHandler(this, "windowMouse");
      }
    }
    
    /**
     * Click the button to create the windows.
     * @param button
     */
    public void handleButtonEvents(GButton button, GEvent event) {
      if (window == null && event == GEvent.CLICKED) {
        createWindows();
        lblInstr.setVisible(true);
        button.setEnabled(false);
      }
    }
    
    /**
     * Handles mouse events for ALL GWindow objects
     *  
     * @param appc the PApplet object embeded into the frame
     * @param data the data for the GWindow being used
     * @param event the mouse event
     */
    public void windowMouse(PApplet appc, GWinData data, MouseEvent event) {
      MyWinData data2 = (MyWinData)data;
      switch(event.getAction()) {
      case MouseEvent.PRESS:
        data2.sx = data2.ex = appc.mouseX;
        data2.sy = data2.ey = appc.mouseY;
        data2.done = false;
        break;
      case MouseEvent.RELEASE:
        data2.ex = appc.mouseX;
        data2.ey = appc.mouseY;
        data2.done = true;
        break;
      case MouseEvent.DRAG:
        data2.ex = appc.mouseX;
        data2.ey = appc.mouseY;
        break;
      }
    }
    
    /**
     * Handles drawing to the windows PApplet area
     * 
     * @param appc the PApplet object embeded into the frame
     * @param data the data for the GWindow being used
     */
    public void windowDraw(PApplet appc, GWinData data) {
      MyWinData data2 = (MyWinData)data;
      appc.background(data2.col);
      if (!(data2.sx == data2.ex && data2.ey == data2.ey)) {
        appc.stroke(255);
        appc.strokeWeight(2);
        appc.noFill();
        if (data2.done) {
          appc.fill(128);
        }
        appc.rectMode(CORNERS);
        appc.rect(data2.sx, data2.sy, data2.ex, data2.ey);
      }
    }  
    
    /**
     * Simple class that extends GWinData and holds the data 
     * that is specific to a particular window.
     * 
     * @author Peter Lager
     */
    class MyWinData extends GWinData {
      int sx, sy, ex, ey;
      boolean done;
      int col;
    }
    
  • Answer ✓

    The Peasycam sample does not open panel when I click title bar

    That might be my mistake I meant the GPanel title bar not the window title bar.

    I have fixed your code above to make sure it can be read in this forum. You should learn to do this here.

    When using PeasyCam with G4P you should be careful because PeasyCam modifies the transformation matrix when the cam object is first created and this messes up drawing of the G4P controls which is happening in the code above.

    There are two solutions -

    Solution 1:
    Create at least one G4P control before creating the PeasyCam object like this

    void setup() {
      size(900, 900, P3D);
      btnStart = new GButton(this, 4, 34, 120, 60, "Create 3 Windows");
      lblInstr = new GLabel(this, 132, 34, 120, 60, "Use the mouse to draw a rectangle in any of the 3 windows");
      lblInstr.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      lblInstr.setVisible(true);
      lblInstr.setOpaque(true);
      cam = new PeasyCam(this, 200);
      cam.setMinimumDistance(120);
      cam.setMaximumDistance(500);
    }
    

    or
    Solution 2:
    Register that you are using G4P before creating the PeasyCam object like this

    void setup() {
      size(900, 900, P3D);
      G4P.registerSketch(this);
      cam = new PeasyCam(this, 200);
      cam.setMinimumDistance(120);
      cam.setMaximumDistance(500);
      btnStart = new GButton(this, 4, 34, 120, 60, "Create 3 Windows");
      lblInstr = new GLabel(this, 132, 34, 120, 60, "Use the mouse to draw a rectangle in any of the 3 windows");
      lblInstr.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      lblInstr.setVisible(true);
      lblInstr.setOpaque(true);
    }
    

    I have tried both of these solutions with the code you posted above and they worked fine on my Mac.

    After clicking the button it opens the 3 windows which all work fine. When coming back to the main sketch window you must click to make it active then you can drag the box. Pressing the button and dragging does not activate the window so it appears unresponsive.

    Also, after exiting this app I will get random lines on the screen and within a few minutes my mac will crash.

    I have no idea why this happens. I can say that it is NOT caused by G4P or PeasyCam since these might cause Processing a headache but it wouldn't bring down your mac. It maybe something in your Processing or Java installations.

Sign In or Register to comment.