./vdo_input_tracker.lua

  1. --[[ 
  2. -- Controller 
  3. "select", 
  4. "alt_select", 
  5.  
  6. "exit", 
  7. "back", 
  8.  
  9. "nav_up", 
  10. "nav_down",  
  11. "nav_left", 
  12. "nav_right", 
  13.  
  14. "joy_up", 
  15. "joy_down", 
  16. "joy_left", 
  17. "joy_right", 
  18.  
  19. "alt_joy_up", 
  20. "alt_joy_down", 
  21. "alt_joy_left", 
  22. "alt_joy_right", 
  23.  
  24. "scroll_up", 
  25. "scroll_down", 
  26. "scroll_left", 
  27. "scroll_right", 
  28.  
  29. "dpad_up", 
  30. "dpad_down", 
  31. "dpad_left", 
  32. "dpad_right", 
  33.  
  34. "all_unassigned", 
  35.  
  36. -- Game specific 
  37. "map", 	// SR4: Bring up the HUB menu 
  38. "pause", // SR4: Pause menu 
  39.  
  40. -- Gamepad 
  41. "gamepad_a", 
  42. "gamepad_b", 
  43. "gamepad_x", 
  44. "gamepad_y", 
  45.  
  46. "key_up", 
  47. "key_down", 
  48. "key_left", 
  49. "key_right", 
  50.  
  51. "any_key", 
  52.  
  53. "scancode", 
  54.  
  55. -- Mouse Inputs 
  56. "mouse_click", 
  57. "mouse_right_click", 
  58. "mouse_middle_click", 
  59. "mouse_down", 
  60. "mouse_move", 
  61. "mouse_move_always", 
  62. "mouse_drag", 
  63. "mouse_drag_release", 
  64. "mouse_scroll", 
  65. ]]-- 
  66.  
  67.  
  68. -- Object to simplify turning on/off input event subscriptions 
  69. Vdo_input_tracker = {} 
  70.  
  71. VDO_INPUT_TRACKER_DELAY_SECONDS				= 0.6		-- Base time in seconds to delay before sending next inpt event 
  72. VDO_INPUT_TRACKER_ACCELERATION_FACTOR		= 1.2		-- How quickly input velocity compounds over time when holding the input button 
  73. VDO_INPUT_TRACKER_MAX_ACCELERATION			= 10		-- Maximum accleration.  The smallest delay time will be VDO_INPUT_TRACKER_DELAY_SECONDS * VDO_INPUT_TRACKER_MAX_ACCELERATION. 
  74.  
  75. VDO_INPUT_SLIDER_ACCEL_REPEAT =	0.4 
  76. VDO_INPUT_SLIDER_ACCEL_FACTOR =	30 
  77. VDO_INPUT_SLIDER_ACCEL_LIMIT =	1000 
  78.  
  79. -- Creates a new vdo input event tracker 
  80. -- 
  81. function Vdo_input_tracker:new() 
  82. 	local obj = { 
  83. 		input_infos = {}, 
  84. 	} 
  85.  
  86. 	setmetatable(obj, self) 
  87. 	self.__index = self 
  88. 	self.highspeed_on = -1 
  89. 	return obj 
  90. end 
  91.  
  92. -- Adds an input event to use (Note that it doesn't actually subscribe the input) 
  93. -- is_raw is optional 
  94. -- 
  95. function Vdo_input_tracker:add_input(event_name, func_name, priority, is_raw, scancode, lock_on_release) 
  96. 	-- Make a new entry after any current entries 
  97. 	local index = #self.input_infos + 1 
  98. 	self.input_infos[index] = {} 
  99. 	local info = self.input_infos[index] 
  100.  
  101. 	info.event_name = event_name 
  102. 	info.func_name = func_name 
  103. 	info.priority = priority 
  104. 	info.is_raw = is_raw or false 
  105. 	 
  106. 	if lock_on_release == nil then 
  107. 		lock_on_release = true 
  108. 	end 
  109. 	info.lock_on_release = lock_on_release 
  110. 	 
  111. 	if game_get_platform() == "PC" then 
  112. 		info.scancode = scancode or -1 
  113. 	end 
  114. end 
  115.  
  116. -- Adds a mouse input event to use (Note that it doesn't actually subscribe the input) 
  117. -- 
  118. function Vdo_input_tracker:add_mouse_input(event_name, func_name, priority, target_handle) 
  119. 	-- Make a new entry after any current entries 
  120. 	local index = #self.input_infos + 1 
  121. 	self.input_infos[index] = {} 
  122. 	local info = self.input_infos[index] 
  123.  
  124. 	info.event_name = event_name 
  125. 	info.func_name = func_name 
  126. 	info.priority = priority 
  127. 	info.target_handle = target_handle 
  128. 	info.is_raw = false 
  129. 	info.is_mouse = true 
  130. end 
  131.  
  132. --[[ --------------------------------------------------------------------- 
  133. -- Function - remove_input_by_object_handle 
  134. -- Description - Removes a specific mouse input event and unsubscribes to it if necessary 
  135. -- Parameters -  
  136. -- 		event_name 		- The name of the mouse input to remove 
  137. --			target_handle 	- The target handle of the input to remove 
  138. -- Returns - nil 
  139. ]]-- --------------------------------------------------------------------- 
  140. function Vdo_input_tracker:remove_input_by_object_handle(event_name, target_handle) 
  141. 	-- Cycle through all inputs  
  142. 	for i, info in pairs(self.input_infos) do 
  143. 		-- Find an input that matches the specified name   
  144. 		if( info.event_name == event_name )then 
  145. 			-- Find an input that matches the specified handle 
  146. 			if( info.target_handle == target_handle )then 
  147. 				if( info.handle ~= nil  ) then 
  148. 					if( info.is_raw )then -- Unsubcribe if it is raw 
  149. 						vint_unsubscribe_to_raw_input(info.handle) 
  150. 					elseif( info.is_mouse )then -- Unsubscribe if it is mouse 
  151. 						vint_unsubscribe_to_mouse_input(info.handle) 
  152. 					else -- Unsubcribe if it is anything else 
  153. 						vint_unsubscribe_to_input_event(info.handle) 
  154. 					end 
  155. 				end 
  156. 				-- Clear that part of the table 
  157. 				self.input_infos[i] = nil 
  158. 				return 
  159. 			end 
  160. 		end 
  161. 	end 
  162. end 
  163.  
  164. -- Removes an input event and unsubscribes to it if necessary 
  165. -- 
  166. function Vdo_input_tracker:remove_input(event_name) 
  167. 	for i, info in pairs(self.input_infos) do 
  168. 		if info.event_name == event_name then 
  169. 			 
  170. 			if info.handle ~= nil then 
  171. 				if info.is_raw then 
  172. 					vint_unsubscribe_to_raw_input(info.handle) 
  173. 				elseif info.is_mouse then 
  174. 					vint_unsubscribe_to_mouse_input(info.handle) 
  175. 				else 
  176. 					vint_unsubscribe_to_input_event(info.handle) 
  177. 				end 
  178. 			end 
  179. 			 
  180. 			self.input_infos[i] = nil	 
  181. 			 
  182. 			return 
  183. 		end 
  184. 	end 
  185. end 
  186.  
  187. -- Removes all of the input events and unsubscribes to them if necessary 
  188. -- 
  189. function Vdo_input_tracker:remove_all(dont_force_mouse_move) 
  190. 	for i, info in pairs(self.input_infos) do 
  191. 		if info.handle ~= nil then 
  192. 			if info.is_raw then 
  193. 				vint_unsubscribe_to_raw_input(info.handle) 
  194. 			elseif info.is_mouse then 
  195. 				vint_unsubscribe_to_mouse_input(info.handle) 
  196. 				 
  197. 				if dont_force_mouse_move ~= true and game_is_active_input_gamepad() == false then 
  198. 					--vint_force_mouse_move_event() 
  199. 				end 
  200. 			else 
  201. 				vint_unsubscribe_to_input_event(info.handle) 
  202. 			end 
  203. 		end 
  204. 		 
  205. 		self.input_infos[i] = nil 
  206. 	end 
  207. end 
  208.  
  209. -- Subscribes/unsubscribes all added input events 
  210. -- 
  211. function Vdo_input_tracker:subscribe(enable, dont_force_mouse_move) 
  212.  
  213. 	self.is_subscribed = enable 
  214.  
  215. 	local mouse_move_event_changed = false 
  216.  
  217. 	-- Just in case, always unsubscribe any existing input_infos (should be empty if we properly unsubscribed before subscribing) 
  218. 	for i, info in pairs(self.input_infos) do 
  219. 		if info.handle ~= nil then 
  220. 			if info.is_raw then 
  221. 				vint_unsubscribe_to_raw_input(info.handle) 
  222. 			elseif info.is_mouse then 
  223. 				vint_unsubscribe_to_mouse_input(info.handle) 
  224. 				mouse_move_event_changed = true 
  225. 			else 
  226. 				vint_unsubscribe_to_input_event(info.handle) 
  227. 			end 
  228. 			info.handle = nil 
  229. 		end 
  230. 	end 
  231. 	 
  232. 	if enable then 
  233. 		for i, info in pairs(self.input_infos) do 
  234. 			if info.is_raw then 
  235. 				info.handle = vint_subscribe_to_raw_input(info.event_name, info.func_name, info.priority) 
  236. 			elseif info.is_mouse then 
  237. 				info.handle = vint_subscribe_to_mouse_input(info.event_name, info.func_name, info.priority, info.target_handle) 
  238. 				mouse_move_event_changed = true 
  239. 			else 
  240. 				local scancode = info.scancode or -1 
  241. 				info.handle = vint_subscribe_to_input_event(info.event_name, info.func_name, info.priority, scancode, info.lock_on_release) 
  242. 				if info.handle ~= nil then 
  243. 					self:set_input_accelleration(info.handle, info.event_name) 
  244. 				end 
  245. 			end 
  246. 		end 
  247. 	end 
  248. 	 
  249. 	if mouse_move_event_changed and dont_force_mouse_move ~= true and game_is_active_input_gamepad() == false then 
  250. 		--vint_force_mouse_move_event() 
  251. 	end 
  252. end 
  253.  
  254. ------------------------------------------------------------------------------- 
  255. -- Sets the input accelleration per input, depends whether or not the highspeed flag has been set or not for the entire vdo. (JMH 5/17/2011) 
  256. -- NOTE: This should be generalized per input subscription and handled directly on each item type in a megalist... 
  257. -- 
  258. function Vdo_input_tracker:set_input_accelleration(handle, event_name) 
  259. 	local delay 		= VDO_INPUT_TRACKER_DELAY_SECONDS 
  260. 	local factor_acc	= VDO_INPUT_TRACKER_ACCELERATION_FACTOR 
  261. 	local max_acc 		= VDO_INPUT_TRACKER_MAX_ACCELERATION 
  262. 	 
  263. 	if self.highspeed_on == true then 
  264. 		if event_name == "nav_left" or event_name == "nav_right" then 
  265. 			delay 		= VDO_INPUT_SLIDER_ACCEL_REPEAT 
  266. 			factor_acc	= VDO_INPUT_SLIDER_ACCEL_FACTOR 
  267. 			max_acc 		= VDO_INPUT_SLIDER_ACCEL_LIMIT 
  268. 		end 
  269. 	end 
  270. 	vint_set_input_params(handle, delay, factor_acc, max_acc, false, true) 
  271. end 
  272.  
  273. ------------------------------------------------------------------------------- 
  274. -- Toggles highspeed for left and right inputs. (JMH 5/17/2011) 
  275. -- Note: This needs to be a more generalized solution, and input_tracker should  
  276. -- be embedded into VDO's with specific callback functions. 
  277. -- 
  278. function Vdo_input_tracker:highspeed_left_right(highspeed_on) 
  279. 	if self.highspeed_on ~= highspeed_on then 
  280. 		self.highspeed_on = highspeed_on 
  281. 		for i, info in pairs(self.input_infos) do 
  282. 			if info.handle ~= nil then 
  283. 				self:set_input_accelleration(info.handle, info.event_name)	 
  284. 			end 
  285. 		end 
  286. 	end 
  287. end 
  288.