-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCouncilNode.as
436 lines (386 loc) · 15.7 KB
/
CouncilNode.as
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import controls.*;
public class CouncilNode {
public static const RADIUS:uint = 171;
public static const RADIUS2:uint = 416;
public static const R_SRC0:uint = 134;//RADIUS - 30;
public static const R_SRC1:uint = R_SRC0 - 6;
public static const R_SPEECH_SRC:uint = 150;
public static const R_SPEECH_DST:uint = 200;
public static const R_DST0:uint = RADIUS + 40;
public static const R_DST1:uint = R_DST0 + 8;
public static const SPEECH_BUBBLE_OFFSET_X:int = -6;
public static const SPEECH_BUBBLE_OFFSET_Y:int = -21;
public static var hoveredNode:CouncilNode = null;
public static var selectedNode:CouncilNode = null;
public static const colorSchemesByCategoryId:Array = [
{zebra0: 0xFFF0DB, zebra1: 0xFFEBCF, selected: 0x925517, related: 0xD6AD60 }, // yellow
{zebra0: 0xF7DAE0, zebra1: 0xEEC8D0, selected: 0x740728, related: 0xED346B }, // red
{zebra0: 0xDFF5F8, zebra1: 0xD4F1F6, selected: 0x005E70, related: 0x25B5D5 }, // blue
{zebra0: 0xD0E7E4, zebra1: 0xC1E0DC, selected: 0x006E66, related: 0x45BDAB }, // green
{zebra0: 0xFFDDD0, zebra1: 0xF9CDBB, selected: 0x8A200A, related: 0xF25B38 } ]; // orange
private static var defaultEdgeColor:uint = 0xC0C0C0;
//private static var unrelatedEdgeColor:uint = 0xA0A0A0;
private static var mutualArrowColor:uint = 0xFFFFFF;
public static function setHoveredNode(node:CouncilNode):void {
if (node == CouncilNode.hoveredNode) { return; }
var i:uint;
var prevHoveredNode:CouncilNode = CouncilNode.hoveredNode;
CouncilNode.hoveredNode = node;
var otherNodeName:String;
var otherNode:CouncilNode;
if (prevHoveredNode != null) {
prevHoveredNode.drawNode();
for (i = 0; i < WEF.instance.nodes.length; i++) { // set all nodes to unrelated
WEF.instance.nodes[i].setRelated(0);
}
}
if (node != null) {
node.drawNode();
for (i = 0; i < node.outboundData.length; i++) {
otherNodeName = node.outboundData[i].token;
otherNode = WEF.instance.nodesByName[otherNodeName];
otherNode.setRelated(otherNode.isRelated + node.outboundData[i].score);
}
for (i = 0; i < WEF.instance.nodes.length; i++) {
otherNode = WEF.instance.nodes[i];
var value:Number = otherNode.hasEdgeTo(node);
otherNode.setRelated(otherNode.isRelated + value);
}
}
WEF.instance.updateEdges();
}
public static var mode:String = "_Countries";
public static function toggleMode():void {
mode = (mode == "_Countries" ? "_Orgs" : "_Countries");
}
public static function setSelectedNode(node:CouncilNode):void {
//if (node == CouncilNode.selectedNode) { return; } // no-op
if (node == CouncilNode.selectedNode) { node = null; } // no-op
var i:uint;
var prevSelectedNode:CouncilNode = CouncilNode.selectedNode;
CouncilNode.selectedNode = node;
var otherNodeName:String;
var otherNode:CouncilNode;
if (prevSelectedNode != null) {
prevSelectedNode.drawNode();
for (i = 0; i < WEF.instance.nodes.length; i++) {
WEF.instance.nodes[i].setRelated(0);
}
}
WEF.instance.clearSecondaryCenter();
if (WEF.instance.secondaryLayer.numChildren) {
WEF.instance.secondaryLayer.removeChildAt(0);
}
WEF.instance.backButtonSprite.visible = true;//(node != null);
if (node != null) {
node.drawNode();
for (i = 0; i < node.outboundData.length; i++) {
otherNodeName = node.outboundData[i].token;
otherNode = WEF.instance.nodesByName[otherNodeName];
otherNode.setRelated(0.1);
}
for (i = 0; i < WEF.instance.nodes.length; i++) {
otherNode = WEF.instance.nodes[i];
var value:Number = otherNode.hasEdgeTo(node);
otherNode.setRelated(otherNode.isRelated + value);
}
WEF.instance.drawSecondaryCenter(colorSchemesByCategoryId[node.categoryId].selected);
mode = "_Countries";
var circles:Circles = new Circles();
var circle:DisplayObject = new (circles[CouncilNode.selectedNode.token + mode])() as DisplayObject;
WEF.instance.secondaryLayer.addChild(circle);
}
}
///////////////////////////////////////////////////////////////////////////////
private var categoryId:uint;
public var token:String;
private var fullName:String;
private var outboundData:Array;
private var id:uint;
private var theta:Number;
private var isRelated:Number = 0;
// visuals
private var nodeRoot:Sprite;
private var nodeShape:Shape;
private var nameLabelContain:Sprite;
private var nameLabel:Label;
private var edges:Array = [];
private var srcSpeechBubble:Sprite = null;
private var dstSpeechBubble:Sprite = null;
public function CouncilNode(categoryId:uint, nodeInfo:Object) {
this.categoryId = categoryId;
this.token = nodeInfo.token;
this.fullName = nodeInfo.name;
this.outboundData = nodeInfo.outbound;
this.nodeRoot = new Sprite();
this.nodeShape = new Shape();
this.nodeRoot.addChild(this.nodeShape);
this.nodeRoot.buttonMode = true;
this.nodeRoot.useHandCursor = true;
this.nodeRoot.addEventListener(MouseEvent.MOUSE_OVER, this.onNodeHover);
this.nodeRoot.addEventListener(MouseEvent.MOUSE_OUT, this.onNodeUnhover);
this.nodeRoot.addEventListener(MouseEvent.CLICK, this.onClickNode);
WEF.instance.nodeLayer.addChild(this.nodeRoot);
this.nameLabelContain = new Sprite();
this.nameLabel = new Label(this.fullName, {'font': 'AndikaBasic', 'size': 13, 'width': 200, 'cursor': "pointer"});
this.nameLabelContain.buttonMode = true;
this.nameLabelContain.useHandCursor = true;
this.nameLabelContain.addChild(this.nameLabel);
this.nameLabel.y = - this.nameLabel.height / 2;
WEF.instance.textLayer.addChild(this.nameLabelContain);
this.nameLabel.addEventListener(MouseEvent.MOUSE_OVER, this.onNodeHover);
this.nameLabel.addEventListener(MouseEvent.MOUSE_OUT, this.onNodeHover);
this.nameLabel.addEventListener(MouseEvent.CLICK, this.onClickNode);
}
// accessors
public function getX():Number { return RADIUS * Math.cos(this.theta); }
public function getY():Number { return RADIUS * Math.sin(this.theta); }
public function hasEdgeTo(other:CouncilNode):Number {
for (var i:uint = 0; i < outboundData.length; i++) {
var outboundDatum:Object = outboundData[i];
if (outboundDatum.token == other.token) { return outboundDatum.score; }
}
return 0;
}
// mutators
public function setId(id:uint):void {
this.id = id;
this.theta = id * WEF.twoPi / WEF.instance.nodes.length;
while (Math.abs(this.theta) > Math.PI + 0.01) {
this.theta -= (this.theta > 0 ? WEF.twoPi : -WEF.twoPi);
}
if (-WEF.piOverTwo < theta && theta < WEF.piOverTwo) {
this.nameLabelContain.x = this.getX();
this.nameLabelContain.y = this.getY();
this.nameLabelContain.rotation = theta * 180 / Math.PI;
this.nameLabel.align = TextFormatAlign.RIGHT;
}
else {
this.nameLabelContain.x = this.getX() * ((RADIUS + 200) / RADIUS);
this.nameLabelContain.y = this.getY() * ((RADIUS + 200) / RADIUS);
this.nameLabelContain.rotation = (theta + Math.PI) * 180 / Math.PI;
this.nameLabel.align = TextFormatAlign.LEFT;
}
this.drawNode();
}
public function setRelated(value:Number):void {
this.isRelated = value;
this.drawNode();
}
// event handlers
private function onNodeHover(e:MouseEvent):void {
if (CouncilNode.selectedNode != null) { return; }
CouncilNode.setHoveredNode(this);
}
private function onNodeUnhover(e:MouseEvent):void {
if (CouncilNode.selectedNode != null) { return; }
var relatedObject:DisplayObject = e.relatedObject as DisplayObject;
if (this.nodeRoot.contains(relatedObject) || this.nameLabelContain.contains(relatedObject)) {
return;
}
if (CouncilNode.hoveredNode == this) {
CouncilNode.setHoveredNode(null);
}
}
private function onClickNode(e:MouseEvent):void {
CouncilNode.setHoveredNode(null);
CouncilNode.setSelectedNode(this);
// oh snap
}
// graphics
public function drawNode():void {
var dThetaOver2:Number = WEF.twoPi / WEF.instance.nodes.length / 2;
var angle1:Number = this.theta - dThetaOver2;
var angle2:Number = this.theta;
var angle3:Number = this.theta + dThetaOver2;
var innerRadius:Number = RADIUS;
var outerRadius:Number = RADIUS2;
var OUTERRADIUS:Number = outerRadius / Math.cos(dThetaOver2);
var originalColor:uint = colorSchemesByCategoryId[this.categoryId]["zebra" + (this.id % 2 ? "0":"1")];
var color:uint;
if (this == CouncilNode.hoveredNode || this == CouncilNode.selectedNode) {
color = colorSchemesByCategoryId[this.categoryId].selected;
}
else if (this.isRelated > 0) {
color = colorSchemesByCategoryId[this.categoryId].related;
}
else {
/*var r1:uint = (color >> 16) % 256;
var g1:uint = (color >> 8) % 256;
var b1:uint = (color ) % 256;
var r2:uint = (originalColor >> 16) % 256;
var g2:uint = (originalColor >> 8) % 256;
var b2:uint = (originalColor ) % 256;
var score:Number = this.isRelated;
if (score < 0.01) { score = 0.01; }
if (score > 0.99) { score = 0.99; }
r1 = int(r1*score + r2*(1 - score));
g1 = int(g1*score + g2*(1 - score));
b1 = int(b1*score + b2*(1 - score));
color = (r1 << 16) + (g1 << 8) + (b1);*/
color = originalColor;
}
var nodeGfx:Graphics = this.nodeShape.graphics;
nodeGfx.clear();
//nodeGfx.lineStyle(1, 0x000000);
nodeGfx.moveTo (innerRadius * Math.cos(angle1), innerRadius * Math.sin(angle1));
nodeGfx.beginFill(color);
nodeGfx.lineTo (outerRadius * Math.cos(angle1), outerRadius * Math.sin(angle1));
nodeGfx.curveTo(OUTERRADIUS * Math.cos(angle2), OUTERRADIUS * Math.sin(angle2),
outerRadius * Math.cos(angle3), outerRadius * Math.sin(angle3));
nodeGfx.lineTo (innerRadius * Math.cos(angle3), innerRadius * Math.sin(angle3));
nodeGfx.endFill();
if (CouncilNode.selectedNode != null) {
var toSelected:Boolean = (this.hasEdgeTo(CouncilNode.selectedNode) > 0);
var fromSelected:Boolean = (CouncilNode.selectedNode.hasEdgeTo(this) > 0);
var smallRadius:Number;
var SMALLRADIUS:Number;
if (toSelected && !fromSelected) {
nodeGfx.moveTo (innerRadius * Math.cos(angle1), innerRadius * Math.sin(angle1));
nodeGfx.beginFill(color);
nodeGfx.lineTo (R_SRC0 * Math.cos(angle1), R_SRC0 * Math.sin(angle1));
nodeGfx.lineTo (R_SRC1 * Math.cos(angle2), R_SRC1 * Math.sin(angle2));
nodeGfx.lineTo (R_SRC0 * Math.cos(angle3), R_SRC0 * Math.sin(angle3));
nodeGfx.lineTo (innerRadius * Math.cos(angle3), innerRadius * Math.sin(angle3));
nodeGfx.endFill();
}
else if (fromSelected && !toSelected) {
color = colorSchemesByCategoryId[ CouncilNode.selectedNode.categoryId ].selected;
nodeGfx.moveTo (innerRadius * Math.cos(angle1), innerRadius * Math.sin(angle1));
nodeGfx.beginFill(color);
nodeGfx.lineTo (R_DST0 * Math.cos(angle1), R_DST0 * Math.sin(angle1));
nodeGfx.lineTo (R_DST1 * Math.cos(angle2), R_DST1 * Math.sin(angle2));
nodeGfx.lineTo (R_DST0 * Math.cos(angle3), R_DST0 * Math.sin(angle3));
nodeGfx.lineTo (innerRadius * Math.cos(angle3), innerRadius * Math.sin(angle3));
nodeGfx.endFill();
}
else if (toSelected && fromSelected) {
color = mutualArrowColor;
nodeGfx.moveTo (R_SRC0 * Math.cos(angle1), R_SRC0 * Math.sin(angle1));
nodeGfx.beginFill(color);
nodeGfx.lineTo (R_SRC1 * Math.cos(angle2), R_SRC1 * Math.sin(angle2));
nodeGfx.lineTo (R_SRC0 * Math.cos(angle3), R_SRC0 * Math.sin(angle3));
nodeGfx.lineTo (R_DST0 * Math.cos(angle3), R_DST0 * Math.sin(angle3));
nodeGfx.lineTo (R_DST1 * Math.cos(angle2), R_DST1 * Math.sin(angle2));
nodeGfx.lineTo (R_DST0 * Math.cos(angle1), R_DST0 * Math.sin(angle1));
nodeGfx.endFill();
}
// and now for the comments
var commentKey:String;
if (toSelected) {
commentKey = this.token + "-" + CouncilNode.selectedNode.token;
if (Data.data.comments.hasOwnProperty(commentKey)) { // actually have comment?
if (srcSpeechBubble == null) {
srcSpeechBubble = new Sprite();
srcSpeechBubble.addChild(WEF.instance.newSpeechBubble());
srcSpeechBubble.addEventListener(MouseEvent.MOUSE_OVER, this.onHoverSrcBubble);
srcSpeechBubble.addEventListener(MouseEvent.MOUSE_OUT, this.onUnhoverSrcBubble);
}
srcSpeechBubble.x = R_SPEECH_SRC * Math.cos(angle2);
srcSpeechBubble.y = R_SPEECH_SRC * Math.sin(angle2);
WEF.instance.commentLayer.addChild(srcSpeechBubble); // TODO: sort
srcSpeechBubble.name = commentKey;
}
}
else if (srcSpeechBubble != null) {
if (WEF.instance.commentLayer.contains(srcSpeechBubble)) {
WEF.instance.commentLayer.removeChild(srcSpeechBubble);
}
}
if (fromSelected) {
commentKey = CouncilNode.selectedNode.token + "-" + this.token;
if (Data.data.comments.hasOwnProperty(commentKey)) { // actually have comment?
if (dstSpeechBubble == null) {
dstSpeechBubble = new Sprite();
dstSpeechBubble.addChild(WEF.instance.newSpeechBubble());
dstSpeechBubble.addEventListener(MouseEvent.MOUSE_OVER, this.onHoverDstBubble);
dstSpeechBubble.addEventListener(MouseEvent.MOUSE_OUT, this.onUnhoverDstBubble);
}
dstSpeechBubble.x = R_SPEECH_DST * Math.cos(angle2);
dstSpeechBubble.y = R_SPEECH_DST * Math.sin(angle2);
WEF.instance.commentLayer.addChild(dstSpeechBubble); // TODO: sort
dstSpeechBubble.name = commentKey;
}
}
else if (dstSpeechBubble != null) {
if (WEF.instance.commentLayer.contains(dstSpeechBubble)) {
WEF.instance.commentLayer.removeChild(dstSpeechBubble);
}
}
}
else {
if (srcSpeechBubble != null) {
if (WEF.instance.commentLayer.contains(srcSpeechBubble)) {
WEF.instance.commentLayer.removeChild(srcSpeechBubble);
}
}
if (dstSpeechBubble != null) {
if (WEF.instance.commentLayer.contains(dstSpeechBubble)) {
WEF.instance.commentLayer.removeChild(dstSpeechBubble);
}
}
}
}
private function onHoverSrcBubble(e:MouseEvent):void {
var comment:String = Data.data.comments[e.currentTarget.name];
WEF.instance.showComment(srcSpeechBubble.x, srcSpeechBubble.y, comment);
}
private function onHoverDstBubble(e:MouseEvent):void {
var comment:String = Data.data.comments[e.currentTarget.name];
WEF.instance.showComment(dstSpeechBubble.x, dstSpeechBubble.y, comment);
}
private function onUnhoverSrcBubble(e:MouseEvent):void {
WEF.instance.hideComment();
}
private function onUnhoverDstBubble(e:MouseEvent):void {
WEF.instance.hideComment();
}
public function updateOutboundEdges():void {
var i:int;
for (i = 0; i < edges.length; i++) {
WEF.instance.edgeLayer.removeChild(edges[i]);
}
edges = [];
for (i = 0; i < outboundData.length; i++) {
var outboundDatum:Object = outboundData[i];
var other:CouncilNode = WEF.instance.nodesByName[outboundDatum.token];
var color:uint;
if (CouncilNode.hoveredNode == null) {
if (outboundDatum.score < 0.6) { continue; } // enforce threshold for global view
color = defaultEdgeColor;
}
else if (CouncilNode.hoveredNode == this) {
color = colorSchemesByCategoryId[this.categoryId].selected;
}
else if (CouncilNode.hoveredNode == other) {
color = colorSchemesByCategoryId[this.categoryId].related;
}
else {
continue;
//color = unrelatedEdgeColor;
}
var edge:Shape = new Shape();
edge.graphics.clear();
edge.graphics.lineStyle(2, color, outboundDatum.score);
edge.graphics.moveTo(this.getX(), this.getY());
var dx:Number = other.getX() - this.getX();
var dy:Number = other.getY() - this.getY();
var ds:Number = Math.sqrt(dx*dx + dy*dy) / RADIUS;
var anchorX:Number = ((other.getX() + this.getX()) / 2.0 + 0*ds) / (1 + ds*10);
var anchorY:Number = ((other.getY() + this.getY()) / 2.0 + 0*ds) / (1 + ds*10);
edge.graphics.curveTo(anchorX, anchorY, other.getX(), other.getY());
/*if (color == unrelatedEdgeColor) { // should never need to make this distinction
WEF.instance.edgeLayer.addChildAt(edge, 0);
}
else {
WEF.instance.edgeLayer.addChild(edge);
}*/
WEF.instance.edgeLayer.addChild(edge);
edges.push(edge);
}
}
}}