summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/s-present-random.lua
blob: f32d7aaea8a9212df2dc2f17c1c177428dd7cc78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
if not modules then modules = { } end modules ['present-random'] = {
    version   = 1.001,
    comment   = "companion to s-present-random.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- For the moment we keep the namespace steps because it can become some
-- shared module some day.

moduledata.steps = moduledata.steps or { }
local steps      = moduledata.steps

local locations = {
    'lefttop',
    'middletop',
    'righttop',
    'middleleft',
    'middle',
    'middleright',
    'leftbottom',
    'middlebottom',
    'rightbottom',
}

local done, current, previous, n

function steps.reset_locations()
    done, current, previous, n = table.tohash(locations,false), 0, 0, 0
end

function steps.next_location(loc)
    previous = current
    n = n + 1
    loc = loc and loc ~= "" and tonumber(loc)
    while true do
        current = loc or math.random(1,#locations)
        if not done[current] then
            done[current] = true
            break
        end
    end
end

function steps.current_location()
    context(locations[current] or "")
end

function steps.previous_location()
    context(locations[previous] or "")
end

function steps.current_n()
    context(current)
end

function steps.previous_n()
    context(previous)
end

function steps.step()
    context(n)
end

steps.reset_locations()