-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathInfixBlockShape.java
More file actions
392 lines (319 loc) · 17.6 KB
/
Copy pathInfixBlockShape.java
File metadata and controls
392 lines (319 loc) · 17.6 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// -*- 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.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import openblocks.renderable.RenderableBlock;
import openblocks.codeblocks.rendering.BlockShapeUtil;
/**
* Specifies the BlockShape of infix blocks. Infix blocks have two bottom sockets.
*/
public class InfixBlockShape extends BlockShape{
/**
* In order to properly determine where the pen should be after drawing all the blocks,
* the variable
* maxX is used. This stores the maximum x-coordinate visited by the pen and is updated
* after each connected block is drawn. After the update, the pen is moved to the maximum
* x-coordinate visited. When finished drawing all connected blocks, maxX is reset to 0 to
* allow for resizing if some of the connected blocks are removed.
*/
private float maxX=0; //Stores the maximum X-coordinate visited by the infix block
public InfixBlockShape(RenderableBlock rb) {
super(rb, rb.getBlock());
}
/**
* Overrided from BlockShape.
* Takes into account the need to resize the dimensions of an infix block for various cases.
*/
@Override
protected void makeBottomSide(){
// Reset the maximum X-coordinate so the infix block can resize if you remove blocks
// within it
maxX = 0;
//start bottom-right
setEndPoint(gpBottom, botLeftCorner, topLeftCorner, true);
//curve down and right
BlockShapeUtil.cornerTo(gpBottom, botLeftCorner, botRightCorner, blockCornerRadius);
/// BOTTOM SOCKETS
//for each socket in the iterator
int socketCounter = 0; //need to use this to determine which socket we're on
for(BlockConnector curSocket : block.getSockets()) {
//if bottom socket
if (curSocket.getPositionType() == BlockConnector.PositionType.BOTTOM) {
//move away from bottom left corner
if(socketCounter > 0){
gpBottom.lineTo(
(float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_MIDDLE_SPACER,
(float) gpBottom.getCurrentPoint().getY());
}else{
gpBottom.lineTo(
(float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
(float) gpBottom.getCurrentPoint().getY());
}
//move down so bevel doesn't screw up from connecting infinitely sharp corner
// as occurs from a curved port
BlockShapeUtil.lineToRelative(gpBottom, 0, -0.1f);
//////////////////////
//begin drawing socket
//////////////////////
if(curSocket.getBlockID() == Block.NULL){
//draw first socket - up left side
Point2D leftSocket = BCS.addDataSocketUp(gpBottom, curSocket.getKind(), true);
rb.updateSocketPoint(curSocket, leftSocket);
//System.out.println("socket poitn: "+rb.getSocketPoint(curSocket));
//System.out.println("socket poitn leftsocket: "+leftSocket);
//draw left standard empty socket space - top side
gpBottom.lineTo(
(float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
(float) gpBottom.getCurrentPoint().getY());
// draw first socket - down right side
// old version used the block kind to make something symmertrical
// to the left side using this code:
// BCS.addDataSocket(gpBottom, curSocket.getKind(), false);
// rb.updateSocketPoint(curSocket, rightSocket);
// In this version, don't make a real socket on
// the right side and use this code instead.
BCS.addDataSocket(gpBottom, "infix-rhs", false);
} else { //there is a connected block
Block connectedBlock = Block.getBlock(curSocket.getBlockID());
RenderableBlock connectedRBlock =
RenderableBlock.getRenderableBlock(curSocket.getBlockID());
//calculate and update the new socket point
//update the socket point of this cursocket which should now adopt the plug
// socket point of its
//connected block since we're also adopting the left side of its shape
//Use coordinates when the zoom level is 1.0 to calculate socket point
double unzoomX =
connectedRBlock.getSocketPixelPoint(connectedBlock.getPlug()).getX() /
connectedRBlock.getZoom();
double unzoomY =
connectedRBlock.getSocketPixelPoint(connectedBlock.getPlug()).getY() /
connectedRBlock.getZoom();
Point2D connectedBlockSocketPoint = new Point2D.Double(unzoomX, unzoomY);
Point2D currentPoint = gpBottom.getCurrentPoint();
double newX =
connectedBlockSocketPoint.getX() +
Math.abs(connectedBlockSocketPoint.getX() - currentPoint.getX());
double newY =
connectedBlockSocketPoint.getY() +
Math.abs(connectedRBlock.getBlockHeight() /
connectedRBlock.getZoom() - currentPoint.getY());
rb.updateSocketPoint(curSocket, new Point2D.Double(newX, newY));
BlockShape connectedBlockShape =
RenderableBlock.getRenderableBlock(curSocket.getBlockID()).getBlockShape();
//append left side of connected block
appendPath(gpBottom, connectedBlockShape.getLeftSide(), false);
//append right side of connected block (more complicated)
if(connectedBlock.getNumSockets() == 0 || connectedBlock.isInfix()){
// append top side of connected block
appendPath(gpBottom, connectedBlockShape.getTopSide(), false);
appendPath(gpBottom, connectedBlockShape.getRightSide(), false);
}else{
//iterate through the sockets of the connected block, checking if
//it has blocks connected to them
appendRightSidePath(gpBottom, connectedBlock, connectedBlockShape);
}
// Updates the maximum X-coordinate and sets the current point to maxX
if(maxX < (float) gpBottom.getCurrentPoint().getX()){
maxX = (float) gpBottom.getCurrentPoint().getX();
}
gpBottom.lineTo(maxX, (float) gpBottom.getCurrentPoint().getY());
}
//bump down so bevel doesn't screw up
BlockShapeUtil.lineToRelative(gpBottom, 0, 0.1f);
//System.out.println("gpbottom starting point: "+gpBottom.getCurrentPoint());
//// draw RIGHT to create divider ////
if(socketCounter < block.getNumSockets()-1){
gpBottom.lineTo(
//need to add the width of the block label.
// warning: this assumes that there is only one block label
(float) gpBottom.getCurrentPoint().getX() +
BOTTOM_SOCKET_MIDDLE_SPACER + rb.accommodateLabelsWidth(),
(float) gpBottom.getCurrentPoint().getY());
}else{
gpBottom.lineTo(
(float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
(float) gpBottom.getCurrentPoint().getY());
}
socketCounter++;
}
}
//curve right and up
BlockShapeUtil.cornerTo(gpBottom, botRightCorner, topRightCorner, blockCornerRadius);
//end bottom
setEndPoint(gpBottom, botRightCorner, topRightCorner, false);
}
/**
* Appends the right side path of the stack of blocks connected to the specified
* connectedBlock. If there are
* some empty sockets, this method will append empty placeholders.
* @param gpBottom the GeneralPath to append the new path to
* @param connectedBlock the Block instance whose right side of its stack of connected blocks
* will be appened to the
* specified gpBottom
* @param connectedBlockShape the BlockShape of the specified connectedBlock
*/
private void appendRightSidePath(GeneralPath gpBottom, Block connectedBlock,
BlockShape connectedBlockShape){
//int lastBottomPathWidth;
//append top side of connected block
appendPath(gpBottom, connectedBlockShape.getTopSide(), false);
float startX = (float) gpBottom.getCurrentPoint().getX();
for(BlockConnector socket : connectedBlock.getSockets()){
// Sets the current x-coordinate to the start x-coordinate
// Makes it so path movements created by previous blocks don't affect
// the subsequent blocks.
gpBottom.lineTo(startX, (float) gpBottom.getCurrentPoint().getY());
if(socket.getBlockID() == Block.NULL){
//just draw an empty socket placeholder
//if its the first socket, draw a top side
gpBottom.lineTo(
(float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
(float) gpBottom.getCurrentPoint().getY());
// now draw the empty right socket side
// draw first socket - down right side
// old version used the block kind to make something symmertrical to the left side
// using this code:
// In this version, don't make a real socket on
// the right side and use this code instead.
// This is the same change as commented above, except to deal with the case where
// a connected block has been placed in infix argument socket
BCS.addDataSocket(gpBottom, "infix-rhs", false);
//BCS.addDataSocket(gpBottom, socket.getKind(), false);
//TODO:lastBottomPathWidth = (int)BOTTOM_SOCKET_SIDE_SPACER;
}else{
//a block is connected to this socket, check if that block has sockets
//OR if the block is an infix block - if it is infix,
// then just wrap around the infix block
Block block = Block.getBlock(socket.getBlockID());
BlockShape shape =
RenderableBlock.getRenderableBlock(socket.getBlockID()).getBlockShape();
if(block.getNumSockets() == 0 || block.isInfix()){
//append this block's top and right side
//TODO instead of just appending the right side...draw line to
appendPath(gpBottom, shape.getTopSide(), false);
appendPath(gpBottom, shape.getRightSide(), false);
}else{
appendRightSidePath(gpBottom, block, shape);
}
}
// Updates the maximum X-coordinate and sets the current point to maxX
if(maxX < (float) gpBottom.getCurrentPoint().getX()){
maxX = (float) gpBottom.getCurrentPoint().getX();
}
gpBottom.lineTo(maxX, (float) gpBottom.getCurrentPoint().getY());
}
}
/**
* Overrided from BlockShape.
* Determines the width of the sum of the bottom sockets and uses it if it is
* greater than the width determined by the determineBlockWidth in BlockShape.
* Else, it returns the sum of these two values.
*/
@Override
protected int determineBlockWidth(){
//System.out.println("determining block width");
int width = super.determineBlockWidth();
//if the sum of bottom sockets is greater than the calculated width, then use it
int bottomSocketWidth = 0;
for(BlockConnector socket : block.getSockets()) {
if (socket.getPositionType() == BlockConnector.PositionType.BOTTOM) {
if(socket.getBlockID() == Block.NULL){
//3 socket spacers = left of socket, between connectors, right of socket
bottomSocketWidth += BOTTOM_SOCKET_SIDE_SPACER ;
}else{ //a block is connected to socket
//TODO get their assigned width from rb
if (rb.getSocketSpaceDimension(socket) != null)
bottomSocketWidth += rb.getSocketSpaceDimension(socket).width;
bottomSocketWidth -= BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH;
// if it's a mirror plug, subtract for the other side, too.
if (Block.getBlock(socket.getBlockID()).getPlug().getPositionType() ==
BlockConnector.PositionType.MIRROR) {
bottomSocketWidth -= BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH;
}
}
}
}
bottomSocketWidth += 2 * BOTTOM_SOCKET_MIDDLE_SPACER;
//TODO need to decide for a size of the middle spacer and how to place them
bottomSocketWidth += 2 * BOTTOM_SOCKET_SIDE_SPACER;
if (bottomSocketWidth > width) return (bottomSocketWidth + rb.accommodateLabelsWidth());
width += bottomSocketWidth;
//make sure its even
if(width % 2 == 1) width++;
return width;
}
/** Append gp2 to gp1. If reversed == true, then add the segments in reverse order
* NOTE: copied and pasted from starlogoblocks/blockengine/BlockShape.java */
private void appendPath(GeneralPath gp1, GeneralPath gp2, boolean reversed)
{
ArrayList<Number[]> points = new ArrayList<Number[]>();
// Each element is an array consisting of one Integer and six Floats
PathIterator i = gp2.getPathIterator(new AffineTransform());
float[] segment = new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
float leftmost = Float.POSITIVE_INFINITY;
while(!i.isDone())
{
int type = i.currentSegment(segment);
i.next();
points.add(new Number[]
{
new Integer(type),
new Float(segment[0]),
new Float(segment[1]),
new Float(segment[2]),
new Float(segment[3]),
new Float(segment[4]),
new Float(segment[5])
});
}
if(!reversed)
{
float deltaX =
(float)gp1.getCurrentPoint().getX() - ((Float)points.get(0)[1]).floatValue();
float deltaY =
(float)gp1.getCurrentPoint().getY() - ((Float)points.get(0)[2]).floatValue();
for(int j = 1; j < points.size(); j++)
{
Object[] typeAndPoints = points.get(j);
int type = ((Integer)typeAndPoints[0]).intValue();
float x1 = ((Float)typeAndPoints[1]).floatValue();
float y1 = ((Float)typeAndPoints[2]).floatValue();
float x2 = ((Float)typeAndPoints[3]).floatValue();
float y2 = ((Float)typeAndPoints[4]).floatValue();
float x3 = ((Float)typeAndPoints[5]).floatValue();
float y3 = ((Float)typeAndPoints[6]).floatValue();
if(type == PathIterator.SEG_MOVETO)
{
}
else if(type == PathIterator.SEG_LINETO)
{
gp1.lineTo(x1 + deltaX, y1 + deltaY);
leftmost = Math.min(leftmost, x1 + deltaX);
}
else if(type == PathIterator.SEG_QUADTO)
{
gp1.quadTo(x1 + deltaX, y1 + deltaY, x2 + deltaX, y2 + deltaY);
leftmost = Math.min(leftmost, x2 + deltaX);
}
else if(type == PathIterator.SEG_CUBICTO)
{
gp1.curveTo(x1 + deltaX,
y1 + deltaY,
x2 + deltaX,
y2 + deltaY,
x3 + deltaX,
y3 + deltaY);
leftmost = Math.min(leftmost, x3 + deltaX);
}
else
assert false : type;
}
}
}
}