Guest User

Untitled

a guest
Feb 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. //Buttonクラス
  2.  
  3. class Button {
  4.  
  5. int x, y, w, h; //x座標、y座標、幅、高さ
  6. String name; //ボタンの名前
  7. boolean wasPressed; //以前にボタンを押したかどうかを保存する
  8.  
  9. int buttonColor = color(255); //通常時のボタンの色
  10. int hoverColor = color(0, 255, 255); //カーソルがボタン上にある時の色
  11. int pushColor = color(255, 255, 0); //押したときの色
  12. int textColor = color(0); //テキストの色
  13.  
  14. //コンストラクタ
  15. Button(int _x, int _y, int _w, int _h, String _name) {
  16. x = _x;
  17. y = _y;
  18. w = _w;
  19. h = _h;
  20. name = _name;
  21. }
  22.  
  23. //カーソルがボタン上にあるかどうか
  24. boolean isHover() {
  25. if ((mouseX > x) && (mouseX < x+w) &&
  26. (mouseY > y) && (mouseY < y+h)) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32.  
  33. //ボタンを押したかどうか
  34. boolean isPressed() {
  35. if (isHover() && mousePressed) {
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41.  
  42. //ボタンをクリックしたかどうか
  43. boolean isClicked() {
  44. boolean isPressed = isPressed(); //現在の押下状態を取得
  45. boolean res;
  46.  
  47. if (!wasPressed && isPressed) { //前に押していない && 今押している
  48. res = true;
  49. } else {
  50. res = false;
  51. }
  52. wasPressed = isPressed; //更新
  53. return res;
  54. }
  55.  
  56. //ボタンの名前を返す
  57. String getName() {
  58. return name;
  59. }
  60.  
  61. //ボタンを表示する
  62. void display() {
  63. //ボタンを描く
  64. if (isPressed()) { //ボタンが押された場合
  65. fill(pushColor); //押したときの色を指定
  66. } else if (isHover()) { //カーソルがボタン上にある場合
  67. fill(hoverColor); //カーソルがボタン上にある時の色を指定
  68. } else { //それ以外
  69. fill(buttonColor); //通常の色を指定
  70. }
  71. rect(x, y, w, h); //長方形を表示
  72.  
  73. //ボタンの上に文字を書く
  74. textAlign(CENTER, CENTER); //テキストの配置を上下中央、左右中央に指定
  75. textSize(32); //テキストサイズを指定
  76. fill(textColor); //テキストの色を指定
  77. text(name, x, y, w, h); //テキストを長方形内に書く
  78. }
  79. }
Add Comment
Please, Sign In to add comment