./vdo_gsi_timer.lua

  1. ------------------------------------------------------------------------------- 
  2. --GSI Timer Component 
  3. ------------------------------------------------------------------------------- 
  4. -- Inherited from Vdo_base_object 
  5. Vdo_gsi_timer = Vdo_base_object:new_base() 
  6.  
  7. --Timer colors... 
  8. TIMER_COLOR_NORMAL_ID 	= 0 
  9. TIMER_COLOR_ALARM_ID 	= 1 
  10. TIMER_COLOR_GOOD_ID 	= 2 
  11.  
  12. local Timer_colors = { 
  13. 	[TIMER_COLOR_NORMAL_ID] = {R=226/255,G=191/255,B=11/255},		--YELLOW 
  14. 	[TIMER_COLOR_ALARM_ID] = {R=255/255,G=0/255,B=0/255},				--RED 
  15. 	[TIMER_COLOR_GOOD_ID] = {R=113/255, G=175/255, B=21/255}			--GREEN 
  16. } 
  17.  
  18. local GSI_TIMER_HEIGHT = 40 
  19.  
  20. -- Standard Init Function 
  21. function vdo_gsi_timer_init() 
  22. end 
  23.  
  24. -- Standard Cleanup Function 
  25. function vdo_gsi_timer_cleanup() 
  26. end 
  27.  
  28. function Vdo_gsi_timer:init() 
  29. end 
  30.  
  31. function Vdo_gsi_timer:create(skin) 
  32. 	--set skin default 
  33. 	if skin ~= "positive" and skin ~= "negative" then 
  34. 		skin = "negative" 
  35. 	end 
  36. 	 
  37. 	if skin == "running_man" then 
  38. 		skin = "negative" 
  39. 		self.running_man_mode = true 
  40. 	end 
  41. 	 
  42. 	--Set default time... 
  43. 	local timer_txt_h = vint_object_find("timer_txt", self.handle) 
  44. 	local seconds_txt_h = vint_object_find("seconds_txt", self.handle) 
  45. 	local colon_txt_h = vint_object_find("colon_txt", self.handle) 
  46. 	 
  47. 	vint_set_property(seconds_txt_h, "visible", false) 
  48. 	vint_set_property(colon_txt_h, "visible", false) 
  49.  
  50. 	local default_time = "00:00" 
  51. 	 
  52. 	--Retarget Animations... 
  53. 	local seconds_flash_anim_h = vint_object_find("seconds_flash_anim", self.handle) 
  54. 	vint_set_property(seconds_flash_anim_h, "target_handle", self.handle) 
  55. 	 
  56. 	local color_shift_anim_h = vint_object_find("color_shift_anim", self.handle) 
  57. 	local color_shift_twn_h = vint_object_find("color_shift_twn", color_shift_anim_h) 
  58. 	vint_set_property(color_shift_twn_h, "target_handle", self.handle)	-- Retarget tween to base object... this changes the color... 
  59.  
  60. 	local timer_slam_anim_h = vint_object_find("timer_slam_anim", self.handle) 
  61. 	vint_set_property(timer_slam_anim_h, "target_handle", self.handle) 
  62. 		 
  63. 	--Get width/height of fullsize 
  64. 	vint_set_property(timer_txt_h, "text_tag", default_time) 
  65. 	local width, height = element_get_actual_size(timer_txt_h) 
  66. 	 
  67. 	--Initialize Standard Indicator Values 
  68. 	self.timer_txt_h = timer_txt_h 
  69. 	self.seconds_txt_h = seconds_txt_h 
  70. 	self.colon_txt_h = colon_txt_h 
  71. 	self.seconds_flash_anim_h = seconds_flash_anim_h 
  72. 	self.color_shift_anim_h = color_shift_anim_h 
  73. 	self.color_shift_twn_h = color_shift_twn_h 
  74. 	self.timer_slam_anim_h = timer_slam_anim_h 
  75. 	self.visible = -1						--Is the indicator displaying?  
  76. 	self.is_dirty = true					--Is the indicator dirty? we set this to true if we want the GSI to re-align everything. 
  77. 	self.skin = skin 
  78. 	self.width = width 
  79. 	self.height = GSI_TIMER_HEIGHT 
  80. 	 
  81. 	--force color of timer to normal 
  82. 	self:color_shift(TIMER_COLOR_NORMAL_ID, true) 
  83. 	 
  84. 	--Initialize Custom Values  
  85. 	self.timer_value = 0 
  86. 	self.label_crc = 0 
  87. end 
  88.  
  89. function Vdo_gsi_timer:update(visible, skin, label_crc, timer_value, is_countdown) 
  90. 	--[[ 
  91. 	debug_print("vint", 	"visible" .. var_to_string(visible) .. "\n") 
  92. 	debug_print("vint", 	"skin" .. var_to_string(skin) .. "\n") 
  93. 	debug_print("vint", 	"label_crc" .. var_to_string(label_crc) .. "\n") 
  94. 	debug_print("vint", 	"timer_value" .. var_to_string(timer_value) .. "\n") 
  95. 	debug_print("vint", 	"is_positive" .. var_to_string(is_positive) .. "\n") 
  96. 	debug_print("vint", 	"is_flashing" .. var_to_string(is_flashing) .. "\n\n") 
  97. 	]] 
  98. 	local force_color = false		--Determines if we are going to force the color to the current timer value... 
  99. 	 
  100. 	--label_crc is no longer supported by this... 
  101. 	--Set visible 
  102. 	self:set_visible(visible) 
  103. 	 
  104. 	--Reference objects 
  105. 	local timer_txt_h 	= self.timer_txt_h 
  106. 	local seconds_txt_h	= self.seconds_txt_h 
  107. 	local colon_txt_h 	= self.colon_txt_h 
  108. 	 
  109. 	--Error Check for timer value 
  110. 	if timer_value == nil then 
  111. 		timer_value = 0 
  112. 	end 
  113. 	 
  114. 	---Split up the timer value(seconds) into... 
  115. 	local minutes = floor(timer_value / 60) 
  116. 	local seconds = abs(timer_value % 60) 
  117. 	 
  118. 	--Pad the seconds for the timer 
  119. 	if seconds < 10 then 
  120. 		seconds = "0" .. seconds 
  121. 	end 
  122. 	 
  123. 	--Timer has reset... 
  124. 	if timer_value >= self.timer_value then 
  125. 		--Pause any animations that modify timer visuals... 
  126. 		vint_set_property(self.seconds_flash_anim_h, "is_paused", true) 
  127. 		vint_set_property(self.timer_slam_anim_h, "is_paused", true) 
  128. 		 
  129. 		--we've reset the timer, reset scales and alpha which could be modified by animations... 
  130. 		vint_set_property(seconds_txt_h, "scale", 1.0, 1.0) 
  131. 		vint_set_property(timer_txt_h, "scale", 1.0, 1.0) 
  132. 		 
  133. 		vint_set_property(seconds_txt_h, "alpha", 1) 
  134. 		vint_set_property(timer_txt_h, "alpha", 1) 
  135. 		 
  136. 		force_color = true	--immediatly change colors... 
  137. 	end 
  138. 	 
  139. 	local width = 0  
  140. 	 
  141. 	if timer_value < 60 then 
  142. 		--Show only colon and seconds... 
  143. 		vint_set_property(timer_txt_h, "visible", false) 
  144. 		vint_set_property(seconds_txt_h, "visible", true) 
  145. 		vint_set_property(colon_txt_h, "visible", true) 
  146. 		vint_set_property(seconds_txt_h, "text_tag", seconds)		 
  147. 		 
  148. 		--calculate width of timer... 
  149. 		local timer_x, timer_y = vint_get_property(seconds_txt_h, "anchor") 
  150. 		-- set scale so we get accurate size... 
  151. 		vint_set_property(seconds_txt_h, "scale", 1.0, 1.0) 
  152. 		local timer_width, timer_height = element_get_actual_size(seconds_txt_h) 
  153. 		width = floor(timer_x + (timer_width * 0.5) + GSI_TEXT_SPACING) 
  154. 	else 
  155. 		--Show only standard timer... 
  156. 		vint_set_property(timer_txt_h, "visible", true) 
  157. 		vint_set_property(seconds_txt_h, "visible", false) 
  158. 		vint_set_property(colon_txt_h, "visible", false) 
  159. 		 
  160. 		local time_formatted = minutes .. ":" .. seconds 
  161. 		vint_set_property(timer_txt_h, "text_tag", time_formatted) 
  162. 		 
  163. 		--calculate width of timer... 
  164. 		local timer_x, timer_y = vint_get_property(timer_txt_h, "anchor") 
  165. 		local timer_width, timer_height = element_get_actual_size(timer_txt_h) 
  166. 		local timer_half_width = (timer_width * 0.5) 
  167. 		timer_x = 31 + timer_half_width 
  168. 		vint_set_property(timer_txt_h, "anchor", timer_x, timer_y) 
  169. 		width = floor(timer_x + timer_half_width + GSI_TEXT_SPACING) 
  170. 	end 
  171. 	 
  172. 	if is_countdown == true then 
  173. 		--If we are under 10 seconds... then we should shift to red. 
  174. 		if timer_value <= 10 then 
  175. 			if (mission_is_active("m05") == true) or (mission_is_active("m22_2") == true) then 
  176. 				-- Shift color to green 
  177. 				self:color_shift(TIMER_COLOR_GOOD_ID, force_color) 
  178. 			else 
  179. 				--Shift Color to red... 
  180. 				self:color_shift(TIMER_COLOR_ALARM_ID, force_color) 
  181. 			end 
  182. 		else 
  183. 			--Modify Color to default state... 
  184. 			self:color_shift(TIMER_COLOR_NORMAL_ID, force_color) 
  185. 		end 
  186. 		 
  187. 		--do flashes if we are under 5 seconds... 
  188. 		if timer_value <= 5 and (mission_is_active("m05") ~= true) then 
  189. 			--Flash if we at 5 or under seconds 
  190. 			lua_play_anim(self.seconds_flash_anim_h) 
  191. 		end 
  192. 		 
  193. 		--Slam if we are at 30, 20 or <= 10 seconds... 
  194. 		if (mission_is_active("m05") ~= true) then 
  195. 			if timer_value == 30 or timer_value == 20 or timer_value <= 10 then 
  196. 				if self.running_man_mode == true and timer_value <= 10 then 
  197. 				--This is a special case fix to change running man, so we don't slam at 10 or under...  
  198. 				-- if we wanted it to work normally, we would just take out the if statement and just play the anim... (JMH 6/23/2011) 
  199. 				else 
  200. 					lua_play_anim(self.timer_slam_anim_h) 
  201. 				end 
  202. 			end 
  203. 		end 
  204. 	end 
  205. 	if self.width ~= width then 
  206. 		self.is_dirty = true 
  207. 	end 
  208.  
  209. 	--Store new width and height of indicator 
  210. 	self.width = width  
  211. 	self.height = GSI_TIMER_HEIGHT  
  212. 	self.timer_value = timer_value 
  213. end 
  214.  
  215. function Vdo_gsi_timer:get_size() 
  216. 	return self.width, self.height 
  217. end 
  218.  
  219. ------------------------------------------------------------------------------- 
  220. -- Changes color to the specified timer state 
  221. -- @param	color_id		state of timer... ( TIMER_COLOR_NORMAL_ID, TIMER_COLOR_ALARM_ID,...) 
  222. -- @param	force_color	if we don't want a transition... 
  223. ------------------------------------------------------------------------------- 
  224. function Vdo_gsi_timer:color_shift(color_id, force_color) 
  225.  
  226. 	if color_id == self.color_id then 
  227. 		--don't change color... 
  228. 		return 
  229. 	end 
  230. 	local twn_h = self.color_shift_twn_h 
  231. 	 
  232. 		--If we force the color set our start time in the past.... 
  233. 	local start_color = {} 
  234. 	start_color.R, start_color.G, start_color.B = vint_get_property(twn_h, "end_value") 
  235. 	 
  236. 	-- Advance time of animation if we are going to foce the color... 
  237. 	local start_time = 0 
  238. 	if force_color then 
  239. 		start_color = Timer_colors[color_id] 
  240. 		start_time = -2 
  241. 	end 
  242. 	 
  243. 	local end_color = Timer_colors[color_id] 
  244. 	 
  245. 	if start_color == nil or end_color == nil then 
  246. 		debug_print("vint", "invalid color id sent to Vdo_gsi_timer:color_shift()\n") 
  247. 		return 
  248. 	end 
  249. 	 
  250. 	-- Override colors 
  251. 	vint_set_property(twn_h, "start_value", start_color.R, start_color.G, start_color.B) 
  252. 	vint_set_property(twn_h, "end_value", end_color.R, end_color.G, end_color.B) 
  253. 	lua_play_anim(self.color_shift_anim_h, start_time) 
  254. 	 
  255. 	self.color_id = color_id 
  256.  
  257. end 
  258.  
  259. --Standard Indicator Functions 
  260. function Vdo_gsi_timer:set_visible(visible) 
  261. 	--Hide the indicator if it is not visible 
  262. 	if visible ~= self.visible then 
  263. 		if visible == true then 
  264. 			self:set_property("visible", true) 
  265. 			self.visible = visible 
  266. 		else 
  267. 			self:set_property("visible", false) 
  268. 			self.visible = visible 
  269. 		end 
  270. 		 
  271. 		--Format of the indicators changed, so lets make sure we set the flag to dirty. 
  272. 		--The vdo_gsi will take care of everything when we set this dirty flag to true... 
  273. 		self.is_dirty = true 
  274. 	end 
  275. end