GraphButton.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:5k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // GraphButton.cpp: implementation of the CGraphButton class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "GraphButton.h"
  6. #include "audio.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CGraphButton::CGraphButton()
  11. {
  12. m_iState=0;
  13. m_bSelected=false;
  14. m_bShowPicture=false;
  15. m_crNormal[0]=m_crNormal[2]=0;
  16. m_crNormal[1]=0.4f;
  17. m_crActivate[0]=m_crActivate[2]=0;
  18. m_crActivate[1]=0.6f;
  19. m_crRect[0]=m_crRect[2]=0;
  20. m_crRect[1]=0.6f;
  21. }
  22. CGraphButton::~CGraphButton()
  23. {
  24. }
  25. void CGraphButton::SetButton(RECT rect,char sText[],int state,int iTextAlign)
  26. {
  27. SetButtonText(sText,iTextAlign);
  28. SetButtonRect(rect);  
  29. m_iState=state;
  30. }
  31. void CGraphButton::SetButton(RECT rect,unsigned int texID,int state)
  32. {
  33. SetButtonRect(rect);
  34. SetButtonPicture(texID);  
  35. m_iState=state;
  36. }
  37. void CGraphButton::SetButtonRect(RECT rect)
  38. {
  39. m_rect=rect;
  40. }
  41. void CGraphButton::SetButtonText(char sText[],int iTextAlign)
  42. {
  43. int len=strlen(sText);
  44. if(len>31)
  45.     sText[31]=NULL;
  46. strcpy(m_sText, sText);
  47. m_bShowPicture=false;
  48. m_iTextAlign=iTextAlign;
  49. }
  50. void CGraphButton::SetButtonPicture(unsigned int texID)
  51. {
  52. m_texID=texID;
  53. m_bShowPicture=true;
  54. }
  55. void CGraphButton::SetButtonState(int state)
  56. {
  57. m_iState=state;
  58. }
  59. void CGraphButton::RenderButton()
  60. {
  61.     UpdateButton();
  62. DrawButton();
  63. }
  64. void CGraphButton::UpdateButton()
  65. {
  66. int old=m_iState;
  67. if(m_iState==BUTTON_DEAD)return;
  68. if(IsInRect())
  69. {
  70. if(m_cInput.m_keys[MOUSE_0])
  71. m_iState=BUTTON_PUSHED;
  72. else
  73. {
  74. if(m_iState==BUTTON_PUSHED)m_bSelected=true;
  75. m_iState=BUTTON_ACTIVATE;
  76. }
  77. }
  78. else
  79. {
  80. m_iState=BUTTON_NORMAL;
  81. m_bSelected=false;
  82. }
  83. if(old!=m_iState)
  84. {
  85. // if(m_iState==BUTTON_ACTIVATE)audio.Play(MENU_SOUND_ONITEM,1,false);
  86. if(m_iState==BUTTON_PUSHED)CAudio::Play(MENU_SOUND_CLICK,1,false);
  87. }
  88. }
  89. void CGraphButton::DrawButton()
  90. {
  91. if(m_bShowPicture)
  92. {
  93.       glBindTexture(GL_TEXTURE_2D, m_texID);
  94.     glEnable(GL_TEXTURE_2D);
  95. if(m_iState==BUTTON_DEAD)glColor3f(0.3f,0.3f,0.3f);
  96. else glColor3f(1,1,1);
  97.       glBegin(GL_QUADS);
  98.           glTexCoord2f(0,1);    
  99.         glVertex3i(m_rect.left-400 , 300-m_rect.top , -520);
  100.         glTexCoord2f(1,1);   
  101.         glVertex3i(m_rect.right-400 , 300-m_rect.top , -520);
  102.     glTexCoord2f(1,0);   
  103.         glVertex3i(m_rect.right-400 , 300-m_rect.bottom ,-520);
  104.     glTexCoord2f(0,0);    
  105.         glVertex3i(m_rect.left-400 , 300-m_rect.bottom , -520);
  106.     glEnd();
  107.     glDisable(GL_TEXTURE_2D);
  108. }
  109. else
  110. {
  111.     glDisable(GL_TEXTURE_2D);
  112. //     glBlendFunc(GL_ONE,GL_SRC_COLOR);
  113. //     glEnable(GL_BLEND);
  114. /////// set color
  115. if(m_iState==BUTTON_NORMAL)glColor3fv(m_crNormal);
  116. else if(m_iState==BUTTON_ACTIVATE)glColor3fv(m_crActivate);
  117. else if(m_iState==BUTTON_PUSHED)glColor3fv(m_crActivate);//(0.4f,0.6f,0);
  118.         else glColor3f(0.35f,0.35f,0.35f);
  119. glBegin(GL_QUADS);
  120.     glVertex3i(m_rect.left-400 , 300-m_rect.top , -520);
  121.     glVertex3i(m_rect.right-400 , 300-m_rect.top , -520);
  122.      glVertex3i(m_rect.right-400 , 300-m_rect.bottom ,-520);
  123.     glVertex3i(m_rect.left-400 , 300-m_rect.bottom , -520);
  124. glEnd();
  125. //     glDisable(GL_BLEND);
  126. }
  127. /////// set color
  128. if(m_iState==BUTTON_NORMAL)glColor3fv(m_crRect);
  129. else if(m_iState==BUTTON_ACTIVATE)glColor3f(m_crRect[0]*2,m_crRect[1]*2,m_crRect[2]*2);
  130. else if(m_iState==BUTTON_PUSHED)glColor3f(0.8f,1.0f,0);
  131.     else glColor3f(0.6f,0.6f,0.6f);
  132. /////// draw rectangle
  133. glBegin(GL_LINE_LOOP);
  134.     glVertex3i(m_rect.left-400 , 300-m_rect.top , -520);
  135.     glVertex3i(m_rect.right-400 , 300-m_rect.top , -520);
  136.     glVertex3i(m_rect.right-400 , 300-m_rect.bottom ,-520);
  137.     glVertex3i(m_rect.left-400 , 300-m_rect.bottom , -520);
  138. glEnd();
  139. if(m_bShowPicture)return;
  140. /////// set color
  141. if(m_iState==BUTTON_NORMAL)glColor3f(0.7f,0.7f,0.3f);
  142. else if(m_iState==BUTTON_ACTIVATE)glColor3f(1,0.5f,0);
  143. else if(m_iState==BUTTON_PUSHED)glColor3f(1,0.5f,0);
  144.     else glColor3f(0.4f,0.4f,0.4f);
  145. /////// draw text
  146.     if(m_iTextAlign==TEXT_ALIGN_CENTER)
  147. m_cText.PrintString((m_rect.left+m_rect.right)/2,
  148.                     (m_rect.bottom+m_rect.top)/2,
  149.      m_sText,true,16,0);
  150. else if(m_iTextAlign==TEXT_ALIGN_LEFT)
  151. m_cText.PrintString(m_rect.left+2,m_rect.top+2,
  152.     m_sText,false,16,0);
  153. glColor3f(1,1,1);
  154. }
  155. void   CGraphButton::SetActivateColor(float r,float g,float b)
  156. {
  157. m_crActivate[0]=r;
  158. m_crActivate[1]=g;
  159. m_crActivate[2]=b;
  160. }
  161. void   CGraphButton::SetNormalColor(float r,float g,float b)
  162. {
  163. m_crNormal[0]=r;
  164. m_crNormal[1]=g;
  165. m_crNormal[2]=b;
  166. }
  167. void  CGraphButton::SetRectangleColor(float r,float g,float b)
  168. {
  169. m_crRect[0]=r;
  170. m_crRect[1]=g;
  171. m_crRect[2]=b;
  172. }
  173. bool CGraphButton::IsInRect()
  174. {
  175. if(m_cInput.m_mousePosX<(m_rect.left) || m_cInput.m_mousePosX>(m_rect.right) )
  176. return false;
  177. if(m_cInput.m_mousePosY<(m_rect.top) || m_cInput.m_mousePosY>(m_rect.bottom) )
  178. return false;
  179.     return true;
  180. }