Advertisement
Guest User

Rect. buttons

a guest
Apr 7th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.13 KB | None | 0 0
  1. // Naming conventions; I use this throughout :
  2. // class = UpperCase and singular (contains only ONE circle) - CircleButton
  3. // array = lowerCase and plural (contains many circles) - myCircleButtons
  4. // object / variable = lowerCase and singular - button / myCircleSize
  5.  
  6. RectButton[] myRectButtons = new RectButton[7]; //
  7.  
  8. int state=0;
  9. int prevState=0; // previous state - can be improved
  10.  
  11. // FOR special buttons:
  12.  
  13. // special value for onWhichStateIsThisButtonVisible
  14. final int ALL_PAGES = -17; // must be negative
  15.  
  16. //special value for stateToGoTo in the class
  17. final int PREVIOUS_PAGE = -18; // must be negative
  18.  
  19. // -----------------------------------------------------------------------------------------------------------------------
  20.  
  21. void setup() {
  22. size(1280, 720);
  23. background(120); // new
  24.  
  25. // Define buttons
  26. defineButtons();
  27.  
  28. // show properties of all buttons
  29. int i=0;
  30. println("# Text state target");
  31. println("-----------------------------------------");
  32. for (RectButton button : myRectButtons) { // cool way of a for-loop (for **each** button (of type CircleButton) in the array myCircleButtons do the following)
  33. print(i+"\t");
  34. button.printlnProperties();
  35. i++;
  36. }//for
  37.  
  38. println("\nEnd of setup()");
  39. }//function
  40.  
  41. void draw() {
  42. background(120); // the image is more crisp with background
  43. fill(255);
  44.  
  45. switch (state) {
  46. case 0:
  47. text ("page 0", 90, 600);
  48. text ("You come to a crossing. Which way do you want to go?", 19, 19);
  49. break;
  50.  
  51. case 1:
  52. text ("page 1", 90, 600);
  53. break;
  54.  
  55. case 2:
  56. text ("page 2", 90, 600);
  57. break;
  58.  
  59. case 3:
  60. text ("page 3", 90, 600);
  61. break;
  62.  
  63. case 4:
  64. background(0);
  65. text ("page 4", 90, 600);
  66. break;
  67.  
  68. default:
  69. println("Error 3459: Unknown state error "
  70. + state);
  71. state = 0;
  72. break;
  73. }//switch
  74.  
  75. // cool way of a for-loop (for **each** button (of type CircleButton) in the array myCircleButtons do the following) - see reference
  76. for (RectButton button : myRectButtons) {
  77. if (state == button.shownInWhichState || button.shownInWhichState == ALL_PAGES) { // only for current state OR for special buttons
  78. button.update();
  79. }
  80. }//for
  81. }//function
  82.  
  83. // -----------------------------------------------------------------------------------------------------------------------
  84. // defineButtons function
  85.  
  86. void defineButtons() {
  87.  
  88. // Define buttons
  89.  
  90. int rectH = 100; //
  91. int rectW = 200;
  92. // we define on Which State Is This Button Visible for each button separately
  93. int onWhichStateIsThisButtonVisible=0;
  94. // color - color when mouse is NOT over this botton
  95. color myColor = color(0, random(255), random(255)); //
  96. // define button and put it into the array
  97.  
  98. // change buttons individually:
  99. // define individual values
  100. rectW = 40; //
  101. rectH = 100;
  102. onWhichStateIsThisButtonVisible=0;
  103. myColor = color(255, 120, 0); // color
  104. // make new button and pass the values to it
  105. myRectButtons[0] = new RectButton(
  106. 410, // x-value position
  107. 310, // y-value position
  108. rectW, rectH, // size
  109. myColor, // color
  110. "Left", // its text
  111. onWhichStateIsThisButtonVisible,
  112. 1 // the state it leads to when clicked !!!!!
  113. );
  114.  
  115. // change buttons individually:
  116. // define individual values
  117. rectW = 40;
  118. rectH = 100; //
  119. onWhichStateIsThisButtonVisible=0;
  120. myColor = color(0, 255, 0); // GREEN
  121. // make new button and pass the values to it
  122. myRectButtons[1] = new RectButton(
  123. 210, // x-value position
  124. 310, // y-value position
  125. rectW, rectH, // size
  126. myColor, // color
  127. "Right", // its text
  128. onWhichStateIsThisButtonVisible,
  129. 2 // the state it leads to when clicked !!!!!
  130. );
  131.  
  132. // change buttons individually:
  133. // define individual values
  134. onWhichStateIsThisButtonVisible=1;
  135. myColor = color(0, 0, 255); // BLUE
  136. // make new button and pass the values to it
  137. myRectButtons[2] = new RectButton(
  138. 310, // x-value position
  139. 110, // y-value position
  140. rectW, rectH,// size
  141. myColor, // color
  142. "North", // its text
  143. onWhichStateIsThisButtonVisible,
  144. 3 // the state it leads to when clicked !!!!!
  145. );
  146.  
  147. // change buttons individually:
  148. // define individual values
  149. onWhichStateIsThisButtonVisible=3;
  150. myColor = color(0, 0, 255); // BLUE
  151. // make new button and pass the values to it
  152. myRectButtons[3] = new RectButton(
  153. 310, // x-value position
  154. 110, // y-value position
  155. rectW, rectH,// size
  156. myColor, // color
  157. "crouch", // its text
  158. onWhichStateIsThisButtonVisible,
  159. 3 // the state it leads to when clicked !!!!!
  160. );
  161.  
  162. // change buttons individually:
  163. // define individual values
  164. onWhichStateIsThisButtonVisible=3;
  165. myColor = color(0, 0, 255); // BLUE
  166. // make new button and pass the values to it
  167. myRectButtons[4] = new RectButton(
  168. 610, // x-value position
  169. 110, // y-value position
  170. rectW, rectH,// size
  171. myColor, // color
  172. "crouch agaom", // its text
  173. onWhichStateIsThisButtonVisible,
  174. 2 // the state it leads to when clicked !!!!!
  175. );
  176.  
  177. // ===============================================================
  178. // on ALL pages
  179.  
  180. // change buttons individually:
  181. // define individual values
  182. onWhichStateIsThisButtonVisible=ALL_PAGES;
  183. myColor = color(0, 0, 255); // BLUE
  184. // make new button and pass the values to it
  185. myRectButtons[5] = new RectButton(
  186. width-115, // x-value position
  187. height-75, // y-value position
  188. rectW, rectH, // size
  189. myColor, // color
  190. "Restart", // its text
  191. onWhichStateIsThisButtonVisible,
  192. 0 // the state it leads to when clicked !!!!!
  193. );
  194.  
  195. // change buttons individually:
  196. // define individual values
  197. onWhichStateIsThisButtonVisible=ALL_PAGES;
  198. myColor = color(0, 0, 255); // BLUE
  199. // make new button and pass the values to it
  200. myRectButtons[6] = new RectButton(
  201. width-115-103, // x-value position
  202. height-75, // y-value position
  203. rectW, rectH, // size
  204. myColor, // color
  205. "Back", // its text
  206. onWhichStateIsThisButtonVisible,
  207. PREVIOUS_PAGE // the state it leads to when clicked !!!!!
  208. );
  209. }//func
  210.  
  211. // -------------------------------------------------------------------------
  212. // Input functions
  213.  
  214. void mousePressed() {
  215. for (RectButton button : myRectButtons) { // cool way of a for-loop (for **each** button (of type CircleButton) in the array myCircleButtons do the following)
  216. if (state == button.shownInWhichState || button.shownInWhichState == ALL_PAGES) { // only for current state OR for special buttons
  217. if (button.overCircle()) { // when mouse on this button
  218. // When its a Back button:
  219. if (button.stateToGoTo==PREVIOUS_PAGE) {
  220. state=prevState; // restore
  221. return; // leave
  222. } else
  223. {
  224. // normal button : go to new page
  225. prevState=state; // save old state
  226. state=button.stateToGoTo; // execute
  227. return; // leave
  228. }
  229. }//if
  230. }//if
  231. }//for
  232. }
  233.  
  234. // ========================================================================
  235. // line to show: here starts a class
  236.  
  237. class RectButton {
  238.  
  239. int rectX, rectY; // Position of circle button
  240. int rectH, rectW; // Diameter of circle
  241.  
  242. String text;
  243.  
  244. color circleColor = color(255, 0, 0); // RED when over!!!!!!!!!!!!!!!
  245. color baseColor; // when not over - gets defined in the constructor
  246.  
  247. boolean circleOver = false; // not really in use, never mind
  248.  
  249. // functionality
  250. int shownInWhichState;
  251. int stateToGoTo;
  252.  
  253. // constructor
  254. RectButton(int posX_, int posY_, // linebreaks for better readability. Marking parameters with a _ sign.
  255. int rectH_,
  256. int rectW_,
  257.  
  258. color baseColor_,
  259. String text_,
  260. int shownInWhichState_,
  261. int stateToGoTo_) {
  262. // constructor
  263. rectX = posX_;
  264. rectY = posY_;
  265. rectH = rectH_;
  266. rectW = rectW_;
  267. baseColor = baseColor_;
  268. shownInWhichState=shownInWhichState_;
  269. stateToGoTo=stateToGoTo_;
  270. text=text_;
  271.  
  272. ellipseMode(CENTER);
  273. }// constructor
  274.  
  275. void update() {
  276. // make use of circleColor and baseColor
  277. if (overCircle()) {
  278. fill( circleColor ); // over
  279. stroke(255);
  280. } else {
  281. fill( baseColor ); // not over
  282. noStroke();
  283. }
  284. rect(rectX, rectY, rectW, rectH);
  285. fill(255);
  286. textAlign(CENTER);
  287. textSize(22);
  288. text(text,
  289. rectX+45, rectY+25);
  290. //reset
  291. textAlign(LEFT);
  292. }//method
  293.  
  294. boolean overCircle() {
  295. if (dist(mouseX, mouseY, rectX+40, rectY+5) < rectW/2 ) { // using in-build function dist() here!
  296. circleOver = true;
  297. return true;
  298. } else {
  299. circleOver = false;
  300. return false;
  301. }
  302. }//method
  303.  
  304. void printlnProperties() {
  305. //
  306. println (text
  307. +"\t"
  308. + shownInWhichState
  309. +"\t"
  310. + stateToGoTo);
  311. }//method
  312. //
  313. }//class // marking the end of the class
  314. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement