./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.  
  169. 	if game_machinima_hide_hints() then 
  170. 		self:pc_button_set_visible(false) 
  171. 		icon:set_visible(false) 
  172. 	end 
  173. end 
  174.  
  175. -- Set the button text based on the button given 
  176. function Vdo_hint_button:pc_set_button(button) 
  177. 	if button == CTRL_MENU_BUTTON_A then 
  178. 		self:pc_set_key_text(game_get_key_name(28)) -- "ENTER" 
  179. 	elseif button == CTRL_MENU_BUTTON_B then 
  180. 		self:pc_set_key_text(game_get_key_name(1)) -- "ESC" 
  181. 	elseif button == CTRL_BUTTON_X then 
  182. 		self:pc_set_key_text(game_get_key_name(59)) -- "F1" 
  183. 	elseif button == CTRL_BUTTON_Y then 
  184. 		self:pc_set_key_text(game_get_key_name(57), false) --"Space" 
  185. 	elseif button == HINT_BUTTON_RMB_OVERRIDE then 
  186. 		self:pc_set_key_text("MENU_RIGHT_MOUSE") 
  187. 	elseif button == CTRL_BUTTON_LB then 
  188. 		self:pc_set_key_text("LB") 
  189. 	elseif button == CTRL_BUTTON_RB then 
  190. 		self:pc_set_key_text("RB") 
  191. 	elseif button == CTRL_BUTTON_LT then 
  192. 		self:pc_set_key_text("LT") 
  193. 	elseif button == CTRL_BUTTON_RT then 
  194. 		self:pc_set_key_text("RT") 
  195. 	elseif button == CTRL_BUTTON_LS then 
  196. 		self:pc_set_key_text("LEFT", false, true, "STICK") 
  197. 	elseif button == CTRL_BUTTON_LS_PRESS then 
  198. 		self:pc_set_key_text("LEFT", false, true, "CLICK") 
  199. 	elseif button == CTRL_BUTTON_RS then 
  200. 		self:pc_set_key_text("RIGHT", false, true, "STICK") 
  201. 	elseif button == CTRL_BUTTON_RS_PRESS then 
  202. 		self:pc_set_key_text("RIGHT", false, true, "CLICK") 
  203. 	elseif button == CTRL_MENU_BUTTON_BACK then 
  204. 		self:pc_set_key_text(game_get_key_name(15), false) --"Tab" 
  205. 	elseif button == CTRL_BUTTON_START then 
  206. 		self:pc_set_key_text(game_get_key_name(28)) -- "ENTER" 
  207. 	elseif button == CTRL_BUTTON_DPAD then 
  208. 		self:pc_set_key_text("MENU_ARROW_KEYS") 
  209. 	elseif button == CTRL_BUTTON_DPAD_UP then 
  210. 		self:pc_set_key_text("UP") 
  211. 	elseif button == CTRL_BUTTON_DPAD_DOWN then 
  212. 		self:pc_set_key_text("DOWN") 
  213. 	elseif button == CTRL_BUTTON_DPAD_LEFT then 
  214. 		self:pc_set_key_text("LEFT") 
  215. 	elseif button == CTRL_BUTTON_DPAD_RIGHT then 
  216. 		self:pc_set_key_text("RIGHT") 
  217. 	elseif button == CTRL_BUTTON_DPAD_UP_DOWN then 
  218. 		self:pc_set_key_text("UP", false, true, "DOWN") 
  219. 	elseif button == CTRL_BUTTON_DPAD_LEFT_RIGHT then 
  220. 		self:pc_set_key_text("LEFT", false, true, "RIGHT") 
  221. 	elseif button == CTRL_BUTTON_MOUSE_SCROLL then 
  222. 		self:pc_set_key_text("PC_MOUSE", false, true, "PC_SCROLL") 
  223. 	else 
  224. 		self:pc_set_key_text("UNKNOWN KEY", false, true, "PLEASE ADD MAPPING") 
  225. 	end 
  226. end 
  227.  
  228. -- Sets the key text 
  229. function Vdo_hint_button:pc_set_key_text(text, single, two_line, t2) 
  230. 	self.single_letter = single or false 
  231. 	self.two_line = two_line or false 
  232. 	local text2 = t2 or "" 
  233. 	 
  234. 	-- Set the primary key text 
  235. 	self.key_text1:set_text(text) 
  236. 	self.key_text1:set_visible(true) 
  237. 	 
  238. 	if self.single_letter then 
  239. 		-- Set to single letter scale 
  240. 		self.key_text1:set_scale(self.size_large, self.size_large) 
  241. 		self.key_text1:set_anchor(0, 0) 
  242. 		self.key_text2:set_text("") 
  243. 		self.key_text2:set_visible(false) 
  244. 		 
  245. 		self.width = self.default_width 
  246. 	else 
  247. 		local max_width = self.default_width 
  248. 		local w, h, w2 
  249. 		 
  250. 		-- Set text scale and resize if necessary 
  251. 		self.key_text1:set_scale(self.size_small, self.size_small) 
  252. 		self.key_text2:set_scale(self.size_small, self.size_small) 
  253. 		w, h = self.key_text1:get_actual_size() 
  254. 		max_width = max(max_width, w) 
  255. 		if max_width > self.max_width then 
  256. 			self.key_text1:set_actual_size(self.max_width, h) 
  257. 			max_width = self.max_width 
  258. 		end 
  259. 		 
  260. 		if self.two_line then 
  261. 			self.key_text1:set_anchor(0, -self.text_offset) 
  262. 			self.key_text2:set_text(text2) 
  263.  
  264. 			self.key_text2:set_visible(true) 
  265. 			 
  266. 			-- Readjust size 
  267. 			w, h = self.key_text2:get_actual_size() 
  268. 			max_width = max(max_width, w) 
  269. 			if max_width > self.max_width then 
  270. 				self.key_text2:set_actual_size(self.max_width, h) 
  271. 				max_width = self.max_width 
  272. 			end 
  273. 		else 
  274. 			self.key_text1:set_anchor(0, 0) 
  275. 			self.key_text2:set_text("") 
  276. 			self.key_text2:set_visible(false) 
  277. 		end 
  278. 		 
  279. 		-- Find the smallest X scale for the two text fields and set that scale for all dimensions of both fields 
  280. 		w, h = self.key_text1:get_scale() 
  281. 		w = w or 1.0 
  282. 		w2, h = self.key_text2:get_scale() 
  283. 		w2 = w2 or 1.0 
  284. 		w = min(w, w2) 
  285. 		self.key_text1:set_scale(w, w) 
  286. 		self.key_text2:set_scale(w, w) 
  287. 		 
  288. 		self.width = max_width 
  289. 	end 
  290. 	 
  291. 	-- Readjust the backdrop size 
  292. 	self.bg_left:set_anchor((self.width + self.buffer) * -0.5 + 4, 0) 
  293. 	self.bg_right:set_anchor((self.width + self.buffer) * 0.5 - 4, 0) 
  294. 	self.bg_middle:set_actual_size(self.width + self.buffer - 16, self.height) 
  295. 	self.bg_left:set_visible(true) 
  296. 	self.bg_right:set_visible(true) 
  297. 	self.bg_middle:set_visible(true) 
  298. end 
  299.  
  300. function Vdo_hint_button:get_size() 
  301. 	if game_is_active_input_gamepad() == false then 
  302. 		local width, height = self.bg_middle:get_actual_size() 
  303. 		width = width + 16 
  304. 		return width, height 
  305. 	else 
  306. 		local icon = Vdo_base_object:new("hint_icon", self.handle, self.doc_handle) 
  307. 		local width, height = icon:get_actual_size() 
  308. 		return width, height 
  309. 	end 
  310. end 
  311.  
  312. -- Gets the width of the default hint icon (used for alignment on PC) 
  313. function Vdo_hint_button:get_default_width() 
  314. 	local icon = Vdo_base_object:new("hint_icon", self.handle, self.doc_handle) 
  315. 	local width, height = icon:get_actual_size() 
  316. 	return width 
  317. end 
  318.  
  319. function Vdo_hint_button:set_disabled() 
  320. 	local hint_anim = Vdo_anim_object:new("hint_ring_anim",self.handle, self.doc_handle) 
  321. 	hint_anim:stop() 
  322. end 
  323.  
  324. function Vdo_hint_button:set_enabled() 
  325. 	local hint_anim = Vdo_anim_object:new("hint_ring_anim",self.handle, self.doc_handle) 
  326. 	hint_anim:play() 
  327. end