% engine=luatex

%D \module
%D   [      file=s-pre-71,
%D        version=2008.08.05,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Presentation Environment 71,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D I might improve this module so consider it experimental.

% \enablemode[numbers]
% \enablemode[paper]

\usemodule[pre-60,abr-02]

\setupinteraction[state=start,click=off]

\definepapersize[wide][width=900pt,height=600pt]

\setuppapersize[wide][wide]

\setuplayout[page]

% \setupbodyfont[15pt]

\usetypescriptfile[type-hgz]
\usetypescript[palatino-informal]
\setupbodyfont[palatino-informal,15pt]

\setupsorting[logo][style=]

\startnotmode[paper]
    \setupbackgrounds[page][background=color,backgroundcolor=black]
\stopnotmode

\TransparencyHack

\definelayer[page][width=\paperwidth,height=\paperheight]

\definecolor[TopicColor-1][r=.3,g=.4,b=.5]
\definecolor[TopicColor-2][r=.3,g=.5,b=.4]
\definecolor[TopicColor-3][r=.4,g=.3,b=.5]
\definecolor[TopicColor-4][r=.4,g=.5,b=.3]
\definecolor[TopicColor-5][r=.5,g=.3,b=.4]
\definecolor[TopicColor-6][r=.5,g=.4,b=.3]
\definecolor[TopicColor-7][r=.35,g=.35,b=.6]
\definecolor[TopicColor-8][r=.6,g=.35,b=.35]
\definecolor[TopicColor-9][r=.35,g=.6,b=.35]

\definecolor[TopicColor-0][t=.5,a=1,s=.5]
\definecolor[TopicColor]  [s=1]

\setupcolors[state=start]
\setupcolors[textcolor=TopicColor]

\startluacode
    local locations = {
        'lefttop',
        'middletop',
        'righttop',
        'middleleft',
        'middle',
        'middleright',
        'leftbottom',
        'middlebottom',
        'rightbottom',
    }
    local done, current, previous, n = table.tohash(locations,false), 0, 0, 0
    function document.reset_locations()
        done, current, previous, n = table.tohash(locations,false), 0, 0, 0
    end
    function document.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 document.current_location()
        tex.print(locations[current] or "")
    end
    function document.previous_location()
        tex.print(locations[previous] or "")
    end
    function document.current_n()
        tex.print(tostring(current))
    end
    function document.previous_n()
        tex.print(tostring(previous))
    end
    function document.step()
        tex.print(tostring(n))
    end
\stopluacode

\def\StartTopics
  {\startstandardmakeup
   \ctxlua{document.reset_locations()}
   \doifnotmode{paper}{\StartLocalSteps}}

\def\StopTopics
  {\doifnotmode{paper}{\StopLocalSteps}
   \flushlayer[page]
   \stopstandardmakeup}

\def\StartTopic
  {\dosingleempty\doStartTopic}

\def\doStartTopic[#1]%
  {\doifnotmode{paper}{\NextStep}
   \ctxlua{document.next_location("#1")}
   \startnotmode[paper]
     \doifnothing{#1}
       {\ifcase\ctxlua{document.previous_n()}\else
          \setlayer
            [page]
            [preset=\ctxlua{document.previous_location()}]
             \bgroup
               \doifnotmode{paper}{\startproperty[\StepLayer]}%
               \framed
                 [offset=20pt,
                  strut=no,
                  align=normal,
                  frame=off,
                  height=\dimexpr\paperheight/3\relax,
                  width=\dimexpr\paperwidth/3\relax,
                  background=color,
                  backgroundcolor=TopicColor-0]
                 {}%
               \doifnotmode{paper}{\stopproperty}%
             \egroup
         \fi}
   \stopnotmode
   \setlayer
     [page]
     [preset=\ctxlua{document.current_location()}]
     \bgroup
       \doifnotmode{paper}{\startproperty[\StepLayer]}%
       \framed
         [offset=20pt,
          strut=no,
          align=\expdoifelse{#1}{}{normal}{middle,lohi},
          align=\expdoifelse{#1}{}{flushleft,verytolerant}{middle,lohi},
          frame=off,
          height=\dimexpr\paperheight/3\relax,
          width=\dimexpr\paperwidth/3\relax,
          background=color,
          backgroundcolor=TopicColor-\ctxlua{document.current_n()}]
         \bgroup
         \ignorespaces}

\def\StopTopic
  {\removeunwantedspaces
   \egroup
   \doifnotmode{paper}{\stopproperty}%
   \egroup
   \startmode[numbers]
   \setlayerframed
     [page]
     [preset=\ctxlua{document.current_location()}]
     [height=\dimexpr\paperheight/3\relax,
      width=\dimexpr\paperwidth/3\relax,
      frame=off,
      foregroundstyle=\bfa,
      align={flushright,low}]
     {\doifnotmode{paper}{\startproperty[\StepLayer]}%
      \ctxlua{document.step()}\kern\strutdepth
      \doifnotmode{paper}{\stopproperty}}
   \stopmode}

\logo [METAPOST] {MetaPost}

\definefont[TitleFont][SansBold at 60pt]
\definefont[TempFont] [SansBold at 12pt]

\doifnotmode{demo}{\endinput}

\starttext

\StartTopics
    \StartTopic[1] A \StopTopic
    \StartTopic[5] B \StopTopic
    \StartTopic[9] C \StopTopic
\StopTopics

\StartTopics
    \StartTopic A \StopTopic
    \StartTopic B \StopTopic
    \StartTopic C \StopTopic
    \StartTopic D \StopTopic
    \StartTopic E \StopTopic
    \StartTopic F \StopTopic
    \StartTopic G \StopTopic
    \StartTopic H \StopTopic
    \StartTopic I \StopTopic
\StopTopics

\stoptext
