./vdo_hint_bar.lua

  1. local TEXT_SCALE_NORMAL 	= 0.8		--Text size for mini hintbar 
  2. local TEXT_SCALE_MINI 		= 0.7		--Text size for mini hintbar 
  3. local BUTTON_SCALE_NORMAL 	= 1.0		--Button size for mini hintbar 
  4. local BUTTON_SCALE_MINI 	= 0.8		--Button size for mini hintbar 
  5.  
  6. local HINT_PADDING_NORMAL 	= 5				--Spacing between button and text 
  7. local HINT_SPACING_NORMAL 	= 30				--Spacing between each button and text 
  8.  
  9. local HINT_PADDING_MINI 	= 0 				--Spacing between button and text 
  10. local HINT_SPACING_MINI		= 25				--Spacing between each button and text 
  11.  
  12. function vdo_hint_bar_init() 
  13. 	local h = vint_object_find("button") 
  14. 	vint_set_property(h, "visible", false) 
  15. 	h = vint_object_find("hint_text") 
  16. 	vint_set_property(h, "visible", false) 
  17. end 
  18.  
  19. function vdo_hint_bar_cleanup() 
  20. end 
  21.  
  22. -- Inherited from Vdo_base_object 
  23. Vdo_hint_bar = Vdo_base_object:new_base() 
  24.  
  25. function Vdo_hint_bar:init() 
  26. 	if self.is_initialized == nil then 
  27. 		self.hint_objects = {} 
  28. 		--Defaults for int spacing 
  29. 		self.hint_padding = HINT_PADDING_NORMAL	--Spacing between button and text 
  30. 		self.hint_spacing = HINT_SPACING_NORMAL	--Spacing between each button and text 
  31.  
  32. 		self.hint_objects[1] = { 
  33. 			button = Vdo_hint_button:new("button", self.handle, self.doc_handle), 
  34. 			text = Vdo_base_object:new("hint_text", self.handle, self.doc_handle), 
  35. 		} 
  36. 		self.hint_objects.num = 1 
  37. 		self.hint_data = 0 
  38. 		self.height = 0 
  39. 		self.width = 0 
  40. 		 
  41. 		--to store the buttons that we cloned and pulsed 
  42. 		self.pulse_buttons = {}		 
  43. 	end 
  44. 	 
  45. 	--Make sure we clear our old hints out  
  46. 	self:set_hints() 
  47. 	self.is_initialized = true 
  48. end 
  49.  
  50. function Vdo_hint_bar:cleanup() 
  51.  
  52. 	--Remove old buttons and clear out stored objects 
  53. 	for i = 2, self.hint_objects.num do 
  54. 		self.hint_objects[i].button:object_destroy() 
  55. 		self.hint_objects[i].text:object_destroy() 
  56. 		self.hint_objects[i] = nil 
  57. 	end 
  58. 	 
  59. end 
  60.  
  61. --Sets hints of hint object 
  62. -- Example of hint_data object param 
  63. --[[ 
  64. 	hint_data = { 
  65. 		{CTRL_MENU_BUTTON_B, "BACK"}, 
  66. 		{CTRL_BUTTON_X, "SAVE GAME"}, 
  67. 		{CTRL_BUTTON_Y, "EAT DONG"}, 
  68. 	} 
  69. ]] 
  70. function Vdo_hint_bar:set_hints(hint_data, force_gamepad_icons, force_kbd_icons, force_scale) 
  71.  
  72. 	self.hint_objects[1].button:set_visible(true) 
  73. 	self.hint_objects[1].text:set_visible(true)  
  74. 	 
  75. 	--Only reset the scale if we are not doing a rebuild... 
  76. 	local do_force_scale = false 
  77. 	if force_scale == nil then 
  78. 		do_force_scale = true 
  79. 		force_scale = 1.0 
  80. 	end 
  81. 	 
  82. 	--reset force_scale... 
  83. 	self.hint_objects[1].text:set_property("scale", force_scale, 1.0) 
  84. 	 
  85. 	--Remove old buttons and clear out stored objects 
  86. 	for i = 2, self.hint_objects.num do 
  87. 		self.hint_objects[i].button:object_destroy() 
  88. 		self.hint_objects[i].text:object_destroy() 
  89. 		self.hint_objects[i] = nil 
  90. 	end 
  91. 	 
  92. 	self.hint_objects.num = 0 
  93. 	local button_width_total = 0 
  94. 	if hint_data == nil or hint_data ==  false then 
  95. 		self.hint_objects[1].button:set_visible(false) 
  96. 		self.hint_objects[1].text:set_visible(false)  
  97. 	else 
  98.  
  99. 		local hint_data_count = #hint_data 
  100. 		 
  101. 		local button, text, text_old 
  102. 		local text_width, text_height, text_x, text_y 
  103. 		local button_width, button_height, button_x, button_y 
  104. 		local button_x_new, text_x_new 
  105. 		local def_button_width 
  106. 		local x_start = 0 
  107. 		 
  108. 		--Create new buttons and set 
  109. 		 
  110. 		for i = 1, hint_data_count do 
  111. 			if i == 1 then 
  112. 				--First button in menu... (no clones) 
  113. 				button = self.hint_objects[1].button 
  114. 				text = self.hint_objects[1].text 
  115. 			else 
  116. 				--Clone Objects from original 
  117. 				button = Vdo_hint_button:clone(self.hint_objects[1].button.handle, self.handle) 
  118. 				text = Vdo_base_object:clone(self.hint_objects[1].text.handle, self.handle) 
  119. 			end	 
  120. 			 
  121. 			--Set Text and Button Image			 
  122. 			local btn_img_string = hint_data[i][1] 
  123. 			local btn_pc_key_text = hint_data[i][3] 
  124. 			local btn_text_string = hint_data[i][2] 
  125. 			button:set_button(btn_img_string, btn_pc_key_text, force_gamepad_icons, force_kbd_icons) 
  126. 			text:set_text(btn_text_string) 
  127. 			 
  128. 			-- Save the button width to make it easier to offset the buttons based on their size 
  129. 			local button_offset 
  130. 			if i == 1 then 
  131. 				--First button... we process differently. 
  132. 				text_x, text_y = text:get_anchor() 
  133. 				 
  134. 				-- Only get the default width from the first button, though this might change later to be hardcoded (30 is the current width of a button image) 
  135. 				def_button_width = button:get_default_width() 
  136. 				button_x, button_y = button:get_anchor() 
  137. 				button_width, button_height = button:get_size() 
  138. 				button_offset = (button_width - def_button_width) / 2 
  139. 				button_x_new = button_offset 
  140. 				 
  141. 				x_start = button_width * 0.5 
  142. 				 
  143. 				--text position is the sum of button position, half the width fo the button and a hint padding value. 
  144. 				text_x_new = button_x_new + (button_width / 2 ) + self.hint_padding 
  145. 				button_width_total = button_width_total + (button_width) + self.hint_padding 
  146. 			else 
  147. 				--Reposition and align based on previous button 
  148. 				text_old = self.hint_objects[i - 1].text 
  149. 				text_width, text_height = text_old:get_actual_size() 
  150. 				text_x, text_y = text_old:get_anchor() 
  151. 				 
  152. 				button_x, button_y = button:get_anchor() 
  153. 				button_width, button_height = button:get_size() 
  154. 				button_offset = (button_width - def_button_width) / 2 
  155. 				button_x_new = button_offset + text_x + text_width + self.hint_spacing 
  156. 				 
  157. 				--text position is the sum of button position, half the width fo the button and a hint padding value. 
  158. 				text_x_new = button_x_new + (button_width / 2 ) + self.hint_padding 
  159. 				button_width_total = button_width_total + (button_width * .5) + self.hint_spacing 
  160. 			end 
  161. 			 
  162. 			button:set_anchor(button_x_new, button_y) 
  163. 			text:set_anchor(text_x_new, text_y) 
  164. 			 
  165. 			--Store into global for processing / cleanup 
  166. 			self.hint_objects[i] = {} 
  167. 			self.hint_objects[i].button = button 
  168. 			self.hint_objects[i].text = text 
  169. 			self.hint_objects.num = self.hint_objects.num + 1 
  170. 		end 
  171. 		 
  172. 		 
  173. 		--Calculate width and height of hint bar by checking the position and width of the last text field... 
  174. 		local text_h = self.hint_objects[self.hint_objects.num].text.handle 
  175. 		local width, height = element_get_actual_size(text_h) 
  176. 		local x, y = vint_get_property(text_h, "anchor") 
  177. 		 
  178. 		width = width + x  
  179. 		 
  180. 		self.height = button_height 
  181. 		self.width = width 
  182. 		self.hint_data = hint_data 
  183. 		 
  184. 		--Re-enable text shadow... 
  185. 		if self.shadow_enabled then 
  186. 			self:enable_text_shadow(self.shadow_enabled) 
  187. 		end 
  188. 		 
  189. 		if self.width_max ~= nil and do_force_scale == true then 
  190. 			local test_width = self.width 
  191. 			--button_width =  
  192. 			if test_width > self.width_max then 
  193. 				 -- Add extra padding to our button widths... 
  194. 				button_width_total = button_width_total + 20 
  195. 				 
  196. 				--scale text 
  197. 				local new_scale = (self.width_max - button_width_total )/(test_width - button_width_total) 
  198. 				--Reset our hints with the new scale. 
  199. 				self:set_hints(hint_data, force_gamepad_icons, force_kbd_icons, new_scale) 
  200. 			end 
  201. 		end 
  202. 	end 
  203. end 
  204.  
  205. -- Pulses one of the buttons in the hint bar... 
  206. -- 
  207. -- @param button_id 	id of button position that we want to pulse... 
  208. function Vdo_hint_bar:pulse(button_id) 
  209. 	 
  210. 	--Wipe out any previously pulsing buttons... 
  211. 	for idx, val in pairs(self.pulse_buttons) do 
  212. 		val:object_destroy() 
  213. 	end 
  214. 	 
  215. 	self.pulse_buttons = {} 
  216. 	 
  217. 	local button = Vdo_base_object:clone(self.hint_objects[button_id].button.handle, self.handle) 
  218. 	local text = Vdo_base_object:clone(self.hint_objects[button_id].text.handle, self.handle) 
  219. 	button:set_depth(-100) 
  220. 	text:set_depth(-100) 
  221. 	button:set_property("render_mode", "additive_alpha") 
  222. 	text:set_property("render_mode", "additive_alpha") 
  223. 	 
  224. 	self.pulse_buttons.button = button 
  225. 	self.pulse_buttons.text = text 
  226. 	 
  227. 	local text_twn = Vdo_tween_object:new("pulse_text_twn", self.handle, self.doc_handle) 
  228. 	local btn_twn = Vdo_tween_object:new("pulse_btn_twn", self.handle, self.doc_handle) 
  229. 	local btn2_twn = Vdo_tween_object:new("pulse_btn2_twn", self.handle, self.doc_handle) 
  230. 	text_twn:set_target_handle(text.handle) 
  231. 	btn_twn:set_target_handle(button.handle) 
  232. 	btn2_twn:set_target_handle(button.handle) 
  233. 	 
  234. 	local anim = Vdo_anim_object:new("button_pulse", self.handle, self.doc_handle) 
  235. 	anim:play() 
  236. end 
  237.  
  238. function Vdo_hint_bar:enable_text_shadow(enable) 
  239. 	if enable then 
  240. 		for i = 1, self.hint_objects.num do 
  241. 			self.hint_objects[i].text:set_property("shadow_enabled", true) 
  242. 			self.hint_objects[i].text:set_property("shadow_offset", 2, 2) 
  243. 			self.hint_objects[i].text:set_property("shadow_alpha", .75) 
  244. 			self.hint_objects[i].text:set_property("shadow_tint", 0,0,0) 
  245. 		end 
  246. 	else 
  247. 		for i = 1, self.hint_objects.num do 
  248. 			self.hint_objects[i].text:set_property("shadow_enabled", false) 
  249. 		end 
  250. 	end 
  251. 	self.shadow_enabled = enable 
  252. end 
  253.  
  254. ------------------------------------------------------------------------------- 
  255. -- All objects get centered.... 
  256. ------------------------------------------------------------------------------- 
  257. function Vdo_hint_bar:enable_centered(enable) 
  258. 	self.centered = enable 
  259. end 
  260.  
  261. function Vdo_hint_bar:set_color(color) 
  262. 	for i = 1, self.hint_objects.num do 
  263. 		self.hint_objects[i].text:set_color(color.R, color.G, color.B)		 
  264. 	end 
  265. end 
  266.  
  267.  
  268. ------------------------------------------------------------------------------- 
  269. -- Makes button hints 18 point and rebuilds the button list... 
  270. -- 
  271. -- @param	enable	(bool) determines whether or the buttons should be 18pt. 
  272. ------------------------------------------------------------------------------- 
  273. function Vdo_hint_bar:enable_mini_mode(enable) 
  274. 	 
  275. 	if enable == true then 
  276. 		--If enabled resize button list... 
  277. 		self.hint_objects[1].button:set_scale(BUTTON_SCALE_MINI, BUTTON_SCALE_MINI) 
  278. 		self.hint_objects[1].text:set_property("text_scale", TEXT_SCALE_MINI, TEXT_SCALE_MINI) 
  279. 		self.hint_padding = HINT_PADDING_MINI 
  280. 		self.hint_spacing = HINT_SPACING_MINI 
  281. 	else 
  282. 		self.hint_objects[1].button:set_scale(BUTTON_SCALE_NORMAL, BUTTON_SCALE_NORMAL) 
  283. 		self.hint_objects[1].text:set_property("text_scale", TEXT_SCALE_NORMAL, TEXT_SCALE_NORMAL) 
  284. 		 
  285. 		self.hint_padding = HINT_PADDING_NORMAL 
  286. 		self.hint_spacing = HINT_SPACING_NORMAL 
  287. 	end 
  288.  
  289. 	--reset and rebuild hints... 
  290. 	if self.hint_data ~= 0 then 
  291. 		self:set_hints(self.hint_data) 
  292. 	end 
  293. end 
  294.  
  295. ------------------------------------------------------------------------------- 
  296. -- Returns the size of the hints in pixels. 
  297. -- 
  298. -- @return	width		Width of the button hints in pixels 
  299. -- @return	height	Height of the button hints in pixels 
  300. ------------------------------------------------------------------------------- 
  301. function Vdo_hint_bar:get_size() 
  302. 	return self.width, self.height 
  303. end 
  304.  
  305. function Vdo_hint_bar:set_width_max(width_max) 
  306. 	self.width_max = width_max 
  307. end 
  308.  
  309. function Vdo_hint_bar:get_hint_index(target_handle) 
  310. 	for i = 1, self.hint_objects.num do 
  311. 		if self.hint_objects[i].text.handle == target_handle or self.hint_objects[i].button.handle == target_handle then 
  312. 			return i 
  313. 		end 
  314. 	end 
  315. 	 
  316. 	-- Return 0 as an invalid index 
  317. 	return 0 
  318. end 
  319.  
  320. function Vdo_hint_bar:set_highlight(index, color) 
  321. 	for i = 1, self.hint_objects.num do 
  322. 		self.hint_objects[i].text:set_color(COLOR_UNHIGHLIGHT_TEXT.R, COLOR_UNHIGHLIGHT_TEXT. G, COLOR_UNHIGHLIGHT_TEXT.B) 
  323. 	end 
  324. 	 
  325. 	if index ~= 0 then 
  326. 		if self.hint_objects[index] ~= nil then 
  327. 			if color == nil then 
  328. 				color = COLOR_SAINTS_PURPLE 
  329. 			end 
  330. 			 
  331. 			game_UI_audio_play("Ui_main_menu_nav_up") 
  332. 			self.hint_objects[index].text:set_color(color.R, color.G, color.B) 
  333. 		else 
  334. 			debug_print("vint", "hint_bar:set_highlight - invalid index") 
  335. 		end 
  336. 	end 
  337. end 
  338.  
  339.  
  340. ------------------------------------------------------------------------------- 
  341. --								Mouse/PC Functions 
  342. ------------------------------------------------------------------------------- 
  343. -- Adds mouse input subscriptions to the input tracker 
  344. -- 
  345. -- @param	func_prefix			Name of the screen that is currently using the hint bar 
  346. -- @param	input_tracker		The input tracker to hold the mouse inputs events 
  347. -- @param	priority				THe priority of the input event 
  348. function Vdo_hint_bar:add_mouse_inputs(func_prefix, input_tracker, priority) 
  349. 	if func_prefix == nil then 
  350. 		return 
  351. 	end 
  352. 	 
  353. 	priority = priority or 50 
  354. 	 
  355. 	local mouse_click_function = func_prefix.."_mouse_click" 
  356. 	local mouse_move_function = func_prefix.."_mouse_move" 
  357.  
  358. 	for i = 1, self.hint_objects.num do 
  359. 		if self.hint_objects[i].text:get_visible() then 
  360. 			input_tracker:add_mouse_input("mouse_click", mouse_click_function, priority, self.hint_objects[i].text.handle) 
  361. 			input_tracker:add_mouse_input("mouse_move", mouse_move_function, priority, self.hint_objects[i].text.handle) 
  362. 			input_tracker:add_mouse_input("mouse_click", mouse_click_function, priority, self.hint_objects[i].button.handle) 
  363. 			input_tracker:add_mouse_input("mouse_move", mouse_move_function, priority, self.hint_objects[i].button.handle) 
  364. 		end 
  365. 	end 
  366. end 
  367.  
  368. function Vdo_hint_bar:override_mouse_depths(depth) 
  369. 	for i = 1, self.hint_objects.num do 
  370. 		if self.hint_objects[i].text:get_visible() then 
  371. 			vint_set_property(self.hint_objects[i].text.handle, "mouse_depth", depth) 
  372. 			vint_set_property(self.hint_objects[i].button.handle, "mouse_depth", depth) 
  373. 		end 
  374. 	end 
  375. end