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