./vdo_cell_menu.lua

  1.  
  2. local MAX_COL = 2 
  3. local MAX_ROW = 2 
  4.  
  5. function vdo_cell_menu_init() 
  6. end 
  7.  
  8. Vdo_cell_menu = Vdo_base_object:new_base() 
  9.  
  10. function Vdo_cell_menu:init() 
  11.  
  12. 	local index = 0 
  13. 	self.menu_btn = {} 
  14. 	 
  15. 	--Declare and populate buttons with data	 
  16. 	for i = 0,MAX_ROW,1 do 
  17. 		for j = 0,MAX_COL,1 do 
  18. 			self.menu_btn[i ..j] = Vdo_cell_menu_button:new("menu_btn" ..i ..j, self.handle, self.doc_handle) 
  19. 			index = index + 1 
  20. 		end		 
  21. 	end	 
  22. 	 
  23. 	self.animate_in = Vdo_anim_object:new("animate_btns_in", self.handle, self.doc_handle) 
  24. 		 
  25. 	self.cursor_x = 0 
  26. 	self.cursor_y = 0  
  27. 	 
  28. 	self:set_highlight(self.cursor_x, self.cursor_y) 
  29. end 
  30.  
  31. function Vdo_cell_menu:cleanup() 
  32. 	-- Nuke all button subscriptions 
  33. 	Input_tracker:subscribe(false) 
  34. end 
  35.  
  36. function Vdo_cell_menu:populate_menu(data, cur_selection) 
  37. 	local index = 0 
  38. 	 
  39. 	local hidden_btn = { 
  40. 		label = "", 
  41. 		icon = nil, 
  42. 		is_enabled = false, 
  43. 		is_visible = false, 
  44. 	} 
  45. 	 
  46. 	--populate buttons with data	 
  47. 	for i = 0,MAX_ROW,1 do 
  48. 		for j = 0,MAX_COL,1 do 
  49. 			local btn_data = hidden_btn 
  50. 			if index <= #data then 
  51. 				btn_data = data[index] 
  52. 			end 
  53. 			self.menu_btn[j ..i]:populate_button(btn_data.label, btn_data.icon, btn_data.is_enabled, btn_data.new_items, btn_data.is_visible, btn_data.can_wrap) 
  54. 			index = index + 1 
  55. 			self.menu_btn[j ..i]:set_highlight(false) 
  56. 		end	 
  57. 	end 
  58. 	 
  59. 	if cur_selection ~= nil and cur_selection > -1 and cur_selection < 9 then 
  60. 		self:set_id(cur_selection) 
  61. 	else 
  62. 		-- get first non-disabled button 
  63. 		local found = false 
  64. 		for i = 0,MAX_ROW,1 do 
  65. 			for j = 0,MAX_COL,1 do 
  66. 				if self.menu_btn[j ..i].is_enabled ~= false then 
  67. 					if found == false then 
  68. 						found = true 
  69. 						 
  70. 						self.cursor_x = j 
  71. 						self.cursor_y = i 
  72. 						--debug_print("always_on", "found " .. self.cursor_x .. self.cursor_y ) 
  73. 					end 
  74. 				end 
  75. 			end 
  76. 		end 
  77. 	end 
  78. 	 
  79. 	self:set_highlight(self.cursor_x, self.cursor_y) 
  80. end 
  81.  
  82. function Vdo_cell_menu:set_highlight(position_x, position_y) 
  83. 	local previous_x = self.cursor_x 
  84. 	local previous_y = self.cursor_y	 
  85.  
  86. 	--Unhighlight previous option... 
  87. 	self.menu_btn[previous_x .. previous_y]:set_highlight(false) 
  88. 	 
  89. 	--Highlight new option... 
  90. 	self.menu_btn[position_x .. position_y]:set_highlight(true) 
  91.  
  92. 	--Store positions to self. 
  93. 	self.cursor_x = position_x 
  94. 	self.cursor_y = position_y	 
  95. end 
  96.  
  97. function Vdo_cell_menu:show_highlight(show) 
  98. 	local previous_x = self.cursor_x 
  99. 	local previous_y = self.cursor_y	 
  100. 		 
  101. 	--Unhighlight previous option... 
  102. 	self.menu_btn[previous_x .. previous_y]:set_highlight(show) 
  103. end 
  104.  
  105. -- navigation helper function 
  106. function row_down_wrap(cur_y) 
  107. 	return abs((cur_y + 1) % (MAX_ROW + 1)) 
  108. end 
  109.  
  110. function row_up_wrap(cur_y) 
  111. 	return abs((cur_y + MAX_ROW) % (MAX_ROW + 1)) 
  112. end 
  113.  
  114. function column_right_wrap(cur_x) 
  115. 	return abs((cur_x + 1) % (MAX_COL + 1)) 
  116. end 
  117.  
  118. function column_left_wrap(cur_x) 
  119. 	return abs((cur_x + MAX_COL) % (MAX_COL + 1)) 
  120. end 
  121.  
  122. function identity(cur) 
  123. 	return cur 
  124. end 
  125.  
  126. -- check if cell button in row / column enabled 
  127. function Vdo_cell_menu:search_enabled_cell(cur_x, cur_y, update_x, update_y, try_count) 
  128. 	for i = 1, try_count, 1 do 
  129. 		if self.menu_btn[cur_x ..cur_y].is_enabled ~= false then 
  130. 			return true, cur_x, cur_y 
  131. 		end 
  132. 		cur_x = update_x(cur_x) 
  133. 		cur_y = update_y(cur_y) 
  134. 	end 
  135. 	 
  136. 	return false, 0, 0 
  137. end 
  138.  
  139. function Vdo_cell_menu:nav_up(event, acceleration) 
  140.  
  141. 	game_UI_audio_play("UI_Cell_Nav")	 
  142.  
  143. 	local cur_x = self.cursor_x 
  144. 	local cur_y = self.cursor_y 
  145. 	local orig_y = cur_y 
  146.  
  147. 	-- check start column 
  148. 	local try_count = MAX_ROW 
  149. 	cur_y = row_up_wrap(cur_y) 
  150. 	local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_up_wrap, try_count) 
  151. 	 
  152. 	if found then 
  153. 		self:set_highlight(found_x, found_y) 
  154. 		return 
  155. 	end 
  156. 	 
  157. 	-- move column left and try again 
  158. 	for i = 1, MAX_COL, 1 do 
  159. 		cur_x = column_left_wrap(cur_x) 
  160. 		cur_y = MAX_COL 
  161. 		try_count = MAX_ROW + 1 
  162. 		found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_up_wrap, try_count) 
  163. 		if found then 
  164. 			self:set_highlight(found_x, found_y) 
  165. 			return 
  166. 		end 
  167. 	end 
  168. end 
  169.  
  170. function Vdo_cell_menu:nav_down(event, acceleration) 
  171.  
  172. 	game_UI_audio_play("UI_Cell_Nav") 
  173.  
  174. 	local cur_x = self.cursor_x 
  175. 	local cur_y = self.cursor_y 
  176.  
  177. 	-- check start column 
  178. 	local try_count = MAX_ROW 
  179. 	cur_y = row_down_wrap(cur_y) 
  180. 	local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_down_wrap, try_count) 
  181. 	 
  182. 	if found then 
  183. 		self:set_highlight(found_x, found_y) 
  184. 		return 
  185. 	end 
  186. 	 
  187. 	-- move column left and try again 
  188. 	for i = 1, MAX_COL, 1 do 
  189. 		cur_x = column_right_wrap(cur_x) 
  190. 		cur_y = 0 
  191. 		try_count = MAX_ROW + 1 
  192. 		found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_up_wrap, try_count) 
  193. 		if found then 
  194. 			self:set_highlight(found_x, found_y) 
  195. 			return 
  196. 		end 
  197. 	end 
  198. end 
  199.  
  200. function Vdo_cell_menu:nav_left(event, acceleration) 
  201.  
  202. 	game_UI_audio_play("UI_Cell_Nav") 
  203. 	 
  204. 	local cur_x = self.cursor_x 
  205. 	local cur_y = self.cursor_y 
  206. 	 
  207. 	-- check start row 
  208. 	local try_count = MAX_COL 
  209. 	cur_x = column_left_wrap(cur_x) 
  210. 	local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_left_wrap, identity, try_count) 
  211. 	 
  212. 	if found then 
  213. 		self:set_highlight(found_x, found_y) 
  214. 		return 
  215. 	end 
  216. 	 
  217. 	-- move row up and try again 
  218. 	for i = 1, MAX_ROW, 1 do 
  219. 		cur_x = MAX_ROW 
  220. 		cur_y = row_up_wrap(cur_y) 
  221. 		try_count = MAX_ROW + 1 
  222. 		found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_left_wrap, identity, try_count) 
  223. 		if found then 
  224. 			self:set_highlight(found_x, found_y) 
  225. 			return 
  226. 		end 
  227. 	end 
  228. end 
  229.  
  230. function Vdo_cell_menu:nav_right(event, acceleration) 
  231. 	 
  232. 	game_UI_audio_play("UI_Cell_Nav") 
  233. 	 
  234. 	local cur_x = self.cursor_x 
  235. 	local cur_y = self.cursor_y 
  236. 	 
  237. 	-- check start row 
  238. 	local try_count = MAX_COL 
  239. 	cur_x = column_right_wrap(cur_x) 
  240. 	local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_right_wrap, identity, try_count) 
  241. 	 
  242. 	if found then 
  243. 		self:set_highlight(found_x, found_y) 
  244. 		return 
  245. 	end 
  246. 	 
  247. 	-- move row down and try again 
  248. 	for i = 1, MAX_ROW, 1 do 
  249. 		cur_x = 0 
  250. 		cur_y = row_down_wrap(cur_y) 
  251. 		try_count = MAX_ROW + 1 
  252. 		found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_right_wrap, identity, try_count) 
  253. 		if found then 
  254. 			self:set_highlight(found_x, found_y) 
  255. 			return 
  256. 		end 
  257. 	end 
  258. end 
  259.  
  260. function Vdo_cell_menu:get_id() 
  261. 	 
  262. 	local max_col_id = MAX_COL + 1 
  263. 	 
  264. 	return (self.cursor_y * max_col_id + self.cursor_x) 
  265. end 
  266.  
  267. function Vdo_cell_menu:set_id(id) 
  268.  
  269. 	local max_col_id = MAX_COL + 1 
  270. 	 
  271. 	self.cursor_y = abs(floor(id / max_col_id)) 
  272. 	self.cursor_x = abs(id % max_col_id	) 
  273. 		 
  274. end 
  275.  
  276. function Vdo_cell_menu:play_animate_in(is_playing) 
  277.  
  278. 	self.animate_in:play(0) 
  279. 	 
  280. end 
  281.  
  282.  
  283. -- ===================================== 
  284. --       Mouse Specific Functions 
  285. -- ===================================== 
  286.  
  287. -- Returns true and sets the highlight to the button if the target_handle matches its image  
  288. -- and if the button is not disabled 
  289. -- 
  290. function Vdo_cell_menu:select_button(target_handle) 
  291. 	for i = 0,MAX_ROW,1 do 
  292. 		for j = 0,MAX_COL,1 do 
  293. 			local button = self.menu_btn[j ..i] 
  294. 			if (button ~= nil) and (button.is_enabled ~= false) and (button.btn_img.handle == target_handle) then 
  295. 				self:set_highlight(j, i) 
  296. 				return true 
  297. 			end 
  298. 		end 
  299. 	end 
  300. 	 
  301. 	return false 
  302. end 
  303.  
  304. function Vdo_cell_menu:add_mouse_inputs(func_prefix, input_tracker, priority) 
  305. 	if (func_prefix == nil) or (input_tracker == nil) then 
  306. 		return 
  307. 	end 
  308. 	 
  309. 	-- Set default priority value to 50 
  310. 	priority = priority or 50 
  311. 	 
  312. 	local mouse_click_function	= func_prefix.."_mouse_click" 
  313. 	local mouse_move_function	= func_prefix.."_mouse_move" 
  314. 	 
  315. 	-- Add mouse inputs to each of the cell phone button's images 
  316. 	for i = 0,MAX_ROW,1 do 
  317. 		for j = 0,MAX_COL,1 do 
  318. 			if self.menu_btn[j ..i] ~= nil then 
  319. 				input_tracker:add_mouse_input("mouse_click", mouse_click_function, priority, self.menu_btn[j ..i].btn_img.handle) 
  320. 				input_tracker:add_mouse_input("mouse_move", mouse_move_function, priority, self.menu_btn[j ..i].btn_img.handle) 
  321. 			end 
  322. 		end 
  323. 	end 
  324. end