gridSnap Function
Marielle Lange
mlange at lexicall.org
Tue Sep 27 16:40:04 EDT 2005
Hi TJ,
Funny, I wrote something similar a week ago, to move a box to a given
day in a calendar display. That looks pretty much equivalent to me.
on MoveDayHiliteBox pLoc
if not within(field "Calendar", pLoc) then exit MoveDayHiliteBox
put 20 into tXGrid --- This is the width of a grid cell
put 21 into tYGrid --- This is the height of a grid cell
--- This is to implement a small grid system, such that points
aref forced on a grid
put item 1 of pLoc into tX
put item 2 of pLoc into tY
put (trunc((tX - (tXGrid/2)) / tXGrid) * tXGrid) into tX
put (trunc((tY - (tYGrid/2)) / tYGrid) * tYGrid) into tY
set the topleft of graphic "Hilite" to (tX,tY)
set the visible of graphic "Hilite" to true
end MoveDayHiliteBox
That's true... I have date handling functions in that stack I should
post on the web one of these days... As I think about it, it is now
on the web:
<http://revolution.lexicall.org/wiki/tiki-index.php?
page=RevolutionSnippetDates>
(you can use the "backlinks" drop down menu to get to the snippets page)
Marielle
> Hey everyone,
> Thought I'd contribute a simple function Im using in my current
> project.
> Someone may find it useful.
> Basically it "snaps" the mouseloc passed to it based on a given
> grid size
> similar to countless programs where you have the ability to snap
> objects to
> a grid. You can have non-square grids by using diffrent gridX and
> gridY
> values
> The way I use the function is that when I move objects by user
> interaction
> I just pass the desired location through this function first. for
> example -
> "Set the loc of myObject to checksnap(the mouseloc,10,10)"
> I also refer to a previously declared global variable gAlignGrid,
> that
> stores a boolean value of whether or not the grid is turned on. You
> can just
> eliminate the first line of the function if you dont need it.
> function checkSnap mloc, gridX, gridY
> if not gAlignGrid then return mloc
> put trunc(item 1 of mloc/gridX) into xfactor
> put trunc(item 2 of mloc/gridY) into yfactor
> if item 1 of mloc >= xfactor * gridX + gridX/2 then add 1 to xfactor
> if item 2 of mloc >= yfactor * gridY + gridY/2 then add 1 to yfactor
> return round(xfactor * gridX ) & "," & round(yfactor * gridY)
> end checkSnap
> Feel free to post any enhancements or more efficient methods of
> doing this
> :)
> - TJ
Marielle Lange (PhD), Psycholinguist
Alternative emails: mlange at blueyonder.co.uk, M.Lange at ed.ac.uk
Homepage: http://homepages.lexicall.org/mlange/
Lexicall: http://lexicall.org
Revolution-education: http://revolution.lexicall.org
More information about the use-livecode
mailing list