-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBlockLinkChecker.java
More file actions
296 lines (265 loc) · 12 KB
/
Copy pathBlockLinkChecker.java
File metadata and controls
296 lines (265 loc) · 12 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblocks;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import openblocks.renderable.RenderableBlock;
import openblocks.workspace.Workspace;
import openblocks.workspace.WorkspaceListener;
/**
* <code>BlockLinkChecker</code> determines if two <code>Block</code> objects can connect. In particular,
* <code>BlockLinkChecker</code> will report which sockets of the two <code>Block</code> objects can connect.
* Interested <code>Block</code> objects may make a static call to canLink() to determine if it can link to another
* <code>Block</code> object.
*
* <code>BlockLinkChecker</code> uses a list of <code>LinkRule</code>s to check the <code>Connector</code>s of each
* <code>Block</code>. Rules may be added, inserted, and removed from the checker.
*
* There is only one instance of the <code>BlockLinkChecker</code>.
*/
public class BlockLinkChecker {
private static ArrayList<LinkRule> rules = new ArrayList<LinkRule>();
// TODO get a better value
private static double MAX_LINK_DISTANCE = 20.0;
/**
* Clears all the rules within this.
*/
public static void reset(){
rules.clear();
}
/**
* Adds a rule to the end of this checker's list of rules.
* If the rule already exists in the rule list, the rule is removed in the original location and
* added to the end of the list.
* @param rule the desired LinkRule to be added
*/
public static void addRule(LinkRule rule){
rules.add(rule);
if (rule instanceof WorkspaceListener)
Workspace.getInstance().addWorkspaceListener((WorkspaceListener)rule);
}
/**
* Insert rule at the specified index in this checker's list of rules. The original rule at the
* specified index and rules after it are shifted down the list. If the index is greater
* or equal to the length of the rule list, then the rule is added to the end of the list.
* If the rule already exists in the rule list, the rule is moved to the specified index.
* @param rule the desired rule to insert
* @param index the index to insert the rule in
*/
public static void insertRule(LinkRule rule, int index){
rules.remove(rule);
rules.add(index, rule);
}
/**
* Removes the specified rule from the rule list
* @param rule the desired LinkRule to remove
*/
public static void removeRule(LinkRule rule){
rules.remove(rule);
}
/**
* Returns a BlockLink instance if the two specified blocks can connect at the specified
* block connectors at each block; null if no link is possible.
* @param block1 Block instance to compare
* @param block2 Block instance to compare
* @param con1 the BlockConnector at block1 to compare against con2
* @param con2 the BlockConnector at block2 to compare against con1
*/
public static BlockLink canLink(Block block1, Block block2, BlockConnector con1, BlockConnector con2){
if(checkRules(block1, block2, con1, con2))
return BlockLink.getBlockLink(block1, block2, con1, con2);
return null;
}
/**
* Checks to see if a <code>RenderableBlock</code>s can connect to other
* <code>RenderableBlock</code>s. This would mean that they have
* <code>BlockConnector</code>s that satisfy at least one of the <code>LinkRule</code>s,
* and that these sockets are in close proximity.
* @param rblock1 one of the blocks to check
* @param otherBlocks the other blocks to check against
* @return a <code>BlockLink</code> object that gives the two closest matching
* <code>BlockConnector</code>s in these blocks, or null if no such matching exists.\
*/
public static BlockLink getLink(RenderableBlock rblock1, Iterable<RenderableBlock> otherBlocks) {
if (rblock1.isCollapsed()) {
return null; // Collapsed blocks can't link.
}
Block block1 = Block.getBlock(rblock1.getBlockID());
BlockConnector closestSocket1 = null;
BlockConnector closestSocket2 = null;
Block closestBlock2 = null;
double closestDistance = MAX_LINK_DISTANCE;
for (RenderableBlock rblock2 : otherBlocks) {
BlockConnector currentPlug = getPlugEquivalent(block1);
Block block2 = Block.getBlock(rblock2.getBlockID());
if (block1.equals(block2) || !rblock1.isVisible() || !rblock2.isVisible()
|| rblock1.isCollapsed() || rblock2.isCollapsed())
continue;
Point2D currentPlugPoint = null;
Point2D currentSocketPoint = null;
if (currentPlug != null) {
currentPlugPoint = getAbsoluteSocketPoint(rblock1, currentPlug);
for (BlockConnector currentSocket : getSocketEquivalents(block2)) {
currentSocketPoint = getAbsoluteSocketPoint(rblock2, currentSocket);
double currentDistance = currentPlugPoint.distance(currentSocketPoint);
if ((currentDistance < closestDistance) && checkRules(block1, block2, currentPlug, currentSocket)) {
closestBlock2 = block2;
closestSocket1 = currentPlug;
closestSocket2 = currentSocket;
closestDistance = currentDistance;
}
}
}
currentPlug = getPlugEquivalent(block2);
if (currentPlug != null) {
currentPlugPoint = getAbsoluteSocketPoint(rblock2, currentPlug);
for (BlockConnector currentSocket : getSocketEquivalents(block1)) {
currentSocketPoint = getAbsoluteSocketPoint(rblock1, currentSocket);
double currentDistance = currentPlugPoint.distance(currentSocketPoint);
if ((currentDistance < closestDistance) && checkRules(block1, block2, currentSocket, currentPlug)) {
closestBlock2 = block2;
closestSocket1 = currentSocket;
closestSocket2 = currentPlug;
closestDistance = currentDistance;
}
}
}
}
if (closestSocket1 == null) {
return null;
}
return BlockLink.getBlockLink(block1, closestBlock2, closestSocket1, closestSocket2);
}
/**
* NOTE: ALWAYS prefer BlockLinkChecker.getLink over this method.
*
* Checks to see if a <code>RenderableBlock</code>s can connect
* to other <code>RenderableBlock</code>s, implying that rblock1
* has at least one <code>BlockConnector</code>s that satisfies at
* least one of the <code>LinkRule</code>s.
*
* Does not require close proximity.
*
* @param rblock1 one of the blocks to check
* @param otherBlocks the other blocks to check against
* @return a <code>BlockLink</code> object that gives the two closest matching <code>BlockConnector</code>s in these blocks,
* or null if no such matching exists.
*/
public static BlockLink getWeakLink(RenderableBlock rblock1, Iterable<RenderableBlock> otherBlocks) {
Block block1 = Block.getBlock(rblock1.getBlockID());
BlockConnector closestSocket1 = null;
BlockConnector closestSocket2 = null;
Block closestBlock2 = null;
double closestDistance = Double.POSITIVE_INFINITY;
double currentDistance;
for (RenderableBlock rblock2 : otherBlocks) {
BlockConnector currentPlug = getPlugEquivalent(block1);
Block block2 = Block.getBlock(rblock2.getBlockID());
if (block1.equals(block2) || !rblock1.isVisible() || !rblock2.isVisible())
continue;
Point2D currentPlugPoint = null;
Point2D currentSocketPoint = null;
if (currentPlug != null) {
currentPlugPoint = getAbsoluteSocketPoint(rblock1, currentPlug);
for (BlockConnector currentSocket : getSocketEquivalents(block2)) {
currentSocketPoint = getAbsoluteSocketPoint(rblock2, currentSocket);
currentDistance = currentPlugPoint.distance(currentSocketPoint);
if ((currentDistance < closestDistance) && checkRules(block1, block2, currentPlug, currentSocket)) {
closestBlock2 = block2;
closestSocket1 = currentPlug;
closestSocket2 = currentSocket;
closestDistance = currentDistance;
}
}
}
currentPlug = getPlugEquivalent(block2);
if (currentPlug != null) {
currentPlugPoint = getAbsoluteSocketPoint(rblock2, currentPlug);
for (BlockConnector currentSocket : getSocketEquivalents(block1)) {
currentSocketPoint = getAbsoluteSocketPoint(rblock1, currentSocket);
currentDistance = currentPlugPoint.distance(currentSocketPoint);
if ((currentDistance < closestDistance) && checkRules(block1, block2, currentSocket, currentPlug)) {
closestBlock2 = block2;
closestSocket1 = currentSocket;
closestSocket2 = currentPlug;
closestDistance = currentDistance;
}
}
}
}
if (closestSocket1 == null) {
return null;
}
return BlockLink.getBlockLink(block1, closestBlock2, closestSocket1, closestSocket2);
}
/**
* Checks if a potential link satisfies ANY of the rules loaded into the link checker
* @param block1 one Block in the potential link
* @param block2 the other Block
* @param socket1 the BlockConnector from block1 in the potential link
* @param socket2 the BlockConnector from block2
* @return true if the pairing of block1 and block2 at socket1 and socket2 passes any rules, false otherwise
*/
private static boolean checkRules(Block block1, Block block2, BlockConnector socket1, BlockConnector socket2) {
Iterator<LinkRule> rulesList = Collections.unmodifiableList(rules).iterator();
LinkRule currentRule = null;
boolean foundRule = false;
while(rulesList.hasNext()) {
currentRule = rulesList.next();
boolean canLink = currentRule.canLink(block1, block2, socket1, socket2);
if (!currentRule.isMandatory())
foundRule |= canLink;
else if (!canLink)
return false;
}
return foundRule;
}
/**
* Gets the screen coordinate of the center of a socket.
* @param block the RenderableBlock containting the socket
* @param socket the desired socket
* @return a Point2D that represents the center of the socket on the screen.
*/
private static Point2D getAbsoluteSocketPoint(RenderableBlock block, BlockConnector socket) {
Point2D relativePoint = block.getSocketPixelPoint(socket);
Point2D blockPosition = block.getLocationOnScreen();
return new Point2D.Double(relativePoint.getX() + blockPosition.getX(), relativePoint.getY() + blockPosition.getY());
}
public static boolean hasPlugEquivalent(Block b) {
if (b == null)
return false;
boolean hasPlug = b.hasPlug();
boolean hasBefore = b.hasBeforeConnector();
// Should have at most one plug-type connector
assert(!(hasPlug & hasBefore));
return hasPlug | hasBefore;
}
public static BlockConnector getPlugEquivalent(Block b) {
if (!hasPlugEquivalent(b))
return null;
if (b.hasPlug())
return b.getPlug();
return b.getBeforeConnector();
}
public static Iterable<BlockConnector> getSocketEquivalents(Block b) {
if (b == null)
return new ArrayList<BlockConnector>();
if (!b.hasAfterConnector())
return b.getSockets();
ArrayList<BlockConnector> socketEquivalents = new ArrayList<BlockConnector>();
for (BlockConnector socket : b.getSockets()) {
socketEquivalents.add(socket);
}
socketEquivalents.add(b.getAfterConnector());
return Collections.unmodifiableList(socketEquivalents);
}
/**
* Prints to the console all the rules this LinkChecker currently supports.
*/
public static void printRules(){
}
}