BaseGUICursor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #include "BaseGUICursor.h"
  2. #include "..\..\..\common_h\InputSrvCmds.h"
  3. const float max_time = 0.1f;
  4. BaseGUICursor:: BaseGUICursor()
  5. {
  6. cursor.Reset();
  7. // m_hided = true;
  8. m_hided = false;
  9. m_time = 0.0f;
  10. m_px = m_x = 0.5f;
  11. m_py = m_y = 0.5f;
  12. m_moved = false;
  13. m_clicked = false;
  14. m_active = null;
  15. m_show = false;
  16. }
  17. BaseGUICursor::~BaseGUICursor()
  18. {
  19. if( m_active )
  20. m_active->Release();
  21. }
  22. void BaseGUICursor::Restart()
  23. {
  24. // m_px = m_x = 0.5f;
  25. // m_py = m_y = 0.5f;
  26. m_moved = false;
  27. m_clicked = false;
  28. m_cnt = 0;
  29. // m_init = true; m_nomove = true;
  30. m_hide = true;
  31. Show(m_show);
  32. // SetUpdate(&BaseGUICursor::InitFn,ML_FIRST);
  33. SetUpdate(&BaseGUICursor::InitFn,ML_FIRST + 2);
  34. }
  35. void _cdecl BaseGUICursor::InitFn(float dltTime, long level)
  36. {
  37. BaseGUIElement *cur = (BaseGUIElement *)cursor.Ptr();
  38. if( cur )
  39. {
  40. if( m_hide )
  41. {
  42. cur->Show(false);
  43. m_hide = false;
  44. }
  45. }
  46. DelUpdate(&BaseGUICursor::InitFn);
  47. }
  48. bool BaseGUICursor::Create (MOPReader &reader)
  49. {
  50. InitParams(reader);
  51. return true;
  52. }
  53. bool BaseGUICursor::EditMode_Update(MOPReader &reader)
  54. {
  55. InitParams(reader);
  56. return true;
  57. }
  58. void BaseGUICursor::Show(bool isShow)
  59. {
  60. MissionObject::Show(isShow);
  61. #ifndef _XBOX
  62. if( isShow )
  63. {
  64. // SetUpdate(&BaseGUICursor::Update,ML_FIRST);
  65. SetUpdate(&BaseGUICursor::Update,ML_FIRST + 2);
  66. SetUpdate(&BaseGUICursor::Redraw,ML_LAST);
  67. }
  68. else
  69. {
  70. DelUpdate(&BaseGUICursor::Update);
  71. DelUpdate(&BaseGUICursor::Redraw);
  72. }
  73. #endif
  74. }
  75. void _cdecl BaseGUICursor::Update(float dltTime, long level)
  76. {
  77. if( EditMode_IsOn())
  78. return;
  79. long active = m_active ? m_active->Get(1) : 1;
  80. if( !active )
  81. {
  82. m_nomove = true;
  83. m_nodraw = 2;
  84. return;
  85. }
  86. BaseGUIElement *cur = (BaseGUIElement *)cursor.Ptr();
  87. if( cur )
  88. {
  89. Controls().FreeMouse();
  90. bool clicked = (
  91. Controls().GetControlStateType(m_lb) != CST_INACTIVE ||
  92. Controls().GetControlStateType(m_rb) != CST_INACTIVE);
  93. if( clicked )
  94. {
  95. m_clicked = true; m_cnt = 5;
  96. }
  97. else
  98. {
  99. if( m_clicked )
  100. {
  101. if( --m_cnt > 0 )
  102. m_clicked = true;
  103. else
  104. m_clicked = false;
  105. }
  106. }
  107. if( dltTime <= 0.0f )
  108. {
  109. if( m_drawOnPause )
  110. cur->Draw();
  111. return;
  112. }
  113. float x = Controls().GetControlStateFloat(m_mh)/Render().GetScreenInfo2D().dwWidth;
  114. float y = Controls().GetControlStateFloat(m_mv)/Render().GetScreenInfo2D().dwHeight;
  115. if( m_nomove )
  116. {
  117. m_nomove = false;
  118. x = 0.5f;
  119. y = 0.5f;
  120. }
  121. /* m_x += x - 0.5f;
  122. m_y += y - 0.5f;
  123. if( m_x < 0.0f )
  124. m_x = 0.0f;
  125. if( m_x > 1.0f )
  126. m_x = 1.0f;
  127. if( m_y < 0.0f )
  128. m_y = 0.0f;
  129. if( m_y > 1.0f )
  130. m_y = 1.0f;*/
  131. m_x = x;
  132. m_y = y;
  133. if( m_x < 0.0f ) m_x = -1.0f;
  134. if( m_x > 1.0f ) m_x = 2.0f;
  135. if( m_y < 0.0f ) m_y = -1.0f;
  136. if( m_y > 1.0f ) m_y = 2.0f;
  137. //// прячем курсор при активации клавиатуры ////
  138. const char *s = Controls().GetPressedControl(null);
  139. // if( s && strncmp(s,"Mouse",5))
  140. // m_hided = true;
  141. ////////////////////////////////////////////////
  142. if( m_hide )
  143. {
  144. cur->Show(false);
  145. m_hide = false;
  146. }
  147. if( m_init )
  148. {
  149. m_init = false;
  150. m_px = m_x = 0.5f;
  151. m_py = m_y = 0.5f;
  152. }
  153. if( m_px == m_x &&
  154. m_py == m_y )
  155. {
  156. m_moved = false;
  157. }
  158. else
  159. {
  160. /* if( m_init )
  161. {
  162. m_init = false;
  163. }
  164. else
  165. {
  166. m_hided = false;
  167. }*/
  168. m_hided = false;
  169. m_moved = true;
  170. }
  171. m_px = m_x;
  172. m_py = m_y;
  173. if( m_hided )
  174. {
  175. m_time -= dltTime;
  176. if( m_time < 0.0f )
  177. m_time = 0.0f;
  178. }
  179. else
  180. {
  181. m_time += dltTime;
  182. if( m_time > max_time )
  183. m_time = max_time;
  184. }
  185. }
  186. }
  187. void _cdecl BaseGUICursor::Redraw(float dltTime, long level)
  188. {
  189. if( EditMode_IsOn())
  190. return;
  191. if( m_nodraw > 0 )
  192. {
  193. m_nodraw--;
  194. return;
  195. }
  196. BaseGUIElement *cur = (BaseGUIElement *)cursor.Ptr();
  197. if( cur )
  198. {
  199. /* cur->Draw(
  200. m_x + m_dx,
  201. m_y + m_dy,m_time/max_time*cursor->GetAlpha());*/
  202. float w,h; cur->GetSize(w,h);
  203. float asp = cur->GetAspect();
  204. cur->SetAlign(IGUIElement::OnLeft);
  205. cur->SetPosition(
  206. (m_x + m_dx)/asp - w*0.5f - w*m_off_x,
  207. (m_y + m_dy) - h*0.5f - h*m_off_y);
  208. cur->SetAlpha(m_time/max_time);
  209. cur->Draw();
  210. cur->Update(dltTime);
  211. }
  212. }
  213. /*
  214. void BaseGUICursor::PostCreate()
  215. {
  216. if( !cursor )
  217. {
  218. cursor = (BaseGUIElement *)FindObject(cursorName);
  219. if( cursor )
  220. {
  221. BaseGUIElement::Rect r; cursor->GetRect(r);
  222. m_dx = (r.r - r.l)*0.5f*0.01f;
  223. m_dy = (r.b - r.t)*0.5f*0.01f;
  224. }
  225. else
  226. {
  227. m_dx = 0.0f;
  228. m_dy = 0.0f;
  229. }
  230. if( cursor && !EditMode_IsOn())
  231. cursor->Show(false);
  232. }
  233. }
  234. */
  235. void BaseGUICursor::InitParams(MOPReader &reader)
  236. {
  237. m_px = m_x = 0.5f;
  238. m_py = m_y = 0.5f;
  239. m_moved = false;
  240. m_clicked = false;
  241. m_cnt = 0;
  242. m_lb = Controls().FindControlByName( "LeftMouseButton");
  243. m_rb = Controls().FindControlByName("RightMouseButton");
  244. m_mh = Controls().FindControlByName("MouseH");
  245. m_mv = Controls().FindControlByName("MouseV");
  246. ConstString t = reader.String();
  247. if( !cursor.Validate() || t != cursor.Ptr()->GetObjectID() )
  248. {
  249. FindObject(t,cursor);
  250. BaseGUIElement *cur = (BaseGUIElement *)cursor.Ptr();
  251. if( cur )
  252. {
  253. BaseGUIElement::Rect r; cur->GetRect(r);
  254. m_dx = (r.r - r.l)*0.5f*0.01f;
  255. m_dy = (r.b - r.t)*0.5f*0.01f;
  256. }
  257. else
  258. {
  259. m_dx = 0.0f;
  260. m_dy = 0.0f;
  261. }
  262. if( cur && !EditMode_IsOn())
  263. cur->Show(false);
  264. }
  265. // cursorName = t;
  266. m_px = m_x = 0.5f;
  267. m_py = m_y = 0.5f;
  268. m_init = true; m_nomove = true;
  269. m_hide = true;
  270. m_nodraw = 0;
  271. m_off_x = reader.Float();
  272. m_off_y = reader.Float();
  273. m_drawOnPause = reader.Bool();
  274. Show(m_show = reader.Bool());
  275. }
  276. MOP_BEGINLISTCG(BaseGUICursor, "GUI Cursor", '1.00', 2000/*0*/, "GUI Cursor\n\n Use to manage screen cursor", "Interface")
  277. MOP_STRING("Face widget", "")
  278. MOP_FLOATEX("Face hor offset",0.0f,0.0f,1.0f)
  279. MOP_FLOATEX("Face ver offset",0.0f,0.0f,1.0f)
  280. MOP_BOOL("Draw on pause", true)
  281. MOP_BOOL("Show", false)
  282. MOP_ENDLIST(BaseGUICursor)