GUIPanel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. #include "GUIPanel.h"
  2. #include "..\Utils\InterfaceUtils.h"
  3. #include "..\ProgressBar\csProgressBar.h"
  4. GUIPanel::GUIPanel(void)
  5. {
  6. preTime = 0.0f;
  7. preTimeMax = 1.0f;
  8. m_hor = null;
  9. m_ver = null;
  10. drawLevel = ML_GUI2;
  11. m_show = false;
  12. }
  13. GUIPanel::~GUIPanel(void)
  14. {
  15. }
  16. void GUIPanel::ReadMOPs(MOPReader & reader)
  17. {
  18. m_clipWidth = reader.Float();
  19. m_clipHeight = reader.Float();
  20. m_natSize.w = m_clipWidth;
  21. m_natSize.h = m_clipHeight;
  22. m_useClipping = reader.Bool();
  23. SetClipping(m_useClipping);
  24. m_texture = reader.String().c_str();
  25. m_texAlpha = reader.Float();
  26. drawPriority = reader.Long();
  27. m_quadRender.SetTexture(m_texture);
  28. ConstString h = reader.String();
  29. ConstString v = reader.String();
  30. MOSafePointer sp;
  31. if( h.NotEmpty() )
  32. {
  33. FindObject(h,sp);
  34. m_hor = (csProgressBar *)sp.Ptr();
  35. }
  36. if( v.NotEmpty() )
  37. {
  38. FindObject(v,sp);
  39. m_ver = (csProgressBar *)sp.Ptr();
  40. }
  41. Activate(reader.Bool());
  42. // Show (reader.Bool());
  43. nativeShow = reader.Bool();
  44. m_show = nativeShow;
  45. m_alphaOriginal = GetNativeAlpha();
  46. m_fadeAlpha = 1.0f;
  47. m_clipX = GetX(false);
  48. m_clipY = GetY(false);
  49. SizeTo(m_clipWidth,m_clipHeight);
  50. m_state = Normal;
  51. }
  52. bool GUIPanel::Create (MOPReader &reader)
  53. {
  54. EditMode_Update(reader);
  55. return true;
  56. }
  57. void _cdecl GUIPanel::InitFunc(float, long)
  58. {
  59. BaseGUIElement *parent = GetParent();
  60. if( BaseGUIElement *newParent = FindParent())
  61. {
  62. newParent->Register(this);
  63. }
  64. else
  65. {
  66. if( parent )
  67. parent->UnRegister(this);
  68. }
  69. // Show(IsShow());
  70. Show(nativeShow);
  71. DelUpdate(&GUIPanel::InitFunc);
  72. }
  73. bool GUIPanel::EditMode_Update(MOPReader & reader)
  74. {
  75. BaseGUIElement::EditMode_Update(reader);
  76. ReadMOPs (reader);
  77. NotifyChildren(ParentChanged);
  78. m_rect = GetFullRect();
  79. Rect r; GetRect(r,false);
  80. float width = r.r - r.l;
  81. float height = r.b - r.t;
  82. if( m_hor )
  83. {
  84. m_hor->SetMax( m_rect.r - m_rect.l - width);
  85. m_hor->SetPos(-m_rect.l);
  86. }
  87. if( m_ver )
  88. {
  89. m_ver->SetMax( m_rect.b - m_rect.t - height);
  90. m_ver->SetPos(-m_rect.t);
  91. }
  92. // SetUpdate(&GUIPanel::InitFunc,ML_GUI5);
  93. SetUpdate(&GUIPanel::InitFunc,ML_GUI1);
  94. return true;
  95. }
  96. void GUIPanel::Restart()
  97. {
  98. Show(m_show);
  99. ScrollTo(0.0f,0.0f);
  100. }
  101. inline float cubic_curve(float t)
  102. {
  103. float t2 = t*t; return 3*t2 - 2*t2*t;
  104. }
  105. #ifdef not_def
  106. void _cdecl GUIPanel::Work(float deltaTime, long)
  107. {
  108. // Rect r; GetRect(r,false);
  109. Rect r; GetPanelRect(r);
  110. /* Rect t; GetParentRect(t);
  111. Rect c; GetParentClipRect(c);
  112. r.l += t.l - c.l; r.r += t.l - c.l;
  113. r.t += t.t - c.t; r.b += t.t - c.t;*/
  114. Rect clip; GetParentClipRect(clip);
  115. if( !RectInRect(
  116. clip.l,
  117. clip.t,
  118. clip.r,
  119. clip.b,r.l,r.t,r.r,r.b))
  120. return;
  121. if( !PointInRect(r.l,r.t,clip) ||
  122. !PointInRect(r.r,r.b,clip))
  123. SetClipRect(&clip);
  124. float x = r.l; float width = r.r - r.l;
  125. float y = r.t; float height = r.b - r.t;
  126. if( m_texture[0] )
  127. {
  128. m_quadRender.DrawQuad(x,y,width,height,0.0f,0.0f,
  129. 1.0f,1.0f,10.0f + m_alphaOriginal*m_texAlpha);
  130. }
  131. if( EditMode_IsOn() && m_useClipping && !m_texture[0] )
  132. {
  133. m_helperQuad.DrawQuad(x,y,width,height,0.0f,0.0f,
  134. 0.0f,0.0f,10.3f);
  135. }
  136. SetClipRect(NULL);
  137. if( GetFadeState() == BaseGUIElement::FadingIn )
  138. {
  139. m_fadeAlpha += (deltaTime/m_fadeTime)*m_alphaOriginal;
  140. if( m_fadeAlpha > m_alphaOriginal )
  141. {
  142. m_fadeAlpha = m_alphaOriginal;
  143. SetFadeState(FadingFinished);
  144. }
  145. }
  146. else
  147. if( GetFadeState() == BaseGUIElement::FadingOut )
  148. {
  149. m_fadeAlpha -= (deltaTime/m_fadeTime)*m_alphaOriginal;
  150. if( m_fadeAlpha < 0 )
  151. {
  152. m_fadeAlpha = 0.0f;
  153. SetFadeState(FadingFinished);
  154. Show(false);
  155. }
  156. }
  157. SetAlpha(m_fadeAlpha);
  158. const float AccelerationFactor = 1.0f;
  159. if( m_state == HScrolling )
  160. {
  161. m_curScrollTime += deltaTime;
  162. if( m_curScrollTime > m_scrollTime )
  163. {
  164. m_curScrollTime = m_scrollTime;
  165. m_state = Normal;
  166. }
  167. float k = m_curScrollTime/m_scrollTime;
  168. float x = Lerp(m_from.x,m_to.x,k);
  169. ScrollTo(x,m_to.y);
  170. }
  171. else
  172. if( m_state == VScrolling )
  173. {
  174. m_curScrollTime += deltaTime;
  175. if( m_curScrollTime > m_scrollTime )
  176. {
  177. m_curScrollTime = m_scrollTime;
  178. m_state = Normal;
  179. }
  180. float k = m_curScrollTime/m_scrollTime;
  181. float y = Lerp(m_from.y,m_to.y,k);
  182. ScrollTo(m_to.x,y);
  183. }
  184. }
  185. #endif
  186. void _cdecl GUIPanel::Work(float deltaTime, long)
  187. {
  188. if( EditMode_IsOn() && !EditMode_IsVisible())
  189. return;
  190. Draw();
  191. Update(deltaTime);
  192. }
  193. void GUIPanel::Update(float deltaTime)
  194. {
  195. if( GetFadeState() == BaseGUIElement::FadingIn )
  196. {
  197. m_fade -= deltaTime;
  198. if( m_fade < 0.0f )
  199. {
  200. SetFadeState(FadingFinished);
  201. m_fade = 0.0f;
  202. }
  203. m_fadeAlpha = 1.0f - m_fade/m_fadeTime;
  204. SetAlpha(m_fadeAlpha*m_alphaOriginal);
  205. }
  206. else
  207. if( GetFadeState() == BaseGUIElement::FadingOut )
  208. {
  209. m_fade -= deltaTime;
  210. if( m_fade < 0.0f )
  211. {
  212. SetFadeState(FadingFinished); Show(false);
  213. m_fade = 0.0f;
  214. }
  215. m_fadeAlpha = m_fade/m_fadeTime;
  216. SetAlpha(m_fadeAlpha*m_alphaOriginal);
  217. }
  218. // SetAlpha(m_fadeAlpha);
  219. const float AccelerationFactor = 1.0f;
  220. if( m_state == HScrolling )
  221. {
  222. m_curScrollTime += deltaTime;
  223. if( m_curScrollTime > m_scrollTime )
  224. {
  225. m_curScrollTime = m_scrollTime;
  226. m_state = Normal;
  227. }
  228. float k = m_curScrollTime/m_scrollTime;
  229. float x = Lerp(m_from.x,m_to.x,k);
  230. ScrollTo(x,m_to.y);
  231. }
  232. else
  233. if( m_state == VScrolling )
  234. {
  235. m_curScrollTime += deltaTime;
  236. if( m_curScrollTime > m_scrollTime )
  237. {
  238. m_curScrollTime = m_scrollTime;
  239. m_state = Normal;
  240. }
  241. float k = m_curScrollTime/m_scrollTime;
  242. float y = Lerp(m_from.y,m_to.y,k);
  243. ScrollTo(m_to.x,y);
  244. }
  245. if( preTime > 0.0f )
  246. {
  247. preTime -= deltaTime;
  248. if( preTime < 0.0f )
  249. preTime = 0.0f;
  250. float k = preTime/preTimeMax;
  251. float dx = preTo.x - preFrom.x;
  252. float dy = preTo.y - preFrom.y;
  253. float a = sinf(PI*Lerp(3.0f,-0.5f,k))*k*k;
  254. float x = preTo.x + a*dx;
  255. float y = preTo.y + a*dy;
  256. MoveTo(x,y);
  257. }
  258. }
  259. void GUIPanel::Draw()
  260. {
  261. if( InterfaceUtils::IsHide())
  262. return;
  263. // Rect r; GetRect(r,false);
  264. Rect r; GetPanelRect(r);
  265. /* Rect t; GetParentRect(t);
  266. Rect c; GetParentClipRect(c);
  267. r.l += t.l - c.l; r.r += t.l - c.l;
  268. r.t += t.t - c.t; r.b += t.t - c.t;*/
  269. Rect clip; GetParentClipRect(clip);
  270. if( !RectInRect(
  271. clip.l,
  272. clip.t,
  273. clip.r,
  274. clip.b,r.l,r.t,r.r,r.b))
  275. return;
  276. if( !PointInRect(r.l,r.t,clip) ||
  277. !PointInRect(r.r,r.b,clip))
  278. SetClipRect(&clip);
  279. float x = r.l; float width = r.r - r.l;
  280. float y = r.t; float height = r.b - r.t;
  281. if( m_texture[0] )
  282. {
  283. m_quadRender.DrawQuad(x,y,width,height,0.0f,0.0f,
  284. 1.0f,1.0f,10.0f + GetAlpha()*m_texAlpha);
  285. }
  286. if( EditMode_IsOn() && m_useClipping && !m_texture[0] )
  287. {
  288. m_helperQuad.DrawQuad(x,y,width,height,0.0f,0.0f,
  289. 0.0f,0.0f,10.3f);
  290. }
  291. SetClipRect(NULL);
  292. }
  293. void GUIPanel::Show(bool isShow)
  294. {
  295. BaseGUIElement::Show(isShow);
  296. if( EditMode_IsOn())
  297. DelUpdate(&GUIPanel::Work);
  298. if( IsShow())
  299. {
  300. // SetUpdate(&GUIPanel::Work,drawLevel);
  301. BaseGUIElement *parent = GetParent();
  302. long level = drawLevel;
  303. /* if( parent )
  304. level = parent->GetDrawLevel() + 100;*/
  305. if( GetAlpha() > 0.0f )
  306. {
  307. SetUpdate(&GUIPanel::Work,level + drawPriority);
  308. }
  309. }
  310. else
  311. {
  312. DelUpdate(&GUIPanel::Work);
  313. }
  314. }
  315. void GUIPanel::Activate(bool isActive)
  316. {
  317. BaseGUIElement::Activate(isActive);
  318. }
  319. void GUIPanel::Command(const char *id, dword numParams, const char **params)
  320. {
  321. if( string::IsEmpty(id))
  322. return;
  323. if( string::IsEqual(id,"hscroll"))
  324. {
  325. if( m_state != Normal )
  326. return;
  327. m_from.x = GetOffsetX();
  328. m_from.y = GetOffsetY();
  329. m_state = HScrolling;
  330. m_scrollLength = 5.0f;
  331. m_scrollTime = 1.0f;
  332. m_curScrollTime = 0.0f;
  333. if( numParams > 0 )
  334. m_scrollLength = (float)atof(params[0]);
  335. if( numParams > 1 )
  336. m_scrollTime = (float)atof(params[1]);
  337. m_to = m_from;
  338. m_to.x += m_scrollLength*m_aspect;
  339. m_rect = GetFullRect();
  340. if( m_to.x > -m_rect.l )
  341. m_to.x = -m_rect.l;
  342. Rect r; GetRect(r);
  343. float width = r.r - r.l;
  344. if( m_to.x < -m_rect.r + width )
  345. m_to.x = -m_rect.r + width;
  346. m_scrollTime *= fabsf((m_to.x - m_from.x)/(m_scrollLength*m_aspect));
  347. if( m_scrollTime == 0.0f )
  348. m_state = Normal;
  349. if( m_hor )
  350. {
  351. m_hor->SetMax( m_rect.r - m_rect.l - width);
  352. m_hor->SetPos(-m_rect.l - m_to.x);
  353. }
  354. return;
  355. }
  356. else
  357. if( string::IsEqual(id,"vscroll"))
  358. {
  359. if( m_state != Normal )
  360. return;
  361. m_from.x = GetOffsetX();
  362. m_from.y = GetOffsetY();
  363. m_state = VScrolling;
  364. m_scrollLength = 5.0f;
  365. m_scrollTime = 1.0f;
  366. m_curScrollTime = 0.0f;
  367. if( numParams > 0 )
  368. m_scrollLength = (float)atof(params[0]);
  369. if( numParams > 1 )
  370. m_scrollTime = (float)atof(params[1]);
  371. m_to = m_from;
  372. m_to.y += m_scrollLength;
  373. m_rect = GetFullRect();
  374. if( m_to.y > -m_rect.t )
  375. m_to.y = -m_rect.t;
  376. Rect r; GetRect(r);
  377. float height = r.b - r.t;
  378. if( m_to.y < -m_rect.b + height )
  379. m_to.y = -m_rect.b + height;
  380. if( m_ver )
  381. {
  382. m_ver->SetMax( m_rect.b - m_rect.t - height);
  383. m_ver->SetPos(-m_rect.t - m_to.y);
  384. }
  385. return;
  386. }
  387. else
  388. if( string::IsEqual(id,"fadein"))
  389. {
  390. Show(true);
  391. SetFadeState(FadingIn);
  392. m_fadeTime = 2.0f;
  393. if( numParams > 0 )
  394. m_fadeTime = (float)atof(params[0]);
  395. m_fade = m_fadeTime;
  396. return;
  397. }
  398. else
  399. if( string::IsEqual(id,"fadeout"))
  400. {
  401. SetFadeState(FadingOut);
  402. m_fadeTime = 2.0f;
  403. if( numParams > 0 )
  404. m_fadeTime = (float)atof(params[0]);
  405. m_fade = m_fadeTime;
  406. m_alphaOriginal = GetNativeAlpha();
  407. return;
  408. }
  409. else
  410. if( string::IsEqual(id,"move"))
  411. {
  412. float x = 0;
  413. float y = 0;
  414. if( numParams > 0 ) x = (float)atof(params[0]);
  415. if( numParams > 1 ) y = (float)atof(params[1]);
  416. MoveTo(x,y);
  417. }
  418. else
  419. if( string::IsEqual(id,"present"))
  420. {
  421. float dx = GetWidth();
  422. float dy = 0;
  423. if( numParams > 0 ) dx = (float)atof(params[0]);
  424. if( numParams > 1 ) dy = (float)atof(params[1]);
  425. float dt = 1.0f;
  426. if( numParams > 2 ) dt = (float)atof(params[2]);
  427. if( dt <= 0.0 )
  428. dt = 1.0f;
  429. preTimeMax = dt;
  430. preTime = dt;
  431. GetNativePos(preFrom.x,preFrom.y);
  432. preTo.x = preFrom.x + dx;
  433. preTo.y = preFrom.y + dy;
  434. }
  435. else
  436. {
  437. BaseGUIElement::Command(id,numParams,params);
  438. }
  439. }
  440. static char GUIDescription[] =
  441. "GUI Panel.\n\n"
  442. " Use it to group GUI items\n"
  443. " NOTE: all sizes-positions are relative (not pixel sizes-positions)\n\n"
  444. "Commands:\n\n"
  445. " hscroll [value] [time] - scrolls panel horizontally by value during time (default: 5,1)\n"
  446. " vscroll [value] [time] - scrolls panel vertically by value during time (default: 5,1)\n\n"
  447. " fadein [time] - fades panel and it's children in\n"
  448. " fadeout [time] - fades panel and it's children out\n"
  449. " move [x] [y] - move panel to new position (0,0 by default)\n"
  450. " present [x] [y] [time] - move panel with speceffect (<width>,0,1 by default)";
  451. MOP_BEGINLISTCG(GUIPanel, "GUI Panel", '1.00', 100, GUIDescription, "Interface")
  452. // MOP_GROUPBEG("Wide screen layout")
  453. // MOP_BOOL("Use shift aspect", false)
  454. // MOP_BOOL("Use width aspect", true)
  455. MOP_ENUMBEG("Layout")
  456. MOP_ENUMELEMENT("Left")
  457. MOP_ENUMELEMENT("Center")
  458. MOP_ENUMELEMENT("Right")
  459. MOP_ENUMEND
  460. MOP_ENUM("Layout", "Layout")
  461. // MOP_GROUPEND()
  462. MOP_FLOAT("X position", 25.0f)
  463. MOP_FLOAT("Y position", 25.0f)
  464. // MOP_FLOAT("Alpha", 1.0f)
  465. MOP_FLOATEX("Alpha", 1.0f, 0.0f, 1.0f)
  466. MOP_STRING("Parent id", "")
  467. MOP_FLOAT("Clip rect width", 50.0f)
  468. MOP_FLOAT("Clip rect height", 50.0f)
  469. MOP_BOOL("Use clip rect", false)
  470. MOP_STRING("Back texture", "")
  471. MOP_FLOAT ("Back alpha", 1.0f)
  472. MOP_LONG("Draw priority", 0)
  473. MOP_STRING("Hor bar", "")
  474. MOP_STRING("Ver bar", "")
  475. MOP_BOOL("Active" , true)
  476. MOP_BOOL("Visible", true)
  477. MOP_ENDLIST(GUIPanel)