Button.cpp
1    #include "Button.h"
2    
3    #include "music.h"
4    
5    /// ボタンの初期化
6    /// @param rect ボタンのサイズ
7    /// @param buttonColor ボタンの色
8    /// @param text ボタンに表示する文字
9    /// @param font 使うフォント
10   Button::Button(RECT rect,Color buttonColor,const TCHAR* text,Font font):button(rect),buttonColor(buttonColor),text(text),font(font)
11   {
12       isActive=true;
13       isOnceClick=false;
14   }
15   
16   /// テキストの設定
17   /// @param text 表示する文字
18   /// @param font 使うフォント
19   void Button::SetText(const TCHAR* text, Font font)
20   {
21       if(font.Handle)
22       {
23           // テキストの幅と高さを取得
24           int textWidth = GetDrawStringWidthToHandle(text, _tcslen(text), font.Handle);
25           int textHeight = GetFontSizeToHandle(font.Handle);
26   
27           // テキストの位置を計算(ボタンの中央に配置)
28           int textX = button.left + (button.right - button.left - textWidth) / 2;
29           int textY = button.top + (button.bottom - button.top - textHeight) / 2;
30   
31           // テキストを描画
32           DrawFormatStringToHandle(textX, textY, GetColor(255, 255, 255), font.Handle, "%s", text);
33       }
34   }
35   
36   //ボタンの描画
37   void Button::Button_Draw()
38   {
39       //ボタンの位置と色を設定
40       DrawBox(button.left, button.top, button.right, button.bottom, GetColor(buttonColor.r,buttonColor.g,buttonColor.b), TRUE);
41   
42       //テキストの設定
43       SetText(text,font);
44   }
45   
46   /// マウスボタンでボタンがクリックされたか
47   /// @param Mouse_Input 入力されるキー
48   /// @return Mouse_Inputが押されたかどうか
49   bool Button::CollButtonToMouseClick(int Mouse_Input)
50   {
51       if(!isActive)
52       {
53           return false;
54       }
55       //ボタンがクリックされたか
56       if(CollRectToMouseClick(button,Mouse_Input))
57       {
58           // クリックSEを再生
59           MusicPlay(buttonSE);
60   
61           isOnceClick = true;
62           //クリックされた
63           return true;
64       }
65       //クリックされていない
66       return false;
67   }
68   
69