./cell_stats.lua

  1. local CELL_STATS_NUM_LINES 	= 10 
  2.  
  3. local Input_tracker 
  4. local Mouse_input_tracker 
  5. local Hint_bar 
  6.  
  7. local Cell_stats_doc_handle = -1 
  8. local Cell_stats_objects = { } 
  9. local Cell_stats_page = 1 
  10. local Cell_stats_num_pages = 1 
  11. local Num_stat_data = 0 
  12. local Cell_stats_data = { } 
  13. local Arrow_left 
  14. local Arrow_right 
  15. local Page_header 
  16.  
  17. function cell_stats_init() 
  18. 	Cell_stats_doc_handle = vint_document_find("cell_stats") 
  19.  
  20. 	-- Subscribe to the button presses we need 
  21. 	Input_tracker = Vdo_input_tracker:new() 
  22. 	Input_tracker:add_input("nav_left", "cell_stats_nav_left", 50) 
  23. 	Input_tracker:add_input("nav_right", "cell_stats_nav_right", 50) 
  24. 	Input_tracker:add_input("back", "cell_stats_button_b", 50) 
  25. 	Input_tracker:add_input("map", "cell_stats_button_map", 50) 
  26. 	-- unused 
  27. 	Input_tracker:add_input("nav_up", "cell_stats_nav_nothing", 50) 
  28. 	Input_tracker:add_input("nav_down", "cell_stats_nav_nothing", 50)	 
  29. 	Input_tracker:add_input("select", "cell_stats_nav_nothing", 50) 
  30.  
  31. 	--hintbar... 
  32. 	local hint_data = { 
  33. 		{CTRL_MENU_BUTTON_B, "MENU_BACK"}, 
  34. 	} 
  35. 	 
  36. 	Hint_bar = Vdo_hint_bar:new("hint_bar") 
  37. 	Hint_bar:set_hints(hint_data) 
  38.  
  39. 	--Set up title 
  40. 	local cell_title_h = Vdo_cell_title:new("cell_title", 0, Cell_stats_doc_handle) 
  41. 	cell_title_h:set_text("MENU_STATS") 
  42. 	cell_title_h:set_icon("ui_cell_icon_stats") 
  43. 	cell_title_h:play_dots_anim() 
  44. 	 
  45. 	--add right and left group objects	 
  46. 	local label_grp_h = vint_object_find("label_grp", 0, Cell_stats_doc_handle) 
  47. 	local value_grp_h = vint_object_find("value_grp", 0, Cell_stats_doc_handle) 
  48. 	 
  49. 	for i = 1, CELL_STATS_NUM_LINES do 
  50. 		Cell_stats_objects[i] = { 
  51. 			label_h = vint_object_find("label_" .. i, label_grp_h), 
  52. 			value_h = vint_object_find("value_" .. i, value_grp_h), 
  53. 		} 
  54. 	end 
  55. 	 
  56. 	-- populate the stats 
  57. 	vint_dataresponder_request("cell_stats_populate", "cell_stats_populate", 0) 
  58. 	Cell_stats_num_pages = Num_stat_data / CELL_STATS_NUM_LINES 
  59. 	Cell_stats_page = 1 
  60. 	cell_stats_show_page(Cell_stats_page) 
  61. 	 
  62. 	-- Add mouse input subscriptions for the PC 
  63. 	if game_get_platform() == "PC" then 
  64. 		Arrow_left = Vdo_base_object:new("arrow_l", 0, Cell_stats_doc_handle) 
  65. 		Arrow_right = Vdo_base_object:new("arrow_r", 0, Cell_stats_doc_handle) 
  66. 		Page_header =  Vdo_base_object:new("scroll_bg", 0, Cell_stats_doc_handle) 
  67. 		Hint_bar:set_highlight(0) 
  68. 	 
  69. 		Mouse_input_tracker = Vdo_input_tracker:new() 
  70. 		Hint_bar:add_mouse_inputs("cell_stats", Mouse_input_tracker)		 
  71. 		Mouse_input_tracker:add_mouse_input("mouse_move", "cell_stats_mouse_move", 0, Page_header.handle) 
  72. 		Mouse_input_tracker:add_mouse_input("mouse_down", "cell_stats_mouse_down", 0, Page_header.handle) 
  73. 		Mouse_input_tracker:add_mouse_input("mouse_click", "cell_stats_mouse_click", 0, Page_header.handle) 
  74. 		Mouse_input_tracker:add_mouse_input("mouse_drag", "cell_stats_mouse_drag", 0, Page_header.handle) 
  75. 		Mouse_input_tracker:add_mouse_input("mouse_drag_release", "cell_stats_mouse_drag_release", 0, Page_header.handle) 
  76. 		Mouse_input_tracker:subscribe(false) 
  77. 	end 
  78. 	 
  79. 	--Transition the screen in... 
  80. 	cell_transition_screen(CELL_STATE_LANDSCAPE, CELL_SCREEN_STATS, CELL_SCREEN_MAIN, cell_stats_unlock_controls) 
  81. 	--glitch 
  82. 	glitch_cell() 
  83. end 
  84.  
  85. function cell_stats_cleanup() 
  86. 	-- Nuke all button subscriptions 
  87. 	Input_tracker:subscribe(false) 
  88. 	 
  89. 	if Mouse_input_tracker ~= nil then 
  90. 		Mouse_input_tracker:subscribe(false) 
  91. 	end 
  92. end 
  93.  
  94. function cell_stats_populate(display_name, has_value, value_str) 
  95. 	Cell_stats_data[Num_stat_data] = { } 
  96. 	Cell_stats_data[Num_stat_data].display_name = display_name 
  97. 	if has_value == 1 then 
  98. 		Cell_stats_data[Num_stat_data].value = value_str 
  99. 	end 
  100. 	Num_stat_data = Num_stat_data + 1 
  101. end 
  102.  
  103. function cell_stats_show_page(page_idx) 
  104. 	local stat_index = (page_idx - 1) * CELL_STATS_NUM_LINES 
  105. 	 
  106. 	for i = 1, CELL_STATS_NUM_LINES do 
  107. 		if stat_index < Num_stat_data then 
  108. 			vint_set_property(Cell_stats_objects[i].label_h, "visible", true) 
  109. 			vint_set_property(Cell_stats_objects[i].label_h, "text_tag", Cell_stats_data[stat_index].display_name) 
  110. 			 
  111. 			--checks if label string is too long and scales 
  112. 			vint_set_property(Cell_stats_objects[i].label_h, "scale", 1,1) 
  113. 			local MAX_WIDTH_LABEL = 555 
  114. 			local text_width_label, text_height_label = element_get_actual_size(Cell_stats_objects[i].label_h) 
  115.  
  116. 			if text_width_label > MAX_WIDTH_LABEL then 
  117. 				local text_scale_label = MAX_WIDTH_LABEL/text_width_label 
  118. 				vint_set_property(Cell_stats_objects[i].label_h, "scale", text_scale_label,1) 
  119. 			end 
  120. 			 
  121. 			 
  122. 			if Cell_stats_data[stat_index].value ~= nil then 
  123. 				vint_set_property(Cell_stats_objects[i].value_h, "visible", true) 
  124. 				vint_set_property(Cell_stats_objects[i].value_h, "text_tag", Cell_stats_data[stat_index].value) 
  125. 				 
  126. 				--checks if value string is too long and scales 
  127. 				vint_set_property(Cell_stats_objects[i].value_h, "scale", 1,1) 
  128. 				local MAX_WIDTH_VALUE = 211 
  129. 				local text_width_value, text_height_value = element_get_actual_size(Cell_stats_objects[i].value_h) 
  130.  
  131. 				if text_width_value > MAX_WIDTH_VALUE then 
  132. 					local text_scale_value = MAX_WIDTH_VALUE/text_width_value 
  133. 					vint_set_property(Cell_stats_objects[i].value_h, "scale", text_scale_value,1) 
  134. 				end 
  135. 				 
  136. 			else 
  137. 				vint_set_property(Cell_stats_objects[i].value_h, "visible", false) 
  138. 			end 
  139. 		else 
  140. 		 
  141. 			vint_set_property(Cell_stats_objects[i].label_h, "visible", false) 
  142. 			vint_set_property(Cell_stats_objects[i].value_h, "visible", false) 
  143. 		end 
  144. 		stat_index = stat_index + 1 
  145. 	end 
  146. 	 
  147. 	--Cell_stats_num_pages = 3 
  148. 	 
  149. 	--Sets the text in the scroll bar 
  150. 	local current_page_h = vint_object_find("current_page", 0, Cell_stats_doc_handle) 
  151. 	local total_page_round = ceil(Cell_stats_num_pages) 
  152. 	 
  153. 	local insert_values = {[0] = Cell_stats_page, [1] = total_page_round} 
  154. 	local stats_string = vint_insert_values_in_string("STATS_PAGE", insert_values) 
  155. 	vint_set_property(current_page_h, "text_tag", stats_string) 
  156. 	vint_set_property(current_page_h, "scale", 1,1) 
  157. 	local max_width = 130 
  158. 	local text_width, text_height = element_get_actual_size(current_page_h) 
  159.  
  160. 	if text_width > max_width then 
  161. 		local text_scale = max_width/text_width 
  162. 		vint_set_property(current_page_h, "scale", text_scale,1) 
  163. 	end 
  164.  
  165. 	 
  166. 	----------------------------------------------------------------- 
  167. 	--THIS EQUATION IS MAGIC, FOR THE LOVE OF GOD DON'T DELETE IT! 
  168. 	--sets scroll bar in correct spot 
  169. 	--629 is the width of the total bg minus the scroll bar (188) 
  170. 	--The -475 offsets it to the correct spot on the screen 
  171. 	----------------------------------------------------------------- 
  172. 	 
  173. 	local scroll_pct = (((Cell_stats_page / total_page_round) * 629) - ((1 / total_page_round ) * 629))   +   ((((Cell_stats_page - 1) / total_page_round) / (total_page_round - 1)) * 629)  - 475    
  174. 	 
  175. 	local scroll_grp_h = vint_object_find("scroll_grp", 0, Cell_stats_doc_handle) 
  176. 	vint_set_property(scroll_grp_h, "anchor", scroll_pct, 0) 
  177. end 
  178.  
  179. MOUSE_DOWN = 0 
  180. local PAGE_SPACING 
  181. function cells_stats_adjust_page_position(mouse_diff_x, pure_mouse_x) 
  182. 	local moved = false; 
  183. 	PAGE_SPACING = 629 / (Cell_stats_num_pages + 1) 
  184. 	--local x = Page_header:get_anchor() 
  185. 	 
  186. 	if mouse_diff_x > PAGE_SPACING * 1.0 then 
  187. 		cell_stats_nav_right() 
  188. 		moved = true 
  189. 	elseif mouse_diff_x < PAGE_SPACING * -1.0 then 
  190. 		cell_stats_nav_left() 
  191. 		moved = true 
  192. 	end 
  193. 	if moved == true then 
  194. 		--x = Page_header:get_anchor() 
  195. 		MOUSE_DOWN = pure_mouse_x 
  196. 	end 
  197. end 
  198.  
  199. function cell_stats_nav_left(event, acceleration) 
  200. 	if Cell_stats_page > 1 then 
  201. 		Cell_stats_page = Cell_stats_page - 1  
  202. 		cell_stats_show_page(Cell_stats_page) 
  203. 		ui_audio_post_event("UI_Hub_Navigate") 
  204. 	end 
  205. end 
  206.  
  207. function cell_stats_nav_right(event, acceleration) 
  208. 	if Cell_stats_page < Cell_stats_num_pages then 
  209. 		Cell_stats_page = Cell_stats_page + 1  
  210. 		cell_stats_show_page(Cell_stats_page) 
  211. 		ui_audio_post_event("UI_Hub_Navigate") 
  212. 	end 
  213. end 
  214.  
  215. function cell_stats_nav_nothing(event, accel) 
  216. end 
  217.  
  218. function cell_stats_button_map(event, acceleration) 
  219. 	ui_audio_post_event("UI_Hub_Menu_Back") 
  220. 	cell_stats_lock_controls() 
  221. 	 
  222. 	--Transition the screen out 
  223. 	cell_transition_screen(CELL_STATE_OUT, CELL_SCREEN_MAIN, CELL_SCREEN_STATS, cell_stats_exit_to_game) 
  224. end 
  225.  
  226. function cell_stats_button_b(event, acceleration) 
  227. 	ui_audio_post_event("UI_Hub_Menu_Back") 
  228. 	cell_stats_lock_controls() 
  229. 	 
  230. 	--Transition the screen to extras 
  231. 	cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MAIN, CELL_SCREEN_STATS, cell_stats_exit_to_extras) 
  232. end 
  233.  
  234. --Added missing *_unload() function 
  235. function cell_stats_unload() 
  236. 	ui_audio_post_event("UI_Hub_Menu_Back") 
  237. 	--Transition the screen to extras 
  238. 	cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MAIN, CELL_SCREEN_STATS, nil) 
  239. end 
  240.  
  241. function cell_stats_exit_to_extras() 
  242. 	pop_screen()	-- stats 
  243. end 
  244.  
  245. function cell_stats_exit_to_game() 
  246. 	pop_screen()	-- stats 
  247. 	pop_screen()	-- extras 
  248. 	pop_screen()	-- main 
  249. 	pop_screen()	-- cell frame 
  250. end 
  251.  
  252. function cell_stats_lock_controls() 
  253. 	Input_tracker:subscribe(false) 
  254. 	 
  255. 	if Mouse_input_tracker ~= nil then 
  256. 		Mouse_input_tracker:subscribe(false) 
  257. 	end 
  258. end 
  259.  
  260. function cell_stats_unlock_controls() 
  261. 	Input_tracker:subscribe(true) 
  262. 	 
  263. 	if Mouse_input_tracker ~= nil then 
  264. 		Mouse_input_tracker:subscribe(true) 
  265. 	end 
  266. end 
  267.  
  268. function cell_stats_mouse_click(event, target_handle) 
  269. 	-- First check if the target_handle is in the hint bar 
  270. 	local hint_index =Hint_bar:get_hint_index(target_handle) 
  271. 	if hint_index == 1 then 
  272. 		cell_stats_button_b(event) 
  273. 	end 
  274. 	 
  275. 	if target_handle == Arrow_left.handle then 
  276. 		cell_stats_nav_left() 
  277. 	elseif target_handle == Arrow_right.handle then 
  278. 		cell_stats_nav_right() 
  279. 	end 
  280. end 
  281.  
  282. function cell_stats_mouse_move(event, target_handle) 
  283.  
  284. 	 
  285. 	-- Reset highlights 
  286. 	Hint_bar:set_highlight(0) 
  287. 	local hint_index = Hint_bar:get_hint_index(target_handle) 
  288. 	if hint_index ~= 0 then 
  289. 		Hint_bar:set_highlight(hint_index) 
  290. 	end 
  291. 	 
  292.  
  293. 	-- Highlight the page header 
  294. 	local page_text = vint_object_find("current_page", 0, Cell_stats_doc_handle) 
  295. 	vint_set_property(page_text, "tint", 0, 0, 0) 
  296. 	 
  297. 	-- Highlight the arrows 
  298. 	Arrow_left:set_color(0, 0, 0) 
  299. 	Arrow_right:set_color(0, 0, 0) 
  300.  
  301. 	if target_handle == Page_header.handle then 
  302. 		vint_set_property(page_text, "tint", COLOR_CELL_MENU_HIGHLIGHT_TEXT.R, COLOR_CELL_MENU_HIGHLIGHT_TEXT.G, COLOR_CELL_MENU_HIGHLIGHT_TEXT.B) 
  303. 		Arrow_left:set_color(COLOR_CELL_MENU_HIGHLIGHT_TEXT) 
  304. 		Arrow_right:set_color(COLOR_CELL_MENU_HIGHLIGHT_TEXT) 
  305. 		vint_set_mouse_cursor("Ui_cursor_hand_open") 
  306. 	else 
  307. 		vint_set_mouse_cursor("") 
  308. 	end 
  309. end 
  310.  
  311. function cell_stats_mouse_drag(event, target_handle, mouse_x) 
  312. 	if target_handle == Page_header.handle then 
  313. 		cells_stats_adjust_page_position(mouse_x - MOUSE_DOWN, mouse_x) 
  314. 		vint_set_mouse_cursor("Ui_cursor_hand_closed") 
  315. 	end 
  316. end 
  317.  
  318. function cell_stats_mouse_drag_release(event, target_handle, mouse_x, mouse_y, scroll_lines, current_target_handle) 
  319. 	debug_print("vint", "highlighted_handle: " 			.. var_to_string(current_target_handle) .. "\n") 
  320. 	debug_print("vint", "Page_header.handle: " 	.. var_to_string(Page_header.handle) .. "\n") 
  321. 	if current_target_handle == Page_header.handle then 
  322. 		vint_set_mouse_cursor("Ui_cursor_hand_open") 
  323. 	else 
  324. 		vint_set_mouse_cursor("") 
  325. 	end 
  326. end 
  327.  
  328. function cell_stats_mouse_down(event, target_handle, mouse_x) 
  329. 	if target_handle == Page_header.handle then 
  330. 		MOUSE_DOWN = mouse_x 
  331. 	end 
  332. end 
  333.  
  334. function cell_stats_mouse_release(event, target_handle, mouse_x) 
  335. 	if target_handle == Page_header.handle then 
  336. 		MOUSE_DOWN = mouse_x 
  337. 		vint_set_mouse_cursor("Ui_cursor_hand_closed") 
  338. 	end 
  339. end