-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageElement.sj
76 lines (67 loc) · 2.05 KB
/
imageElement.sj
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
enum imageStretch (
fill
center
aspectRatio
)
imageElement #element (
image := 'image
margin := margin()
stretch := imageStretch.fill
_rect := rect()
_imageRenderer := empty'imageRenderer
getSize(maxSize : 'size) {
size(maxSize.w, maxSize.h)
}
getRect()'rect { _rect }
setRect(rect_ : 'rect)'void {
if _rect != rect_ {
_rect = rect_
_imageRenderer = empty'imageRenderer
}
void
}
render(scene : 'scene2d)'void {
if isEmpty(_imageRenderer) {
r := _rect.subtractMargin(margin)
switch stretch {
imageStretch.fill { }
imageStretch.center {
s : size(r.w, r.h)
finalSize : s.min(image.texture.size)
r = rect(
(r.w - finalSize.w) / 2
(r.h - finalSize.h) / 2
finalSize.w
finalSize.h
)
void
}
imageStretch.aspectRatio {
imageAspectRatio : image.texture.size.w as f32 / image.texture.size.h as f32
rectAspectRatio : r.w as f32 / r.h as f32
finalSize : if imageAspectRatio > rectAspectRatio {
size(r.w, (r.h as f32 / imageAspectRatio) as i32)
} else {
size((r.w as f32 * imageAspectRatio) as i32, r.h)
}
r = rect(
(r.w - finalSize.w) / 2
(r.h - finalSize.h) / 2
finalSize.w
finalSize.h
)
void
}
}
_imageRenderer = valid(imageRenderer(
image : image
rect : r
))
void
}
_imageRenderer?.render(scene)
}
fireMouseEvent(mouseEvent : 'mouseEvent) {
true
}
) { this }