./vdo_cell_menu.lua

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