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

游戏引擎

开发平台:

Visual C++

  1. // SelectBox.cpp: implementation of the CSelectBox class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "SelectBox.h"
  6. #include <stdio.h>
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CSelectBox::CSelectBox()
  11. {
  12.     m_pButton=NULL;
  13. }
  14. CSelectBox::~CSelectBox()
  15. {
  16. if(m_pButton!=NULL)
  17. {
  18.         delete []m_pButton;
  19. }
  20. }
  21. void CSelectBox::SetSelectBox(RECT rect,char *titleFile,int curItem)
  22. {
  23. int width=rect.right-rect.left;
  24. int height=rect.bottom-rect.top;
  25. m_iLineHeight=25;
  26. m_iShowLine=height/m_iLineHeight;
  27. rect.bottom=rect.top+ m_iShowLine*m_iLineHeight+5;
  28. m_rect=rect;
  29.     
  30. /////////////////// read file/////////
  31. FILE *file;
  32. file = fopen(titleFile, "rt");
  33.     if(file==NULL)
  34. {
  35. m_numLine=1;  
  36. m_pButton=new CGraphButton [1];
  37. char strErr[64];
  38. strcpy(strErr,"Can't Open file "");
  39. strcat(strErr,titleFile);
  40. strcat(strErr,""!");
  41. rect.bottom=rect.top+20;
  42. m_pButton[0].SetButton(rect,strErr);
  43. }
  44. else
  45. {
  46.         fseek(file,0,SEEK_END);
  47.     int size=ftell(file);
  48.     fseek(file,0,SEEK_SET);
  49. /////////////////// Find out lines num
  50. int numLine=0;
  51. char str[256];
  52. char *pr=NULL;
  53. do
  54. {
  55. pr=fgets(str,254,file);
  56. if(pr!=NULL && str[0]!='n')numLine++;
  57. }while(pr!=NULL);
  58.      m_numLine= numLine;
  59.         ///////////////////// read title
  60.     fseek(file,0,SEEK_SET);
  61. m_pButton=new CGraphButton [m_numLine] ;
  62. int index=0;
  63. do
  64. {
  65. pr=fgets(str,254,file);
  66. if(pr!=NULL && str[0]!='n')
  67. {
  68. int len=strlen(str);
  69. if(str[len-1]=='n')str[len-1]=NULL;
  70. m_pButton[index].SetButton(rect,str,BUTTON_NORMAL,TEXT_ALIGN_LEFT);
  71.                 index++;
  72. }
  73. }while(pr!=NULL && index<m_numLine);
  74. fclose(file);
  75. }
  76. ///////////////// set button color
  77. m_iSelect=0;
  78. for(int i=0;i<m_numLine;i++)
  79. {
  80.         m_pButton[i].SetActivateColor(0,0.4f,0);
  81.         m_pButton[i].SetNormalColor(0,0.4f,0);
  82.         m_pButton[i].SetRectangleColor(0,0.4f,0);
  83. }
  84.     m_pButton[m_iSelect].SetActivateColor(0,0.6f,0);
  85. m_pButton[m_iSelect].SetNormalColor(0,0.6f,0);
  86.   ////////// scroll bar   
  87.     rect=m_rect;
  88. if(m_numLine>m_iShowLine)
  89. {
  90. m_bShowScroll=true;
  91.      rect.left=rect.right-20; // 20 is scrollbar's width
  92.      m_cScrollBar.SetScrollBar(rect,false,m_numLine-m_iShowLine,0);
  93.      m_cScrollBar.SetBlockWidth(16);
  94. }
  95. else
  96. {
  97.         m_iShowLine=m_numLine;
  98. m_bShowScroll=false;
  99. }
  100. m_iHead=0;
  101. }
  102. void CSelectBox::SetSelect(int iItem)
  103. {
  104. m_iSelect=iItem;
  105. if(m_iSelect>(m_iMaxItem-1))m_iSelect=m_iMaxItem-1;
  106. if(m_iSelect<0)m_iSelect=0;
  107. }
  108. int  CSelectBox::GetSelect()
  109. {
  110.     return m_iSelect;
  111. }
  112. void CSelectBox::RenderSelectBox()
  113. {
  114. UpdateSelectBox();
  115.     if(m_bShowScroll)
  116. {
  117. m_cScrollBar.RenderScrollBar();
  118. }
  119. ///////////////////////////////////////
  120. RECT rect=m_rect;
  121. rect.right=m_rect.right-21;
  122.     rect.top=m_rect.top+5;
  123. rect.bottom=rect.top+20;
  124. for(int i=0;i<m_iShowLine;i++)
  125. {
  126. m_pButton[i+m_iHead].SetButtonRect(rect);
  127.         m_pButton[i+m_iHead].RenderButton();
  128. rect.top+=m_iLineHeight;
  129. rect.bottom+=m_iLineHeight;
  130. }
  131. //////////////////////////background
  132. /////// draw rectangle
  133.     glColor3f(0,0.7f,0);
  134. glBegin(GL_LINE_LOOP);
  135.     glVertex3i(m_rect.left-400 , 300-m_rect.top , -520);
  136.     glVertex3i(m_rect.right-400 , 300-m_rect.top , -520);
  137.     glVertex3i(m_rect.right-400 , 300-m_rect.bottom ,-520);
  138.     glVertex3i(m_rect.left-400 , 300-m_rect.bottom , -520);
  139. glEnd();
  140. }
  141. void  CSelectBox::UpdateSelectBox()
  142. {
  143. //////////////////////////////
  144.     if(!m_bShowScroll)m_iHead=0;
  145. else m_iHead=m_cScrollBar.GetValue();
  146. int old = m_iSelect;
  147. for(int i=0;i<m_iShowLine;i++)
  148. {
  149. if(m_pButton[i+m_iHead].m_bSelected)
  150. {
  151. m_pButton[i+m_iHead].m_bSelected=false;
  152. m_iSelect=i+m_iHead;
  153. m_pButton[i+m_iHead].SetActivateColor(0,0.6f,0);
  154. m_pButton[i+m_iHead].SetNormalColor(0,0.6f,0);
  155. }
  156. }
  157. if(old!=m_iSelect)
  158. {
  159. m_bValueChanged=true;
  160.         m_pButton[old].SetActivateColor(0,0.4f,0);
  161. m_pButton[old].SetNormalColor(0,0.4f,0);
  162. }
  163. else m_bValueChanged=false;
  164. }