class Button { PImage[] icon, object; float buttonX, buttonY; // ボタンのXY座標 float buttonWidth, buttonHeight; // ボタンの縦横幅 int state; boolean isHover; Button(String name, int num, float x, float y) { imageMode(CENTER); object = ButtonImageManager.getObjectImages(name, num); icon = ButtonImageManager.getIconImages(name, num); state = -1; // 位置の初期設定 buttonWidth = 40; buttonHeight = 17; buttonX = x; buttonY = y; } void update() { // ボタンの描画 updateButton(); // ボタンがONの時に背景オブジェクトを描く if(state >= 0) { image(object[state], buttonX + buttonWidth/2, buttonY - object[state].height/2); } } // ボタンを状態に合わせて描く void updateButton() { rectMode(CORNER); // マウスポインタが中にあれば塗りの色を変える if( isMouseInside(buttonX, buttonY, buttonWidth, buttonHeight) ) { // ボタン上 fill(#DE621E); isHover = true; } else { fill(#000000); isHover = false; } // ボタンの形を描く stroke(#FFFFFF); rect(buttonX, buttonY, buttonWidth, buttonHeight); // ボタンがONの時アイコンを描く if(state >= 0) { image(icon[state], buttonX + buttonWidth/2, buttonY + buttonHeight/2); } } // 指定された領域にマウスカーソルがあるか判定 boolean isMouseInside(float x, float y, float w, float h) { if(is3DMode) return false; // 3Dモードの時は常にfalse; x += screenX(0, 0); y += screenY(0, 0); if( mouseX >= x && mouseX <= x + w && mouseY >= y && mouseY <= y + h ) { return true; } else { return false; } } void mousePressed() { if( isHover ) { state++; if( state == object.length ) { state = -1; } } } }