uiscriptedmenu.c 13 KB

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