local SCROLLBAR_TAB_MIN_SIZE = 16
local Start_mouse_y = nil
local Start_tab_y = nil
local First_time = false
function vdo_scrollbar_init()
end
function vdo_scrollbar_cleanup()
end
-- Inherited from Vdo_base_object
Vdo_scrollbar = Vdo_base_object:new_base()
function Vdo_scrollbar:init()
-- Retarget the animations
self.tab_grp = Vdo_base_object:new("scrollbar_tab_grp", self.handle, self.doc_handle)
self.tab_grp_h = vint_object_find("scrollbar_tab_grp", self.handle, self.doc_handle)
self.tab = Vdo_base_object:new("scrollbar_tab", self.handle, self.doc_handle)
self.tab_left = Vdo_base_object:new("scrollbar_left", self.handle, self.doc_handle)
self.tab_right = Vdo_base_object:new("scrollbar_right", self.handle, self.doc_handle)
self.tab_bottom = Vdo_base_object:new("scrollbar_bottom", self.handle, self.doc_handle)
self.fill = Vdo_base_object:new("scrollbar_fill", self.handle, self.doc_handle)
self.tab_anim = Vdo_anim_object:new("scrollbar_tab_anim", self.handle, self.doc_handle)
self.tab_anim:unpause()
self.tab_anim:set_target_handle(self.handle)
self.tab_grp:set_property("anchor",0, 0)
self.fill:set_property("anchor",0, 0)
First_time = true
end
function Vdo_scrollbar:set_size(width, height, total_height)
if First_time then -- This solves a weird bug where sometimes the tab doesn't start at 0
self.tab_grp:set_property("anchor",0, 0)
self.fill:set_property("anchor",0, 0)
First_time = false
end
self.fill:set_actual_size(width, height)
if game_get_platform() == "PC" and (total_height ~= nil and total_height ~= 0) then
local new_tab_height = ( height / total_height ) * height
self.tab:set_actual_size(width, new_tab_height)
self.tab_left:set_actual_size( 2.0, new_tab_height )
self.tab_right:set_actual_size( 2.0, new_tab_height )
self.tab_bottom:set_anchor( 0, new_tab_height - 2 )
end
end
-- Control the scrollbar height and tab position
-- max_index: maximum index of menu choices (ie. how many choices)
-- current_index: the index of the currently highlighted choice
-- instant_set: true if it should skip the animation and just set the tab instantly
--
function Vdo_scrollbar:set_value(max_index, current_index, instant_set)
local tab_x, tab_y = self.tab_grp:get_property("anchor")
local fill_width, fill_height = self.fill:get_actual_size()
if max_index <= 1 then
return
end
local tab_height = 0
if game_get_platform() == "PC" then
local tab_size_width, tab_size_height = self.tab:get_actual_size()
tab_height = tab_size_height
else
tab_height = fill_height / (max_index * 0.2)
--Duplicate code in here because I don't know how the PC stuff works differently...(JMH 5/4/2011)
if tab_height < SCROLLBAR_TAB_MIN_SIZE then
tab_height = SCROLLBAR_TAB_MIN_SIZE
end
self.tab:set_actual_size(fill_width, tab_height)
self.tab_left:set_actual_size( 2.0, tab_height )
self.tab_right:set_actual_size( 2.0, tab_height )
self.tab_bottom:set_anchor( 0, tab_height - 2 )
end
if tab_height < SCROLLBAR_TAB_MIN_SIZE then
tab_height = SCROLLBAR_TAB_MIN_SIZE
end
local percent_done = (current_index - 1)/ (max_index - 1)
local tab_position = (percent_done * (fill_height - tab_height))
if instant_set ~= true then
local tab_anchor_tween = Vdo_tween_object:new("scrollbar_tab_anchor_tween", self.handle, self.doc_handle)
tab_anchor_tween:set_property("start_value", 0, tab_y)
tab_anchor_tween:set_property("end_value", 0, tab_position)
local tab_anim = Vdo_anim_object:new("scrollbar_tab_anim", self.handle, self.doc_handle)
tab_anim:play()
else
--local scrollbar_tab = Vdo_base_object:new("scrollbar_tab", self.handle, self.doc_handle)
self.tab_grp:set_anchor(0, tab_position)
end
end
function Vdo_scrollbar:enable(enabled)
end
function Vdo_scrollbar:set_highlight_color(color)
self.tab_grp:set_color(color.R,color.G,color.B)
end
-- =====================================
-- Mouse Specific Functions
-- =====================================
-- Sets the scrolltab's position and calculates the index based on the position
--
function Vdo_scrollbar:drag_scrolltab(mouse_y, max_index)
--local tab_anim = Vdo_anim_object:new("scrollbar_tab_anim", self.handle)
--tab_anim:stop()
if Start_mouse_y == nil then
Start_mouse_y = mouse_y
end
local diff = mouse_y - Start_mouse_y
--get the fill width and height
local fill_width, fill_height = self.fill:get_actual_size()
--get the tab width and height
local tab_width, tab_height = self.tab:get_actual_size()
--get the tab x and y
local tab_x, tab_y = self.tab_grp:get_anchor()
if Start_tab_y == nil then
Start_tab_y = tab_y
end
local max_tab_pos = fill_height - tab_height
--Don't have the scroll tab go past the top or bottom of the fill
local tab_position = Start_tab_y + diff
if tab_position > max_tab_pos then
tab_position = max_tab_pos
elseif tab_position < 0 then
tab_position = 0
end
--Find the visible start index for the list that is associated with the tab position
local scroll_pct = tab_position / max_tab_pos
local start_index = round(scroll_pct * (max_index - 1)) + 1
self.tab_grp:set_anchor(tab_x, tab_position)
return start_index
end
-- Reset the internal tracking values and snap the tab to a valid position
--
function Vdo_scrollbar:release_scrolltab(start_index, max_index)
Start_mouse_y = nil
Start_tab_y = nil
--Have the scroll tab snap to the closest visible start index position
self:set_value(max_index, start_index, false)
end
-- Initialize mouse inputs for the scroll tab
--
function Vdo_scrollbar:add_mouse_inputs(func_prefix, input_tracker, priority)
priority = priority or 50
-- Mouse click and mouse move are needed for correct mouse event processing
input_tracker:add_mouse_input("mouse_click", func_prefix.."_mouse_click", priority, self.tab_grp.handle)
input_tracker:add_mouse_input("mouse_move", func_prefix.."_mouse_move", priority, self.tab_grp.handle)
input_tracker:add_mouse_input("mouse_drag", func_prefix.."_mouse_drag", priority, self.tab_grp.handle)
input_tracker:add_mouse_input("mouse_drag_release", func_prefix.."_mouse_drag_release", priority, self.tab_grp.handle)
end