./cell_missions.lua

  1. local Data = { } 
  2. Num_items = 0 
  3.  
  4. local Input_tracker 
  5. local Mouse_input_tracker 
  6. local Hint_bar 
  7. local Call_bar 
  8. local Call_text 
  9. local Mission_in_progress = false 
  10.  
  11. local MOUSE_INVALID_HANDLE = -1 
  12.  
  13. local List = nil 
  14. local Call_grp 
  15. local Cell_missions_handle = -1 
  16.  
  17. local Mouse_area_h = -1 
  18.  
  19. function cell_missions_init() 
  20. 	Cell_missions_handle = vint_document_find("cell_missions") 
  21.  
  22. 	-- Subscribe to the button presses we need 
  23. 	Input_tracker = Vdo_input_tracker:new() 
  24. 	Input_tracker:add_input("nav_up", "cell_missions_nav_up", 50) 
  25. 	Input_tracker:add_input("nav_down", "cell_missions_nav_down", 50)	 
  26. 	Input_tracker:add_input("back", "cell_missions_button_b", 50) 
  27. 	Input_tracker:add_input("select", "cell_missions_button_a", 50) 
  28. 	Input_tracker:add_input("map", "cell_missions_button_map", 50) 
  29.  
  30.  
  31. 	-- This needs to be hidden when in a mission and no missions are available 
  32. 	Call_grp = Vdo_base_object:new("call_grp") 
  33. 	 
  34. 	local title = Vdo_base_object:new("title") 
  35. 	title:set_text("MENU_MISSIONS_TITLE") 
  36. 	 
  37. 	Call_text = Vdo_base_object:new("call_txt") 
  38. 	Call_text:set_text("CELL_OPTION_CALL") 
  39. 	Call_text:set_color(0, 0, 0) 
  40. 	 
  41. 	Call_bar = Vdo_base_object:new("call_img") 
  42. 	 
  43. 	local in_progress_txt = Vdo_base_object:new("in_progress_txt", 0, Cell_missions_handle) 
  44. 	in_progress_txt:set_visible(false) 
  45. 	 
  46. 	local current_mission_txt = Vdo_base_object:new("current_mission_txt", 0, Cell_missions_handle) 
  47. 	current_mission_txt:set_visible(false) 
  48. 	current_mission_txt:set_text("MISSIONS_CURRENT") 
  49. 	 
  50. 	vint_dataresponder_request("cell_missions_list_items_dr", "cell_missions_add_menu_items", 0) 
  51. 		 
  52. 	List = Vdo_cell_missions_list:new("list_missions") 
  53. 	 
  54. 	-- Draw list or handle mission in progress text 
  55. 	cell_missions_update()	 
  56. 	 
  57. 	local hint_btn = Vdo_hint_button:new("btn_a", 0, Cell_missions_handle) 
  58. 	hint_btn:set_button(CTRL_MENU_BUTTON_A) 
  59. 	 
  60. 	--Make sure our cellphone frame is in view, sometimes we could come straight into this menu... 
  61. 	cell_show_frame() 
  62. 	 
  63. 	-- Add mouse inputs for the PC 
  64. 	if game_get_platform() == "PC" then		 
  65. 		Mouse_input_tracker = Vdo_input_tracker:new() 
  66. 		if Num_items > 0 and Mission_in_progress == false then 
  67. 			List:add_mouse_inputs("cell_missions", Mouse_input_tracker)				 
  68. 			 
  69. 			Mouse_area_h = vint_object_find("missions_mouse_area_img", 0, Cell_missions_handle)			 
  70. 			Mouse_input_tracker:add_mouse_input("mouse_scroll", "cell_missions_mouse_scroll", 50, Mouse_area_h) 
  71. 			Mouse_input_tracker:add_mouse_input("mouse_move", "cell_missions_mouse_scroll", 50, List.scrollbar.tab.handle) 
  72. 			Mouse_input_tracker:add_mouse_input("mouse_click", "cell_missions_mouse_scroll", 50, List.scrollbar.tab.handle) 
  73. 			Mouse_input_tracker:add_mouse_input("mouse_drag", "cell_missions_mouse_drag", 50, List.scrollbar.tab.handle) 
  74. 			Mouse_input_tracker:add_mouse_input("mouse_drag_release", "cell_missions_mouse_drag_release", 50, List.scrollbar.tab.handle) 
  75.  
  76. 			vint_set_property(List.scrollbar.tab.handle, "mouse_depth", -5001) 
  77. 		end 
  78. 		 
  79. 		Call_grp:set_visible(false) 
  80. 		 
  81. 		-- Setup Button Hints 
  82. 		Hint_bar = Vdo_hint_bar:new("hint_bar", 0, Cell_missions_handle) 
  83. 		local hint_data = { 
  84. 			{CTRL_MENU_BUTTON_B, "MENU_BACK"}, 
  85. 		} 
  86. 		Hint_bar:set_hints(hint_data)  
  87. 		Hint_bar:set_visible(true)	 
  88. 		Hint_bar:set_highlight(0) 
  89. 		Hint_bar:add_mouse_inputs("cell_missions", Mouse_input_tracker) 
  90. 	end 
  91. 	 
  92. 	if Missions_from_menu then 
  93. 		--Transition the screen in... 
  94. 		cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MISSIONS, CELL_SCREEN_MAIN, cell_missions_unlock_controls) 
  95. 	else 
  96. 		--Transition the screen in... 
  97. 		cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MISSIONS, CELL_SCREEN_NONE, cell_missions_unlock_controls) 
  98. 	end 
  99. end 
  100.  
  101. function cell_missions_cleanup() 
  102. 	-- Nuke all button subscriptions 
  103. 	Input_tracker:subscribe(false) 
  104. 	 
  105. 	if Mouse_input_tracker ~= nil then 
  106. 		Mouse_input_tracker:subscribe(false) 
  107. 	end 
  108. end 
  109.  
  110. function cell_missions_add_menu_items(handle, mission_name, display_name, contact_name, contact_image, is_new, in_progress) 
  111. 	Num_items = Num_items + 1 
  112. 	Data[Num_items] = { handle = handle, mission_name = mission_name, display_name = display_name, contact_name = contact_name, contact_image = contact_image, is_new = is_new } 
  113. 	Mission_in_progress = in_progress 
  114. end 
  115.  
  116. function cell_missions_update()	 
  117. 	-- Check if we're in a mission 
  118. 	if Mission_in_progress then 
  119. 		-- Mission is in progress so hide everything except the in progress message 
  120. 		local contact_grp = Vdo_base_object:new("contact_grp", 0, Cell_missions_handle) 
  121. 		contact_grp:set_visible(false)	 
  122.  
  123. 		local current_mission_txt = Vdo_base_object:new("current_mission_txt", 0, Cell_missions_handle) 
  124. 		current_mission_txt:set_text("MISSIONS_CURRENT") 
  125. 		current_mission_txt:set_visible(true) 
  126. 			 
  127. 		local in_progress_txt = Vdo_base_object:new("in_progress_txt", 0, Cell_missions_handle) 
  128. 		in_progress_txt:set_text(Data[1].display_name) 
  129. 		in_progress_txt:set_visible(true)	 
  130. 		 
  131. 		--Safe to show mission text... 
  132. 		vint_set_property(vint_object_find("missions_grp"), "visible", true) 
  133. 	else 
  134. 		if Num_items > 0 then 
  135. 			List:draw_items(Data, 1, 4) 
  136. 			 
  137. 			--Safe to show missions... 
  138. 			vint_set_property(vint_object_find("missions_grp"), "visible", true) 
  139. 		end	 
  140. 	end	 
  141. end 
  142.  
  143. function cell_missions_nav_up(event, acceleration) 
  144. 	if Num_items > 0 then 
  145. 		List:move_cursor(-1) 
  146. 		if Num_items > 1 then 
  147. 			game_UI_audio_play("UI_Main_Menu_Nav_Down") 
  148. 		end 
  149. 	end 
  150. end 
  151.  
  152. function cell_missions_nav_down(event, acceleration) 
  153. 	if Num_items > 0 then 
  154. 		List:move_cursor(1) 
  155. 		if Num_items > 1 then 
  156. 			game_UI_audio_play("UI_Main_Menu_Nav_Down") 
  157. 		end 
  158. 	end 
  159. end 
  160.  
  161. function cell_missions_button_map(event, acceleration) 
  162. 	cell_missions_lock_controls() 
  163. 	vint_dataresponder_post("cell_menu_state_dr", "Set", ID_MAIN) 
  164. 	game_UI_audio_play("UI_Cell_Close") 
  165. 	 
  166. 	--Transition the screen in... 
  167. 	cell_transition_screen(CELL_STATE_OUT, CELL_SCREEN_NONE, CELL_SCREEN_MISSIONS, cell_missions_exit_to_game) 
  168. end 
  169.  
  170. function cell_missions_button_b(event, acceleration) 
  171. 	cell_missions_lock_controls() 
  172. 	game_UI_audio_play("UI_Cell_Nav_Back") 
  173. 	 
  174. 	--Transition the screen in... 
  175. 	cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MAIN, CELL_SCREEN_MISSIONS, cell_missions_exit_to_main) 
  176. end 
  177.  
  178. function cell_missions_button_a(event, acceleration) 
  179.  
  180. 	if Num_items > 0 then 
  181. 		debug_print("vint", "List: " .. var_to_string(List) .. "\n")  
  182. 		debug_print("vint", "Data[List.current_idx]: " .. var_to_string(Data[List.current_idx]) .. "\n")  
  183. 		debug_print("vint", "List.current_idx: " .. var_to_string(List.current_idx) .. "\n")  
  184. 		if List ~= nil and Data[List.current_idx] ~= nil then 
  185. 			cell_missions_lock_controls() 
  186. 			vint_dataresponder_post("cell_missions_list_items_dr", "Choose", Data[List.current_idx].mission_name) 
  187. 			 
  188. 			game_UI_audio_play("UI_Main_Menu_Select") 
  189. 			 
  190. 			--Exit phone... 
  191. 			vint_dataresponder_post("cell_menu_state_dr", "Set", ID_MAIN) 
  192. 			game_UI_audio_play("UI_Cell_Close") 
  193. 		 
  194. 			--Transition the screen in... 
  195. 			cell_transition_screen(CELL_STATE_OUT, CELL_SCREEN_NONE, CELL_SCREEN_MISSIONS, cell_missions_exit_to_game) 
  196. 		end 
  197. 	end 
  198. end 
  199.  
  200. function cell_missions_exit_to_main() 
  201. 	pop_screen() 
  202. end 
  203.  
  204. function cell_missions_exit_to_game() 
  205. 	pop_screen()	--mission 
  206. 	pop_screen()	--main menu 
  207. 	pop_screen()	--cellphone frame 
  208. end 
  209.  
  210. ------------------------------------------------------------------------------- 
  211. -- Controls Unlock  
  212. -- 
  213. function cell_missions_unlock_controls() 
  214. 		 
  215. 	if Num_items == 0 then 
  216. 		--No items, don't unlock anything and exit early 
  217. 		cell_missions_lock_controls() 
  218. 		thread_new("cell_missions_exit_early") 
  219. 		return 
  220. 	end 
  221. 	 
  222. 	Input_tracker:subscribe(true) 
  223. 	if Mouse_input_tracker ~= nil then 
  224. 		Mouse_input_tracker:subscribe(true) 
  225. 	end 
  226. end 
  227.  
  228. function cell_missions_exit_early() 
  229. 		--Wait a frame... 
  230. 		thread_yield() 
  231. 		 
  232. 		--Transition the screen out to main... 
  233. 		cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MAIN, CELL_SCREEN_MISSIONS, cell_missions_exit_to_main) 
  234. end 
  235.  
  236. ------------------------------------------------------------------------------- 
  237. -- Controls Lock 
  238. -- 
  239. function cell_missions_lock_controls() 
  240. 	Input_tracker:subscribe(false) 
  241. 	if Mouse_input_tracker ~= nil then 
  242. 		Mouse_input_tracker:subscribe(false) 
  243. 	end 
  244. end 
  245.  
  246. function cell_missions_mouse_click(event, target_handle) 
  247. 	local hint_index = Hint_bar:get_hint_index(target_handle) 
  248. 	if hint_index == 1 then 
  249. 		cell_missions_button_b() 
  250. 	end 
  251.  
  252. 	if Num_items > 0 and Mission_in_progress == false then 
  253. 		local new_index = List:get_button_index(target_handle) 
  254. 		if new_index ~= 0 then 
  255. 			List:set_selection(new_index, true, false) 
  256. 			cell_missions_button_a() 
  257. 		end 
  258. 	end 
  259. end 
  260.  
  261. function cell_missions_mouse_move(event, target_handle) 
  262. 	Hint_bar:set_highlight(0) 
  263. 	local hint_index = Hint_bar:get_hint_index(target_handle) 
  264. 	if hint_index ~= 0 then 
  265. 		Hint_bar:set_highlight(hint_index) 
  266. 		game_UI_audio_play("UI_Cell_Nav") 
  267. 	end 
  268. 	 
  269. 	if Num_items > 0 and Mission_in_progress == false then 
  270. 		local new_index = List:get_button_index(target_handle) 
  271. 		if new_index ~= 0 and cell_missions_get_valid_handle(target_handle) ~= MOUSE_INVALID_HANDLE then 
  272. 			List:set_selection(new_index, true, false) 
  273. 		end 
  274. 	end 
  275. end 
  276.  
  277. function cell_missions_mouse_scroll(event, target_handle, mouse_x, mouse_y, scroll_lines)			 
  278. 	--scroll lines are inverted for some reason 
  279. 	scroll_lines = scroll_lines * -1 
  280.  
  281. 	local num_missions = #Data	 
  282. 	--if list has enough items to scroll then allow mouse wheel 
  283. 	if num_missions > 4 then	 
  284. 		if scroll_lines > 0 then 
  285. 			cell_missions_nav_down() 
  286. 		elseif scroll_lines < 0 then 
  287. 			cell_missions_nav_up() 
  288. 		end 
  289. 	end 
  290. end 
  291.  
  292. function cell_missions_mouse_drag(event, target_handle, mouse_x, mouse_y, scroll_lines) 
  293. 	if List.scrollbar.tab.handle == target_handle then 
  294. 		local new_start_index = List.scrollbar:drag_scrolltab(mouse_y, #Data) 
  295. 		 
  296. 		--don't highlight anything, just scroll the list. 
  297. 		List:set_selection(new_start_index, false, true) 
  298. 	end 
  299. end 
  300.  
  301. function cell_missions_mouse_drag_release(event, target_handle, mouse_x, mouse_y, scroll_lines) 
  302. 	-- scroll phone book 
  303. 	if List.scrollbar.tab.handle == target_handle then 
  304. 		List.scrollbar:release_scrolltab(List.current_idx, #Data) 
  305. 	end 
  306. end 
  307.  
  308. --Search through active buttons and determine if the mouse is over a valid target 
  309. -- 
  310. function cell_missions_get_valid_handle(target_handle) 
  311. 	for idx, current_button in pairs(List.buttons) do 
  312. 		if current_button.handle ~= nil and current_button.handle == target_handle then			 
  313. 			--if selection is visible then return idx 
  314. 			if idx >= List.visible_start_idx and idx <= List.visible_end_idx then 
  315. 				return idx 
  316. 			end			 
  317. 		end 
  318. 	end 
  319. 	 
  320. 	return MOUSE_INVALID_HANDLE	 
  321. end