LuaWindow is moving ahead !

A few month ago, I wrote about LuaWindow, a small package I have been working on that was able to show text in a GTK window from a Lua script within the OpenViBE Lua Stimulator box. Well, I just added an image loading function so that you can display pictures in addition to text.

Installing luawindow 0.2.0 :

  • Download and uncompress the luawindow source archive in the openvibe-externals folder (the code is licensed under the Lesser GPL v2 or any later version).
  • Edit your init_env_command script and add the OpenViBE_external_luawindow project to the build order.
  • Compile OpenViBE.
  • Congratulations !

Using luawindow :

In order to use luawindow, you have to tell lua where the package is. This could be done the same way as in the luasocket tutorial or you could add the following lines at the beginning of your script :

package.cpath = package.cpath..“;../lib/lib?-dynamic.so;../bin/?-dynamic.dll”
luawindow = require(“luawindow”)

You’ll then be able to call the functions of the API :

luawindow.initialize(width, height)

This function creates the luawindow and initializes its size to width x height pixels. If you don’t call this function or omit the width or height parameters, a window with a default size will be created when needed (that is when you’ll print some text)

luawindow.uninitialize()

This function closes the already initialized window.

luawindow.print(id, x, y, text)

This function prints some text in the window. If the window is not created, it gets created after this call. The id parameter allows to partially refresh the displayed information, that is if the same id was used previously, the corresponding text is removed and replaced by the new text. The x and y specifies the position of the text in the window in pixels. The text parameter contains the text to display in the window. It should be noted that any pango markup tag could be used !

luawindow.draw(id, x, y, filename)

This function draws a picture in the window. If the window is not created, it gets created after this call. The id parameter allows to partially refresh the displayed information, that is if the same id was used previously, the corresponding picture is removed and replaced by the new one. The x and y specifies the position of the picture in the window in pixels. The filename parameter points to the file to display in the window. It should be noted that any picture type that is supported by GTK can be used (the is at least .png, .jpg, etc) !

luawindow.clear()

This function removes any existing text in the window.

Sample script :

The following script demonstrates the new feature of luawindow :

package.cpath = package.cpath..“;../lib/lib?-dynamic.so;../bin/?-dynamic.dll”
luawindow = require(“luawindow”)

function process(box)
  local names =
  {
    “openvibe-logo.png”,
    “arkanoid.png”,
    “bomberman.png”,
    “doom.png”,
    “dott.png”,
    “lemmings.png”,
    “mario.png”,
    “megaman.png”,
    “pacman.png”,
    “sonic.png”,
    “space-invaders.png”,
    “worms.png”,
    “zelda.png”,
    }
    local speed = 1
    local i = 1
    local x = 0
    local y = 0
    local dx = speed * 1.13
    local dy = speed * 0.94
    luawindow.initialize(100+192, 133+192)

    while true do
      for j = 1, 16 do
        luawindow.draw(0, x, y, “../share/openvibe-plugins/simple-visualisation/p300-magic-card/”..names[i])

        x = x + dx
        if x < 0 then dx = -dx end
        if x > 100 then dx = -dx end

        y = y + dy
        if y < 0 then dy = -dy end
        if y > 133 then dy = -dy end

        box:sleep()
      end

      i = i+1
      if i > #names then i = 1 end
    end
  end
end

Have fun 😉

Comments are closed.