./vdo_scrollbar.lua

  1. local SCROLLBAR_TAB_MIN_SIZE = 16 
  2.  
  3. local Start_mouse_y = nil 
  4. local Start_tab_y = nil 
  5.  
  6. local First_time = false 
  7.  
  8. function vdo_scrollbar_init() 
  9. end 
  10.  
  11. function vdo_scrollbar_cleanup() 
  12. end 
  13.  
  14. -- Inherited from Vdo_base_object 
  15. Vdo_scrollbar = Vdo_base_object:new_base() 
  16.  
  17. function Vdo_scrollbar:init() 
  18. 	-- Retarget the animations 
  19. 	self.tab_grp = Vdo_base_object:new("scrollbar_tab_grp", self.handle, self.doc_handle) 
  20. 	self.tab_grp_h = vint_object_find("scrollbar_tab_grp", self.handle, self.doc_handle) 
  21. 	 
  22. 	self.tab = Vdo_base_object:new("scrollbar_tab", self.handle, self.doc_handle) 
  23. 	self.tab_left = Vdo_base_object:new("scrollbar_left", self.handle, self.doc_handle) 
  24. 	self.tab_right = Vdo_base_object:new("scrollbar_right", self.handle, self.doc_handle) 
  25. 	self.tab_bottom = Vdo_base_object:new("scrollbar_bottom", self.handle, self.doc_handle) 
  26. 	self.fill = Vdo_base_object:new("scrollbar_fill", self.handle, self.doc_handle) 
  27. 	self.tab_anim = Vdo_anim_object:new("scrollbar_tab_anim", self.handle, self.doc_handle) 
  28. 	self.tab_anim:unpause() 
  29. 	self.tab_anim:set_target_handle(self.handle) 
  30.  
  31. 	self.tab_grp:set_property("anchor",0, 0) 
  32. 	self.fill:set_property("anchor",0, 0) 
  33. 	First_time = true 
  34. end 
  35.  
  36. function Vdo_scrollbar:set_size(width, height, total_height) 
  37. 	if First_time then -- This solves a weird bug where sometimes the tab doesn't start at 0 
  38. 		self.tab_grp:set_property("anchor",0, 0) 
  39. 		self.fill:set_property("anchor",0, 0) 
  40. 		First_time = false 
  41. 	end 
  42.  
  43. 	self.fill:set_actual_size(width, height) 
  44. 	 
  45. 	if game_get_platform() == "PC" and (total_height ~= nil and total_height ~= 0) then 
  46. 		local new_tab_height = ( height / total_height ) * height 
  47. 		self.tab:set_actual_size(width, new_tab_height) 
  48. 		self.tab_left:set_actual_size( 2.0, new_tab_height ) 
  49. 		self.tab_right:set_actual_size( 2.0, new_tab_height ) 
  50. 		self.tab_bottom:set_anchor( 0, new_tab_height - 2 ) 
  51. 	end 
  52. end 
  53.  
  54. -- Control the scrollbar height and tab position 
  55. -- max_index: maximum index of menu choices (ie. how many choices) 
  56. -- current_index: the index of the currently highlighted choice 
  57. -- instant_set: true if it should skip the animation and just set the tab instantly 
  58. -- 
  59. function Vdo_scrollbar:set_value(max_index, current_index, instant_set) 
  60.  
  61. 	local tab_x, tab_y = self.tab_grp:get_property("anchor") 
  62. 	 
  63. 	local fill_width, fill_height = self.fill:get_actual_size() 
  64. 	 
  65. 	if max_index <= 1 then 
  66. 		return 
  67. 	end 
  68. 	 
  69. 	local tab_height = 0 
  70. 	if game_get_platform() == "PC" then 
  71. 		local tab_size_width, tab_size_height = self.tab:get_actual_size() 
  72. 		tab_height = tab_size_height 
  73. 	else 
  74. 		tab_height = fill_height / (max_index * 0.2) 
  75. 		 
  76. 		--Duplicate code in here because I don't know how the PC stuff works differently...(JMH 5/4/2011) 
  77. 		if tab_height < SCROLLBAR_TAB_MIN_SIZE then 
  78. 			tab_height = SCROLLBAR_TAB_MIN_SIZE 
  79. 		end 
  80. 		self.tab:set_actual_size(fill_width, tab_height) 
  81. 		self.tab_left:set_actual_size( 2.0, tab_height ) 
  82. 		self.tab_right:set_actual_size( 2.0, tab_height ) 
  83. 		self.tab_bottom:set_anchor( 0, tab_height - 2 ) 
  84. 	end 
  85. 	 
  86. 	if tab_height < SCROLLBAR_TAB_MIN_SIZE then 
  87. 		tab_height = SCROLLBAR_TAB_MIN_SIZE 
  88. 	end 
  89. 	 
  90. 	local percent_done = (current_index - 1)/ (max_index - 1) 
  91. 	local tab_position = (percent_done * (fill_height - tab_height)) 
  92. 		 
  93. 	if instant_set ~= true then 
  94. 		local tab_anchor_tween = Vdo_tween_object:new("scrollbar_tab_anchor_tween", self.handle, self.doc_handle) 
  95. 		tab_anchor_tween:set_property("start_value", 0, tab_y) 
  96. 		tab_anchor_tween:set_property("end_value", 0, tab_position) 
  97. 		 
  98. 		local tab_anim = Vdo_anim_object:new("scrollbar_tab_anim", self.handle, self.doc_handle) 
  99. 		tab_anim:play() 
  100. 	else 
  101. 		--local scrollbar_tab = Vdo_base_object:new("scrollbar_tab", self.handle, self.doc_handle) 
  102. 		self.tab_grp:set_anchor(0, tab_position) 
  103. 	end 
  104. end 
  105.  
  106. function Vdo_scrollbar:enable(enabled) 
  107. end 
  108.  
  109. function Vdo_scrollbar:set_highlight_color(color) 
  110. 	self.tab_grp:set_color(color.R,color.G,color.B) 
  111. end 
  112.  
  113.  
  114. -- ===================================== 
  115. --       Mouse Specific Functions 
  116. -- ===================================== 
  117.  
  118. -- Sets the scrolltab's position and calculates the index based on the position 
  119. -- 
  120. function Vdo_scrollbar:drag_scrolltab(mouse_y, max_index) 
  121. 	--local tab_anim = Vdo_anim_object:new("scrollbar_tab_anim", self.handle) 
  122. 	--tab_anim:stop() 
  123. 		 
  124. 	if Start_mouse_y == nil then 
  125. 		Start_mouse_y = mouse_y 
  126. 	end 
  127. 	local diff = mouse_y - Start_mouse_y 
  128. 	 
  129. 	--get the fill width and height 
  130. 	local fill_width, fill_height = self.fill:get_actual_size() 
  131.  
  132. 	--get the tab width and height 
  133. 	local tab_width, tab_height = self.tab:get_actual_size() 
  134. 	 
  135. 	--get the tab x and y 
  136. 	local tab_x, tab_y = self.tab_grp:get_anchor() 
  137. 	if Start_tab_y == nil then 
  138. 		Start_tab_y = tab_y 
  139. 	end 
  140. 	 
  141. 	local max_tab_pos = fill_height - tab_height 
  142. 	 
  143. 	--Don't have the scroll tab go past the top or bottom of the fill 
  144. 	local tab_position = Start_tab_y + diff 
  145. 	if tab_position > max_tab_pos then 
  146. 		tab_position = max_tab_pos 
  147. 	elseif tab_position < 0 then 
  148. 		tab_position = 0 
  149. 	end 
  150. 	 
  151. 	--Find the visible start index for the list that is associated with the tab position 
  152. 	local scroll_pct = tab_position / max_tab_pos 
  153. 	local start_index = round(scroll_pct * (max_index - 1)) + 1 
  154.  
  155. 	self.tab_grp:set_anchor(tab_x, tab_position) 
  156. 	 
  157. 	return start_index 
  158. end 
  159.  
  160. -- Reset the internal tracking values and snap the tab to a valid position 
  161. -- 
  162. function Vdo_scrollbar:release_scrolltab(start_index, max_index) 
  163. 	Start_mouse_y = nil 
  164. 	Start_tab_y = nil 
  165. 	 
  166. 	--Have the scroll tab snap to the closest visible start index position 
  167. 	self:set_value(max_index, start_index, false) 
  168. end 
  169.  
  170. -- Initialize mouse inputs for the scroll tab 
  171. -- 
  172. function Vdo_scrollbar:add_mouse_inputs(func_prefix, input_tracker, priority) 
  173. 	priority = priority or 50 
  174.  
  175. 	-- Mouse click and mouse move are needed for correct mouse event processing 
  176. 	input_tracker:add_mouse_input("mouse_click", func_prefix.."_mouse_click", priority, self.tab_grp.handle) 
  177. 	input_tracker:add_mouse_input("mouse_move", func_prefix.."_mouse_move", priority, self.tab_grp.handle) 
  178. 	input_tracker:add_mouse_input("mouse_drag", func_prefix.."_mouse_drag", priority, self.tab_grp.handle) 
  179. 	input_tracker:add_mouse_input("mouse_drag_release", func_prefix.."_mouse_drag_release", priority, self.tab_grp.handle) 
  180. end 
  181.