View Javadoc

1   package org.catacomb.graph.gui;
2   
3   
4   
5   // REFAC this all needs cleaning up a bit;
6   
7   
8   public class PickStore {
9   
10      int npoint;
11      Pickable[] points;
12      int[][] pointBoxes;
13  
14  
15      int nregion;
16      PickableRegion[] regions;
17      int[][] regionRefs;
18  
19  
20  
21      // sloppy buffering;
22      int xref = 0;
23      int yref = 0;
24  
25  
26      public PickStore() {
27          points = new Pickable[10];
28          pointBoxes = new int[10][4];
29  
30          regions = new PickableRegion[10];
31          regionRefs = new int[10][2];
32      }
33  
34  
35      public void clear() {
36          npoint = 0;
37          nregion = 0;
38      }
39  
40  
41      public void addPoint(PickablePoint pbl, int xc, int yc) {
42  
43          int hrng = (pbl.getRange()) / 2;
44  
45          int icx0 = xc - hrng;
46          int icy0 = yc - hrng;
47  
48          int icx1 = xc + hrng;
49          int icy1 = yc + hrng;
50          addPoint(pbl, icx0, icy0, icx1, icy1);
51      }
52  
53  
54      public void addPoint(Pickable pbl, int xa, int ya, int xb, int yb) {
55          if (npoint >= points.length) {
56              int nnew = (int)(1.5 * points.length);
57  
58              Pickable[] npb = new Pickable[nnew];
59              int[][] ab = new int[nnew][4];
60  
61              for (int i = 0; i < npoint; i++) {
62                  npb[i] = points[i];
63                  // more efficient? - keeps memory intact? TOOD
64                  int[] aab = ab[i];
65                  int[] apb = pointBoxes[i];
66                  aab[0] = apb[0];
67                  aab[1] = apb[1];
68                  aab[2] = apb[2];
69                  aab[3] = apb[3];
70              }
71              points = npb;
72              pointBoxes = ab;
73          }
74          points[npoint] = pbl;
75          int[] apb = pointBoxes[npoint];
76          apb[0] = xa;
77          apb[1] = ya;
78          apb[2] = xb;
79          apb[3] = yb;
80  
81          pbl.setCache(npoint);
82          npoint += 1;
83      }
84  
85  
86      public void addPickableRegion(PickableRegion pbl, int xr, int yr) {
87          if (nregion >= regions.length) {
88              int nnew = (int)(1.5 * regions.length + 10);
89              PickableRegion[] npb = new PickableRegion[nnew];
90              int[][] nrc = new int[nnew][2];
91  
92              for (int i = 0; i < nregion; i++) {
93                  npb[i] = regions[i];
94                  nrc[i][0] = regionRefs[i][0];
95                  nrc[i][1] = regionRefs[i][1];
96              }
97              regions = npb;
98              regionRefs = nrc;
99          }
100         regions[nregion] = pbl;
101         regionRefs[nregion][0] = xr;
102         regionRefs[nregion][1] = yr;
103         nregion += 1;
104 
105     }
106 
107 
108     private boolean within(int[] xyxy, int x, int y) {
109         return ((x > xyxy[0]) && (x < xyxy[2]) && (y > xyxy[1]) && (y < xyxy[3]));
110     }
111 
112 
113     public Pickable getClaimant(int mx, int my, double dx, double dy) {
114         Pickable ret = null;
115         for (int j = npoint - 1; j >= 0; j--) {
116             if (within(pointBoxes[j], mx, my)) {
117                 ret = points[j];
118 
119                 int[] pbox = pointBoxes[j];
120                 xref = (pbox[0] + pbox[2]) / 2;
121                 yref = (pbox[1] + pbox[3]) / 2;
122 
123                 break;
124             }
125         }
126 
127         if (ret == null) {
128             for (int j = nregion - 1; j >= 0; j--) {
129                 PickableRegion pr = regions[j];
130                 if (pr.contains(dx, dy)) {
131                     ret = pr;
132                     xref = regionRefs[j][0];
133                     yref = regionRefs[j][1];
134 
135                     break;
136                 }
137             }
138         }
139 
140 
141         return ret;
142     }
143 
144 
145     public int getClaimantRefX() {
146         return xref;
147     }
148 
149 
150     public int getClaimantRefY() {
151         return yref;
152     }
153 
154 
155     public boolean itemClaims(Pickable pbl, int mx, int my, double dx, double dy) {
156         boolean ret = false;
157         int ind = pbl.getCache();
158 
159 
160         if (pbl instanceof PickableRegion) {
161             if (((PickableRegion) pbl).contains(dx, dy)) {
162                 ret = true;
163             }
164         } else {
165             if (within(pointBoxes[ind], mx, my)) {
166                 ret = true;
167             }
168         }
169 
170 
171         return ret;
172     }
173 
174 
175     public int[] getEchoBox(PickablePoint pp) {
176         return pointBoxes[pp.getCache()];
177     }
178 
179 
180 
181 }