sr2lua/mp_scoreboard.lua

  1. ---- MP Scoreboard 
  2.  
  3. MP_Scoreboard = { handles = { }, cur_idx = 0} 
  4. MP_SCOREBOARD_MAX_PLAYERS = 12 
  5.  
  6. -- Initialize and Clean up  
  7. function mp_scoreboard_init() 
  8.  
  9. 	local grp_h = vint_object_find("mp_scoreboard") 
  10. 	vint_set_property(grp_h, "visible", false)	 
  11. 	 
  12. 	local line_grp_h = vint_object_find("mp_scoreboard_line", grp_h) 
  13. 	vint_set_property(line_grp_h, "visible", false) 
  14. 	 
  15. 	-- Handles for the column headings 
  16. 	MP_Scoreboard.handles.headings_sa_h = vint_object_find("headings_sa") 
  17. 	MP_Scoreboard.handles.headings_gb_h = vint_object_find("headings_gb") 
  18. 	 
  19. 	-- Handles for the highlight 
  20. 	MP_Scoreboard.handles.highlight_h = vint_object_find("mp_scoreboard_highlight") 
  21. 	MP_Scoreboard.handles.highlight = {} 
  22. 	MP_Scoreboard.handles.highlight.player_name_h = vint_object_find("player_name",MP_Scoreboard.handles.highlight_h) 
  23. 	MP_Scoreboard.handles.highlight.column1_h = vint_object_find("column1",MP_Scoreboard.handles.highlight_h) 
  24. 	MP_Scoreboard.handles.highlight.column2_h = vint_object_find("column2",MP_Scoreboard.handles.highlight_h) 
  25. 	MP_Scoreboard.handles.highlight.column3_h = vint_object_find("column3",MP_Scoreboard.handles.highlight_h) 
  26. 	MP_Scoreboard.handles.highlight.column4_h = vint_object_find("column4",MP_Scoreboard.handles.highlight_h) 
  27. 	 
  28. 	MP_Scoreboard.handles.highlight.pulsate_h = vint_object_find("scoreboard_pulsate_anim") 
  29. 	vint_set_property(MP_Scoreboard.handles.highlight.pulsate_h,"is_paused", true) 
  30. 	 
  31. 	MP_Scoreboard.handles.highlight.fade_in_h = vint_object_find("scoreboard_highlight_fade_in") 
  32. 	vint_set_property(MP_Scoreboard.handles.highlight.fade_in_h,"is_paused", true) 
  33.  
  34. 	--Set up animation 
  35. 	MP_Scoreboard.handles.highlight.pulsate_h = vint_object_find("scoreboard_pulsate_anim") 
  36. 	vint_set_property(MP_Scoreboard.handles.highlight.pulsate_h,"is_paused", true) 
  37. 	 
  38. 	MP_Scoreboard.handles.highlight.fade_in_h = vint_object_find("scoreboard_highlight_fade_in") 
  39. 	vint_set_property(MP_Scoreboard.handles.highlight.fade_in_h,"is_paused", true) 
  40. 	 
  41. 	--Re-parent animation  
  42. 	vint_object_set_parent(MP_Scoreboard.handles.highlight.pulsate_h, vint_object_find("root_animation", nil, vint_document_find("pause_menu"))) 
  43. 	vint_object_set_parent(MP_Scoreboard.handles.highlight.fade_in_h, vint_object_find("root_animation", nil, vint_document_find("pause_menu"))) 
  44. 	 
  45. 	--Re-target animation 
  46. 	vint_set_property(MP_Scoreboard.handles.highlight.pulsate_h, "target_handle", vint_object_find("scoreboard_pulsate", MP_Scoreboard.handles.highlight_h)) 
  47. 	vint_set_property(MP_Scoreboard.handles.highlight.fade_in_h, "target_handle", MP_Scoreboard.handles.highlight_h) 
  48. 	 
  49. 	MP_Scoreboard.handles.grp_h = grp_h 
  50. 	MP_Scoreboard.handles.master_line = line_grp_h 
  51. 	 
  52. 	MP_Scoreboard.handles.lines = { } 
  53. 	MP_Scoreboard.data = { } 
  54. 	 
  55. 	for i = 0, MP_SCOREBOARD_MAX_PLAYERS - 1 do  
  56. 		MP_Scoreboard.data[i] = { enabled = false } 
  57. 		MP_Scoreboard.handles.lines[i] = { } 
  58. 		MP_Scoreboard.handles.lines[i].highlight = { } 
  59. 		MP_Scoreboard.handles.lines[i].normal = { } 
  60.  
  61. 		MP_Scoreboard.handles.lines[i].grp_h = vint_object_clone(line_grp_h) 
  62. 		 
  63. 		MP_Scoreboard.handles.lines[i].normal.grp_h = vint_object_find("normal", MP_Scoreboard.handles.lines[i].grp_h) 
  64. 		 
  65. 		grp_h = MP_Scoreboard.handles.lines[i].normal.grp_h	 
  66. 		MP_Scoreboard.handles.lines[i].normal.player_name_h = vint_object_find("player_name", grp_h) 
  67. 		MP_Scoreboard.handles.lines[i].normal.column1_h = vint_object_find("column1", grp_h) 
  68. 		MP_Scoreboard.handles.lines[i].normal.column2_h = vint_object_find("column2", grp_h) 
  69. 		MP_Scoreboard.handles.lines[i].normal.column3_h = vint_object_find("column3", grp_h) 
  70. 		MP_Scoreboard.handles.lines[i].normal.column4_h = vint_object_find("column4", grp_h) 
  71. 		 
  72. 		MP_Scoreboard.handles.lines[i].badge = vint_object_find("player_badge", grp_h) 
  73. 		MP_Scoreboard.handles.lines[i].speaker_h = vint_object_find("speaker", grp_h) 
  74. 		MP_Scoreboard.handles.lines[i].sound_h = vint_object_find("sound", grp_h) 
  75. 		 
  76. 		if i ~= 0 then 
  77. 			local x, y = vint_get_property(MP_Scoreboard.handles.lines[i - 1].grp_h, "anchor") 
  78. 			local w, h = element_get_actual_size(MP_Scoreboard.handles.lines[i - 1].normal.player_name_h) 
  79. 			 
  80. 			vint_set_property(MP_Scoreboard.handles.lines[i].grp_h, "anchor", x, y + h + 1)		 
  81. 		end 
  82. 	end 
  83.  
  84. 	if vint_document_find("pause_menu") ~= 0 then 
  85. 		mp_scoreboard_pause_menu_show() 
  86. 	else 
  87. 		mp_scoreboard_completion_show() 
  88. 	end 
  89. end 
  90.  
  91. function mp_scoreboard_pause_menu_show() 
  92.  
  93. 	vint_datagroup_add_subscription("mp_scoreboard_data_group", "insert", "mp_scoreboard_populate") 
  94. 	vint_datagroup_add_subscription("mp_scoreboard_data_group", "remove", "mp_scoreboard_populate") 
  95. 	vint_datagroup_add_subscription("mp_scoreboard_data_group", "update", "mp_scoreboard_populate") 
  96. 	 
  97. 	MP_Scoreboard.input_subs = { 
  98. 		vint_subscribe_to_input_event(nil, "nav_up",		"mp_scoreboard_nav_up", 50), 
  99. 		vint_subscribe_to_input_event(nil, "nav_down", 	"mp_scoreboard_nav_down", 50), 
  100. 		vint_subscribe_to_input_event(nil, "select",		"mp_scoreboard_select", 50) 
  101. 	}	 
  102. 	 
  103. 	lua_play_anim(MP_Scoreboard.handles.highlight.pulsate_h, 0) 
  104. 	mp_scoreboard_update_highlight(-1) 
  105. end 
  106.  
  107. function mp_scoreboard_completion_show() 
  108. 	 
  109. end 
  110.  
  111. function mp_scoreboard_cleanup() 
  112. 	for i = 0, MP_SCOREBOARD_MAX_PLAYERS - 1 do  
  113. 		vint_object_destroy(MP_Scoreboard.handles.lines[i].grp_h) 
  114. 	end 
  115. 	 
  116. 	vint_set_property(MP_Scoreboard.handles.grp_h, "visible", true)	 
  117. 	vint_object_destroy(MP_Scoreboard.handles.grp_h) 
  118. 	 
  119. 	-- Kill the player info popup 
  120. 	if vint_document_find("mp_player_info_popup") ~= nil then 
  121. 		vint_document_unload(vint_document_find("mp_player_info_popup")) 
  122. 	end 
  123.  
  124. 	for idx, val in MP_Scoreboard.input_subs do 
  125. 		vint_unsubscribe_to_input_event(val) 
  126. 	end 
  127.  
  128. 	vint_datagroup_remove_subscription("mp_scoreboard_data_group", "insert", "mp_scoreboard_populate") 
  129. 	vint_datagroup_remove_subscription("mp_scoreboard_data_group", "remove", "mp_scoreboard_populate") 
  130. 	vint_datagroup_remove_subscription("mp_scoreboard_data_group", "update", "mp_scoreboard_populate") 
  131. 	 
  132. 	MP_Scoreboard.input_subs = { } 
  133. end 
  134.  
  135. -- Input 
  136. function mp_scoreboard_nav_up() 
  137. 	local old_idx = MP_Scoreboard.cur_idx 
  138. 	MP_Scoreboard.cur_idx = MP_Scoreboard.cur_idx - 1 
  139. 	audio_play(Menu_sound_item_nav) 
  140. 	 
  141. 	if MP_Scoreboard.cur_idx < 0 then 
  142. 		for i = 0, MP_SCOREBOARD_MAX_PLAYERS - 1 do 
  143. 			if MP_Scoreboard.data[i].enabled == true then 
  144. 				MP_Scoreboard.cur_idx = i 
  145. 			end 
  146. 		end 
  147. 	end 
  148.  
  149. 	mp_scoreboard_update_highlight(old_idx) 
  150. end 
  151.  
  152. function mp_scoreboard_nav_down() 
  153. 	local old_idx = MP_Scoreboard.cur_idx 
  154. 	MP_Scoreboard.cur_idx = MP_Scoreboard.cur_idx + 1 
  155. 	audio_play(Menu_sound_item_nav) 
  156. 	 
  157. 	if MP_Scoreboard.cur_idx >= MP_SCOREBOARD_MAX_PLAYERS then 
  158. 		MP_Scoreboard.cur_idx = 0 
  159. 	elseif MP_Scoreboard.data[MP_Scoreboard.cur_idx].enabled ~= true then 
  160. 		MP_Scoreboard.cur_idx = 0 
  161. 	end 
  162. 	 
  163. 	mp_scoreboard_update_highlight(old_idx) 
  164. end 
  165.  
  166. function mp_scoreboard_select() 
  167. 	if get_is_syslink() == false then 
  168. 		mp_popup_open(MP_Scoreboard.data[MP_Scoreboard.cur_idx].handle) 
  169. 	end 
  170. end 
  171.  
  172. -- Highlight 
  173. function mp_scoreboard_update_highlight(old_idx) 
  174. 	if old_idx == MP_Scoreboard.cur_idx then 
  175. 		return 
  176. 	end 
  177.  
  178. 	-- Hide the highlight 
  179. 	vint_set_property(MP_Scoreboard.handles.highlight.fade_in_h,"is_paused", true) 
  180. 	vint_set_property(MP_Scoreboard.handles.highlight_h, "alpha", 0.0) 
  181. 	 
  182. 	-- Update the text 
  183. 	mp_scoreboard_highlight_text() 
  184. 	 
  185. 	-- Move the highlight and animate it in 
  186. 	local x, y = vint_get_property(MP_Scoreboard.handles.lines[MP_Scoreboard.cur_idx].grp_h, "anchor") 
  187. 	vint_set_property(MP_Scoreboard.handles.highlight_h, "anchor", x, y) 
  188. 	lua_play_anim(MP_Scoreboard.handles.highlight.fade_in_h,0) 
  189. end 
  190.  
  191. function mp_scoreboard_highlight_text() 
  192.  
  193. 	if MP_Scoreboard.data[MP_Scoreboard.cur_idx].suicides == nil then 
  194. 		-- Make sure the data is clear, this is usually only gonna happen when the screen starts up 
  195. 		vint_set_property(MP_Scoreboard.handles.highlight.player_name_h, "text_tag", " ") 
  196. 		vint_set_property(MP_Scoreboard.handles.highlight.column1_h, "text_tag", " ") 
  197. 		vint_set_property(MP_Scoreboard.handles.highlight.column2_h, "text_tag", " ") 
  198. 		vint_set_property(MP_Scoreboard.handles.highlight.column3_h, "text_tag", " ") 
  199. 		vint_set_property(MP_Scoreboard.handles.highlight.column4_h, "text_tag", " ") 
  200. 	else 
  201. 		-- Show the data 
  202. 		vint_set_property(MP_Scoreboard.handles.highlight.player_name_h, "text_tag", MP_Scoreboard.data[MP_Scoreboard.cur_idx].player_name) 
  203. 	 
  204. 		-- Format according to game mode 
  205. 		if MP_Scoreboard.data[MP_Scoreboard.cur_idx].suicides == -1 then 
  206. 			vint_set_property(MP_Scoreboard.handles.highlight.column1_h, "text_tag", " ") 
  207. 			vint_set_property(MP_Scoreboard.handles.highlight.column2_h, "text_tag", "$" .. format_cash(MP_Scoreboard.data[MP_Scoreboard.cur_idx].score)) 
  208. 			vint_set_property(MP_Scoreboard.handles.highlight.column3_h, "text_tag", format_cash(MP_Scoreboard.data[MP_Scoreboard.cur_idx].kills)) 
  209. 			vint_set_property(MP_Scoreboard.handles.highlight.column4_h, "text_tag", format_cash(MP_Scoreboard.data[MP_Scoreboard.cur_idx].deaths)) 
  210. 		else 
  211. 			vint_set_property(MP_Scoreboard.handles.highlight.column1_h, "text_tag", format_cash(MP_Scoreboard.data[MP_Scoreboard.cur_idx].score)) 
  212. 			vint_set_property(MP_Scoreboard.handles.highlight.column2_h, "text_tag", format_cash(MP_Scoreboard.data[MP_Scoreboard.cur_idx].kills)) 
  213. 			vint_set_property(MP_Scoreboard.handles.highlight.column3_h, "text_tag", format_cash(MP_Scoreboard.data[MP_Scoreboard.cur_idx].deaths)) 
  214. 			vint_set_property(MP_Scoreboard.handles.highlight.column4_h, "text_tag", format_cash(MP_Scoreboard.data[MP_Scoreboard.cur_idx].suicides)) 
  215. 		end	 
  216. 	end 
  217. end 
  218.  
  219. -- Data 
  220. function mp_scoreboard_populate(di_h, event) 
  221. 	local player_index, enabled, voice, player_name, score, kills, deaths, suicides, host, player_team, image, handle = vint_dataitem_get(di_h) 
  222. 	MP_Scoreboard.data[player_index] = { player_name = player_name, enabled = enabled, score = score, kills = kills, handle = handle, 
  223. 										 suicides = suicides, deaths = deaths, player_team = player_team, voice = voice, host = host, badge_image = image } 
  224.  
  225. 	-- Update the labels 
  226. 	mp_scoreboard_update_labels(event) 
  227. end 
  228.  
  229. function mp_scoreboard_update_labels(event) 
  230. 	for player_index = 0, MP_SCOREBOARD_MAX_PLAYERS - 1 do 
  231. 		if MP_Scoreboard.data[player_index].enabled == false then 
  232. 			vint_set_property(MP_Scoreboard.handles.lines[player_index].grp_h, "visible", false) 
  233. 		else 
  234. 		 
  235. 			-- Format all the data for the game type 
  236. 			if MP_Scoreboard.data[player_index].suicides == -1 then 
  237. 				vint_set_property(MP_Scoreboard.handles.headings_sa_h, "visible", true) 
  238. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column1_h, "text_tag", " ") 
  239. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column2_h, "text_tag", "$" .. format_cash(MP_Scoreboard.data[player_index].score)) 
  240. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column3_h, "text_tag", format_cash(MP_Scoreboard.data[player_index].kills)) 
  241. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column4_h, "text_tag", format_cash(MP_Scoreboard.data[player_index].deaths)) 
  242. 			else 
  243. 				vint_set_property(MP_Scoreboard.handles.headings_gb_h, "visible", true) 
  244. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column1_h, "text_tag", format_cash(MP_Scoreboard.data[player_index].score)) 
  245. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column2_h, "text_tag", format_cash(MP_Scoreboard.data[player_index].kills)) 
  246. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column3_h, "text_tag", format_cash(MP_Scoreboard.data[player_index].deaths)) 
  247. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.column4_h, "text_tag", format_cash(MP_Scoreboard.data[player_index].suicides)) 
  248. 			end 
  249. 			 
  250. 			vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.player_name_h, "text_tag", MP_Scoreboard.data[player_index].player_name) 
  251. 			vint_set_property(MP_Scoreboard.handles.lines[player_index].badge, "image", MP_Scoreboard.data[player_index].badge_image) 
  252. 			vint_set_property(MP_Scoreboard.handles.lines[player_index].grp_h, "visible", true) 
  253. 			vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.grp_h, "visible", true) 
  254.  
  255. 			if MP_Scoreboard.data[player_index].voice == 0 then 
  256. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].speaker_h, "visible", false) 
  257. 			elseif MP_Scoreboard.data[player_index].voice == 1 then 
  258. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].speaker_h, "visible", true) 
  259. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].speaker_h, "image", "mp_speaker_off") 
  260. 			elseif MP_Scoreboard.data[player_index].voice == 2 then 
  261. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].speaker_h, "visible", true) 
  262. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].speaker_h, "image", "mp_speaker_on") 
  263. 			end 
  264. 			 
  265. 			if MP_Scoreboard.data[player_index].player_team == true then 
  266. 				-- color the name blue 
  267. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.player_name_h, "tint", .27, .51, .84) 
  268. 			else 
  269. 				-- color the name red 
  270. 				vint_set_property(MP_Scoreboard.handles.lines[player_index].normal.player_name_h, "tint", .71, 0, 0) 
  271. 			end 
  272. 			 
  273. 			-- Fixed the highlight 
  274. 			if event == "Remove" then 
  275. 				-- Update the highlight position 
  276. 				mp_scoreboard_update_highlight(-1) 
  277. 			else 
  278. 				-- Update the highlight text 
  279. 				mp_scoreboard_highlight_text() 
  280. 			end 
  281. 		end 
  282. 	end 
  283. end 
  284.