uiscriptedmenu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. //-----------------------------------------------------------------------------
  2. class UIMenuPanel: Managed
  3. {
  4. proto native UIMenuPanel GetSubMenu();
  5. proto native UIMenuPanel GetParentMenu();
  6. proto native UIMenuPanel GetVisibleMenu();
  7. proto native void SetSubMenu(UIMenuPanel submenu);
  8. proto native void SetParentMenu(UIMenuPanel parent);
  9. proto native bool CanClose();
  10. proto native bool CanCloseOnEscape();
  11. //! Create & open menu with specific id (see \ref MenuID) and set this menu as its parent
  12. proto native UIScriptedMenu EnterScriptedMenu(int id);
  13. proto native void DestroySubmenu();
  14. proto native bool IsAnyMenuVisible();
  15. proto native bool IsVisible();
  16. proto native bool IsClosing();
  17. #ifdef FEATURE_CURSOR
  18. //! Signal when the menu is created through 'UIManager.CreateScriptedMenu'
  19. proto native bool IsCreatedHidden();
  20. #endif
  21. //! If visibility of application is changed. On console it is called when application is suspended or constrained.
  22. //! @param isVisible indicate if application is visible in foreground
  23. void OnVisibilityChanged(bool isVisible)
  24. {
  25. }
  26. //! Safe way to close window, using this function can even window safely close itself
  27. proto native void Close();
  28. bool UseMouse() {
  29. #ifdef PLATFORM_CONSOLE
  30. return GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer();
  31. #else
  32. return true;
  33. #endif
  34. }
  35. bool UseKeyboard() {
  36. #ifdef PLATFORM_CONSOLE
  37. return GetGame().GetInput().IsEnabledMouseAndKeyboardEvenOnServer();
  38. #else
  39. return true;
  40. #endif
  41. }
  42. bool UseGamepad() {
  43. return true;
  44. }
  45. //! Returns \ref MenuID
  46. int GetID() {
  47. return MENU_UNKNOWN;
  48. }
  49. //! Refresh request, called from anywhere
  50. void Refresh()
  51. {
  52. }
  53. };
  54. //-----------------------------------------------------------------------------
  55. //! Part of main menu hierarchy to create custom menus from script
  56. class UIScriptedMenu extends UIMenuPanel
  57. {
  58. int m_id;
  59. Widget layoutRoot;
  60. private Widget m_AnimAlphaWidget;
  61. private bool m_AnimAlphaIsIncreasing;
  62. private float m_AnimAlphaValue;
  63. private ScriptInvoker m_PlayerDeathInvoker; //DayZPlayer::GetOnDeathStart -> used to keep track of and ensure proper callback handling
  64. Widget GetLayoutRoot()
  65. {
  66. return layoutRoot;
  67. }
  68. void LockControls()
  69. {
  70. #ifdef FEATURE_CURSOR
  71. if (IsCreatedHidden())
  72. return;
  73. #endif
  74. if (UseMouse())
  75. {
  76. GetGame().GetInput().ChangeGameFocus(1, INPUT_DEVICE_MOUSE);
  77. GetGame().GetUIManager().ShowUICursor(true);
  78. }
  79. if (UseKeyboard())
  80. {
  81. GetGame().GetInput().ChangeGameFocus(1, INPUT_DEVICE_KEYBOARD);
  82. }
  83. if (UseGamepad())
  84. {
  85. GetGame().GetInput().ChangeGameFocus(1, INPUT_DEVICE_GAMEPAD);
  86. }
  87. }
  88. void UnlockControls()
  89. {
  90. #ifdef FEATURE_CURSOR
  91. if (IsCreatedHidden())
  92. return;
  93. #endif
  94. if (UseMouse())
  95. {
  96. GetGame().GetInput().ChangeGameFocus(-1, INPUT_DEVICE_MOUSE);
  97. }
  98. if (GetParentMenu() && GetParentMenu().UseMouse())
  99. {
  100. GetGame().GetUIManager().ShowUICursor(true);
  101. }
  102. else
  103. {
  104. GetGame().GetUIManager().ShowUICursor(false);
  105. }
  106. if(UseKeyboard())
  107. {
  108. GetGame().GetInput().ChangeGameFocus(-1, INPUT_DEVICE_KEYBOARD);
  109. }
  110. if(UseGamepad())
  111. {
  112. GetGame().GetInput().ChangeGameFocus(-1, INPUT_DEVICE_GAMEPAD);
  113. }
  114. }
  115. void UIScriptedMenu()
  116. {
  117. m_id = MENU_UNKNOWN;
  118. }
  119. void ~UIScriptedMenu()
  120. {
  121. }
  122. //! Sets \ref MenuID
  123. void SetID(int id) {
  124. m_id = id;
  125. }
  126. //! Returns \ref MenuID
  127. override int GetID() {
  128. return m_id;
  129. }
  130. void SetWidgetAnimAlpha( Widget widget )
  131. {
  132. m_AnimAlphaValue = 0.3;
  133. m_AnimAlphaWidget = widget;
  134. }
  135. //create widgets here and return layout root Widget
  136. //widgets will be destroyed automatically by c++ side
  137. Widget Init()
  138. {
  139. return NULL;
  140. }
  141. void Cleanup()
  142. {
  143. }
  144. //after show
  145. void OnShow()
  146. {
  147. LockControls();
  148. if (IsHandlingPlayerDeathEvent() && g_Game && g_Game.GetPlayer())
  149. {
  150. m_PlayerDeathInvoker = g_Game.GetPlayer().GetOnDeathStart();
  151. m_PlayerDeathInvoker.Insert( OnPlayerDeath );
  152. }
  153. }
  154. //after hide
  155. void OnHide()
  156. {
  157. UnlockControls();
  158. if (m_PlayerDeathInvoker) // Only ever registered while `IsHandlingPlayerDeathEvent`. Remove callback directly.
  159. {
  160. m_PlayerDeathInvoker.Remove( OnPlayerDeath, EScriptInvokerRemoveFlags.NONE );
  161. m_PlayerDeathInvoker = null;
  162. }
  163. }
  164. //! Per frame update, called from engine
  165. void Update(float timeslice)
  166. {
  167. #ifdef PLATFORM_CONSOLE
  168. if ( m_AnimAlphaWidget )
  169. {
  170. float anim_speed = 1.2;
  171. float anim_value_max = 1.0;
  172. float anim_value_min = 0.3;
  173. if ( m_AnimAlphaIsIncreasing )
  174. {
  175. m_AnimAlphaValue += anim_speed * timeslice;
  176. if ( m_AnimAlphaValue >= anim_value_max )
  177. {
  178. m_AnimAlphaValue = anim_value_max;
  179. m_AnimAlphaIsIncreasing = false;
  180. }
  181. }
  182. else
  183. {
  184. m_AnimAlphaValue -= anim_speed * timeslice;
  185. if ( m_AnimAlphaValue <= anim_value_min )
  186. {
  187. m_AnimAlphaValue = anim_value_min;
  188. m_AnimAlphaIsIncreasing = true;
  189. }
  190. }
  191. m_AnimAlphaWidget.SetAlpha( m_AnimAlphaValue );
  192. }
  193. #endif
  194. }
  195. // Moved to parent
  196. //! Refresh request, called from anywhere
  197. //void Refresh()
  198. //{
  199. //}
  200. proto native void SetFadingPanels(Widget panel0, Widget panel1, Widget panel2, Widget panel3, Widget panel4);
  201. bool OnClick(Widget w, int x, int y, int button)
  202. {
  203. if ( UIScriptedWindow.GetActiveWindows() )
  204. {
  205. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  206. {
  207. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnClick( w, x, y, button ) )
  208. {
  209. return true;
  210. }
  211. }
  212. }
  213. return false;
  214. }
  215. bool OnModalResult(Widget w, int x, int y, int code, int result)
  216. {
  217. if ( UIScriptedWindow.GetActiveWindows() )
  218. {
  219. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  220. {
  221. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnModalResult( w, x, y, code, result ) )
  222. {
  223. return true;
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229. bool OnDoubleClick(Widget w, int x, int y, int button)
  230. {
  231. if ( UIScriptedWindow.GetActiveWindows() )
  232. {
  233. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  234. {
  235. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnDoubleClick( w, x, y, button ) )
  236. {
  237. return true;
  238. }
  239. }
  240. }
  241. return false;
  242. }
  243. bool OnSelect(Widget w, int x, int y)
  244. {
  245. if ( UIScriptedWindow.GetActiveWindows() )
  246. {
  247. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  248. {
  249. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnSelect( w, x, y ) )
  250. {
  251. return true;
  252. }
  253. }
  254. }
  255. return false;
  256. }
  257. bool OnItemSelected(Widget w, int x, int y, int row, int column, int oldRow, int oldColumn)
  258. {
  259. if ( UIScriptedWindow.GetActiveWindows() )
  260. {
  261. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  262. {
  263. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnItemSelected( w, x, y, row, column, oldRow, oldColumn ) )
  264. {
  265. return true;
  266. }
  267. }
  268. }
  269. return false;
  270. }
  271. bool OnFocus(Widget w, int x, int y)
  272. {
  273. if ( UIScriptedWindow.GetActiveWindows() )
  274. {
  275. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  276. {
  277. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnFocus( w, x, y ) )
  278. {
  279. return true;
  280. }
  281. }
  282. }
  283. return false;
  284. }
  285. bool OnFocusLost(Widget w, int x, int y)
  286. {
  287. if ( UIScriptedWindow.GetActiveWindows() )
  288. {
  289. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  290. {
  291. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnFocusLost( w, x, y ) )
  292. {
  293. return true;
  294. }
  295. }
  296. }
  297. return false;
  298. }
  299. bool OnMouseEnter(Widget w, int x, int y)
  300. {
  301. if ( UIScriptedWindow.GetActiveWindows() )
  302. {
  303. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  304. {
  305. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnMouseEnter( w, x, y ) )
  306. {
  307. return true;
  308. }
  309. }
  310. }
  311. return false;
  312. }
  313. bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  314. {
  315. if ( UIScriptedWindow.GetActiveWindows() )
  316. {
  317. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  318. {
  319. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnMouseLeave( w, enterW, x, y ) )
  320. {
  321. return true;
  322. }
  323. }
  324. }
  325. return false;
  326. }
  327. bool OnMouseButtonDown(Widget w, int x, int y, int button)
  328. {
  329. if ( UIScriptedWindow.GetActiveWindows() )
  330. {
  331. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  332. {
  333. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnMouseButtonDown( w, x, y, button ) )
  334. {
  335. return true;
  336. }
  337. }
  338. }
  339. return false;
  340. }
  341. bool OnMouseButtonUp(Widget w, int x, int y, int button)
  342. {
  343. if ( UIScriptedWindow.GetActiveWindows() )
  344. {
  345. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  346. {
  347. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnMouseButtonUp( w, x, y, button ) )
  348. {
  349. return true;
  350. }
  351. }
  352. }
  353. return false;
  354. }
  355. bool OnMouseWheel(Widget w, int x, int y, int wheel)
  356. {
  357. if ( UIScriptedWindow.GetActiveWindows() )
  358. {
  359. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  360. {
  361. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnMouseWheel( w, x, y, wheel ) )
  362. {
  363. return true;
  364. }
  365. }
  366. }
  367. return false;
  368. }
  369. bool OnController(Widget w, int control, int value)
  370. {
  371. if ( UIScriptedWindow.GetActiveWindows() )
  372. {
  373. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  374. {
  375. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnController( w, control, value ) )
  376. {
  377. return true;
  378. }
  379. }
  380. }
  381. return false;
  382. }
  383. bool OnKeyDown(Widget w, int x, int y, int key)
  384. {
  385. if ( UIScriptedWindow.GetActiveWindows() )
  386. {
  387. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  388. {
  389. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnKeyDown( w, x, y, key ) )
  390. {
  391. return true;
  392. }
  393. }
  394. }
  395. return false;
  396. }
  397. bool OnKeyUp(Widget w, int x, int y, int key)
  398. {
  399. if ( UIScriptedWindow.GetActiveWindows() )
  400. {
  401. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  402. {
  403. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnKeyUp( w, x, y, key ) )
  404. {
  405. return true;
  406. }
  407. }
  408. }
  409. return false;
  410. }
  411. bool OnKeyPress(Widget w, int x, int y, int key)
  412. {
  413. if ( UIScriptedWindow.GetActiveWindows() )
  414. {
  415. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  416. {
  417. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnKeyPress( w, x, y, key ) )
  418. {
  419. return true;
  420. }
  421. }
  422. }
  423. return false;
  424. }
  425. bool OnChange(Widget w, int x, int y, bool finished)
  426. {
  427. if ( UIScriptedWindow.GetActiveWindows() )
  428. {
  429. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  430. {
  431. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnChange( w, x, y, finished ) )
  432. {
  433. return true;
  434. }
  435. }
  436. }
  437. return false;
  438. }
  439. bool OnDrag(Widget w, int x, int y)
  440. {
  441. if ( UIScriptedWindow.GetActiveWindows() )
  442. {
  443. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  444. {
  445. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnDrag( w, x, y ) )
  446. {
  447. return true;
  448. }
  449. }
  450. }
  451. return false;
  452. }
  453. bool OnDragging(Widget w, int x, int y, Widget reciever)
  454. {
  455. if ( UIScriptedWindow.GetActiveWindows() )
  456. {
  457. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  458. {
  459. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnDragging( w, x, y, reciever ) )
  460. {
  461. return true;
  462. }
  463. }
  464. }
  465. return false;
  466. }
  467. bool OnDraggingOver(Widget w, int x, int y, Widget reciever)
  468. {
  469. if ( UIScriptedWindow.GetActiveWindows() )
  470. {
  471. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  472. {
  473. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnDraggingOver( w, x, y, reciever ) )
  474. {
  475. return true;
  476. }
  477. }
  478. }
  479. return false;
  480. }
  481. bool OnDrop(Widget w, int x, int y, Widget reciever)
  482. {
  483. if ( UIScriptedWindow.GetActiveWindows() )
  484. {
  485. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  486. {
  487. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnDrop( w, x, y, reciever ) )
  488. {
  489. return true;
  490. }
  491. }
  492. }
  493. return false;
  494. }
  495. bool OnDropReceived(Widget w, int x, int y, Widget reciever)
  496. {
  497. if ( UIScriptedWindow.GetActiveWindows() )
  498. {
  499. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  500. {
  501. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnDropReceived( w, x, y, reciever ) )
  502. {
  503. return true;
  504. }
  505. }
  506. }
  507. return false;
  508. }
  509. bool OnEvent(EventType eventType, Widget target, int parameter0, int parameter1)
  510. {
  511. if ( UIScriptedWindow.GetActiveWindows() )
  512. {
  513. for ( int i = 0; i < UIScriptedWindow.GetActiveWindows().Count(); i++ )
  514. {
  515. if ( UIScriptedWindow.GetActiveWindows().GetElement( i ).OnEvent( eventType, target, parameter0, parameter1 ) )
  516. {
  517. return true;
  518. }
  519. }
  520. }
  521. return false;
  522. }
  523. ScriptedWidgetEventHandler GetContextMenu()
  524. {
  525. return null;
  526. }
  527. bool OnXboxEvent(int xboxEvent)
  528. {
  529. return true;
  530. }
  531. void OnRPC(ParamsReadContext ctx){}
  532. void OnRPCEx(int rpc_type, ParamsReadContext ctx){}
  533. void InitNoteWrite(EntityAI paper, EntityAI pen, string text = "") {}
  534. void InitNoteRead(string text = "") {}
  535. void InitMapItem(EntityAI item) {}
  536. void LoadMapMarkers() {}
  537. bool IsHandlingPlayerDeathEvent()
  538. {
  539. return true;
  540. }
  541. void OnPlayerDeath()
  542. {
  543. Close();
  544. }
  545. };