menuitem.cpp
上传用户:jnfxsk
上传日期:2022-06-16
资源大小:3675k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.7
  3. ** Copyright (C) 2003-2007, Relish Games
  4. ** hge.relishgames.com
  5. **
  6. ** Tutorial 06 - Creating menus
  7. */
  8. // In menuitem.cpp/h we define the
  9. // behaviour of our custom GUI control
  10. #include "menuitem.h"
  11. // This is a GUI control constructor,
  12. // we should initialize all the variables here
  13. hgeGUIMenuItem::hgeGUIMenuItem(int _id, hgeFont *_fnt, HEFFECT _snd, float _x, float _y, float _delay, char *_title)
  14. {
  15. float w;
  16. id=_id;
  17. fnt=_fnt;
  18. snd=_snd;
  19. delay=_delay;
  20. title=_title;
  21. color.SetHWColor(0xFFFFE060);
  22. shadow.SetHWColor(0x30000000);
  23. offset=0.0f;
  24. timer=-1.0f;
  25. timer2=-1.0f;
  26. bStatic=false;
  27. bVisible=true;
  28. bEnabled=true;
  29. w=fnt->GetStringWidth(title);
  30. rect.Set(_x-w/2, _y, _x+w/2, _y+fnt->GetHeight());
  31. }
  32. // This method is called when the control should be rendered
  33. void hgeGUIMenuItem::Render()
  34. {
  35. fnt->SetColor(shadow.GetHWColor());
  36. fnt->Render(rect.x1+offset+3, rect.y1+3, HGETEXT_LEFT, title);
  37. fnt->SetColor(color.GetHWColor());
  38. fnt->Render(rect.x1-offset, rect.y1-offset, HGETEXT_LEFT, title);
  39. }
  40. // This method is called each frame,
  41. // we should update the animation here
  42. void hgeGUIMenuItem::Update(float dt)
  43. {
  44. if(timer2 != -1.0f)
  45. {
  46. timer2+=dt;
  47. if(timer2 >= delay+0.1f)
  48. {
  49. color=scolor2+dcolor2;
  50. shadow=sshadow+dshadow;
  51. offset=0.0f;
  52. timer2=-1.0f;
  53. }
  54. else
  55. {
  56. if(timer2 < delay) { color=scolor2; shadow=sshadow; }
  57. else { color=scolor2+dcolor2*(timer2-delay)*10; shadow=sshadow+dshadow*(timer2-delay)*10; }
  58. }
  59. }
  60. else if(timer != -1.0f)
  61. {
  62. timer+=dt;
  63. if(timer >= 0.2f)
  64. {
  65. color=scolor+dcolor;
  66. offset=soffset+doffset;
  67. timer=-1.0f;
  68. }
  69. else
  70. {
  71. color=scolor+dcolor*timer*5;
  72. offset=soffset+doffset*timer*5;
  73. }
  74. }
  75. }
  76. // This method is called when the GUI
  77. // is about to appear on the screen
  78. void hgeGUIMenuItem::Enter()
  79. {
  80. hgeColor tcolor2;
  81. scolor2.SetHWColor(0x00FFE060);
  82. tcolor2.SetHWColor(0xFFFFE060);
  83. dcolor2=tcolor2-scolor2;
  84. sshadow.SetHWColor(0x00000000);
  85. tcolor2.SetHWColor(0x30000000);
  86. dshadow=tcolor2-sshadow;
  87. timer2=0.0f;
  88. }
  89. // This method is called when the GUI
  90. // is about to disappear from the screen
  91. void hgeGUIMenuItem::Leave()
  92. {
  93. hgeColor tcolor2;
  94. scolor2.SetHWColor(0xFFFFE060);
  95. tcolor2.SetHWColor(0x00FFE060);
  96. dcolor2=tcolor2-scolor2;
  97. sshadow.SetHWColor(0x30000000);
  98. tcolor2.SetHWColor(0x00000000);
  99. dshadow=tcolor2-sshadow;
  100. timer2=0.0f;
  101. }
  102. // This method is called to test whether the control
  103. // have finished it's Enter/Leave animation
  104. bool hgeGUIMenuItem::IsDone()
  105. {
  106. if(timer2==-1.0f) return true;
  107. else return false;
  108. }
  109. // This method is called when the control
  110. // receives or loses keyboard input focus
  111. void hgeGUIMenuItem::Focus(bool bFocused)
  112. {
  113. hgeColor tcolor;
  114. if(bFocused)
  115. {
  116. hge->Effect_Play(snd);
  117. scolor.SetHWColor(0xFFFFE060);
  118. tcolor.SetHWColor(0xFFFFFFFF);
  119. soffset=0;
  120. doffset=4;
  121. }
  122. else
  123. {
  124. scolor.SetHWColor(0xFFFFFFFF);
  125. tcolor.SetHWColor(0xFFFFE060);
  126. soffset=4;
  127. doffset=-4;
  128. }
  129. dcolor=tcolor-scolor;
  130. timer=0.0f;
  131. }
  132. // This method is called to notify the control
  133. // that the mouse cursor has entered or left it's area
  134. void hgeGUIMenuItem::MouseOver(bool bOver)
  135. {
  136. if(bOver) gui->SetFocus(id);
  137. }
  138. // This method is called to notify the control
  139. // that the left mouse button state has changed.
  140. // If it returns true - the caller will receive
  141. // the control's ID
  142. bool hgeGUIMenuItem::MouseLButton(bool bDown)
  143. {
  144. if(!bDown)
  145. {
  146. offset=4;
  147. return true;
  148. }
  149. else 
  150. {
  151. hge->Effect_Play(snd);
  152. offset=0;
  153. return false;
  154. }
  155. }
  156. // This method is called to notify the
  157. // control that a key has been clicked.
  158. // If it returns true - the caller will
  159. // receive the control's ID
  160. bool hgeGUIMenuItem::KeyClick(int key, int chr)
  161. {
  162. if(key==HGEK_ENTER || key==HGEK_SPACE)
  163. {
  164. MouseLButton(true);
  165. return MouseLButton(false);
  166. }
  167. return false;
  168. }