1
2
3
4
5
6
7
8 package org.xwt.util;
9
10 import java.util.*;
11
12
21 public class DirtyList {
22
23 public DirtyList() { }
24
25
26 private int[][] dirties = new int[10][];
27
28
29 private int numdirties = 0;
30
31
32 private static final int epsilon = 50 * 50;
33
34 public int num() { return numdirties; }
35
36
37 private void grow() {
38 int[][] newdirties = new int[dirties.length * 2][];
39 System.arraycopy(dirties, 0, newdirties, 0, numdirties);
40 dirties = newdirties;
41 }
42
43
47 public synchronized boolean dirty(int x, int y, int w, int h) {
48 if (numdirties == dirties.length) grow();
49
50
51 for(int i=0; i<numdirties; i++) {
52 int[] cur = dirties[i];
53
54
55 if (x >= cur[0] && y >= cur[1] && x + w <= cur[0] + cur[2] && y + h <= cur[1] + cur[3]) {
56 return false;
57
58
59 } else if (x <= cur[0] && y <= cur[1] && x + w >= cur[0] + cur[2] && y + h >= cur[1] + cur[3]) {
60 dirties[i][2] = 0;
61 dirties[i][3] = 0;
62
63
64 } else if (x >= cur[0] && x < cur[0] + cur[2] && y >= cur[1] && y + h <= cur[1] + cur[3]) {
65 w = x + w - (cur[0] + cur[2]);
66 x = cur[0] + cur[2];
67 i = -1; continue;
68
69
70 } else if (x + w > cur[0] && x + w <= cur[0] + cur[2] && y >= cur[1] && y + h <= cur[1] + cur[3]) {
71 w = cur[0] - x;
72 i = -1; continue;
73
74
75 } else if (x >= cur[0] && x + w <= cur[0] + cur[2] && y >= cur[1] && y < cur[1] + cur[3]) {
76 h = y + h - (cur[1] + cur[3]);
77 y = cur[1] + cur[3];
78 i = -1; continue;
79
80
81 } else if (x >= cur[0] && x + w <= cur[0] + cur[2] && y + h > cur[1] && y + h <= cur[1] + cur[3]) {
82 h = cur[1] - y;
83 i = -1; continue;
84
85
86 } else if (dirties[i][0] >= x && dirties[i][0] < x + w && dirties[i][1] >= y && dirties[i][1] + dirties[i][3] <= y + h) {
87 dirties[i][2] = dirties[i][2] - (x + w - dirties[i][0]);
88 dirties[i][0] = x + w;
89 i = -1; continue;
90
91
92 } else if (dirties[i][0] + dirties[i][2] > x && dirties[i][0] + dirties[i][2] <= x + w &&
93 dirties[i][1] >= y && dirties[i][1] + dirties[i][3] <= y + h) {
94 dirties[i][2] = x - dirties[i][0];
95 i = -1; continue;
96
97
98 } else if (dirties[i][0] >= x && dirties[i][0] + dirties[i][2] <= x + w && dirties[i][1] >= y && dirties[i][1] < y + h) {
99 dirties[i][3] = dirties[i][3] - (y + h - dirties[i][1]);
100 dirties[i][1] = y + h;
101 i = -1; continue;
102
103
104 } else if (dirties[i][0] >= x && dirties[i][0] + dirties[i][2] <= x + w &&
105 dirties[i][1] + dirties[i][3] > y && dirties[i][1] + dirties[i][3] <= y + h) {
106 dirties[i][3] = y - dirties[i][1];
107 i = -1; continue;
108 }
109
110 }
111
112
113 for(int i=0; i<numdirties; i++) {
114 int[] cur = dirties[i];
115 if (w > 0 && h > 0 && cur[2] > 0 && cur[3] > 0 &&
116 ((max(x + w, cur[0] + cur[2]) - min(x, cur[0])) *
117 (max(y + h, cur[1] + cur[3]) - min(y, cur[1])) <
118 w * h + cur[2] * cur[3] + epsilon)) {
119 int a = min(cur[0], x);
120 int b = min(cur[1], y);
121 int c = max(x + w, cur[0] + cur[2]) - min(cur[0], x);
122 int d = max(y + h, cur[1] + cur[3]) - min(cur[1], y);
123 dirties[i][2] = 0;
124 dirties[i][3] = 0;
125 return dirty(a, b, c, d);
126 }
127 }
128
129 dirties[numdirties++] = new int[] { x, y, w, h };
130 return true;
131 }
132
133
134 public boolean empty() { return (numdirties == 0); }
135
136
143 public synchronized int[][] flush() {
144 if (numdirties == 0) return null;
145 int[][] ret = dirties;
146 for(int i=numdirties; i<ret.length; i++) ret[i] = null;
147 dirties = new int[dirties.length][];
148 numdirties = 0;
149 return ret;
150 }
151
152
153 private static final int min(int a, int b) {
154 if (a<b) return a;
155 else return b;
156 }
157
158
159 private static final int max(int a, int b) {
160 if (a>b) return a;
161 else return b;
162 }
163
164
165 private static final int min(int a, int b, int c) {
166 if (a<=b && a<=c) return a;
167 else if (b<=c && b<=a) return b;
168 else return c;
169 }
170
171
172 private static final int max(int a, int b, int c) {
173 if (a>=b && a>=c) return a;
174 else if (b>=c && b>=a) return b;
175 else return c;
176 }
177
178
179 private static final int bound(int a, int b, int c) {
180 if (a > b) return a;
181 if (c < b) return c;
182 return b;
183 }
184
185 }
186