Wiki
Clone wikiCore / parameter
Back to Beyond the Codea in-app reference
parameter table
Introduction
Before Codea version 1.5, parameter
referred to a function. From version 1.5, it refers to a table of functions. Calling parameter
as a function is deprecated and generates a warning message in the Output pane.
The 'Parameters' chapter of Codea's in-app reference documents the parameter
functions.
parameter.number() constructors
When parameter.number()
is called with two arguments, the second is interpreted as the maximum value of the slider and the minimum value defaults to 0
. For example:
parameter.number("parameter1", 20) -- Slider running from 0 to 20
parameter.number("parameter2", -20) -- Slider stuck at 0
Resetting previously set parameters
As documented in the in-app reference, all previously set parameters can be cleared using the parameter.clear()
function. Once cleared, parameters can then be re-established, if required.
The example code below illustrates how the functionality of parameter()
, iparameter()
and clearParameters()
can be extended to cause the extended functions to keep track of what parameters have previously been set and, consequently, allow other parameters to be reset automatically when a previously set parameter is reset.
do
-- Preserve built-in functions
local oldP = parameter
local oldIP = iparameter
local oldCP = clearParameters
local pList = {} -- Will hold the set i/parameters
local pProp = {} -- Will hold their properties
-- Private generic helper
local addP = function(isIParam, var, min, max, init)
local old = oldP
if isIParam then
if init then init = math.modf(init) end
if min then min = math.modf(min) end
if max then max = math.modf(max) end
old = oldIP
end
if not min then
min = 0
max = 1
if isIParam then max = 10 end
end
if not max then
max = math.max(0, min)
min = 0
end
if not pProp[var] then
if not init then init = min end
pProp[var] = {
min = math.min(min, init),
max = math.max(max, init),
isIParam = isIParam}
pList[#pList+1] = var
old(var, min, max, init)
return
end
local p = pProp
local l = pList
p[var] = {
min = min,
max = max,
isIParam = isIParam}
clearParameters()
for i = 1, #l do
local k = l[i]
local v = p[k]
local i = _G[k]
if isIParam then i = math.modf(i) end
if k == var and init then i = init end
if v.isIParam then
iparameter(k, v.min, v.max, i)
else
parameter(k, v.min, v.max, i)
end
end
return
end
-- Redefine parameter()
parameter = function (var, min, max, init)
addP(false, var, min, max, init)
end
-- Redefine iparameter()
iparameter = function (var, min, max, init)
addP(true, var, min, max, init)
end
-- Redefine clearParameters()
clearParameters = function ()
pProp = {}
pList = {}
oldCP()
return
end
end
--
-- An example of the use of the extended functionality above
--
function setup()
parameter("test1", 0, 100, 50)
iparameter("itest1", 0, 100, 50)
parameter("test2", 100, 200, 50)
iparameter("itest2", 100, 200, 50)
parameter("test3") -- A default parameter
iparameter("itest3") -- A default iparameter
end
function draw()
background(40, 40, 50)
end
local hasBeenReset = false
function touched(touch)
if touch.state == BEGAN and not hasBeenReset then
parameter("test2", 0, 300) -- Reset the limits of 'test2'
iparameter("test1", 0, 100) -- Change 'test1' to an iparameter
hasBeenReset = true
end
end
Updated