Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Buttonクラス
- class Button {
- int x, y, w, h; //x座標、y座標、幅、高さ
- String name; //ボタンの名前
- boolean wasPressed; //以前にボタンを押したかどうかを保存する
- int buttonColor = color(255); //通常時のボタンの色
- int hoverColor = color(0, 255, 255); //カーソルがボタン上にある時の色
- int pushColor = color(255, 255, 0); //押したときの色
- int textColor = color(0); //テキストの色
- //コンストラクタ
- Button(int _x, int _y, int _w, int _h, String _name) {
- x = _x;
- y = _y;
- w = _w;
- h = _h;
- name = _name;
- }
- //カーソルがボタン上にあるかどうか
- boolean isHover() {
- if ((mouseX > x) && (mouseX < x+w) &&
- (mouseY > y) && (mouseY < y+h)) {
- return true;
- } else {
- return false;
- }
- }
- //ボタンを押したかどうか
- boolean isPressed() {
- if (isHover() && mousePressed) {
- return true;
- } else {
- return false;
- }
- }
- //ボタンをクリックしたかどうか
- boolean isClicked() {
- boolean isPressed = isPressed(); //現在の押下状態を取得
- boolean res;
- if (!wasPressed && isPressed) { //前に押していない && 今押している
- res = true;
- } else {
- res = false;
- }
- wasPressed = isPressed; //更新
- return res;
- }
- //ボタンの名前を返す
- String getName() {
- return name;
- }
- //ボタンを表示する
- void display() {
- //ボタンを描く
- if (isPressed()) { //ボタンが押された場合
- fill(pushColor); //押したときの色を指定
- } else if (isHover()) { //カーソルがボタン上にある場合
- fill(hoverColor); //カーソルがボタン上にある時の色を指定
- } else { //それ以外
- fill(buttonColor); //通常の色を指定
- }
- rect(x, y, w, h); //長方形を表示
- //ボタンの上に文字を書く
- textAlign(CENTER, CENTER); //テキストの配置を上下中央、左右中央に指定
- textSize(32); //テキストサイズを指定
- fill(textColor); //テキストの色を指定
- text(name, x, y, w, h); //テキストを長方形内に書く
- }
- }
Add Comment
Please, Sign In to add comment