/
Timing.fs
133 lines (108 loc) · 4.1 KB
/
Timing.fs
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
namespace SharpVG
open System
type Restart =
| Always
| WhenNotActive
| Never
type DurationValue =
| Duration of TimeSpan
| Media
type RepeatCountValue =
| RepeatCount of float
| Indefinite
type RepeatDurationValue =
| RepeatDuration of TimeSpan
| Indefinite
type Repetition =
{
RepeatCount: RepeatCountValue
RepeatDuration: RepeatDurationValue option
}
type FinalState = // fill
| Freeze
| Remove
type Timing =
{
Begin: TimeSpan // TODO: make optional
Duration: DurationValue option
End: TimeSpan option
Minimum: TimeSpan option
Maximum: TimeSpan option
Restart: Restart option
Repetition: Repetition option
FinalState: FinalState option
}
module Timing =
let create b =
{
Begin = b
Duration = None
End = None
Minimum = None
Maximum = None
Restart = None
Repetition = None
FinalState = None
}
let withDuration duration timing =
{ timing with Duration = Some(Duration(duration)) }
let withMediaDuration timing =
{ timing with Duration = Some(DurationValue.Media) }
let withEnd e timing =
{ timing with End = Some(e) }
let withMinimum minimum timing =
{ timing with Minimum = Some(minimum) }
let withMaximum maximum timing =
{ timing with Maximum = Some(maximum) }
let withResart restart timing =
{ timing with Restart = Some(restart) }
let withRepetition repetition timing =
{ timing with Repetition = Some(repetition) }
let withFinalState finalState timing =
{ timing with FinalState = Some(finalState) }
let toAttributes timing =
let timeSpanToString (timeSpan:TimeSpan) =
string timeSpan.TotalSeconds + "s"
let durationToString duration =
match duration with
| Duration(d) -> d |> timeSpanToString
| Media -> "media"
let repetitionToAttributes repetition =
let repetitionCountToAttribute repetitionCount =
Attribute.createXML "repeatCount"
(
match repetitionCount with
| RepeatCount c -> string c
| RepeatCountValue.Indefinite -> "indefinite"
)
let repetitionDurationToAttribute repetitionDuration =
Attribute.createXML "repeatDuration"
(
match repetitionDuration with
| RepeatDuration c -> c |> timeSpanToString
| RepeatDurationValue.Indefinite -> "indefinite"
)
match repetition with
| None -> []
| Some {RepeatCount = c; RepeatDuration = None} -> [repetitionCountToAttribute c]
| Some {RepeatCount = c; RepeatDuration = Some(d)} -> [repetitionCountToAttribute c; repetitionDurationToAttribute d]
let finalStateToString finalState =
match finalState with
| Freeze -> "freeze"
| Remove -> "remove"
let restartToString restart =
match restart with
| Always -> "always"
| WhenNotActive -> "whennotactive"
| Never -> "never"
[
Some (Attribute.createXML "begin" (timing.Begin |> timeSpanToString))
timing.Duration |> Option.map (durationToString >> Attribute.createXML "dur")
timing.End |> Option.map (timeSpanToString >> Attribute.createXML "end")
timing.Minimum |> Option.map (timeSpanToString >> Attribute.createXML "min")
timing.Maximum |> Option.map (timeSpanToString >> Attribute.createXML "max")
timing.Restart |> Option.map (restartToString >> Attribute.createXML "restart")
timing.FinalState |> Option.map (finalStateToString >> Attribute.createXML "fill")
]
|> List.choose id
|> List.append (repetitionToAttributes timing.Repetition)