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