This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathImageView.uno
91 lines (82 loc) · 3.01 KB
/
ImageView.uno
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
using Uno;
using Uno.Graphics;
using Fuse;
using Fuse.Resources;
using Fuse.Elements;
using Fuse.Triggers;
using WhileLoaded;
using WhileCacheLoaded;
public class ImageView: Element
{
HttpImageSource _source = new HttpImageSource();
public ImageSource Source
{
get { return _source; }
set
{
_source = value as HttpImageSource;
}
}
public String ImageUrl
{
get { return _source.Url; }
set
{
_source.Url = value;
if (value == "" || value == null)
{
// Cleanup state triggers
WhileLoaded.SetState(this, false);
WhileFailed.SetState(this, false, "");
WhileLoading.SetState(this, false);
}
else
{
// Check if image is already available
WhileCacheLoaded.SetState(this, _source.State == ImageSourceState.Ready);
}
}
}
public MemoryPolicy ImageMemoryPolicy
{
get { return _source.Policy; }
set { if (_source.Policy != value) _source.Policy = value; }
}
protected override void OnRooted()
{
base.OnRooted();
_source.Changed += OnSourceChanged;
}
protected override void OnUnrooted()
{
_source.Changed -= OnSourceChanged;
_source.Url = null; // abort pending Http request
base.OnUnrooted();
}
protected override VisualBounds CalcRenderBounds()
{
return base.CalcRenderBounds().AddRect(float2(0), ActualSize);
}
void OnSourceChanged(object Source, object Args)
{
if (_source.State == ImageSourceState.Ready) UpdateManager.AddDeferredAction(InvalidateVisual);
WhileLoaded.SetState(this, _source.State == ImageSourceState.Ready);
WhileFailed.SetState(this, _source.State == ImageSourceState.Failed, "Image loading error");
WhileLoading.SetState(this, _source.State == ImageSourceState.Loading);
}
protected override void OnDraw(DrawContext dc)
{
texture2D tex = _source.GetTexture();
if (tex != null)
draw
{
apply Fuse.Drawing.Planar.Image;
DrawContext: dc;
Visual: this;
Size: ActualSize;
Texture: tex;
BlendEnabled: false;
DepthTestEnabled: false;
};
}
}