Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to make spheres stay green?

$
0
0

Hello, I'm developing a program in which I have red spheres that when clicked they change their color to green but when you click in another place, they turn back to the original color.

What I need to do is that when they are clicked they stay with the green color instead of turning back again to the original color ir order to make all the spheres look green. This logic hasn't got an order, I mean, any sphere could be pressed first or second, or last... Here is the code

import peasy.*;
import gifAnimation.*;

PeasyCam cam;

Gif myAnimation;

PShape ammonite;
PImage fondo, titulo, titulo2, ammoniteinicio, almeja, gestos, flecha;
String estado;
PFont helvetica, nexa;

// invisible PGraphics
PGraphics pg;

//define hot spots
PVector[] hotSpotsPosition=new PVector[5];
color[] hotSpotsColor =new color [hotSpotsPosition.length];
String[] hotSpotsText = new String [hotSpotsPosition.length];
PImage[] hotSpotsImg = new PImage [hotSpotsPosition.length];
boolean [] hotSpotsRightSide = new boolean [hotSpotsPosition.length];
Gif [] hotSpotsGif = new Gif [hotSpotsPosition.length];
boolean almejacerrada;
int undefined=-1;
int hotSpotFound=undefined; // when undefined no text is displayed

int mousePressedAtX;
int mousePressedAtY;

// button data
int buttonX, buttonY,
  buttonWidth, buttonHeight;

void setup() {

  fullScreen(P3D);
  // size(900, 900, P3D);

  rectMode(CENTER);
  imageMode(CENTER);
  textAlign(CENTER);

  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(110);
  cam.setMaximumDistance(500);

  ammonite = loadShape("ammonite.obj");

  // define the hot spots in parallel arrays
  // the first hot spot is defined in hotSpotsPosition[0] and hotSpotsColor[0] and hotSpotsText[0] and so on
  // positions:
  hotSpotsPosition[0] = new PVector(-15, -20, 0);
  hotSpotsPosition[1] = new PVector(-5, -5, -25);
  hotSpotsPosition[2] = new PVector(-20, -10, 3);
  hotSpotsPosition[3] = new PVector(-25, -10, 15);
  hotSpotsPosition[4] = new PVector(-5, 19, -5);

  //colors: colors are not visible but must be unique
  hotSpotsColor[0] = color(255, 0, 0);
  hotSpotsColor[1] = color(100, 0, 0);
  hotSpotsColor[2] = color(110, 0, 0);
  hotSpotsColor[3] = color(120, 0, 0);
  hotSpotsColor[4] = color(160, 0, 0);

  // texts
  hotSpotsText[0] = "Embudo";
  hotSpotsText[1] = "Caparazon";
  hotSpotsText[2] = "Ojos";
  hotSpotsText[3] = "Tentaculos";
  hotSpotsText[4] = "Sifon";

  //images
  hotSpotsImg[0] = loadImage ("EMBUDO.png");
  hotSpotsImg[1] = loadImage ("CAPARAZON.png");
  hotSpotsImg[2] = loadImage ("OJOS.png");
  hotSpotsImg[3] = loadImage ("TENTACULOS.png");
  hotSpotsImg[4] = loadImage ("SIFON.png");

  //  for (PImage img : hotSpotsImg)
  //     img.resize(200, 0);

  // Gifs
  hotSpotsGif[0]  = new Gif(this, "embudoanim.gif");
  hotSpotsGif[1]  = new Gif(this, "caparazonanim.gif");
  hotSpotsGif[2]  = new Gif(this, "ojosanim.gif");
  hotSpotsGif[3]  = new Gif(this, "tentaculosanim.gif");
  hotSpotsGif[4]  = new Gif(this, "sifonanim.gif");



  // whether we display the spot left or right
  hotSpotsRightSide[0] = false;
  hotSpotsRightSide[1] = true;
  hotSpotsRightSide[2] = true;
  hotSpotsRightSide[3] = true;
  hotSpotsRightSide[4] = false;

  fondo = loadImage("fondo.jpg");
  titulo = loadImage("titulo.png");
  titulo2 = loadImage("conociendo.png");
  ammoniteinicio = loadImage("ammoniteparainicio.png");
  almeja = loadImage("almeja.png");
  gestos = loadImage("gestos2.png");
   flecha = loadImage("flecha.png");

  buttonX = 0;
  buttonY = 0;
  buttonWidth = 400;
  buttonHeight = 400;

  estado = "inicio";
  almejacerrada = false;
  helvetica = createFont("HelveticaWorld-Regular.ttf", 30);
  nexa = createFont("Nexa Bold.otf", 50);

  pg = createGraphics(width, height, P3D);

  fondo.resize(width, height);
  image(fondo, width/2, height/2);
} //func

void draw() {

  avoidClipping(); // so the graphic AMMONITE is not cut when rotating

  if (estado.equals("inicio")) {
    //-----------Stopping peasy ------
    cam.beginHUD();

    image(fondo, width/2, height/2);
    image(almeja, width-300, height-130, 250, 250);
    fill(0, 50);
    rect(width/2, height/2, width, height);
    fill(0, 30);
    noStroke();
    rect(width/2, 75, width, 150);
    fill(255);
    textFont(nexa);
    text("AMMONITE", width/2, height/6 - 50);
    text("EL ANCESTRO MARINO", width/2, height/6);
    textFont(helvetica);
    text("Toca para comenzar", width/2, height/2 + 300);
    image(gestos, width/2 + 400, height/2-100);

    cam.endHUD(); //--------------------------------
    pushMatrix();
    spotLight(255, 255, 255, 80, 20, 40, -1, 0, 0, PI/2, 2);
    directionalLight(255, 255, 255, width/2, height/2, 20);
    scale(20);
    shape(ammonite);
    popMatrix();
  }//if
  // --------------------- next estado !!!! --------
  else if ( estado.equals("modelo")) {

    doEstadoModelo(); // most important
  }//else if
  // --------------------- EROR - NO estado - program error !!!! --------
  else {
    println ("--------------- EROR - NO estado in draw() !!!! --------");
    exit();
    return;
  }//else
}//func draw()

//------------------------------------------------------------------------

void mousePressed() {

  // mouse is pressed

  if (estado.equals("inicio")) {
    // start screen
    estado = "modelo";
  }
  //
  // --------------------- next estado  --------
  //
  else if ( estado.equals("modelo")) {
    mousePressedAtX = width/2 - 400;
    mousePressedAtY = height/2;
    //
    color colorFromMouse = pg.get(mouseX, mouseY);
    int oldHotSpotFound=hotSpotFound;
    // hotSpotFound=undefined; // reset
    for (int i=0; i<hotSpotsPosition.length; i++) {
      if (colorFromMouse==hotSpotsColor[i]) {
        if (i==oldHotSpotFound)
          hotSpotFound=undefined; // reset
        else
        hotSpotFound=i; // set
        break;
      }
      if (hotSpotFound!=undefined) {
        hotSpotFound = undefined;
      }
    }//for
    //---
  }//else if
  // --------------------- EROR - NO estado
  else {
    println ("--------------- EROR - NO estado in mousePressed()  --------");
    exit();
    return;
  }//else
}//func

void doEstadoModelo() {

  makeInternalPGraphics();

  // background
  cam.beginHUD(); // ----
  image(fondo, width/2, height/2, width, height);
  textFont(nexa);
  fill(0, 40);
  rect(width/2, 75, width, 150);
  fill(255);
  text("CONOCIENDO AL AMMONITE", width/2, height/6 - 50);
  if (almejacerrada == true) {
    image(almeja, width-300, height-130, 250, 250);
  }
  almejacerrada = true;
  image(flecha, 125, height/6 - 60 , 125, 125);

  if (mousePressed && mouseX < 300 && mouseX > 0 && mouseY > 0 && mouseY < 300) {
    estado = "inicio";
  }
  cam.endHUD(); // ----

  // show Ammonite
  pushMatrix();
  spotLight(255, 255, 255, 80, 20, 40, -1, 0, 0, PI/2, 2);
  directionalLight(255, 255, 255, width/2, height/2, 20);
  scale(20);
  shape(ammonite);
  popMatrix();

  cam.beginHUD(); // ----
  // ----


  // text field

  if (hotSpotFound!=undefined) {

    if (hotSpotsRightSide[hotSpotFound]) {
      // show rect with text
      // on right side
      pushMatrix();
      //  translate(mousePressedAtX, mousePressedAtY, hotSpotsPosition[hotSpotFound].z);
      translate(mousePressedAtX, mousePressedAtY );
      buttonX=mousePressedAtX;
      buttonY=mousePressedAtY;
      scale(0.5);
      fill(250, 250, 35);
      // rect(mousePressedAtX, mousePressedAtY, 100, 100);
      fill(255);
      textFont(nexa);
      textSize(90);
      textMode(SHAPE);
      translate(0, 0, 0.2);

      if (hotSpotsImg[hotSpotFound]!=null) {
        // translate(100+hotSpotsImg[hotSpotFound].width/2, 0, 0);
        image(hotSpotsImg[hotSpotFound], 0, 0);
        text(hotSpotsText[hotSpotFound], width/2, -500);
        //fill(0, 0, 0, 50);
      }
      popMatrix();
    } else
    {
      // show rect with text
      // on left side
      pushMatrix();
      // translate(mousePressedAtX, mousePressedAtY, hotSpotsPosition[hotSpotFound].z);
      translate(mousePressedAtX, mousePressedAtY );
      buttonX=mousePressedAtX;
      buttonY=mousePressedAtY;
      //  translate(-50, 0, 0);
      fill(250, 250, 35, 0);
      scale(0.5);
      fill(255);
      // rect(mousePressedAtX, mousePressedAtY, 100, 100);
      textFont(nexa);
      textSize(90);
      textMode(SHAPE);
      translate(0, 0, 0.2);

      if (hotSpotsImg[hotSpotFound]!=null) {
        // translate(-25-hotSpotsImg[hotSpotFound].width/4, hotSpotsImg[hotSpotFound].height/2, 0);
        image(hotSpotsImg[hotSpotFound], 0, 0);
        text(hotSpotsText[hotSpotFound], width/2, -500);
        //fill(0, 0, 0, 50);
        //rect(buttonX, buttonY, buttonWidth, buttonHeight);
      }
      popMatrix();
    }// else
  }//if
  cam.endHUD(); // ----

  // spheres
  for (int i=0; i<hotSpotsPosition.length; i++) {
    PVector pv = hotSpotsPosition[i];
    pushMatrix();
    translate(pv.x, pv.y, pv.z);
    noStroke();
    // use any color your want here
    fill(0, 255, 255, 70); // normal
    if (i==hotSpotFound) {
      fill(2, 255, 100, 70); // highlight
    }
    sphere(7);

    popMatrix();
  }
}//func

void makeInternalPGraphics() {
  pg.beginDraw();
  pg. perspective(PI/3.0, (float) width/height, 1, 1000000);
  pg.setMatrix(getMatrix()); // replace the PGraphics-matrix
  pg.background(0);
  pg.noLights();
  for (int i=0; i<hotSpotsPosition.length; i++) {
    PVector pv=hotSpotsPosition[i];
    pg.pushMatrix();
    pg.translate(pv.x, pv.y, pv.z);
    pg.noStroke();
    pg.fill(hotSpotsColor[i]);
    pg.sphere(7);
    pg.popMatrix();
  }//for
  pg.endDraw();
}//func

void avoidClipping() {
  // avoid clipping :
  // https : // // forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
  perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func
//

The part color change is made in line 302.

Any help?? Thanks


Viewing all articles
Browse latest Browse all 2896

Trending Articles