Setting boundaries on a scrolling field

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Contact:

Setting boundaries on a scrolling field

Post by William Jamieson » Sat Jul 13, 2013 9:58 am

So I am using Livecode's Android scroller button for a group on my stack and was wondering how to set the upper and lower limits for the scroll so that it cant scroll past where the contents stop. Let me know if there is any clarification needed. I feel that this is a fairly simple task, I'm just very unfamiliar with the scroll function still and trying to figure it out. If anyone can contribute to this, I would greatly appreciate it!

here is the code for the android scroller if you want to look again

Code: Select all

local xStart,yStart,hScrollStart,vScrollStart,tLastLoc,allowDrag,scrollChunks

on mouseDown
  --###cancel item 1 of line lineOffset("runEase",pendingMessages()) of pendingMessages()
  cancelEaseMsgs
  put "" into tLastLoc
  put "" into scrollChunks
  put mouseH() into xStart
  put mouseV() into yStart
  put hScroll of me into hScrollStart
  put vScroll of me into vScrollStart
  put true into allowDrag
end mouseDown

on mouseMove X,Y
   if not allowDrag then exit mouseMove
   lock screen
   
   --### set hScroll of me to hScrollStart + (xStart - X)
   set vScroll of me to vScrollStart + (yStart - Y)
   unlock screen
   if "storeScrollChunks" is not in pendingMessages() then send "storeScrollChunks X,Y" to me in 100 millisecs
   put X,Y into tLastLoc
end mouseMove

on storeScrollChunks pX,pY
  put item 1 of tLastLoc - pX,item 2 of tLastLoc - pY into scrollChunks
end storeScrollChunks

on mouseUp
  prepareScroll
end mouseUp

on mouseRelease
  prepareScroll
end mouseRelease

command prepareScroll
  put false into allowDrag
  put 4 into xMultiplier
  put 1700 into xDuration
  if abs(item 1 of scrollChunks) < 100 then
    put 3 into xMultiplier
    put 1000 into xDuration
  end if
  put 4 into yMultiplier
  put 1700 into yDuration
  if abs(item 2 of scrollChunks) < 100 then
    put 3 into yMultiplier
    put 1000 into yDuration
  end if
  put max(xDuration,yDuration) into pDuration
  --### put hScroll of me - (xMultiplier * item 1 of scrollChunks),vScroll of me - (xMultiplier * item 2 of scrollChunks) into pDestScroll
  put 0,vScroll of me - (xMultiplier * item 2 of scrollChunks) into pDestScroll
  easeScroll long ID of me,pDestScroll,pDuration
end prepareScroll


local tStartScroll,tDistance,tStartTime,tEaseID,bounceBackBusy

command easeScroll pObj,pDestScroll,pDuration
  # CANCEL ANY EXISTING EASE MOVEMENT
  --### cancel item 1 of line lineOffset("runEase",pendingMessages()) of pendingMessages()
  cancelEaseMsgs
  put empty into tEaseID
  put false into bounceBackBusy
  # BEGIN EASE ROUTINE
  runEase pObj,pDestScroll,pDuration
end easeScroll

on cancelEaseMsgs --###jg
  repeat for each line l in the pendingmessages
    if l contains "runease" then cancel item 1 of l
  end repeat
end cancelEaseMsgs

-- NOTE: HOSTOBJ PARAMETER IS ONLY USED LOCALLY
-- AND SHOULD BE EMPTY WHEN ROUTINE IS FIRST RUN
-- TO INITIALIZE EASE MOVEMENT

command runEase pObj,pDestScroll,pDuration,pHostObj
  lock screen
  # INIT VARIABLES
  if pHostObj = "" then
    put item 1 of line 1 of the executionContexts into pHostObj
    put hScroll of pObj,vScroll of pObj into tStartScroll
    put (item 1 of pDestScroll - item 1 of tStartScroll),(item 2 of pDestScroll - item 2 of tStartScroll) into tDistance
    put the millisecs into tStartTime
  end if
  
  # TRACK TIME
  put (the millisecs - tStartTime)/pDuration into phi
  put min(phi,1) into phi
  put (2*phi - phi^2) into tEase # EASE OUT
  
  # MOVE OBJECT
  put 0 into newHScroll  --### put item 1 of tStartScroll + round(tEase * item 1 of tDistance) into newHScroll
  put item 2 of tStartScroll + round(tEase * item 2 of tDistance) into newVScroll
  set hScroll of pObj to newHScroll
  set vScroll of pObj to newVScroll
  unlock screen
  
  # REVERSE DIRECTION (BOUNCEBACK) IF END OF SCROLL IS REACHED
  put hScroll of pObj,vScroll of pObj into currScroll
  put 0 into hBounce
  put 0 into vBounce
  if item 1 of currScroll < minHScroll(pObj) then put 1 into hBounce
  if item 1 of currScroll > maxHScroll(pObj) then put -1 into hBounce
  if item 2 of currScroll < minVScroll(pObj) then put 1 into vBounce
  if item 2 of currScroll > maxVScroll(pObj) then put -1 into vBounce
  if (hBounce <> 0 or vBounce <> 0) and not bounceBackBusy then
    send "bounceBack pObj,hBounce,vBounce" to me in 10 millisecs
    exit runEase
  end if
  
  # EXIT WHEN OBJECT REACHES ITS DESTINATION
  if ((newHScroll,newVScroll) = pDestScroll) or (phi >= 1) then
    put empty into tEaseID
    put false into bounceBackBusy
    --    try
    --      send "easeScrollDone pObj" to pHostObj
    --    end try
    exit runEase
  end if
  
  # LOOP SCRIPT
  send "runEase pObj,pDestScroll,pDuration,pHostObj" to me in 10 milliseconds
  put the result into tEaseID
end runEase


command bounceBack pObj,hBounce,vBounce
  exit bounceBack  --###jg: no bounce on android
  
  put true into bounceBackBusy
  put hScroll of pObj,vScroll of pObj into pDestScroll
  if hBounce = 1 then put minHScroll(pObj) into item 1 of pDestScroll
  if hBounce = -1 then put maxHScroll(pObj) into item 1 of pDestScroll
  if vBounce = 1 then put minVScroll(pObj) into item 2 of pDestScroll
  if vBounce = -1 then put maxVScroll(pObj) into item 2 of pDestScroll
  put 500 into pDuration
  runEase pObj,pDestScroll,pDuration
end bounceBack

function minHScroll pObj
  return item 1 of marginValues(pObj)
end minHScroll

function maxHScroll pObj
  if formattedWidth of pObj <= width of pObj then return width of pObj
  return (formattedWidth of pObj - item 3 of marginValues(pObj) - width of pObj)
end maxHScroll

function minVScroll pObj
  return item 2 of marginValues(pObj)
end minVScroll

function maxVScroll pObj
  if formattedHeight of pObj <= height of pObj then return height of pObj
  return (formattedHeight of pObj - item 4 of marginValues(pObj) - height of pObj)
end maxVScroll

function marginValues pObj
  if number of items of margins of pObj > 1 then return margins of pObj
  repeat 4
    put margins of pObj & "," after tValues
  end repeat
  delete last char of tValues
  return tValues
end marginValues

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Setting boundaries on a scrolling field

Post by Simon » Sat Jul 13, 2013 6:30 pm

Hi William,
Seems to me you already have a maxVscroll why don't you set it in there?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply