-
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
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-- Copyright 2026 Shota FUJI
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
--
-- SPDX-License-Identifier: MPL-2.0
module Parameters exposing (..)
import Dict exposing (Dict)
import Length exposing (Length, mm, toMM)
import Parameters.Key as Key exposing (Key(..))
type TipShape
= Round
type alias BuckleHole =
-- Offset from the case side (lug side.)
{ offset : Length
-- Number of buckle holes, can be 0.
, count : Int
-- Center-to-center.
, interval : Length
-- TODO: Support non circular holes.
, diameter : Length
}
{-| Parameters for the long piece, which has buckle holes.
This does not have to be longer than `ShortPiece`.
-}
type alias LongPiece =
{ length : Length
, tip : TipShape
, buckleHole : BuckleHole
, flap : Length
}
type alias FixedLoop =
{ width : Length
, play : Length
, offset : Length
}
type alias FreeLoop =
{ width : Length
, play : Length
, overlap : Length
}
type LoopStyle
= Simple
| Folded
loopStyleToString : LoopStyle -> String
loopStyleToString style =
case style of
Simple ->
"simple"
Folded ->
"folded"
type alias Loops =
{ fixed : Maybe FixedLoop
, free : Maybe FreeLoop
, style : LoopStyle
, thickness : Length
}
type alias BuckleTongue =
{ thickness : Length
, width : Length
}
{-| Clasp / Buckle.
-}
type alias Buckle =
{ springBarDiameter : Length
, tongue : Maybe BuckleTongue
}
{-| Parameters for the short piece, which has clasp at the end.
This does not have to be shorter than `LongPiece`.
-}
type alias ShortPiece =
{ length : Length
, loops : Loops
, caseSideFlap : Length
, claspSideFlap : Length
, buckle : Buckle
}
type CanvasSize
= A4
canvasSizeDimension : CanvasSize -> ( Length, Length )
canvasSizeDimension size =
case size of
A4 ->
( mm 210, mm 297 )
type ColorSchema
= BlackOnWhite
| WhiteOnBlack
colorSchemaToString : ColorSchema -> String
colorSchemaToString schema =
case schema of
BlackOnWhite ->
"black-on-white"
WhiteOnBlack ->
"white-on-black"
type alias Rendering =
{ size : CanvasSize
, margin : Length
, gap : Length
, lineWidth : Length
, colorSchema : ColorSchema
, qrCode : Bool
}
type alias Parameters =
{ lugWidth : Length
, longPiece : LongPiece
, shortPiece : ShortPiece
, thickness : Length
, lining : Length
, taper : Length
, rendering : Rendering
}
defaultFixedLoop : FixedLoop
defaultFixedLoop =
{ offset = mm 8
, width = mm 6
, play = mm 1
}
defaultFreeLoop : FreeLoop
defaultFreeLoop =
{ width = mm 5
, play = mm 2
, overlap = mm 5
}
defaultBuckleTongue : BuckleTongue
defaultBuckleTongue =
{ thickness = mm 2
, width = mm 2.6
}
default : Parameters
default =
{ lugWidth = mm 20
, longPiece =
{ length = mm 112
, tip = Round
, buckleHole =
{ offset = mm 50
, count = 7
, interval = mm 6
, diameter = mm 2
}
, flap = mm 15
}
, shortPiece =
{ length = mm 80
, loops =
{ fixed = Just defaultFixedLoop
, free = Just defaultFreeLoop
, style = Simple
, thickness = mm 1.0
}
, caseSideFlap = mm 15
, claspSideFlap = mm 20
, buckle =
{ springBarDiameter = mm 1.4
, tongue = Just defaultBuckleTongue
}
}
, thickness = mm 2.5
, lining = mm 0.5
, taper = mm 0
, rendering =
{ size = A4
, margin = mm 3
, gap = mm 10
, lineWidth = mm 0.3
, colorSchema = BlackOnWhite
, qrCode = False
}
}
type alias ParametersDict =
Dict String String
toDict : Parameters -> ParametersDict
toDict params =
[ ( "version", "1.0" )
, ( Key.toString LugWidth, String.fromFloat (toMM params.lugWidth) )
, ( Key.toString SurfaceThickness, String.fromFloat (toMM params.thickness) )
, ( Key.toString LiningThickness, String.fromFloat (toMM params.lining) )
, ( Key.toString LongPieceLength, String.fromFloat (toMM params.longPiece.length) )
, ( Key.toString LoopThickness, String.fromFloat (toMM params.shortPiece.loops.thickness) )
, ( Key.toString LoopStyle, loopStyleToString params.shortPiece.loops.style )
, ( Key.toString BuckleSpringBarDiameter, String.fromFloat (toMM params.shortPiece.buckle.springBarDiameter) )
, ( Key.toString ShortPieceLength, String.fromFloat (toMM params.shortPiece.length) )
, ( Key.toString CanvasMargin, String.fromFloat (toMM params.rendering.margin) )
, ( Key.toString LineWidth, String.fromFloat (toMM params.rendering.lineWidth) )
, ( Key.toString ColorSchema, colorSchemaToString params.rendering.colorSchema )
, ( Key.toString BuckleHoleCount, String.fromInt params.longPiece.buckleHole.count )
, ( Key.toString BuckleHoleOffset, String.fromFloat (toMM params.longPiece.buckleHole.offset) )
, ( Key.toString BuckleHoleInterval, String.fromFloat (toMM params.longPiece.buckleHole.interval) )
]
|> (++)
(if toMM params.taper > 0 then
[ ( Key.toString Taper, String.fromFloat (toMM params.taper) ) ]
else
[]
)
|> (++)
(if params.longPiece.buckleHole.count > 0 then
[ ( Key.toString BuckleHoleDiameter, String.fromFloat (toMM params.longPiece.buckleHole.diameter) )
]
else
[]
)
|> (++)
(case params.shortPiece.loops.fixed of
Just fixed ->
[ ( Key.toString HasFixedLoop, "" )
, ( Key.toString FixedLoopWidth, String.fromFloat (toMM fixed.width) )
, ( Key.toString FixedLoopPlay, String.fromFloat (toMM fixed.play) )
]
Nothing ->
[]
)
|> (++)
(case params.shortPiece.loops.free of
Just free ->
[ ( Key.toString HasFreeLoop, "" )
, ( Key.toString FreeLoopWidth, String.fromFloat (toMM free.width) )
, ( Key.toString FreeLoopPlay, String.fromFloat (toMM free.play) )
, ( Key.toString FreeLoopOverlap, String.fromFloat (toMM free.overlap) )
]
Nothing ->
[]
)
|> (++)
(case params.shortPiece.buckle.tongue of
Just tongue ->
[ ( Key.toString HasBuckleTongue, "" )
, ( Key.toString BuckleTongueThickness, String.fromFloat (toMM tongue.thickness) )
, ( Key.toString BuckleTongueWidth, String.fromFloat (toMM tongue.width) )
]
Nothing ->
[]
)
|> (++)
(if params.rendering.qrCode then
[ ( Key.toString QRCode, "" ) ]
else
[]
)
|> Dict.fromList
{-| Fields under `Maybe` record may be empty when the parsed parent is
`Nothing`. These values sets default values for those fields.
-}
fallbackValues : ParametersDict
fallbackValues =
[ ( Key.toString Taper, "0" )
, ( Key.toString FixedLoopWidth, String.fromFloat (toMM defaultFixedLoop.width) )
, ( Key.toString FixedLoopPlay, String.fromFloat (toMM defaultFixedLoop.play) )
, ( Key.toString FreeLoopWidth, String.fromFloat (toMM defaultFreeLoop.width) )
, ( Key.toString FreeLoopPlay, String.fromFloat (toMM defaultFreeLoop.play) )
, ( Key.toString FreeLoopOverlap, String.fromFloat (toMM defaultFreeLoop.overlap) )
, ( Key.toString BuckleTongueThickness, String.fromFloat (toMM defaultBuckleTongue.thickness) )
, ( Key.toString BuckleTongueWidth, String.fromFloat (toMM defaultBuckleTongue.width) )
, ( Key.toString BuckleHoleDiameter, String.fromFloat (toMM default.longPiece.buckleHole.diameter) )
]
|> Dict.fromList
getKey : Key -> ParametersDict -> Maybe String
getKey key fields =
Dict.get (Key.toString key) fields
hasKey : Key -> ParametersDict -> Bool
hasKey key fields =
not (getKey key fields == Nothing)