This code makes horizontal and vertical sliders, with any range of values you specify. You have to InitSliders() to get it going. Then you SliderCreate() and SliderShow(). You can also set the slider background image.There are a host of other supporting functions. If you want to use it, I would suggest reading it, then asking any questions you may have.
#CONSTANT cSLIDER_HORIZONTAL = 0
#CONSTANT cSLIDER_VERTICAL = 1
#CONSTANT cSLIDER_ALIGN_LEFT = 2
#CONSTANT cSLIDER_ALIGN_TOP = 3
#CONSTANT cSLIDER_ALIGN_RIGHT = 4
#CONSTANT cSLIDER_ALIGN_BOTTOM = 5
#CONSTANT cSLIDER_ALIGN_CENTRE_H = 6
#CONSTANT cSLIDER_ALIGN_CENTRE_V = 7
type tSliderSystem
nextId as integer
isPressed as integer
endtype
type tSlider
active
size as tVector2
orientation
spriteBar
spriteSlider
spriteBack
colourSlider as tColour
slideRange as float
alignment
minVal as float
maxVal as float
endtype
// *******************************************************
function initSliders()
global gSliderSystem as tSliderSystem
gSliderSystem.nextId = 1
dim arrSlider[0] as tSlider
endfunction
// *******************************************************
// X defines the length of the bar
// Y defines the height of the slider (needs to be touchable)
// *******************************************************
function SliderCreate(width, height, orientation)
// *******************************************************
id = gSliderSystem.nextId
arrSlider.length = id
inc gSliderSystem.nextId, 1
arrSlider[id].size.x = width
arrSlider[id].size.y = height
arrSlider[id].orientation = orientation
arrSlider[id].active = 0
arrSlider[id].minVal = 0
arrSlider[id].maxVal = 100
endfunction id
// *******************************************************
function SliderShow(id, x, y, align)
//Make the slider sprites
arrSlider[id].spriteBar = createSprite(0)
arrSlider[id].spriteSlider = createSprite(0)
arrSlider[id].spriteBack = createSprite(0)
SetSpriteColor(arrSlider[id].spriteBack,32,32,32,255)
if arrSlider[id].orientation = cSLIDER_HORIZONTAL
setSpritesize(arrSlider[id].spriteBar, arrSlider[id].size.x, 3)
setSpritesize(arrSlider[id].spriteSlider, arrSlider[id].size.y, arrSlider[id].size.y)
setSpritesize(arrSlider[id].spriteBack, arrSlider[id].size.x, arrSlider[id].size.y)
else
setSpritesize(arrSlider[id].spriteBar, 3, arrSlider[id].size.y)
setSpritesize(arrSlider[id].spriteSlider, arrSlider[id].size.x, arrSlider[id].size.x)
setSpritesize(arrSlider[id].spriteBack, arrSlider[id].size.x, arrSlider[id].size.y)
endif
SetSpriteDepth(arrSlider[id].spriteBar, 8)
SetSpriteDepth(arrSlider[id].spriteSlider, 7)
SetSpriteDepth(arrSlider[id].spriteBack, 9)
// Set Default positions
select align
case cSLIDER_ALIGN_CENTRE_H
x = x - (GetSpriteWidth(arrSlider[id].spriteBar) * 0.5)
endcase
case cSLIDER_ALIGN_RIGHT
x = x - (GetSpriteWidth(arrSlider[id].spriteBar))
endcase
case cSLIDER_ALIGN_CENTRE_V
y = y - (GetSpriteHeight(arrSlider[id].spriteBar) * 0.5)
endcase
case cSLIDER_ALIGN_BOTTOM
y = y - (GetSpriteHeight(arrSlider[id].spriteBar))
endcase
endselect
SetSpritePosition(arrSlider[id].spriteBar,x,y)
SetSpritePositionByOffset(arrSlider[id].spriteBack, GetSpriteXByOffset(arrSlider[id].spriteBar),GetSpriteYByOffset(arrSlider[id].spriteBar))
select arrSlider[id].orientation
case cSLIDER_HORIZONTAL
SetSpritePositionByOffset(arrSlider[id].spriteSlider, x + (getSpritewidth(arrSlider[id].spriteSlider) / 2), y)
arrSlider[id].slideRange = (getSpritewidth(arrSlider[id].spriteBar) - getSpritewidth(arrSlider[id].spriteSlider))
endcase
case cSLIDER_VERTICAL
SetSpritePositionByOffset(arrSlider[id].spriteSlider, x, y + (getSpriteHeight(arrSlider[id].spriteSlider) * 0.5))
arrSlider[id].slideRange = (GetSpriteHeight(arrSlider[id].spriteBar) - GetSpriteHeight(arrSlider[id].spriteSlider))
endcase
endselect
arrSlider[id].active = 1
endfunction
function sliderHideAll()
for id = 1 to arrSlider.length
if arrSlider[id].active = 1
SliderHide(id)
endif
next id
endfunction
function SliderHide(id)
DeleteSprite(arrSlider[id].spriteBar)
DeleteSprite(arrSlider[id].spriteSlider)
DeleteSprite(arrSlider[id].spriteBack)
arrSlider[id].active = 0
endfunction
function SliderIsActive(id)
ret = arrSlider[id].active
endfunction ret
//****************************************************
function SliderSetBackImage(id, imageId)
//****************************************************
SetSpriteImage(arrSlider[id].spriteBack, imageId)
SetSpriteColor(arrSlider[id].spriteBack, 255,255,255,255)
endfunction
//****************************************************
function SliderSetRange(id, min#, max#)
//****************************************************
arrSlider[id].minVal = min#
arrSlider[id].maxVal = max#
endfunction
//****************************************************
function SliderSetPositionAbsolute(id, value#)
//****************************************************
perc# = value# / (arrSlider[id].maxVal - arrSlider[id].minVal)
if perc# < 0 then perc# = 0
if perc# > 1 then perc# = 1
select arrSlider[id].orientation
case cSLIDER_HORIZONTAL
offset# = arrSlider[id].slideRange * perc# + (GetSpriteWidth(arrSlider[id].spriteSlider) * 0.5)
SetSpritePositionByOffset(arrSlider[id].spriteSlider, getSpriteX(arrSlider[id].spriteBar) + offset#, GetSpriteYByOffset(arrSlider[id].spriteSlider))
endcase
case cSLIDER_VERTICAL
offset# = arrSlider[id].slideRange * perc# + (GetSpriteHeight(arrSlider[id].spriteSlider) * 0.5)
SetSpritePositionByOffset(arrSlider[id].spriteSlider, GetSpriteXByOffset(arrSlider[id].spriteSlider), getSpriteY(arrSlider[id].spriteBar) + offset#)
endcase
endselect
endfunction
//*****************************************
// If a slider is now being clicked, this will tell you the slider ID
// You would call this as part of your interface click checks.
// It is assumed that you want to interact with the slider if a hit is positive
// If the hit is on the bar it will jump to that position
//*****************************************
function SliderisFirstPress()
//*****************************************
hit = GetSpriteHit(getpointerX(), getPointerY())
if hit > 0
for id = 1 to arrSlider.length
if arrSlider[id].active = 1
if hit = arrSlider[id].spriteBar or hit = arrSlider[id].spriteSlider or hit = arrSlider[id].spriteBack
log("First Press: " + str(id))
exitfunction id
endif
endif
next id
endif
endfunction 0
//*****************************************
// If a slider is being processed, this will return > 0
//*****************************************
function SliderisPressed()
//*****************************************
log("Slider is Pressed : " + str(gSliderSystem.isPressed))
exitfunction gSliderSystem.isPressed
endfunction 0
//*****************************************
// Tell the slider to stop moving
//*****************************************
function SliderStopPress()
//*****************************************
log("Stop monitoring slider position")
gSliderSystem.isPressed = 0
endfunction
//*****************************************
// Internal function to reposition slider
//*****************************************
function sliderPositionSlider(id)
//*****************************************
sb = arrSlider[id].spriteBar
ss = arrSlider[id].spriteSlider
select arrSlider[id].orientation
case cSLIDER_HORIZONTAL
x# = GetPointerX()
if getPointerX() > GetSpriteWidth(sb) + getSpriteX(sb) - (arrSlider[id].size.y / 2) then x# = GetSpriteWidth(sb) + getSpriteX(sb) - (arrSlider[id].size.y / 2)
if getPointerX() < getSpriteX(sb) + (arrSlider[id].size.y / 2) then x# = getSpriteX(sb) + (arrSlider[id].size.y / 2)
SetSpritePositionByOffset(ss, x#, GetSpriteYByOffset(ss))
pos# = (x# - (getSpriteX(sb) + (arrSlider[id].size.y / 2))) / (GetSpriteWidth(sb) - arrSlider[id].size.y)
endcase
case cSLIDER_VERTICAL
y# = GetPointerY()
if getPointerY() > GetSpriteHeight(sb) + getSpriteY(sb) - (arrSlider[id].size.x / 2) then y# = GetSpriteHeight(sb) + getSpriteY(sb) - (arrSlider[id].size.x / 2)
if getPointerY() < getSpriteY(sb) + (arrSlider[id].size.x / 2) then y# = getSpriteY(sb) + (arrSlider[id].size.x / 2)
SetSpritePositionByOffset(ss, GetSpriteXByOffset(ss), y#)
pos# = (y# - (getSpriteY(sb) + (arrSlider[id].size.x / 2))) / (GetSpriteHeight(sb) - arrSlider[id].size.x)
endcase
endselect
ret# = arrSlider[id].minVal + (pos# * (arrSlider[id].maxVal - arrSlider[id].minVal))
//print("pos# = " + str(pos#,2))
//print("ret# = " + str(ret#,2))
//print("range# = " + str((arrSlider[id].maxVal - arrSlider[id].minVal),2))
//print("Slider x: " + str(x#))
//print("Slider y: " + str(y#))
endfunction ret#
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt