LoopCrement Utility Function
Posted: Tue May 08, 2012 10:41 am
This is a handy little thing that I have in python and wanted to reipmlement in tlisp, plus a couple functions I used to test it:
Um, yeah, what's it good for: It's good for any time you have, say, a list of items, and you want to let the user page through them with looping in either direction and with the option to move more than one item at a time. So, a hypothetical setup -- this is totally ass pull pseudocode at the moment, I know screens don't look like this but I don't have the format handy.
I got the idea to reimplement this function when I saw TransGeek doing something like this with his inventory categories mod, and doing some clunky loop-end checkings in the up/down actions for it.
Code: Select all
<Globals>(block nil
(setq loopCrement (lambda (current start stop increment) (block (new)
(setq new (add current increment))
(switch
(and (gr increment 0) (gr new stop))
(setq new (add start (subtract new stop) -1 ))
(and (ls increment 0) (ls new start))
(setq new (add 1 (subtract stop (subtract start new))))
)
new
)))
(setq lctest (lambda () (block nil
(setq x 5)
(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 1))))
(dbgOutput "-----------")
(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 -1))))
)))
(setq lctest2 (lambda () (block nil
(setq x 5)
(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 3))))
(dbgOutput "-----------")
(for i 1 10 (dbgOutput (setq x (loopCrement x 5 12 -3))))
)))
)</Globals>
Code: Select all
<init screen>
(setq pages (list 'page1 'page2 'page3 'page4 'page5))
(setq curpage 0)
</init screen>
<init pane>
(setPaneDesc gScreen (cat "Currently viewing " (item pages curpage) "."))
</init pane>
<action next>
(setq curpage (loopCrement curpage 0 (count pages) 1)
</action next>
<action previous>
(setq curpage (loopCrement curpage 0 (count pages) -1)
</action previous>
<action pagedown>
(setq curpage (loopCrement curpage 0 (count pages) 5)
</action pagedown>
<action pageup>
(setq curpage (loopCrement curpage 0 (count pages) -5)
</action pageup>
I got the idea to reimplement this function when I saw TransGeek doing something like this with his inventory categories mod, and doing some clunky loop-end checkings in the up/down actions for it.