playerlistscriptedwidget.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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( ref 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. {
  173. next_entry = m_Entries.GetElement( i + 1 );
  174. }
  175. }
  176. }
  177. m_Entries.Remove( UID );
  178. m_TotalEntries--;
  179. SelectPlayer( next_entry );
  180. m_Content.Update();
  181. }
  182. }
  183. bool IsMuted( string UID )
  184. {
  185. if ( m_Entries && m_Entries.Get( UID ) )
  186. {
  187. return m_Entries.Get( UID ).IsMuted();
  188. }
  189. return false;
  190. }
  191. bool IsGloballyMuted( string UID )
  192. {
  193. if ( m_Entries && m_Entries.Get( UID ) )
  194. {
  195. return m_Entries.Get( UID ).IsGloballyMuted();
  196. }
  197. return false;
  198. }
  199. void SetMute( string UID, bool mute )
  200. {
  201. if ( m_Entries && m_Entries.Get( UID ) )
  202. {
  203. m_Entries.Get( UID ).SetMute( mute );
  204. }
  205. }
  206. void ToggleMute( string UID )
  207. {
  208. if ( m_Entries && m_Entries.Get( UID ) )
  209. {
  210. m_Entries.Get( UID ).ToggleMute();
  211. }
  212. }
  213. PlayerListEntryScriptedWidget GetSelectedPlayer()
  214. {
  215. return m_SelectedEntry;
  216. }
  217. void SelectPlayer( PlayerListEntryScriptedWidget entry )
  218. {
  219. if ( m_SelectedEntry )
  220. m_SelectedEntry.Deselect();
  221. m_SelectedEntry = entry;
  222. if ( GetGame().GetUIManager().GetMenu() )
  223. GetGame().GetUIManager().GetMenu().Refresh();
  224. ScrollToEntry( entry );
  225. }
  226. void ScrollToEntry( PlayerListEntryScriptedWidget entry )
  227. {
  228. if ( entry )
  229. {
  230. float x, y;
  231. float x_s, y_s;
  232. float x_l, y_l;
  233. Widget root = entry.GetButtonWidget().GetParent();
  234. Widget first_child = root.GetParent().GetChildren();
  235. Widget last_child = first_child;
  236. while ( last_child )
  237. {
  238. if ( last_child.GetSibling() )
  239. last_child = last_child.GetSibling();
  240. else
  241. break;
  242. }
  243. root.GetParent().Update();
  244. root.Update();
  245. m_ScrollContainer.GetScreenPos( x, y );
  246. m_ScrollContainer.GetScreenSize( x_s, y_s );
  247. float bottom_pos = y + y_s;
  248. root.GetScreenPos( x_l, y_l );
  249. root.GetScreenSize( x_s, y_s );
  250. if ( root == first_child )
  251. {
  252. m_ScrollContainer.VScrollToPos01( 0 );
  253. }
  254. else if ( root == last_child )
  255. {
  256. m_ScrollContainer.VScrollToPos01( 1 );
  257. }
  258. else if ( y_l + y_s >= bottom_pos )
  259. {
  260. m_ScrollContainer.VScrollToPos( m_ScrollContainer.GetVScrollPos() + y_s );
  261. }
  262. else if ( y_l <= y )
  263. {
  264. m_ScrollContainer.VScrollToPos( m_ScrollContainer.GetVScrollPos() - y_s );
  265. }
  266. }
  267. }
  268. }