I found that code to be a little bit confusing as well and decided to put my own little slider include file together. It may or may not be as performant, but you might find it easier to use.
Slider.agc
// -----------------------------------
// Simple Horizontal Slider module
// by Hendron
// -----------------------------------
// adjust to change slider appearance
#constant SLIDER_BAR_WIDTH 200.0
#constant SLIDER_BAR_HEIGHT 8.0
#constant SLIDER_BAR_COLOR MakeColor(128, 128, 128)
#constant SLIDER_HANDLE_WIDTH 8.0
#constant SLIDER_HANDLE_HEIGHT 24.0
#constant SLIDER_HANDLE_COLOR MakeColor(128,255,128)
#constant SLIDER_TEXT_SIZE 16
type tSliderResource
imgBar
imgHandle
sliderList as tSlider[]
hotIdx
endtype
type tSlider
name as string
min as float
max as float
current as float
x as float
y as float
sprBar
sprHandle
txtLabel
txtValue
endtype
global _sliderResource as tSliderResource
// must be called before using other functions
function InitSliders()
_sliderResource.imgBar = _SliderCreateBarImage()
_sliderResource.imgHandle = _SliderCreateHandleImage()
_sliderResource.hotIdx = -1
endfunction
function CreateSlider(name$, min#, max#, x#, y#)
inst as tSlider
inst.name = name$
inst.min = min#
inst.max = max#
inst.x = x#
inst.y = y#
inst.current = inst.min
// setup sprites
inst.sprBar = CreateSprite(_sliderResource.imgBar)
SetSpriteOffset(inst.sprBar, 0, GetSpriteHeight(inst.sprBar)/2)
inst.sprHandle = CreateSprite(_sliderResource.imgHandle)
SetSpriteOffset(inst.sprHandle,SLIDER_HANDLE_WIDTH/2,0)
SetSpritePosition(inst.sprHandle, x#, y#)
SetSpritePositionByOffset(inst.sprBar, x#, y# + (GetSpriteHeight(inst.sprHandle)/2))
// setup texts
inst.txtLabel = CreateText(name$)
SetTextSize(inst.txtLabel, SLIDER_TEXT_SIZE)
SetTextPosition(inst.txtLabel, x#, y# + SLIDER_HANDLE_HEIGHT/2 + SLIDER_TEXT_SIZE)
inst.txtValue = CreateText(str(inst.min,2))
SetTextSize(inst.txtValue, SLIDER_TEXT_SIZE)
SetTextAlignment(inst.txtValue, 2)
SetTextPosition(inst.txtValue, x# + SLIDER_BAR_WIDTH, GetTextY(inst.txtLabel))
// add to slider list
_sliderResource.sliderList.insertSorted(inst)
endfunction
// remove slider and its resources
function DeleteSlider(name$)
idx = _sliderResource.sliderList.find(name$)
if idx > -1
DeleteSprite(_sliderResource.sliderList[idx].sprBar)
DeleteSprite(_sliderResource.sliderList[idx].sprHandle)
DeleteText(_sliderResource.sliderList[idx].txtLabel)
DeleteText(_sliderResource.sliderList[idx].txtValue)
_sliderResource.sliderList.remove(idx)
endif
endfunction
// call this at the beginning of every loop
function HandleSliders()
if _sliderResource.hotIdx > -1
_SliderHandleHot(_sliderResource.sliderList[_sliderResource.hotIdx])
else
if GetPointerPressed() = 1
px# = GetPointerX()
py# = GetPointerY()
for i = 0 to _sliderResource.sliderList.length
if GetSpriteHitTest(_sliderResource.sliderList[i].sprHandle, px#, py#) or GetSpriteHitTest(_sliderResource.sliderList[i].sprBar, px#, py#)
_sliderResource.hotIdx = i
exitfunction
endif
next i
endif
endif
endfunction
// get the current slider value
function GetSliderValue(name$)
idx = _sliderResource.sliderList.find(name$)
if idx > -1
exitfunction _sliderResource.sliderList[idx].current
endif
endfunction 0.0
// set the current slider value
function SetSliderValue(name$, value)
idx = _sliderResource.sliderList.find(name$)
if idx > -1
if value > _sliderResource.sliderList[idx].max then value = _sliderResource.sliderList[idx].max
if value < _sliderResource.sliderList[idx].min then value = _sliderResource.sliderList[idx].min
_sliderResource.sliderList[idx].current = value
_SliderUpdateAppearance(_sliderResource.sliderList[idx])
endif
endfunction
// set custom bar image
function SetSliderBarImage(name$, img)
idx = _sliderResource.sliderList.find(name$)
if idx > -1
SetSpriteImage(_sliderResource.sliderList[idx].sprBar, img)
endif
endfunction
// set custom handle image
function SetSliderHandleImage(name$, img)
idx = _sliderResource.sliderList.find(name$)
if idx > -1
SetSpriteImage(_sliderResource.sliderList[idx].sprHandle, img)
endif
endfunction
// --------------------------------------------------------------------------
// The following functions are not meant to be used outside of this file
// handle slider that is currently "hot" (pointer pressed on it)
function _SliderHandleHot(inst ref as tSlider)
if GetPointerState() = 1
px# = GetPointerX()
sx# = inst.x
t# = ((px# - sx#) / SLIDER_BAR_WIDTH)
if t# > 1.0 then t# = 1.0
if t# < 0.0 then t# = 0.0
range# = inst.max - inst.min
inst.current = inst.min + (range# * t#)
_SliderUpdateAppearance(inst)
else
_sliderResource.hotIdx = -1
endif
endfunction
// update the slider's sprite positions and text
function _SliderUpdateAppearance(inst ref as tSlider)
t# = (inst.current - inst.min) / (inst.max - inst.min)
SetTextString(inst.txtValue, str(inst.current,2))
SetSpritePosition(inst.sprHandle, inst.x + (SLIDER_BAR_WIDTH - SLIDER_HANDLE_WIDTH / 2) * t#, GetSpriteY(inst.sprHandle))
endfunction
// create a default bar image
function _SliderCreateBarImage()
ClearScreen()
DrawBox(0,0,SLIDER_BAR_WIDTH,SLIDER_BAR_HEIGHT,SLIDER_BAR_COLOR,SLIDER_BAR_COLOR,SLIDER_BAR_COLOR,SLIDER_BAR_COLOR,1)
Render()
imgId = GetImage(0,0,SLIDER_BAR_WIDTH,SLIDER_BAR_HEIGHT)
endfunction imgId
// create a default handle image
function _SliderCreateHandleImage()
ClearScreen()
DrawBox(0,0,SLIDER_HANDLE_WIDTH,SLIDER_HANDLE_HEIGHT,SLIDER_HANDLE_COLOR,SLIDER_HANDLE_COLOR,SLIDER_HANDLE_COLOR,SLIDER_HANDLE_COLOR,1)
Render()
imgId = GetImage(0,0,SLIDER_HANDLE_WIDTH,SLIDER_HANDLE_HEIGHT)
endfunction imgId
// -------------------------------------------------------------
and to test it out:
#include "Slider.agc"
// setup window and create test object
SetWindowSize(1024, 768, 0)
SetVirtualResolution(1024, 768)
SetSyncRate(0, 0)
UseNewDefaultFonts(1)
obj = CreateObjectBox(10,10,10)
// must be called before any other slider functions
InitSliders()
// create some sliders
// name, min, max, x position, y position
CreateSlider("Red", 0, 255, 24, 16)
CreateSlider("Green", 0, 255, 24, 96)
CreateSlider("Blue", 0, 255, 24, 176)
CreateSlider("Angle", 0, 360, 24, 256)
// uncomment to test changing the slider images
//SetSliderHandleImage("Red", CreateImageColor(255,0,0,255))
//SetSliderHandleImage("Green", CreateImageColor(0,255,0,255))
//SetSliderHandleImage("Blue", CreateImageColor(0,0,255,255))
do
// call this before retrieving any slider values
HandleSliders()
// press space to max out the sliders
if GetRawKeyPressed(32)
SetSliderValue("Red", 255)
SetSliderValue("Green", 255)
SetSliderValue("Blue", 255)
SetSliderValue("Angle", 360)
endif
// apply slider values to object
SetObjectColor(obj, GetSliderValue("Red"), GetSliderValue("Green"), GetSliderValue("Blue"), 255)
SetObjectRotation(obj,0,GetSliderValue("Angle"),0)
Sync()
loop