./vdo_input_tracker.lua

  1. -- Object to simplify turning on/off input event subscriptions 
  2. Vdo_input_tracker = {} 
  3.  
  4. VDO_INPUT_TRACKER_DELAY_SECONDS				= 0.6		-- Base time in seconds to delay before sending next inpt event 
  5. VDO_INPUT_TRACKER_ACCELERATION_FACTOR		= 1.2		-- How quickly input velocity compounds over time when holding the input button 
  6. VDO_INPUT_TRACKER_MAX_ACCELERATION			= 10		-- Maximum accleration.  The smallest delay time will be VDO_INPUT_TRACKER_DELAY_SECONDS * VDO_INPUT_TRACKER_MAX_ACCELERATION. 
  7.  
  8. VDO_INPUT_SLIDER_ACCEL_REPEAT =	0.4 
  9. VDO_INPUT_SLIDER_ACCEL_FACTOR =	30 
  10. VDO_INPUT_SLIDER_ACCEL_LIMIT =	1000 
  11.  
  12. -- Creates a new vdo input event tracker 
  13. -- 
  14. function Vdo_input_tracker:new() 
  15. 	local obj = { 
  16. 		input_infos = {}, 
  17. 	} 
  18.  
  19. 	setmetatable(obj, self) 
  20. 	self.__index = self 
  21. 	self.highspeed_on = -1 
  22. 	return obj 
  23. end 
  24.  
  25. -- Adds an input event to use (Note that it doesn't actually subscribe the input) 
  26. -- is_raw is optional 
  27. -- 
  28. function Vdo_input_tracker:add_input(input_name, func_name, priority, is_raw, scancode) 
  29. 	-- Make a new entry after any current entries 
  30. 	local index = #self.input_infos + 1 
  31. 	self.input_infos[index] = {} 
  32. 	local info = self.input_infos[index] 
  33.  
  34. 	info.input_name = input_name 
  35. 	info.func_name = func_name 
  36. 	info.priority = priority 
  37. 	info.is_raw = is_raw or false 
  38. 	 
  39. 	if game_get_platform() == "PC" then 
  40. 		info.scancode = scancode or -1 
  41. 	end 
  42. end 
  43.  
  44. -- Adds a mouse input event to use (Note that it doesn't actually subscribe the input) 
  45. -- 
  46. function Vdo_input_tracker:add_mouse_input(input_name, func_name, priority, target_handle) 
  47. 	-- Make a new entry after any current entries 
  48. 	local index = #self.input_infos + 1 
  49. 	self.input_infos[index] = {} 
  50. 	local info = self.input_infos[index] 
  51.  
  52. 	info.input_name = input_name 
  53. 	info.func_name = func_name 
  54. 	info.priority = priority 
  55. 	info.target_handle = target_handle 
  56. 	info.is_raw = false 
  57. 	info.is_mouse = true 
  58. end 
  59.  
  60. -- Removes an input event and unsubscribes to it if necessary 
  61. -- 
  62. function Vdo_input_tracker:remove_input(input_name) 
  63. 	for i, info in pairs(self.input_infos) do 
  64. 		if info.input_name == input_name then 
  65. 			 
  66. 			if info.handle ~= nil then 
  67. 				if info.is_raw then 
  68. 					vint_unsubscribe_to_raw_input(info.handle) 
  69. 				elseif info.is_mouse then 
  70. 					vint_unsubscribe_to_mouse_input(info.handle) 
  71. 				else 
  72. 					vint_unsubscribe_to_input_event(info.handle) 
  73. 				end 
  74. 			end 
  75. 			 
  76. 			self.input_infos[i] = nil	 
  77. 			 
  78. 			return 
  79. 		end 
  80. 	end 
  81. end 
  82.  
  83. -- Removes all of the input events and unsubscribes to them if necessary 
  84. -- 
  85. function Vdo_input_tracker:remove_all(dont_force_mouse_move) 
  86. 	for i, info in pairs(self.input_infos) do 
  87. 		if info.handle ~= nil then 
  88. 			if info.is_raw then 
  89. 				vint_unsubscribe_to_raw_input(info.handle) 
  90. 			elseif info.is_mouse then 
  91. 				vint_unsubscribe_to_mouse_input(info.handle) 
  92. 				 
  93. 				if dont_force_mouse_move ~= true and game_is_active_input_gamepad() == false then 
  94. 					--vint_force_mouse_move_event() 
  95. 				end 
  96. 			else 
  97. 				vint_unsubscribe_to_input_event(info.handle) 
  98. 			end 
  99. 		end 
  100. 		 
  101. 		self.input_infos[i] = nil 
  102. 	end 
  103. end 
  104.  
  105. -- Subscribes/unsubscribes all added input events 
  106. -- 
  107. function Vdo_input_tracker:subscribe(enable, dont_force_mouse_move) 
  108.  
  109. 	local mouse_move_event_changed = false 
  110.  
  111. 	-- Just in case, always unsubscribe any existing input_infos (should be empty if we properly unsubscribed before subscribing) 
  112. 	for i, info in pairs(self.input_infos) do 
  113. 		if info.handle ~= nil then 
  114. 			if info.is_raw then 
  115. 				vint_unsubscribe_to_raw_input(info.handle) 
  116. 			elseif info.is_mouse then 
  117. 				vint_unsubscribe_to_mouse_input(info.handle) 
  118. 				mouse_move_event_changed = true 
  119. 			else 
  120. 				vint_unsubscribe_to_input_event(info.handle) 
  121. 			end 
  122. 			info.handle = nil 
  123. 		end 
  124. 	end 
  125. 	 
  126. 	if enable then 
  127. 		for i, info in pairs(self.input_infos) do 
  128. 			if info.is_raw then 
  129. 				info.handle = vint_subscribe_to_raw_input(info.input_name, info.func_name, info.priority) 
  130. 			elseif info.is_mouse then 
  131. 				info.handle = vint_subscribe_to_mouse_input(info.input_name, info.func_name, info.priority, info.target_handle) 
  132. 				mouse_move_event_changed = true 
  133. 			else 
  134. 				local scancode = info.scancode or -1 
  135. 				info.handle = vint_subscribe_to_input_event(info.input_name, info.func_name, info.priority, scancode) 
  136. 				if info.handle ~= nil then 
  137. 					self:set_input_accelleration(info.handle, info.input_name) 
  138. 				end 
  139. 			end 
  140. 		end 
  141. 	end 
  142. 	 
  143. 	if mouse_move_event_changed and dont_force_mouse_move ~= true and game_is_active_input_gamepad() == false then 
  144. 		--vint_force_mouse_move_event() 
  145. 	end 
  146. end 
  147.  
  148. ------------------------------------------------------------------------------- 
  149. -- 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) 
  150. -- NOTE: This should be generalized per input subscription and handled directly on each item type in a megalist... 
  151. -- 
  152. function Vdo_input_tracker:set_input_accelleration(handle, input_name) 
  153. 	local delay 		= VDO_INPUT_TRACKER_DELAY_SECONDS 
  154. 	local factor_acc	= VDO_INPUT_TRACKER_ACCELERATION_FACTOR 
  155. 	local max_acc 		= VDO_INPUT_TRACKER_MAX_ACCELERATION 
  156. 	 
  157. 	if self.highspeed_on == true then 
  158. 		if input_name == "nav_left" or input_name == "nav_right" then 
  159. 			delay 		= VDO_INPUT_SLIDER_ACCEL_REPEAT 
  160. 			factor_acc	= VDO_INPUT_SLIDER_ACCEL_FACTOR 
  161. 			max_acc 		= VDO_INPUT_SLIDER_ACCEL_LIMIT 
  162. 		end 
  163. 	end 
  164. 	vint_set_input_params(handle, delay, factor_acc, max_acc, false, true) 
  165. end 
  166.  
  167. ------------------------------------------------------------------------------- 
  168. -- Toggles highspeed for left and right inputs. (JMH 5/17/2011) 
  169. -- Note: This needs to be a more generalized solution, and input_tracker should  
  170. -- be embedded into VDO's with specific callback functions. 
  171. -- 
  172. function Vdo_input_tracker:highspeed_left_right(highspeed_on) 
  173. 	if self.highspeed_on ~= highspeed_on then 
  174. 		self.highspeed_on = highspeed_on 
  175. 		for i, info in pairs(self.input_infos) do 
  176. 			if info.handle ~= nil then 
  177. 				self:set_input_accelleration(info.handle, info.input_name)	 
  178. 			end 
  179. 		end 
  180. 	end 
  181. end 
  182.