
FreeBASIC's Official Forums
|
| View previous topic :: View next topic |
| Author |
Message |
|
|
Posted: Dec 12, 2009 13:12 Post subject: Getting input from user using a simple GUI (IUP/LUA/FB) |
|
|
As an example of interfacing FreeBASIC with LUA/IUP I've put together
an example that:
- shows an input screen with an OK and a Cancel button;
This input screen is a dialog with some controls on it (edit - fields,
check boxes, sliders etc...);
- displays the values entered by the user (using Print statements).
You'll need some external files to get the example going. I've packed them
all into one package downloadable. You can get that package here:
http://www.sarmardar.nl/FreeBASIC/fb_lua_iup.zip
In the package you'll find
- A slightly updated version of lua.bi and laux.bi. Unpack these
to FreeBASIC/inc.
- The example (lua_test.bas and test_gui.lua).
- The latest version of the lua interpreter (iup5.1.a). Unpack this
to FreeBASIC/lib/win32.
- A bunch of dlls' needed to get the GUI going. It's a copy of the
files I'm using when combining IUP/LUA/FreeBASIC. Unpack these to
some place where the FreeBASIC compiler can find them.
Time for some code. First of the gui. It's written in LUA using IUP
(IUP = a GUI library)
| Code:
|
|
-- IupGetParam Example in IupLua -- Shows a dialog With all possible fields.
require( "iuplua" ) require( "iupluacontrols" )
-- set initial values pboolean = 1 pinteger = 3456 preal = 3.543 pinteger2 = 192 preal2 = 0.5 pangle = 90 pstring = "string text" plist = 2 pstring2 = "second text\nsecond line" cancel = 0
iup.SetLanguage("ENGLISH")
Function param_action(dialog, param_index) If (param_index == -3) Then cancel = 1 End Return 1 End
ret, pboolean, pinteger, preal, pinteger2, preal2, pangle, pstring, plist, pstring2 = iup.GetParam("LUA/IUP Gui Example", param_action, "Boolean: %b\n".. "Integer: %i\n".. "Real 1: %r\n".. "Sep1 %t\n".. "Integer: %i[0,255]\n".. "Real 2: %r[-1.5,1.5]\n".. "Sep2 %t\n".. "Angle: %a[0,360]\n".. "String: %s\n".. "List: %l|item1|item2|item3|\n".. "Sep3 %t\n".. "Multiline: %m\n", pboolean, pinteger, preal, pinteger2, preal2, pangle, pstring, plist, pstring2) If (ret == 0) Then Return -1 End
If (Not iup.MainLoopLevel Or iup.MainLoopLevel()==0) Then iup.MainLoop() End
|
As you can see there is not much to the GUI. Just a bunch of variables,
a call to a function called GetParam and a GUI loop. LUA looks a bit like
FreeBASIC mixed with C.
A picture of the resulting gui:
Now for the FreeBASIC part of the example.
| Code:
|
|
#include "lua/lauxlib.bi"
Sub main()
Dim As lua_State Ptr L Dim As Integer pboolean Dim As Integer pinteger Dim As Double preal Dim As Integer pinteger2 Dim As Double preal2 Dim As Double pangle Dim As Zstring Ptr pstring Dim As Integer plist Dim As Zstring Ptr pstring2 L = luaL_newstate() luaL_openlibs(L) Var status = luaL_loadfile(L,@"test_gui.lua") Var result = lua_pcall(L,0,1,0) If (result <> 0 ) Then If (result = LUA_ERRRUN) Then Print "LUA script failed: IUP dynamic link libraries not installed or unreachable" Else Print "Error executing test_gui.lua" End If lua_close(L) Return Else lua_getfield(L,LUA_GLOBALSINDEX,@"cancel") Var cancel = lua_tonumber(L,-1) If (cancel = 1) Then Print "The user pressed cancel" lua_pop(L,1) lua_close(L) Return Else Print "Returned values: " lua_getfield(L,LUA_GLOBALSINDEX,@"pboolean") pboolean = lua_tonumber(L,-1) Print "Boolean = ";pboolean lua_getfield(L,LUA_GLOBALSINDEX,@"pinteger") pinteger = lua_tonumber(L,-1) Print "Integer 1 = ";pinteger lua_getfield(L,LUA_GLOBALSINDEX,@"preal") preal = lua_tonumber(L,-1) Print "Real 1 = ";preal lua_getfield(L,LUA_GLOBALSINDEX,@"pinteger2") pinteger2 = lua_tonumber(L,-1) Print "Integer 2 = ";pinteger2 lua_getfield(L,LUA_GLOBALSINDEX,@"preal2") preal2 = lua_tonumber(L,-1) Print "Real 2 = ";preal2 lua_getfield(L,LUA_GLOBALSINDEX,@"pangle") pangle = lua_tonumber(L,-1) Print "Angle = ";pangle lua_getfield(L,LUA_GLOBALSINDEX,@"pstring") pstring = lua_tostring(L,-1) Print "String = ";*pstring lua_getfield(L,LUA_GLOBALSINDEX,@"plist") plist = lua_tonumber(L,-1) Print "List = ";plist lua_getfield(L,LUA_GLOBALSINDEX,@"pstring2") pstring2 = lua_tostring(L,-1) Print "String 2 = ";*pstring2 lua_pop(L,10) lua_close(L) Print "String 2 = ";*pstring2 End If Return End If Return End Sub
main()
|
FreeBASIC communicates with LUA by using a stack. To get the value of
a variable defined in the LUA script LUA pushes the value of the
variable onto a stack. The FB program uses lua_getfield to instruct
LUA to push the value of a variable onto the stack.
After a value has been pushed FreeBASIC can get the value from the
stack by using lua_tonumber, lua_tostring etc...
Getting the value from the stack does not pop the value from
the stack. For example, lua_tostring(L,-1) means "get the value at the top
of the stack and return it as a string". -1 = 'top of the stack'
(-2 = top of the stack - 1, -3 = top of the stack - 2 etc... ).
After the program is done pushing values to the stack it pops all the values
from the stack again using lua_pop (the second parameter of lua_pop denotes
the number of values that need to be popped from the stack).
Apart from the stack operations the example is an easy way to get
some input from an user.
And it's cross - platform as well (WIN/LIN/UNIX).
If you want to use the above example using Linux go to
http://sourceforge.net/projects/iup/files/
and pick the version of your choice (there are files for linux using the
'old' kernel (2.4) and Linux using the 'new' kernel (2.6).)
A recent version of LUA should be available for download from the package
repository of the Linux distro you are using. IUP is in the Debian package
repository so I'm guessing it's available for Ubuntu (and other Linux distributions)
as well.
Buon fine settimana :) |
| |
|
| Back to top |
|
 |
|
|
Posted: Dec 19, 2009 3:15 Post subject: |
|
|
didnt work for me
(the iup.dll missing so i down the iup but still didnt work) |
| |
|
| Back to top |
|
 |
|
|
Posted: Dec 19, 2009 15:15 Post subject: |
|
|
| JaDogg wrote: | didnt work for me
(the iup.dll missing so i down the iup but still didnt work) |
Problem with experimenting with external libraries (and posting examples like the IUP gui) is getting the right number/kind of libraries into the zip so the example will run (getting the dependencies right).
The program should not need iup.dll. It uses a static build of iup. The library it uses should be liblua5.1.a and it should be in lib/win32/ (in the FreeBASIC install directory). The lua.bi that's in inc/lua/ should have the statement
in it. Getting liblua5.1.a in the right place might not solve the problem.
Perhaps removing the old libraries (after you've backed them up somewhere? ) will help.
Don't know why the example will not run on your PC. But the GUI looks great, doesn't it ;)?
If you get the example to work please post a message on what what you did to get the example to work so I can get things right next time I post a LUA/IUP example.
(P.S. I haven't tested the example on Linux) |
| |
|
| Back to top |
|
 |
|
|
Posted: Dec 19, 2009 18:37 Post subject: |
|
|
Several years ago I did a semi-fb interpreter - basically lua with most of fb's graphics and input command exposed, and it included a very basic script editor using IUP for the gui. It's been a long time since I've even looked at the code and it doesn't seem to be working anymore, but here it is if you're interested:
http://www.file-pasta.com/file/FBlua.rar
(the luaplayerwindows folder has a very old version of the windows port of the PSP luaplayer, which was actually what got me back into programming, and which is why the file is so large) |
| |
|
| Back to top |
|
 |
|
|
Posted: Dec 25, 2009 8:37 Post subject: |
|
|
| Merick wrote: | Several years ago I did a semi-fb interpreter - basically lua with most of fb's graphics and input command exposed, and it included a very basic script editor using IUP for the gui. It's been a long time since I've even looked at the code and it doesn't seem to be working anymore, but here it is if you're interested:
http://www.file-pasta.com/file/FBlua.rar
(the luaplayerwindows folder has a very old version of the windows port of the PSP luaplayer, which was actually what got me back into programming, and which is why the file is so large) |
That's... impressive, Merick. Very nice piece of coding(very impressive!).
Thanks for the code. |
| |
|
| Back to top |
|
 |
|
|
Posted: Dec 26, 2009 1:05 Post subject: |
|
|
Yah, I was thinking of trying to update it and linking with the newest version of the static libs for Lua and IUP to get rid of the dll's (and getting rid of the luaplayer stuff since I don't actually have a psp anymore, and anyway the luaplayer stuff isn't mine in the first place) but I haven't had much energy to put into coding lately.
You might want to take a look at lua_macros.bi as it has a couple macros (stolen and converted from the C luaplayer source actually) to make it easier to combine FB procedures into Lua modules, and another that uses Yetifoot's incfile macro to compile lua scripts into the exe |
| |
|
| Back to top |
|
 |
|
|
Posted: Aug 18, 2010 3:17 Post subject: |
|
|
wow thank you for that information you given..
btw im new in this forum..
thanks for letting me in..
have a nice day..
___________________________
Spam |
| |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|