./vdo_cell_filter.lua

  1. -- Inherited from Vdo_base_object 
  2. Vdo_cell_filter = Vdo_base_object:new_base() 
  3.  
  4. local FILTER_VERTICAL_SPACING = 26 
  5. local FILTER_SELECTED_COLOR = {R=175/255, G=63/255, B=213/255} 
  6. local FILTER_UNSELECTED_COLOR = {R=170/255, G=170/255, B=170/255} 
  7.  
  8. local FILTER_ICON_OFFSET_X = 30 
  9.  
  10. local Filter_icons = { 
  11. 	["PFT_ALL"] 			= "", 
  12. 	["PFT_ACTIVITIES"]	= "map_lod_activity", 
  13. 	["PFT_DIVERSIONS"]	= "map_lod_diversion", 
  14. 	["PFT_STORES"]		 	= "map_lod_store", 
  15. 	["PFT_PROPERTIES"] 	= "map_lod_property", 
  16. 	["PFT_FLASHPOINTS"]	= "map_lod_flashpoint", 
  17. 	["PFT_COLLECTION"] 	= "map_lod_flashpoint", 
  18. 	["PFT_CRIBS"] 			= "map_lod_crib", 
  19. 	["PFT_NONE"] 			= "", 
  20. } 
  21.  
  22. local Dpad = -1 
  23.  
  24. local vdo_cell_filter_gamepad_listener = -1 
  25.  
  26. function vdo_cell_filter_init() 
  27. end 
  28.  
  29. function vdo_cell_filter_cleanup() 
  30. end 
  31.  
  32. --[[ --------------------------------------------------------------------- 
  33. -- Function - cleanup 
  34. -- Description - Clean up function for vdo_cell_filter 
  35. -- Parameters - none 
  36. -- Returns - nil 
  37. ]]-- --------------------------------------------------------------------- 
  38. function Vdo_cell_filter:cleanup() 
  39. 	vint_scriptevent_stop_listening( vdo_cell_filter_gamepad_listener ) 
  40. end 
  41.  
  42. ------------------------------------------------------------------------------- 
  43. -- Init filter list... 
  44. ------------------------------------------------------------------------------- 
  45. function Vdo_cell_filter:init() 
  46. 	 
  47. 	vdo_cell_filter_gamepad_listener = vint_scriptevent_listen( "gamepad_active", "vdo_cell_filter_update_gamepad_state" ) 
  48. 	 
  49. 	self.selected_index = 0			--Currently selected filter. 
  50. 	self.filter_count = 0			--How many filters are in the list. 
  51. 	self.filters = {}					--Table of filter references... 
  52. 	 
  53. 	self.use_icons = false			--If we show a filter icon next to the name 
  54. 	 
  55. 	--Initialize the filters... 
  56. 	--Store globals for filter start point... 
  57. 	local h = Vdo_base_object:new("filter_type_grp", self.handle, self.doc_handle) 
  58. 	self.start_x, self.start_y = h:get_anchor() 
  59. 	h:set_visible(false) 
  60. 	local x_y_h = vint_object_find("x_of_y", h.handle, self.doc_handle) 
  61. 	vint_set_property(x_y_h, "text_tag", "00/00") 
  62. 	self.x_y_width = element_get_actual_size(x_y_h) 
  63. 	 
  64. 	--Set filter control buttons... 
  65. 	Dpad = Vdo_hint_button:new("filter_dpad", self.handle, self.doc_handle) 
  66. 	Dpad:set_button(CTRL_BUTTON_DPAD_UP_DOWN, game_get_key_name(59))--"F1" 
  67. end 
  68.  
  69. ------------------------------------------------------------------------------- 
  70. -- Adds item into filter list... 
  71. ------------------------------------------------------------------------------- 
  72. function Vdo_cell_filter:add(filter_type, filter_name, x, y) 
  73.  
  74. 	--Clone Item 
  75. 	local orig_filter_item = Vdo_base_object:new("filter_type_grp", self.handle, self.doc_handle) 
  76. 	local filter_item = orig_filter_item:clone(orig_filter_item.handle) 
  77. 	filter_item:set_visible(true) 
  78. 	 
  79. 	--Store data... 
  80. 	self.filters[self.filter_count] = {  
  81. 		filter_type = filter_type, 
  82. 		item = filter_item, 
  83. 		filter_name = filter_name, 					 
  84. 		x = x,  
  85. 		y = y, 
  86. 	} 
  87. 		 
  88. 	--Reposition... 
  89. 	local pos_x = self.start_x 
  90. 	local pos_y = self.start_y + (FILTER_VERTICAL_SPACING * self.filter_count) 
  91. 	filter_item:set_anchor(pos_x, pos_y) 
  92.  
  93. 	-- Set text label 
  94. 	local filter_name_h = vint_object_find("filter_name", filter_item.handle, self.doc_handle) 
  95. 	vint_set_property(filter_name_h, "text_tag", filter_name) 
  96. 	 
  97. 	-- Set x/y 
  98. 	local x_of_y_h = vint_object_find("x_of_y", filter_item.handle, self.doc_handle) 
  99. 	if y > 0 or y == nil then 
  100. 		local x_of_y_string = x .. "/" .. y 
  101. 		vint_set_property(x_of_y_h, "text_tag", x_of_y_string) 
  102. 		vint_set_property(x_of_y_h, "visible", true) 
  103. 	else 
  104. 		--If y is 0 then there is no x/y. Hide it. 
  105. 		vint_set_property(x_of_y_h, "visible", false) 
  106. 	end 
  107. 	 
  108. 	--Set unhighlighted... 
  109. 	vint_set_property(filter_name_h, "tint", FILTER_UNSELECTED_COLOR.R, FILTER_UNSELECTED_COLOR.G, FILTER_UNSELECTED_COLOR.B) 
  110. 	vint_set_property(x_of_y_h, 		"tint", FILTER_UNSELECTED_COLOR.R, FILTER_UNSELECTED_COLOR.G, FILTER_UNSELECTED_COLOR.B) 
  111.  
  112. 	--Determine if we use icons, hide or show... 
  113. 	local icon_bmp_h = vint_object_find("filter_bmp", filter_item.handle, self.doc_handle) 
  114. 	if self.use_icons == true then 
  115. 		local filter_icon = Filter_icons[filter_type] 
  116. 		if filter_icon ~= "" then 
  117. 			vint_set_property(icon_bmp_h, "visible", true) 
  118. 			vint_set_property(icon_bmp_h, "image", filter_icon) 
  119. 		else 
  120. 			vint_set_property(icon_bmp_h, "visible", false) 
  121. 		end 
  122. 		local pos_x, pos_y = vint_get_property(filter_name_h, "anchor") 
  123. 		vint_set_property(filter_name_h, "anchor", FILTER_ICON_OFFSET_X, pos_y) 
  124. 	else 
  125. 		vint_set_property(icon_bmp_h, "visible", false) 
  126. 	end 
  127. 	 
  128.  
  129. 	--Scale size of background to fit filters... (This only adjusts the height of the box) 
  130. 	local background = Vdo_base_object:new("filter_bg", self.handle, self.doc_handle) 
  131. 	local width, height = background:get_actual_size() 
  132. 	 
  133. 	--MAGIC NUMBER 30 (box offset size) 
  134. 	height = self.start_y + (FILTER_VERTICAL_SPACING * self.filter_count) + 30 
  135. 	background:set_actual_size(width, height) 
  136.  
  137. 	self.filter_count = self.filter_count + 1 
  138. end 
  139.  
  140. ------------------------------------------------------------------------------- 
  141. -- Clears out list and deletes all objects in it. 
  142. ------------------------------------------------------------------------------- 
  143. function Vdo_cell_filter:clear() 
  144.  
  145. 	--Cycle through list and delete objects... 
  146. 	for idx, val in pairs(self.filters) do 
  147. 		val.item:object_destroy() 
  148. 	end 
  149. 	 
  150. 	self.filters = {} 
  151. 	self.filter_count = 0 
  152. end 
  153.  
  154. ------------------------------------------------------------------------------- 
  155. -- Navigates the filter list from current position... 
  156. -- 
  157. -- @param	direction	+1 = up or -1 = down 
  158. ------------------------------------------------------------------------------- 
  159. function Vdo_cell_filter:move_cursor(direction) 
  160.  
  161. 	if self.filter_count == 0 then 
  162. 		--No Filters to nav... 
  163. 		return 
  164. 	end 
  165. 	 
  166. 	local new_index = self.selected_index + direction 
  167.  
  168. 	--Wrap index if needed... 
  169. 	if new_index < 0 then 
  170. 		new_index = self.filter_count - 1 
  171. 	elseif new_index == self.filter_count then 
  172. 		new_index = 0 
  173. 	end 
  174. 	 
  175. 	--Change the filter... 
  176. 	self:change_filter(new_index, true) 
  177. end 
  178.  
  179. ------------------------------------------------------------------------------- 
  180. -- Change to a specific filter. 
  181. -- @param	index					index of filter 
  182. --	@param	set_game_filter	(Bool) Determines if we set the game filter. 
  183. ------------------------------------------------------------------------------- 
  184. function Vdo_cell_filter:change_filter(index, do_callback) 
  185.  
  186. 	local filter_previous = self.filters[self.selected_index].item 
  187. 	local filter = self.filters[index] 
  188. 	local filter_obj = filter.item 
  189.  
  190. 	--Move D-Pad 
  191. 	local x, y = filter_obj:get_anchor() 
  192. 	local dpad = Vdo_base_object:new("filter_dpad", self.handle, self.doc_handle) 
  193. 	local dpad_x, dpad_y = dpad:get_anchor() 
  194. 	dpad:set_anchor(dpad_x, y) 
  195. 	 
  196. 	 
  197. 	local x_of_y_h = vint_object_find("x_of_y", filter_obj.handle, self.doc_handle) 
  198. 	local filter_name_h = vint_object_find("filter_name", filter_obj.handle, self.doc_handle)			 
  199. 	 
  200. 	vint_set_property(filter_name_h, "tint", FILTER_SELECTED_COLOR.R, FILTER_SELECTED_COLOR.G, FILTER_SELECTED_COLOR.B) 
  201. 	vint_set_property(x_of_y_h, 		"tint", FILTER_SELECTED_COLOR.R, FILTER_SELECTED_COLOR.G, FILTER_SELECTED_COLOR.B) 
  202. 	 
  203. 	--Change text colors 
  204. 	if index ~= self.selected_index then 
  205. 		x_of_y_h = vint_object_find("x_of_y", filter_previous.handle, self.doc_handle) 
  206. 		filter_name_h = vint_object_find("filter_name", filter_previous.handle, self.doc_handle)			 
  207. 		vint_set_property(filter_name_h, "tint", FILTER_UNSELECTED_COLOR.R, FILTER_UNSELECTED_COLOR.G, FILTER_UNSELECTED_COLOR.B) 
  208. 		vint_set_property(x_of_y_h, 		"tint", FILTER_UNSELECTED_COLOR.R, FILTER_UNSELECTED_COLOR.G, FILTER_UNSELECTED_COLOR.B) 
  209. 	end 
  210.  
  211. 	if do_callback then 
  212. 		self:filter_callback_change_do(filter.filter_type) 
  213. 	end 
  214. 	 
  215. 	--store index to object... 
  216. 	self.selected_index = index 
  217. end 
  218.  
  219. ------------------------------------------------------------------------------- 
  220. -- Changes the filter by filter type 
  221. -- 
  222. -- @param	filter_type		filter type is determined by what the game uses to change filters 
  223. -- @param	do_callback		True to actually change the filter in game, False if to change the visual. 
  224. ------------------------------------------------------------------------------- 
  225. function Vdo_cell_filter:change_filter_by_type(filter_type, do_callback) 
  226. 	local filter_idx = self.filters[0] 
  227. 	for idx, val in pairs(self.filters) do 
  228. 		if val.filter_type == filter_type then 
  229. 			filter_idx = idx 
  230. 			break 
  231. 		end 
  232. 	end 
  233. 	self:change_filter(filter_idx, do_callback) 
  234. end 
  235.  
  236.  
  237. ------------------------------------------------------------------------------- 
  238. -- Adjust layout of the screen 
  239. ------------------------------------------------------------------------------- 
  240. function Vdo_cell_filter:adjust_layout(min_width) 
  241. 	 
  242. 	--Go through filters and store the elements into a format to find widest text element... 
  243. 	local data = {} 
  244. 	for idx, val in pairs(self.filters) do 
  245. 		local h = vint_object_find("filter_name", val.item.handle) 
  246. 		data[idx] = h 
  247. 	end 
  248. 	 
  249. 	local widest_filter_element_h, filter_element_width = find_widest_text_element(data) 
  250. 	 
  251. 	if widest_filter_element_h == 0 then 
  252. 		--exit if no filter elements exit yet.. 
  253. 		return 0 
  254. 	end 
  255. 		 
  256. 	local start_x, start_y = vint_get_property(widest_filter_element_h, "anchor") 
  257. 	local target_x = start_x + filter_element_width + self.x_y_width  + 10 
  258. 	 
  259. 	if min_width ~= nil then 
  260. 		--when resetting to our min width value we need to subtract the padding. 
  261. 		min_width = min_width - 32 
  262. 		target_x = min_width 
  263. 	end 
  264.  
  265. 	--reposition each x_y element... 
  266. 	for idx, val in pairs(self.filters) do 
  267. 		local x_y_h = vint_object_find("x_of_y", val.item.handle, self.doc_handle) 
  268. 		local x, y = vint_get_property(x_y_h, "anchor") 
  269. 		vint_set_property(x_y_h, "anchor", target_x, y ) 
  270. 	end 
  271. 	 
  272. 	--Add padding in from the group on the left side. 
  273. 	return target_x + 32	 
  274. end 
  275.  
  276. ------------------------------------------------------------------------------- 
  277. -- Calls the game function callback to change the filter type. 
  278. -- @param filter_type	the filter type that the game reads to change it... 
  279. ------------------------------------------------------------------------------- 
  280. function Vdo_cell_filter:filter_callback_change_do(filter_type) 
  281. 	if self.filter_change_callback ~= nil then 
  282. 		self.filter_change_callback(filter_type) 
  283. 	end 
  284. end 
  285.  
  286. ------------------------------------------------------------------------------- 
  287. -- Sets the callback function for the vdo to call. 
  288. -- @param func		The game function that the vdo calls to change the filter type. 
  289. ------------------------------------------------------------------------------- 
  290. function Vdo_cell_filter:filter_callback_set(func) 
  291. 	self.filter_change_callback = func 
  292. end 
  293.  
  294. function Vdo_cell_filter:get_filter_data() 
  295. 	local filter = self.filters[self.selected_index] 
  296. 	local name 	= filter.filter_name 
  297. 	local x		= filter.x 
  298. 	local y 		= filter.y 
  299. 	return name, x, y 
  300. end 
  301.  
  302. ------------------------------------------------------------------------------- 
  303. -- Determines whether or not we should show filter icons 
  304. -- @param func		The game function that the vdo calls to change the filter type. 
  305. ------------------------------------------------------------------------------- 
  306. function Vdo_cell_filter:set_use_icons(use_icons) 
  307. 	self.use_icons = use_icons 
  308. end 
  309.  
  310. function Vdo_cell_filter:get_filter_index(handle) 
  311.  
  312. 	for idx, filter in pairs(self.filters) do 
  313. 		if filter.item.handle == handle then 
  314. 			return idx 
  315. 		end 
  316. 	end 
  317.  
  318. 	return -1 
  319. end 
  320.  
  321. -- callback_nav and callback_action are optional overrides (func_prefix is ignored if they are used) 
  322. function Vdo_cell_filter:add_mouse_inputs(func_prefix, input_tracker, priority, callback_nav, callback_action) 
  323. 	if (self.filters == nil) or (func_prefix == nil) or (input_tracker == nil) then 
  324. 		return 
  325. 	end 
  326. 	 
  327. 		-- Default priority value to 50 
  328. 	self.priority = priority or 50 
  329. 	 
  330. 	self.mouse_move_function = callback_nav or func_prefix.."_mouse_move" 
  331. 	self.mouse_click_function = callback_action or func_prefix.."_mouse_click" 
  332. 	 
  333. 	for idx, filter in pairs(self.filters) do 
  334. 		vint_set_property(filter.item.handle, "mouse_depth", -4002) 
  335. 		input_tracker:add_mouse_input("mouse_move", self.mouse_move_function, self.priority, filter.item.handle) 
  336. 		input_tracker:add_mouse_input("mouse_click", self.mouse_click_function, self.priority, filter.item.handle) 
  337. 	end 
  338. end 
  339.  
  340. --[[ --------------------------------------------------------------------- 
  341. -- Function - vdo_cell_filter_update_gamepad_state 
  342. -- Description - Called by code anytime gamepad is plugged or unplugged. 
  343. -- Parameters - none 
  344. -- Returns - nil 
  345. ]]-- --------------------------------------------------------------------- 
  346. function vdo_cell_filter_update_gamepad_state() 
  347. 	if( Dpad ~= -1 ) then 
  348. 		Dpad:set_button(CTRL_BUTTON_DPAD_UP_DOWN, game_get_key_name(59))--"F1" 
  349. 	end 
  350. end