sr2lua/hud_multi_prototype.lua

  1. ---------- 
  2. -- Globals 
  3. ---------- 
  4.  
  5. Score_bars_info = { 
  6. 	score_bars_group_h 			= 0, 
  7. 	friend_bar_h 						= 0, 
  8. 	foe_bar_h 							= 0, 
  9. 	 
  10. 	friend_text_h						= 0, 
  11. 	foe_text_h							= 0, 
  12. 	 
  13. 	friend_bar_length 				= 0, 
  14. 	friend_bar_width 					= 0, 
  15. 	foe_bar_length 					= 0, 
  16. 	foe_bar_width 						= 0, 
  17. 	 
  18. 	score_limit 						= 0, 
  19. } 
  20.  
  21. Game_timer_h = 0 
  22.  
  23. Activity_score_bar_info = { 
  24. 	bar_group_h			= 0, 
  25. 	split_group_h		= 0, 
  26. 	 
  27. 	friend_text_h		= 0, 
  28. 	foe_text_h			= 0, 
  29. 	 
  30. 	bar_length			= 0, 
  31. } 
  32.  
  33. Tagging_progress_info = { 
  34. 	group_h				= 0, 
  35. 	fill_h				= 0, 
  36. 	 
  37. 	bar_length			= 0, 
  38. } 
  39.  
  40. Tagging_perk_listing_info = { 
  41. 	[1] = { text_h = 0, scale_tween_h = 0, color_tween_h = 0 }, 
  42. 	[2] = { text_h = 0, scale_tween_h = 0, color_tween_h = 0 }, 
  43. 	[3] = { text_h = 0, scale_tween_h = 0, color_tween_h = 0 }, 
  44. 	[4] = { text_h = 0, scale_tween_h = 0, color_tween_h = 0 }, 
  45. } 
  46.  
  47. FOF_NEUTRAL 		= -1 
  48. FOF_FRIEND			= 0 
  49. FOF_FOE				= 1 
  50.  
  51. -- Colors need to be specified from 0 to 1 
  52. NEUTRAL_COLOR =	{ r = 255/255, g = 255/255, b = 255/255 } 
  53. FRIEND_COLOR = 	{ r =  52/255, g = 167/255, b = 255/255 } 
  54. FOE_COLOR = 		{ r = 205/255, g =  29/255, b =  29/255 } 
  55.  
  56. function get_color_from_fof_type(fof_type) 
  57.  
  58. 	if fof_type == FOF_FRIEND then 
  59. 		return FRIEND_COLOR 
  60. 	elseif fof_type == FOF_FOE then 
  61. 		return FOE_COLOR 
  62. 	else 
  63. 		return NEUTRAL_COLOR	 
  64. 	end 
  65.  
  66. end 
  67.  
  68. function create_tween(name, target_h, target_prop, duration) 
  69. 	local tween_h = vint_object_create(name, "tween", vint_object_find("root_animation")) 
  70. 	vint_set_property(tween_h, "duration", duration) 
  71. 	vint_set_property(tween_h, "target_handle", target_h) 
  72. 	vint_set_property(tween_h, "target_property", target_prop) 
  73. 	vint_set_property(tween_h, "start_time",	vint_get_time_index()) 
  74. 	vint_set_property(tween_h, "state", "disabled") 
  75. 	return tween_h 
  76. end 
  77.  
  78. -- Given a score and the score limit, finds a percentage between 0 and 1 
  79. -- 
  80. function find_score_percent(score, score_limit) 
  81. 	if score_limit <= 0 then 
  82. 		return 0 
  83. 	end 
  84. 	 
  85. 	local percent = score / score_limit 
  86. 	if percent > 1 then 
  87. 		percent = 1 
  88. 	elseif percent < 0 then 
  89. 		percent = 0 
  90. 	end 
  91. 	 
  92. 	return percent 
  93. end 
  94.  
  95. -- Given a boolean value, returns TRUE or FALSE as text (for debugging) 
  96. -- 
  97. function bool_to_text(bool_var) 
  98. 	if bool_var then 
  99. 		return "TRUE" 
  100. 	else 
  101. 		return "FALSE" 
  102. 	end 
  103. end 
  104.  
  105. --------------- 
  106. -- Respect Bars 
  107. --------------- 
  108.  
  109. function score_bars_init() 
  110. 	-- Get the various interface object handles 
  111. 	local group_h = vint_object_find("respect_bars") 
  112. 	Score_bars_info.score_bars_group_h = group_h 
  113. 	Score_bars_info.friend_bar_h = vint_object_find("friend_bar", group_h) 
  114. 	Score_bars_info.foe_bar_h = vint_object_find("foe_bar", group_h) 
  115. 	 
  116. 	Score_bars_info.friend_text_h = vint_object_find("friend_text", group_h) 
  117. 	Score_bars_info.foe_text_h = vint_object_find("foe_text", group_h) 
  118. 	 
  119. 	-- Get the length and width of each bar from the start of the match 
  120. 	Score_bars_info.friend_bar_length, Score_bars_info.friend_bar_width = vint_get_property(Score_bars_info.friend_bar_h, "source_se") 
  121. 	Score_bars_info.foe_bar_length, Score_bars_info.foe_bar_width = vint_get_property(Score_bars_info.foe_bar_h, "source_se") 
  122.  
  123. 	-- Set the team scores to both be zeroed out to start 
  124. 	vint_set_property(Score_bars_info.friend_bar_h, "source_se", 0, Score_bars_info.friend_bar_width) 
  125. 	vint_set_property(Score_bars_info.foe_bar_h, "source_se", 0, Score_bars_info.foe_bar_width) 
  126. 	 
  127. 	-- Make 'em all visible 
  128. 	vint_set_property(Score_bars_info.score_bars_group_h, "visible", true) 
  129. 	 
  130. 	-- Add a subscription to the fof scores data item 
  131. 	vint_dataitem_add_subscription("sr2_fof_scores_item", "update", "fof_scores_change") 
  132. 	 
  133. 	-- Add a subscription to the score limit data item 
  134. 	vint_dataitem_add_subscription("sr2_score_limit_item", "update", "score_limit_change") 
  135. end 
  136.  
  137. function score_bars_hide() 
  138. 	vint_set_property( vint_object_find( "respect_bars" ), "visible", false ) 
  139. end 
  140.  
  141. -- Callback for an update to the respect bar scores 
  142. -- 
  143. function fof_scores_change(data_item_h, event_name) 
  144. 	local friend_score, foe_score, friend_text, foe_text = vint_dataitem_get(data_item_h) 
  145. 	 
  146. 	local percent 
  147. 	 
  148. 	-- Readjust the score visibility based on the size of the bar 
  149. 	if Score_bars_info.score_limit > 0 then 
  150. 		-- Show the bars as a percentage of the score limit 
  151. 		percent = find_score_percent(friend_score, Score_bars_info.score_limit) 
  152. 		vint_set_property(Score_bars_info.friend_bar_h, "source_se", percent * Score_bars_info.friend_bar_length, Score_bars_info.friend_bar_width) 
  153. 		debug_print("mpvint", "Friend bar set to " .. friend_score .. "/" .. Score_bars_info.score_limit .. "\n") 
  154. 		 
  155. 		percent = find_score_percent(foe_score, Score_bars_info.score_limit) 
  156. 		vint_set_property(Score_bars_info.foe_bar_h, "source_se", percent * Score_bars_info.foe_bar_length, Score_bars_info.foe_bar_width) 
  157. 		debug_print("mpvint", "Foe bar set to " .. foe_score .. "/" .. Score_bars_info.score_limit .. "\n") 
  158. 	else 
  159. 		-- Show the bars as a percentage of the highest score 
  160. 		local highest_score = friend_score 
  161. 		if foe_score > friend_score then 
  162. 			highest_score = foe_score 
  163. 		end 
  164.  
  165. 		percent = find_score_percent(friend_score, highest_score) 
  166. 		vint_set_property(Score_bars_info.friend_bar_h, "source_se", percent * Score_bars_info.friend_bar_length, Score_bars_info.friend_bar_width) 
  167. 		debug_print("mpvint", "Friend bar set to " .. friend_score .. "/" .. highest_score .. "\n") 
  168. 		 
  169. 		percent = find_score_percent(foe_score, highest_score) 
  170. 		vint_set_property(Score_bars_info.foe_bar_h, "source_se", percent * Score_bars_info.foe_bar_length, Score_bars_info.foe_bar_width) 
  171. 		debug_print("mpvint", "Foe bar set to " .. foe_score .. "/" .. highest_score .. "\n") 
  172. 	end 
  173. 	 
  174. 	vint_set_property(Score_bars_info.friend_text_h, "text_tag", friend_text) 
  175. 	vint_set_property(Score_bars_info.foe_text_h, "text_tag", foe_text) 
  176. end 
  177.  
  178. -- Callback to set the score limit 
  179. -- 
  180. function score_limit_change(data_item_h, event_name) 
  181. 	Score_bars_info.score_limit = vint_dataitem_get(data_item_h) 
  182. 	debug_print("mpvint", "Setting score limit to " .. Score_bars_info.score_limit .. "\n") 
  183. end 
  184.  
  185. ------------- 
  186. -- Game Timer 
  187. ------------- 
  188.  
  189. -- Clears out the timer object and adds a subscription to the mode timestamp 
  190. -- 
  191. function game_timer_init() 
  192. 	Game_timer_h = vint_object_find("timer") 
  193. 	vint_set_property(Game_timer_h, "text_tag", "") 
  194. 	vint_set_property(Game_timer_h, "visible", true ) 
  195. 	 
  196. 	vint_dataitem_add_subscription("sr2_sa_game_timer_item", "update", "game_timer_change") 
  197. end 
  198.  
  199. function game_timer_hide() 
  200. 	vint_set_property( vint_object_find( "timer" ), "visible", false ) 
  201. end 
  202.  
  203. -- Callback when the timer value changes (currently every frame), changes the timer string 
  204. -- 
  205. function game_timer_change(data_item_h, event_name) 
  206. 	local timer_string = vint_dataitem_get(data_item_h) 
  207. 	vint_set_property(Game_timer_h, "text_tag", timer_string) 
  208. end 
  209.  
  210. --------------------- 
  211. -- Activity Score Bar 
  212. --------------------- 
  213.  
  214. function activity_score_bar_init() 
  215. 	debug_print("mpvint", "initing activity score bar\n") 
  216.  
  217. 	local group_h = vint_object_find("activity_score_bar_group") 
  218. 	Activity_score_bar_info.bar_group_h = group_h 
  219. 	local split_h = vint_object_find("act_bar_split_group", group_h) 
  220. 	Activity_score_bar_info.split_group_h = split_h 
  221. 	Activity_score_bar_info.friend_text_h = vint_object_find("act_bar_friend_text", split_h) 
  222. 	Activity_score_bar_info.foe_text_h = vint_object_find("act_bar_foe_text", split_h) 
  223. 	 
  224. 	-- Find the width of the bar (clip region) 
  225. 	local x, y = vint_get_property(group_h, "clip_size") 
  226. 	Activity_score_bar_info.bar_length = x 
  227. 	 
  228. 	-- Add a subscription to the score bar item for Strong Arm 
  229. 	vint_dataitem_add_subscription("sr2_sa_score_bar_item", "update", "activity_score_bar_change") 
  230. 	 
  231. 	-- Hide the bar for now 
  232. 	vint_set_property(group_h, "visible", false) 
  233. end 
  234.  
  235. function activity_score_bar_hide() 
  236. 	vint_set_property( vint_object_find( "activity_score_bar_group" ), "visible", false ) 
  237. end 
  238.  
  239. function activity_score_bar_change(data_item_h, event_name) 
  240. 	local friend_score, foe_score, friend_string, foe_string, visible_flag = vint_dataitem_get(data_item_h) 
  241. 	 
  242. 	-- Set visibility 
  243. 	vint_set_property(Activity_score_bar_info.bar_group_h, "visible", visible_flag) 
  244. 	 
  245. 	-- Debug spew 
  246. 	debug_print("mpvint", "Setting activity score bar visibility to be " .. bool_to_text(visible_flag) .. "\n") 
  247. 	 
  248. 	-- If it's visible, we have more to do 
  249. 	if visible_flag then 
  250. 		-- Calculate the ratio to show 
  251. 		local ratio = 0.5 
  252. 		 
  253. 		-- If no one's scored yet, show an even match 
  254. 		local total_score = friend_score + foe_score 
  255. 		if total_score > 0 then 
  256. 			ratio = friend_score / total_score 
  257. 		end 
  258. 		debug_print("mpvint", "score bar ratio is " .. ratio .. "\n") 
  259. 		 
  260. 		-- Position the split based on the ratio 
  261. 		local position = ratio * Activity_score_bar_info.bar_length 
  262. 		debug_print("mpvint", "split position is " .. position .. " out of " .. Activity_score_bar_info.bar_length .. "\n") 
  263. 		 
  264. 		-- Set the split group position 
  265. 		vint_set_property(Activity_score_bar_info.split_group_h, "anchor", position, 0) 
  266. 		 
  267. 		-- Update the text 
  268. 		vint_set_property(Activity_score_bar_info.friend_text_h, "text_tag", friend_string) 
  269. 		vint_set_property(Activity_score_bar_info.foe_text_h, "text_tag", foe_string) 
  270. 	end 
  271. end 
  272.  
  273. ------------------- 
  274. -- Tagging Progress 
  275. ------------------- 
  276.  
  277. function tagging_progress_init() 
  278. 	local group_h = vint_object_find( "tagging_progress_group" ) 
  279. 	Tagging_progress_info.group_h = group_h 
  280. 	Tagging_progress_info.fill_h = vint_object_find( "tagging_progress_fill", group_h ) 
  281. 	 
  282. 	local x, y = vint_get_property( group_h, "clip_size" ) 
  283. 	Tagging_progress_info.bar_length = x 
  284. 	 
  285. 	-- Hide progress until we need it 
  286. 	vint_set_property( group_h, "visible", false ) 
  287. 	 
  288. 	vint_dataitem_add_subscription( "sr2_sa_tagging_progress_item", "update", "tagging_progress_change" ) 
  289. end 
  290.  
  291. function tagging_progress_hide() 
  292. 	vint_set_property( vint_object_find( "tagging_progress_group" ), "visible", false ) 
  293. end 
  294.  
  295. function tagging_progress_change( data_item_h, event_name ) 
  296. 	local percent = vint_dataitem_get( data_item_h ) 
  297. 	 
  298. 	percent = min( percent, 1 ) 
  299. 	 
  300. 	if percent > 0 then 
  301. 		vint_set_property( Tagging_progress_info.group_h, "visible", true ) 
  302. 		 
  303. 		local fill_position = ( percent - 1 ) * Tagging_progress_info.bar_length 
  304. 		vint_set_property( Tagging_progress_info.fill_h, "anchor", fill_position, 0 ) 
  305. 	else 
  306. 		vint_set_property( Tagging_progress_info.group_h, "visible", false ) 
  307. 	end 
  308. end 
  309.  
  310. ----------------------- 
  311. -- Tagging Perk Listing 
  312. ----------------------- 
  313.  
  314. function tagging_perk_listing_init() 
  315. 	-- Find the overall status grouping 
  316. 	local group_h = vint_object_find( "tagging_perk_listing" ) 
  317. 	vint_set_property( group_h, "visible", true ) 
  318.  
  319. 	-- Initialize the first perk text field 
  320. 	local text_h = vint_object_find( "perk_text1", group_h ) 
  321. 	local info = Tagging_perk_listing_info[ 1 ] 
  322. 	info.text_h = text_h 
  323. 	vint_set_property( text_h, "visible", false ) 
  324.  
  325. 	-- Clone the second text field and change its position	 
  326. 	text_h = vint_object_clone( text_h ) 
  327. 	info = Tagging_perk_listing_info[ 2 ] 
  328. 	info.text_h = text_h 
  329. 	vint_set_property( text_h, "visible", false ) 
  330.  
  331. 	-- Move the clone 
  332. 	local point_h = vint_object_find( "perk_pt2", group_h ) 
  333. 	local x, y = vint_get_property( point_h, "anchor" ) 
  334. 	vint_set_property( text_h, "anchor", x, y ) 
  335.  
  336. 	-- Clone the third text field and change its position	 
  337. 	text_h = vint_object_clone( text_h ) 
  338. 	info = Tagging_perk_listing_info[ 3 ] 
  339. 	info.text_h = text_h 
  340. 	vint_set_property( text_h, "visible", false ) 
  341.  
  342. 	-- Move the clone 
  343. 	local point_h = vint_object_find( "perk_pt3", group_h ) 
  344. 	local x, y = vint_get_property( point_h, "anchor" ) 
  345. 	vint_set_property( text_h, "anchor", x, y ) 
  346.  
  347. 	-- Clone the fourth text field and change its position	 
  348. 	text_h = vint_object_clone( text_h ) 
  349. 	info = Tagging_perk_listing_info[ 4 ] 
  350. 	info.text_h = text_h 
  351. 	vint_set_property( text_h, "visible", false ) 
  352.  
  353. 	-- Move the clone 
  354. 	local point_h = vint_object_find( "perk_pt4", group_h ) 
  355. 	local x, y = vint_get_property( point_h, "anchor" ) 
  356. 	vint_set_property( text_h, "anchor", x, y ) 
  357.  
  358. 	for i, perk_info in pairs(Tagging_perk_listing_info) do 
  359. 		Tagging_perk_listing_info[i].scale_tween_h = create_tween("tagging_perk_scale_tween"..i, perk_info.text_h, "scale", .25) 
  360. 		Tagging_perk_listing_info[i].color_tween_h = create_tween("tagging_perk_color_tween"..i, perk_info.text_h, "tint", .25) 
  361. 		vint_set_property(Tagging_perk_listing_info[i].scale_tween_h, "state", "disabled") 
  362. 		vint_set_property(Tagging_perk_listing_info[i].color_tween_h, "state", "disabled") 
  363. 	end 
  364.  
  365.    vint_datagroup_add_subscription("sr2_sa_tagging_perk_listing_group", "insert", "tagging_perk_listing_change") 
  366. 	vint_datagroup_add_subscription("sr2_sa_tagging_perk_listing_group", "update", "tagging_perk_listing_change") 
  367. end 
  368.  
  369. function tagging_perk_listing_hide() 
  370. 	vint_set_property( vint_object_find( "tagging_perk_listing" ), "visible", false ) 
  371. end 
  372.  
  373. function tagging_perk_listing_change( data_item_h, event_name ) 
  374.  
  375. 	local id, perk_name, owner_team, current_tagger_team = vint_dataitem_get( data_item_h ) 
  376. 	 
  377. 	-- Find the right perk info to edit 
  378. 	local info = Tagging_perk_listing_info[ id ] 
  379. 	if info == nil then 
  380. 		debug_print( "mpvint", "ERROR!  Tagging perk listing " .. id .. " not found\n" ) 
  381. 		return 
  382. 	end 
  383. 	 
  384. 	local visible = ( perk_name ~= "" ) 
  385. 	vint_set_property( info.text_h, "visible", visible ) 
  386. 	 
  387. 	if visible then 
  388. 		vint_set_property( info.text_h, "text_tag", perk_name ) 
  389. 	else 
  390. 		return 
  391. 	end 
  392.  
  393. 	-- The current tag is no longer being tagged. Return the perk text to the new owner. 
  394. 	if current_tagger_team == FOF_NEUTRAL then 
  395. 	 
  396. 		vint_set_property(info.color_tween_h, "state", "disabled") 
  397.  
  398. 		local new_color = get_color_from_fof_type(owner_team) 
  399.  
  400. 		vint_set_property(info.text_h, "tint", new_color.r, new_color.g, new_color.b) 
  401.  
  402. 		local scale_tween_state = vint_get_property(info.scale_tween_h, "state") 
  403. 		 
  404. 		if scale_tween_state ~= 3 then -- 3 is the state value of "disabled" 
  405. 			local start_scale = {} 
  406. 			start_scale.x, start_scale.y = vint_get_property(info.scale_tween_h, "start_value") 
  407. 			vint_set_property(info.text_h, "scale", start_scale.x, start_scale.y) 
  408. 			vint_set_property(info.scale_tween_h, "state", "disabled") 
  409. 		end 
  410.  
  411. 	else 
  412.  
  413. 		local start_color = get_color_from_fof_type(owner_team) 
  414.  
  415. 		local end_color = get_color_from_fof_type(current_tagger_team) 
  416.  
  417. 		vint_set_property(info.color_tween_h, "start_value", start_color.r, start_color.g, start_color.b) 
  418. 		vint_set_property(info.color_tween_h, "end_value", end_color.r, end_color.g, end_color.b) 
  419. 		vint_set_property(info.color_tween_h, "loop_mode", "bounce") 
  420. 		vint_set_property(info.color_tween_h, "max_loops", 0) 
  421. 		 
  422. 		vint_set_property(info.color_tween_h, "start_time", vint_get_time_index()) 
  423. 		vint_set_property(info.color_tween_h, "state", "running") 
  424. 		 
  425. 		local start_size = {} 
  426. 		start_size.width, start_size.length = vint_get_property( info.text_h, "scale" ) 
  427. 		local zoom_size = {} 
  428. 		zoom_size.width = start_size.width * 1.25 
  429. 		zoom_size.length = start_size.length * 1.25 
  430.  
  431. 		vint_set_property(info.scale_tween_h, "start_value", start_size.width, start_size.length) 
  432. 		vint_set_property(info.scale_tween_h, "end_value", zoom_size.width, zoom_size.length) 
  433. 		vint_set_property(info.scale_tween_h, "loop_mode", "bounce") 
  434. 		vint_set_property(info.scale_tween_h, "max_loops", 0) 
  435.  
  436. 		vint_set_property(info.scale_tween_h, "start_time", vint_get_time_index()) 
  437. 		vint_set_property(info.scale_tween_h, "state", "running") 
  438. 	 
  439. 	end 
  440.  
  441. end 
  442.  
  443. ------------------- 
  444. -- Init and Cleanup 
  445. ------------------- 
  446.  
  447. function hud_multi_prototype_init() 
  448. 	local gp_mode = get_game_play_mode() 
  449. 	debug_print( "mpvint", "Game play mode is " .. gp_mode .. "\n" ) 
  450. 	 
  451. 	-- Hide everything to start 
  452. 	score_bars_hide() 
  453. 	game_timer_hide() 
  454. 	activity_score_bar_hide() 
  455. 	tagging_progress_hide() 
  456. 	tagging_perk_listing_hide() 
  457. 	 
  458. 	if gp_mode == "Braveheart" then 
  459. 		score_bars_init() 
  460. 		game_timer_init() 
  461. 		activity_score_bar_init() 
  462. 		tagging_progress_init() 
  463. 		tagging_perk_listing_init() 
  464. 		 
  465. 	elseif gp_mode == "Gangsta Brawl" or gp_mode == "Team Gangsta Brawl" then 
  466. 		score_bars_init() 
  467. 		game_timer_init() 
  468. 	end 
  469. end 
  470.  
  471. function hud_multi_prototype_cleanup() 
  472.  
  473.  
  474. end 
  475.