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

游戏引擎

开发平台:

Visual C++

  1. // ProcessBar.cpp: implementation of the CProcessBar class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "ProcessBar.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CProcessBar::CProcessBar()
  10. {
  11.     m_iBlockWidth=18;
  12. }
  13. CProcessBar::~CProcessBar()
  14. {
  15. }
  16. void CProcessBar::SetProcessBar(RECT rect,int iMaxValue,int iMinValue,
  17.                    int curValue,bool bHorz,int style)
  18. {
  19. CGraphButton::SetButtonRect(rect);
  20. m_iMaxValue=iMaxValue;
  21. m_iMinValue=iMinValue;
  22. m_iValue=curValue;
  23. m_bHorz=bHorz;
  24. m_iStyle=style;
  25. }
  26. void CProcessBar::SetMaxMinValue(int iMaxValue,int iMinValue)
  27. {
  28. m_iMaxValue=iMaxValue;
  29. m_iMinValue=iMinValue;
  30. }
  31. void CProcessBar::SetValue(int iValue)
  32. {
  33. if(iValue<m_iMinValue || iValue>m_iMaxValue)return;
  34. m_iValue=iValue;
  35. }
  36. void CProcessBar::SetBlockWidth(int width)
  37. {
  38. if(m_bHorz)
  39. {
  40. if(width>(m_rect.right-m_rect.left-2))width=m_rect.right-m_rect.left-2;
  41. }
  42.     else
  43. {
  44. if(width>(m_rect.bottom-m_rect.top-2))width=m_rect.bottom-m_rect.top-2;
  45. }
  46.   if(width<16)width=16;   
  47.     m_iBlockWidth=width;
  48. }
  49. void CProcessBar::RenderProcessBar()
  50. {
  51. UpdateProcessBar();
  52. CGraphButton::RenderButton();
  53. int width=m_rect.right-m_rect.left;
  54. int height=m_rect.bottom-m_rect.top;
  55. RECT rect=m_rect;
  56. if(m_bHorz)
  57. {
  58. rect.left += int(float((width-m_iBlockWidth)*(m_iValue-m_iMinValue))/(m_iMaxValue-m_iMinValue));
  59. rect.right=rect.left+m_iBlockWidth;
  60. rect.top+=2;
  61. rect.bottom-=2;
  62. }
  63. else
  64. {
  65. rect.top += int(float((height-m_iBlockWidth)*(m_iValue-m_iMinValue))/(m_iMaxValue-m_iMinValue));
  66. rect.bottom=rect.top+m_iBlockWidth;
  67. rect.left+=2;
  68. rect.right-=2;
  69. }
  70.     DrawBlock(rect);
  71. }
  72. void CProcessBar::UpdateProcessBar()
  73. {
  74. if(m_iState!=BUTTON_PUSHED)return;
  75. /////////// mouse cling
  76. POINT point;
  77. GetCursorPos(&point);
  78. if(m_bHorz)
  79.         point.y=(m_rect.bottom+m_rect.top)/2;
  80. else
  81.         point.x=(m_rect.right+m_rect.left)/2;
  82.     SetCursorPos(point.x,point.y);
  83. ////////////////////////////////////
  84. int width=m_rect.right-m_rect.left;
  85. int height=m_rect.bottom-m_rect.top;
  86. int delta=m_iMaxValue-m_iMinValue;
  87. if(m_bHorz)
  88. {
  89.     if(m_rect.left>=m_cInput.m_mousePosX)
  90. m_iValue=m_iMinValue;
  91. else if((m_rect.right-m_iBlockWidth)<=m_cInput.m_mousePosX)
  92. m_iValue=m_iMaxValue;
  93. else
  94. {
  95. float factor=float(m_cInput.m_mousePosX-m_rect.left)/(width-m_iBlockWidth);
  96. m_iValue=m_iMinValue + int(delta*factor);
  97. }
  98. }
  99.     else
  100. {
  101.     if(m_rect.top>=m_cInput.m_mousePosY)
  102. m_iValue=m_iMinValue;
  103. else if((m_rect.bottom-m_iBlockWidth)<=m_cInput.m_mousePosY)
  104. m_iValue=m_iMaxValue;
  105. else
  106. {
  107. float factor=float(m_cInput.m_mousePosY-m_rect.top)/(height-m_iBlockWidth);
  108. m_iValue=m_iMinValue+ int(delta*factor);
  109. }
  110. }
  111. }
  112. void  CProcessBar::DrawBlock(RECT rect)
  113. {
  114.     glDisable(GL_TEXTURE_2D);
  115. /////// set color
  116. if(m_iState==BUTTON_NORMAL)glColor3f(0.25f,0.6f,0.0f);
  117. else if(m_iState==BUTTON_ACTIVATE)glColor3f(0,0.8f,0);
  118. else if(m_iState==BUTTON_PUSHED)glColor3f(0.4f,0.8f,0);
  119.     else glColor3f(0.35f,0.35f,0.35f);
  120. glBegin(GL_QUADS);
  121.     glVertex3i(rect.left-400 , 300-rect.top , -520);
  122.     glVertex3i(rect.right-400 , 300-rect.top , -520);
  123.      glVertex3i(rect.right-400 , 300-rect.bottom ,-520);
  124.     glVertex3i(rect.left-400 , 300-rect.bottom , -520);
  125. glEnd();
  126. /////// set color
  127. if(m_iState==BUTTON_NORMAL)glColor3f(0.3f,0.7f,0.3f);
  128. else if(m_iState==BUTTON_ACTIVATE)glColor3f(0,1.0f,0);
  129. else if(m_iState==BUTTON_PUSHED)glColor3f(0.8f,1.0f,0);
  130.     else glColor3f(0.6f,0.6f,0.6f);
  131.  
  132. /////// draw rectangle
  133. glBegin(GL_LINE_LOOP);
  134.     glVertex3i(rect.left-400 , 300-rect.top , -520);
  135.     glVertex3i(rect.right-400 , 300-rect.top , -520);
  136.     glVertex3i(rect.right-400 , 300-rect.bottom ,-520);
  137.     glVertex3i(rect.left-400 , 300-rect.bottom , -520);
  138. glEnd();
  139. }