-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiers.go
379 lines (350 loc) · 8.37 KB
/
modifiers.go
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package contraption
import (
"github.com/neputevshina/geom"
"github.com/neputevshina/contraption/nanovgo"
)
func (wo *World) Vfollow() (s Sorm) {
s = wo.beginsorm()
s.tag = tagVfollow
wo.endsorm(s)
return
}
func vfollowrun(wo *World, c, m *Sorm) {
c.aligner = alignerVfollow
}
func (wo *World) Hfollow() (s Sorm) {
s = wo.beginsorm()
s.tag = tagHfollow
wo.endsorm(s)
return
}
func hfollowrun(wo *World, c, m *Sorm) {
c.aligner = alignerHfollow
}
// Halign aligns elements horizontally.
//
// If amt == 0, elements are aligned to the left, if 0.5 to the middle and if 1 to the right.
// Values between those are acceptable.
//
// amt is clipped to the range 0 < amt < 1.
func (wo *World) Halign(amt float64) (s Sorm) {
s = wo.beginsorm()
s.tag = tagHalign
s.Size.X = clamp(0, amt, 1)
wo.endsorm(s)
return
}
func halignrun(wo *World, c, m *Sorm) {
x := 0.0
c.kidsiter(wo, kiargs{}, func(k *Sorm) {
x = max(x, k.Size.X)
})
c.Size.X = max(c.Size.X, x)
c.kidsiter(wo, kiargs{}, func(k *Sorm) {
k.p.X += (x - k.Size.X) * m.Size.X
k.ialign.X = m.Size.X
})
}
// Valign aligns elements vertically.
// If amt == 0, elements are aligned to the top, if 0.5 to the center and if 1 to the bottom.
// Values between those are acceptable.
//
// amt is clipped to the range 0 < amt < 1.
func (wo *World) Valign(amt float64) (s Sorm) {
s = wo.beginsorm()
s.tag = tagValign
s.Size.X = clamp(0, amt, 1)
wo.endsorm(s)
return
}
func valignrun(wo *World, c, m *Sorm) {
y := 0.0
c.kidsiter(wo, kiargs{}, func(k *Sorm) {
y = max(y, k.Size.Y)
})
c.Size.Y = max(c.Size.Y, y)
c.kidsiter(wo, kiargs{}, func(k *Sorm) {
k.p.Y += (y - k.Size.Y) * m.Size.X
k.ialign.Y = m.Size.X
})
}
// Strokewidth sets the fill paint.
func (wo *World) Fill(p nanovgo.Paint) (s Sorm) {
s = wo.beginsorm()
s.tag = tagFill
s.fill = p
wo.endsorm(s)
return
}
func fillrun(wo *World, s, m *Sorm) {
s.kidsiter(wo, kiargs{}, func(k *Sorm) {
if k.tag >= 0 {
k.fill = m.fill
}
})
}
// Strokewidth sets the stroke paint.
func (wo *World) Stroke(p nanovgo.Paint) (s Sorm) {
s = wo.beginsorm()
s.tag = tagStroke
s.stroke = p
wo.endsorm(s)
return
}
func strokerun(wo *World, s, m *Sorm) {
s.kidsiter(wo, kiargs{}, func(k *Sorm) {
if k.tag >= 0 {
k.stroke = m.stroke
}
})
}
// Strokewidth sets the stroke width.
func (wo *World) Strokewidth(w float64) (s Sorm) {
s = wo.beginsorm()
s.tag = tagStrokewidth
s.strokew = w
wo.endsorm(s)
return
}
func strokewidthrun(wo *World, s, m *Sorm) {
s.flags |= flagSetStrokewidth
s.kidsiter(wo, kiargs{}, func(k *Sorm) {
if k.tag >= 0 {
k.strokew = m.strokew
}
})
}
// Identity gives a compound the key on which it can be retrieved from the layout tree on the
// next event loop cycle.
func (wo *World) Identity(key any) (s Sorm) {
s = wo.beginsorm()
s.tag = tagIdentity
s.key = key
wo.endsorm(s)
return
}
func identityrun(wo *World, s, m *Sorm) {
s.key = m.key
m.key = nil
}
// Cond adds an event callback to a compound.
func (wo *World) Cond(f func(m Matcher)) (s Sorm) {
s = wo.beginsorm()
s.tag = tagCond
s.cond = f
wo.endsorm(s)
return
}
func condrun(wo *World, s, m *Sorm) {
s.cond = m.cond
}
// CondFill adds an event callback to a Compound.
func (wo *World) CondFill(f func(geom.Rectangle) nanovgo.Paint) (s Sorm) {
s = wo.beginsorm()
s.tag = tagCondfill
s.condfill = f
wo.endsorm(s)
return
}
func condfillrun(wo *World, s, m *Sorm) {
s.condfill = m.condfill
}
func (wo *World) CondStroke(f func(geom.Rectangle) nanovgo.Paint) (s Sorm) {
s = wo.beginsorm()
s.tag = tagCondstroke
s.condstroke = f
wo.endsorm(s)
return
}
func condstrokerun(wo *World, s, m *Sorm) {
s.condstroke = m.condstroke
}
// Between adds a Sorm from given constructor between every other shape in a compound.
func (wo *World) Between(f func() Sorm) (s Sorm) {
s = wo.beginsorm()
s.tag = tagBetween
s.key = f
wo.endsorm(s)
return
}
func betweenrun(wo *World, s, m *Sorm) {
// Between is the special case and handled in Compound()
}
// BetweenVoid adds a Void between every other shape of a compound.
func (wo *World) BetweenVoid(w, h complex128) (s Sorm) {
return wo.Between(func() Sorm { return wo.Void(w, h) })
}
// Source marks area of current compound as a drag source.
// It uses compound's identity (set with Identity modifier) as a drag value.
func (wo *World) Source() (s Sorm) {
s = wo.beginsorm()
s.tag = tagSource
wo.endsorm(s)
return
}
func sourcerun(wo *World, s *Sorm, m *Sorm) {
s.flags |= flagSource
}
// Sink marks area of current compound as a drag sink.
// When program receives Release(1) event with mouse cursor inside a sink,
// it calls given function with the drag value.
func (wo *World) Sink(f func(drop any)) (s Sorm) {
s = wo.beginsorm()
s.tag = tagSink
s.sinkid = len(wo.sinks)
wo.sinks = append(wo.sinks, f)
wo.endsorm(s)
return
}
func sinkrun(wo *World, s *Sorm, m *Sorm) {
s.sinkid = m.sinkid
m.sinkid = 0
}
// Hshrink shrinks the horizontal size of a stretchy compound to the size of the
// children with the maximum known horizontal size.
func (wo *World) Hshrink() (s Sorm) {
s = wo.beginsorm()
s.tag = tagHshrink
wo.endsorm(s)
return
}
func hshrinkrun(wo *World, s *Sorm, m *Sorm) {
s.flags |= flagHshrink
}
// Vshrink works exactly like Hshrink, but for vertical sizes.
func (wo *World) Vshrink() (s Sorm) {
s = wo.beginsorm()
s.tag = tagVshrink
wo.endsorm(s)
return
}
func vshrinkrun(wo *World, s *Sorm, m *Sorm) {
s.flags |= flagVshrink
}
// Crop limits the painting area of a compound to its limit.
func (wo *World) Crop() (s Sorm) {
s = wo.beginsorm()
s.tag = tagCrop
wo.endsorm(s)
return
}
func croprun(wo *World, s *Sorm, m *Sorm) {
s.flags |= flagCrop
}
// Limit limits the maximum compound size to specified limits.
// If a given size is negative, it limits the corresponding size of a compound by
// the rules of negative units for shapes.
//
// TODO Imaginary limits.
func (wo *World) Limit(w, h float64) (s Sorm) {
s = wo.beginsorm()
s.tag = tagLimit
s.Size.X, s.Size.Y = w, h
wo.endsorm(s)
return
}
func limitrun(wo *World, s, m *Sorm) {
p := s.m.ApplyPt(geom.Pt(m.Size.X, m.Size.Y))
if m.Size.X > 0 {
m.Size.X = p.X
s.l.X = min(s.l.X, m.Size.X)
} else if m.Size.X < 0 {
s.eprops.X = -m.Size.X
}
if m.Size.Y > 0 {
m.Size.Y = p.Y
s.l.Y = min(s.l.Y, m.Size.Y)
} else if m.Size.Y < 0 {
s.eprops.Y = -m.Size.Y
}
}
// Posttransform applies transformation that only affects objects visually.
// It doesn't affect object sizes for layout.
func (wo *World) Posttransform(x, y float64) (s Sorm) {
s = wo.beginsorm()
s.tag = tagPosttransform
s.Size.X = x
s.Size.Y = y
wo.endsorm(s)
return
}
func posttransformrun(wo *World, c, m *Sorm) {
c.p.X += m.Size.X
c.p.Y += m.Size.Y
// Because moves are inherited in a separate pass
}
// Transform applies transformation that affects objects sizes for layout.
func (wo *World) Transform(m geom.Geom) (s Sorm) {
s = wo.beginsorm()
s.tag = tagTransform
s.m = m
wo.endsorm(s)
return
}
func transformrun(wo *World, c, m *Sorm) {
c.m = c.m.Mul(m.m)
// Matrices are cascaded in (*World).resolvepremods
}
type labelt struct {
value any
counter int
}
func (wo *World) Whereis(s Sorm) Sorm {
s.flags |= flagFindme
return s
}
// Key is a temporary key-value storage for on-screen state.
// The value is deleted if it had been not accessed for two frames.
func (wo *World) Key(k any) (v *any) {
mv, ok := wo.keys[k]
if !ok {
wo.keys[k] = &labelt{}
mv = wo.keys[k]
}
mv.counter = 2
v = &mv.value
return
}
func (wo *World) Noround() (s Sorm) {
s = wo.beginsorm()
s.tag = tagNoround
wo.endsorm(s)
return
}
func noroundrun(wo *World, c, m *Sorm) {
c.flags |= flagNoround
}
func (wo *World) Round() (s Sorm) {
s = wo.beginsorm()
s.tag = tagRound
wo.endsorm(s)
return
}
func roundrun(wo *World, c, m *Sorm) {
c.flags |= flagRound
}
func (wo *World) Hscroll(idx *Index, du float64) (s Sorm) {
// TODO Smooth scrolling, rate limiting/exponential decay.
s = wo.beginsorm()
s.tag = tagHscroll
s.idx = idx
wo.endsorm(s)
return
}
func hscrollrun(wo *World, c, m *Sorm) {
c.idx = m.idx
}
func (wo *World) Vscroll(idx *Index, du float64) (s Sorm) {
s = wo.beginsorm()
s.tag = tagVscroll
s.idx = idx
wo.endsorm(s)
return
}
func vscrollrun(wo *World, c, m *Sorm) {
// NOTE Vscroll is a premodifier, they are sorted by tag before execution.
if c.idx != nil && c.idx != m.idx {
panic(`contraption: different Indexes in Hscroll and Vscroll on the same compound (id ` + sprint(c.i) + `)`)
}
c.idx = m.idx
}