playerlistscriptedwidget.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. class PlayerListScriptedWidget extends ScriptedWidgetEventHandler
  2. {
  3. protected Widget m_Root;
  4. protected ScrollWidget m_ScrollContainer;
  5. protected Widget m_Content;
  6. protected ref map<string, ref PlayerListEntryScriptedWidget> m_Entries;
  7. protected int m_TotalEntries;
  8. protected PlayerListEntryScriptedWidget m_SelectedEntry;
  9. void PlayerListScriptedWidget( Widget parent, string header_text = "" )
  10. {
  11. m_Root = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/xbox/ingamemenu_xbox/players_info_panel.layout", parent );
  12. m_ScrollContainer = ScrollWidget.Cast( m_Root.FindAnyWidget( "ScrollFrame" ) );
  13. m_Content = m_Root.FindAnyWidget( "Content" );
  14. m_Entries = new map<string, ref PlayerListEntryScriptedWidget>;
  15. m_ScrollContainer.VScrollToPos01( 0 );
  16. }
  17. void ~PlayerListScriptedWidget()
  18. {
  19. delete m_Root;
  20. }
  21. void FocusFirst()
  22. {
  23. if ( m_Content && m_Content.GetChildren() )
  24. SetFocus( m_Content.GetChildren().FindAnyWidget( "Button" ) );
  25. m_ScrollContainer.VScrollToPos01( 0 );
  26. }
  27. void Reload( SyncPlayerList player_list )
  28. {
  29. if ( player_list && player_list.m_PlayerList && m_Entries )
  30. {
  31. foreach ( string UID, PlayerListEntryScriptedWidget widget : m_Entries )
  32. {
  33. SyncPlayer player_found;
  34. foreach ( SyncPlayer player : player_list.m_PlayerList )
  35. {
  36. if ( player && player.m_UID == UID )
  37. {
  38. player_found = player;
  39. break;
  40. }
  41. }
  42. if ( !player_found )
  43. {
  44. RemovePlayer( UID );
  45. }
  46. }
  47. for ( int i = 0; i < player_list.m_PlayerList.Count(); i++ )
  48. {
  49. SyncPlayer player2 = player_list.m_PlayerList.Get( i );
  50. PlayerListEntryScriptedWidget player_widget;
  51. m_Entries.Find( player2.m_UID, player_widget );
  52. if ( !player_widget )
  53. {
  54. AddPlayer( player2.m_PlayerName, player2.m_UID, true );
  55. }
  56. }
  57. }
  58. }
  59. bool IsEmpty()
  60. {
  61. return ( m_Entries.Count() == 0 );
  62. }
  63. void OnLoadServersAsync( GetServersResult result_list, EBiosError error, string response )
  64. {
  65. }
  66. void Reload( BiosFriendInfoArray player_list )
  67. {
  68. if ( player_list && m_Entries )
  69. {
  70. foreach ( string UID, PlayerListEntryScriptedWidget widget : m_Entries )
  71. {
  72. BiosFriendInfo player_found;
  73. int j = 0;
  74. while ( !player_found && j < player_list.Count() )
  75. {
  76. if ( player_list[j].m_Uid == UID )
  77. player_found = player_list[j];
  78. j++;
  79. }
  80. if ( !player_found )
  81. {
  82. RemovePlayer( UID );
  83. }
  84. }
  85. for ( int i = 0; i < player_list.Count(); i++ )
  86. {
  87. BiosFriendInfo player2 = player_list.Get( i );
  88. PlayerListEntryScriptedWidget player_widget;
  89. m_Entries.Find( player2.m_Uid, player_widget );
  90. if ( !player_widget )
  91. {
  92. AddPlayer( player2.m_DisplayName, player2.m_Uid, false );
  93. }
  94. }
  95. }
  96. }
  97. void Reload( BiosPrivacyUidResultArray player_list )
  98. {
  99. foreach ( BiosPrivacyUidResult result : player_list )
  100. {
  101. PlayerListEntryScriptedWidget player_widget;
  102. m_Entries.Find( result.m_Uid, player_widget );
  103. if ( player_widget )
  104. {
  105. player_widget.LoadPermissions( result.m_Results );
  106. }
  107. }
  108. }
  109. void ReloadLocal( map<string, bool> player_list )
  110. {
  111. if ( player_list )
  112. {
  113. for ( int i = 0; i < player_list.Count(); i++ )
  114. {
  115. string uid = player_list.GetKey( i );
  116. bool muted = OnlineServices.IsPlayerMuted( uid );
  117. PlayerListEntryScriptedWidget player_widget;
  118. m_Entries.Find( uid, player_widget );
  119. if ( player_widget )
  120. {
  121. player_widget.SetMute( muted );
  122. }
  123. }
  124. }
  125. }
  126. PlayerListEntryScriptedWidget FindEntryByWidget( Widget button )
  127. {
  128. if ( button && m_Entries )
  129. {
  130. foreach ( string UID, PlayerListEntryScriptedWidget widget : m_Entries )
  131. {
  132. if ( widget && widget.GetButtonWidget() == button )
  133. {
  134. return widget;
  135. }
  136. }
  137. }
  138. return null;
  139. }
  140. string FindPlayerByWidget( Widget button )
  141. {
  142. if ( button && m_Entries )
  143. {
  144. foreach ( string UID, PlayerListEntryScriptedWidget widget : m_Entries )
  145. {
  146. if ( widget && widget.GetButtonWidget() == button )
  147. {
  148. return UID;
  149. }
  150. }
  151. }
  152. return "";
  153. }
  154. void AddPlayer( string name, string UID, bool show_permissions )
  155. {
  156. if ( m_Entries )
  157. {
  158. m_Entries.Insert( UID, new PlayerListEntryScriptedWidget( m_Content, name, UID, show_permissions, this ) );
  159. m_TotalEntries++;
  160. }
  161. }
  162. void RemovePlayer(string UID)
  163. {
  164. if (m_Entries)
  165. {
  166. PlayerListEntryScriptedWidget next_entry;
  167. if (m_Entries.Get(UID) == m_SelectedEntry)
  168. {
  169. for (int i = 0; i < m_Entries.Count() - 1; i++)
  170. {
  171. if (m_Entries.GetElement(i) != m_Entries.Get(UID))
  172. continue;
  173. // Select next possibe player entry after the one to delete if possible
  174. if (i + 1 <= m_Entries.Count() - 1)
  175. {
  176. next_entry = m_Entries.GetElement(i + 1);
  177. }
  178. }
  179. }
  180. m_Entries.Remove(UID);
  181. m_TotalEntries--;
  182. // If there is no other possibe player entry after the current one to select we try to select the first indexed one
  183. if (!next_entry)
  184. {
  185. next_entry = m_Entries.GetElement(0);
  186. }
  187. // If we found a next entry to select from we select it in the UI class and focus on it
  188. // if not we only deselect the current one as there is no other possible entry to focus on
  189. if (next_entry)
  190. {
  191. SelectPlayer(next_entry);
  192. }
  193. else
  194. {
  195. if (m_SelectedEntry)
  196. m_SelectedEntry.Deselect();
  197. }
  198. m_Content.Update();
  199. }
  200. }
  201. bool IsMuted( string UID )
  202. {
  203. if ( m_Entries && m_Entries.Get( UID ) )
  204. {
  205. return m_Entries.Get( UID ).IsMuted();
  206. }
  207. return false;
  208. }
  209. bool IsGloballyMuted( string UID )
  210. {
  211. if ( m_Entries && m_Entries.Get( UID ) )
  212. {
  213. return m_Entries.Get( UID ).IsGloballyMuted();
  214. }
  215. return false;
  216. }
  217. void SetMute( string UID, bool mute )
  218. {
  219. if ( m_Entries && m_Entries.Get( UID ) )
  220. {
  221. m_Entries.Get( UID ).SetMute( mute );
  222. }
  223. }
  224. void ToggleMute( string UID )
  225. {
  226. if ( m_Entries && m_Entries.Get( UID ) )
  227. {
  228. m_Entries.Get( UID ).ToggleMute();
  229. }
  230. }
  231. PlayerListEntryScriptedWidget GetSelectedPlayer()
  232. {
  233. return m_SelectedEntry;
  234. }
  235. void SelectPlayer(PlayerListEntryScriptedWidget entry)
  236. {
  237. if (m_SelectedEntry)
  238. {
  239. m_SelectedEntry.Deselect();
  240. }
  241. m_SelectedEntry = entry;
  242. #ifdef PLATFORM_CONSOLE
  243. // Focus on new selected entry as OnFocus event function will handle the rest
  244. m_SelectedEntry.Focus();
  245. #endif
  246. if (GetGame().GetUIManager().GetMenu())
  247. GetGame().GetUIManager().GetMenu().Refresh();
  248. ScrollToEntry(m_SelectedEntry);
  249. }
  250. void ScrollToEntry( PlayerListEntryScriptedWidget entry )
  251. {
  252. if ( entry )
  253. {
  254. float x, y;
  255. float x_s, y_s;
  256. float x_l, y_l;
  257. Widget root = entry.GetButtonWidget().GetParent();
  258. Widget first_child = root.GetParent().GetChildren();
  259. Widget last_child = first_child;
  260. while ( last_child )
  261. {
  262. if ( last_child.GetSibling() )
  263. last_child = last_child.GetSibling();
  264. else
  265. break;
  266. }
  267. root.GetParent().Update();
  268. root.Update();
  269. m_ScrollContainer.GetScreenPos( x, y );
  270. m_ScrollContainer.GetScreenSize( x_s, y_s );
  271. float bottom_pos = y + y_s;
  272. root.GetScreenPos( x_l, y_l );
  273. root.GetScreenSize( x_s, y_s );
  274. if ( root == first_child )
  275. {
  276. m_ScrollContainer.VScrollToPos01( 0 );
  277. }
  278. else if ( root == last_child )
  279. {
  280. m_ScrollContainer.VScrollToPos01( 1 );
  281. }
  282. else if ( y_l + y_s >= bottom_pos )
  283. {
  284. m_ScrollContainer.VScrollToPos( m_ScrollContainer.GetVScrollPos() + y_s );
  285. }
  286. else if ( y_l <= y )
  287. {
  288. m_ScrollContainer.VScrollToPos( m_ScrollContainer.GetVScrollPos() - y_s );
  289. }
  290. }
  291. }
  292. }