Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2498 | - | 1 | |
2 | namespace GMap.NET.Internals |
||
3 | { |
||
4 | using System; |
||
5 | using System.Collections.Generic; |
||
6 | using System.Diagnostics; |
||
7 | using System.Threading; |
||
8 | |||
9 | /// <summary> |
||
10 | /// represent tile |
||
11 | /// </summary> |
||
12 | public struct Tile : IDisposable |
||
13 | { |
||
14 | public static readonly Tile Empty = new Tile(); |
||
15 | |||
16 | GPoint pos; |
||
17 | int zoom; |
||
18 | PureImage[] overlays; |
||
19 | long OverlaysCount; |
||
20 | |||
21 | public readonly bool NotEmpty; |
||
22 | |||
23 | public Tile(int zoom, GPoint pos) |
||
24 | { |
||
25 | this.NotEmpty = true; |
||
26 | this.zoom = zoom; |
||
27 | this.pos = pos; |
||
28 | this.overlays = null; |
||
29 | this.OverlaysCount = 0; |
||
30 | } |
||
31 | |||
32 | public IEnumerable<PureImage> Overlays |
||
33 | { |
||
34 | get |
||
35 | { |
||
36 | #if PocketPC |
||
37 | for (long i = 0, size = OverlaysCount; i < size; i++) |
||
38 | #else |
||
39 | for (long i = 0, size = Interlocked.Read(ref OverlaysCount); i < size; i++) |
||
40 | #endif |
||
41 | { |
||
42 | yield return overlays[i]; |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | |||
47 | internal void AddOverlay(PureImage i) |
||
48 | { |
||
49 | if (overlays == null) |
||
50 | { |
||
51 | overlays = new PureImage[4]; |
||
52 | } |
||
53 | #if !PocketPC |
||
54 | overlays[Interlocked.Increment(ref OverlaysCount) - 1] = i; |
||
55 | #else |
||
56 | overlays[++OverlaysCount - 1] = i; |
||
57 | #endif |
||
58 | } |
||
59 | |||
60 | internal bool HasAnyOverlays |
||
61 | { |
||
62 | get |
||
63 | { |
||
64 | #if PocketPC |
||
65 | return OverlaysCount > 0; |
||
66 | #else |
||
67 | return Interlocked.Read(ref OverlaysCount) > 0; |
||
68 | #endif |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public int Zoom |
||
73 | { |
||
74 | get |
||
75 | { |
||
76 | return zoom; |
||
77 | } |
||
78 | private set |
||
79 | { |
||
80 | zoom = value; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | public GPoint Pos |
||
85 | { |
||
86 | get |
||
87 | { |
||
88 | return pos; |
||
89 | } |
||
90 | private set |
||
91 | { |
||
92 | pos = value; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | #region IDisposable Members |
||
97 | |||
98 | public void Dispose() |
||
99 | { |
||
100 | if (overlays != null) |
||
101 | { |
||
102 | #if PocketPC |
||
103 | for (long i = OverlaysCount - 1; i >= 0; i--) |
||
104 | |||
105 | #else |
||
106 | for (long i = Interlocked.Read(ref OverlaysCount) - 1; i >= 0; i--) |
||
107 | #endif |
||
108 | { |
||
109 | #if !PocketPC |
||
110 | Interlocked.Decrement(ref OverlaysCount); |
||
111 | #else |
||
112 | OverlaysCount--; |
||
113 | #endif |
||
114 | overlays[i].Dispose(); |
||
115 | overlays[i] = null; |
||
116 | } |
||
117 | overlays = null; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | #endregion |
||
122 | |||
123 | public static bool operator ==(Tile m1, Tile m2) |
||
124 | { |
||
125 | return m1.pos == m2.pos && m1.zoom == m2.zoom; |
||
126 | } |
||
127 | |||
128 | public static bool operator !=(Tile m1, Tile m2) |
||
129 | { |
||
130 | return !(m1 == m2); |
||
131 | } |
||
132 | |||
133 | public override bool Equals(object obj) |
||
134 | { |
||
135 | if (!(obj is Tile)) |
||
136 | return false; |
||
137 | |||
138 | Tile comp = (Tile)obj; |
||
139 | return comp.Zoom == this.Zoom && comp.Pos == this.Pos; |
||
140 | } |
||
141 | |||
142 | public override int GetHashCode() |
||
143 | { |
||
144 | return zoom ^ pos.GetHashCode(); |
||
145 | } |
||
146 | } |
||
147 | } |