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