Hi guys,
this snippet is about scrolling items/images, it could be used for a hotbar or inventory(that's my intention).For base i used few stuff but i wrote most of it.I used image block tutorial thread from here :
http://forum.thegamecreators.com/?m=forum_view&t=110437&b=1 and phaelax's scrollbar example from here :
http://forum.thegamecreators.com/?m=forum_view&t=196148&b=4
There is more work that can be done to drawing calls to make it easier but it's good for the purpose of the demo.I optimized it as i can so it should be ok even with many images on the screen.Be aware that it's not pixel perfect some time it doesn't draw first line of the image or the last line, this doesen't bother me at all because my images are transparent there.I position images manually here but intend to use a grid in the future, feel free to do it.
sync on : sync rate 0
backdrop on
REM load an image 64x64
load image "C:\Users\lusi\Desktop\rus\items\434.png", 1
global widthMax as integer
global heightMax as integer
REM create a memblock from the image to read from
make memblock from image 2, 1
REM get the image's width and height
widthMax = memblock dword(2,0)
heightMax = memblock dword(2,4)
global top_border as integer
top_border = 100
Type SliderObject
focus as boolean
offset as integer
thumb as integer
value as float
last_value as float
maxVal as integer
minVal as integer
Endtype
dim sliders(1) as SliderOBject
sliders(1).maxVal = 128
sliders(1).minVal = 0
sliders(1).thumb = 0
Global _MFlag = 0
Global _MClick = 0
type items
DrawHeight as integer
topVal as integer
botVal as integer
ypos as integer
endtype
dim items(5) as items
items(1).topVal = 0
items(2).topVal = 0
items(3).topVal = 128
items(4).topVal = 192
items(4).botVal = 0
items(4).DrawHeight = -64
items(5).topVal = 256
items(5).botVal = 64
items(5).DrawHeight = -64
drawing(1,3,1,2,1)
drawing(2,4,1,2,3)
drawing(3,5,1,2,4)
drawing(4,6,1,2,5)
drawing(5,7,1,2,6)
do
`Refresh for hidden images/It does not work if mouse is out of window when started,should be ok for full screen
if old_ms_y = mousey() and refresh = 0
drawing(4,6,1,2,5)
drawing(5,7,1,2,6)
refresh = 1
endif
line 0,100,260,100
line 0,292,260,292
_MClick = mouseclick()
slider(1, 264,100, 200, 20, sliders(1).minVal, sliders(1).maxVal)
if _MClick > 0 : _MFlag = 1 : else : _MFlag = 0 : endif
`Redraw images only if there is offset in current mousey and old one.
`IF use old_ms_y <> mousey() and mouseclick() = 1 -> more FPS
`IF use old_ms_y <> mousey() or mouseclick() = 1 -> smooth transition, less FPS
if old_ms_y <> mousey() and mouseclick() = 1
drawing(1,3,1,2,1)
drawing(2,4,1,2,3)
drawing(3,5,1,2,4)
drawing(4,6,1,2,5)
drawing(5,7,1,2,6)
top_border = 100-sliders(1).value
endif
`Old values
if timer() >= timestamp+1
sliders(1).last_value = sliders(1).value
old_ms_y = mousey()
timestamp = timer()
endif
for n = 1 to 5
items(n).ypos = top_border + items(n).topVal
next n
paste image 3,200,int(top_border+items(1).topVal),1
paste image 4,136,int(top_border+items(2).topVal),1
paste image 5,200,int(top_border+items(3).topVal),1
paste image 6,200,int(top_border+items(4).topVal),1
paste image 7,200,int(top_border+items(5).topVal),1
text 0,0,"FPS: "+str$(screen fps())
text 0,20,"items(4).DrawHeight: "+str$(items(4).DrawHeight)
text 0,40,"items(1).DrawHeight: "+str$(items(1).DrawHeight)
text 0,60,"items(4).ypos: "+str$(items(4).ypos)
text 0,80,"sliders(1).value: "+str$(sliders(1).value)
text 0,100,"mousex():"+str$(mousex())+" mousey(): "+str$(mousey())
sync
loop
`Just call argb() like you would rgb(), but send an extra variable for alpha.
function argb2(r, g, b, a)
col = a<<24 || r<<16 || g<<8 || b
endfunction col
function slider(id, x, y, width, height, minVal, maxVal)
ink rgb(255,255,255),0
`text x-text width(str$(minVal))-2, y, str$(minVal)
`text x+width+2, y, str$(maxVal)
`center text x+sliders(id).thumb, y-text height(" ")-2, str$(sliders(id).value)
ink rgb(110,244,2), 0
box x, y, x+height, y+width
ty = sliders(id).thumb
ink rgb(255,255,0), 0
box x, y+ty, x+height, y+ty+height
if _MClick = 1
if _MFlag = 0
if mouseHotspot(x, y+ty, x+height, y+ty+height) = 1 and sliders(id).focus = 0
sliders(id).focus = 1
sliders(id).offset = mousey() - sliders(id).thumb
endif
_MFlag = 1
endif
if sliders(id).focus = 1
sliders(id).thumb = mousey() - sliders(id).offset
if sliders(id).thumb < 0 then sliders(id).thumb = 0
if sliders(id).thumb > width-height then sliders(id).thumb = width-height
t# = sliders(id).thumb / (width-height+0.0)
sliders(id).value = minVal + (maxVal - minVal)*t#
endif
else
sliders(id).focus = 0
endif
endfunction
function mouseHotspot(x1, y1, x2, y2)
if mousex() > x1 and mousex() < x2 and mousey() > y1 and mousey() < y2 then exitfunction 1
endfunction 0
function drawing(ID,PasteImg,ImgNum,ReadMemblock,WriteMemblock)
if memblock exist(WriteMemblock) = 0 then make memblock from image WriteMemblock,ImgNum
if items(ID).ypos < 100
if items(ID).DrawHeight > 0
for x = 1 to widthMax
for y = 1 to items(ID).DrawHeight
if items(ID).DrawHeight <= heightMax
location = ((y-1)*widthMax + x - 1)*4 + 12
endif
write memblock dword WriteMemblock, location, argb2(255,255, 255, 0)
next y
next x
endif
if items(ID).DrawHeight < 0
for x = 1 to widthMax
for y = heightMax to items(ID).DrawHeight+65 step -1
if y = heightMax
location = ((y+items(ID).DrawHeight)*widthMax + x - 1)*4 + 12
else
location = ((y-1)*widthMax + x - 1)*4 + 12
endif
write memblock dword WriteMemblock, location,memblock dword (ReadMemblock,location)
next y
next x
endif
endif
if items(ID).ypos > 228
if items(ID).DrawHeight > 0
for x = 1 to widthMax
for y = 1 to items(ID).DrawHeight
if items(ID).DrawHeight <= heightMax
location = ((y-1)*widthMax + x - 1)*4 + 12
endif
write memblock dword WriteMemblock, location,memblock dword (2,location)
next y
next x
endif
if items(ID).DrawHeight < 0
for x = 1 to widthMax
for y = heightMax to items(ID).DrawHeight+65 step -1
if y = heightMax
location = ((y+items(ID).DrawHeight)*widthMax + x - 1)*4 + 12
else
location = ((y-1)*widthMax + x - 1)*4 + 12
endif
write memblock dword WriteMemblock, location, argb2(255,255, 255, 0)
next y
next x
endif
endif
`DRAW HEIGHT CORRECTION
if items(ID).ypos < 150 and items(ID).ypos > 100
items(ID).DrawHeight = -64
endif
if items(ID).ypos > 150 and items(ID).ypos < 228
items(ID).DrawHeight = 64
endif
`TOPSIDE DRAW HEIGHT
if items(ID).ypos <100
if sliders(1).value > items(ID).topVal
if int(sliders(1).last_value) > int(sliders(1).value) then items(ID).DrawHeight = int(sliders(1).value)-items(ID).topVal -63
endif
if sliders(1).value > items(ID).topVal
if int(sliders(1).last_value) < int(sliders(1).value) then items(ID).DrawHeight = int(sliders(1).value)-items(ID).topVal
endif
if items(1).DrawHeight > 0 and int(sliders(1).last_value) = int(sliders(1).value)-items(ID).topVal
items(ID).DrawHeight = int(sliders(1).value)-items(ID).topVal
endif
endif
`BOTSIDE DRAW HEIGHT
if items(ID).ypos > 228
if sliders(1).value > items(ID).botVal
if int(sliders(1).last_value) > int(sliders(1).value) then items(ID).DrawHeight = int(sliders(1).value)-items(ID).botVal -63
endif
if sliders(1).value > items(ID).botVal
if int(sliders(1).last_value) < int(sliders(1).value) then items(ID).DrawHeight = int(sliders(1).value)-items(ID).botVal
endif
if items(1).DrawHeight > 0 and int(sliders(1).last_value) = int(sliders(1).value)-items(ID).botVal
items(ID).DrawHeight = int(sliders(1).value)-items(ID).botVal
endif
endif
if items(ID).DrawHeight > 64 then items(ID).DrawHeight = 64
if items(ID).DrawHeight < -64 then items(ID).DrawHeight = -64
REM create a new image from the memblock data
make image from memblock PasteImg, WriteMemblock
endfunction
Coding is My Kung Fu!
And My Kung Fu is better than Yours!