Share a simple Msgbox

edited April 2014 in Share Your Work

hello all,

I wanted to share a simple MsgBox class which is based on the classic state-model of a program.

Best wishes, Chrisir ;-)

// the states
// consts for states
final int NORMAL = 0;  // normal state 
final int ALERT  = 1;  // alert / shows the msgbox
// current 
int state =  NORMAL;

// this is the object msgBox
MsgBox msgBox = new MsgBox();

// cam 
PVector camPos;     // its vectors 
PVector camLookAt;
PVector camUp;

// cam rotation 
float camCurrentAngle;       // for cam rot around center
float camRadius;             // same situation 

// Font 
PFont font1 ;

// --------------------------------------------------------
// main funcs: 

void setup() {
  size(800, 800, OPENGL);

  // set cams vectors 
  camPos    = new PVector(width/2.0, height/2.0, 600);
  camLookAt = new PVector(width/2.0, height/2.0, -210);
  camUp     = new PVector( 0, 1, 0 );

  background(111);
  font1 = createFont("Arial", 32);
  textFont(font1); 
  //
} // func 

void draw() {
  background(111);

  lookAtAngle();

  camera (camPos.x, camPos.y, camPos.z, 
  camLookAt.x, camLookAt.y, camLookAt.z, 
  camUp.x, camUp.y, camUp.z);

  lights();

  translate(width/2, height/2);
  box (61);

  // camera 
  if (state==NORMAL && !keyPressed)
    camCurrentAngle++;
  lookAtAngle();

  // upper left corner 
  HUD_text("Hit return for MsgBox.");

  // this is the msgbox 
  msgBox.msgBoxDisplay();

  //
} // func draw()

// ----------------------------------------------------
// input funcs

void mousePressed() {

  switch (state) {

  case NORMAL: 
    // 
    break;

  case ALERT: 
    msgBox.mousePressedForMsgbox();
    break;

  default:
    // error 
    println ("Error 139");
    break;
  } // switch
} // func 

void keyPressed () {

  switch (state) {

  case NORMAL: 
    if (key=='X') { 
      //
    }
    else if (key>='0' && key <= '9') {
      //
    }
    else if (key == RETURN || key == ENTER) {
      // start msgbox 
      msgBox.msgBox("You submitted something.");
    } 
    else if (key==ESC) {
      // key=0;
    }
    else {
      //
    }
    break;

  case ALERT: 
    msgBox.keyPressedForMsgbox();
    break;

  default:
    // error 
    println ("Error 296");
    break;
  } // switch
} // func 

// ----------------------------------------------------
// misc funcs

void lookAtAngle() {
  // rotation in the plane : cam 
  camRadius = camLookAt.dist (camPos); 
  // camRadius = 100;
  camPos.x = camRadius * cos (radians(camCurrentAngle)) + camLookAt.x;
  camPos.z = camRadius * sin (radians(camCurrentAngle)) + camLookAt.z;
} // func 

void HUD_text (String a1) {
  // HUD text upper left corner 

  // this is a 2D HUD 
  camera();
  hint(DISABLE_DEPTH_TEST);
  noLights();
  // ------------------
  textSize(16);
  text (a1, 20, 20);
  // ------------------
  // reset all parameters to defaults
  textAlign(LEFT, BASELINE);
  rectMode(CORNER);
  textSize(32);
  hint(ENABLE_DEPTH_TEST); // no HUD anymore
  lights();
}

// =====================================================

class MsgBox {

  // the string to display 
  String alertText = ""; 

  int posX, posY;
  int msgboxW = 400, msgboxH = 200;
  final int innerBorder1=14; 

  // the ok box
  final int innerBorder2 = 8;
  final int okBoxAddY = 70;
  final int okBoxW = 60;
  final int okBoxH = 30;

  // no constructor here 

  void msgBox ( String a1 ) {
    // this activates the message box 
    alertText = a1;
    state = ALERT;
  } // method 

  void msgBoxDisplay() {

    // shows the msgbox (centered on the screen).
    // Call this from draw() always (at the end). 
    // It's active only when the state is ALERT.

    if (state==ALERT && !alertText.equals("")) {

      posX=width/2;
      posY=height/2;

      // this is a 2D HUD 
      camera();
      hint(DISABLE_DEPTH_TEST);
      noLights();

      // the outer box / msgbox 
      noStroke();
      fill(0);
      rectMode(CENTER);  // Set rectMode to CENTER
      rect (posX, posY, msgboxW, msgboxH);
      // a frame
      noFill();
      stroke(111);
      rect (posX, posY, msgboxW-innerBorder1, msgboxH-innerBorder1);

      // the X top left
      textSize(16);
      fill(255, 0, 0); // red
      textAlign(CENTER, CENTER);
      text ("X", posX+msgboxW/2-innerBorder1-4, posY-msgboxH/2+innerBorder1+4);

      // the variable text
      fill(244);
      textAlign(LEFT, CENTER);
      text (alertText, posX-msgboxW/2+innerBorder1, posY-msgboxH/2+2*innerBorder1); 

      // the OK box 
      noStroke();
      fill(88);
      rectMode(CENTER);  // Set rectMode to CENTER
      rect (posX, posY+okBoxAddY, okBoxW-innerBorder2, okBoxH-innerBorder2);
      noFill();
      stroke(111);
      rect (posX, posY+okBoxAddY, okBoxW, okBoxH);
      // the word OK 
      fill(255, 255, 255);
      textAlign(CENTER, CENTER);
      text ("OK", posX, posY+okBoxAddY); 

      // reset all parameters to defaults
      textAlign(LEFT, BASELINE);
      rectMode(CORNER);
      textSize(32);
      hint(ENABLE_DEPTH_TEST); // no HUD anymore
      lights();
    } // if
  } // method  

  // inputs 

    void keyPressedForMsgbox() {
    if (key==ESC||key==RETURN||key==ENTER||key==' ')
      state=NORMAL;
    // if (key==ESC)
    key=0;
  } // method

  void mousePressedForMsgbox() {
    state=NORMAL;
  } // method 
  //
} // class

// =====================================================
Tagged:
Sign In or Register to comment.