./vdo_hint_button.lua

  1. -- NOTE: For PC-specific functions, they all start with pc_ so they can easily be removed or blanked out if necessary (due to memory constraints on consoles) 
  2.  
  3. HINT_BUTTON_RMB_OVERRIDE = "rmb_override" 
  4.  
  5. function vdo_hint_button_init() 
  6. end 
  7.  
  8. function vdo_hint_button_cleanup() 
  9. end 
  10.  
  11. -- Inherited from Vdo_base_object 
  12. Vdo_hint_button = Vdo_base_object:new_base() 
  13.  
  14. function Vdo_hint_button:init() 
  15. 	if game_get_platform() == "PC" then 
  16. 		self:pc_init() 
  17. 	end 
  18. end 
  19.  
  20. -- Do all the initialization for PC-centric hints 
  21. function Vdo_hint_button:pc_init() 
  22. 	-- Destroy any extra elements if they exist 
  23. 	self:pc_destroy_extra() 
  24. 	 
  25. 	-- Hide the default hint icon 
  26. 	self.old_icon = Vdo_base_object:new("hint_icon", self.handle, self.doc_handle) 
  27. 	self.old_icon:set_visible(false) 
  28. 	 
  29. 	-- Create default settings 
  30. 	self.max_width = 100 -- Not including the buffer 
  31. 	self.default_width = 28 
  32. 	self.height = 32 -- Includes buffer 
  33. 	self.text_offset = 6 
  34. 	self.width = self.default_width 
  35. 	self.buffer = 6 
  36. 	self.single_letter = true 
  37. 	self.two_line = false 
  38. 	self.bg_color = {R=220/255,G=220/255,B=220/255} 
  39. 	self.text_color = {R=220/255,G=220/255,B=220/255} 
  40. 	self.size_large = 0.75 
  41. 	self.size_small = 0.55 
  42.  
  43. 	-- Create the backdrop (three images, move/scale them in set_button) 
  44. 	self.bg_left_h = vint_object_create("bg_left", "bitmap", self.handle) 
  45. 	self.bg_right_h = vint_object_create("bg_right", "bitmap", self.handle) 
  46. 	self.bg_middle_h = vint_object_create("bg_middle", "bitmap", self.handle) 
  47. 	self.bg_left = Vdo_base_object:new("bg_left", self.handle, self.doc_handle) 
  48. 	self.bg_right = Vdo_base_object:new("bg_right", self.handle, self.doc_handle) 
  49. 	self.bg_middle = Vdo_base_object:new("bg_middle", self.handle, self.doc_handle) 
  50. 		 
  51. 	self.bg_left:set_property("auto_offset", "c") 
  52. 	self.bg_left:set_image("ui_pc_key_side") 
  53. 	self.bg_left:set_color(self.bg_color) 
  54. 	self.bg_left:set_anchor((self.width + self.buffer) * -0.5 + 4, 0) 
  55. 	self.bg_left:set_actual_size(8, self.height) 
  56. 	self.bg_left:set_visible(false) 
  57. 	self.bg_left:set_depth(2) 
  58. 	 
  59. 	self.bg_right:set_property("auto_offset", "c") 
  60. 	self.bg_right:set_image("ui_pc_key_side") 
  61. 	self.bg_right:set_color(self.bg_color) 
  62. 	self.bg_right:set_anchor((self.width + self.buffer) * 0.5 - 4, 0) 
  63. 	self.bg_right:set_actual_size(-8, self.height) 
  64. 	self.bg_right:set_visible(false) 
  65. 	self.bg_right:set_depth(2) 
  66. 	 
  67. 	self.bg_middle:set_property("auto_offset", "c") 
  68. 	self.bg_middle:set_image("ui_pc_key_middle") 
  69. 	self.bg_middle:set_color(self.bg_color) 
  70. 	self.bg_middle:set_actual_size(self.width + self.buffer - 16, self.height) 
  71. 	self.bg_middle:set_visible(false) 
  72. 	self.bg_middle:set_depth(2) 
  73. 	 
  74. 	-- Set up text for key names 
  75. 	self.key_text1_h = vint_object_create("key_text1", "text", self.handle) 
  76. 	self.key_text2_h = vint_object_create("key_text2", "text", self.handle) 
  77. 	self.key_text1 = Vdo_base_object:new("key_text1", self.handle, self.doc_handle) 
  78. 	self.key_text2 = Vdo_base_object:new("key_text2", self.handle, self.doc_handle) 
  79. 	 
  80. 	self.key_text1:set_property("font", "thin") 
  81. 	self.key_text1:set_property("auto_offset", "c") 
  82. 	self.key_text1:set_text("!") 
  83. 	self.key_text1:set_anchor(0, 0) 
  84. 	self.key_text1:set_scale(self.size_large, self.size_large) 
  85. 	self.key_text1:set_color(self.text_color) 
  86. 	self.key_text1:set_visible(false) 
  87. 	 
  88. 	self.key_text2:set_property("font", "thin") 
  89. 	self.key_text2:set_property("auto_offset", "c") 
  90. 	self.key_text2:set_text("") 
  91. 	self.key_text2:set_anchor(0, self.text_offset) -- self.buffer * 0.5) 
  92. 	self.key_text2:set_scale(self.size_small, self.size_small) 
  93. 	self.key_text2:set_color(self.text_color) 
  94. 	self.key_text2:set_visible(false) 
  95. end 
  96.  
  97. function Vdo_hint_button:pc_destroy_extra() 
  98. 	local temp_obj = Vdo_base_object:new("key_text1", self.handle, self.doc_handle) 
  99. 	if temp_obj ~= nil then 
  100. 		temp_obj:object_destroy() 
  101. 	end 
  102. 	temp_obj = Vdo_base_object:new("key_text2", self.handle, self.doc_handle) 
  103. 	if temp_obj ~= nil then 
  104. 		temp_obj:object_destroy() 
  105. 	end 
  106. 	temp_obj = Vdo_base_object:new("bg_left", self.handle, self.doc_handle) 
  107. 	if temp_obj ~= nil then 
  108. 		temp_obj:object_destroy() 
  109. 	end 
  110. 	temp_obj = Vdo_base_object:new("bg_right", self.handle, self.doc_handle) 
  111. 	if temp_obj ~= nil then 
  112. 		temp_obj:object_destroy() 
  113. 	end 
  114. 	temp_obj = Vdo_base_object:new("bg_middle", self.handle, self.doc_handle) 
  115. 	if temp_obj ~= nil then 
  116. 		temp_obj:object_destroy() 
  117. 	end 
  118. end 
  119.  
  120. function Vdo_hint_button:pc_button_set_visible(visible) 
  121. 	local temp_obj = Vdo_base_object:new("key_text1", self.handle, self.doc_handle) 
  122. 	if temp_obj ~= nil then 
  123. 		temp_obj:set_visible(visible) 
  124. 	end 
  125. 	temp_obj = Vdo_base_object:new("key_text2", self.handle, self.doc_handle) 
  126. 	if temp_obj ~= nil then 
  127. 		temp_obj:set_visible(visible) 
  128. 	end 
  129. 	temp_obj = Vdo_base_object:new("bg_left", self.handle, self.doc_handle) 
  130. 	if temp_obj ~= nil then 
  131. 		temp_obj:set_visible(visible) 
  132. 	end 
  133. 	temp_obj = Vdo_base_object:new("bg_right", self.handle, self.doc_handle) 
  134. 	if temp_obj ~= nil then 
  135. 		temp_obj:set_visible(visible) 
  136. 	end 
  137. 	temp_obj = Vdo_base_object:new("bg_middle", self.handle, self.doc_handle) 
  138. 	if temp_obj ~= nil then 
  139. 		temp_obj:set_visible(visible) 
  140. 	end 
  141. end 
  142.  
  143. -- Sets the Art and Color of the button object 
  144. function Vdo_hint_button:set_button( button, pc_key_text, force_gamepad_icon, force_kbd_icon ) 
  145.  
  146. 	force_gamepad_icon = force_gamepad_icon or false 
  147. 	force_kbd_icon = force_kbd_icon or false 
  148.  
  149. 	local icon = Vdo_base_object:new( "hint_icon", self.handle, self.doc_handle) 
  150. 	if force_kbd_icon or (game_is_active_input_gamepad() == false and force_gamepad_icon == false) then 
  151. 		if game_get_platform() == "PC" then 
  152. 			if (pc_key_text ~= nil) then 
  153. 				self:pc_set_key_text(pc_key_text) 
  154. 			else 
  155. 				self:pc_set_button(button) 
  156. 			end 
  157. 			self:pc_button_set_visible(true) 
  158. 		end	 
  159. 		icon:set_visible(false) 
  160. 	else 
  161. 		icon:set_image(button) 
  162. 		icon:set_visible(true) 
  163. 		 
  164. 		if game_get_platform() == "PC" then 
  165. 			self:pc_button_set_visible(false) 
  166. 		end 
  167. 	end 
  168. end 
  169.  
  170. -- Set the button text based on the button given 
  171. function Vdo_hint_button:pc_set_button(button) 
  172. 	if button == CTRL_MENU_BUTTON_A then 
  173. 		self:pc_set_key_text(game_get_key_name(28)) -- "ENTER" 
  174. 	elseif button == CTRL_MENU_BUTTON_B then 
  175. 		self:pc_set_key_text(game_get_key_name(1)) -- "ESC" 
  176. 	elseif button == CTRL_BUTTON_X then 
  177. 		self:pc_set_key_text(game_get_key_name(59)) -- "F1" 
  178. 	elseif button == CTRL_BUTTON_Y then 
  179. 		self:pc_set_key_text(game_get_key_name(57), false) --"Space" 
  180. 	elseif button == HINT_BUTTON_RMB_OVERRIDE then 
  181. 		self:pc_set_key_text("MENU_RIGHT_MOUSE") 
  182. 	elseif button == CTRL_BUTTON_LB then 
  183. 		self:pc_set_key_text("LB") 
  184. 	elseif button == CTRL_BUTTON_RB then 
  185. 		self:pc_set_key_text("RB") 
  186. 	elseif button == CTRL_BUTTON_LT then 
  187. 		self:pc_set_key_text("LT") 
  188. 	elseif button == CTRL_BUTTON_RT then 
  189. 		self:pc_set_key_text("RT") 
  190. 	elseif button == CTRL_BUTTON_LS then 
  191. 		self:pc_set_key_text("LEFT", false, true, "STICK") 
  192. 	elseif button == CTRL_BUTTON_LS_PRESS then 
  193. 		self:pc_set_key_text("LEFT", false, true, "CLICK") 
  194. 	elseif button == CTRL_BUTTON_RS then 
  195. 		self:pc_set_key_text("RIGHT", false, true, "STICK") 
  196. 	elseif button == CTRL_BUTTON_RS_PRESS then 
  197. 		self:pc_set_key_text("RIGHT", false, true, "CLICK") 
  198. 	elseif button == CTRL_MENU_BUTTON_BACK then 
  199. 		self:pc_set_key_text(game_get_key_name(15), false) --"Tab" 
  200. 	elseif button == CTRL_BUTTON_START then 
  201. 		self:pc_set_key_text(game_get_key_name(28)) -- "ENTER" 
  202. 	elseif button == CTRL_BUTTON_DPAD then 
  203. 		self:pc_set_key_text("ARROW KEYS") 
  204. 	elseif button == CTRL_BUTTON_DPAD_UP then 
  205. 		self:pc_set_key_text("UP") 
  206. 	elseif button == CTRL_BUTTON_DPAD_DOWN then 
  207. 		self:pc_set_key_text("DOWN") 
  208. 	elseif button == CTRL_BUTTON_DPAD_LEFT then 
  209. 		self:pc_set_key_text("LEFT") 
  210. 	elseif button == CTRL_BUTTON_DPAD_RIGHT then 
  211. 		self:pc_set_key_text("RIGHT") 
  212. 	elseif button == CTRL_BUTTON_DPAD_UP_DOWN then 
  213. 		self:pc_set_key_text("UP", false, true, "DOWN") 
  214. 	elseif button == CTRL_BUTTON_DPAD_LEFT_RIGHT then 
  215. 		self:pc_set_key_text("LEFT", false, true, "RIGHT") 
  216. 	else 
  217. 		self:pc_set_key_text("UNKNOWN KEY", false, true, "PLEASE ADD MAPPING") 
  218. 	end 
  219. end 
  220.  
  221. -- Sets the key text 
  222. function Vdo_hint_button:pc_set_key_text(text, single, two_line, t2) 
  223. 	self.single_letter = single or false 
  224. 	self.two_line = two_line or false 
  225. 	local text2 = t2 or "" 
  226. 	 
  227. 	-- Set the primary key text 
  228. 	self.key_text1:set_text(text) 
  229. 	self.key_text1:set_visible(true) 
  230. 	 
  231. 	if self.single_letter then 
  232. 		-- Set to single letter scale 
  233. 		self.key_text1:set_scale(self.size_large, self.size_large) 
  234. 		self.key_text1:set_anchor(0, 0) 
  235. 		self.key_text2:set_text("") 
  236. 		self.key_text2:set_visible(false) 
  237. 		 
  238. 		self.width = self.default_width 
  239. 	else 
  240. 		local max_width = self.default_width 
  241. 		local w, h, w2 
  242. 		 
  243. 		-- Set text scale and resize if necessary 
  244. 		self.key_text1:set_scale(self.size_small, self.size_small) 
  245. 		self.key_text2:set_scale(self.size_small, self.size_small) 
  246. 		w, h = self.key_text1:get_actual_size() 
  247. 		max_width = max(max_width, w) 
  248. 		if max_width > self.max_width then 
  249. 			self.key_text1:set_actual_size(self.max_width, h) 
  250. 			max_width = self.max_width 
  251. 		end 
  252. 		 
  253. 		if self.two_line then 
  254. 			self.key_text1:set_anchor(0, -self.text_offset) 
  255. 			self.key_text2:set_text(text2) 
  256.  
  257. 			self.key_text2:set_visible(true) 
  258. 			 
  259. 			-- Readjust size 
  260. 			w, h = self.key_text2:get_actual_size() 
  261. 			max_width = max(max_width, w) 
  262. 			if max_width > self.max_width then 
  263. 				self.key_text2:set_actual_size(self.max_width, h) 
  264. 				max_width = self.max_width 
  265. 			end 
  266. 		else 
  267. 			self.key_text1:set_anchor(0, 0) 
  268. 			self.key_text2:set_text("") 
  269. 			self.key_text2:set_visible(false) 
  270. 		end 
  271. 		 
  272. 		-- Find the smallest X scale for the two text fields and set that scale for all dimensions of both fields 
  273. 		w, h = self.key_text1:get_scale() 
  274. 		w = w or 1.0 
  275. 		w2, h = self.key_text2:get_scale() 
  276. 		w2 = w2 or 1.0 
  277. 		w = min(w, w2) 
  278. 		self.key_text1:set_scale(w, w) 
  279. 		self.key_text2:set_scale(w, w) 
  280. 		 
  281. 		self.width = max_width 
  282. 	end 
  283. 	 
  284. 	-- Readjust the backdrop size 
  285. 	self.bg_left:set_anchor((self.width + self.buffer) * -0.5 + 4, 0) 
  286. 	self.bg_right:set_anchor((self.width + self.buffer) * 0.5 - 4, 0) 
  287. 	self.bg_middle:set_actual_size(self.width + self.buffer - 16, self.height) 
  288. 	self.bg_left:set_visible(true) 
  289. 	self.bg_right:set_visible(true) 
  290. 	self.bg_middle:set_visible(true) 
  291. end 
  292.  
  293. function Vdo_hint_button:get_size() 
  294. 	if game_is_active_input_gamepad() == false then 
  295. 		local width, height = self.bg_middle:get_actual_size() 
  296. 		width = width + 16 
  297. 		return width, height 
  298. 	else 
  299. 		local icon = Vdo_base_object:new("hint_icon", self.handle, self.doc_handle) 
  300. 		local width, height = icon:get_actual_size() 
  301. 		return width, height 
  302. 	end 
  303. end 
  304.  
  305. -- Gets the width of the default hint icon (used for alignment on PC) 
  306. function Vdo_hint_button:get_default_width() 
  307. 	local icon = Vdo_base_object:new("hint_icon", self.handle, self.doc_handle) 
  308. 	local width, height = icon:get_actual_size() 
  309. 	return width 
  310. end 
  311.  
  312. function Vdo_hint_button:set_disabled() 
  313. 	local hint_anim = Vdo_anim_object:new("hint_ring_anim",self.handle, self.doc_handle) 
  314. 	hint_anim:stop() 
  315. end 
  316.  
  317. function Vdo_hint_button:set_enabled() 
  318. 	local hint_anim = Vdo_anim_object:new("hint_ring_anim",self.handle, self.doc_handle) 
  319. 	hint_anim:play() 
  320. end