./cell_cheats.lua

  1. local	CELL_CHEATS_POPULATE_CATEGORIES 	= 0 
  2. local	CELL_CHEATS_POPULATE_CHEATS		= 1 
  3.  
  4. local ID_CHEAT	= -1 
  5.  
  6. local Category_menu = { } 
  7. local Num_category_items = 1 
  8. local Category_selected_idx = 1 
  9. local Sub_menu = { } 
  10. local Num_submenu_items = 0 
  11. local Unlocked_cheat 
  12. local Cheat_just_unlocked = false 
  13. local Sub_menu_category = 1 
  14.  
  15. local Add_cheat_button = { label = "ADD_CHEAT", 	type = TYPE_BUTTON, id = ID_CHEAT		} 
  16.  
  17. local Sub_menu_is_active = false 
  18.  
  19. local Input_tracker 
  20. local Mouse_input_tracker 
  21. local Hint_bar 
  22. local Continue_hint_bar 
  23.  
  24. Cheats_list		 = -1				--This must be global because the megalist needs it to be for callback (JMH 4/4/2011) 
  25.  
  26. local Cell_cheats_doc_handle = -1 
  27. local CHEATS_LIST_WIDTH = 354 
  28. local CHEATS_LIST_NUM_OPTIONS = 9 
  29. local CHEATS_LIST_SCALE = .9 
  30. local CHEATS_LIST_TITLE_OFFSET = 30		--Offset to accomodate for header on submenus. 
  31.  
  32. local Cheat_being_entered = false 
  33.  
  34. --Base position for cheatslist... 
  35. local Cheats_list_start_x, Cheats_list_start_y 
  36.  
  37. function cell_cheats_init() 
  38. 	Cell_cheats_doc_handle = vint_document_find("cell_cheats") 
  39.  
  40. 	-- Subscribe to the button presses we need 
  41. 	Input_tracker = Vdo_input_tracker:new() 
  42. 	Input_tracker:add_input("nav_up", "cell_cheats_nav_up", 50) 
  43. 	Input_tracker:add_input("nav_down", "cell_cheats_nav_down", 50)	 
  44. 	Input_tracker:add_input("back", "cell_cheats_button_b", 50) 
  45. 	Input_tracker:add_input("select", "cell_cheats_button_a", 50) 
  46. 	Input_tracker:add_input("map", "cell_cheats_button_map", 50) 
  47.  
  48. 	local grp_h = vint_object_find("new_cheat_grp", 0, Cell_cheats_doc_handle) 
  49. 	vint_set_property(grp_h, "visible", false) 
  50.  
  51. 	local title_h = vint_object_find("title", 0, Cell_cheats_doc_handle) 
  52. 	vint_set_property(title_h, "text_tag", "HELP_TEXT_CHEATS_TITLE") 
  53. 	 
  54. 	local data = { 
  55. 		{CTRL_MENU_BUTTON_A, "MAINMENU_CONTINUE"}, 
  56. 	} 
  57. 	Continue_hint_bar = Vdo_hint_bar:new("cheat_btn_hint_bar", grp_h) 
  58. 	Continue_hint_bar:set_hints(data) 
  59. 	 
  60. 	Cheats_list = Vdo_mega_list:new("cheats_list", 0, Cell_cheats_doc_handle, "cell_cheats.lua", "Cheats_list") 
  61. 	Cheats_list:set_highlight_color(COLOR_CHEATS_PRIMARY, COLOR_CHEATS_SECONDARY)	 
  62. 	 
  63. 	--Get base position for cheatslist... 
  64. 	Cheats_list_start_x, Cheats_list_start_y = vint_get_property(Cheats_list.handle, "anchor") 
  65. 	 
  66. 	cell_cheats_enter_category_menu() 
  67. 	 
  68. 	--Transition the screen in... 
  69. 	cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_CHEATS, CELL_SCREEN_EXTRAS, cell_cheats_unlock_controls) 
  70. 	 
  71. 	if game_get_platform() == "PC" then 
  72. 		-- Setup Button Hints 
  73. 		Hint_bar = Vdo_hint_bar:new("hint_bar", 0, Cell_cheats_doc_handle) 
  74. 		local hint_data = { 
  75. 			{CTRL_MENU_BUTTON_B, "MENU_BACK"}, 
  76. 		} 
  77. 		Hint_bar:set_hints(hint_data)  
  78. 		Hint_bar:set_visible(true)	 
  79. 		Hint_bar:set_highlight(0) 
  80.  
  81. 		Continue_hint_bar:set_highlight(1, COLOR_CHEATS_SECONDARY) 
  82. 		vint_dataitem_add_subscription("cell_cheats_text", "update", "cell_cheats_text_update") 
  83. 	 
  84. 		Mouse_input_tracker = Vdo_input_tracker:new() 
  85. 		-- Inputs also updated in cell_cheats_mouse_update 
  86. 		Cheats_list:add_mouse_inputs("cell_cheats", Mouse_input_tracker) 
  87. 		Hint_bar:add_mouse_inputs("cell_cheats", Mouse_input_tracker) 
  88. 		Mouse_input_tracker:subscribe(false) 
  89. 	end 
  90. end 
  91.  
  92. function cell_cheats_cleanup() 
  93. 	-- Nuke all button subscriptions 
  94. 	Input_tracker:subscribe(false) 
  95. 	 
  96. 	if Mouse_input_tracker ~= nil then 
  97. 		Mouse_input_tracker:subscribe(false) 
  98. 	end 
  99. end 
  100.  
  101. function cell_cheats_enter_category_menu() 
  102.  
  103. 	local subtitle_h = vint_object_find("subtitle", 0, Cell_cheats_doc_handle) 
  104. 	local description_grp_h = vint_object_find("description_grp", 0, Cell_cheats_doc_handle) 
  105. 	 
  106. 	-- set description text invisble 
  107. 	Cheats_list:set_anchor(Cheats_list_start_x, Cheats_list_start_y) 
  108. 	vint_set_property(description_grp_h, "visible", false) 
  109. 	vint_set_property(subtitle_h, "visible", false) 
  110.  
  111. 	Sub_menu_is_active = false 
  112.  
  113. 	Category_menu = {  
  114. 		[1] = Add_cheat_button, 
  115. 	} 
  116. 	Num_category_items = 1 
  117. 	vint_dataresponder_request("cell_cheats_populate", "cell_cheats_populate_categories", 0, CELL_CHEATS_POPULATE_CATEGORIES) 
  118. 	Cheats_list:draw_items(Category_menu, Category_selected_idx, CHEATS_LIST_WIDTH, CHEATS_LIST_NUM_OPTIONS, CHEATS_LIST_SCALE, true) 
  119. 	 
  120. 	if Hint_bar ~= nil then 
  121. 		Hint_bar:set_anchor(-166, 244) 
  122. 	end 
  123. 	 
  124. 	cell_cheats_mouse_update() 
  125. end 
  126.  
  127. function cell_cheats_mouse_update() 
  128. 	if Mouse_input_tracker ~= nil then 
  129. 		Mouse_input_tracker:remove_all() 
  130. 		Cheats_list:add_mouse_inputs("cell_cheats", Mouse_input_tracker) 
  131. 		Hint_bar:add_mouse_inputs("cell_cheats", Mouse_input_tracker) 
  132. 		Mouse_input_tracker:subscribe(true) 
  133. 	end 
  134. end 
  135.  
  136. function cell_cheats_populate_categories(display_name, id) 
  137. 	Num_category_items = Num_category_items + 1 
  138. 	Category_menu[Num_category_items] = { label = display_name, type = TYPE_BUTTON, id = id } 
  139. end 
  140.  
  141. function cell_cheats_enter_submenu(category_id) 
  142. 	local cheats_list_h = vint_object_find("cheats_list", 0, Cell_cheats_doc_handle) 
  143. 	local description_grp_h = vint_object_find("description_grp", 0, Cell_cheats_doc_handle) 
  144. 	local subtitle_h = vint_object_find("subtitle", 0, Cell_cheats_doc_handle) 
  145. 	 
  146. 	-- Use category name for this 
  147. 	local category_tag = nil 
  148. 	 
  149. 	for idx, category in pairs(Category_menu) do 
  150. 		if category.id == category_id then 
  151. 			category_tag = category.label 
  152. 		end 
  153. 	end 
  154. 	 
  155. 	Num_submenu_items = 0 
  156. 	Sub_menu = { } 
  157. 	vint_dataresponder_request("cell_cheats_populate", "cell_cheats_populate_cheats", 0, CELL_CHEATS_POPULATE_CHEATS, category_id) 
  158. 	Cheats_list:draw_items(Sub_menu, 1, CHEATS_LIST_WIDTH, CHEATS_LIST_NUM_OPTIONS, CHEATS_LIST_SCALE, true) 
  159. 	Sub_menu_is_active = true 
  160. 	Sub_menu_category = category_id 
  161. 		 
  162. 	--Move megalist to accomodate for header... 
  163. 	vint_set_property(Cheats_list.handle, "anchor", Cheats_list_start_x, Cheats_list_start_y + CHEATS_LIST_TITLE_OFFSET) 
  164. 	vint_set_property(description_grp_h, "visible", true) 
  165. 	vint_set_property(subtitle_h, "text_tag", category_tag) 
  166. 	vint_set_property(subtitle_h, "visible", true) 
  167. 	 
  168. 	if Hint_bar ~= nil then 
  169. 		Hint_bar:set_anchor(-166, 130) 
  170. 	end 
  171. 	 
  172. 	cell_cheats_set_description() 
  173. 	cell_cheats_mouse_update() 
  174. end 
  175.  
  176. function cell_cheats_populate_cheats(display_name, description, cheat_is_active, cheat_index, toggleable) 
  177. 	Num_submenu_items = Num_submenu_items + 1 
  178. 	Sub_menu[Num_submenu_items] = { 	label = display_name,	type = TYPE_BUTTON, description = description, cheat_index = cheat_index } 
  179. 	if toggleable then 
  180. 		Sub_menu[Num_submenu_items].equipped = cheat_is_active 
  181. 		Sub_menu[Num_submenu_items].owned = true 
  182. 	else  
  183. 		Sub_menu[Num_submenu_items].equipped = false 
  184. 		Sub_menu[Num_submenu_items].owned = false 
  185. 	end 
  186. end 
  187.  
  188. function cell_cheats_set_description() 
  189. 	local selection = Cheats_list:get_selection() 
  190. 	local description = Sub_menu[selection].description 
  191. 	 
  192. 	local description_grp_h = vint_object_find("description_grp", 0, Cell_cheats_doc_handle) 
  193. 	vint_set_property(description_grp_h, "visible", true) 
  194. 	 
  195. 	local description_text_h = vint_object_find("cheats_description", description_grp_h) 
  196. 	vint_set_property(description_text_h, "text_tag", description) 
  197. end 
  198.  
  199. function cell_cheats_cheat_unlocked(cheat_index, display_name, description, category_id) 
  200. 	Cheat_just_unlocked = true 
  201. 	 
  202. 	local unlocked_header 
  203. 	local unlocked_text 
  204. 	if cheat_index == - 1 then 
  205. 		Unlocked_cheat = nil 
  206. 		unlocked_header = "CHEAT_NOT_FOUND" 
  207. 		unlocked_text = "CHEAT_NOT_FOUND_DESC" 
  208. 	else 
  209. 		Unlocked_cheat = { cheat_index = cheat_index, category_id = category_id	} 
  210. 		unlocked_header = display_name 
  211. 		unlocked_text = description 
  212. 		game_UI_audio_play("UI_Cell_Cheat_Unlocked") 
  213. 	end 
  214. 	 
  215. 	Cheats_list:set_visible(false) 
  216. 	local grp_h = vint_object_find("new_cheat_grp") 
  217. 	vint_set_property(grp_h, "visible", true) 
  218. 	 
  219. 	local title_h = vint_object_find("new_cheat_title", grp_h) 
  220. 	local desc_h = vint_object_find("new_cheat_description", grp_h) 
  221. 	 
  222. 	vint_set_property(title_h, "text_tag", unlocked_header) 
  223. 	vint_set_property(desc_h, "text_tag", unlocked_text) 
  224. 	 
  225. 	--Align all elements... 
  226. 	--Align web info... 
  227. 	local data = { 
  228. 		{h = title_h, 	type = VINT_OBJECT_TEXT, space = 0}, 
  229. 		{h = desc_h, 	type = VINT_OBJECT_TEXT, space = 0}, 
  230. 		{h = Continue_hint_bar.handle,	type = VINT_OBJECT_INVALID, space = 25}, 
  231. 	} 
  232. 	local width, height = vint_align_elements(data, true) 
  233. 	 
  234. 	--Hide back button... 
  235. 	if Hint_bar ~= nil then 
  236. 		Hint_bar:set_visible(false) 
  237. 	end 
  238. 			 
  239. 	if Mouse_input_tracker ~= nil then 
  240. 		Mouse_input_tracker:remove_all() 
  241. 		Continue_hint_bar:add_mouse_inputs("cell_cheats", Mouse_input_tracker) 
  242. 		Mouse_input_tracker:subscribe(true) 
  243. 	end 
  244. end 
  245.  
  246. function cell_cheats_cheat_activated(Cheat_index, is_active) 
  247. 	for idx, Cheat  in pairs(Sub_menu) do 
  248. 		if Cheat.cheat_index == Cheat_index then 
  249. 			if Cheat.owned == true then 
  250. 				Cheat.equipped = is_active 
  251. 			end 
  252. 			 
  253. 			-- repopulate the list 
  254. 			Num_submenu_items = 0 
  255. 			Sub_menu = { } 
  256. 			vint_dataresponder_request("cell_cheats_populate", "cell_cheats_populate_cheats", 0, CELL_CHEATS_POPULATE_CHEATS, Sub_menu_category) 
  257. 			Cheats_list:draw_items(Sub_menu, idx, CHEATS_LIST_WIDTH, CHEATS_LIST_NUM_OPTIONS, CHEATS_LIST_SCALE, true) 
  258. 		end 
  259. 	end 
  260. 	cell_cheats_mouse_update() 
  261. end 
  262.  
  263. function cell_cheats_nav_up(event, acceleration) 
  264. 	if Cheat_just_unlocked == true then 
  265. 		return 
  266. 	end 
  267. 	Cheats_list:move_cursor(-1) 
  268.  
  269. 	if Sub_menu_is_active then 
  270. 		cell_cheats_set_description() 
  271. 	end 
  272. end 
  273.  
  274. function cell_cheats_nav_down(event, acceleration) 
  275. 	if Cheat_just_unlocked == true then 
  276. 		return 
  277. 	end 
  278. 	Cheats_list:move_cursor(1) 
  279. 	 
  280. 	if Sub_menu_is_active then 
  281. 		cell_cheats_set_description() 
  282. 	end 
  283. end 
  284.  
  285. function cell_cheats_button_map(event, acceleration) 
  286. 	if Cheat_just_unlocked == true then 
  287. 		return 
  288. 	end 
  289. 	game_UI_audio_play("UI_Cell_Close") 
  290. 	cell_transition_screen(CELL_STATE_OUT, CELL_SCREEN_NONE, CELL_SCREEN_CHEATS, cell_cheats_exit_to_game) 
  291. end 
  292.  
  293. function cell_cheats_button_b(event, acceleration) 
  294.  
  295. 	if Cheat_being_entered == false then 
  296. 		if Cheat_just_unlocked == false then 
  297. 			if Sub_menu_is_active == false then 
  298. 				--Transition the screen out to extras... 
  299. 				cell_cheats_lock_controls() 
  300. 				cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_EXTRAS, CELL_SCREEN_CHEATS, cell_cheats_exit_to_extras) 
  301. 			else  
  302. 				cell_cheats_enter_category_menu() 
  303. 			end 
  304. 			game_UI_audio_play("UI_Cell_Nav_back") 
  305. 		end 
  306. 	elseif game_get_platform() == "PC" then 
  307. 		cell_cheats_cancel_cheat() 
  308. 	end 
  309. end 
  310.  
  311. function cheat_add_cb(success) 
  312.  
  313. 	if success then 
  314. 		cell_cheats_unlock_cheat("VKEYBOARD_RESULTS") 
  315. 	end 
  316.  
  317. end 
  318.  
  319. function cell_cheats_button_a(event, acceleration) 
  320. 	if Cheat_being_entered == false then 
  321. 		if Cheat_just_unlocked == true then 
  322. 			Cheat_just_unlocked = false 
  323. 			if Unlocked_cheat == nil then 
  324. 				cell_cheats_enter_category_menu() 
  325. 			else 
  326. 				-- Update the category menu so we can get the display name for the submenu header. 
  327. 				Category_menu = {  
  328. 					[1] = Add_cheat_button, 
  329. 				} 
  330. 				Num_category_items = 1 
  331. 				vint_dataresponder_request("cell_cheats_populate", "cell_cheats_populate_categories", 0, CELL_CHEATS_POPULATE_CATEGORIES) 
  332. 				cell_cheats_enter_submenu(Unlocked_cheat.category_id) 
  333. 				 
  334. 				for idx, Cheat  in pairs(Sub_menu) do 
  335. 					if Cheat.cheat_index == Unlocked_cheat.cheat_index then 
  336. 						Cheats_list:draw_items(Sub_menu, idx, CHEATS_LIST_WIDTH, CHEATS_LIST_NUM_OPTIONS, CHEATS_LIST_SCALE, true) 
  337. 					end 
  338. 				end 
  339. 				 
  340. 				cell_cheats_mouse_update() 
  341. 			end 
  342. 			local grp_h = vint_object_find("new_cheat_grp", 0, Cell_cheats_doc_handle) 
  343. 			vint_set_property(grp_h, "visible", false) 
  344. 			Cheats_list:set_visible(true) 
  345. 			 
  346. 			--show back button... 
  347. 			if Hint_bar ~= nil then 
  348. 				Hint_bar:set_visible(true) 
  349. 			end 
  350. 			 
  351. 		elseif Sub_menu_is_active == false then 
  352. 			local id = Cheats_list:get_id() 
  353. 			Category_selected_idx = Cheats_list:get_selection() 
  354. 			-- Selection made in Category menu 
  355. 			if id == ID_CHEAT then 
  356. 				-- add cheat was selected 
  357. 				game_vkeyboard_input("CHEAT_ENTRY_SHORT", "CHEAT_ENTRY_LONG", 32, "", "cheat_add_cb", true) 
  358. 			else 
  359. 				 
  360. 				-- entering a sub menu 
  361. 				cell_cheats_enter_submenu(id) 
  362. 			end 
  363. 		else 
  364. 			-- Selection made in Sub menu 
  365. 			local selection = Cheats_list:get_selection() 
  366. 			cell_cheats_activate_cheat(Sub_menu[selection].cheat_index) 
  367. 		end 
  368. 		 
  369. 		game_UI_audio_play("UI_Cell_Select") 
  370. 	elseif game_get_platform() == "PC" then 
  371. 		cell_cheats_confirm_cheat() 
  372. 	end 
  373. end 
  374.  
  375. function cell_cheats_lock_controls() 
  376. 	Input_tracker:subscribe(false) 
  377. 	if Mouse_input_tracker ~= nil then 
  378. 		Mouse_input_tracker:subscribe(false) 
  379. 	end 
  380. end 
  381.  
  382. function cell_cheats_unlock_controls() 
  383. 	Input_tracker:subscribe(true) 
  384. 	if Mouse_input_tracker ~= nil then 
  385. 		Mouse_input_tracker:subscribe(true) 
  386. 	end 
  387. end 
  388.  
  389. function cell_cheats_exit_to_extras() 
  390. 	pop_screen() 
  391. end 
  392.  
  393. function cell_cheats_exit_to_game() 
  394. 	pop_screen() 
  395. 	pop_screen() 
  396. 	pop_screen() 
  397. 	pop_screen() 
  398. end 
  399.  
  400. function cell_cheats_mouse_click(event, target_handle) 
  401. 	local hint_index = Hint_bar:get_hint_index(target_handle) 
  402. 	if hint_index == 1 then 
  403. 		cell_cheats_button_b() 
  404. 	end 
  405. 	 
  406. 	local hint_index = Continue_hint_bar:get_hint_index(target_handle) 
  407. 	if hint_index == 1 then 
  408. 		cell_cheats_button_a() 
  409. 	end 
  410.  
  411. 	-- If the target_handle matches a button, activate it 
  412. 	local new_index = Cheats_list:get_button_index(target_handle) 
  413. 	if new_index ~= 0 then 
  414. 		-- Enter an option if the target_handle is in the List 
  415. 		Cheats_list:set_selection(new_index) 
  416. 		cell_cheats_button_a() 
  417. 	end 
  418. end 
  419.  
  420. function cell_cheats_mouse_move(event, target_handle) 
  421. 	Hint_bar:set_highlight(0) 
  422. 	Continue_hint_bar:set_highlight(0) 
  423. 	local hint_index = Hint_bar:get_hint_index(target_handle) 
  424. 	if hint_index ~= 0 then 
  425. 		Hint_bar:set_highlight(hint_index) 
  426. 	end 
  427. 	 
  428. 	local hint_index = Continue_hint_bar:get_hint_index(target_handle)	 
  429. 	if hint_index == 1 then 
  430. 		Continue_hint_bar:set_highlight(1, COLOR_CHEATS_PRIMARY) 
  431. 	end 
  432. 	 
  433. 	local new_index = Cheats_list:get_button_index(target_handle) 
  434. 	if new_index ~= 0 then 
  435. 		-- Set the button as the new selected highlight 
  436. 		Cheats_list:set_selection(new_index) 
  437. 		Cheats_list:move_cursor(0, true) 
  438. 		if Sub_menu_is_active then 
  439. 			cell_cheats_set_description() 
  440. 		end 
  441. 	end 
  442. end 
  443.  
  444. function cell_cheats_mouse_scroll(event, target_handle, mouse_x, mouse_y, scroll_lines) 
  445. 	if scroll_lines ~= 0 then 
  446. 		Cheats_list:scroll_list(scroll_lines * -1) 
  447. 		cell_cheats_mouse_update() 
  448. 	end 
  449. end 
  450.  
  451. function cell_cheats_mouse_drag(event, target_handle, mouse_x, mouse_y) 
  452. 	if Cheats_list.scrollbar.tab.handle == target_handle then 
  453. 		local new_start_index = Cheats_list.scrollbar:drag_scrolltab(mouse_y, Cheats_list.num_buttons - (Cheats_list.max_buttons - 1)) 
  454. 		Cheats_list:scroll_list(0, new_start_index) 
  455. 	end 
  456. end 
  457.  
  458. function cell_cheats_mouse_drag_release(event, target_handle, mouse_x, mouse_y) 
  459. 	if Cheats_list.scrollbar.tab.handle == target_handle then 
  460. 		local start_index = Cheats_list:get_visible_indices() 
  461. 		Cheats_list.scrollbar:release_scrolltab(start_index, Cheats_list.num_buttons - (Cheats_list.max_buttons - 1)) 
  462. 		cell_cheats_mouse_update() 
  463. 	end 
  464. end 
  465.  
  466. function cell_cheats_text_update(di_h, event_name) 
  467. 	local override, cheat_text = vint_dataitem_get(di_h) 
  468. 	 
  469. 	Cheat_being_entered = override; 
  470. 	 
  471. 	if override == true then 
  472. 		Add_cheat_button.label = cheat_text 
  473. 	else 
  474. 		Add_cheat_button.label = "ADD_CHEAT" 
  475. 	end 
  476. 	 
  477. 	Cheats_list:draw_items(Category_menu, 1, CHEATS_LIST_WIDTH, CHEATS_LIST_NUM_OPTIONS, CHEATS_LIST_SCALE, true) 
  478. end 
  479.