Worm
02-28-2006, 01:39 PM
This is a spin-off of the Splitter Framework and uses the same conventions.
This framework will let you use objects as sliders. There are two object to each slider, the Slider Background (Named: vslider_YourObjectName) or hslider_YourObjectName), and the Knob (Named: vknob_YourObjectName) or hknob_YourObjectName).
There is a custom event that will return the percentage the knob has traveled across the background object. The returned percentage can then be used to set volumes, move objects, set opacity, scroll pictures etc...
How to use:
1. Add these function calls to the following Page Events
On Show Event
--Initialize the sliders
InitSliders();
On Mouse Button
DoSlider_MouseButton(e_Type, e_X, e_Y);
On Mouse Move
DoSlider_MouseMove(e_X, e_Y);
2. Copy this code to your Global Functions
-- Custom event
function On_Slider_Move(sCurrentSlider, nPercent)
-- use this event to make change the values the slider
-- is related to
end
--[[************************************************** ******]]--
--[[ Set up a table to reference the objects in AMS ]]--
--[[************************************************** ******]]--
tbObjectType = {}
tbObjectType[OBJECT_BUTTON] = Button;
tbObjectType[OBJECT_LABEL] = Label;
tbObjectType[OBJECT_PARAGRAPH] = Paragraph;
tbObjectType[OBJECT_IMAGE] = Image;
tbObjectType[OBJECT_FLASH] = Flash;
tbObjectType[OBJECT_VIDEO] = Video;
tbObjectType[OBJECT_WEB] = Web;
tbObjectType[OBJECT_INPUT] = Input;
tbObjectType[OBJECT_HOTSPOT] = Hotspot;
tbObjectType[OBJECT_LISTBOX] = ListBox;
tbObjectType[OBJECT_COMBOBOX] = ComboBox;
tbObjectType[OBJECT_PROGRESS] = Progress;
tbObjectType[OBJECT_TREE] = Tree;
tbObjectType[OBJECT_PLUGIN] = Plugin;
-- Function to initialize the splitters with code to change the cursors
-- also initiates the table that holds the ratios for sizing objects
function InitSliders()
m_tblObjectData = {};
tblObjects = Page.EnumerateObjects();
end
-- Function to tell whether a point is in a given
-- objects rectangle
function IsInRect(m_nX, m_nY, m_tblPos, m_tblSize)
local bReturn = false;
if (m_nX >= m_tblPos.X) and (m_nX <= m_tblPos.X + m_tblSize.Width) then
if (m_nY >= m_tblPos.Y) and (m_nY <= m_tblPos.Y + m_tblSize.Height) then
bReturn = true;
end
end
return bReturn;
end
-- Function to detect when a slider has been selected
function DoSlider_MouseButton(e_Type, e_X, e_Y)
local bReturn = false;
local Object = nil;
if e_Type == LEFT_BUTTON_DOWN then
-- Get the objects on the page
tblObjects = Page.EnumerateObjects();
--if there are objects, and we are not currently moving a slider
if tblObjects and not m_blnMoveSlider then
for index, sObject in tblObjects do
--if the object is a splitter
if (String.Left(sObject, 6) == "vknob_") or (String.Left(sObject, 6) == "hknob_") then
--get the object type
Object = tbObjectType[Page.GetObjectType(sObject)];
--if our mouse pointer is inside of a slider rectangle
if IsInRect(e_X, e_Y, Object.GetPos(sObject), Object.GetSize(sObject)) then
if not m_blnMoveSlider then
--Get the initial Original Positions and Size
m_nOrigX = Object.GetPos(sObject).X;
m_nOrigY = Object.GetPos(sObject).Y;
m_nSplitterHeight = Object.GetSize(sObject).Height;
m_nSplitterWidth = Object.GetSize(sObject).Width;
end
--set the variables to allow the slider to move
m_blnMoveSlider = true;
m_sCurrentSlider = sObject;
m_XOffset = e_X - Object.GetPos(sObject).X;
m_YOffset = e_Y - Object.GetPos(sObject).Y;
break;
end
end
end
end
else
-- we're no longer on a slider, reset the variables that
-- allow the splitter to be moved.
m_blnMoveSlider = false;
end
end
-- Function to move the slider
function DoSlider_MouseMove(e_X, e_Y)
local z;
if not System.IsKeyDown(1) then
m_blnMoveSlider = false;
end
m_nDiff = 0;
if m_blnMoveSlider then
-- get the current slider's object type
local SliderObject = tbObjectType[Page.GetObjectType(m_sCurrentSlider)];
m_blnAllowMove = true;
if String.Left(m_sCurrentSlider, 6) == "hknob_" then
-- it's a vertical slider
if m_blnAllowMove then
m_sVolObjectName = String.Replace(m_sCurrentSlider, "hknob_", "hslider_", true);
local VolObject = tbObjectType[Page.GetObjectType(m_sVolObjectName)];
m_MaxX = (VolObject.GetPos(m_sVolObjectName).X + VolObject.GetSize(m_sVolObjectName).Width) - SliderObject.GetSize(m_sCurrentSlider).Width
m_MinX = VolObject.GetPos(m_sVolObjectName).X
if (e_X - m_XOffset) >= m_MaxX then
SliderObject.SetPos(m_sCurrentSlider, m_MaxX, SliderObject.GetPos(m_sCurrentSlider).Y);
elseif (e_X - m_XOffset) <= m_MinX then
SliderObject.SetPos(m_sCurrentSlider, m_MinX, SliderObject.GetPos(m_sCurrentSlider).Y);
else
-- Set the slider position
SliderObject.SetPos(m_sCurrentSlider, e_X - m_XOffset, SliderObject.GetPos(m_sCurrentSlider).Y);
end
-- get the value of pixels the slider was moved
m_nDiff = e_X - (m_nOrigX + m_XOffset);
-- set our sliders original position to the new position
m_nOrigX = m_nOrigX + m_nDiff;
end
m_Percent = Math.Floor(((SliderObject.GetPos(m_sCurrentSlider) .X - m_MinX) / (m_MaxX - m_MinX)) * 100)
elseif String.Left(m_sCurrentSlider, 6) == "vknob_" then
-- it's a Horizontal Slider
if m_blnAllowMove then
m_sVolObjectName = String.Replace(m_sCurrentSlider, "vknob_", "vslider_", true);
local VolObject = tbObjectType[Page.GetObjectType(m_sVolObjectName)];
m_MaxY = (VolObject.GetPos(m_sVolObjectName).Y + VolObject.GetSize(m_sVolObjectName).Height) - SliderObject.GetSize(m_sCurrentSlider).Height
m_MinY = VolObject.GetPos(m_sVolObjectName).Y
if (e_Y - m_YOffset) >= m_MaxY then
SliderObject.SetPos(m_sCurrentSlider, SliderObject.GetPos(m_sCurrentSlider).X ,m_MaxY);
elseif (e_Y - m_YOffset) <= m_MinY then
SliderObject.SetPos(m_sCurrentSlider, SliderObject.GetPos(m_sCurrentSlider).X, m_MinY );
else
-- Set the slider position
SliderObject.SetPos(m_sCurrentSlider, SliderObject.GetPos(m_sCurrentSlider).X, e_Y - m_YOffset);
end
-- get the value of pixels the slider was moved
m_nDiff = e_Y - (m_nOrigY + m_YOffset);
-- set our splitters original position to the new position
m_nOrigY = m_nOrigY + m_nDiff;
end
m_Percent = 100 - Math.Floor(((SliderObject.GetPos(m_sCurrentSlider) .Y - m_MinY) / (m_MaxY - m_MinY)) * 100)
end
-- fire event our own event
On_Slider_Move(m_sCurrentSlider, m_Percent);
end
end
3. Place your objects and name them accordingly
4. Use the custom event in the Global Functions to get the slider's position
This framework will let you use objects as sliders. There are two object to each slider, the Slider Background (Named: vslider_YourObjectName) or hslider_YourObjectName), and the Knob (Named: vknob_YourObjectName) or hknob_YourObjectName).
There is a custom event that will return the percentage the knob has traveled across the background object. The returned percentage can then be used to set volumes, move objects, set opacity, scroll pictures etc...
How to use:
1. Add these function calls to the following Page Events
On Show Event
--Initialize the sliders
InitSliders();
On Mouse Button
DoSlider_MouseButton(e_Type, e_X, e_Y);
On Mouse Move
DoSlider_MouseMove(e_X, e_Y);
2. Copy this code to your Global Functions
-- Custom event
function On_Slider_Move(sCurrentSlider, nPercent)
-- use this event to make change the values the slider
-- is related to
end
--[[************************************************** ******]]--
--[[ Set up a table to reference the objects in AMS ]]--
--[[************************************************** ******]]--
tbObjectType = {}
tbObjectType[OBJECT_BUTTON] = Button;
tbObjectType[OBJECT_LABEL] = Label;
tbObjectType[OBJECT_PARAGRAPH] = Paragraph;
tbObjectType[OBJECT_IMAGE] = Image;
tbObjectType[OBJECT_FLASH] = Flash;
tbObjectType[OBJECT_VIDEO] = Video;
tbObjectType[OBJECT_WEB] = Web;
tbObjectType[OBJECT_INPUT] = Input;
tbObjectType[OBJECT_HOTSPOT] = Hotspot;
tbObjectType[OBJECT_LISTBOX] = ListBox;
tbObjectType[OBJECT_COMBOBOX] = ComboBox;
tbObjectType[OBJECT_PROGRESS] = Progress;
tbObjectType[OBJECT_TREE] = Tree;
tbObjectType[OBJECT_PLUGIN] = Plugin;
-- Function to initialize the splitters with code to change the cursors
-- also initiates the table that holds the ratios for sizing objects
function InitSliders()
m_tblObjectData = {};
tblObjects = Page.EnumerateObjects();
end
-- Function to tell whether a point is in a given
-- objects rectangle
function IsInRect(m_nX, m_nY, m_tblPos, m_tblSize)
local bReturn = false;
if (m_nX >= m_tblPos.X) and (m_nX <= m_tblPos.X + m_tblSize.Width) then
if (m_nY >= m_tblPos.Y) and (m_nY <= m_tblPos.Y + m_tblSize.Height) then
bReturn = true;
end
end
return bReturn;
end
-- Function to detect when a slider has been selected
function DoSlider_MouseButton(e_Type, e_X, e_Y)
local bReturn = false;
local Object = nil;
if e_Type == LEFT_BUTTON_DOWN then
-- Get the objects on the page
tblObjects = Page.EnumerateObjects();
--if there are objects, and we are not currently moving a slider
if tblObjects and not m_blnMoveSlider then
for index, sObject in tblObjects do
--if the object is a splitter
if (String.Left(sObject, 6) == "vknob_") or (String.Left(sObject, 6) == "hknob_") then
--get the object type
Object = tbObjectType[Page.GetObjectType(sObject)];
--if our mouse pointer is inside of a slider rectangle
if IsInRect(e_X, e_Y, Object.GetPos(sObject), Object.GetSize(sObject)) then
if not m_blnMoveSlider then
--Get the initial Original Positions and Size
m_nOrigX = Object.GetPos(sObject).X;
m_nOrigY = Object.GetPos(sObject).Y;
m_nSplitterHeight = Object.GetSize(sObject).Height;
m_nSplitterWidth = Object.GetSize(sObject).Width;
end
--set the variables to allow the slider to move
m_blnMoveSlider = true;
m_sCurrentSlider = sObject;
m_XOffset = e_X - Object.GetPos(sObject).X;
m_YOffset = e_Y - Object.GetPos(sObject).Y;
break;
end
end
end
end
else
-- we're no longer on a slider, reset the variables that
-- allow the splitter to be moved.
m_blnMoveSlider = false;
end
end
-- Function to move the slider
function DoSlider_MouseMove(e_X, e_Y)
local z;
if not System.IsKeyDown(1) then
m_blnMoveSlider = false;
end
m_nDiff = 0;
if m_blnMoveSlider then
-- get the current slider's object type
local SliderObject = tbObjectType[Page.GetObjectType(m_sCurrentSlider)];
m_blnAllowMove = true;
if String.Left(m_sCurrentSlider, 6) == "hknob_" then
-- it's a vertical slider
if m_blnAllowMove then
m_sVolObjectName = String.Replace(m_sCurrentSlider, "hknob_", "hslider_", true);
local VolObject = tbObjectType[Page.GetObjectType(m_sVolObjectName)];
m_MaxX = (VolObject.GetPos(m_sVolObjectName).X + VolObject.GetSize(m_sVolObjectName).Width) - SliderObject.GetSize(m_sCurrentSlider).Width
m_MinX = VolObject.GetPos(m_sVolObjectName).X
if (e_X - m_XOffset) >= m_MaxX then
SliderObject.SetPos(m_sCurrentSlider, m_MaxX, SliderObject.GetPos(m_sCurrentSlider).Y);
elseif (e_X - m_XOffset) <= m_MinX then
SliderObject.SetPos(m_sCurrentSlider, m_MinX, SliderObject.GetPos(m_sCurrentSlider).Y);
else
-- Set the slider position
SliderObject.SetPos(m_sCurrentSlider, e_X - m_XOffset, SliderObject.GetPos(m_sCurrentSlider).Y);
end
-- get the value of pixels the slider was moved
m_nDiff = e_X - (m_nOrigX + m_XOffset);
-- set our sliders original position to the new position
m_nOrigX = m_nOrigX + m_nDiff;
end
m_Percent = Math.Floor(((SliderObject.GetPos(m_sCurrentSlider) .X - m_MinX) / (m_MaxX - m_MinX)) * 100)
elseif String.Left(m_sCurrentSlider, 6) == "vknob_" then
-- it's a Horizontal Slider
if m_blnAllowMove then
m_sVolObjectName = String.Replace(m_sCurrentSlider, "vknob_", "vslider_", true);
local VolObject = tbObjectType[Page.GetObjectType(m_sVolObjectName)];
m_MaxY = (VolObject.GetPos(m_sVolObjectName).Y + VolObject.GetSize(m_sVolObjectName).Height) - SliderObject.GetSize(m_sCurrentSlider).Height
m_MinY = VolObject.GetPos(m_sVolObjectName).Y
if (e_Y - m_YOffset) >= m_MaxY then
SliderObject.SetPos(m_sCurrentSlider, SliderObject.GetPos(m_sCurrentSlider).X ,m_MaxY);
elseif (e_Y - m_YOffset) <= m_MinY then
SliderObject.SetPos(m_sCurrentSlider, SliderObject.GetPos(m_sCurrentSlider).X, m_MinY );
else
-- Set the slider position
SliderObject.SetPos(m_sCurrentSlider, SliderObject.GetPos(m_sCurrentSlider).X, e_Y - m_YOffset);
end
-- get the value of pixels the slider was moved
m_nDiff = e_Y - (m_nOrigY + m_YOffset);
-- set our splitters original position to the new position
m_nOrigY = m_nOrigY + m_nDiff;
end
m_Percent = 100 - Math.Floor(((SliderObject.GetPos(m_sCurrentSlider) .Y - m_MinY) / (m_MaxY - m_MinY)) * 100)
end
-- fire event our own event
On_Slider_Move(m_sCurrentSlider, m_Percent);
end
end
3. Place your objects and name them accordingly
4. Use the custom event in the Global Functions to get the slider's position