uiscriptedmenu.c 13 KB

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