./vdo_newsticker.lua

  1. ---------------------------------------------------------------------------  
  2. -- Newsticker! 
  3. --------------------------------------------------------------------------- 
  4. Current_ticker = -1 
  5. local Screen_width = 1280 
  6.  
  7. local NEWSTICKER_SEPERATOR_PADDING	= 10 
  8. local NEWSTICKER_TEXT_PADDING			= 20 
  9. local PIXELS_PER_SECOND					= 60 
  10.  
  11. local Ticker_data_thread = -1 
  12. local Text_clones = { } 
  13.  
  14. local NTS_OFFLINE_STRINGS				= 0 
  15. local NTS_ONLINE_STRINGS				= 1 
  16.  
  17. function vdo_newsticker_init() 
  18. end 
  19.  
  20. function vdo_newsticker_cleanup() 
  21. end 
  22.  
  23. -- Inherited from Vdo_base_object 
  24. Vdo_newsticker = Vdo_base_object:new_base() 
  25.  
  26. function Vdo_newsticker:init() 
  27. 	self.lines = { }	 
  28. 	self.num_lines = 0 
  29. 	 
  30. 	self.clones = {} 
  31. 	self.clone_count = 0 
  32. 	 
  33. 	--Animation that slide that scales all the objects in. 
  34. 	 
  35. 	self.anim_h = vint_object_find("scroll_text_anim", self.handle) 
  36. 	self.twn_h = vint_object_find("scroll_text_twn", self.handle) 
  37. 	vint_set_property(self.twn_h, "end_event", "vint_anim_loop_callback") 
  38.  
  39. 	self.current_line = 0 
  40. 	Current_ticker = self 
  41. 	 
  42. 	Ticker_data_thread = thread_new("newsticker_data_request_thread") 
  43. end 
  44.  
  45. function Vdo_newsticker:on_destroy() 
  46. 	if Current_ticker == self then 
  47. 		Current_ticker = -1 
  48. 	end 
  49. 	 
  50. 	if Ticker_data_thread ~= -1 then 
  51. 		thread_kill(Ticker_data_thread) 
  52. 		Ticker_data_thread = -1 
  53. 	end 
  54. end 
  55.  
  56. function Vdo_newsticker:populate_strings(text_line, online_string) 
  57. 	self.lines[self.num_lines] = text_line 
  58. 	self.num_lines = self.num_lines + 1 
  59. end 
  60.  
  61. function Vdo_newsticker:build() 
  62. 	--remove all clones from before... 
  63. 	self:clones_destroy() 
  64.  
  65. 	local text_h = vint_object_find("text", self.handle) 
  66. 	local seperator_h = vint_object_find("seperator", self.handle) 
  67. 	local text_clone_h, seperator_clone_h 
  68. 	local text_x, text_y = vint_get_property(text_h, "anchor") 
  69. 	local seperator_x, seperator_y = vint_get_property(seperator_h, "anchor") 
  70.  
  71. 	local text_width, text_height 	--leave undefined... need to check string length... 
  72. 	local seperator_width, seperator_height = element_get_actual_size(seperator_h) 
  73. 	 
  74. 	local next_x = 0 
  75. 	 
  76. 	for i = 0, self.num_lines -1 do 
  77. 		local is_clone = false 
  78. 		if i == 0 then 
  79. 			text_clone_h		= text_h 
  80. 			seperator_clone_h = seperator_h 
  81. 		else 
  82. 			text_clone_h = vint_object_clone(text_h) 
  83. 			seperator_clone_h = vint_object_clone(seperator_h) 
  84. 			is_clone = false 
  85. 			self:clone_store(text_clone_h) 
  86. 			self:clone_store(seperator_clone_h) 
  87. 		end 
  88. 		 
  89. 		--Set text tag... 
  90. 		vint_set_property(text_clone_h, "text_tag", self.lines[i]) 
  91. 		 
  92. 		--Set positions... 
  93. 		seperator_x = next_x  
  94. 		text_x = seperator_x + seperator_width + NEWSTICKER_SEPERATOR_PADDING 
  95. 		vint_set_property(seperator_clone_h, "anchor", seperator_x , seperator_y)  
  96. 		vint_set_property(text_clone_h, "anchor", text_x, text_y) 
  97. 				 
  98. 		--Get width and height... to calculate for next ticker message... 
  99. 		text_width, text_height = element_get_actual_size(text_clone_h) 
  100. 		next_x = text_x + text_width + NEWSTICKER_TEXT_PADDING 
  101. 	end 
  102. 	 
  103. 	local twn_h = vint_object_find("scroll_text_twn", self.handle) 
  104. 	vint_set_property(twn_h, "duration", (Screen_width+next_x)/PIXELS_PER_SECOND) 
  105. 	vint_set_property(twn_h, "start_value", Screen_width, 0) 
  106. 	vint_set_property(twn_h, "end_value", -next_x, 0) 
  107. 	 
  108. 	lua_play_anim(self.anim_h) 
  109. 	 
  110. 	--width... 
  111. 	self.width = next_x 
  112. end 
  113.  
  114. function Vdo_newsticker:clone_store(clone_h) 
  115. 	self.clones[self.clone_count] = clone_h 
  116. 	self.clone_count = self.clone_count + 1 
  117. end 
  118.  
  119. function Vdo_newsticker:clones_destroy() 
  120. 	for i = 0, self.clone_count - 1 do  
  121. 		vint_object_destroy(self.clones[i]) 
  122. 	end 
  123. 	self.clone_count = 0 
  124. 	self.clones = {} 
  125. end 
  126.  
  127. ------------------------------------------------------------------------------- 
  128. -- news ticker utility functions... 
  129. ------------------------------------------------------------------------------- 
  130.  
  131. function newsticker_data_request_thread() 
  132. 	vint_dataresponder_request("newsticker_populate", "newsticker_populate", 0, NTS_OFFLINE_STRINGS) 
  133. 	Current_ticker:build() 
  134. 	 
  135. 	Current_ticker.num_lines = 0 
  136. 	vint_dataresponder_request("newsticker_populate", "newsticker_populate", 0, NTS_ONLINE_STRINGS) 
  137. 	Current_ticker:build() 
  138. 	 
  139. 	Ticker_data_thread = -1 
  140. end 
  141.  
  142. function newsticker_populate(text_line, online) 
  143. 	if Current_ticker == -1 then 
  144. 		return 
  145. 	end 
  146. 		 
  147. 	Current_ticker:populate_strings(text_line, online) 
  148. end