Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Naming conventions; I use this throughout :
- // class = UpperCase and singular (contains only ONE circle) - CircleButton
- // array = lowerCase and plural (contains many circles) - myCircleButtons
- // object / variable = lowerCase and singular - button / myCircleSize
- RectButton[] myRectButtons = new RectButton[7]; //
- int state=0;
- int prevState=0; // previous state - can be improved
- // FOR special buttons:
- // special value for onWhichStateIsThisButtonVisible
- final int ALL_PAGES = -17; // must be negative
- //special value for stateToGoTo in the class
- final int PREVIOUS_PAGE = -18; // must be negative
- // -----------------------------------------------------------------------------------------------------------------------
- void setup() {
- size(1280, 720);
- background(120); // new
- // Define buttons
- defineButtons();
- // show properties of all buttons
- int i=0;
- println("# Text state target");
- println("-----------------------------------------");
- for (RectButton button : myRectButtons) { // cool way of a for-loop (for **each** button (of type CircleButton) in the array myCircleButtons do the following)
- print(i+"\t");
- button.printlnProperties();
- i++;
- }//for
- println("\nEnd of setup()");
- }//function
- void draw() {
- background(120); // the image is more crisp with background
- fill(255);
- switch (state) {
- case 0:
- text ("page 0", 90, 600);
- text ("You come to a crossing. Which way do you want to go?", 19, 19);
- break;
- case 1:
- text ("page 1", 90, 600);
- break;
- case 2:
- text ("page 2", 90, 600);
- break;
- case 3:
- text ("page 3", 90, 600);
- break;
- case 4:
- background(0);
- text ("page 4", 90, 600);
- break;
- default:
- println("Error 3459: Unknown state error "
- + state);
- state = 0;
- break;
- }//switch
- // cool way of a for-loop (for **each** button (of type CircleButton) in the array myCircleButtons do the following) - see reference
- for (RectButton button : myRectButtons) {
- if (state == button.shownInWhichState || button.shownInWhichState == ALL_PAGES) { // only for current state OR for special buttons
- button.update();
- }
- }//for
- }//function
- // -----------------------------------------------------------------------------------------------------------------------
- // defineButtons function
- void defineButtons() {
- // Define buttons
- int rectH = 100; //
- int rectW = 200;
- // we define on Which State Is This Button Visible for each button separately
- int onWhichStateIsThisButtonVisible=0;
- // color - color when mouse is NOT over this botton
- color myColor = color(0, random(255), random(255)); //
- // define button and put it into the array
- // change buttons individually:
- // define individual values
- rectW = 40; //
- rectH = 100;
- onWhichStateIsThisButtonVisible=0;
- myColor = color(255, 120, 0); // color
- // make new button and pass the values to it
- myRectButtons[0] = new RectButton(
- 410, // x-value position
- 310, // y-value position
- rectW, rectH, // size
- myColor, // color
- "Left", // its text
- onWhichStateIsThisButtonVisible,
- 1 // the state it leads to when clicked !!!!!
- );
- // change buttons individually:
- // define individual values
- rectW = 40;
- rectH = 100; //
- onWhichStateIsThisButtonVisible=0;
- myColor = color(0, 255, 0); // GREEN
- // make new button and pass the values to it
- myRectButtons[1] = new RectButton(
- 210, // x-value position
- 310, // y-value position
- rectW, rectH, // size
- myColor, // color
- "Right", // its text
- onWhichStateIsThisButtonVisible,
- 2 // the state it leads to when clicked !!!!!
- );
- // change buttons individually:
- // define individual values
- onWhichStateIsThisButtonVisible=1;
- myColor = color(0, 0, 255); // BLUE
- // make new button and pass the values to it
- myRectButtons[2] = new RectButton(
- 310, // x-value position
- 110, // y-value position
- rectW, rectH,// size
- myColor, // color
- "North", // its text
- onWhichStateIsThisButtonVisible,
- 3 // the state it leads to when clicked !!!!!
- );
- // change buttons individually:
- // define individual values
- onWhichStateIsThisButtonVisible=3;
- myColor = color(0, 0, 255); // BLUE
- // make new button and pass the values to it
- myRectButtons[3] = new RectButton(
- 310, // x-value position
- 110, // y-value position
- rectW, rectH,// size
- myColor, // color
- "crouch", // its text
- onWhichStateIsThisButtonVisible,
- 3 // the state it leads to when clicked !!!!!
- );
- // change buttons individually:
- // define individual values
- onWhichStateIsThisButtonVisible=3;
- myColor = color(0, 0, 255); // BLUE
- // make new button and pass the values to it
- myRectButtons[4] = new RectButton(
- 610, // x-value position
- 110, // y-value position
- rectW, rectH,// size
- myColor, // color
- "crouch agaom", // its text
- onWhichStateIsThisButtonVisible,
- 2 // the state it leads to when clicked !!!!!
- );
- // ===============================================================
- // on ALL pages
- // change buttons individually:
- // define individual values
- onWhichStateIsThisButtonVisible=ALL_PAGES;
- myColor = color(0, 0, 255); // BLUE
- // make new button and pass the values to it
- myRectButtons[5] = new RectButton(
- width-115, // x-value position
- height-75, // y-value position
- rectW, rectH, // size
- myColor, // color
- "Restart", // its text
- onWhichStateIsThisButtonVisible,
- 0 // the state it leads to when clicked !!!!!
- );
- // change buttons individually:
- // define individual values
- onWhichStateIsThisButtonVisible=ALL_PAGES;
- myColor = color(0, 0, 255); // BLUE
- // make new button and pass the values to it
- myRectButtons[6] = new RectButton(
- width-115-103, // x-value position
- height-75, // y-value position
- rectW, rectH, // size
- myColor, // color
- "Back", // its text
- onWhichStateIsThisButtonVisible,
- PREVIOUS_PAGE // the state it leads to when clicked !!!!!
- );
- }//func
- // -------------------------------------------------------------------------
- // Input functions
- void mousePressed() {
- for (RectButton button : myRectButtons) { // cool way of a for-loop (for **each** button (of type CircleButton) in the array myCircleButtons do the following)
- if (state == button.shownInWhichState || button.shownInWhichState == ALL_PAGES) { // only for current state OR for special buttons
- if (button.overCircle()) { // when mouse on this button
- // When its a Back button:
- if (button.stateToGoTo==PREVIOUS_PAGE) {
- state=prevState; // restore
- return; // leave
- } else
- {
- // normal button : go to new page
- prevState=state; // save old state
- state=button.stateToGoTo; // execute
- return; // leave
- }
- }//if
- }//if
- }//for
- }
- // ========================================================================
- // line to show: here starts a class
- class RectButton {
- int rectX, rectY; // Position of circle button
- int rectH, rectW; // Diameter of circle
- String text;
- color circleColor = color(255, 0, 0); // RED when over!!!!!!!!!!!!!!!
- color baseColor; // when not over - gets defined in the constructor
- boolean circleOver = false; // not really in use, never mind
- // functionality
- int shownInWhichState;
- int stateToGoTo;
- // constructor
- RectButton(int posX_, int posY_, // linebreaks for better readability. Marking parameters with a _ sign.
- int rectH_,
- int rectW_,
- color baseColor_,
- String text_,
- int shownInWhichState_,
- int stateToGoTo_) {
- // constructor
- rectX = posX_;
- rectY = posY_;
- rectH = rectH_;
- rectW = rectW_;
- baseColor = baseColor_;
- shownInWhichState=shownInWhichState_;
- stateToGoTo=stateToGoTo_;
- text=text_;
- ellipseMode(CENTER);
- }// constructor
- void update() {
- // make use of circleColor and baseColor
- if (overCircle()) {
- fill( circleColor ); // over
- stroke(255);
- } else {
- fill( baseColor ); // not over
- noStroke();
- }
- rect(rectX, rectY, rectW, rectH);
- fill(255);
- textAlign(CENTER);
- textSize(22);
- text(text,
- rectX+45, rectY+25);
- //reset
- textAlign(LEFT);
- }//method
- boolean overCircle() {
- if (dist(mouseX, mouseY, rectX+40, rectY+5) < rectW/2 ) { // using in-build function dist() here!
- circleOver = true;
- return true;
- } else {
- circleOver = false;
- return false;
- }
- }//method
- void printlnProperties() {
- //
- println (text
- +"\t"
- + shownInWhichState
- +"\t"
- + stateToGoTo);
- }//method
- //
- }//class // marking the end of the class
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement