sr2lua/mp_lobby_players.lua

  1. -- Variable section 
  2. Mp_lobby_players_data = { 
  3. 	player_data = {}, 
  4. } 
  5.  
  6. -- Init and cleanup functions 
  7. function mp_lobby_players_init() 
  8.  
  9. 	-- Set current line 
  10. 	Mp_lobby_players_data.current_line = 1 
  11.  
  12. 	-- Find elements 
  13. 	Mp_lobby_players_data.line_h = vint_object_find("mp_lobby_line") 
  14. 	Mp_lobby_players_data.pulsate_anim_h = vint_object_find("player_pulsate_anim") 
  15. 	vint_set_property(Mp_lobby_players_data.pulsate_anim_h,"is_paused", true) 
  16.  
  17. 	Mp_lobby_players_data.fade_in_anim_h = vint_object_find("highlight_fade_in") 
  18. 	vint_set_property(Mp_lobby_players_data.fade_in_anim_h,"is_paused", true) 
  19. 	 
  20. 	Mp_lobby_players_data.fade_out_anim_h = vint_object_find("highlight_fade_out") 
  21. 	vint_set_property(Mp_lobby_players_data.fade_out_anim_h,"is_paused", true) 
  22. 	 
  23. 	-- Subscribe to updates 
  24. 	vint_datagroup_add_subscription("sr2_multi_lobby_player_data", "update", "mp_lobby_players_data_update") 
  25. 	vint_datagroup_add_subscription("sr2_multi_lobby_player_data", "insert", "mp_lobby_players_data_update") 
  26. 	vint_datagroup_add_subscription("sr2_multi_lobby_player_data", "remove", "mp_lobby_players_data_update") 
  27. 	 
  28. 	Mp_lobby_players_data.input_subs = { 
  29. 		vint_subscribe_to_input_event(nil, "nav_up",		"mp_lobby_players_nav_up", 50), 
  30. 		vint_subscribe_to_input_event(nil, "nav_down", 	"mp_lobby_players_nav_down", 50), 
  31. 		vint_subscribe_to_input_event(nil, "select",		"mp_lobby_players_select", 50) 
  32. 	} 
  33. end 
  34.  
  35. function mp_lobby_players_cleanup() 
  36. 	 
  37. 	--Cleanup cloned animations 
  38. 	for idx, val in Mp_lobby_players_data.player_data do 
  39. 		vint_object_destroy(val.pulsate_anim_h) 
  40. 		vint_object_destroy(val.fade_in_anim_h) 
  41. 		vint_object_destroy(val.fade_out_anim_h) 
  42. 	end 
  43. 	 
  44. 	-- Unsubscribt to updates 
  45. 	vint_datagroup_remove_subscription("sr2_multi_lobby_player_data", "update", "mp_lobby_players_data_update") 
  46. 	vint_datagroup_remove_subscription("sr2_multi_lobby_player_data", "insert", "mp_lobby_players_data_update") 
  47. 	vint_datagroup_remove_subscription("sr2_multi_lobby_player_data", "remove", "mp_lobby_players_data_update") 
  48.  
  49. 	-- Kill the player info popup 
  50. 	if vint_document_find("mp_player_info_popup") ~= nil then 
  51. 		vint_document_unload(vint_document_find("mp_player_info_popup")) 
  52. 	end 
  53. 	 
  54. 	-- Unsubscribe to input events 
  55. 	for idx, val in Mp_lobby_players_data.input_subs do 
  56. 		vint_unsubscribe_to_input_event(val) 
  57. 	end 
  58. end 
  59.  
  60. function mp_lobby_players_nav_up(target, event, accelleration) 
  61. 	-- Move cursor up 
  62. 	mp_lobby_players_update_highlight(-1) 
  63. 	 
  64. 	if Mp_lobby_players_data.num_lines > 1 then 
  65. 		audio_play(Menu_sound_item_nav) 
  66. 	end 
  67. end 
  68.  
  69. function mp_lobby_players_nav_down(target, event, accelleration) 
  70. 	-- Move cursor down 
  71. 	mp_lobby_players_update_highlight(1) 
  72. 	 
  73. 	if Mp_lobby_players_data.num_lines > 1 then 
  74. 		audio_play(Menu_sound_item_nav) 
  75. 	end 
  76. end 
  77.  
  78. function mp_lobby_players_select(target, event, accelleration) 
  79. 	-- Select current player 
  80. 	for idx, val in Mp_lobby_players_data.player_data do 
  81. 		if Mp_lobby_players_data.player_data[idx].physical_line == Mp_lobby_players_data.current_line then 
  82. 			if get_platform() == "PC" then 
  83. 				if get_is_matchmaking() == true then 
  84. 					mp_popup_open(Mp_lobby_players_data.player_data[idx].handle) 
  85. 				end 
  86. 			else 
  87. 				if get_is_syslink() == false then 
  88. 					mp_popup_open(Mp_lobby_players_data.player_data[idx].handle) 
  89. 				end 
  90. 			end 
  91. 			 
  92. 			break 
  93. 		end 
  94. 	end 
  95. end 
  96.  
  97. -- Highlight 
  98. function mp_lobby_players_update_highlight(idir) 
  99. 	 
  100. 	local old_physical_line = {} 
  101. 	local new_physical_line = {} 
  102. 	 
  103. 	local old_line = Mp_lobby_players_data.current_line 
  104. 	 
  105. 	-- Find old line object 
  106. 	for idx, val in Mp_lobby_players_data.player_data do 
  107. 		if Mp_lobby_players_data.player_data[idx].physical_line == Mp_lobby_players_data.current_line then 
  108. 			old_physical_line.fade_in_anim_h = Mp_lobby_players_data.player_data[idx].fade_in_anim_h 
  109. 			old_physical_line.fade_out_anim_h = Mp_lobby_players_data.player_data[idx].fade_out_anim_h 
  110. 			old_physical_line.pulsate_anim_h = Mp_lobby_players_data.player_data[idx].pulsate_anim_h 
  111. 		end 
  112. 	end 
  113. 	 
  114. 	-- Set new line value 
  115. 	Mp_lobby_players_data.current_line = Mp_lobby_players_data.current_line + idir 
  116. 	 
  117. 	if Mp_lobby_players_data.current_line > Mp_lobby_players_data.num_lines then 
  118. 		Mp_lobby_players_data.current_line = 1 
  119. 	elseif Mp_lobby_players_data.current_line < 1 then 
  120. 		Mp_lobby_players_data.current_line = Mp_lobby_players_data.num_lines 
  121. 	end	 
  122.  
  123. 	-- Find new line object 
  124. 	for idx, val in Mp_lobby_players_data.player_data do 
  125. 		if Mp_lobby_players_data.player_data[idx].physical_line == Mp_lobby_players_data.current_line then 
  126. 			new_physical_line.fade_in_anim_h = Mp_lobby_players_data.player_data[idx].fade_in_anim_h 
  127. 			new_physical_line.fade_out_anim_h = Mp_lobby_players_data.player_data[idx].fade_out_anim_h 
  128. 			new_physical_line.pulsate_anim_h = Mp_lobby_players_data.player_data[idx].pulsate_anim_h 
  129. 		end 
  130. 	end 
  131.  
  132. 	if old_line ~= Mp_lobby_players_data.current_line then 
  133. 		--Pause all related animations 
  134. 		vint_set_property(old_physical_line.fade_in_anim_h, "is_paused", true) 
  135. 		vint_set_property(old_physical_line.fade_out_anim_h, "is_paused", true) 
  136. 		 
  137. 		vint_set_property(new_physical_line.fade_in_anim_h, "is_paused", true) 
  138. 		vint_set_property(new_physical_line.fade_out_anim_h, "is_paused", true) 
  139. 		 
  140. 		-- Fade out old highlight 
  141. 		lua_play_anim(old_physical_line.fade_out_anim_h,0) 
  142. 	end 
  143.  
  144. 	-- Fade in new highlight 
  145. 	lua_play_anim(new_physical_line.fade_in_anim_h,0) 
  146. 	lua_play_anim(new_physical_line.pulsate_anim_h,0) 
  147.  
  148. end 
  149.  
  150. function mp_lobby_players_data_update(di_h, event) 
  151. 	 
  152. 	local team, name, voip, host, handle, badge_bmp = vint_dataitem_get(di_h) 
  153. 	local team_old = -1 
  154. 	local voip_old = -1 
  155. 	 
  156. 	-- Save old voip and team values if this is a state change 
  157. 	if Mp_lobby_players_data.player_data[di_h] ~= nil then 
  158. 		team_old = Mp_lobby_players_data.player_data[di_h].team 
  159. 		voip_old = Mp_lobby_players_data.player_data[di_h].voip 
  160. 	end 
  161. 	 
  162. 	if event == "insert" then -- Make a new line 
  163. 		-- Clone up a new line 
  164. 		local line_h = vint_object_clone(Mp_lobby_players_data.line_h) 
  165. 		Mp_lobby_players_data.player_data[di_h] = {team = team, voip = voip, line_h = line_h, handle = handle, physical_line = 0} 
  166.  
  167. 		-- Clone all the animations and pause them 
  168. 		Mp_lobby_players_data.player_data[di_h].pulsate_anim_h = vint_object_clone(Mp_lobby_players_data.pulsate_anim_h) 
  169. 		vint_set_property(Mp_lobby_players_data.player_data[di_h].pulsate_anim_h, "is_paused", true) 
  170. 		 
  171. 		Mp_lobby_players_data.player_data[di_h].fade_in_anim_h = vint_object_clone(Mp_lobby_players_data.fade_in_anim_h) 
  172. 		vint_set_property(Mp_lobby_players_data.player_data[di_h].fade_in_anim_h, "is_paused", true) 
  173. 		 
  174. 		Mp_lobby_players_data.player_data[di_h].fade_out_anim_h = vint_object_clone(Mp_lobby_players_data.fade_out_anim_h) 
  175. 		vint_set_property(Mp_lobby_players_data.player_data[di_h].fade_out_anim_h, "is_paused", true) 
  176. 		 
  177. 		local pause_menu_anim_root_h = vint_object_find("root_animation", nil, PAUSE_MENU_DOC_HANDLE) 
  178. 		local grp_h = vint_object_find("highlight", Mp_lobby_players_data.player_data[di_h].line_h) 
  179. 		 
  180. 		--Re-parent animation  
  181. 		vint_object_set_parent(Mp_lobby_players_data.player_data[di_h].pulsate_anim_h, pause_menu_anim_root_h) 
  182. 		vint_object_set_parent(Mp_lobby_players_data.player_data[di_h].fade_in_anim_h, pause_menu_anim_root_h) 
  183. 		vint_object_set_parent(Mp_lobby_players_data.player_data[di_h].fade_out_anim_h, pause_menu_anim_root_h) 
  184. 		 
  185. 		--Re-target animation 
  186. 		vint_set_property(Mp_lobby_players_data.player_data[di_h].pulsate_anim_h, "target_handle", vint_object_find("player_pulsate", grp_h)) 
  187. 		vint_set_property(Mp_lobby_players_data.player_data[di_h].fade_in_anim_h, "target_handle", grp_h) 
  188. 		vint_set_property(Mp_lobby_players_data.player_data[di_h].fade_out_anim_h, "target_handle", grp_h) 
  189.  
  190. 		-- Set name text 
  191. 		vint_set_property(vint_object_find("normal_name", Mp_lobby_players_data.player_data[di_h].line_h), "text_tag", name) 
  192. 		vint_set_property(vint_object_find("highlight_name", Mp_lobby_players_data.player_data[di_h].line_h), "text_tag", name) 
  193. 		 
  194. 		-- Set badge 
  195. 		local badge_icon_h = vint_object_find("player_badge", Mp_lobby_players_data.player_data[di_h].line_h) 
  196. 		vint_set_property(badge_icon_h, "image", badge_bmp) 
  197. 		vint_set_property(badge_icon_h, "scale", 0.6, 0.6) 
  198. 		 
  199. 		-- Set team color 
  200. 		mp_lobby_players_set_team(di_h, host) 
  201. 		 
  202. 		-- Set voip icon 
  203. 		mp_lobby_players_set_voip(di_h) 
  204. 		 
  205. 		-- Rebuild the line list 
  206. 		mp_lobby_players_arrange_lines() 
  207. 		 
  208. 	elseif event == "update" then -- Update an existing line 
  209. 		if Mp_lobby_players_data.player_data[di_h] ~= nil then 
  210. 		 
  211. 			-- Update the global variables 
  212. 			Mp_lobby_players_data.player_data[di_h].team = team 
  213. 			Mp_lobby_players_data.player_data[di_h].voip = voip 
  214. 		 
  215. 			if voip ~= voip_old then -- Update voip icon 
  216. 				mp_lobby_players_set_voip(di_h) 
  217. 			end 
  218. 			 
  219. 			if host == false then 
  220. 				if team ~= team_old then -- Update team 
  221. 					-- Change color 
  222. 					mp_lobby_players_set_team(di_h, host) 
  223. 				 
  224. 					-- Rebuild the line list 
  225. 					mp_lobby_players_arrange_lines() 
  226. 				end 
  227. 			end 
  228. 		end 
  229. 		 
  230. 	elseif event == "remove" then -- Remove existing line 
  231. 		if Mp_lobby_players_data.player_data[di_h] ~= nil then 
  232. 		 
  233. 			-- Destroy the animations 
  234. 			vint_object_destroy(Mp_lobby_players_data.player_data[di_h].pulsate_anim_h) 
  235. 			vint_object_destroy(Mp_lobby_players_data.player_data[di_h].fade_in_anim_h) 
  236. 			vint_object_destroy(Mp_lobby_players_data.player_data[di_h].fade_out_anim_h) 
  237. 		 
  238. 			-- Destroy graphics and all variables 
  239. 			vint_object_destroy(Mp_lobby_players_data.player_data[di_h].line_h) 
  240. 			Mp_lobby_players_data.player_data[di_h] = nil 
  241. 			 
  242. 			-- Fix the rest of the lines 
  243. 			mp_lobby_players_arrange_lines() 
  244. 		end 
  245. 	end 
  246. 	 
  247. end 
  248.  
  249. function mp_lobby_players_set_team(idx, host) 
  250. 	if Mp_lobby_players_data.player_data[idx] ~= nil then 
  251.  
  252. 		if host == true then -- Host, set to yellow 
  253. 			vint_set_property(vint_object_find("normal_name", Mp_lobby_players_data.player_data[idx].line_h), "tint", 0.9451, 0.8902, 0.004) 
  254. 		else 
  255. 			if Mp_lobby_players_data.player_data[idx].team == 0 then -- Blue team 
  256. 				vint_set_property(vint_object_find("normal_name", Mp_lobby_players_data.player_data[idx].line_h), "tint", 0.0, 0.5, 1.0) 
  257. 			elseif Mp_lobby_players_data.player_data[idx].team == 1 then -- Red team 
  258. 				vint_set_property(vint_object_find("normal_name", Mp_lobby_players_data.player_data[idx].line_h), "tint", 0.8, 0.0, 0.0) 
  259. 			end 
  260. 		end 
  261. 	end 
  262. end 
  263.  
  264. function mp_lobby_players_set_voip(idx) 
  265. 	if Mp_lobby_players_data.player_data[idx] ~= nil then 
  266. 		local voip_speaker = vint_object_find("speaker", Mp_lobby_players_data.player_data[idx].line_h) 
  267. 		local voip_speaker_on = vint_object_find("sound", Mp_lobby_players_data.player_data[idx].line_h) 
  268. 		 
  269. 		if Mp_lobby_players_data.player_data[idx].voip == 0 then -- No voip 
  270. 			vint_set_property(voip_speaker, "visible", false) 
  271. 			vint_set_property(voip_speaker_on, "visible", false) 
  272. 			 
  273. 		elseif Mp_lobby_players_data.player_data[idx].voip == 1 then -- Plugged in 
  274. 			vint_set_property(voip_speaker, "visible", true) 
  275. 			vint_set_property(voip_speaker_on, "visible", false) 
  276. 			 
  277. 		elseif Mp_lobby_players_data.player_data[idx].voip == 2 then -- Talking 
  278. 			vint_set_property(voip_speaker, "visible", true) 
  279. 			vint_set_property(voip_speaker_on, "visible", true) 
  280. 		end 
  281. 	end 
  282. end 
  283.  
  284. function mp_lobby_players_arrange_lines() 
  285.  
  286. 	local LINE_HEIGHT = 32 -- Distance between lines of text 
  287. 	local current_line = 0 
  288. 	local x = 2.0 -- X position of the top option (hard wired to get around some bug) 
  289. 	local y = 51.0 -- Y position of the top option (hard wired to get around some bug) 
  290. 	 
  291. 	 -- Draw all blue team lines 
  292. 	for idx, val in Mp_lobby_players_data.player_data do 
  293. 		if Mp_lobby_players_data.player_data[idx].team == 0 then 
  294. 			 
  295. 			local new_y = y + (current_line * LINE_HEIGHT) 
  296. 			vint_set_property(Mp_lobby_players_data.player_data[idx].line_h, "anchor", x, new_y) 
  297. 			vint_set_property(Mp_lobby_players_data.player_data[idx].line_h, "visible", true) 
  298. 			 
  299. 			Mp_lobby_players_data.player_data[idx].physical_line = current_line + 1 
  300. 			current_line = current_line + 1 
  301. 		end 
  302. 	end 
  303.  
  304. 	 -- Draw all red team lines 
  305. 	for idx, val in Mp_lobby_players_data.player_data do 
  306. 		if Mp_lobby_players_data.player_data[idx].team == 1 then 
  307. 			 
  308. 			local new_y = y + (current_line * LINE_HEIGHT) 
  309. 			vint_set_property(Mp_lobby_players_data.player_data[idx].line_h, "anchor", x, new_y) 
  310. 			vint_set_property(Mp_lobby_players_data.player_data[idx].line_h, "visible", true) 
  311. 			 
  312. 			local text = vint_object_find("text", Mp_lobby_players_data.player_data[idx].line_h) 
  313. 			local width, height = vint_get_property(text, "screen_size") 
  314. 			 
  315. 			Mp_lobby_players_data.player_data[idx].physical_line = current_line + 1 
  316. 			current_line = current_line + 1 
  317. 		end 
  318. 	end 
  319. 	 
  320. 	-- Set the number of lines 
  321. 	Mp_lobby_players_data.num_lines = current_line 
  322. 	 
  323. 	-- Clear out the old highlight 
  324. 	for idx, val in Mp_lobby_players_data.player_data do 
  325. 		local highlight_h = vint_object_find("highlight", Mp_lobby_players_data.player_data[idx].line_h) 
  326. 		vint_set_property(Mp_lobby_players_data.player_data[idx].fade_in_anim_h, "is_paused", true) 
  327. 		vint_set_property(highlight_h, "alpha", 0.0) 
  328. 	end 
  329. 	 
  330. 	-- Draw the new highlight at the top of the list 
  331. 	Mp_lobby_players_data.current_line = 1 
  332. 	mp_lobby_players_update_highlight(0) 
  333. end