lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
I am writing a small lua module and after some struggling I can only
manage to require it like:

  local canvas = require 'canvas'

But I can *not* require it like plain:

  require 'canvas'

The problem is, the module is a table *and* an 'instance' of a
'class'. So after 'require', user must access
a table *and* an instance. It can then use this instance to create
more 'brother' instances.
This behavior is defined here:

http://www.lua.inf.puc-rio.br/~francisco/nclua/referencia/canvas.html

Here is part of the module code (just the show my problem):

---------
---------
local pairs, print, setmetatable, unpack, type, error, select = pairs,
print, setmetatable, unpack, type, error, select

module('canvas')

-- Canvas declaration
local Canvas = {}
Canvas.__index = Canvas

Canvas.colorTable = {}
Canvas.colorTable.blue = {0, 0, 255}
Canvas.colorTable.red = {255, 0, 0}
Canvas.colorTable.green = {0, 255, 0}
Canvas.colorTable.black = {0, 0, 0}
Canvas.colorTable.white = {255, 255, 255}

function Canvas.newcanvas(surface)
	local canvas = {}
	canvas.surface = surface
	setmetatable(canvas, Canvas)
	return canvas
end

function Canvas:new(...)
	local argType = type(...)
	local surface

        -- stuff

	return Canvas.newcanvas(surface)
end

function Canvas:flush()
end

-- Create base instance
return Canvas.newcanvas(otherModule:CreateSurface
{caps='DSCAPS_PRIMARY|DSCAPS_FLIPPING'})