// // script: tk32's ultimate progress bar // revision: 2 // date: 18th Feb 2004 // email: tkelly32@btinternet.com // // notes: // i am trying to write the ultimate progress bar script, which // not only can do everything required, but is also easy to use // // usage guide: // to attach the script properly, you will need a total of 2 sprites (3 if you want a grip) // // 1. // the first sprite is the actual 'bar', or 'fill', of the progress bar. // it can be a graphic, a fill, or even invisible. // important notes: // - this sprite never recieves mouse input (so use 'bool enabled false') // - the width of this sprite should be set to 0 (the script will update the width automatically) // // here is an example: // // sprite progBar // bool enabled false // list rect int { 10 60 0 22 } // list rendermap int { render.fill } // list fillcolor int { 150 150 150 150 } // // 2. // the second sprite will be an invisible outline of whole width of the bar, and will be the part that // receives clicks & movement from the mouse. so our script will be attached to this sprite. // important notes: // - this sprite will be invisible // - this sprite have a set width of the entire progress bar // - the script will be attached to this sprite // - we can include the previous sprite to save us repeating some values // - when attaching the script, we must tell it the name of our 'bar' sprite (as above) // // here is my sprite as an example (i've chosen to use an added gray border): // // sprite progBox // string include progBar // int width 216 // list rendermap int { render.border } // int borderwidth 1 // list bordercolor int { 150 150 150 150 } // list scripts string { "tk32_progBar[bar=progBar]" } // // // 3. // to add a grip, simply add a basic grip sprite like this: // // sprite progGrip // bool enabled false // list rect int { 10 60 38 22 } // list srcoff int { 12 217 } // // (note that this sprite is disabled too, since we don't want it to receive mouse commands) // // lastly, we need to change the script arguments in the 'box' sprite (step 2) so that it looks like this: // // list scripts string { "tk32_progBar[bar=progBar grip=progGrip]" } // // // 4. // that's it for now :) // // details of more customizations to follow (as they are added) // // // ...now for the code... lua tk32_progBar sliding, scanning = false inc = 0.02 prev = 0 gripOffset = 0 function onattach(this) boxRect = look_getRect(this) if grip then gripOffset = look_getWidth(grip) / 2 boxRect.x = boxRect.x + gripOffset boxRect.width = boxRect.width - (2 * gripOffset) end end function onupdate(this) if sliding == true and not fb2k_isPaused() then followCursor(this) elseif fb2k_isPlaying() then setBarPercent(this, getTimePercent(this)) end if scanning == true then scanTrack(this) end end function onleftbuttondown(this) sliding = true end function onleftbuttonup(this) sliding = false setTimePercent(this) end function onleftbuttonupoutside(this) onleftbuttonup(this) end function onrightbuttondown(this) scanning = true end function onrightbuttonup(this) scanning = false end function onrightbuttonupoutside(this) onrightbuttonup(this) end function onplaybackstop(this) setBarPercent(this, 0) end function constrain(variable,min,max) temp = variable if variable < min then temp = min elseif variable > max then temp = max end return temp; end function getBarPercent(this) currentWidth = look_getWidth(bar) - gripOffset maxWidth = boxRect.width return currentWidth / maxWidth end function setBarPercent(this, percent) newWidth = percent * boxRect.width + gripOffset look_setWidth(bar, newWidth) setGrip(newWidth - gripOffset) end function setGrip(newWidth) if grip then look_setX(grip, boxRect.x + newWidth - gripOffset) end end function getTimePercent(this) return fb2k_getPlayPosition() / fb2k_getSongLength(); end function setTimePercent(this) fb2k_playbackSeek(getBarPercent(this) * fb2k_getSongLength()) end function addIncrement(direction,distance) local current = fb2k_getPlayPosition() local increment = fb2k_getSongLength() * inc if (direction == 0) then fb2k_playbackSeek(current - increment) else fb2k_playbackSeek(current + increment) end look_updateTagz() end function scanTrack(this) relativeMouseX = look_mouseX() - look_getX(this) barWidth = look_getWidth(bar) checkval = abs(relativeMouseX - barWidth) if checkval > 2 then if relativeMouseX < barWidth then direction = 0 elseif relativeMouseX > barWidth then direction = 1 end addIncrement(direction,checkval) end end function followCursor(this) relativeMouseX = look_mouseX() - boxRect.x if newx ~= prev then temp = constrain(relativeMouseX, 0, boxRect.width) look_setWidth(bar, temp + gripOffset) setGrip(temp) prev = temp end end endlua