Getting input from user using a simple GUI (IUP/LUA/FB)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Getting input from user using a simple GUI (IUP/LUA/FB)

Postby AGS » Dec 12, 2009 13:12

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: Select all

-- 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:

Image


Now for the FreeBASIC part of the example.

Code: Select all

#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 :)
JaDogg
Posts: 344
Joined: Apr 13, 2008 12:11
Location: Sri Lanka - Negombo
Contact:

Postby JaDogg » Dec 19, 2009 3:15

didnt work for me
(the iup.dll missing so i down the iup but still didnt work)
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Postby AGS » Dec 19, 2009 15:15

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

Code: Select all

#inclib "lua5.1"


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)
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Postby Merick » Dec 19, 2009 18:37

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)
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Postby AGS » Dec 25, 2009 8:37

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.
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Postby Merick » Dec 26, 2009 1:05

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
srvaldez
Posts: 1762
Joined: Sep 25, 2005 21:54

Postby srvaldez » Sep 28, 2010 6:03

AGS, I got it to run but the process does not exit when you close dialog box.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 0 guests