summaryrefslogtreecommitdiff
path: root/doc/context/examples/clock/clock.tex
blob: 6b70de58512d361e4f1ddbc9371b088e52112fb3 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
%D This example dates from late 2001, and ws probaby made for some \TEX\ related
%D meeting. It's a relative simple example that uses a few function from a general
%D \JAVASCRIPT\ preamble. In fact forcing the \type {FieldStack} code into the
%D is the only adaptation from \MKII\ to \MKIV\ (because we load on demand and here
%D the code is ude sindirectly).
%D
%D Nowadays we could layers instead which is probably a bit more ligthweight than
%D widgets. Also, we should actually synchronize the time but on the other hand, but
%D on the other hand, now it's a sort of stopwatch.

% \nopdfcompression

\starttext

\useJSscripts[fld]

\useJSpreamble[FieldStack]

\definepapersize
  [clock]
  [width=2.5cm,
   height=2.5cm]

\setuppapersize
  [clock][clock]

\setuplayout
  [header=0pt,
   footer=0pt,
   backspace=.25cm,
   topspace=.25cm,
   width=middle,
   height=middle]

\startJSpreamble stepper used now

    var state = 0 ;
    var more  = 0 ;
    var step  = 0 ;

    function step_clock() {
        try {
            if (more == 60) {
                more = 0 ;
                Walk_Field("more") ;
            } else {
            }
            more += 1 ;
            Walk_Field("clock") ;
            this.dirty = false ;
        } catch (e) {
        }
    }

    function start_clock() {
        try {
            if (state == 0) {
                step = app.setInterval ("step_clock()", 1000) ;
                step.count = 0 ;
                state = 1 ;
            } else if (state == 1) {
                app.clearInterval (step) ;
                state = 2 ;
            } else if (state == 2) {
                app.clearInterval (step) ;
                Reset_Fields("more") ;
                Reset_Fields("clock") ;
                Set_Field("more", "1") ;
                Set_Field("clock", "1") ;
                more = 0 ;
                state = 0 ;
            }
        } catch(e) {
        }
    }

    function stop_clock () {
        try {
            app.clearInterval(step) ;
        } catch(e) {
        }
    }

\stopJSpreamble

\definereference[StopClock] [JS(stop_clock)]
\definereference[StartClock][JS(start_clock)]

\setupinteraction
  [state=start,
   closeaction={StopClock,ForgetChanges},
   closepageaction={StopClock}]

\startreusableMPgraphic{common}
    drawoptions(withpen pencircle scaled 1mm withcolor .4white) ;
    fill fullsquare scaled 2.5cm ;
    drawoptions(withpen pencircle scaled 1mm withcolor .6green) ;
    draw fullsquare scaled 2.5cm ;
    drawoptions(withpen pencircle scaled 1mm withcolor .6red) ;
    draw fullcircle scaled 2cm ;
    drawoptions(withpen pencircle scaled 2mm withcolor .6yellow) ;
    for i=1 upto 12 :
        draw (0,1cm) rotated ((i-1)*(360/12)) ;
    endfor ;
\stopreusableMPgraphic

\startuseMPgraphic{clock}
    numeric stp ; stp := (\MPvar{n}-1)*(360/60) ;
    pair p ; p := (0,\MPvar{l}-.5mm) ;
    drawoptions(withpen pencircle scaled 1mm withcolor .6\MPvar{c}) ;
    draw (origin -- p) rotated -stp ;
    draw (p shifted (-2mm,-2.5mm)--p--p shifted (2mm,-2.5mm)) rotated -stp ;
    setbounds currentpicture to fullsquare scaled 2cm ;
    drawoptions(withpen pencircle scaled 2mm withcolor .6white) ;
    draw origin ;
\stopuseMPgraphic

\defineoverlay [common] [\reuseMPgraphic{common}]
\defineoverlay [start]  [\overlaybutton{StartClock}]

\setupbackgrounds
  [page]
  [background={common,start}]

\let\clocklist\empty
\let\morelist \empty

\dorecurse {60} {
    \appendtocommalist{step:#1}\clocklist
    \definesymbol
        [step:#1]
        [\useMPgraphic{clock}{n=#1,l=1cm,c=blue}]
    \appendtocommalist{more:#1}\morelist
    \definesymbol
        [more:#1]
        [\useMPgraphic{clock}{n=#1,l=.75cm,c=green}]
}

\definefieldstack
    [clock]
    [\clocklist]
    [width=2cm,height=2cm,offset=overlay,frame=off]

\definefieldstack
    [more]
    [\morelist]
    [width=2cm,height=2cm,offset=overlay,frame=off]

\starttext

    \startoverlay
        {\fieldstack[more]}
        {\fieldstack[clock]}
    \stopoverlay

\stoptext