Omnimaga
Calculator Community => Casio Calculators => Topic started by: flyingfisch on March 06, 2013, 12:59:46 pm
-
What it is
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>"
-
This is interesting. I think that if the syntax is very similar to TI-BASIC or Casio BASIC, it might be popular eventually, because some people tend to dislike having to learn a totally new language. This would also be a great alternative to the insanely slow PRIZM drawing commands.
-
This is interesting. I think that if the syntax is very similar to TI-BASIC or Casio BASIC, it might be popular eventually, because some people tend to dislike having to learn a totally new language. This would also be a great alternative to the insanely slow PRIZM drawing commands.
Well, I suppose I could make the syntax the same, but I sort of like the customizability I have right now.
like in BASIC:
menu "TITLE","ENTRY1",1,"ENTRY2",2
with b2b:
var = menu(1, 1, 10, 5, "TITLE", {"Entry1", "Entry2"}, zmg.makeColor("blue"), zmg.makeColor("white"))
I guess I could change the syntax to this if there was a demand for it:
var = menu("TITLE",{"Entry1", "Entry2"})
-
Ah ok, well I meant not changing it too drastically to the point where it's just like a computer language that isn't any easier than LuaZM itself. More customizability would be nice, though, as above. What I like in some TI-BASIC commands is how new color arguments are optional, so porting programs is easy.
-
I am trying to decide whether or not to try to add goto... what do you think?
-
It might be a good idea for compatibility and for certain programs, but some people tend to consider using Goto/Lbl as bad practice. It could be useful in some cases like sub-routines I guess.
-
... but some people tend to consider using Goto/Lbl as bad practice.
I know... I don't really like giving people that functionality, but I'll wait and see. If people want it i will implement it.
-
OK, I made a git repo (http://flyingfisch.github.com/Back2BASIC/) for this, and added a couple functions (read the readme).
-
Cool to hear. :) And yes it's always good to back up often :P (even if not programming on-calc, people lose stuff due to hard drive failures x.x)
-
(even if not programming on-calc, people lose stuff due to hard drive failures x.x)
That happened to a friend of mine and I don't really want to learn from experience, so.
Also, about goto, i found that it was already implemented in lua 5, so we're good.
So far, here are all the functions:
- b2b.menu (a prefabricated menu function)
- b2b.printText (like "HELLO" in BASIC, but with LESS-style page breaks)
- b2b.locate (like locate in BASIC)
- b2b.ygraph (graphs a y= graph)
And I plan to make a string input function today, so stay tuned! :)
-
Is printText like Disp on TI calcs (displaying text then moving to next line, scrolling if needed)?
-
Is printText like Disp on TI calcs (displaying text then moving to next line, scrolling if needed)?
Nope. It displays a page, and waits for a key until the next page is shown.
also...
UPDATE:
Added b2b.inputString()
-
OK, people, I have run out of ideas temporarily!
Any features that are missing in lua that you had in BASIC? Please ideas in this thread. :)
-
I sadly have no clue about Lua since I never coded it, but does it feature floating points, the ability to copy a flash program to RAM and delete RAM programs?
-
I sadly have no clue about Lua since I never coded it, but does it feature floating points, the ability to copy a flash program to RAM and delete RAM programs?
Hmm... I think you are thinking about TI calculators.
LuaZM programs are run from the 16MB storage memory.
And yes, lua does support floating points.
-
Oh wait I didn't know LuaZM programs ran directly from archive.
-
Yup, they do :)
-
UPDATE:
Fixed the blinkrate on b2b.inputString()
-
Cool to hear about new updates. :)
Also I wonder if this should be moved to Casio PRIZM projects for more attention?
-
I think I may start a gfx lib pretty soon... I am going to try making a drawSpriteMono routine. it would look something like this:
sprite="11000011000110001100001101111110"
drawSpriteMono(x,y,fgcolor,bgcolor,resolution (optional))
either bgcolor or fgcolor could be set to 0 (transparent).
Also I wonder if this should be moved to Casio PRIZM projects for more attention?
Sounds good to me. :)