Back2BASIC is a library that allows you to have all the functions you loved in BASIC. This will help BASIC programmers switch to lua, as well as making it more straightforward to port BASIC programs to lua.
Already implemented
- b2b.menu (a prefabricated menu function)
- b2b.printText (like "HELLO" in BASIC, but with LESS-style page breaks)
To be implemented
- a graphing function
- Locate
Usage
Make a folder called "lib" in your root directory. Put b2b.lua in this folder.
In the program you wish to use the library, put run([[lib\b2b.lua]]) at the top.
Function syntax
NOTE: all width, height, x, and y values are not pixels, but characters.
b2b.menu(x, y, width, height, title, array, color1, color2)
b2b.printText(string, colorfg, colorbg)
Screenshots
See "Known Bugs"
Code
lib\b2b.lua
b2b={} b2b.version="0.1beta" b2b.menu = function (x, y, width, height, title, array, color1, color2) local x=x*18-18 local y=y*18-18 local continue=0 local selected=1 local max=height local j=1 while continue==0 do --handle keys if key==28 then if selected>1 then selected=selected-1 if selected<max-height+1 then max=selected+height-1 end else selected=#array max=#array end end if key==37 then if selected<#array then selected=selected+1 if selected>height then max=selected end else selected=1 max=height end end if key==31 then continue=1 end --draw menu zmg.drawRectFill(x+1, y+18, width*12, height*18, color2) zmg.drawText(x+1, y+1, title, color2, color1) j=1 for i=max-height+1, max, 1 do if selected==i then zmg.drawText(x+1, y+(j*18), array[i], color2, color1) else zmg.drawText(x+1, y+(j*18), array[i], color1, color2) end j=j+1 end --refresh screen zmg.fastCopy() --keyMenu if continue~=1 then key=zmg.keyMenu() end end return selected end b2b.printText = function (string, colorfg, colorbg) local substring={} local k=1 --if string extends past screen end (x) if #string>31 then --split string into screen width sized portions for i=1, math.floor(#string/31)+1, 1 do substring[i] = string.sub(string, i*31-30, i*31) end end --display for j=1, math.floor(#substring/11)+1, 1 do k=1 --clear screen zmg.drawRectFill(0, 0, 384, 216, colorbg) for i=j*11-10, j*11, 1 do if substring[i] then zmg.drawText(1, k*18-18, substring[i], colorfg, colorbg) end k=k+1 end zmg.drawText(1, 198, "Press a key (Page " .. j .. "/" .. math.floor(#substring/11)+1 .. ")", colorbg, colorfg) --refresh zmg.fastCopy() --wait zmg.keyMenu() end end
demo.lua
run([[lib\b2b.lua]]) zmg.clear() test = b2b.menu(1, 1, 15, 5, "Demo", {"display text","entry2","entry3","entry4","entry5","entry","entry","entry","entry"}, zmg.makeColor("blue"), zmg.makeColor("black")) print(test) teststring="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed porttitor, sapien quis sagittis sodales, metus felis faucibus sem, eget mollis erat dolor non elit. Cras id nibh vel massa auctor euismod. Fusce semper rutrum neque, ut faucibus lacus egestas at. Donec velit augue, pulvinar sit amet vulputate et, consectetur iaculis neque. Quisque eu enim eu est condimentum feugiat. Duis gravida ultrices elit ac malesuada. Sed dui sapien, hendrerit nec mattis in, viverra sit amet nibh. Donec vel sodales risus. Duis facilisis cursus placerat. Donec sed ligula sed odio mollis posuere. Integer ante sapien, cursus eu eleifend in, tristique vehicula arcu. Fusce vel erat nibh, non placerat quam. Vivamus quis nibh ut est rhoncus faucibus et vestibulum elit. Pellentesque vel dui eget leo varius faucibus a ac mi. Nulla a nulla non enim euismod ornare a non mauris. Proin ut sagittis turpis. Maecenas in sem tellus, sit amet placerat ligula. Praesent non augue tellus, a convallis lacus. Curabitur suscipit consectetur aliquet. Nunc vehicula lorem in odio accumsan a placerat neque aliquam. Etiam ultricies orci eu justo dapibus vel elementum libero fermentum. Donec et risus nisi, non pulvinar nunc. Duis quis sem neque. Ut eu dignissim nisl. Maecenas a nisl risus, sed consequat ante. Morbi vitae imperdiet erat. Suspendisse potenti. In urna est, viverra id hendrerit vel, pulvinar eget felis. Donec suscipit, dui ac molestie molestie, mi orci scelerisque mi, quis cursus mi metus eget nibh. Aliquam commodo mi at eros sagittis dictum. Nunc ultrices turpis eu urna luctus dictum. Cras sollicitudin ante quis metus aliquet rutrum. Nam vestibulum velit commodo risus ornare eu varius sem placerat. Maecenas egestas odio eu mi tincidunt in porttitor lorem consequat. Proin libero risus, venenatis ut suscipit et, varius non sapien. Suspendisse eget elementum dolor." if test==1 then b2b.printText(teststring, zmg.makeColor("blue"), zmg.makeColor("black")) end
Known Bugs
- All the functions use zmg.keyMenu(), so it should be possible to use Screen Record to record the program. However, if you run them while in ScreenRecord mode, your calc will crash with the "SYSTEM ERROR" message.
- running demo.lua twice in a row gives an error starting with "demo.lua:1:syntax error near <eof>"